Off the bat of my growing interest in IoT, I decided to order the seemingly ever-popular ESP8266. This was not a difficult choice, owing to the breadth of documentation, support, and the dirt-cheap price. When the module arrived I did have a few setbacks which delayed my usage of it, although these were mostly due to my lack of research. An example would be the fact that the ESP8266 is programmable on its own due to it having microcontroller capabilities. This means you can install the ESP8266 board in the Arduino IDE and program it separately to your Arduino board.
This is amazing of course, but it did irk me slightly because I wanted to use the module as a peripheral, and not as a separate board. This made the library search difficult to an extent as quite a few expected you to be programming the ESP8266 as a board. However if you aren’t, expect you to have a wifi shield or another one of Adafruit’s magical boards for it to work.
So, I wrote my own and called it “ESPresso” (Witty, I know). I did this because I wanted using the module to be simple and as a novice, I wasn’t finding it a walk in the park. The idea was fairly simple, the module uses “AT” (Attention) commands for operations. So all I had to do was make functions that passed the correct AT commands to the module. For example, a CheckAlive() function would send “AT” and assure that an “OK” response was received.
To pass along the AT commands you have to have another way of Serial Communication on the Arduino in addition to the standard Serial. I used the SoftwareSerial library for this. This is to prevent miscommunication between the ESP8266 and the Arduino board. My library worked, but I ditched it in the end because I found another library called WifiEsp that also used serial communication to talk to the module and that was already built.
What You Will Need
- M – F and M-M wires
- Arduino or similar board
If you are following on with the app, you will also need the following installed:
- An IDE (VSCode, Visual Studio or other)
- .NET Core SDK installed
- NodeJs and Ionic framework installed (Plus other SDKs if you wish to test the app natively on your phone, although not necessary)
This was another point of knowledge in which I was lacking. The board needs 3.3v NOT 5.0v. The board will apparently fry if anything greater than 3.3v is used. I ordered a voltage divider to try and divvy up the power from the Arduino’s 5v pin such that I would only feed in what the module needed. However, for me, the 3.3v pin works just fine. People say that this can underpower the module and can be the source of a few problems should they occur, but to save a headache I just use the 3.3v.
Just in case: I am not condoning that you should wire your ESP8266 in this way and am not responsible for damage to your module.
Use this image of the component pinout as fritzing’s component did not look that similar to what I had. We are not using GPIO0, GPIO2 or RESET pins.
Make sure that the TX and RX pins from the ESP8266 are in pins 3 and 2 respectively on the Arduino. This can cause the module to not work correctly in the code. Also, make sure that the VCC from the Bread Board goes to 3.3v NOT 5.0v, CH_PD also needs to go to the 3.3v source, and GND goes directly to ground.
You can now test the module by installing the WifiEsp library above and opening the ConnnectWPA sketch that comes with it through File -> Examples -> WifiEsp -> ConnectWPA. You will need to change the SSID and password in the sketch to your internet details. You will also need to change the line SoftwareSerial Serial1(6, 7); // RX, TX to SoftwareSerial Serial1(3, 2); // RX, TX to correspond to the correct pins on the Arduino.
Upload the sketch to your board by pressing the arrow in the top left corner of the IDE or Sketch -> Upload. If you open up the Serial Monitor Tools -> Serial Monitor and set the baud rate to 115200, you should see the output from the Arduino. Hopefully, it should connect to your Access Point with no problems and output the ESP8266’s IP address with a whole host of other info every 10 seconds. As for using the ESP8266 at a basic level, that’s pretty much it.
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initialization successful – 2.0.0
Attempting to connect to WPA SSID: ******
[WiFiEsp] Connected to ******
You’re connected to the network
IP Address: 192.168.0.72
Take a note of the IP address assigned if you are continuing on with this guide.
I quickly scaffolded and wrote an app that enabled you to control the Arduino’s inbuilt LED. The base of the code is already written in an example sketch of the WifiEsp library and is called WebServerLed. Load it in the Arduino IDE, change the SSID, password and serial pins like we did before, then upload it. The code essentially utilizes the server functionality of the ESP8266 and allows it to serve requests (You can even connect to an AP and do this at the same time!). In this case, if you send a GET request to the modules IP address with “H” (High) it will turn the LED on, and if you send “L” (Low) it will turn it off.
I used the endpoints above in a wrapper written .NET Core, which is called as a proxy for the module. The reason why I used this configuration was to try and have as little logic as possible within the Arduino’s loop() function. My belief was that the Arduino should just listen for requests and turn the LED on or off and nothing more. Another reason I did this was for security. Even though I only used this project on my local network, if I forwarded the port and allowed requests directly to the ESP8266, this could be exploited in some way (possibly by a port scan/brute force). By having the API exposed instead (with some form of Authentication) I can be more certain that my IoT projects are secure.
The app was written using the ionic framework , which entails AngularJs with typescript, HTML, and CSS (native API’s too if needed) and was fun to make. There is only one tab even though the others are labeled, which just allows you to turn the LED on or off by making API calls to the above backend endpoints.
In order to get everything working, follow these steps:
- Download the .zip files from both the API and App repositories and extract them.
- Open the API code in one instance of your chosen IDE and the App code in another.
- In the API code, edit the appsettings.json file and update the IP entry with the IP address of your ESP8266. Run the API code either by clicking IIS Express with the green arrow in visual studio or by typing dotnet run from the command line in the root of the project directory. Note the port number when the application starts.
- In the App code, update lines 23 and 24 in src -> pages -> led.ts, and make sure that the port on http://localhost:5000 matches the port number from above. Start the app by running ionic serve -c in the root of the project directory. The first time may take a while due to NPM module installs and such.
- On startup, the app should open a browser window, but if it doesn’t, it will tell you the port to navigate to. If you aren’t seeing the buttons, try right click -> inspect -> Toggle Device Toolbar and set that to iPhone/Android.
- Give it a try! If the ports and IP addresses are correct everything should be working.
In summary its been an exciting first IoT project with the ESP8266. I have many more ideas for projects using this module and can’t wait to get started.