Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The technique in the 3rd example will be the most reliable.You can send data in a compatible format with code like this
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker
Also study the difference between Serial.write() and Serial.print() ...R
Okay, so it works!
I didn't really modify your code, But when I try to print to my LCD, the program just doesn't compile.
I imported <LiquidCrystal.h> just like the rest of my projects, but when I try to compile the program, the compiler says that 'lcd' was not declared in this scopeat every line I use the LCD command. Do you know what could be causing this?
Thank you for your very insightful guide on Serial Inputs and for helping me with this! Rock on!
Code of the ESP:
#include <ESP8266WiFi.h>
//ESP-8266
int y = 123456789;
void setup() {
Serial.begin(9600);
}
void loop() {
delay(500);
Serial.print('<'); // start marker
Serial.print(y);
Serial.println('>'); // end marker
}
Code of the Arduino:
#include <LiquidCrystal.h>
// Example 3 - Receive with start- and end-markers
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
lcd.begin(20, 4);
lcd.setCursor(10, 3);
}
void loop() {
recvWithStartEndMarkers();
showNewData();
}
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}