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).
"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="";
}
}
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.
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.