sandeepmistry / arduino-LoRa

An Arduino library for sending and receiving data using LoRa radios.
MIT License
1.61k stars 621 forks source link

Separate the packet received #568

Open Victorio1209 opened 2 years ago

Victorio1209 commented 2 years ago

Hello!

My problem is that I am receiving a packet with name and value, but I need to separate these. I need to separate the "buff", the packet received is: Received packet 'Bovino20.00'. I would like to return in this example: String ->Bovino; float-> 20.00;

#include <SPI.h>
#include <LoRa.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ThingsBoard.h>    // ThingsBoard SDK
#include <stdlib.h>
#include <stdio.h>

char ssid[] = "Mariani";
char pass[] = "yu-gi-oh";
#define TOKEN               "HqKUEKoQx6Mht14PCguC"
#define THINGSBOARD_SERVER  "demo.thingsboard.io"

// Initialize ThingsBoard client
WiFiClient espClient;
// Initialize ThingsBoard instance
ThingsBoard tb(espClient);
// the Wifi radio's status
int status = WL_IDLE_STATUS;

char *Bovinos[]= {"Bovino1", "Bovino2"};

void setup() {
  Serial.begin(9600);
  while (!Serial);

  WiFi.begin(ssid, pass);
  InitWiFi();

  Serial.println("LoRa Receiver");
  LoRa.setPins(18, 14, 26);
  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {

  if (WiFi.status() != WL_CONNECTED) {
  reconnect();
  }
   if (!tb.connected()) {
    // Connect to the ThingsBoard
    Serial.print("Connecting to: ");
    Serial.print(THINGSBOARD_SERVER);
    Serial.print(" with token ");
    Serial.println(TOKEN);
    if (!tb.connect(THINGSBOARD_SERVER, TOKEN)) {
      Serial.println("Failed to connect");
      return;
   }
  }
    // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    // read packet
        char buff[32] = {0};
    // adjust size if needed
    uint8_t ix = 0;
    while (LoRa.available()) {
      buff[ix++] = LoRa.read();
    }
    Serial.print(buff);
    tb.sendTelemetryFloat("Bovinos", atof(buff));
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
    tb.loop();
}
Kongduino commented 2 years ago

The issue here is that in order to separate the key from the value, you're going to need a separator, like, or \t, or have a fixed format, like 8 chars for name and then your float.

You'll need to do some basic C string manip. Which is beyond the scope of this library.