Blog Barista: Greg Antrim | Aug 15, 2018 | Internet of Things | Brew time: 10 min
The internet of things (IoT) has exploded in recent years. Lights, cars, thermostats, even plants are now connected devices. Devices like the Amazon Echo and Google Home have made things even more convenient by adding voice commands to the mix.
One of the most popular platforms for IoT is the Arduino. Used by novices and experts alike, an Arduino can handle almost limitless tasks. When it comes to connecting your Arduino to the internet there are many options, but one of the most popular and widely available is the ESP8266.
Getting an ESP8266 module working and communicating with your Arduino, however, can be a complicated process. I recently went through the process myself and was shocked at how few good and complete guides there were out there. Luckily for you, I’m about to fix that situation. I have poured through forum posts, blogs with incomplete information, and half-working code samples so you don’t have to.
This post will give you everything you need to get your ESP8266 module connected and accepting commands. Specifically, we will be covering:
- Materials and software needed
- Wiring your ESP8266 module to your Arduino for testing and programming
- Initial programming of the ESP8266 including changing the mode, baud rate, and verifying that you can connect to your router
- Wiring your ESP8266 module to your Arduino for real world use
- Code needed to accept and handle JSON data
- Sending JSON Requests to the ESP Module and monitoring the output
Materials and Software
First, I would like to clarify that this post pertains specifically to a standalone ESP8266 module (Image below). There are several boards out there that have the ESP8266 chip built into the board, and the process for such boards may be different.
Above image: A Makerfocus ESP8266 wireless wifi transceiver module.
The code provided should also work in the standard Arduino IDE, but for the actual coding portion of this guide, I’d recommend PlatformIO via VS Code since it has better built in formatting, it’s easier to add packages, and most importantly, it has a dark theme. As far as a REST Client goes, I’ve been using Insomnia, but other clients such as Postman will work just as well.
Wiring ESP8266 To Arduino for Programming
Before wiring, I recommend that you connect your Arduino to your computer and flash the “Bare Minimum” example sketch to the board. This will ensure that nothing else is running on your Arduino.
We will first be connecting the ESP8266 to our Arduino to do some initial setup. I found the wiring configuration thanks to a helpful post on the Arduino forums, though I did have to make some modifications.
The wiring configuration for sending commands to the ESP8266 is different than the wiring configuration for using the ESP8266 so make sure to follow along closely. See the image below for the ESP8266 Pinout.
Connect the ESP8266 to your Arduino Uno as follows.
Note that while the ESP8266 is rated at 3.3v, we are connecting it to the 5v line from the Arduino. This is because the 3.3v line from the Arduino does not reliably provide enough power.
I have been running the module at 5v for months now and haven’t had any issues. If you are really worried about it, you can purchase a voltage regulator that will put out a reliable 3.3v.
Initial Programming of ESP8266
In order to get our ESP8266 to work properly with our Arduino, we need to do some initial programming. Specifically, we will be changing the ESP8266 to work as an access point and a client and changing the baud rate. Since most code samples out there are communicating with the ESP module with a baud rate of 9600, that’s what we will use. We will also verify that the ESP8266 module can connect to our router.
With your Arduino Uno connected to your computer, open the serial monitor via the Arduino IDE (ctrl + shift + m). On the bottom of the serial monitor there are dropdowns for line endings and baud rate. Set line endings to “Both NL & CR” and change the baud rate to “115200”. Then send the following commands:
1. Verify that the ESP8266 is connected properly.
Command to send: AT
Expected response: OK
2. Change the mode.
Command to send: AT+CWMODE=3
Expected response: OK
3. Connect to your router (Make sure to replace YOUR_SSID and YOUR_WIFI_PASSWORD).
Command to send: AT+CWJAP=”YOUR_SSID”,”YOUR_WIFI_PASSWORD”
WIFI GOT IP
4. Set baud rate to 9600.
Command to send: AT+UART=9600,8,1,0,0
Expected response: OK
5. Verify that the ESP8266 is communicating with baud rate of 9600.
Command to send: AT
Expected response: OK
If you are curious about what each command does, or are looking for more info about the commands that are available, I found this page both readable and incredibly helpful.
Wiring the ESP8266 for Real World Use
Now that we have set up our ESP module and verified that it can connect to our wireless network, it’s time to connect the ESP module to the Arduino for actual use.
In order to communicate with the ESP Module, we need to connect the TX and RX Lines to Digital Pins. You may use any two pins from 8-13, but for this tutorial I will be using pin 11 for RX and pin 10 for TX.
For reference, here is how everything should be connected:
Sending JSON Data to the ESP8266
Now that we have everything wired its time to start sending commands to our ESP module. In order to keep things simple, I am only including the code to initialize the ESP server and parse the JSON. What you do once you have the JSON data is up to you. I personally use it to control my lights with one JSON payload turning the lights off and another turning the lights on.
To get started, open Visual studio code. If you have the PlatformIO IDE set up correctly, a tab should open titled “PIO HOME”. From there follow these steps:
1. Under the Quick Access menu, click New project
2. On the Project Wizard dialog:
a. Enter a project name in the Name field
b. Select Arduino Uno from the Board dropdown
c. Click Finish
3. Go back to the PIO Home tab and select Libraries from the side navigation bar
4. In the search box, type WifiESP and hit Enter
5. Install the library by bportaluri
6. Clear the Search box and type in ArduinoJson
7. Install the library by Benoit Blanchon
8. From the file explorer on the left side, open src/main.cpp
9. Copy and paste the code below:
Make sure to change line 11 to match the pins that you used (if you used 10 and 11 as recommended, no modification is needed). Also, be sure to enter your SSID and WIFI password on lines 18 and 19.
Before we upload the code to the Arduino, let’s take a look at what is happening. Here is a quick overview:
- Initialize the ESP module via the WifiESP Library we installed earlier
- Connect to our wireless network and wait for that connection to be established
- Print out our local IP so that we can use it later in our REST requests
- Start our server and tell it to listen for clients on port 80, the standard HTTP port
We continuously wait for a client to connect. Once a client is connected:
- Print that a client has connected
- Parse the incoming request
- Search the request for the opening JSON bracket
- Read in request until closing JSON bracket is found
- Add brackets back around the JSON data
- Use the ArduinoJSON library to convert our JSON string into a JSON object
- NOTE: If you change the JSON request format at all, you will need to re-calculate your buffer size using this tool
- Get and print the value of the “action” key in our JSON object.
- Print a different message based on what the value of the action key was
Now it’s time to upload everything to the board.
From Visual Studio Code, open the Tasks menu and select Run Task, then select PlatformIO: Upload and monitor.
If everything goes right, you should see the following in the console:
Sending JSON Request to ESP8266
All that’s left to do now is send a request to our module and verify that the request is received. Make sure you grabbed the IP address from the last step and that your upload and monitor task is still running. Then, open up your favorite REST client and enter the following:
Send the request and go back to Visual studio code. You should now see the following in the upload and monitor console:
You now have an Arduino capable of receiving commands via WIFI. All you should need to do now is decide what your JSON data looks like and what actions need to be taken once that data is received.
Hopefully you found this blog post helpful and I’ve spared you the pain of tracking down all of this information yourself. Let me know what you think in the comments.
Bonus points for links to projects that made use of this guide.
Other recent posts:
OKEMOS, MI, April 19, 2021 — Kunz, Leigh & Associates (KL&A) congratulates Bob Marquis, one of KL&A’s most experienced and well-respected program management consultants, for obtaining the Program Management Professional (PgMP) certification from the Project Management Institute (PMI). PMI is the global leader for those who work in project, program, and…
Pandemic-Driven Feedback Sparks Significant Changes at KL&A
OKEMOS, MI, Nov. 9, 2020 — Kunz, Leigh & Associates (KL&A) has been named a Top Workplace by the Detroit Free Press for the third year in a row.
In the midst of a global pandemic, KL&A is humbled to be named a Top Workplace during this unpredictable and challenging year. Of the 2,707 organizations invited to complete the survey this year, only 150 were recognized as Top Workplaces in Michigan…