Communication between arduino and esp8266-01

Today I managed to get an esp8266-01 module to make a server on my local network and send data. I then decided to try writing a separate arduino code to send information to the esp module and the esp module to send the received information to the local server. The problem is that the ESP module creates the server but does not receive the information from the arduino.
Arduino code:

#include <SoftwareSerial.h>

SoftwareSerial espSerial(2, 3); // RX, TX

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

void loop() {
  // Send number 1 to ESP8266
  espSerial.println("1");

  delay(1000); // Wait for 1 second
}

ESP code:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";

ESP8266WebServer server(80);

String receivedNumber = "";

void handleRoot() {
  String message = server.arg("plain");
  receivedNumber = message;
  Serial.print("Received number: ");
  Serial.println(receivedNumber);

  server.send(200, "text/plain", "OK");
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);
  server.begin();
}

void loop() {
  server.handleClient();
}

Have you taken the different voltages of the 2 boards ?

Have the boards got a common GND connection as they should have ?

ESP is connected to arduino GND and 3.3v and arduino is powered with usb

the UNO uses 5V logic so you should have a potential divider on the UNO Tx line to convert the 5V signal to 3V for the ESP-01, e.g. here I am using UNO pins 8 and 9

also rather than SoftwareSerial I use Alt SoftSerial, e.g.

// UNO - ESP-01 AltSoftSerial test - ESP-01 loopsback - character transmitted to it are echoed back
//  requires program ESP-01_Serial_loopback loaded in to ESP-01 and serial connections (see below)
//  note that at baudrates above 38400 some echoed characters are corrupted

#include <AltSoftSerial.h>

// load program ESP-01_Serial_loopback in to ESP-01
// ESP-01 U0TXD to UNO RX pin 8
// ESP-01 U0RXD to UNO TX pin 9
// ESP-01 GPIO0 and GPIO2 need to have pull-up resistors connected to ensure the module starts up correctly
// The ESP-01S has 12K resistors on the board for GPIO0, RST and CH_PD 

AltSoftSerial espSerial;

void setup() {
  Serial.begin(115200);
  Serial.println("AltSoftSerial test");
  //Begin serial communication with Arduino and SIM800L
  espSerial.begin(38400);
  Serial.println("ESP-01 serial intialized");
  Serial.println("load ESP-01_Serial_loopback in to ESP-01");
  Serial.println("ESP-01 U0TXD to UNO RX pin 8");
  Serial.println("ESP-01 U0RXD to UNO TX pin 9");
}

void loop() {
  if (Serial.available()) {
    char command = Serial.read();
    //Serial.println(command);
    espSerial.print(command);
  }
  while (espSerial.available()) {
    char reponse = espSerial.read();
    Serial.print(reponse);
  }
}

In your code i do not see any part that actually does the Serial reception ? Your UNO code does send
"1/n" , but nowhere do you do anything about reception on the ESP.

And all of the above (seldom do i see a post where so much information is correct), observe also the Baudrate on softwareSerial, i does ot get mentioned (though corrected) but 115200 is not a very reliable baudrate for softwareSerial, for sure not on reception, and a bit iffy on transmission

In the diagram above, I don't see 3.3V supplied to the CH_PD/EN pin of the ESP MODULE

Apparently the problem is that I can't write the code correctly so that the ESP module receives the information from the arduino.

correct - CH_P/EN needs to be connected to 3v3 to enable the ESP

I am using esp-01S which has on board pull-up for CH-PD (normal esp-01 doesn't therefore needs a pullup)

more details How-to-use-the-ESP8266-01-pins

do you have an ESP-01 or ESP-01S?

i use ESP-01.

But the OP has the webserver working...

that is what i am saying, yes. Or at least, you haven't yet.

String message = server.arg("plain");
  receivedNumber = message;
  Serial.print("Received number: ");
  Serial.println(receivedNumber);

This actually looks like the opposite of what you intended. It takes an argument from the server (chances are this argument doesn't exist but that is a different matter)
and writes that to the Serial port. Whereas you want read from the Serial port, and display on the webpage.

I made a connection between еsp and arduino but I can't between arduino and esp:
for ESP

void setup()
{
 Serial.begin(9600);
}
void loop(){
    Serial.write("Hello from ESP");
    delay(2000);
 
 }

For Arduino

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
 
void setup()
{
 Serial.begin(9600);
 mySerial.begin(9600);
 delay(5000);
 }
 
void loop(){
  
 String IncomingString="";
 boolean StringReady = false;
 
 while (mySerial.available()){
   IncomingString=mySerial.readString();
   StringReady= true;
  }
 
  if (StringReady){
    Serial.println("Received String: " + IncomingString);
  
  }
 }

I don't think it's a perfect method but there should be no reason why almost the same should not work on the ESP unless there is a hardware issue.
Main thing is that transmission at 9600bps takes about 1 ms / byte, and reception is much faster.
So far you have succeeded because the whole string has been transmitted (and received) before parsing starts.
There are loads of examples for Serial transmission and reception to be found, many of which are none-blocking in nature, this is what you want.
have a look at Serial input basics

The problem with this module is that I have no way to follow through the serial monitor what happens to it if the information is transmitted from the arduino to the esp. I think it will be much easier if I buy an esp32 which I can always monitor in real time.

That is not true strictly speaking, There are 2 available Serial output pins available, but you would need to receive that information and convert it from TTL to USB. Another option would be for the ESP to just echo back whatever it receives. I have Serial transmission between boards stable, but i still use the builtin led on GPIO 1 (also TX0 ) to let me know where i am in a program. But if you think a nodeMCU orESP32 will solve your problem it could be your solution. As a guitarist i always figure that more practice is more important than a better guitar. The practice can also be applied on another guitar.
One of the questions i have with your current project is 'why ?' do you want to send data from an arduino to an ESP, but i assumed 9incorrectly) that it was for educational purposes.

  espSerial.begin(115200);

SoftwareSerial does not work well, or at all, for speeds above about 38400 Baud.

very simple test program
ESP-01S reads characters from serial U0RXD (GPIO3) and echo back to U0TXD (GPIO1)

//  ESP-01 blink LED and read characters from serial U0RXD  and echo back to U0TXD

void setup() {
  Serial.begin(38400);
  delay(1000);
  Serial.println();
  Serial.println("\n\nESP-01 blinking LED and read characters from serial U0RXD  and echo back to U0TXD ");
  Serial.print("LED blinking pin ");
  Serial.println(LED_BUILTIN);
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  static long timer = millis();
  if (millis() - timer > 500) {
    timer = millis();
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }
  // read serial U0RXD and echo it back to U0TXD
  while (Serial.available() > 0) {
    char ch = Serial.read();
    Serial.write(ch);
  }
}

this UNO program reads on pins 8and transmis on pin 9

// UNO - ESP-01 AltSoftSerial test - ESP-01 loopsback - character transmitted to it are echoed back
//  requires program ESP-01_Serial_loopback loaded in to ESP-01 and serial connections (see below)
//  note that at baudrates above 38400 some echoed characters are corrupted

#include <AltSoftSerial.h>

// load program ESP-01_Serial_loopback in to ESP-01
// ESP-01 U0TXD to UNO RX pin 8
// ESP-01 U0RXD to UNO TX pin 9
// ESP-01 GPIO0 and GPIO2 need to have pull-up resistors connected to ensure the module starts up correctly
// The ESP-01S has 12K resistors on the board for GPIO0, RST and CH_PD 

AltSoftSerial espSerial;

void setup() {
  Serial.begin(115200);
  Serial.println("AltSoftSerial test");
  //Begin serial communication with Arduino and SIM800L
  espSerial.begin(38400);
  Serial.println("ESP-01 serial intialized");
  Serial.println("load ESP-01_Serial_loopback in to ESP-01");
  Serial.println("ESP-01 U0TXD to UNO RX pin 8");
  Serial.println("ESP-01 U0RXD to UNO TX pin 9");
}

void loop() {
  if (Serial.available()) {
    char command = Serial.read();
    //Serial.println(command);
    espSerial.print(command);
  }
  while (espSerial.available()) {
    char reponse = espSerial.read();
    Serial.print(reponse);
  }
}

connect as diagram in post 4, e.g.

  1. ESP-01 pin GPIO1 U0TXD to UNO pin 8 RX
  2. ESP-01 pin GPIO3 U0RXD to UNO pin 9 TX

text entered on the UNO serial monitor to transmitted to the ESP-01 which echoes it back and displayed on the serial monitor

as @jremington stated in post 18 the UNO software serial does not work well at high baudrates
the above UNO code uses AltSoftSerial at 38400baud

yes code work but i see only this symbol in console: �|���L4.

addition: I fixed this problem and everything is fine now. Thank you very much.
Today is the third day I've been struggling

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.