WiFi for Arduino Mega 2560 R3

Greetings;

I've been making great progress on my pump room monitoring project, having used an ethernet Shield so far.

Although ethernet connectivity is easy in the pumproo, I'm going to run out of cables and need to add a switch for more devices.

The other alternative (likely a bit cheaper and with an opportunity to learn), is to switch the pump monitor to WiFi.

I found an ESP-01 in my drawer, along with the 5V adapter. I can find it on Wifi, but but no matter how hard I tried with the sketches available (different UARTs, all baud rates, etc) I have not been able to get the Mega to talk to the 01.

Various posts suggest not to bother with the 01.

I have some spare ESP8266 NodeMCU CP2102 ESP-12E. Can I use these instead? Any tips and tricks?

cheers.

one option for esp-01 GitHub - jandrassy/WiFiEspAT: Arduino networking library. Standard Arduino WiFi networking API over ESP8266 or ESP32 AT commands.

trilife:
I found an ESP-01 in my drawer, along with the 5V adapter. I can find it on Wifi, but but no matter how hard I tried with the sketches available (different UARTs, all baud rates, etc) I have not been able to get the Mega to talk to the 01.

As you have not posted your program it is a bit difficult to see what might be wrong with it.

Have you a voltage converter (e.g. a voltage divider with a couple of resistors) to drop the Mega's 5v Tx to the ESP8266's 3.3v Rx?

...R

Hello Robin,

Yes, I do have a voltage adapter.

Here is the code I used:

//#include <SoftwareSerial.h>
   //use mega Serial 2 for serial monitor; Serial 1 on pins 19 (RX) and 18 (TX);// Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX).
   #define SSID "enter SSID here"
   #define PASS "enter password here"
   #define DST_IP "220.181.111.85" //baidu.com
   //SoftwareSerial dbgSerial(10, 11); // RX, TX
   void setup()
   {
     // Open serial communications and wait for port to open:
     //serial 2 is to esp8266 
     Serial2.begin(74800);//9600 (mine), 57600, 115200
     Serial2.setTimeout(1000);
    
     //serial 0 is to usb
     Serial.begin(115200);
     
          
     while(!Serial); 
     while(!Serial2);
     
     //dbgSerial.begin(9600); //can't be faster than 19200 for softserial
     //dbgSerial.println("ESP8266 Demo");
     Serial.println("ESP8266 Demo on Mega2560");
    
    while(Serial2.available()>0)
    Serial2.read();
      
     delay(1000);
       //test if the module is ready
     Serial2.println("AT+RST");
     //delay(1000);
     //delay(1000);
     Serial.println("Resetting module");
     Serial2.flush();
     
     //if(Serial2.find("ready"))
     if(Serial2.find("Ready")||Serial2.find("ready"))
     {
       //dbgSerial.println("Module is ready");
       Serial.println("Module is ready");
     }
     else
     {
       //dbgSerial.println("Module have no response.");
       Serial.println("Module has no response.");
       while(1);
     }
     delay(1000);
     //connect to the wifi
     boolean connected=false;
     for(int i=0;i<5;i++)
     {
       if(connectWiFi())
       {
         connected = true;
         break;
       }
     }
     if (!connected){while(1);}
     delay(5000);
     //print the ip addr
     /*
   Serial2.println("AT+CIFSR");
     Serial.println("ip address:");
     while (Serial2.available())
     Serial.write(Serial2.read());
   
   */
     //set the single connection mode
     Serial2.println("AT+CIPMUX=0");
   }
   void loop()
   {
     String cmd = "AT+CIPSTART=\"TCP\",\"";
     cmd += DST_IP;
     cmd += "\",80";
     Serial2.println(cmd);
     //dbgSerial.println(cmd);
     Serial.println(cmd);
     if(Serial2.find("Error")) return;
     cmd = "GET / HTTP/1.0\r\n\r\n";
     Serial2.print("AT+CIPSEND=");
     Serial2.println(cmd.length());
     if(Serial2.find(">"))
     {
       //dbgSerial.print(">");
       Serial.print(">");
       }else
       {
         Serial2.println("AT+CIPCLOSE");
         //dbgSerial.println("connect timeout");
         Serial.println("connect timeout");
         delay(1000);
         return;
       }
       Serial2.print(cmd);
       delay(2000);
       //Serial.find("+IPD");
       while (Serial2.available())
       {
         char c = Serial2.read();
         //dbgSerial.write(c);
         Serial.write(c);
         //if(c=='\r') dbgSerial.print('\n');
         if(c=='\r') Serial.print('\n');
       }
       //dbgSerial.println("====");
       Serial.println("====");
       delay(1000);
     }
     boolean connectWiFi()
     {
       Serial2.println("AT+CWMODE=1");
       String cmd="AT+CWJAP=\"";
       cmd+=SSID;
       cmd+="\",\"";
       cmd+=PASS;
       cmd+="\"";
       //dbgSerial.println(cmd);
       Serial2.println(cmd);
       Serial.println(cmd);
       delay(2000);
       if(Serial2.find("OK"))
       {
         //dbgSerial.println("OK, Connected to WiFi.");
         Serial.println("OK, Connected to WiFi.");
         return true;
         }else
         {
           //dbgSerial.println("Can not connect to the WiFi.");
           Serial.println("Can not connect to the WiFi.");
           return false;
         }
       }

I never get past the “module has no response”.

74800 is a very strange baud rate, the usual is 76800.

Make a pencil drawing showing how you have everything connected and post a photo of the drawing. See this Simple Image Posting Guide

A link to the datasheet for the voltage adapter would also be useful.

There is a lot of useful info (and code) about using an ESP8266-01 on the ESP8266 Forum and there is a very useful book by Neil Kolban that you can download.

...R

AT firmware’s default is 115200 baud.
74800 is boot log for esp8266 with the usual 26 MHz crystal. it is because the esp8266 was developed for 40 MHz crystal with that it has has boot log at 115200 baud.

Why do you want a Mega 2560? :cold_sweat:

@trilife, if you want to replace the Ethernet shield with esp8266 with only small changes in sketch, use my WiFiEspAT library.

Paul__B:
Why do you want a Mega 2560? :cold_sweat:

Hi Paul, lots of analog inputs

Robin2:
74800 is a very strange baud rate, the usual is 76800.

Make a pencil drawing showing how you have everything connected and post a photo of the drawing. See this Simple Image Posting Guide

A link to the datasheet for the voltage adapter would also be useful.

There is a lot of useful info (and code) about using an ESP8266-01 on the ESP8266 Forum and there is a very useful book by Neil Kolban that you can download.

…R

Hi Robin, here’s the sketch. Only 4 wires. I made sure that Rx/Tx goes to Tx/Rx, all pairs on the Mega, tried all the Baudrates available from the serial monitor drop down, and the 74800 odd rate.

@Juraj, I had also tried your WiFiEspAT library before. No dice.

I’m thinking my unit is busted.

My question was and is though:

I have some spare ESP8266 NodeMCU CP2102 ESP-12E. Can I use these instead? Any tips and tricks?

https://www.amazon.com/gp/product/B081CSJV2V/ref=ppx_yo_dt_b_search_asin_title?ie=UTF8&psc=1.

Thanks.

trilife:
Hi Paul, lots of analog inputs

How many? How about a 74HC4067?

Remember a Mega 2560 only has one ADC and a multiplexer. :grinning:

Hi Paul;

I'll keep that in mind for the next time, for when this baby goes into production!

It's been 35 years since I held a soldering iron and as many since I did programming. Give this old man a break!

trilife:
Hi Robin, here’s the sketch.

I don’t see any program. If you have posted a picture of the program then please don’t. Just copy and paste the program and use the code button </> so the code looks like this

If the picture is something other than code then please make it visible in your Post. I gave you a link to the instructions.

…R

Robin2:
I don’t see any program. If you have posted a picture of the program then please don’t. Just copy and paste the program and use the code button </> so the code looks like this

If the picture is something other than code then please make it visible in your Post. I gave you a link to the instructions.

…R

I see the sketch in comment #3. it is in code-tags

Juraj:
I see the sketch in comment #3. it is in code-tags

That may indeed be the correct program but Reply #9 clearly says "here's the sketch." and it does not say "the sketch is in Reply #3"

Of course this may be confusion caused by the nonsensical decision of the Arduino folks to use "sketch" when they mean "program" and perhaps Reply #9 was using the word "sketch" to mean drawing.

On that interpretation this is the image from Reply #9

...R

@trilife, please also post a link to the datasheet for the adapter you are using with the ESP8266-01

...R

it looks like a wiring for this 5 V adapter board, not a plain esp-01.


(the single white wire on the photo is to reset the Nano with the esp io 0)

Juraj:
it looks like a wiring for this 5 V adapter board, not a plain esp-01.

From where did that image come?

...R

Robin2:
From where did that image come?

...R

it is my

Hello Robin,

Yes indeed, the moment I hit return to post, I realized that "sketch" might lead to confusion. Sorry about that.

So, now that we have the Arduino-Sketch of #3 and the real-world-sketch of #9:

Can I use the ESP8266 referred to in Item #1 and item #9, as an alternative to the ESP-01? If so, Amy tips and tricks...

Cheers.