Hello, i have a project where i collect data from various sensors installed on an mkr wan 1310 and need to send the acquired data to another 1310 board. To do that i want to give each sensor an unique ID and store said ID and Data in one byte and one word respectively. I managed to send and receive the ID without much problem but i can't manage to send over the data. I need to save the value 1022 for example, but even if i use a uint16_t and send the desired value, on the reciving end it maxes out at 0xFF (255) and i have no idea why.
use a structure and send the structure
struct __attribute__((packed)) t_info {
byte id;
uint16_t data;
};
void setup() {
// create a variable to store the message
t_info myInfo;
// fill in the fields
myInfo.id = 0x42;
myInfo.data = 1022;
// send it. (use &myInfo for the start of the buffer and sizeof myInfo for the number of bytes)
// ....
}
void loop() {}
1 Like
Hey, thank you for the advice. I tried to make the structure and send it in the packet instead but i can't seem to make the packet to send it as i receive an error when trying to use LoRa.print/write.
post your code
#include <SPI.h>
#include <LoRa.h>
struct __attribute__((packed)) t_info {
byte id;
uint16_t data;
};
void setup() {
if (!LoRa.begin(434E6)) {
Serial.println("Starting LoRa failed!");
while (1);
}
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(250000);
LoRa.setCodingRate4(8);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
t_info myInfo;
myInfo.id = 0x42;
}
void loop() {
if(digitalRead(2)== HIGH)
{
digitalWrite(3, HIGH);
digitalWrite(2, LOW);
}else if(digitalRead(2)== LOW)
{
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
}
myInfo.data = analogRead(A5);
// send packet
LoRa.beginPacket();
LoRa.write(myInfo);
LoRa.endPacket();
delay(3000);
}
What happened to code tags?
Sorry about that. New here and work's been pretty hectic. Didn't know about that option. It should be alright now i think?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.