Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By Mohammed Ubaid
#46232 Hello all,

I need help with receiving a data packet from Arduino TX to ESP8266 RX hardwired.

I am using arduino IDE and arduino part is fine, i have few variables that i put in an array and sent is using the command "Serial.write(array)".

But for the esp8266 part, on receiving side, how do i receive this array?

Serial.read or Serial.readString() none of these are working...? I am getting a compilation error
Thanks in advance..
Here is my code
Code: Select all// Import required libraries
#include "ESP8266WiFi.h"

String msg[1000];

// WiFi parameters
const char* ssid = "lenovo";
const char* password = "password";
void setup(void)
{
// Start Serial
Serial.begin(9600);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
Serial.println("I am in loop, now the data part");
msg = Serial.read();
Serial.println(msg);

}
User avatar
By martinayotte
#46247 With "String msg[1000];", you are not declaring a string of 1000 character, but declaring an array of 1000 strings.
You should declare it as "char msg[1000];" ... :ugeek:

Also, in your code, you are reading the serial with "msg = Serial.read();", this read only one character at a time, and you also don't check if there is actually any character to read. :geek:

You should have some code similar to that :

Code: Select allvoid loop() {
  if (Serial.available()) {
    int inByte = Serial.read();
    Serial.write(inByte);
    msg[i] = (char)inByte;
    if (inByte == '\r' or inByte == '\n' or i > (sizeof(msg) - 1)) {
      msg[i] = 0;
      i = 0;
      Serial.print("echo = ");
      Serial.println(msg);
    }
    else
      i++;
  }
}
User avatar
By Mohammed Ubaid
#46288 Thank you Mr.Martin,

Actually i need some help, I have been over this since a week.

My requirement is, I am writing some data on arduino's TX pin using the code:
Code: Select allsprintf(msg, "SMKBXX,SMCXXX,%d,%d,%d,%d",pPconL,pPerL,0,0);
Serial.write(msg);


I need to receive this "msg" packet on my nodeMCU esp8266.

I connected the TX of arduino to my nodemcu RX through a voltage divider of 1.2k and 2.4k resistors.

I just used the code you suggested, but data is not being received on my nodemcu.

Baudrate for both i have set as 9600.

What can be done..??

Thanks in advance