ESP8266 as Master, Arduino MEGA as slave

As of right now, I am in a team that is working on a project, using an Adafruit ESP8266 Huzzah breakout board, and an Arduino MEGA. We plan on making a rover that is controlled with Blynk. Although, we’re having a few issues with the code, and where to upload them.

We want the Arduino MEGA to hold basic instructions on powering motors through a DC motor shield, as well as servos through a servo shield. How would we know which code goes to the ESP and the MEGA? I have used the ESP8266_standalone example, but didn’t really know what to upload to the MEGA after that. The esp worked with Blynk, but would the MEGA be programmed the same way? I am just confused on how the two would communicate with Blynk.

We have two esps, one flashed with AT firmware(SDK NONOS V2.0.0), and another that has some code that we tried flashing for Blynk. Should we flash our esp with custom firmware?

I have seen code such as this:

#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
}

void loop()
{
  Blynk.run();
}

Will most of the code when using Blynk be this simple? Where do we program the Blynk buttons and functions? What does Blynk.run() do exactly?

The misconception with using an ESP to make an Arduino connected to WiFi is that the ESP is also programmable. Not really (possible, but a bit beyond the scope of this forum… see below).

It is actually just acting as a WiFi adapter, via the installed AT firmware, and your Blynk sketch, operation and I/O control is on the Arduino.

http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

Now, of course of you are using a decent ESP you would probably be better off using it as a standalone Blynk device… perhaps acting as “master” to a connected Arduino “slave” that is running normal Arduino code for controlling the I/O and feeding the data back to the Blynk controlled ESP “Master”, via serial, I2C, etc. But again, the “how to” for that is not really Blynk specific.

You can run Blynk on the ESP or the MEGA but not both. The micro end programming for Blynk buttons etc goes with whichever processor is running Blynk.

Have a close look at Blynk Docs to understand what Blynk.run() and related things do - see the Firmware section. Everything you need to know to create the user interface for a micro processor based system is in the Docs.

Assuming the Huzzah board has an ESP826612E module, as Gunner suggests it will have plenty of capacity to handle your application with the MEGA just acting as a slave to provide the physical fan out of I/O and 5V operation of peripherals. You’ll have to define your own protocol for passing data (e.g.MEGA input states to ESP and desired MEGA outputs states from ESP) between the ESP and the MEGA.

The Arduino IDE provides a very convenient tool for writing the custom firmware that you will need for your ESP and flashing it.

Not normally on both but could technically be done i.e. Mega could use USB (or Ethernet) for Blynk and ESP obviously uses the WiFi connection.

Getting back to the original query, if it was from a student group just starting out with Blynk and they want to just use Blynk to directly control and read Mega I/O, I guess the simplest thing would be to run Blynk on the MEGA. Running it on the ESP would require them getting into using virtual pins etc.

Is using the ESP8266_Shield.ino example on the MEGA with ESP flashed for AT commands the way to go for the simplest set up? Subject to the advice at http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware? Their main loop would then only need Blynk.run() with the app using only digital or analog pins.

I haven’t tried the AT route but have instead programmed ESP’s to set up the client connection to Blynk server and then used Blynk serial on the Arduino device.

You misunderstand what virtual pins are. Physical and virtual pins work the same on an ESP or a Mega with some internet connection.

Simplest is ESP Standalone, then Mega without an ESP (i.e. USB or Ethernet) and by far the most problematic is Mega with ESP. At least the Mega has multiple Serial ports which makes it much easier than Uno / Nano with ESP.

Virtual pins are much more powerful than digital / analog pins. Study the docs.

Not entirely sure what you mean by Blynk serial but I’m guessing you mean ESP master with Arduino slave rather than Arduino master with ESP slave. I prefer ESP as the master as they were designed for iOT whereas Arduino’s weren’t.

Hi Costas,

I understand virtual pins and use them extensively in my own projects. What I was getting at was that for a student group starting from a very low level, if they only wanted to use Blynk as a simple remote control for their project, then they could confine themselves to digital and analog pins and let Blynk take care of the detail.

By Blynk serial I meant BlynkSimpleSerialBLE.h or BlynkSimpleStream.h. I had a MEGA running Blynk and programmed the ESP to connect to WiFi, connect as client to Blynk server and then bridge via serial to the MEGA. Works ok but as you say, running Blynk on the ESP, using virtual pins, and just using a MEGA/UNO/NANO as a cheap serial connected device to fan out to 5V stuff is much easier.

Thanks for all the work you put into Blynk. It’s such a great product - especially for home automation with multiple ESP’s.

Yes for low level use then virtual pins are not required but this is very low level.

What age are your students?

I would say teenagers and above should be using virtual pins unless their course is just a basic understanding of MCU’s and iOT.

So, we are college level and not against using virtual pins. All that we need to do, if we choose to use virtual pins, is study the documents. For now, we just want the MEGA to connect to the blynk app with the esp, so we can control the motors on the rover. We just want to make sure it works before getting too involved. At the moment, we’re on a deadline.

From what I gathered in here, is that I can use the ESP as a master, using virtual pins on Blynk? Those virtual pins should be able to control the physical motors on the MEGA? If that’s the case, I am gonna look into virtual pins for this project. I actually like the sound of running Blynk on the MEGA, and using the esp to connect to wifi, but if it’s easier to work with the virtual pins running Blynk on the esp, then I will go that route.

I also read that using an esp with AT firmware installed was harder to work with. Is there firmware that is ready to use for the esp from Blynk?

@BayBayMan I’ll start by answering your post in reverse.

That is correct, AT commands is very old technology but nonetheless it’s still possible to control ESP’s in this way. It’s not really a problem with the AT technology it’s more down to the physical connections between Mega and ESP (remembering the voltage divider before the RX on the ESP) and getting the baud rates right. Most ESP’s are shipped with a baud of 115200 or the rather odd 74800 and it needs to be 9600 for Blynk / Arduino. It’s a fairly straightforward AT command to change the baud on the ESP but failure to do it means your project will simply not work.

You can only use the ESP as a master if you write your own code to interface with the Arduino and it’s not related to virtual pins. With ESP as master you can then send serial commands to the Arduino to control the motors, turn on an LED etc but as I say the coding of this is down to you.

If you need a Mega, because you have a lot of motors and sensors then the normal way is the “AT firmware route” with Mega as master and ESP as slave.

For me virtual pins is simply an “Arduino function” that allows you to do lots of things with a single button press or slider movement etc compared with physical pins that just allow one thing to happen.

So physical pin is for example D13 ON / OFF

Virtual pin for example can do all the following:

Turn D13 ON / OFF
Turn D2 ON / OFF
Turn D45 ON / OFF
Send an email
Send a PUSH message
Send data to GoogleSheet
etc, etc

You have to code each of the actions for the virtual pin (Arduino function) but it’s basic Arduino code or well covered in the Blynk docs for Email widget, Webhook widget etc.

I should perhaps add that your understanding of virtual pins is probably right in that IF you were using the ESP as the master then you would have to use virtual pins rather than physical pins.

Many long term Blynkers rarely use physical pin control as they get much more flexibility with virtual pins. There are lots of Blynk features that are simply not available when using physical pins.

So for many, after the first 5 minutes of flashing an onboard LED on and off with Blynk tied to physical pins they quickly move to virtual pins.

I must admit we don’t really do any motor control so there might be some benefit of physical pins for your project but presumably you have libraries that do most of the processing.

If you use the ESP as a master to the Mega then interfacing can be as simple as using SoftwareSerial:

Send ‘1’ from ESP to Mega, Mega reads the 1 and does A
Send ‘2’ from ESP to Mega, Mega reads the 2 and does B

Send ‘26’ from ESP to Mega, Mega reads the 26 and does Z.

Since we don’t have enough knowledge controlling DC/servo motors with only an ESP, we decided on getting controllers for those motors that attach to the MEGA. We were thinking of writing our own code to interface with the Arduino serially. If we did end up doing that, could we still use Blynk? Would that make it easier to work with Blynk?

We first tried creating an HTML page that was hosted by the ESP. We quickly figured out that you could only produce so many buttons in println() functions before it becomes inefficient. That was the reason we decided to look into Blynk. We thought Blynk would take care of that problem for us.

Right now we have two esp8266 huzzah boards for testing. One has AT firmware, and another we have for testing with various firmware. We have looked into plenty ways of using AT commands to send HTTP requests. It works, but not well enough. I suppose getting the mega and esp to work together the way we want is not what the blynk forums are for. So, I am just asking if it’s possible with what we currently have to use Blynk to control this type of project.

I read your second comment, and am understanding that virtual pins just act as functions, since they work with multiple sensors. For example, one virtual pin could control the entire four DC motors to either move forward, backward, left and right? Those movements would then be written in one arduino function, which would be called by Blynk?

I believe I am at an intermediate level when it comes to programming, so I can make functions to do what I want. Whats screwing me up, is the idea of connecting an arduino and an esp together to function using Blynk. Creating a serial connection between the two successfully, then bridging that to Blynk to control our project seems very foreign to me. It could be that I am tired, or not as good at these things as I think I am, but we are working as a team to try and understand these things. For now, we want to try sticking to physical pins, to see if that works well with Blynk.

I really appreciate you all responding with great advice and questions. It helps me come up with better questions to ask; at least I hope they’re better.

I think that’s a reasonable way forward and you seem to know how to do the serial interface. Yes Blynk will work just fine like this and to be honest I believe Blynk was developed for ESP’s. They only offer the ESP as slave method because there are millions of Arduino’s in the market without internet access.

You suppose wrong and this thread is just one of many that covers using Blynk with Arduino’s and ESP’s (or ESP’s and Arduino’s). Yes you have all the hardware you need and Blynk is much easier than messing around with html on an ESP.

Correct.

If you want to use physical pins then you will have to flash the Blynk “firmware” (sketch) on the Mega which uses AT commands to interface with the ESP. You don’t need to know anything about the AT commands (other than one command for baud rate) as the Blynk library does all that.

Feel free to ask additional questions if it’s still not clear how Blynk works with Arduino’s / ESP’s.

@BayBayMan I know @costas really hates the combo setup ;), but honestly, based on what I am reading, I believe you have the knowledge to figure out supplying dedicated power to each of the MEGA and the ESP; Using 5v-3.3v level conversion (really only needed from the MEGA’s TX to the ESP’s RX), and following the AT firmware and BAUD rate setup directions provided by Blynk.

Thus I still think your simplest route (AKA tricky to setup, but tried and tested) is to use one of your ESPs with the AT firmware and connected to the Mega using one of it’s extra hardware UART ports (i.e. Serial1 instead of softserial). Then the ESP simply becomes a Serial-to-WiFi modem and you install Blynk on the MEGA. Then once the MEGA is successfully connected to Blynk server (via the ESP) you can just focus on programming and using the MEGA I/O for your motor controllers, etc.

http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

After all, is essentially the same thing as designing your own “Master - Slave” serial interface between the MEGA and ESP and sharing data between them.

As for virtual pins, they are not a replacement for physical pins, they are just a better way of linking data between App - Server - Hardware, in order to control the physical pins. Thus one button on the App can control an entire group of commands that in turn manipulates the required physical pins.

Hmm… there used to be a page for “What are Virtual Pins” in the Help Center, but either it has gone AWOL or I am going blind :confused:

EDIT while it appears that it is no longer visible in the Help Center (@Pavel or @vshymanskyy ??) this link to the document still works - http://help.blynk.cc/what-is-virtual-pins

1 Like

@Gunner I think I would agree that for the OP and his project then Mega as master is the way to go.

I just provided details of Mega as slave because that’s the title the OP gave to the thread.

1 Like

Where ESP as master is very important is when you are monitoring a remote location rather than using a local rover. With ESP as master you have the power of OTA updates etc which is not available when using the ESP as a slave. ESP’s as masters will reconnect to the internet much more readily than an Arduino as master as they were developed by Espressif to do exactly that.

Alright, nice. Thanks for that. I will look into uploading the Blynk sketch into the MEGA. I actually had to look into AT commands more when I first got the ESP, and I learned a bit from that. So, I am happy to hear that Blynk does all that for me!

I guess my next question would be what the “firmware” on the MEGA would look like. Is it similar to a sketch that we can add functions to? Is that why you put quotes on firmware? For example, the “firmware” would have the necessary code blocks to allow a connection to Blynk, but every other function would be purely us writing it. Including the serial communication. Then we can implement those functions using either physical pins, or for easier access, virtual pins. Am I understanding this correctly?

Currently we have 5V’s from the MEGA powering the ESP, and I think the Adafruit ESP8266 Huzzah actually has that level conversion all on board. Unless you mean setting up a dedicated power supply just for the ESP, and another for the MEGA. In that case, we have plans of giving each it’s own power source. We have learned from other sources that the ESP would drain the 5V, in the case we needed that 5V for other sensors.

When you say installing Blynk on the MEGA, you’re not talking about “flashing” the MEGA, right? Would installing Blynk on the MEGA replace bin files, similar to flashing an ESP module? When we flashed the ESP, we had to look for specific addresses for the bin files to go into. I guess it’s a similar question to what I asked Costas. Installing Blynk on the MEGA just means having a necessary base code for the MEGA to connect to Blynk through an ESP, right?

Thanks for the link!

I appreciate you giving a reason why the ESP would be a master! I was actually wondering what advantages the ESP would have acting as the master. I had just read that possibly having the ESP as a master could help out with sending requests by calling the basic instructions held on the MEGA from an HTML page.

Tomorrow I plan on testing more out, then coming back with more questions. If I solve my problem, then I will come back with what worked for me! Thanks, guys!

Just a side note! I really love the design of these forums. Quoting is super easy, and the scroll bar is really nice on the side. This forum is very aesthetically pleasing.

Assuming the MEGA using ESP as WiFi adapter route, then once you have done all the firmware flashing to the ESP, it just sits there waiting to be told by the MEGA to connect to the WiFi and pass that data to and from the MEGA.

Meanwhile you would program, compile and upload code to the MEGA just like you would normally, via the Arduino IDE or whatever other IDE you prefer… the only difference between a “Normal” Arduino sketch and “Blynkified” sketche is some of the formatting for Blynk functions, a bit stricter timing requirements, and of course the required ESP libraries that do all the “AT” stuff in the background, using the ESP as a WiFi conduit.

For example, here is a basic MEGA/ESP sketch that uses a Joystick widget and shows the X & Y values (via the serial monitor I think).

https://examples.blynk.cc/?board=Arduino%20Mega%202560&shield=ESP8266%20WiFi%20Shield&example=Widgets%2FJoystickTwoAxis

This uses the MEGA’s Serial1 UART port on pins 19 (RX) and 18 (TX) to link with the ESP.

Just tieing up a few loose ends as I think we have covered most things now.

Sketch Builder as per the link provided by @Gunner is where you start with a basic sketch / firmware for your particular hardware. The sketches are all included in the Arduino IDE (and on GitHub) if you have correctly configured the IDE but the Sketch Builder is easier to use for beginners.

Everything else you have put is spot on with the possible exception of serial communication. You only have to provide serial comms code when you use the Mega as a slave, which we have decided is probably not best for you at this stage. All Serial comms code for Mega as master is included in the Blynk examples in the Sketch Builder.

Yes you should use separate power for the 2 devices but remember the common ground. You can flash an ESP with low power i.e. from an Arduino but the ESP uses much more power in running mode and will generally keep crashing if you try to run if from an Arduino. ESP’s are 3.3V devices so for safety you should put a voltage divider before the ESP’s RX pin (pin 19 from @Gunner’s example). It might be the HUZZAH has this covered but for the cost of a 1K and 2K resistor I wouldn’t take the risk.

Again everything you have here is correct.

One point to note though with the 2 ESP’s you have if you have flashed either of them with any Arduino code you will have removed the AT firmware required by Blynk to use the ESP as a slave to the Mega. So you would have to flash AT bin files back to ESP.

The Mega example shows ESP baud at 115200 which I think is OK as the Mega has the additional hardware serial ports, unlike the Uno / Nano that uses the much slower software serial ports that require a baud of 9600. So just check what baud rates the ESP’s are currently using and make sure the sketch and ESP’s are using the same rate.

This may be obvious, but perhaps worth mentioning… you flash your ESP (HUZZAH) with the AT firmware via it’s own USB port, but once it is all done and working, you connect it to it’s own external power (yes, shared ground, thanks for remembering that one @Costas) and link it to the MEGA’s Serial1 ports… then leave it alone.

You do ALL other Blynk programming and serial port monitoring, etc via the MEGA’s USB port, while it links to the Blynk server via the ESP.

1 Like