Using Raspberry Pi to Receive Float from Arduino using NRF24L01

Ask questions about an Arduino program you are writing or questions about libraries

Moderator: fabioc

User avatar
foxfaisal
Posts: 3
Joined: 17 Aug 2017, 21:56
Status: Offline

Using Raspberry Pi to Receive Float from Arduino using NRF24L01

Postby foxfaisal » 17 Aug 2017, 22:05

Good day. I've been learning on how to transfer data from Arduino to Raspberry Pi wirelessly using NRF24L01 based on the following reference:
http://invent.module143.com/daskal_tutorial/raspberry-pi-3-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:

Code: Select all

//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:

Code: Select all

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:

Code: Select all

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?

Thank you very much.

User avatar
Juraj
Posts: 76
Joined: 28 Jun 2017, 16:18
Location: Slovakia
Contact:
Status: Offline

Re: Using Raspberry Pi to Receive Float from Arduino using NRF24L01

Postby Juraj » 18 Aug 2017, 06:39

what precision you need?

Code: Select all

long l = (long) (f * 100)

f = (float) l / 100;

User avatar
foxfaisal
Posts: 3
Joined: 17 Aug 2017, 21:56
Status: Offline

Re: Using Raspberry Pi to Receive Float from Arduino using NRF24L01

Postby foxfaisal » 22 Aug 2017, 08:54

Thank you for your reply and sorry for my late reply because I was having a business trip for few days and didn't have internet connection during that time.

The precision that I need is only up to 1 decimal point, because DHT22 gives values up until 1 decimal point for both temperature and humidity reading. For example, temperature = 32.1 °C and humidity = 59.6%.

Thank you.

User avatar
foxfaisal
Posts: 3
Joined: 17 Aug 2017, 21:56
Status: Offline

Re: Using Raspberry Pi to Receive Float from Arduino using NRF24L01

Postby foxfaisal » 23 Aug 2017, 09:26

Somebody suggested me to convert the float to string in Arduino, then transmit the string from Arduino to Raspberry Pi throught NRF24L01, then convert back the string to float in Raspberry Pi. I think I will also try that. Below are some of my references:

Arduino - StringConstructor
https://www.arduino.cc/en/Reference/StringConstructor

Parse String to Float or Int
https://stackoverflow.com/questions/379 ... oat-or-int

I will inform the result once I tried this.


Return to “Programming questions”

Who is online

Users browsing this forum: No registered users and 1 guest

DISCOVER OUR

WHOLE STORY

About us


GET IN TOUCH

WITH US

Contact us


FIND ALL OUR OPEN POSITIONS AND JOIN US!
 

JOB SEARCH