1

I want to send data from arduino to ESP8266. I have done all the RX and TX connections. The problem is that data is not sent every second, it stops after a single byte. When I remove the arduino cable then data is read.

My Arduino Code :

void setup() {
    Serial.begin(9600);
}

void loop() {

    Serial.write("50");
    delay(1000);
}

My ESP8266 Code :

void setup() {

Serial.begin(9600);

  // connect to wifi.
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {

    delay(500);
  }
  Serial.println("Connected !");

}   

void loop() {     

 Serial.print(Serial.readString());

}
0

2 Answers 2

5

Since you are Arduino Uno, it has only one Serial. This Serial can be used to transmit data in USART mode to another Serial Device as well as see any Debug data in the Serial terminal.

However, both the functionality can not be used simultaneously. The Tx Rx pair of Arduino can be connected to one pair of other device i.e. either to the computer via the cable or to the Tx Rx of ESP8266.

But if you really need to plug in the Uno to the computer and send data to ESP8266 simultaneously, then you have to use SoftwareSerial library to convert any digital pin to Tx Rx pin, and connect them to ESP8266 for transmitting data.

0

As explained by goddland_16. Using Software Serial library will solve the issue. Code is given below. By Initializing other digital pin as communication pin, Interupption between Tx and Rx and to ESP8266 is seperated.

Arduino UNO / Nano Code :

/*
 * bismillah hir rahman nir raheem
 * UNO/Nano = Pin 7 & Pin 8
 * Note: Uno and ESP8266 cross connection
 */

#include <SoftwareSerial.h>

SoftwareSerial ArduinoUno(7,8);
String f;

void setup() {
    Serial.begin(9600);
    ArduinoUno.begin(115200);
}

void loop() {
    float i = (random(100) + 1);
    float j = (random(100) + 1);

    f = String('H') + String(i) + String('T') + String(j);
    ArduinoUno.println(f);
    delay(2000);
}

ESP8266 Code :

/*
 * bismillah hir rahman nir raheem
 * ESP8266 = Pin D7 & Pin D8
 * Note: Uno and ESP8266 cross connection
 */

#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>

SoftwareSerial NodeMCU(D7,D8);

void setup() {
    Serial.begin(9600);
    NodeMCU.begin(115200);

    pinMode(D7,INPUT);
    pinMode(D8,OUTPUT);
}

void loop() {
    String content = "";
    char character;

    while (NodeMCU.available()) {
       character = NodeMCU.read();
       content.concat(character);
    }

    if (content != "") {
        Serial.println(content);
    }

    Serial.println("Temperature");
    Serial.println(content.substring(1, 6));
    Serial.println("Humidity");
    Serial.println(content.substring(7, 12));
    delay(2000);
}
1
  • 2
    Hi and welcome to the Arduino Stack Exchange. Please improve your answer by explaining to everyone what your code does and how it fixes the problem.
    – sa_leinad
    May 9, 2018 at 14:01