Arduino and ESP8266-01 serial communication (5V to 3.3V)

LesJones

Joined Jan 8, 2017
4,544
As I said in post #5 I don't think the software serial port will work reliably for two way communication at 115200 bauds. Why do you want to put extra links in the communication chain that are not required ?

Les.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Because if I want to serial print some message from esp8266 to serial monitor I have to use softwareserial because the other serial ports are occupied by the PC to Arduino link.
 

LesJones

Joined Jan 8, 2017
4,544
The way I suggested connecting the ESP8266 to the Aruino Uno board you are not using the Arduino part of the board. (You are holding the Atmega328P in a reset state to prevent it doing anything. You are only using the part of the Uno board that does the USB to serial conversion (Just think of it as using an Rt232RL module.) If you use the serial monitor you are talking to the ESP8266.

Les.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
But the problem is that most ESP8266 remote LED control through web server code examples use the software serial . Lets take a code example:


C:
#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX
//line from the esp to the Arduino's pin 2


    
void setup()
{
// and the RX line from the esp to the Arduino's pin
Serial.begin(9600);
esp8266.begin(9600); // your esp's baud rate might be different
pinMode(4,OUTPUT);
digitalWrite(4,LOW);
pinMode(5,OUTPUT);
digitalWrite(5,LOW);
pinMode(6,OUTPUT);
digitalWrite(6,LOW);
pinMode(7,OUTPUT);
digitalWrite(7,LOW);
  
        pinMode(13,OUTPUT);
  digitalWrite(13,LOW);
  sendData("AT+RST\r\n",2000,DEBUG); // reset module
  sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
  sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
}
void loop() {
  if(esp8266.available()) // check if the esp is sending a message
  {
    if(esp8266.find("+IPD,"))
    {
     delay(1000); // wait for the serial buffer to fill up (read all theserial data)
     // get the connection id so that we can then disconnect

  
           int connectionId = esp8266s.read()-48; // subtract 48 because the read() function returns
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48
     esp8266.find("pin="); // advance cursor to "pin="
     int pinNumber = (esp8266.read()-48)*10; // get first number i.e. if the pin 13 then the 1st number is 1, then multiply to get 10
     pinNumber += (esp8266.read()-48); // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
     switch (pinNumber)
      {
      case 1://switch 1 on
             digitalWrite(4,HIGH);
             break;
      case 2://switch 2 on
             digitalWrite(5,HIGH);
            break;
      case 3://switch 3 on
     digitalWrite(6,HIGH);
            break;
      case 4://switch 4 on
     digitalWrite(7,HIGH);
            break;
      case 5://led on
     digitalWrite(13,HIGH);
            break;
case 6://switch 1 off
     digitalWrite(4,LOW);
     break;
case 7://switch 2 off

  
              digitalWrite(5,LOW);
        break;
    case 8://switch 3 off
        digitalWrite(6,LOW);
        break;
    case 9://switch 4 off
        digitalWrite(7,LOW);
        break;
    case 10://led off
        digitalWrite(13,LOW);
        break;
default:
       break;
}
digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin

  
           // make close command
     String closeCommand = "AT+CIPCLOSE=";
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";
     sendData(closeCommand,1000,DEBUG); // close connection
    }
} }
/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{

  
String response = "";
    esp8266.print(command); // send the read character to the esp8266
    long int time = millis();
     while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {
if(debug) {
      Serial.print(response);
     return response;
}
      }
    }
}

If I keep the arduino reset pin grounded, and connected as you suggested RX to RX and TX and TX (Arduino pin 2 to esp rx and arduino pin 3 to esp tx) I am not able to program it. I am getting memory error.


Also, quick question. I am sometimes confused whether I should program the Arduino UNO or the ESP8266 itself. I had planned my arduino to have some code and just using ESP8266 to give my arduino internet access to trigger some digital pins through web server

Moderators note: Please use code tags for pieces of code
 

LesJones

Joined Jan 8, 2017
4,544
I have neverused an ESP8266 together with an Arduino. I have just used the ESP as it is a more powerfull proccesor than the Arduino. I am useless at "C" programming so the code I have used has been very slightly modified code I have found on the web. You will have to waid for someone that is good at "C" programming to help you.

Les.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
Okay thank you Les. By the way. I have decided to go back to the beggining to Identify whether my ESP8266 is able to respond to any AT commnads. I have uploaded a blank sketch to both Arduino and ESP, I have tried connecting TX -TX, RX-RX and TX-RX, RX-TX and yet my ESP8266 does not seem to respond to any of the AT commands . Do I have to pull arduino Reset to test AT commands? Because arduinos RX and TX I believe are used by the USB and Arduino communications and not letting me use for arduino and esp communcation
 

djsfantasi

Joined Apr 11, 2010
9,237
Hello guys. I have been experimenting with my ESP8266 and arduino. Unfortunately I am not able to program my ESP8266 using softwareSerial. I am able to program/flash some basic code to my ESP8266 by using Arduino pins D0 (RX) and D1 (TX) and connecting them to ESP. I am trying to upload the same program just using SoftwareSerial pins 2 and 3 instead of 1 and my ESP8266 does not react at all. All I did was change arduino wires instead of D0 and D1 to D2 and D3 and added 3 lines of code..

Perhaps I am not quite understanding the SoftwareSerial?

The code that I use:

C:
#include <SoftwareSerial.h>
SoftwareSerial esp8266serial(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3./*

#include "ESP8266WiFi.h"

void setup() {
esp8266serial.begin(115200);
Serial.begin(115200);
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  Serial.println("Setup done");
}

void loop() {
  Serial.println("scan start");

  // WiFi.scanNetworks will return the number of networks found
  int n = WiFi.scanNetworks();
  Serial.println("scan done");
  if (n == 0) {
    Serial.println("no networks found");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(")");
      Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
      delay(10);
    }
  }
  Serial.println("");

  // Wait a bit before scanning again
  delay(5000);
}
Looking at your code, I think I see an issue. I have only used software serial once; I use a Mega when I need additional serial ports, as it has three serial ports in hardware.

From what I remember and what I know, if you’re using a serial port other than the standard serial port, the objects needed to control it are different.

That is, you issue different commands for different ports:

Serial.begin(9600); // std port
Serial1.begin(9600); // add’l port

Note the ‘1’ character hiding in the second line.

Your code, which you say is not working, uses the UNO’s TX & RX pins. Your connections are to the D2 & D3 pins. You have to use different objects in your code.
 

Thread Starter

zazas321

Joined Nov 29, 2015
936
But i identify the second serial port using software serial library. So I assume that Arduino will use its general tx and rx ports for communicating with computer and d2 and d3 pins for communicating with esp
 

djsfantasi

Joined Apr 11, 2010
9,237
But i identify the second serial port using software serial library. So I assume that Arduino will use its general tx and rx ports for communicating with computer and d2 and d3 pins for communicating with esp
You assume wrong. The Arduino TX and RX are always controlled by the Serial. methods. Software serial ports are referred to by different objects. Serial1., Serial2., etc.

The standard serial ports do indeed still communicate with the computer. But different objects are necessary to communicate on additional ports.

Try it. You have nothing to lose but if it works, you’ve gained new knowledge.
 
Top