HelTecAutomation / Heltec_ESP32

Arduino library for Heltec ESP32 (or ESP32+LoRa) based boards
Other
634 stars 226 forks source link

Receive HEX data over LORA #48

Open davidhunt018 opened 4 years ago

davidhunt018 commented 4 years ago

Hello, Hope you are doing Great today! I contact you to request Help regarding an issue to receive HEX values in receiver node.

I use 02 Heltec SX1278 Esp32 nodes. One is used to send a float value to receiver node.

The problem that, i'm able to send float values over LORA in HEX Encode like this .

typedef struct sensorData_t{
  float d0;
};

typedef union packData_t{
  sensorData_t data;
  byte data_buffer[sizeof(sensorData_t)];
};

packData_t info;

void loop()
{
info.data.d0 = 6683.00 ;   // float value

LoRa.beginPacket();
  for(int i=0; i<sizeof(sensorData_t); i++)
  { 
    LoRa.print( info.data_buffer[i] , HEX);  // the float value is converted to 04 bytes HEX ---> ( 0x00, 0xD8,0xD0,0x45 ) in this case.
  }
  LoRa.endPacket();
}

In the receive node, i used the function (char)LoRa.read();

typedef struct sensorData_t{
  float d0;
};

typedef union packData_t{
  sensorData_t data;
  byte data_buffer[sizeof(sensorData_t)];
};

packData_t info;

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

    // read packet

    while (LoRa.available()) {
      byte byteArray[packetSize];

      for (int i = 0; i < packetSize; i++) { 
        byteArray[i] = (char) LoRa.read();   // <------ THE PROBLEM HERE, i don't get HEX values 
        Serial.println((char) LoRa.read());
      }

      for (int k=0; k < packetSize; k++)
      { 
        info.data_buffer[k] = byteArray[k];
      }

     Serial.print("d0 : ");
     Serial.println(info.data.d0);
    }
  }
}

The (char) LoRa.read(); function dont return a HEX value like it is sent. i tryed to use (byte) LoRa.read(), and (HEX) LoRa.read(); without success.

Could you please tell me if it exist a read function like LoRa.ReadByte to read byte by byte ?

It would be very appreciated if you could help because i'm stacked in this step.

Thanks.

edjo82 commented 3 years ago

Because it should be this: byteArray[i] += (char) LoRa.read();