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());

}
4

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

because use software serial in both codes , in one from arduino send the data o message, and with software serial read in the esp8266, while(NodeMCU.available()) {

  character = NodeMCU.read();

  content.concat(character);

}

use 9600 to print in monitor serie, and use 115200 for comunication, thats why this code solves this @sa_leinad , is a serial comunnication of two ways made by sodtware serial

0

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);
}
  • 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 '18 at 14:01