converting ascii to int

The values(i.e. sensor data which is an integer value) received by arduino from LoRa is an ascii value, but we require the values in integer form so how do we convert it.

.
Given below is our code
.
#include <SPI.h>
#include <LoRa.h>

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

Serial.println("LoRa Receiver");

if (!LoRa.begin(915E6)) {
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((int)LoRa.read());
}

// print RSSI of packet
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
.
.
[Serial.print((int)LoRa.read()); ] even though we have used this function, still we are not getting integer value in our output.
.
please help us out with this doubt soon....

The values(i.e. sensor data which is an integer value) received by arduino from LoRa is an ascii value, but we require the values in integer form so how do we convert it.

The normal way to do this would be to put each char as it arrives in an array until the message is complete (usually signaled by a special character such as Carriage Return or Linefeed) and terminate it with '\0' to turn it into a C style string (not a String) then use the atoi() function to turn the string into an integer.

However, I am not familiar with LoRa and the data format received so I don't know if this method is applicable in this case

If your data is numeric, send and receive it in binary. Then, no conversion required. If you're sending multiple values, put them in a struct and send that (in binary).

gfvalvo:
If your data is numeric, send and receive it in binary. Then, no conversion required. If you're sending multiple values, put them in a struct and send that (in binary).

Any example?

"The values(i.e. sensor data which is an integer value) received by arduino from LoRa is an ascii value, but we require the values in integer form so how do we convert it."

A very simple example of receiving ascii numbers and converting them to numbers using the String function.

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {

  while (Serial.available()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}

GolamMostafa:
Any example?

The exact function call depends on the particular library being used, but it will look something like:

For an Integer:

 int16_t integerToSend;

  integerToSend = 1234;
  radio.send(&integerToSend, sizeof(integerToSend));

For a struct:

  struct dataStructure {
    int16_t integerToSend;
    float floatToSend;
  };

  dataStructure dataToSend;

  dataToSend.integerToSend = 1234;
  dataToSend.floatToSend = 3.14159;
  radio.send(&dataToSend, sizeof(dataToSend));

Sending the data as ASCII simply makes no sense.

gfvalvo:
If your data is numeric, send and receive it in binary. Then, no conversion required. If you're sending multiple values, put them in a struct and send that (in binary).

Hello gfvalvo.

Isn't everything sent in binary? Data is always binary. It's the way we interpret the data that changes its meaning to numerical or string.

I'm struggling with this challenge too.

What are the functions you would use to send and receive in binary? Thanks.

John.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

What model Arduino are you using?
I have some code I used with String on an ESP32 that has worked for me.

Thanks. Tom.... :slight_smile:

Isn't everything sent in binary? Data is always binary.

Very true, but there is a world of difference between sending say '1' and 1

'1' is sent as 00110000 whilst 1 is sent as 00000001
'1' is a character representing the number 1 whilst 1 is the actual number 1

The receiving end needs to know the format in which the data was sent so that it can correctly interpret it

UKHeliBob:
'1' is sent as 00110000 [...]

The following table contradicts the above quote.

he following table contradicts the above quote.

Whoops. A brain fart on my part

'1' is sent as 00110001 (ie, HEX 31, decimal 49)

I think the OP has left the hanger with his/her answer, leaving others buzzing around in circles.