LoraWAN char to int

Hi I am using the MkrWAN 1310 with the following code from the tutorial:

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

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

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(868E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

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

    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
    }

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

The issue is that I am trying to take the value from LoRa.read() that is a char and convert it to an integer to do an analogWrite. I have tried atoi and strtol and both only covert the second digit if the value is two digits. If it's only one digit it's fine. For the atoi function I created an intermediary char array to resolve the char* issue. Am I missing something? Thanks!

Hello

read() will return one character of the number. So you have to read multiple characters, until the last character of the number is received.

You may not need to use a char array and atoi, maybe you can use an integer variable n that you initialize to 0, and everytime a digit character is received, multiply n by 10, add the digit character and substract character '0'. Similar to reading a number from a keypad, for example. It may be slightly more complicated if the number can be negative.

Yeah, that formula of converting a single digit definitely works but it only "keeps" the second digit. I guess my real issue is that, if a value is two digits and I am reading data that is incoming char by char, how do I know if it's part of the same number such as 12 vs 2? I'm even good with storing the values into an array but I can't figure out how to distinguish between storing/converting a two digit vs a single digit number when it's just a stream of chars.

Actually I figured out what the issue is but not how to solve it. If the number 12 is received, it comes over as integer value 4950 the two ascii values for 1 and 2. In the end I want the number 12 as an integer. Is there a way to break apart the bytes if it's two bytes long and store them into an array to do the conversion?

Here is a small example of what I poorly described and that you didn't understand : k5QSGM - Online C++ Compiler & Debugging Tool - Ideone.com

Since Lora transmission is packetized, there's no reason to send ASCII or "convert" anything. Pack all the datatypes you need to transfer into a struct and send / receive it in binary. Note ... I'd use a struct even if you're only transporting one value. That way you can easily scale it to multiple values later on.

have a look at thread decimals-strings-and-lora for example code transmitting structures with multiple data mebers over LoRa

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