Good day. 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) < 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 > 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 >= 32 and n <= 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 >= 32 and n <= 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.
dtostrf()
function, this article may be provide an example on how to use it. – hcheung Aug 18 '17 at 7:55