Serial communication between esp and arduino
Posted: Sun Sep 25, 2016 2:17 am
I am testing sending serial data from arduino mega to ESP (NodeMcu1.0) and vice versa . No problems in sending data from ESP to arduino . However, when I send data from arduino to esp (TX_arduino - RX_esp), I used a level shifter (LM2596 DC-DC )from 3.3-5 V between them , the data is lost and not received . But , when I directly attach the (TX"arduino"-RX"esp") it works perfect! I know that it is not recommended to do that and it will damage the board after all. I also tried to use a voltage divider but still no result.
I couldn't find any solutions on the net. I am stuck with this problem from the last week . Any one can help ?
I am using arduino IDE .Here is my code :
ESP code:
/*this code :
1- connects to the network. Then it waits unitl arduino mega sends "hello esp!" . When it recievs it sends "hello arduino " to the arduino
2- It recieves data throgh RX0 and sends data throgh TX1 .
3- It prints the ip address only.But I need to know the port too.
*/
#include "ESP8266WiFi.h"
String readString;
char c ;
int recieving_counter = 0;
// WiFi parameters to be configured
const char* ssid = "bt";
const char* password = "braintools" ;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
// Connect to WiFi
WiFi.begin(ssid, password);
// while wifi not connected yet, print '.'
// then after it connected, get out of the loop
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//print a new line, then print WiFi connected and the IP address
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
recieve_data();
if (recieving_counter>0){
recieving_counter=0;
send_data();
}
// always check to see if the esp is connected to the network
if (WiFi.status() == WL_CONNECTED){
}
else{//reconnect
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
}
void send_data(){
Serial1.println("Hello Arduino! ");
delay(3000);
}
void recieve_data(){
while (!Serial.available()){
// wait until the arduino sends the data
delay(1000);
Serial.println("- ");
}
while (Serial.available()){
Serial.print("r");
c = Serial.read();
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
recieving_counter++;
}
Serial.print(readString);
readString = ' ';
delay(2000);
}
Arduino code:
String readString;
char c ;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
send_data();
recive_data();
}
void send_data(){
//Serial.println("Hello Esp!");
Serial1.println("Hello Esp!");
delay(3000);
}
void recive_data(){
while (!Serial1.available()){
// wait until the esp sends the data
delay(1000);
Serial.println("* ");
}
while (Serial1.available()){
Serial.print("r");
c = Serial1.read();
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
Serial.print(readString);
readString = ' ';
//delay (500);
delay (2000);
}
I couldn't find any solutions on the net. I am stuck with this problem from the last week . Any one can help ?
I am using arduino IDE .Here is my code :
ESP code:
/*this code :
1- connects to the network. Then it waits unitl arduino mega sends "hello esp!" . When it recievs it sends "hello arduino " to the arduino
2- It recieves data throgh RX0 and sends data throgh TX1 .
3- It prints the ip address only.But I need to know the port too.
*/
#include "ESP8266WiFi.h"
String readString;
char c ;
int recieving_counter = 0;
// WiFi parameters to be configured
const char* ssid = "bt";
const char* password = "braintools" ;
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
// Connect to WiFi
WiFi.begin(ssid, password);
// while wifi not connected yet, print '.'
// then after it connected, get out of the loop
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
//print a new line, then print WiFi connected and the IP address
Serial.println("");
Serial.println("WiFi connected");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
recieve_data();
if (recieving_counter>0){
recieving_counter=0;
send_data();
}
// always check to see if the esp is connected to the network
if (WiFi.status() == WL_CONNECTED){
}
else{//reconnect
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
}
void send_data(){
Serial1.println("Hello Arduino! ");
delay(3000);
}
void recieve_data(){
while (!Serial.available()){
// wait until the arduino sends the data
delay(1000);
Serial.println("- ");
}
while (Serial.available()){
Serial.print("r");
c = Serial.read();
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
recieving_counter++;
}
Serial.print(readString);
readString = ' ';
delay(2000);
}
Arduino code:
String readString;
char c ;
void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
send_data();
recive_data();
}
void send_data(){
//Serial.println("Hello Esp!");
Serial1.println("Hello Esp!");
delay(3000);
}
void recive_data(){
while (!Serial1.available()){
// wait until the esp sends the data
delay(1000);
Serial.println("* ");
}
while (Serial1.available()){
Serial.print("r");
c = Serial1.read();
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
Serial.print(readString);
readString = ' ';
//delay (500);
delay (2000);
}