Hi all trying to understand how the LoRa payload length is determined in this code:
void sendMessage(String outgoing) {
LoRa.beginPacket(); // start packet
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(msgCount); // add message ID
LoRa.write(outgoing.length()); // add payload length
LoRa.write(rcontrol); // add payload
LoRa.endPacket(); // finish packet and send it
msgCount++; // increment message ID
Serial.print("Message sent=");
Serial.println (rcontrol, BIN);
Serial.print ("rcontrolsent=");
rcontrolsent = rcontrol;
Serial.println(rcontrolsent);
}
Is it based on everything between LoRa.beginPacket(); and LoRa.endPacket(); or is it determined by this part of the code
if(rcontrol != rcontrolsent){
scontrol = rcontrol;
String message = String (scontrol);
sendMessage(message);
I have been trying to follow along on this thread to get an idea of what I need to do.
My code is currently working to send one byte.
I want to expand to sending and receiving 5 or 7 bytes. (1 byte and 2/3 ints)
I get that the LoRa.write/read must match in the send and receive. Is it as simple as just adding in the bytes I want to send in sequence in the packet structure (between LoRa.beginPacket(); and LoRa.endPacket(); ) and then have the same sequence in the receive structure. or do I need to build the packet before to get the outgoing length.
Full code I am currently using. (There are some sections that don't work but I am still working on it and are not really relevant to the question in this post)
#include <SPI.h>
#include <LoRa.h>
//LORA SETTINGS
const int csPin = 10; // LoRa radio chip select
const int resetPin = 13; // LoRa radio reset
const int irqPin = 2; // change for your board; must be a hardware interrupt pin
//LORA PACKET INFO
byte msgCount = 0; // count of outgoing messages
byte localAddress = 0xFF; // address of this device
byte destination = 0xBB; // destination to send to
long lastSendTime = 0; // last send time
int interval = 1000;
String sent;
String message;
//DATA IN
byte pcontrol = 0b00000000;
byte pcontrolr = 0b00000000;
//DATA OUT
byte rcontrol;
byte scontrol;
byte rcontrolsent;
byte controller_status;
//PINS_IN
int POWER_BUTTON = 29;
int RIGHT_ARROW = 30;
int STOP_ALL = 31;
int PLUS_SPEED = 32;
int MINUS_SPEED = 33;
int LEFT_ARROW = 34;
//PINS_OUT
int POWER_HOLD = 28;
int THERM_POWER_CONT = 39;
unsigned int speedp = 0;
unsigned int speedr = 3000;
unsigned int speedl = 0;
unsigned long hmillis = 0;
unsigned long pmillis = 0;
unsigned int holdcnt = 0;
#define pmaxs 35000
#define pmins 3000
void setup(void)
{
Serial.begin(9600);
while (!Serial);
Serial.println("LoRa Duplex");
Serial.print("Freq: ");
// override the default CS, reset, and IRQ pins (optional)
LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin
LoRa.setSignalBandwidth(15.6E3);
LoRa.setSpreadingFactor(12);
LoRa.setTxPower(20);
if (!LoRa.begin(433E6)) { // initialize ratio at 433 MHz
Serial.println("LoRa init failed. Check your connections.");
while (true); // if failed, do nothing
}
Serial.println("LoRa init succeeded.");
Serial.print("Rcontrol0=");
Serial.println(rcontrol);
Serial.print("Rcontrolsent0=");
Serial.println(rcontrolsent);
pinMode(POWER_BUTTON, INPUT_PULLUP);
pinMode(RIGHT_ARROW, INPUT_PULLUP);
pinMode(STOP_ALL, INPUT_PULLUP);
pinMode(PLUS_SPEED, INPUT_PULLUP);
pinMode(MINUS_SPEED, INPUT_PULLUP);
pinMode(LEFT_ARROW, INPUT_PULLUP);
pinMode(POWER_HOLD, OUTPUT);
pinMode(THERM_POWER_CONT, OUTPUT);
digitalWrite(THERM_POWER_CONT, HIGH);
}
void loop()
{
if(digitalRead(POWER_BUTTON) == LOW && millis() > 3000 && bitRead(controller_status, 0) == 0){
digitalWrite(POWER_HOLD, HIGH);
bitWrite(controller_status, 0, 1);
Serial.println("POWER_ON");
Serial.print("CONTROLER_STAT=");
Serial.println(controller_status, BIN);
}
if(digitalRead(POWER_BUTTON) == HIGH && bitRead(controller_status, 0) == 1 && bitRead(controller_status, 1) == 0 ){
bitWrite(controller_status, 1, 1);
}
if(digitalRead(POWER_BUTTON) == LOW && bitRead(controller_status, 1) == 1){
if(bitRead(controller_status, 2) == 0){
pmillis = millis() + 5000;
bitWrite(controller_status, 2, 1);
}
if(millis() > pmillis){
digitalWrite(POWER_HOLD, LOW);
Serial.println("SHUT_DOWN");
}
}
if(pcontrol != pcontrolr){
digitalWrite(THERM_POWER_CONT, LOW);
bitWrite(controller_status, 3, 1);
hmillis = millis() + 1000;
pcontrolr = pcontrol;
}
if(bitRead(controller_status, 0) == 1 && millis() > hmillis){
digitalWrite(THERM_POWER_CONT, HIGH);
bitWrite(controller_status, 3, 0);
}
if(rcontrol != rcontrolsent){
scontrol = rcontrol;
String message = String (scontrol);
sendMessage(message);
}
if (digitalRead(LEFT_ARROW) == LOW && bitRead(rcontrol, 2) == 0 ) {
bitWrite(rcontrol, 0, 1);
bitWrite(rcontrol, 1, 0);
bitWrite(rcontrol, 2, 1);
Serial.println("RUN_LEFT");
Serial.print("CONTROL=");
Serial.println(rcontrol, BIN);
}
if (digitalRead(RIGHT_ARROW) == LOW && bitRead(rcontrol, 1) == 0) {
bitWrite(rcontrol, 0, 1);
bitWrite(rcontrol, 1, 1);
bitWrite(rcontrol, 2, 0);
Serial.println("RUN_RIGHT");
Serial.print("CONTROL=");
Serial.println(rcontrol, BIN);
}
if (digitalRead(STOP_ALL) == LOW && bitRead(rcontrol,0)== 1) {
if(bitRead(rcontrol, 1) == 1){
bitWrite(rcontrol, 1, 0);
Serial.println("STOP_ALL");
Serial.print("CONTROL=");
Serial.println(rcontrol, BIN);
}
if(bitRead(rcontrol, 2) == 1){
bitWrite(rcontrol, 2, 0);
Serial.print("CONTROL=");
Serial.println(rcontrol, BIN);
}
bitWrite(rcontrol, 0, 0);
Serial.print("CONTROL=");
Serial.println(rcontrol, BIN);
}
if (digitalRead(PLUS_SPEED) == LOW && bitRead(rcontrol,3) == 0){
bitWrite(rcontrol, 3, 1);
Serial.println("SPEED_UP");
Serial.print("rcontrol=");
Serial.println(rcontrol, BIN);
}
if (digitalRead(MINUS_SPEED) == LOW && bitRead(rcontrol,4) == 0){
bitWrite(rcontrol, 4, 1);
Serial.println("SPEED_DOWN");
Serial.print("rcontrol=");
Serial.println(rcontrol, BIN);
}
if(digitalRead(PLUS_SPEED) == HIGH){ holdcnt = 0;bitWrite(rcontrol, 3, 0);}
if(digitalRead(MINUS_SPEED) == HIGH){ holdcnt = 0;bitWrite(rcontrol, 4, 0);}
onReceive(LoRa.parsePacket());
}
void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return
// read packet header bytes:
int recipient = LoRa.read(); // recipient address
byte sender = LoRa.read(); // sender address
byte incomingMsgId = LoRa.read(); // incoming msg ID
byte incomingLength = LoRa.read(); // incoming msg length
pcontrol = LoRa.read();
String incoming = String(pcontrol);
if (incomingLength != incoming.length()) { // check length for error
Serial.println("error: message length does not match length");
return; // skip rest of function
}
// if the recipient isn't this device or broadcast,
if (recipient != localAddress && recipient != 0xFF) {
Serial.println("This message is not for me.");
return; // skip rest of function
}
// if message is for this device, or broadcast, print details:
Serial.println("Received from: 0x" + String(sender, HEX));
Serial.println("Sent to: 0x" + String(recipient, HEX));
Serial.println("Message ID: " + String(incomingMsgId));
Serial.println("Message length: " + String(incomingLength));
Serial.println("Message: " + incoming);
Serial.println(pcontrol, BIN);
Serial.println("RSSI: " + String(LoRa.packetRssi()));
Serial.println("Snr: " + String(LoRa.packetSnr()));
Serial.println();
}
void sendMessage(String outgoing) {
LoRa.beginPacket(); // start packet
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(msgCount); // add message ID
LoRa.write(outgoing.length()); // add payload length
LoRa.write(rcontrol); // add payload
LoRa.endPacket(); // finish packet and send it
msgCount++; // increment message ID
Serial.print("Message sent=");
Serial.println (rcontrol, BIN);
Serial.print ("rcontrolsent=");
rcontrolsent = rcontrol;
Serial.println(rcontrolsent);
}