i need only to read the "86" (it could be another number it is not always 86) and put it in a variable.
This is how i read message:
void loop()
{
updateSerial();
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
sim.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while (sim.available())
{
String sms = sim.readString();
Serial.println(sms);
}
}
Please can someone help me?
Thank you
Read the serial input and discard it until you get a Carriage Return, then use Serial.parseInt() or Serial.parseFloat() depending in data type, to read the value into a String
In general, it would be more correct to come up with some kind of protocol instead of sending a bare number. Something like "value=86" instead of just 86
Single number is very easy to confuse with some service message
{
delay(500);
while (Serial.available())
{
sim.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while (sim.available())
{
Serial.write(sim.read());
String sms = sim.readString();
Serial.println(sms);
{
str = sim.readStringUntil('\n');
x = Serial.parseInt();
}
Serial.println(str);
}
}
`but i don't receive nothing str and x``
I would speculate that the SIM899 is connected to a Serial port of some kind but as @Riccio99 has not posted a complete sketch let alone a schematic we can only gutess
#include <SoftwareSerial.h>
SoftwareSerial sim(10, 11);
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and SIM800L
sim.begin(9600);
Serial.println("Initializing...");
delay(1000);
sim.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
sim.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best
updateSerial();
sim.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
updateSerial();
sim.println("AT+CREG?"); //Check whether it has registered in the network
updateSerial();
sim.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
sim.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
}
void loop()
{
updateSerial();
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
sim.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while (sim.available())
{
String sms = sim.readStringUntil('\n');
Serial.println(sms);
//int index = sms.indexOf(';');
//String message = sms.substring(index);
//Serial.println(message);
}
}