使用Raspberry Pi使用NRF24L01从Arduino接收浮点数

时间:2017-08-22 07:10:32

标签: python raspberry-pi3 arduino-uno

我一直在学习如何使用NRF24L01基于以下参考以无线方式将数据从Arduino传输到Raspberry Pi: Raspberry Pi 3 Tutorial 14 – Wireless Pi to Arduino Communication with NRF24L01+

我想这样做的原因是使用DHT22传感器无线记录温度和湿度数据。

Arduino代码如下所示:

//SendReceive.ino

#include<SPI.h>
#include<RF24.h>

// CE, CSN pins
RF24 radio(9, 10);

void setup(void){
    while(!Serial);
    Serial.begin(9600);

    radio.begin();
    radio.setPALevel(RF24_PA_MAX);
    radio.setChannel(0x76);
    radio.openWritingPipe(0xF0F0F0F0E1LL);
    const uint64_t pipe = (0xE8E8F0F0E1LL);
    radio.openReadingPipe(1, pipe);

    radio.enableDynamicPayloads();
    radio.powerUp();

}

void loop(void){
    radio.startListening();
    Serial.println("Starting loop. Radio on.");
    char receivedMessage[32] = {0};
    if(radio.available()){
        radio.read(receivedMessage, sizeof(receivedMessage));
        Serial.println(receivedMessage);
        Serial.println("Turning off the radio.");
        radio.stopListening();

        String stringMessage(receivedMessage);

        if(stringMessage == "GETSTRING"){
            Serial.println("Looks like they want a string!");
            const char text[] = "Yo wassup, haha";
            radio.write(text, sizeof(text));
            Serial.println("We sent our message.");
        }
    }
    delay(100);

}

同时,Raspberry Pi代码如下所示:

import RPi.GPIO as GPIO
from lib_nrf24 import NRF24
import time
import spidev

GPIO.setmode(GPIO.BCM)

pipes = [[0xE8, 0xE8, 0xF0, 0xF0, 0xE1], [0xF0, 0xF0, 0xF0, 0xF0, 0xE1]]

radio = NRF24(GPIO, spidev.SpiDev())
radio.begin(0, 17)

radio.setPayloadSize(32)
radio.setChannel(0x76)
radio.setDataRate(NRF24.BR_1MBPS)
radio.setPALevel(NRF24.PA_MIN)

radio.setAutoAck(True)
radio.enableDynamicPayloads()
radio.enableAckPayload()

radio.openWritingPipe(pipes[0])
radio.openReadingPipe(1, pipes[1])
radio.printDetails()
# radio.startListening()

message = list("GETSTRING")
while len(message) &lt; 32:
    message.append(0)

while(1):
    start = time.time()
    radio.write(message)
    print("Sent the message: {}".format(message))
    radio.startListening()

    while not radio.available(0):
        time.sleep(1 / 100)
        if time.time() - start &gt; 2:
            print("Timed out.")
            break

    receivedMessage = []
    radio.read(receivedMessage, radio.getDynamicPayloadSize())
    print("Received: {}".format(receivedMessage))

    print("Translating the receivedMessage into unicode characters")
    string = ""
    for n in receivedMessage:
        # Decode into standard unicode set
        if (n &gt;= 32 and n &lt;= 126):
            string += chr(n)
    print("Out received message decodes to: {}".format(string))

    radio.stopListening()
    time.sleep(1)

根据上面的Arduino代码,显示传输数据的代码如下所示:

const char text[] = "Yo wassup, haha";

根据上面的Raspberry代码,解码Arduino接收数据的代码如下所示:

for n in receivedMessage:
    # Decode into standard unicode set
    if (n &gt;= 32 and n &lt;= 126):
        string += chr(n)

然而,这些解码代码仅在我将字符串或整数从Arduino传输到Raspberry Pi时才有效。 如果我传输浮点数它不起作用。由于DHT22将温度和湿度记录到1小数点,因此我需要传输浮点数。 这里的任何人都可以教我如何解码浮点值吗?

2 个答案:

答案 0 :(得分:0)

您需要将浮点值转换为string,并通过radio.write函数发送该字符串。

我不是C ++专家,但this answer提到std::to_string()函数可以将或多或少的内容转换为C ++ string。 如果你需要一个char[]that answer提供一个C风格的函数来完成这项工作(好吧,把它放在一个函数中):

char buffer[64];
int ret = snprintf(buffer, sizeof buffer, "%f", myFloat);

if (ret < 0) {
    return EXIT_FAILURE;
}
if (ret > sizeof buffer) {
    /* Result was truncated - resize the buffer and retry.
}

在Python方面,如果你确定缓冲区msg包含一个浮点值(也就是说,你确定所有要接收的数据都是有效接收的),那么只需要调用float(msg)

答案 1 :(得分:0)

如果你定义A是数字0的等价,B是1,C是2,D = 3,E = 4,F = 5,G = 6,H = 7,I = 8,J = 9, P为小数点。然后,当您将字符串传递给Raspberry pi时,例如“CEDPBA”,则表示为243.10。但是对于这种方法,您需要将字符串解码为数字数据。让我知道这是否有意义,是否有效?如果有效,请告诉我。