Connecting ESP8266-01 to Arduino Mega

Hi,
I am new to Arduino so my apologies if I have done something really silly. I have connected up the ESP8266 module as follows:
VCC----->3.3V
EN------>3.3V
GND---->GND
RX----> TX1
TX------RX1

I have added a resistor between the VCC and 3.3.V. This configuration works perfectly on the Arduino Uno ( I am also using the older ESP8266 - with two LEDs not the newer version with one LED). I need to recreate this setup in an Arduino Mega, I am working on a project with multiple sensors, and I need more I/O pins ( I am using an external 3.3V power supply for the sensors).

The Mega board does not want to connect to the ESP, it does not even see it . Any assistance will be greatly appreciated.
Here is my code (It is not perfect - just a start). This project is part of my computer science Ph.D. looking into Tangible User Interfaces - so apart from C++, I am a total newbie.

#include <WiFiEspClient.h>
#include <WiFiEspServer.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <WiFiEspUdp.h>
#define RX 10
#define TX 11 
SoftwareSerial esp8266(RX,TX);


char ssid[] = "SmartQuilt"; //  your network SSID (name)
char pass[] = "CID3208till";    // your network password (use for WPA, or use as key for WEP)

int status = WL_IDLE_STATUS;
int temperature;
int humidity;
int val=0;
int sensorVal;
int touchPin=9;
int reqCount = 0; 
int correct;
String json;

WiFiEspServer server(80);

RingBuffer buf(8);


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

  // Init variables and expose them to REST API


   WiFi.init(&esp8266);    // initialize ESP module
   while (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    delay(1000);
  }

  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while (true); // don't continue
  }

  Serial.print("Attempting to start AP ");
  Serial.println(ssid);

  // uncomment these two lines if you want to set the IP address of the AP
  IPAddress localIp(192, 168, 8, 1);
 WiFi.configAP(localIp);
  
  // start access point
  status = WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);

  Serial.println("Access point started");
  printWifiStatus();
  
  // start the web server on port 80
  server.begin();
  Serial.println("Server started");

  
}

void loop() 
{
  sensorVal - getSensorData();
  int correct = sensorVal++;
 WiFiEspClient client = server.available();  // listen for incoming clients
  
  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
    while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        Serial.println("available");
        char c = client.read();               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n")) {
          delay(1000);
          sendHttpResponse(client);
          break;
        }
      }
    }
    
    // give the web browser time to receive the data
    delay(10);

    // close the connection
    client.stop();
    Serial.println("Client disconnected");
  }

  }  



int getSensorData(){
  val = digitalRead(touchPin); 
  if(val ==1){
     
  }
     return val;
}

void printWifiStatus()
{
  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print where to go in the browser
  Serial.println();
  Serial.print("To see this page in action, connect to ");
  Serial.print(ssid);
  Serial.print(" and open a browser to http://");
  Serial.println(ip);
  Serial.println();
}

void sendHttpResponse(WiFiEspClient client)
{
     
  client.print(
    "HTTP/1.1 200 OK\r\n"
    "Content-Type: application/json\r\n"
    "Connection: close\r\n"  // the connection will be closed after completion of the response
    "Refresh: 20\r\n"        // refresh the page automatically every 20 sec
    "\r\n");
  
 
  //delay(10000);
  client.print("{\n \"LearnShapes\":\n  {\"activityID\": \"001\",\n    \"acticityName\": \"shapes\",\n    \"date\":\"12/12/2021\",\n    \"correct\": \"8\",\n     \"timeOnTask\":\"5\"\n },\n \"LearnNumbers\":\n  {\"activityID\": \"002\",\n    \"acticityName\": \"numbers\",\n    \"date\":\"12/12/2021\",\n    \"correct\": \"5\",\n     \"timeOnTask\":\"1.5\"\n },\n \"MarchShapes\":\n  {\"activityID\": \"003\",\n    \"acticityName\": \"match\",\n    \"date\":\"12/12/2021\",\n    \"correct\": \"6\",\n     \"timeOnTask\":\"3\"\n },\n \"Love\":\n  {\"activityID\": \"004\",\n    \"acticityName\": \"loves\",\n    \"date\":\"12/12/2021\",\n    \"correct\": \"8\",\n     \"timeOnTask\":\"5\"\n }\n\n}\n\n");

}

I always end up with the output: No WIFI shield present.
Thank you so much for your time.

For one thing, you shouldn't use SoftwareSerial with a 'Mega' - you should use one of its hardware UARTs (there are 4).
And 115200 is too fast for softwareserial anyway.

Thank you so much for your reply. I have only ever worked with software serials. I am going to read up on the hardware serials now. Is there anything else you can spot that I did incorrectly?

this is risky business

your MEGA Tx is sending 5V when HIGH to the ESP Rx which can only accept 3.3V...
you'll probably destroy the ESP's pin...

you need to convert the 5V into 3.3V ( Resistor voltage dividers or I2C voltage adapter circuits that are easy to use)

Thank you, I can address this. I have double-checked and the ESP is still working (thank goodness I did not fry it). Would this misconfiguration cause the Mega to not see the ESP?

the Tx from ESP to Rx from Arduino MEGA will work fine.
a logical 0 (LOW) will arrive as GND level, which is shared.
a logical 1 (HIGH) will arrive at 3.3V which is high enough for the MEGA to recognize a HIGH.

so TxESP ➜ RxMEGA is fine

the opposite is not true

Apologies if this is an incredibly ill-informed question, but would it be better if I then use pins 10 and 11 for TX and RX on the Mega?

No, the advice you got before is great : don't use software serial, use Serial1 or Serial2 or Serial3 :slight_smile: (with the voltage adaptor on Tx at least)

they are labeled "communication" on your board
image

I cannot believe I missed that! I am going to try and reconfigure this board, may I please come back for some more help if I have no luck? And if I may, are there any tutorials that you can recommend ?

sure, the forum is always happy to help those doing their part

The only tutorial I've in mind to handle a serial port would be to read Serial Input Basics. That would help you understand how to manage an asynchronous communication

otherwise Serial1/2/3 behave like Serial, they are hardware Serial ports and so what you know for Serial also works for those (print, write, read etc...)

Thank you ! I am going to read it in detail tonight, but I managed this so far

{
  Serial.begin(9600);
  Serial1.begin(115200);

  // Init variables and expose them to REST API


   WiFi.init(&Serial1);    // initialize ESP module
   while (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    delay(1000);
  }

Super helpful!
I have another question ( if I may) should I be using 9600 as the baud rate?

it depends on what the other side expects. how is the ESP configured?
115200 is fine on both side given it's hardware serial

you could go much faster than that

J-M_L Jackson Thank you ! Thank you ! Thank you !

16:50:25.227 -> [WiFiEsp] Initilization successful - 1.5.4
16:50:25.275 -> Attempting to start AP SmartQuilt
16:50:26.253 -> [WiFiEsp] IP address set 192.168.8.1
16:50:27.139 -> [WiFiEsp] Access point started SmartQuilt
16:50:27.185 -> Access point started
16:50:27.232 -> IP Address: 192.168.8.1
16:50:27.232 -> 
16:50:27.232 -> To see this page in action, connect to SmartQuilt and open a browser to http://192.168.8.1
16:50:27.326 -> 
16:50:27.326 -> [WiFiEsp] Server started on port 80
16:50:27.372 -> Server started

It works! Now I just need to figure out how to build a power distribution board
THANK YOU

have fun

1 Like

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