WiFi and the Internet Of Things
The Internet Of Things (IOT) is going to be (or rather already is) the biggest hype in technology in recent years. It is predicted to be a multi-Billion (with a capital B) dollar business with 30 billion devices connected by 2020. If you do some rough estimates, this is about 20 million IOT devices manufactured and deployed every day!
A popular IOT device that is catching fast momentum is the ESP8266 module. In this project, you'll learn how to work with the ESP8266 WiFi module using the Arduino IDE.
The ESP8266 WiFi module has both a microcontroller and a WiFi interface built-in. Hence there is no need for the Arduino board, but we still will use the Arduino IDE for programming the ESP8266 module. There are many different versions of the ESP8266 board. This tutorial discusses how to use the ESP8266-01, ESP8266-12, and the Wemos D1 mini modules. Here are the documentations for the basic Arduino WiFi functions, the main ESP8266 library depository with the source files and examples. Another link here.
This is an advanced project and requires many skills, however, the instructions in this tutorial should be simple enough to quickly get you going. At the end, you'll have a WiFi web server implemented on this tiny chip and you can browse to it from anywhere in the world to control things.
Parts needed:
The Wemos D1 mini development board has a ESP8266-12 and a USB-to serial adapter built-in. So this is much easier to work with than just the barebone ESP8266. With this you can directly plug in a micro USB cable to program the on-board ESP8266-12. | ||
1 | To program the ESP8266 WiFi module, you need to add the ESP8266 Board definition to the Arduino IDE program. You only need to do this once so if it has already been done then skip this step and go to step 2.
| |
| ||
2 | If you are using the Wemos D1 mini development board then just plug in the micro USB cable between the D1 mini board and your computer, and continue with step 3 below.
If you are using the CP2102 USB-to-serial adapter to program the ESP8266-12 or ESP8266-01 WiFi module, then click here for instructions on how to make the connections. Come back here and continue to step 3 after you have made the connections. | |
3 | Create a new sketch and type in this program.
Replace "your ssid" and "your password" with the ssid and password of the WiFi network that you want to connect to. Note: You can only connect to a 2.4 GHz wifi signal. A 5 GHz wifi signal will not work. Also you cannot use a "Guest" account that requires a response. |
|
4 | Uploading the program to the ESP8266 WiFi module.
| |
5 | Running the program.
After the program is uploaded successfully, it will immediately run and you should see something like this. Note the IP address that is automatically assigned by your router to your ESP8266 wifi module. In this example it is 192.168.1.116. If you don’t see the last line but just more and more dots ........., then most likely the ssid and/or the password for the WiFi network is incorrect. Correct it and upload the program again. Remember that you can only connect to a 2.4 GHz wifi signal. A 5 GHz wifi signal will not work. Also you cannot use a "Guest" account that requires a response. | |
6 | The above code doesn't do anything interesting except that it connects to the internet and gets an IP address from your router.
To make it into a simple webserver, we just have to add a few extra lines of code as shown here. The new lines are flagged with ****.
If there are some garbage characters at the beginning of the printout then just ignore them. If the program doesn't automatically run, then press the reset button on the Wemos board. If it cannot connect to the wifi for about 30 seconds, then make sure that you have entered your ssid and password correctly in the code and upload the program again. |
|
7 | Now that you are able to program the ESP8266 module,
you can run (almost) all of the programs on the ESP8266 module just like on the Arduino.
The differences are in the I/O port usage.
The Wemos D1 mini uses the ESP8266-12 so they are the same in terms of programming. For the ESP8266-12, the built-in LED is on pin 2. For the ESP8266-01, the built-in LED is on pin 1. The ESP8266-01 also uses pin 1 for the connection to the serial monitor. So if you use the built-in LED, it will interfere with the outputs to the serial monitor. In other words, don't use both the built-in LED and the serial monitor in your program. Just use one or the other. Here are the GPIO pins that you can use for the three different wifi modules.
|
|
We are now ready to create some fun wifi projects. Click on the Experiments button below to continue. | ||