SoftwareSerial between ESP8266-01 and Arduino Nano

Hello Community,

i try to Recive and Send Data between the ESP8266-01 and the Arduino Nano over SoftwareSerial but
i recive only numbers or wrong strings "_??"

here is my Code.

ESP8266-01:

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServerSecure.h>
#include <ESP8266mDNS.h>

extern "C" {
  #include <user_interface.h>
}

uint8_t mac[6] {macadresse};

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,1);

#ifndef STASSID
#define STASSID "wlan"
#define STAPSK  "pw"
#endif

const char* ssid = STASSID;
const char* password = STAPSK;

BearSSL::ESP8266WebServerSecure server(443);

static const char serverCert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----

-----END CERTIFICATE-----
)EOF";

static const char serverKey[] PROGMEM =  R"EOF(
-----BEGIN RSA PRIVATE KEY-----

-----END RSA PRIVATE KEY-----
)EOF";

const int led = 13;

void handleRoot() {
  digitalWrite(led, 1);
  mySerial.write("B");
  Serial.write("A");
  Serial.println("N");
  String IncomingString = "";
  boolean StringReady = false;
  /*while(mySerial.available()){
    IncomingString=mySerial.readString();
    StringReady = true;
  }
  if(StringReady){
    server.send(200, "text/plain", "Hello from esp8266 over HTTPS!");
    digitalWrite(led, 0);
  }*/
    server.send(200, "text/plain", "Hello from esp8266 over HTTPS!");
    digitalWrite(led, 0);
}

void handleNotFound(){
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET)?"GET":"POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i=0; i<server.args(); i++){
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void){
  wifi_set_macaddr(0, const_cast<uint8*>(mac));
  
  pinMode(led, OUTPUT);
  digitalWrite(led, 0);

  uint32_t baud = 9600;
  mySerial.begin(baud);
  
  Serial.begin(baud);  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));

  server.on("/", handleRoot);

  server.on("/inline", [](){
    server.send(200, "text/plain", "this works as well");
  });

  server.onNotFound(handleNotFound);

  server.begin();
  Serial.println("HTTPS server started");
}

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

ArduinoNano:

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <cactus_io_BME280_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);// RX,TX

BME280_I2C bme(0x76);

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

const int touchSw = 4;
const int led_pin = 13;


void setup() {  
  uint32_t baud = 9600;
  mySerial.begin(baud);
  
  pinMode(touchSw,INPUT_PULLUP);
  pinMode(12, INPUT);
 
  Serial.begin(baud); 


  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

    for (int i=0; i<2; i++){
        delay(500);                       
        digitalWrite(LED_BUILTIN, LOW); 
        delay(500);
        digitalWrite(LED_BUILTIN, HIGH);
    }
    
  }

  if (!bme.begin()) {

    display.clearDisplay();
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(10, 20);
    display.println("Bitte Überprüfen sie die verbindung von BME280!");
    display.display();

    while (1);
  }

  bme.setTempCal(-1);
 
  pinMode(led_pin, OUTPUT); //starte led ausgabe auf pin 13
  
}

//int Variable1;

void loop() {

  bme.readSensor();
  String Data = "";
  char a;
  while(mySerial.available() > 0){
    //a = mySerial.read();
    Serial.print(mySerial.read());
    //Data.concat(a);
    //if(a == '\n'){
    //  Serial.println(a);
    //  //if(a != '\0'){
    //    if(a == 'B'){
    //      mySerial.write(bme.getPressure_MB());mySerial.write("mbar");
     //     mySerial.write(bme.getTemperature_C());mySerial.write("°C");
     //     mySerial.write(bme.getHumidity());mySerial.write("%");
     //   }
      //}
   // }
  }

  boolean touchState;
  touchState = digitalRead(touchSw); //pin 4 auslesen

  //display.println(touchState);
  
  if(touchState == 1)
  {
    //Serial.print("On");
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    //Serial.print("Off");
    digitalWrite(LED_BUILTIN, LOW); 
  }

  display.clearDisplay();
  
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  //display.println(Variable1);
  display.print(bme.getPressure_MB());display.println("mbar");
  display.print(bme.getTemperature_C());display.println("°C");
  display.print(bme.getHumidity());display.println("%");
  
  display.display();      // Show initial text

}

I have actually connected GPIO1 from Esp8266 with the D3 from arduino and D2 from Arduino to the GPIO0 from ESP8266 with a 1k ohm resistance.

I dont know what i m doing wrong. i Use the Arduino IDE. and for the ESP http://arduino.esp8266.com/stable/package_esp8266com_index.json

Regards Michael N.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

The examples can also be used with SoftwareSerial

...R

I have already read it and i try it. Read my code this is exact same. The Problem is i sending an A and become "_??" oder a number like 02224.

Srry but i m really enjoy when you read the post not only the title.

Carry out the following simple UART Communication between Node and UNO/NANO.
1. Connections:
D5 (SRX) of Node with 3 (STX) of UNO/NANO
D6 (STX) of Node with 2 (SRX) of UNO/NANO
G/GND of Node with GND of UNO/NANO
NoderMCUPic.png
Figure-1:

2. Node Codes:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(D5, D6);  //SRX = D5. STX = D6

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

void loop()
{
  byte n = mySerial.available();
  if(n != 0)
  {
    char x = mySerial.read();
    Serial.print(x);
  }
}

3. UNO/NANO Codes:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);  //SRX = 2, STX = 3

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

void loop()
{
  mySerial.println("Hello Node.");
  delay(1000);
}

4. Node Screenshot:
sm-1.png
Figure-2:

NoderMCUPic.png

sm-1.png

Srry that was my bad i mean the ESP8266-01.

ESP-01-Pin-Out.png

When i m at home i make a new example without any other code to show you what i mean.

ESP-01-Pin-Out.png

Hiow do you program ESP8266-01?

Over an Clear Arduino Nano and TX RX or over https://www.amazon.de/AZDelivery-USB-zu-ESP8266-Serial-Wireless-Module/dp/B0743GNGRL/ref=sr_1_1_sspa?__mk_de_DE=ÅMÅŽÕÑ&keywords=esp8266+programmer&qid=1576148522&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExWk9NR1BST1hGV1lNJmVuY3J5cHRlZElkPUEwMDkxODM0MVpTWlc3MkVWV0NXRiZlbmNyeXB0ZWRBZElkPUEwMTkzNjQxVEs1Wjc5RUdUUFVMJndpZGdldE5hbWU9c3BfYXRmJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ== but for the programming over arduino nano i have another circuit diagramm as when i run the esp with the arduino nano. then i use the arduino nano 3v3 volt for programming and for running i use a extra powersuply

Trackhe:
I have already read it and i try it. Read my code this is exact same.

I can see nothing in either of the programs in your Original Post that bears any resemblance to the examples in Serial Input Basics or my suggested means of sending data.

If I have overlooked something in your code then please mention the relevant line numbers.

...R

So this is the simpelst example. here is the ESP8266-01 the sender and the Arduino Nano the Reciver than i know only how to read Serial on the Arduino. my Result:

18:01:37.666 -> <Arduino is ready>
18:01:39.170 -> <Arduino is ready>
18:02:15.129 -> This just in ... ⸮
18:02:28.078 -> This just in ... ⸮
18:02:28.116 -> This just in ... ⸮
18:02:28.116 -> This just in ... ⸮
18:02:28.153 -> This just in ... ⸮
18:02:28.153 -> This just in ... 
18:02:28.191 -> This just in ... ⸮
18:02:28.228 -> This just in ... 
18:02:28.228 -> This just in ... ⸮
18:02:28.266 -> This just in ... v
18:02:28.266 -> This just in ... ⸮
18:02:28.266 -> This just in ... [
18:02:28.303 -> This just in ... 7
18:02:28.303 -> This just in ... 
18:02:28.341 -> This just in ... 
18:02:28.379 -> This just in ... 7
18:02:28.379 -> This just in ... ⸮
18:02:28.413 -> This just in ... v
18:02:28.450 -> This just in ... ⸮
18:02:28.450 -> This just in ... ⸮
18:02:28.487 -> This just in ... 7
18:02:28.487 -> This just in ... 
18:02:28.523 -> 
18:02:28.523 -> This just in ... x
18:02:28.561 -> This just in ... ⸮
18:02:28.561 -> This just in ... '
18:02:28.597 -> This just in ... '
18:02:28.635 -> This just in ... !
18:02:28.635 -> This just in ... ⸮
18:02:28.635 -> This just in ... c
18:02:28.635 -> This just in ... !
18:02:28.673 -> This just in ... 7
18:02:28.710 -> This just in ... 
18:02:28.710 -> This just in ... M
18:02:28.746 -> This just in ... ⸮
18:02:28.784 -> This just in ... ⸮
18:02:28.784 -> This just in ... 
18:02:29.213 -> This just in ... ⸮
18:02:29.213 -> This just in ... :
18:02:29.246 -> This just in ... :
18:02:29.246 -> This just in ... 
18:02:29.284 -> 
18:02:29.284 -> This just in ... ⸮
18:02:29.320 -> This just in ... e
18:02:29.320 -> This just in ... ⸮
18:02:29.356 -> This just in ... 7
18:02:29.394 -> This just in ... 5
18:02:29.394 -> This just in ... ⸮
18:02:29.430 -> This just in ... ⸮
18:02:29.430 -> This just in ... ⸮
18:02:29.468 -> This just in ... 
18:02:30.246 -> This just in ... ⸮
18:02:30.246 -> This just in ... :
18:02:30.283 -> This just in ... :
18:02:30.283 -> This just in ... 
18:02:30.318 -> 
18:02:30.318 -> This just in ... ⸮
18:02:30.356 -> This just in ... e
18:02:30.356 -> This just in ... ⸮
18:02:30.393 -> This just in ... 7
18:02:30.428 -> This just in ... 5
18:02:30.428 -> This just in ... 
18:02:30.428 -> This just in ... +
18:02:30.428 -> This just in ... ⸮
18:02:30.466 -> This just in ... 
18:02:31.268 -> This just in ... ⸮
18:02:31.268 -> This just in ... :
18:02:31.306 -> This just in ... :
18:02:31.306 -> This just in ... 
18:02:31.343 -> 
18:02:31.343 -> This just in ... ⸮
18:02:31.343 -> This just in ... e
18:02:31.343 -> This just in ... ⸮
18:02:31.380 -> This just in ... 7
18:02:31.418 -> This just in ... 5
18:02:31.418 -> This just in ... ⸮
18:02:31.456 -> This just in ... ⸮
18:02:31.456 -> This just in ... ⸮
18:02:31.493 -> This just in ... 
18:02:32.145 -> This just in ... 
18:02:32.145 -> This just in ... 
18:02:32.291 -> This just in ... ⸮
18:02:32.291 -> This just in ... :
18:02:32.291 -> This just in ... :
18:02:32.291 -> This just in ... 
18:02:32.329 -> 
18:02:32.329 -> This just in ... ⸮
18:02:32.366 -> This just in ... e
18:02:32.366 -> This just in ... ⸮
18:02:32.403 -> This just in ... 7
18:02:32.437 -> This just in ... 5
18:02:32.437 -> This just in ... ⸮
18:02:32.475 -> This just in ... ⸮
18:02:32.475 -> This just in ... ⸮
18:02:32.512 -> This just in ...

(Sender)ESP8266-01:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,1);

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

void loop()
{
  mySerial.println("Hello Node.");
  delay(1000);
}

(Reciver)Arduino Nano:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);// RX,TX

char receivedChar;
boolean newData = false;

uint32_t baud = 9600;

void setup() { 
  Serial.begin(baud);
  mySerial.begin(baud);
  Serial.println("<Arduino is ready>");
}

void loop() {
    recvOneChar();
    showNewData();
}

void recvOneChar() {
    if (mySerial.available() > 0) {
        receivedChar = mySerial.read();
        newData = true;
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChar);
        newData = false;
    }
}

I can also provide a simplified schematic if needed. My last option would be because ESP over Arduino Nano to power over because 5v pin with resistance because I have no voltage divider. I hope you can help me now. I've been trying different things for 4 days.

You may try uploading the following sketch in NANO (receiver):

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //SRX =2, STX = 3

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

void loop()
{
  byte n = mySerial.available();
  if(n != 0)
  {
     char x = mySerial.read();
     Serial.print(x);                //the screen will should show: Hello Node at a sec interval
  }
}

Result:

18:33:56.398 -> ⸮⸮⸮⸮v⸮S?7⸮v۾?
18:33:58.988 -> x⸮''!⸮c!7M⸮⸮⸮::
18:34:00.007 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:01.013 -> ⸮e⸮75⸮⸮⸮::
18:34:02.055 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:03.068 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:04.064 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:05.097 -> ⸮e⸮75⸮⸮y=⸮::
18:34:06.092 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:07.110 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:08.130 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:09.148 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:10.167 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:11.155 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:12.181 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:13.194 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:14.214 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:15.240 -> ⸮e⸮75+⸮⸮::
18:34:16.240 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:17.254 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:18.279 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:19.264 -> ⸮e⸮75⸮⸮⸮⸮::
18:34:20.298 -> ⸮e⸮75⸮⸮⸮

and over because arduino because I can not run ESP. even though it only consumes 0.03A. at least not without a voltage divider. I'm just watching if I can use ESP programmer as a ttl adapter to read out because of serial ESP.

From Reply #8 ...

So this is the simpelst example.

The receive code in your nano is not appropriate - you are not sending single characters. Use the code from the second example in Serial Input Basics

...R

Check that you have selected correct baud rate (Bd = 9600) in the Serial Monitor of NANO.
SerialMonitor.png

SerialMonitor.png

Reciver Nano:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); //SRX =2, STX = 3

// Example 2 - Receive with an end-marker

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup()
{
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
    while (mySerial.available() > 0 && newData == false) {
        rc = mySerial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

Result:

Trackhe:
Result:

?

Trackhe:
?

Did you read Reply #11 ?

...R

Yes i have done it. In #13

So after you kidding me, have anyone an idea why this dont work? Is it possible that it is the extra power supply?

Ok i don't usually but into threads that have so many responses already but there are some things that needs to be addressed. First of all,

#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,1);

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

void loop()
{
  mySerial.println("Hello Node.");
  delay(1000);
}

The ESP has hwSerial on pins 1 (TX) & 3 (RX), declaring a swSerial object with the same pins is asking for trouble (and pointless !! )

I can also provide a simplified schematic if needed. My last option would be because ESP over Arduino Nano to power over because 5v pin with resistance because I have no voltage divider. I hope you can help me now. I've been trying different things for 4 days.

You probably should provide us with some kind of schematic, but power a ESP-01 with 5v is breaking it for sure. and the nano's 3.3v pin will not be sufficient current. You will need either a 3.3v regulator (or a LM317 tuned to that) or if powered through a board, an UNO (my UNO which is an original rev3 ) does do it.
If you just want to confirm that the ESP-01 is working, you can load an empty sketch onto the UNO and connect the ESP-01 as if your are uploading a sketch (but not have the GPIO 0 pulled to ground at startup) and then you can communicate over Serial directly. You can power the ESP-01 using the external powersupply as long as you connect GND on the Nano to GND on the ESP