Dear all,
I am really blocked into a problem that I can not solve and understand since a couple of days. I created a new topics as my development evoluated and the way to structure my code has changed, I modifed my code following this way to send data
https://www.thethingsnetwork.org/docs/devices/bytes.html#how-to-send-multiple-numbers
I would really appreciate if you can help :o)
Let’s resume first
I got values from sensors. The function return me a float value with decimal (value can be negative)
float float_temperature = get_temperature();
float float_pression = get_pression();
float float_humidity = get_humidity();
float float_moiature = get_moisture();
float float_luminosity = get_luminoisty();
float float_battery = get_battery();
As I do not want to have a decimal, I covnerted it into int
// let's say, float_temperature is egual to -10.55
float_temperature = float_temperature*100;
int int_temperature = float_temperature;
Serial.println(int_temperature)
Serial.println() should print
-1055
Now I need to convert into bytes and I have serveral values to send to TTN.
I red the following page working with bytes and particularely this sections
https://www.thethingsnetwork.org/docs/devices/bytes.html#how-to-send-multiple-numbers
and
[https://www.thethingsnetwork.org/docs/devices/bytes.html#how-to-send-negative-numbers](https://www.thethingsnetwork.org/docs/devices/bytes.html#how-to-send-
Library (for info)
I work around the library LoRa-LMIC-1.51 and I am based on that exemple (FYI)
https://github.com/mikenz/LoRa-LMIC-1.51/blob/master/examples/nano-lmic-v1.51-F/nano-lmic-v1.51-F.ino
My problem
I need to send float values and to convert it into bytes. I also several values (may be too much).
In the Lora-LMIC-1.5 exemple, the variable uint8_t mydata[64] is used to store the value and sen it to TTN.
I absoluetly can not convert and send it but now i would like to work around the TTN exemple and I would like to ask you some help regarding the comment I am adding below
Let’s start :
Concidering this
byte payloadA[] = { 0xF0 };
byte payloadB[] = { 0xF0, 0x0F };
byte payloadC[] = { 0xF0, 0x0F, 0xFF };
int sizeofPayloadA = sizeof(payloadA);
int sizeofPayloadB = sizeof(payloadB);
int sizeofPayloadC = sizeof(payloadC);
byte payload[sizeofPayloadA + sizeofPayloadB + sizeofPayloadC];
memcpy(payload, payloadA, sizeofPayloadA);
memcpy(payload + sizeofPayloadA, payloadB, sizeofPayloadB);
memcpy(payload + sizeofPayloadA + sizeofPayloadB, payloadC, sizeofPayloadC);
and the values
int int_battery = 345;
int int_temperature = -1025;
int int_pression = 2222;
int int_humidity = 4321;
int int_moisture = 1234;
int int_luminosity = 56;
This is my big problem. (I am really sory if I have diffuclties to get it ). Even if I read that int, long, char are all bytes. That right?
(How can I convert it? I did it manually with my values)
byte payloadBattery[] = {0x33 0x34 0x35} // = 345
byte payloadTemperature[] = { 0x2D 0x31 0x30 0x32 0x35 }; // = -1025
byte payloadPression[] = { 0x32 0x32 0x32 0x32 }; // = 2222
byte payloadHumidity[] = { 0x34 0x33 0x32 0x31 }; // = 4321
byte payloadMoisture[] = {0x31 0x32 0x33 0x34}; // = 1234
byte payloadLuminosity[] = { 0x35 0x36 }; // = 56
But HOW to do it programly keeping the negative values???
I am sorry but I really have pain to understand the convertion
Then, Here I count the size of my measure variables
int sizeofPayloadBattery = sizeof(payloadBattery);
int sizeofPayloadTemperature = sizeof(payloadTempearture);
int sizeofPayloadPression = sizeof(payloadPression);
int sizeofPayloadHumidity = sizeof(payloadHumidity);
int sizeofPayloadMoisture = sizeof(payloadMoisture);
int sizeofPayloadLuminosity = sizeof(payloadLuminosity);
Here I declare my payload variable and it size is the addition of value
it may be possilbe, it over 51.But let’s try to understand, and later, I will see how to split my send. Ok?
(or let’s work only with 3 sensors)
byte payload[sizeofPayloadBattery + sizeofPayloadTemperature + sizeofPayloadPression + sizeofPayloadHumidity + sizeofPayloadMoisture + sizeofPayloadLuminosity ];
Here I copy the data into payload. I added some if() for information. I will make it batter later
memcpy(payload, payloadBattery, sizeofPayloadBattery);
memcpy(payload + sizeofPayloadBattery, payloadTemperature, sizeofPayloadTemperature);
memcpy(payload + sizeofPayloadBattery + sizeofPayloadTemperature, payloadPression, sizeofPayloadPression);
// from now I hope, I continue cottectly. Pls correct me
if((sizeofPayloadBattery + sizeofPayloadTemperature + sizeofPayloadPression + sizeofPayloadHumidity) > 51) Serial.println(F("playload is over (4)"));
memcpy(payload + sizeofPayloadBattery + sizeofPayloadTemperature + sizeofPayloadPression, payloadHumidity, sizeofPayloadHumidity);
if((sizeofPayloadBattery + sizeofPayloadTemperature + sizeofPayloadPression + sizeofPayloadHumidity + sizeofPayloadMoisture) > 51) Serial.println(F("playload is over (5)"));
memcpy(payload + sizeofPayloadBattery + sizeofPayloadTemperature + sizeofPayloadPression + sizeofPayloadHumidity, payloadMoisture, sizeofPayloadMoisture);
if((sizeofPayloadBattery + sizeofPayloadTemperature + sizeofPayloadPression + sizeofPayloadHumidity + sizeofPayloadMoisture + sizeofPayloadLuminosity) > 51) Serial.println(F("playload is over (6)"));
memcpy(payload + sizeofPayloadBattery + sizeofPayloadTemperature + sizeofPayloadPression + sizeofPayloadHumidity + sizeofPayloadMoisture, payloadLuminosity, sizeofPayloadLuminosity);
If the lora_LMIC_1.5 exemple use uint8_t type of variable, I guess byte payload variable can be used as well?
Does my code make sens? May I ask you to correct it, but first and particularely the conversion int into byte /hex
byte payloadBattery = {0x33 0x34 0x35} // = 345
I really rally thank your for any help you can tell and teach me
Cheers