so i want to connect two arduinos serially and send data from one and another. one arduino is connected to an array of sensors and the other is nodemcu which is connected to the internet and twitter.
the main arduino which is connected to the sensors will send a command like "command alert" to the nodemcu using Serial.print(). nodemcu must decode the command which is in the format "command xx".
here is the code.
i am currently testing this code with a nano which is connected to the computer.
void setup() {
// initialize both serial ports:
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
Serial.println("waiting for a command");
String c = Serial.readString();
Serial.println("the command recieved is");
Serial.println(c);
Serial.println("decoding the command");
if (c.substring(8) == "alert") {
Serial.println("intruder detected");
}
}
}
the output on the serial monitor is
waiting for a command
the command recieved is
command alert
decoding the command
so i dont understand what might the problem be. maybe in the received string there maybe some garbage values at the end of the string. Any help is much appreciated.
in my final project the arduino uno will get the sensor values and send to the nodemcu(i have added the boards for esp8266 to arduino ide) nothing will be connected to the compute.
right now im just testing this code with a nano connected to the computer. my computer is sending the inputs and nano is receiving. if this work i will later incorporate this to arduino+nodemcu(esp8266 12e module)
means to read the characters from#8 position to the end.
Try
substring(0, 5);
(from character 0 to character 5, exclusive.)
I would suggest that you look at serial input basics to see how to use null terminated character arrays (AKA c-strings) for serial communication. Strings (big S or String class objects) can cause hard to find problem in Arduino sketches.
I'd suggest that you print strings (or resource wasting Strings) between markers that are not part of the text, so you can see exactly what the string (or String) contains.