0

I've been learning on how to transfer data from Arduino to Raspberry Pi wirelessly using NRF24L01 based on the following reference: Raspberry Pi 3 Tutorial 14 – Wireless Pi to Arduino Communication with NRF24L01+.

The reason why I want to do this is to log temperature and humidity data wirelessly using DHT22 sensors.

The Arduino code is shown below:

//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);

}

Meanwhile, the Raspberry Pi code is shown below:

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)

Based on the Arduino code above, the code that shows the data that is transmitted is shown below:

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

Based on the Raspberry code above, the codes that decode the received data from Arduino are shown below:

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

However, these decoding code only works if I transmit string or integer from Arduino to Raspberry Pi. It doesn't work if I transmit float. Since DHT22 records temperature and humidity up until 1 decimal point, it is required for me to transmit float. Can anyone here please teach me how to decode the float values?

Right leg
  • 16,080
  • 7
  • 48
  • 81
foxfaisal
  • 1
  • 1

2 Answers2

0

You need to convert your float value to a string, and to send that string through the radio.write function.

I'm not a C++ specialist, but this answer mentions a std::to_string() function that converts more or less anything to a C++ string. If you need a char[] instead, that answer provides a C-style function that will do the job (well, put it in a function):

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.
}

On the Python side, if you're certain that a buffer msg contains a float value (that is, you're certain that all the data that was to be received was effectively received), then just call float(msg).

Right leg
  • 16,080
  • 7
  • 48
  • 81
  • Understood. All this time I thought the problem was only on the raspberry pi's side, but now it is identified that the float should also be converted to string on the Arduino's side. I will spend some time to study and solve the problem. Will update my progress later. Thank you very much. – foxfaisal Aug 22 '17 at 11:07
  • Instead of converting float to string in Arduino, I think I need to convert the float to char because the example that I refer transmit char from Arduino to Raspberry Pi through NRF24L01. However, I still don't know how to convert the float value to char at this moment. – foxfaisal Aug 25 '17 at 11:26
0

if you define A is quivalent to numerical 0, B as 1, C as 2, D=3, E=4, F=5, G=6, H=7, I=8, J=9, and P as decimal point. Then when you pass a string to Raspberry pi, for example, "CEDPBA", then it means 243.10. But for this approach, you will need to decode your string to numerical data. Let me know if this makes senses and if it works? If it works, let me know.

eric
  • 1