Creating Variables from Packet Data Strings

Hi Everyone,

I'm relatively new to this so I'm struggling to find the right solution to my woes.

I currently have a Lora Node transmitting some Barometric Data to my Lora Receiver/Arduino stack up, this part works fine, a connection between sender and receiver is made and the packet data from the sender appears in the serial monitor.

The issue I have is trying to turn the packet data into variables which I can then use for an IOT dashboard.

I will post the code when I can but essentially the packet data is this;

Packet Received: Temp 22.45
Packet Received: ATP 12.22

Previously all my readings have been in one string such as;

Packet Received: Temp 22.45 ATP 12.22

In the instance of a single string I would just use the substring function to pick out the individual readings and use these.

However with the strings in two packets the substring function seems to get lost because it doesn't actually know which packet im referring to.

Is there an if solution somewhere to look at the string first and if Temp appears then create the temp variable, else create an ATP variable?

Any help is appreciated.

You can simply check if the string contains the substring you need with indexOf()

String packet = "Packet Received: Temp 22.45";

if (packet.indexOf("Temp") > -1) {
  temp = packet.substring(packet.indexOf("Temp") + strlen("Temp")).toFloat();
} 
else if (packet.indexOf("ATP") > -1) {
 ATP = .....
} 
etc etc

You already answered above!! So ignore the below, however I will leave it there for critique.

Thanks a bunch cotestatnt, I'll go have a play with this.
So when doing the below would the rest of this be correct, after checking the packet contains the string I need, I create my variable using the substring function to locate the value only and put these into double.

String packet = "Packet Received: Temp 22.45";

if (packet.indexOf("Temp") > -1) {
temp = packet.substring(22);
}
else if (packet.indexOf("ATP") > -1) {
atp = packet.substring(21);
}

temp.toDouble
atp.toDouble

etc etc

You could use also the C/C++ standard function sscanf()
(but I don't know if it works correctly with float/double vars)

  String packet ="Packet Received: Temp 22.45";

  double temp, atp;

  sscanf (packet.c_str(), "Packet Received: Temp %lf", &temp);
  Serial.println(temp);
  
  sscanf (packet.c_str(), "Packet Received: ATP %lf", &atp);
  Serial.println(atp);   // this is 0.0

Do bear in mind that you can simplify the receipt of different types of data by sending an array that includes an identifier byte decribing the datatype.

If you used 1 to identify the Temp and 2 to identify ATP, and the data is a float (4 bytes) then you can send 5 byte packets;

For Temp 1,xx,xx,xx,xx
For ATP 2,yy,yy,yy,yy

Or if your sending text based packets, set the first character of the packet to 'T' or 'A' so you dont need to search the packet at all, just read the identifier from the first character.

@cotestatnt @srnet

Okay so I tried to implement your solution cotestatnt, but the DO and rtd variables I created always return 0.00 which means something is off.

I also tried the scanf function but the same result.

#define _DEBUG_
#define _DISABLE_TLS_

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>
#include <LoRa.h>
#include <SoftwareSerial.h>


#define nss D10
#define rst D14
#define dio0 D2

#define USERNAME "test"
#define DEVICE_ID "test"
#define DEVICE_CREDENTIAL "test"

#define SSID "test"
#define SSID_PASSWORD "test"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup()
{
  LoRa.setPins(nss, rst, dio0);
  Serial.begin(115200);
  while (!Serial);
  if (!LoRa.begin(868E6)) {//914E6
    Serial.println("Starting LoRa failed!");
    while (1);
 }
  thing.add_wifi(SSID, SSID_PASSWORD);
}

void loop() 
{

  String lora_data;
  int lora_rssi;  

  int packetSize = LoRa.parsePacket();
  static int count = 0;
  if (packetSize)
  {
    Serial.print("packetSize = ");
    Serial.println(packetSize);
    
    // READ PACKET
    while (LoRa.available()) 
    {
      delay(100);
      lora_data = LoRa.readString();      
      Serial.print(lora_data);
      
       // what the lora_data looks like;
       //packetSize = 28
       //RHBS                DO: 5.94RSSI:-97
       //packetSize = 31
       //RHBS                TEMP: 21.68RSSI:-97

       if (lora_data.indexOf("TEMP") > ) -1{
       rtd = lora_data.substring(lora_data.indexOf("TEMP"))+ strlen("TEMP")).toFloat();
       Serial.print(rtd)
       }
       else if (lora_data.indexOf("DO") > ) -1{
       DO = lora_data.substring(lora_data.indexOf("DO")+ strlen("DO")).toFloat();
       Serial.print(DO)
       {
      thing["DO"] >> outputValue(DO);
      thing["rtd"] >> outputValue(rtd)
       }
      
    }

    lora_rssi = LoRa.packetRssi();
    Serial.print("RSSI:");
    Serial.println(lora_rssi);
  }

  thing.handle();
}

This is wrong! Check the position of ')' before -1

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.