I want to send a character string from one ESP8266 to another ESP8266 via the Tx and Rx pins.
The two processors are mounted on a breadboard, the Tx pin of one is connected to the Rx pin of the other. The two gnd pins are connected and the ESPs are powered separately via USB connectors.
The problem is that the received data is a series of digits, not the character string that was sent.
Here is the transmitter code
#define led_pin 2
void setup() {
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, HIGH); // off
Serial.begin(9600);
while (!Serial); // wait for Serial Monitor to open
}
void loop() {
Serial.println('aa;bb;cc\n'); // send data to Tx pin
digitalWrite(led_pin, LOW); // flash the on board LED
delay(500);
digitalWrite(led_pin, HIGH);
delay(10000);
}
and this the receiver code
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop() {
recvWithEndMarker();
showNewData();
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
The receive ESP is connected to a USB port on a PC, this is what is displayed on the monitor:
also, if you just looking for a single end-marker such as '\n', why not use Serial.readBytesUntil()?
if your considering doing something more elaborate, such as start bytes plus checksum and plan on sending binary data (not just ascii), you need a routine that checks for each starting byte, restarting if it finds bytes out of sequence and then capturing some specified # of bytes and finally checking the checksum.
println() includes '\n' (Newline character) which is transmitted as a frame; but, double quotes includes '\0` (null character) which is not transmitted; but, it is used by Sender to detect the end-of-string.
Simplified version of your non-blocking Receiver Sketch:
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
int index = 0;
void setup()
{
Serial.begin(9600);
Serial.println("<Arduino is ready>");
}
void loop()
{
byte n = Serial.available();
if(n != 0 ) //there is charcater in buffer
{
char rc = Serial.read():
if(rc != '\n`) //Newline charcater has not yet arrived
{
receivedChars[index] = rc; //save character
index++; //point next location of arry
if (index >= numChars)
{
index = numChars - 1; //keep last location of user array to hold null
}
}
else //Newline charcater has arrived
{
receivedChars[index] = '\0'; //insert null character
Serial.println(receivedChars); //show charcaters on serial Monitor
index = 0; //reset array index for the next cycle
}
}
}
why does that matter unless you start queueing up bytes to send but then wait a while before sending the rest or if your transmitting at a very low rate and need the time between chars to do something else
OP's receiver spends time in while() loop to receive characters which is similar to mine receiver spending time in main loop() in the non-blocking sketch of post #6.
Thank you so much for the many replies (and quickly too!)
Changing the transmitted string from single quite to to double quote does the job.
I was hoping to get away with using the monitor on Rx chip for the purpose of testing the data transfer. I'll now forward the received data to my NAS device via wifi and ftp.
The Tx chip will be acquiring data via NOW from a remote ESP chip.