I have an Arduino Nano (8 bit ATmega328) with a nrf24 radio reading a temperature DHT22 sensor and sending that data to an Arduino Nano 33 IoT (32 bit SAMD21).
I really want to be able to transmit a float value from the 8 bit architecture to the 32 bit architecture, but know this may require some additional work to repackage the float to be correctly interpretted by the 32 bit system.
As a first step, I am trying to send uint8_t values which I was hoping would not have architecture compatibility issues, but something I am doing is preventing the received value from correct interpretation.
When a send the value of 2 as an int8_t, it is received as a 3 for instance.
For now, I am using the "GettingStarted" example code to transmit the data. When using to identical Nanos (8 bit each), I can easily send and receive the correct values. I have modified this to set the TX to the 8 bit Nano with a radio and the RX to the 32 bit Nano with different radio pipe.
When using a mix of 8 bit and 32 bit Nanos, the data received on the 32 bit does not match the 8 bit.
I know for the actual temperature sensor, I will need to modify the sensor read value to a fixed point and send it or some other method to account for the different bit resolution, but first I am trying to send a simple uint8_t value and verify that works before trying to break up and custom encode and decode a larger number using uint8_t to do so.
In this code, there is no sensor involved, it is only creating a payload variable of type uint8_t with initial value of 0 and then incrementing by 1 each iteration and transmitting from the 8 bit architecture. The 32 bit architecture receives the value and displays in the serial window.
The expectation is that I should be able to send a compatible uint8_t type number from one device to the next and properly read it. The next step is to then determine how to properly break up a more complex number and transmit between architectures.
Please help figure out how to do the first part in order to accomplish the second part.
An example of the output is here:
TX side
RF24/examples/GettingStarted
radioNumber = 0
*** PRESS 'T' to begin transmitting to the other node
Transmission failed or timed out
Transmission successful! Binary rep: 0 -Time to transmit = 528 us. Sent: 0
Transmission successful! Binary rep: 1 -Time to transmit = 528 us. Sent: 1
Transmission successful! Binary rep: 10 -Time to transmit = 524 us. Sent: 2
Transmission successful! Binary rep: 11 -Time to transmit = 520 us. Sent: 3
Transmission successful! Binary rep: 100 -Time to transmit = 524 us. Sent: 4
Transmission successful! Binary rep: 101 -Time to transmit = 528 us. Sent: 5
Transmission successful! Binary rep: 110 -Time to transmit = 528 us. Sent: 6
Transmission successful! Binary rep: 111 -Time to transmit = 2228 us. Sent: 7
Transmission successful! Binary rep: 1000 -Time to transmit = 524 us. Sent: 8
Transmission successful! Binary rep: 1001 -Time to transmit = 528 us. Sent: 9
Transmission successful! Binary rep: 1010 -Time to transmit = 528 us. Sent: 10
Transmission successful! Binary rep: 1011 -Time to transmit = 524 us. Sent: 11
Transmission successful! Binary rep: 1100 -Time to transmit = 528 us. Sent: 12
Transmission successful! Binary rep: 1101 -Time to transmit = 520 us. Sent: 13
Transmission successful! Binary rep: 1110 -Time to transmit = 528 us. Sent: 14
Transmission successful! Binary rep: 1111 -Time to transmit = 524 us. Sent: 15
RX side
Received 1 bytes on pipe 1: Binary rep: 0 -: 0
Received 1 bytes on pipe : Binary rep: 1 -: 1
Received 1 bytes on pipe 1: Binary rep: 11 -: 3
Received 1 bytes on pipe 1: Binary rep: 113
Received 1 bytes on pipe 1: Binary rep: 110 -: 6
Received 1 bytes on pipe 1: Binary rep: 111 -: 7
Received 1 bytes on pipe 1: Binary rep: 111 -: 7
Received 1 bytes on pipe 1: Binary rep: -: 7
Received 1 bytes on pipe 1: Binary rep: 1100 -: 12
Received 1 bytes on pipe 1: Binary rep: 1101 -: 13
Received 1 bytes on pipe 1: Binary rep: 1111 -: 15
Received 1 bytes on pipe 1 Binary rep: 1111 -: 15
Received 1 bytes on pipe 1: Binary rep: 1110 -: 14
Received 1 bytes on pipe 1: Binary rep: 1111 -: 15
Received 1 bytes on pipe 1: Binary rep: 1111 -: 15
TX code
/*
* See documentation at https://nRF24.github.io/RF24
* See License information at root directory of this library
* Author: Brendan Doherty (2bndy5)
*/
/**
* A simple example of sending data from 1 nRF24L01 transceiver to another.
*
* This example was written to be used on 2 devices acting as "nodes".
* Use the Serial Monitor to change each node's behavior.
*/
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#include <Arduino.h>
// instantiate an object for the nRF24L01 transceiver
RF24 radio(7, 8); // using pin 7 for the CE pin, and pin 8 for the CSN pin
// Let these addresses be used for the pair
uint8_t address[][6] = {"1Node", "2Node"};
// It is very helpful to think of an address as a path instead of as
// an identifying device destination
// to use different addresses on a pair of radios, we need a variable to
// uniquely identify which address this radio will use to transmit
bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
// Used to control whether this node is sending or receiving
bool role = true; // true = TX role, false = RX role
// For this example, we'll be using a payload containing
// a single float number that will be incremented
// on every successful transmission
uint8_t payload = 0;
void setup() {
Serial.begin(115200);
while (!Serial) {
// some boards need to wait to ensure access to serial over USB
}
// initialize the transceiver on the SPI bus
if (!radio.begin()) {
Serial.println(F("radio hardware is not responding!!"));
while (1) {} // hold in infinite loop
}
// print example's introductory prompt
Serial.println(F("RF24/examples/GettingStarted"));
// To set the radioNumber via the Serial monitor on startup
// Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
// while (!Serial.available()) {
// // wait for user input
// }
char input = Serial.parseInt();
radioNumber = input == 1;
radioNumber=0;
Serial.print(F("radioNumber = "));
Serial.println((int)radioNumber);
// role variable is hardcoded to RX behavior, inform the user of this
Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
// Set the PA Level low to try preventing power supply related problems
// because these examples are likely run with nodes in close proximity to
// each other.
radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
// save on transmission time by setting the radio to only transmit the
// number of bytes we need to transmit a float
radio.setPayloadSize(sizeof(payload)); // float datatype occupies 4 bytes
// set the TX address of the RX node into the TX pipe
radio.openWritingPipe(address[radioNumber]); // always uses pipe 0
// set the RX address of the TX node into a RX pipe
radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
// additional setup specific to the node's role
if (role) {
radio.stopListening(); // put radio in TX mode
} else {
radio.startListening(); // put radio in RX mode
}
// For debugging info
// printf_begin(); // needed only once for printing details
// radio.printDetails(); // (smaller) function that prints raw register values
// radio.printPrettyDetails(); // (larger) function that prints human readable data
} // setup
void loop() {
if (role) {
// This device is a TX node
unsigned long start_timer = micros(); // start the timer
bool report = radio.write(&payload, sizeof(payload)); // transmit & save the report
unsigned long end_timer = micros(); // end the timer
if (report) {
Serial.print(F("Transmission successful! ")); // payload was delivered
Serial.print(" Binary rep: ");
Serial.print(payload, BIN);
Serial.print(F(" -Time to transmit = "));
Serial.print(end_timer - start_timer); // print the timer result
Serial.print(F(" us. Sent: "));
Serial.println(payload); // print payload sent
payload += 1; // increment float payload
} else {
Serial.println(F("Transmission failed or timed out")); // payload was not delivered
}
// to make this example readable in the serial monitor
delay(1000); // slow transmissions down by 1 second
} else {
// This device is a RX node
uint8_t pipe;
if (radio.available(&pipe)) { // is there a payload? get the pipe number that recieved it
uint8_t bytes = radio.getPayloadSize(); // get the size of the payload
radio.read(&payload, bytes); // fetch payload from FIFO
Serial.print(F("Received "));
Serial.print(bytes); // print the size of the payload
Serial.print(F(" bytes on pipe "));
Serial.print(pipe); // print the pipe number
Serial.print(F(": "));
Serial.print(payload, BIN);
Serial.println(payload); // print the payload's value
}
} // role
if (Serial.available()) {
// change the role via the serial monitor
char c = toupper(Serial.read());
if (c == 'T' && !role) {
// Become the TX node
role = true;
Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
radio.stopListening();
} else if (c == 'R' && role) {
// Become the RX node
role = false;
Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
radio.startListening();
}
}
} // loop
RX code
/*
* See documentation at https://nRF24.github.io/RF24
* See License information at root directory of this library
* Author: Brendan Doherty (2bndy5)
*/
/**
* A simple example of sending data from 1 nRF24L01 transceiver to another.
*
* This example was written to be used on 2 devices acting as "nodes".
* Use the Serial Monitor to change each node's behavior.
*/
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#include <Arduino.h>
// instantiate an object for the nRF24L01 transceiver
RF24 radio(7, 8); // using pin 7 for the CE pin, and pin 8 for the CSN pin
// Let these addresses be used for the pair
uint8_t address[][6] = {"1Node", "2Node"};
// It is very helpful to think of an address as a path instead of as
// an identifying device destination
// to use different addresses on a pair of radios, we need a variable to
// uniquely identify which address this radio will use to transmit
bool radioNumber = 1; // 0 uses address[0] to transmit, 1 uses address[1] to transmit
// Used to control whether this node is sending or receiving
bool role = false; // true = TX role, false = RX role
// For this example, we'll be using a payload containing
// a single float number that will be incremented
// on every successful transmission
uint8_t payload = 0;
void setup() {
Serial.begin(115200);
while (!Serial) {
// some boards need to wait to ensure access to serial over USB
}
// initialize the transceiver on the SPI bus
if (!radio.begin()) {
Serial.println(F("radio hardware is not responding!!"));
while (1) {} // hold in infinite loop
}
// print example's introductory prompt
Serial.println(F("RF24/examples/GettingStarted"));
// To set the radioNumber via the Serial monitor on startup
// Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
// while (!Serial.available()) {
// // wait for user input
// }
char input = Serial.parseInt();
radioNumber = input == 1;
radioNumber=1;
Serial.print(F("radioNumber = "));
Serial.println((int)radioNumber);
// role variable is hardcoded to RX behavior, inform the user of this
Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
// Set the PA Level low to try preventing power supply related problems
// because these examples are likely run with nodes in close proximity to
// each other.
radio.setPALevel(RF24_PA_LOW); // RF24_PA_MAX is default.
// save on transmission time by setting the radio to only transmit the
// number of bytes we need to transmit a float
radio.setPayloadSize(sizeof(payload)); // float datatype occupies 4 bytes
Serial.print("payload size: ");
Serial.println(sizeof(payload));
// set the TX address of the RX node into the TX pipe
radio.openWritingPipe(address[radioNumber]); // always uses pipe 0
// set the RX address of the TX node into a RX pipe
radio.openReadingPipe(1, address[!radioNumber]); // using pipe 1
// additional setup specific to the node's role
if (role) {
radio.stopListening(); // put radio in TX mode
} else {
radio.startListening(); // put radio in RX mode
}
// For debugging info
// printf_begin(); // needed only once for printing details
// radio.printDetails(); // (smaller) function that prints raw register values
// radio.printPrettyDetails(); // (larger) function that prints human readable data
} // setup
void loop() {
if (role) {
// This device is a TX node
unsigned long start_timer = micros(); // start the timer
bool report = radio.write(&payload, sizeof(payload)); // transmit & save the report
unsigned long end_timer = micros(); // end the timer
if (report) {
Serial.print(F("Transmission successful! ")); // payload was delivered
Serial.print(F("Time to transmit = "));
Serial.print(end_timer - start_timer); // print the timer result
Serial.print(F(" us. Sent: "));
Serial.println(payload); // print payload sent
Serial.print(payload, BIN);
payload += 1; // increment float payload
} else {
Serial.println(F("Transmission failed or timed out")); // payload was not delivered
}
// to make this example readable in the serial monitor
delay(1000); // slow transmissions down by 1 second
} else {
// This device is a RX node
uint8_t pipe;
if (radio.available(&pipe)) { // is there a payload? get the pipe number that recieved it
uint8_t bytes = radio.getPayloadSize(); // get the size of the payload
radio.read(&payload, bytes); // fetch payload from FIFO
Serial.print(F("Received "));
Serial.print(bytes); // print the size of the payload
Serial.print(F(" bytes on pipe "));
Serial.print(pipe); // print the pipe number
Serial.print(F(": "));
Serial.print(" Binary rep: ");
Serial.print(payload, BIN);
Serial.print(" -: ");
Serial.println(payload); // print the payload's value
}
} // role
if (Serial.available()) {
// change the role via the serial monitor
char c = toupper(Serial.read());
if (c == 'T' && !role) {
// Become the TX node
role = true;
Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
radio.stopListening();
} else if (c == 'R' && role) {
// Become the RX node
role = false;
Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
radio.startListening();
}
}
} // loop