I'm trying to do wireless thermometer on max31865 arduino and nrf24l01 but I'm having problem sending whole data struct I can send int or char arrays but when I try to send whole struct I got output as
id: 131073 temperature: 0
Is there error in my code or there is some issue with differences in how arduino and raspberry store data? I don't really have experience in this so my question is how do I send struct that contain two fields of int or there is some better way? I need to transmit id and temperature in one package.
Transmitter code(Arduino)
#include <SPI.h>
#include "RF24.h"
#include <Adafruit_MAX31865.h>
#define RREF 430.0
#define RNOMINAL 100.0
typedef struct{
int id = 100;
int temperature = 100;
}
temp;
temp data;
bool radioNumber = 0;
int i =0;
Adafruit_MAX31865 thermo = Adafruit_MAX31865(10, 11, 12, 13);
RF24 radio(7,8);
byte addresses[][6] = {"1Node","2Node"};
bool role = 0;
void setup() {
Serial.begin(115200);
thermo.begin(MAX31865_3WIRE);
radio.begin();
radio.setChannel(125);
radio.setPALevel(RF24_PA_MIN);
radio.powerUp();
radio.setDataRate(RF24_1MBPS);
if(radioNumber){
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
}else{
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
}
radio.stopListening();
}
void loop() {
data.id = 1;
data.temperature = 2;
//int temp = 2; //thermo.temperature(RNOMINAL, RREF);
Serial.println(temp);
if (!radio.write( &data, sizeof(data) )){
Serial.println(F("failed"));
}
delay(1000);
}
Receiver code(Raspberry pi b3+)
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h>
using namespace std;
typedef struct{
int id = 99; // debug value
int temperature = 99;
}
temp;
RF24 radio(22,0);
temp data;
bool radioNumber = 1;
const uint8_t pipes[][6] = {"1Node", "2Node"};
int main(int argc, char** argv)
{
radio.begin();
radio.setChannel(125);
radio.setPALevel(RF24_PA_MIN);
radio.setDataRate(RF24_1MBPS);
radio.setRetries(15, 15);
radio.printDetails();
if (!radioNumber) {
radio.openWritingPipe(pipes[0]);
radio.openReadingPipe(1, pipes[1]);
} else {
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1, pipes[0]);
}
radio.startListening();
while (1) {
if (radio.available()) {
int tem = 0;
while (radio.available()) {
radio.read(&data, sizeof(data));
}
cout << tem << " " << "id: "<< data.id<< " temperatura: " << data.temperature << endl;
delay(925);
}
}
return 0;
}
EDIT: Ok after running sizeof(data) on both devices, I notice that structs have different size 4 bytes for arduino and 8 for raspberry. So change on raspberry side to
typedef struct{
uint16_t id = 100;
uintt16_t temperature = 100;
}
temp;
And this is working but, may I ask why int have various size on arduino and raspberry?