Help programming a Mega R3 with a built-in ESP8266

Hi Everyone,

I hope you don't mind me asking this here but I bought a Wemos Mega R3 with a built-in ESP8266 and was hoping to get some guidance on how to set it up. I know it's not a genuine Arduino product and I wasn't sure if I'd be able to ask questions about it here.

I got this chip because it was the best way to keep my project neat and save pin space on the mega. I'm building a wifi controlled robot with 2 motors and 3 servos. The motors are both controlled by an L298n. As I'm new to Arduino, I've been building my robot in stages while I learn how to add the different components. I've got a good understanding of servos and L298n but lack skill and understanding of ESP's.

The issue I'm currently facing is that this mega has the ESP built in. According to the chip's specs, both the ESP and Mega are individual components that simply share the same board in a manner of speaking. The benefit here is that not only does it save space but it doesn't require any additional wiring to hook them together. I still need to program the ESP and the Mega separately but once that is done, I'll be able to run them together.

I have been looking at different guides and videos that explain how to do this but I'm finding them rather difficult to understand. I know this might seem like a trivial question but what is the basic code that I need to get the ESP to work with the Mega? If I can get the ESP working as the wifi bridge for the mega, I'll be able to figure out the rest from there.

Here are the links for the chip's specs

Chip overview

Schematic

Pinout

Thanks in advance!

Hi

I played with one a few months back... from my recollection,

  • The ESP8266 can be flashed with various firmware like NodeMCU, ESP8266 AT command or programmed with Arduino IDE. So the first decision you need to make is which firmware you want for the ESP.

  • The configuration for a serial communication connection between the ATmega2560 the ESP8266 and the USB to USART is set through DIP Switch. There is one configuration in which you set if this is Serial or Serial3 which connects one ATmega2560 hardware serial to the ESP8266 Rx/Tx.

  • Once the switch is set, whatever is printed to that serial from at Atmega side arrives to the ESP and whatever the ESP prints arrives to that serial of the ATMega (115200 baud as default)

For example If you flashed your ESP with AT command line firmware, and configure the communication to use Serial3 then you could conceptually do (in practice write a function adding some delay for ESP to ack, and ideally read the answer of the command to ensure it’s well received)

Serial3.begin(115200); // open the serial communication with ESP

Serial3.println(F("AT+CWMODE=1")); // client of your local wi-Fi
Serial3.println(F("AT+CWJAP=\"SSID\",\"PWD\"")); // join your home network network
Serial3.println(F("AT+CIFSR")); // if you print the output you’ll see your IP

Serial3.println(F("AT+CIPMUX=1")); // multi IP connection
Serial3.println(F("AT+CIPSERVER=1,80")); // TCP server on port 80
Serial3.println(F(“AT+CIPSTO=3600")); // server timeout 1 hour

If you then point a web browser to the IP of the esp - say http://192.168.1.28/Hello:80 (assuming 192.168.1.28 Is the IP of your ESP. Note the :80 is not necessary really as this is the default port, but just to show what to 80 in AT+CIPSERVER=1,80 was for), and hit enter and have some code in the loop() on the mega just routing what comes from Serial3 to Serial , you should see the incoming TCP request in the form of a well formed HTTP GET request with header, something starting with

0,CONNECT

+IPD,0,408:[color=blue][b]GET /Hello[/b][/color] HTTP/1.1
Host: ....

The 0,CONNECT is the AT firmware stating you got an incoming tcp request and the +IPD,0,408:[color=blue][b]GET /Hello[/b][/color] HTTP/1.1 is what you’d be parsing to see which URL is being asked

Of course that’s a very crude use of the power of your ESP, you could have a full arduino program in it instead of the AT firmware and then you get to define the communication protocol you want on Serial3 and benefit from the advanced programming environment and OS for the ESP code.

To properly handle a Serial communication, reading Serial Input Basics could help

(that being said I find it easier to have two separate module and 3 wires for Tx/Rx and GND with voltage adaptor and handle them the way I want - with the price of an ESP32 this days, you get a way more capable WiFi chip that you can add to your plain old mega)

wernejo:
I know it's not a genuine Arduino product and I wasn't sure if I'd be able to ask questions about it here.

You're welcome to ask questions here about anything, as long as it's Arduino-related.

I do like to see people buy official Arduino products because that money supports the work Arduino does that benefits all of us. You won't see WeMos making significant contributions to the Arduino community (other than selling us cheap, unique boards of reasonable quality). However, I'll always try to help everyone, no matter where they got their hardware.

1 Like

pert:
You're welcome to ask questions here about anything, as long as it's Arduino-related.

Thanks Pert,

I agree with what you're saying about supporting the community. However, the only reason I got this chip was that I am space limited and wanted to try and make coding my robot less confusing. However, I'm still not sure if I have been able to achieve that just yet haha

did you read my answer?

JML's answer is very complete, in addition to that i would recommend you to actually program the ESP using the Arduino IDE (and it's language) using the ESP core. Check out the examples for the ESPwebserver and have a look at Serial Input Basics for communication between the 2 CPU's . This is definitely a reasonably priced board !

I did read JLM's post and replied to it yesterday. However, it seems that my post didn't get posted for some reason.

I've been reading up on serial and serial3, and how it fits in. I'm still a little confused about the difference between the two. I'm still not sure about how it works in this situation.

The one thing that I'm still unsure about is updating the firmware and setting up the ESP. I've found a few other topics on the forum talking about the same chip and seems like we're all having the same issue. I'm still working my way through reading everything and it's still going to be a while before I get my head around this.

Thanks JLM for your post. It has helped but I'm new to AT commands. I honestly thought that this whole process would have been a simple matter of setting up the ESP with my wifi username and password, and then treat the chip as a really powerful ESP and not two separate modules. I figured that's what the DIP switch was for anyway.

I guess what I'm now asking is what are the steps I need to take to get this chip to work?

wernejo:
I've been reading up on serial and serial3, and how it fits in. I'm still a little confused about the difference between the two. I'm still not sure about how it works in this situation.

If you set the switch to "RXD0/TXD0", then the ESP8266 will be connected to pins 0 and 1 of the ATmega2560, which is referred to as "Serial" in your Arduino code for the ATmega2560.

If you set the switch to "RXD3/TXD3", then the ESP8266 will be connected to pins 15 and 14 of the ATmega2560, which is referred to as "Serial3" in your Arduino code for the ATmega2560.

Since Serial is also used for communication with your computer, I would recommend setting that switch to use Serial3.

wernejo:
The one thing that I'm still unsure about is updating the firmware and setting up the ESP. I've found a few other topics on the forum talking about the same chip and seems like we're all having the same issue. I'm still working my way through reading everything and it's still going to be a while before I get my head around this.

If you haven't yet successfully uploaded anything to the ESP8266, I would check if the ESP8266 comes with the AT firmware already installed, as most ESP8266 modules do. That could save you some hassle and confusion. To test it out, do this:

  • Set the DIP switches to the "CH340 connect to ESP8266 (connect)" setting, as documented on the product page for your board.
  • Connect the board to your computer with a USB cable.
  • Select the board's port from the Arduino IDE's Tools > Port menu.
  • Tools > Serial Monitor
  • Set Serial Monitor's line ending menu to "Both NL & CR".
  • Set Serial Monitor's baud rate menu to 115200.
  • Click on Serial Monitor's input field.
  • Type AT in the input field.
  • Press Enter.
  • If the AT firmware is installed, you should see "OK" in Serial Monitor's output field.

Note that the first time you upload something to the ESP8266, the AT firmware will be erased. Installing it again is a bit complicated.

wernejo:
I honestly thought that this whole process would have been a simple matter of setting up the ESP with my wifi username and password

With the AT firmware, you set the WiFi username and password in the code that runs on the ATmega2560.

wernejo:
and then treat the chip as a really powerful ESP and not two separate modules.

With the AT firmware, it will be like a regular Arduino Mega with a WiFi connection. The ESP8266 only acts as a WiFi adapter. The funny thing is the ESP8266 has way more memory and processing power than the ATmega2560. However, the ATmega2560 has way more IO pins and is a bit easier to use.

You should really consider this as being 2 different chips that you connect through Serial. Using Serial3 is what makes the more sense to me...

given the power and capability of the chips, I would use the ESP as the master processor and use the ATMega just as a "slave" in charge of driving the pins.

Build a Serial protocol that will rule the data exchange between the ESP and ATMega. for example if the ESP wants the value of digital pin 23, may be you send through Serial "<D,23>". The '

<

' would be the start marker for a communication, '

D

' for Get the Digital value of pin [color=purple]23[/color] and The '

>

' would be the end communication marker.
The ATMega could then answer with "<D,23,0>" to tell you that the [color=blue]D[/color]igital Value of pin [color=blue]23[/color] is [color=blue]0[/color] (LOW).

"<P,10,100>" could be set AnalogWrite at 100 on pin 10 (PWM)
"<A,10>" could be read Analog pin 10
etc..
could be higher abstraction level as well, like "<M,2,100>" could be set Motor #2 at speed 100...

the cool thing is that you do what you want :slight_smile:

--> you need to build a parser to handle communication on both side on Serial3, use Serial Input Basics to understand how to get a command line.

Once this is defined and programmed,

  • configure the board switches to have the USB connected to the ATMega.
  • upload to the ATMega the code handling the command language.
  • configure the board switches to have the USB connected to the ESP.
  • upload to the ESP the code handling the command language and a web REST based user interface
  • configure the switches so that Serial3 is used to connect the ESP to the Atmega
  • reboot the board

if you need debug trace, the USB cable will be connected to the ATMega Serial (pin 0 and 1) so all the debug stuff will have to be handled in the Atmega code as well.

J-M-L:
You should really consider this as being 2 different chips that you connect through Serial. Using Serial3 is what makes the more sense to me...

given the power and capability of the chips, I would use the ESP as the master processor and use the ATMega just as a "slave" in charge of driving the pins.

Build a Serial protocol that will rule the data exchange between the ESP and ATMega. ...

Firmata in Mega and MasterFirmata library in esp8266 can do this for you

Thanks everyone for clearing up the confusion I had with Serial 3.

J-M-L:
Build a Serial protocol that will rule the data exchange between the ESP and ATMega. for example if the ESP wants the value of digital pin 23, may be you send through Serial "<D,23>". The '

<

' would be the start marker for a communication, '

D

' for Get the Digital value of pin [color=purple]23[/color] and The '

>

' would be the end communication marker.
The ATMega could then answer with "<D,23,0>" to tell you that the [color=blue]D[/color]igital Value of pin [color=blue]23[/color] is [color=blue]0[/color] (LOW).

"<P,10,100>" could be set AnalogWrite at 100 on pin 10 (PWM)
"<A,10>" could be read Analog pin 10
etc..
could be higher abstraction level as well, like "<M,2,100>" could be set Motor #2 at speed 100...

@JML I really like what you're saying here. I assumed that I would be using something similar but this is a much better method than what I had in mind. I had a read over the link you posted and it makes a bit of scene. I'll just need to keep re-reading over it until it all clicks. The one question I do have about the serial connection is where do I put the code? I know I'll need to reference pins 14 and 15 as serial 3 connections on the ATmega and set up some code to receive the data from the ESP. However, the code in the examples, is that for the ESP or the ATmega, or do I need to somehow split it into two?

I've been doing some extra reading on connecting an ESP to an Uno (as that's more common than a Mega).

In both of the tutorials I've found, they both say that I need to use #include <ESP8266WiFi.h> for the wifi. Going back to what JML said about using the ATmega as a slave, would I need to include that on the ESP or the ATmega?

https://www.geekstips.com/esp8266-arduino-tutorial-iot-code-example/

Just to give you some extra insight into my robot, I'm 3d printing a small tank with a robotic claw arm mounted on the top. My goal is to create a robot that I can drive around my home while I'm at work. This will be done using a web interface that uses my keyboard to drive the robot and operate the arm. I'll also instal a webcam to steer the robot using an RPi Zero. but all the controls will be done via the ATmega. I'm still a way off from adding the webcam and will be doing all of the building/testings at a hackerspace.

The one thing I was really hoping to somehow achieve is to be able to upload new code wirelessly. This will help if I ever need to make quick changes or update the wifi details.

wernejo:
I need to use #include <ESP8266WiFi.h> for the wifi. Going back to what JML said about using the ATmega as a slave, would I need to include that on the ESP or the ATmega?

On the ESP.

wernejo:
The one thing I was really hoping to somehow achieve is to be able to upload new code wirelessly. This will help if I ever need to make quick changes or update the wifi details.

You can do OTA uploads to the ESP8266. Doing that to the ATmega2560 will be more tricky. Theoretically, you could send the data via WiFi to the ESP8266, then through the serial port to the ATmega2560, but it will be a bit tricky to get it set up. You would probably need to do some wiring so that the ESP8266 can pull the reset pin of the ATmega2560 LOW to reset the ATmega2560 to activate the bootloader just before starting the upload.

pert:
You can do OTA uploads to the ESP8266. Doing that to the ATmega2560 will be more tricky. Theoretically, you could send the data via WiFi to the ESP8266, then through the serial port to the ATmega2560, but it will be a bit tricky to get it set up. You would probably need to do some wiring so that the ESP8266 can pull the reset pin of the ATmega2560 LOW to reset the ATmega2560 to activate the bootloader just before starting the upload.

or use ArduinoOTA library in Mega. it doesn't matter if the esp8266 is on Serial3 or if it can reset the ATmega. only it would require to use WiFiLink firmware in esp8266 and replace the bootloader of ATmega

1 Like

The one question I do have about the serial connection is where do I put the code?

You need code in both. On the mega you'll do a Serial3.begin(115200); to open the communication channel at 11500 bauds, and similarly you do Serial.begin(115200); on the ESP. With the Dip Switches in the right position, then both are connected and the exchange of data can happen.

If you're reading this thread because you can't get your MEGA or UNO with on-board ESP8266 WiFi (robotdyn, wemos, unbranded) to send data to the web (ie Thingspeak) using a WiFi connection, consider sending AT commands to the OEM ESP8266 firmware from an Arduino sketch, which avoids figuring out how to transfer bytes between an Arduino sketch on the MEGA / UNO and an ESP8266 sketch on the ESP8266.

Perhaps the pros consider AT commands to be a complicated / inefficient / knuckle-dragging approach, but if, like me, you just want your data to flow with minimal effort learning to code, you might benefit from the many hours I spent figuring out what might seem obvious to the pros...though simple successful example code for the MEGA/UNO w/ESP8266 boards is noticeably absent from all the web posts I looked at... Based on the many postings I reviewed, people (like me) asking how to get these MEGA/UNO w/ESP8266 boards working didn't seem to know enough about this subject to interpret informed responses, while the informed respondents don't realize their responses assume a certain level of understanding that someone like me doesn't have.

Anyway, here's the basic concept / process I eventually used for the MEGA w/ESP8266:

  1. Flash ESP8266 chip with appropriate Espressif firmware (or don't erase the firmware in the first place...I spent several hours with the esp flash tool figuring out correct reflash settings)
  2. Using key elements (commented in attached MEGA sketch) upload an Arduino sketch to the Mega with DIP positions: ON:3,4; OFF:1,2,5,6,7,8; RX/TX switch: any position
  3. Use the 'special solution' switch configuration: DIPs: ON:1,2,3,4; OFF:5,6,7,8; RX/TX switch set to RX3/TX3
  4. The 'special solution' connects the MEGA's serial3 port to the ESP8266 at the same time the serial port connects to the serial monitor
  5. Open serial monitor and your Thingspeak channel to see what's happening. You should see AT commands and values in the serial monitor, and about every 30 seconds a new value should appear on Thingspeak.

After figuring this out with the MEGA w/ESP8266, here's the basic concept / process I used for the UNO:

  1. Flash ESP8266 chip with appropriate Espressif firmware (or don't erase the firmware in the first place...I spent several hours with esp tool figuring out correct reflash settings)
  2. Using key elements (commented in attached UNO sketch) upload an Arduino sketch to the UNO with DIP positions: ON:3,4; OFF:1,2,5,6,7,8
  3. To connect the UNO serial output directly to the ESP8266, use the switch configuration: DIPs: ON:1,2; OFF:3,4,5,6,7,8.
  4. Serial monitor will not work (since serial is being used for data transfer between UNO MCU and ESP8266), but you can open your Thingspeak channel to see if anything is showing up.

If nothing shows up on Thingspeak, use serial monitor to connect directly to the ESP8266 (DIP: ON:5,6; OFF:1,2,3,4,7,8) and send AT commands directly to the ESP8266; copy AT command strings by using serial monitor while running the MEGA sketch, copying the AT command strings to MS Word or notepad.

Hope it works for you!

MEGAwWiFi_logger_webpost_example.ino (8.78 KB)

UNOwWiFi_logger_webpost_example.ino.ino (7.66 KB)

You can do OTA uploads to the ESP8266.

Yes but as far as i understood, you need to have the receiving code installed and enough memory left over for the code that is 'to be installed' Now on an ESP-01 this always seems to be nearly impossible since i always have a sketch which takes up just about 50% of flash (without it actually doing to much already) mind you i've never managed to get above 80% of flash so far.

given the power and capability of the chips, I would use the ESP as the master processor and use the ATMega just as a "slave" in charge of driving the pins.

But there is something to be said for letting the Mega perform slightly more complex tasks, depending on the project. If you are or instance using the mega to drive several servos then telling it to move one servo at a time to make a multi servo arm move seems inefficient. If there are parts which are interrupt driven and direct output as a result then why involve the ESP at all ? Same for extra UART communication (the ESP has a limited BAUD-rate of 115200) Your UI will normally be the ESP. Anyway looking at processing power and memory you'd let the ESP do most the work, but try and minimize communication between the 2 units.

solarsev:
consider sending AT commands to the OEM ESP8266 firmware from an Arduino sketch, which avoids figuring out how to transfer bytes between an Arduino sketch on the MEGA / UNO and an ESP8266 sketch on the ESP8266.

That doesn't make sense. If by "bytes", you mean data, then those AT commands are data.

solarsev:
Perhaps the pros consider AT commands to be a complicated / inefficient / knuckle-dragging approach

I wouldn't say that. AT commands have been used by pros to control devices long before the ESP8266 came along. Who do you think wrote the ESP8266's AT firmware? That's right, pros did, and they didn't write it with the intention that Arduino beginners would be using it. They wrote it for use in mass manufactured consumer product. But what many users of ESP8266 in this sort of configuration prefer is to use a library that uses those same AT commands under the hood, but presents the user with a standardized API that is consistent with all the other Arduino networking libraries. That way, you can use it with the huge amount of existing libraries and example sketches. The popular library for doing this is:

another option:

You'll never see an AT command in your sketch while using these libraries, but they're all right there in the library code.

Personally, I think using one of those libraries is far easier than trying to deal with AT commands. It is true that the high level interface of the library could make it more difficult to troubleshoot if there is a bug in the library code. I did have one problem with the WiFiEsp library in the project I did using it because the library author has debug printed to Serial, but I was using one of these ESP8266 shields that uses Serial to communicate with the ESP8266. So the debug output was conflicting with the communication with the ESP8266. Once I turned off the debug output, everything was very simple and problem free.

solarsev:
simple successful example code for the MEGA/UNO w/ESP8266 boards is noticeably absent from all the web posts I looked at...

Those libraries come with example sketches.

I would say that the AT commands complete the ESP as a product, giving access to the ESP's features from the command line, and in the same way that other external (slave) units can be controlled. They turn the ESP into an ' interpreter' effectively. Directly programming the ESP firmware makes it a lot easier to let the ESP perform more complex tasks and use it's fair sized memory and quick CPU. If i am going to host a few webpages from the ESP, why would i want to load a program onto an AVR so that it can load these pages onto the ESP every time i start that program up, when i can also load them into flash directly, and use the CPU to perform task depending on the input. i Suppose it all depends on what you want to do.

Thanks everyone for all of their help! I'm still learning my way around everything but you've all been a huge help!

I've started looking into Blynk and using that as a way to create an app on my phone rather than using a web server. I'm still reading the tutorials on how it all works but I think this might be the best way to go for my project.

Given that the Wemos board is two chips in one, would I need to flash the Blynk code onto the ESP or onto the AT Mega?

I found one video tutorial that uses a Wemos D1 (esp8266 Blynk Introduction - YouTube). The one thing I'm not sure is if that board is the UNO version of the AT Mega that I have and if the demo code/method is correct for what I've got.

So in this situation, do I treat the ESP as a shield and flash the AT Mega with the Blynk code or do I need to upload it to the ESP and the use AT commands to communicate the commands via serial3?

wernejo:
I found one video tutorial that uses a Wemos D1 (https://www.youtube.com/watch?v=cRC4KnWPnrI). The one thing I'm not sure is if that board is the UNO version of the AT Mega that I have and if the demo code/method is correct for what I've got.

So in this situation, do I treat the ESP as a shield and flash the AT Mega with the Blynk code or do I need to upload it to the ESP and the use AT commands to communicate the commands via serial3?

Wemos D1 is esp8266 only. the pins of the board are the pins of the esp8266

on Mega + WiFi the pins of the board are the pins of the ATmega.
Blynk controls the pins so it should run in ATmega. put AT firmware 1.7 in esp8266 and use Blynk over my WiFiEspAT library in ATmega