Uint8 Lora decode help

Hi Everybody.

I am struggling with following code.
My idea is to send Weather values over Lora.
The programming comes more or less from my helium nodes where I use this.
However the decoding in Arduino is my problem.

I get as answer from the receiver:
Received packet '0 , 150 , 1 , 103 , 0 , 130 , 1 , 24 , 10 , 140 , 1 , 44 , 3 , 245 , 15 , 12 , ' with RSSI -62

How can I declutter it back to Integers again on the receiving end?

Best regards
Bart

Sender Code
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>

//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
#define BAND 866E6

int counter = 0;
uint8_t data[16];     


void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);

  Serial.println("LoRa Sender Test")

  //SPI LoRa pins
  SPI.begin(SCK, MISO, MOSI, SS);
  //setup LoRa transceiver module
  LoRa.setPins(SS, RST, DIO0);
  
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initializing OK!");

    LoRa.setSpreadingFactor(12);
  LoRa.setSignalBandwidth(250000);
  LoRa.setCodingRate4(8);
  LoRa.enableCrc();
  delay(2000);

}


void loop() {

 //  Generatepayload();
   
  Serial.print("Sending packet: ");
  Serial.println(counter);

int WindSpeed = 15;
int WindDir = 359;
int WindAv = 13;
int WindGust = 28;
int WindVar1 = 270;
int WindVar2 = 30;
int Qnh = 1013;
int Temp = 15;
int Dew = 12;

    int LWS = ((int)(WindSpeed * 10));
    int LWD = (int)(WindDir);
    int LWA = (int)(WindAv * 10);
    int LWG = (int)(WindGust * 10);
    int LWV1 = (int)(WindVar1 * 10);
    int LWV2 = (int)(WindVar2 * 10);
    int LQ = (int)(Qnh);
    byte LT = (int)(Temp);
    byte LD = (int)(Dew);


    data[0] = LWS >> 8;
    data[1] = LWS;
    data[2] = LWD >> 8;
    data[3] = LWD;
    data[4] = LWA >> 8;
    data[5] = LWA;
    data[6] = LWG >> 8;
    data[7] = LWG;    
    data[8] = LWV1 >> 8;
    data[9] = LWV1;
    data[10] = LWV2 >> 8;
    data[11] = LWV2;
    data[12] = LQ >> 8;
    data[13] = LQ;
    data[14] = LT;
    data[15] = LD;  
  // send packet
  LoRa.beginPacket();
 // LoRa.write(data);
   LoRa.write((uint8_t *)(&data), sizeof(data));
  LoRa.endPacket();

  counter++;

  delay(3000);

}
Receiver code
#include <SPI.h>
#include <LoRa.h>

//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
#define BAND 866E6


void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);

  
  Serial.println("LoRa Sender Test");

  //SPI LoRa pins
  SPI.begin(SCK, MISO, MOSI, SS);
  //setup LoRa transceiver module
  LoRa.setPins(SS, RST, DIO0);
  
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initializing OK!");

    LoRa.setSpreadingFactor(12);
  LoRa.setSignalBandwidth(250000);
  LoRa.setCodingRate4(8);
  LoRa.enableCrc();
  delay(2000);

}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");

    // read packet
    while (LoRa.available()) {
      uint8_t input = (uint8_t)LoRa.read();
      Serial.print(input);
 //     Serial.print("(0x");
  //    Serial.print(input, HEX);
      Serial.print(" , ");
    }

    
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

What does "declutter" mean, in this context, please?

I would like to get ie the integers LWS LWD etc back instead of a stream like I have now.

Best regards

Bart

int data[20]; //big enough to hold the data
byte index = 0; // start at zero
// read packet
while (LoRa.available())
{
  uint8_t input = (uint8_t)LoRa.read();
  data[index++] = input;  // save the current data item
  Serial.print(input);
  Serial.print(" , ");
}

The values are now in the array to do whatever you want with

Reverse of this transform

obviously will be this:

LWS = (data[0] << 8) | data[1];

Also you will need to convert your values to float and divide it by 10.

It would just be the opposite of that, wouldn't it?
Am I missing something?

put your data in a structure and transmit/receive the complete structure in a packet
have a look at decimals-strings-and-lora

If I do that it returns with following error: error: invalid types 'uint8_t {aka unsigned char}[int]' for array subscript
LWS = (data[0] << 8) | data[1];

That is where I'm am stuck right now

You can't shift an eight bit value left by eight places.
Cast it to uint16_t before the shift

Please show the error message in full.

/private/var/folders/fz/hbjk_wh112q1_58fnb80tjbm0000gn/T/.arduinoIDE-unsaved202308-3184-2pewqi.8qf4e/sketch_jan8b/sketch_jan8b.ino: In function 'void loop()':
/private/var/folders/fz/hbjk_wh112q1_58fnb80tjbm0000gn/T/.arduinoIDE-unsaved202308-3184-2pewqi.8qf4e/sketch_jan8b/sketch_jan8b.ino:102:20: error: invalid types 'uint8_t {aka unsigned char}[int]' for array subscript
       LWS = (data[0] << 8) | data[1];
                    ^
/private/var/folders/fz/hbjk_wh112q1_58fnb80tjbm0000gn/T/.arduinoIDE-unsaved202308-3184-2pewqi.8qf4e/sketch_jan8b/sketch_jan8b.ino:102:36: error: invalid types 'uint8_t {aka unsigned char}[int]' for array subscript
       LWS = (data[0] << 8) | data[1];
                                    ^

exit status 1

Compilation error: invalid types 'uint8_t {aka unsigned char}[int]' for array subscript

No, it is not an issue, the operation will be performed in int type.

Please show your receiver code after editing.
In the code in the initial post you did not have the data array at all.

#include <SPI.h>
#include <LoRa.h>

//Libraries for OLED Display
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

int LWS = 0;

//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
#define BAND 866E6

//OLED pins

#define OLED_SDA 4
#define OLED_SCL 15 
#define OLED_RST 16
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels




Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST);

void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);

  //reset OLED display via software
  pinMode(OLED_RST, OUTPUT);
  digitalWrite(OLED_RST, LOW);
  delay(20);
  digitalWrite(OLED_RST, HIGH);

  //initialize OLED
  Wire.begin(OLED_SDA, OLED_SCL);
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3c, false, false)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("LORA SENDER ");
  display.display();
  
  Serial.println("LoRa Sender Test");

  //SPI LoRa pins
  SPI.begin(SCK, MISO, MOSI, SS);
  //setup LoRa transceiver module
  LoRa.setPins(SS, RST, DIO0);
  
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initializing OK!");
  display.setCursor(0,10);
  display.print("LoRa Initializing OK!");
  display.display();

    LoRa.setSpreadingFactor(12);
  LoRa.setSignalBandwidth(250000);
  LoRa.setCodingRate4(8);
  LoRa.enableCrc();
  delay(2000);
  display.clearDisplay();






}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");

    // read packet
    while (LoRa.available()) {
      uint8_t data = (uint8_t)LoRa.read();


      LWS = (data[0] << 8) | data[1];
            Serial.print(LWS);
    }

    
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

In your sketch data is an int not an array of ints
See post #4

You could also take the "radical" step of not bothering to copy the values in the array to other variables and just use them as they are

put your data in a structure such as

struct Data {
  int WindSpeed;
  int WindDir;
  int WindAv;
  int WindGust;
  int WindVar1;
  int WindVar2;
  int Qnh;
  int Temp;
  int Dew;
};

Data data = { 15, 359, 13, 28, 170, 30, 1013, 15, 12 };

and the transmission simplifies to

void loop() {
  //  Generatepayload();
  Serial.print("Sending packet: ");
  Serial.println(counter);
  // send packet
  LoRa.beginPacket();
  // LoRa.write(data);
  LoRa.write((uint8_t *)(&data), sizeof(data));
  LoRa.endPacket();
  counter++;
  delay(3000);
}

receiver similar

Thank you Horace.
I have got it working with a structure.

Out of curiosity I would still be interested what went wrong in my bitshifting.

best regards
Bart

nothing was wrong with the bitshifting in the sender
in the receiver you need to read the bytes into an array then bitshift them out
e.g. a version of your sender - minor changes for the Feather 32u4 board and printing the transmitted data (always a good idea when testing)

//Sender Code
//Libraries for LoRa
#include <SPI.h>
#include <LoRa.h>

//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
#define BAND 866E6

int counter = 0;
uint8_t data[16];     


void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Sender Test");

  //SPI LoRa pins
  //SPI.begin(SCK, MISO, MOSI, SS);
  //LoRa.setPins(SS, RST, DIO0);
  LoRa.setPins(8, 4, 7); // for Lora 32u4
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
  Serial.println("LoRa Initializing OK!");

    LoRa.setSpreadingFactor(12);
  LoRa.setSignalBandwidth(250000);
  LoRa.setCodingRate4(8);
  LoRa.enableCrc();
  delay(2000);

}


void loop() {

 //  Generatepayload();
   
  Serial.print("Sending packet: ");
  Serial.println(counter);

int WindSpeed = 15;
int WindDir = 359;
int WindAv = 13;
int WindGust = 28;
int WindVar1 = 270;
int WindVar2 = 30;
int Qnh = 1013;
int Temp = 15;
int Dew = 12;

    int LWS = ((int)(WindSpeed * 10));
    int LWD = (int)(WindDir);
    int LWA = (int)(WindAv * 10);
    int LWG = (int)(WindGust * 10);
    int LWV1 = (int)(WindVar1 * 10);
    int LWV2 = (int)(WindVar2 * 10);
    int LQ = (int)(Qnh);
    byte LT = (int)(Temp);
    byte LD = (int)(Dew);


    data[0] = LWS >> 8;
    data[1] = LWS;
    data[2] = LWD >> 8;
    data[3] = LWD;
    data[4] = LWA >> 8;
    data[5] = LWA;
    data[6] = LWG >> 8;
    data[7] = LWG;    
    data[8] = LWV1 >> 8;
    data[9] = LWV1;
    data[10] = LWV2 >> 8;
    data[11] = LWV2;
    data[12] = LQ >> 8;
    data[13] = LQ;
    data[14] = LT;
    data[15] = LD;  
  // send packet
  LoRa.beginPacket();
 // LoRa.write(data);
   LoRa.write((uint8_t *)(&data), sizeof(data));
  LoRa.endPacket();
   for (int i = 0; i <  sizeof(data); i++) {
       // ((byte *) &data)[i] = LoRa.read();
        Serial.print(' ');
        Serial.print(((byte *) &data)[i]);
      }

  counter++;

  delay(3000);

}

receiver code with bitshifting added

// Receiver code
#include <SPI.h>
#include <LoRa.h>

//define the pins used by the LoRa transceiver module
#define SCK 5
#define MISO 19
#define MOSI 27
#define SS 18
#define RST 14
#define DIO0 26
#define BAND 866E6


void setup() {
  //initialize Serial Monitor
  Serial.begin(115200);
  while (!Serial)
    ;

  Serial.println("LoRa receiver Test");

  //SPI LoRa pins
  //SPI.begin(SCK, MISO, MOSI, SS);
  //setup LoRa transceiver module
  //LoRa.setPins(SS, RST, DIO0);
  LoRa.setPins(8, 4, 7);  // for Lora 32u4
  if (!LoRa.begin(BAND)) {
    Serial.println("Starting LoRa failed!");
    while (1)
      ;
  }
  Serial.println("LoRa Initializing OK!");

  LoRa.setSpreadingFactor(12);
  LoRa.setSignalBandwidth(250000);
  LoRa.setCodingRate4(8);
  LoRa.enableCrc();
  delay(2000);
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    uint8_t data[16];
    LoRa.readBytes((byte *)&data, packetSize);
    // read packet
    //while (LoRa.available())
    for (int i = 0; i < packetSize; i++) {
      // ((byte *) &data)[i] = LoRa.read();
      Serial.print(' ');
      Serial.print(((byte *)&data)[i]);
    }

    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
    int WindSpeed = 15;
    int WindDir = data[0] << 8 | data[1];
    Serial.print("WindDir ");
    Serial.println(WindDir);
    int WindAv = data[2] << 8 | data[3];
    Serial.print("WindAv ");
    Serial.println(WindAv);
    int WindGust = data[4] << 8 | data[5];
    Serial.print("WindGust ");
    Serial.println(WindGust);
    int WindVar1;
    int WindVar2;
    int Qnh;
    int Temp;
    int Dew = data[15];
    Serial.print("Dew ");
    Serial.println(Dew);
  }
}

the sender prints

 0 150 1 103 0 130 1 24 10 140 1 44 3 245 15 12Sending packet: 246
 0 150 1 103 0 130 1 24 10 140 1 44 3 245 15 12Sending packet: 247
 0 150 1 103 0 130 1 24 10 140 1 44 3 245 15 12Sending packet: 248
 0 150 1 103 0 130 1 24 10 140 1 44 3 245 15 12Sending packet: 249
 0 150 1 103 0 130 1 24 10 140 1 44 3 245 15 12Sending packet: 250

the receiver prints

Received packet ' 0 150 1 103 0 130 1 24 10 140 1 44 3 245 15 12' with RSSI -63
WindDir 150
WindAv 359
WindGust 130
Dew 12
Received packet ' 0 150 1 103 0 130 1 24 10 140 1 44 3 245 15 12' with RSSI -63
WindDir 150
WindAv 359
WindGust 130
Dew 12
Received packet ' 0 150 1 103 0 130 1 24 10 140 1 44 3 245 15 12' with RSSI -63
WindDir 150
WindAv 359
WindGust 130
Dew 12

only printed four results - you can add the rest

the bitshift code is assuming 16bit integers - probably have problems on a 32bit int host
it is s good idea to use int16_t just in case the code is ported to a different host

In your receiver:

the data is the single byte, so data[0] and data[1] means mothing

Thank you so much.

Also this now works for me.

Is there a difference in how secured the transmission is to come thru correctly and is the size of the data different from the 2 methods?

Best regards

Bart