Get error with .toInt()

Hi Everyone.

I am using the LoraReceiver and LoraSender examples.

The sending and receiving works fine but I want to use real values to be able to switch a LED on or off if a certain value is received.

Trying to convert the received string into a INT gives me an error.

#include <LoRa.h>

int counter = 0;
long int myValue = 1010001;
void setup() {
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Sender");

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

void loop() {
  Serial.print("Sending packet: ");
 // Serial.println(counter);

  // send packet
  LoRa.beginPacket();
 
  LoRa.print(5);//
  delay(2000);
  LoRa.print(9);//
  delay(2000);
  LoRa.endPacket();

  //counter++;

  
}

#include <SPI.h>
#include <LoRa.h>
#include <LiquidCrystal.h> //Arduino standard LCD Library

//
//                RS   E  D4  D5  D6  D7
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

char incoming ;
int rxData;
void setup()
{
  Serial.begin(9600);
  //lcd.begin(16, 2);
  // lcd.setCursor(0, 0);
  lcd.print("Lora Receiver");//This works
  delay(500);
  lcd.clear();

  lcd.setCursor(0, 0);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(433E6)) {
    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())

    {
      incoming = ((char)LoRa.read());//char
      //Serial.print((char) LoRa.read());

      Serial.println(incoming);

      // lcd.print(incoming);
      rxData = (incoming).toInt();//This gives a error

    }
  }
}
Arduino: 1.8.12 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"

C:\Users\mike\Desktop\LoRaReceiver\LoRaReceiver.ino: In function 'void loop()':

LoRaReceiver:55:27: error: request for member 'toInt' in 'incoming', which is of non-class type 'char'

       rxData = (incoming).toInt();//This gives a error

                           ^~~~~

exit status 1
request for member 'toInt' in 'incoming', which is of non-class type 'char'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Thanks in advance.

Hi
Test like this:
rxData = incoming.toInt();
if "incoming" is a string!!!!

RV mineirin

How about:

rxData = incoming;

Can't see why you need to do it though.

 rxData = incoming.toInt();//This gives a error

Still get same error.

Can't see why you need to do it though.

Need to use the value I get so to decide on the next action .. for example stop or start a pump.

you send a char and are receiving a char in ASCII. just do (incoming - '0') to get the original value or test against '9' for example

EDIT

actually you send two chars in the transaction (not sure why you delay...)

  LoRa.print(5);//
  delay(2000);
  LoRa.print(9);//

so the payload on the other side will be 2 bytes with ASCII codes '5' and '9'.

try with this (typed here, no clue if it works)


void loop() {
  size_t packetSize = LoRa.parsePacket();
  if (packetSize) {
    char payload[packetSize + 1];
    for (size_t index = 0; index < packetSize; index++)  payload[index] = LoRa.read();
    payload[packetSize] = '\0'; // terminate the cString
    char* endPtr = nullptr;
    long value = strtol(payload, &endPtr, 10); // https://www.cplusplus.com/reference/cstdlib/strtol/
    if (endPtr != payload) {
      Serial.print("Decimal value read = "); Serial.println(value);
    } else {
      Serial.print("Could not read a Decimal value from "); Serial.println(payload);
    }
  }
}

.toInt() works on String variables, not on individual characters.

I made this changes on the transmitter yes it was sending 2 values in the payload not what I wanted.Now I get 2 separate values 2 seconds appart.

  LoRa.print(5);//
  LoRa.endPacket();
  delay(2000);

  LoRa.beginPacket();
  LoRa.print(9);//
  LoRa.endPacket();
  delay(2000);

Then in the receiver made this changes.The serial monitor prints the 5 as 53 and the 9 as 57 .

 while (LoRa.available())

    {
      incoming = ((char)LoRa.read());//char
      //Serial.print((char) LoRa.read());

      Serial.println(incoming);

      // lcd.print(incoming);
      rxData = incoming - 0;//
      Serial.println(rxData);
    }
    if (rxData == 5)
    {
      digitalWrite(A0, HIGH);
    }

    if (rxData == 9)
    {
      digitalWrite(A0, LOW);
    }

  }
}

When I change the 5 and 9 to 53 and 57 the LED switches on and off ,still need to get the 5 and 9.

      rxData = incoming - '0';//

Dont know why the " LoRa.beginPacket();" is cut of on the top line it is actually still yellow in the sketch were I copy it.

print sends the ASCI representation of your number 5 becomes '5' (as a character) so you can test with

if (rxData == '5')

or convert it as @wildbill suggested

if you want to send the raw binary value, use write() (if the data fits in a byte)

I still don't think you need to do any conversion at all. Just work with incoming and deal with the values you get.

Further to that point, having those magic numbers (q.v.) in the code isn't a good idea. Better to create some named constants containing the values you get and then the code can use your nicely named constants rather than hard coded numbers.

Thanks this works , since I will also print the values sending it as ASCII is better.

Really not sure what you refer to as magic numbers but I used the 5 and 9 just for a easy testing , building the code up from simple tested lines .I will eventually use analogRead and then send that.

Thanks a lot I learned a lot and achieved my first goal.

that's programmer's language for saying you sprinkle those values around the code and it's hard to know what they mean. if you were to do

const byte moveForward = '5';
const byte moveBackward = '9';

and then do LoRa.print(moveForward); in the sender or if (rxData == moveForward) in the receiver, code would be way easier to read for everyone (and maintain in the future if you need to come back to it)