How to connect Arduino Mega to Wi-Fi (with ESP-01)?

Hi everyone, in my last post, I was trying to connect my Arduino Mega to my ESP-01 module. I was able to do that and to connect my ESP-01 module to my phone’s hotspot, but now I’m wondering how I would also connect my Arduino Mega to my phone’s hotspot, or whether it is already being done? Essentially, I’m confused on how to access Wi-Fi from the Mega, when it is connected to an ESP-01 module that is connected to Wi-Fi. I hope this makes sense!

Confusing. The Arduino Mega 2560 Rev3 does NOT have WiFi, so it can't be connected to itself. As you found out, though, a small, simple ESP-01 does have WiFi and can connect to the internet. It also sounds like you connected the ESP-01 to the Mega and got access to the hotspot/router in your phone. What else did you want to do?

Sorry for the confusion! I’m really not sure how to explain it but I hope this makes more sense.

I want to send data from my Arduino Mega to ThingSpeak. So, I connected the ESP-01 module to my Mega, and then also connected the ESP-01 module to Wi-Fi—does this mean I can now send data from the Mega to ThingSpeak?

Basically, yes.
In order this to work, you'll need to write two sketches —first for ESP-01 and second for Mega. The Mega program will send data to the ESP-01 via UART, and the ESP-01 program will receive data from Mega, form a message from it, and send it to ThingSpeak.

Ah ok I see. Could you clarify what “form a message from it” means? Also, can I just program the ESP-01 using the Arduino IDE and write the program how I would for my Arduino?

Could this be avoided by using the AT-command set firmware on the ESP-01? Would this configuration be compatible with Thingspeak?

That's the idea.

Communicate with ThinkSpeak requires sending data in a specific format, adding field names (or numbers) and your ThinkSpeak key.
This is what I called message formation.

yes, definitely

I don't know.
I always immediately erase the original firmware and replace it with a new code, I have never even tried to connect to it :slight_smile:

Maybe, but the required WiFi code is just moved to the less capable mega and then sent to the esp-01 over the limited function AT commands.

Like @b707 I ignore the AT interface and write the WiFi related code directly for the esp-01. Use a serial interface between the Mega and the esp-01 to send the Mega data to the esp-01 which will map it to the ThinkSpeak commands.

If this is a new project I would forget the Mega entirely and use an ESP32. The only reason for a Mega is if there is a need for a lot of 5V I/O pins.

Correct, but I assume there are one or more Arduino libraries that simplify sending data to Thingspeak. The question is, are any of those libraries compatible with Mega sending AT-commands to ESP-01? Or are those libraries only compatible with Arduino boards that have native WiFi?

I'm asking if there is a way @arduino_learner1 can avoid writing 2x codes and uploading one of them to the ESP-01, because that's a big challenge for a beginner.

I agree. Using 2 processors with communication between them is more than 2X the work. I would say 5-10x depending on how the functions are split up and how complicated the communication between them is. That is why I recommended looking at the esp32 and putting it all on one processor if possible.

ThingSpeak is available for the esp32, an example project...

1 Like

https://github.com/JAndrassy/WiFiEspAT?tab=readme-ov-file#getting-started

I did it without Esp and WiFi at all.
In my project an Arduino Nano board sends a data to PC via Uart, where a Perl script receives a data and sends it to Thinkspeak via Ethernet

I’ve connected 3 sensors to the Mega, all of which communicate via UART, so I don’t think I can transition to a new board. I’ve been specifically using the Mega since it has so many UART pins

OK, so in this case using your existing Mega probably makes sense. How are you currently interfacing to you Mega? I assume via the serial port to the PC.

What I would do is leave the Mega communication interface like it is and write code on the esp-01 to simulate the serial monitor interaction. On the esp-01 side write code to interface to ThingSpeak (libraries are available for the esp8266) on one side and the Mega serial interface on the other.

From
Mega <-----> serial monitor
To
Mega <-----> esp-01 serial to esp-01 ThingSpeak <---> WiFi

If you keep a human readable serial interface you can debug the two sides independently with the serial monitor. The Mega interface would be the same. For testing the esp-01 interface you can send commands from a serial monitor.

serial monitor <---> esp-01 serial to ThingSpeak <---> WiFi
test commands/
responses

This is the method I used to switch from a serial interface to a Web interface.

I think I understand what you’re saying, but I just want to make sure it will work long-term before I try to do anything. I eventually want to be able to create a sensor system that I can leave in some area to collect measurements, but not connected to a computer or anything. I want to be able to just power the system, and have the data sent to ThingSpeak. Is this still possible with the workflow you’ve described?

You will still need a WiFi connection to the router, but the PC can be eliminated. The esp-01 will talk directly to the router in station mode for the ThingSpeak connection.

The esp-01 acts as a translator between the Mega serial interface and the ThingSpeak web interface.

With two processors talking to each other you will need some way to get them in sync. At power on it may not be predictable which one of them will start up first. You should decide "who is in charge" and have that one send the initial message to the other and wait for a response. If the response doesn't come in some time out interval try again. If start up time is not a problem this can be simplified to a delay longer than any reasonable start up on the second processor (probably seconds) before sending the message.

2 Likes

Is it possible to use this library if my ESP-01 is using AT firmware version 1.2.0.0?

According to the library page, the minimum supported AT firmware version is 1.7