python - Using Raspberry Pi to Receive Float from Arduino using NRF24L01 -




i've been learning on how transfer data arduino raspberry pi wirelessly using nrf24l01 based on following reference: raspberry pi 3 tutorial 14 – wireless pi arduino communication nrf24l01+.

the reason why want log temperature , humidity data wirelessly using dht22 sensors.

the arduino code 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 radio.");         radio.stoplistening();          string stringmessage(receivedmessage);          if(stringmessage == "getstring"){             serial.println("looks want string!");             const char text[] = "yo wassup, haha";             radio.write(text, sizeof(text));             serial.println("we sent our message.");         }     }     delay(100);  } 

meanwhile, raspberry pi code shown below:

import rpi.gpio gpio 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 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 receivedmessage unicode characters")     string = ""     n in receivedmessage:         # decode standard unicode set         if (n &gt;= 32 , n &lt;= 126):             string += chr(n)     print("out received message decodes to: {}".format(string))      radio.stoplistening()     time.sleep(1) 

based on arduino code above, code shows data transmitted shown below:

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

based on raspberry code above, codes decode received data arduino shown below:

for n in receivedmessage:     # decode standard unicode set     if (n &gt;= 32 , n &lt;= 126):         string += chr(n) 

however, these decoding code works if transmit string or integer arduino raspberry pi. doesn't work if transmit float. since dht22 records temperature , humidity until 1 decimal point, required me transmit float. can here please teach me how decode float values?

you need convert float value string, , send string through radio.write function.

i'm not c++ specialist, this answer mentions std::to_string() function converts more or less c++ string. if need char[] instead, that answer provides c-style function job (well, put in function):

char buffer[64]; int ret = snprintf(buffer, sizeof buffer, "%f", myfloat);  if (ret < 0) {     return exit_failure; } if (ret > sizeof buffer) {     /* result truncated - resize buffer , retry. } 

on python side, if you're buffer msg contains float value (that is, you're data received received), call float(msg).





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -