Date: Friday 29th July
Author: Amit Kumar, Technology consultant
This is the second article of our three part series on the Internet-of-Things (IoT). In the first article we have talked about what IoT is, and what the future possibilities are for this technology. In this article we are going to get hands-on and make a connected IoT device.
Before we start making the device , we need a couple of things to make it work:
This IoT device will be connected to Azure services so you will need an Azure Subscription and ID. If you don’t already have one you can sign up for a free trial with £125 credit.
We are going to use ESP8266 to connect with Azure Services. This is a tiny device, almost size of a coin and it’s not very expensive (Purchase here).
We need a PIR sensor module to detect motion and report it to ESP8266 (Purchase here).
We need a FTDI adapter to connect our ESP8266 to the computer to program it. You can buy any FTDI device but try to get one which gives you 3.3 and 5v output. For this project, I am using FT232RL. This gives me 3.3 and 5v via a jumper setting (Purchase here).
We need a breadboard and a couple of jumper wires to connect all the components together.
This is not necessary if you want to use FTDI to power the device. However, I’d recommend to have this as it’s a handy power source for breadboard prototypes.
Now we have all the required components, we can start building our IoT device.
I have broken the build down into 4 steps to keep things simple:
Login to Azure Portal and follow the steps below to create the Mobile Service. We use this to monitor the motion detections.
4. Click on the ‘Create’ button as shown in the screenshot below:
5. It will take you to another URL and will open the following dialog box. Please fill the details as shown in the screenshot below and click on next button:
6. In this step, select ‘Create a new SQL database instance’ from the ‘Database’ dropdown and follow the steps on screen. Alternatively, you can choose any existing database if you already have one.
7. Once the service is created, create a table to save the data.
a. Click on Mobile Service you have just created
b. Select the DATA tab, and click on “Add a table”
c. Create the table name “MotionLog”
8. Create new API to connect with Mobile Service
a. Select the API tab and click on “Create a Custom API”
b. Name the API as “ESPAPI”
9. Select the “Dashboard” tab and Click on the ‘Manage Keys’ button to create the application keys:
10. Copy the application key and save it somewhere in the notepad. We’ll need it shortly when we program the ESP8266.
Now, we have Azure service ready to be used in ESP8266. Let’s move on to the next step and put the hardware together.
2. FTDI Serial pin outs
3. ESP8266 (01) pin outs
Once we have the hardware connected, it’s time to program the ESP8266.
There are many ESP loader tools available and you can use any of them. I am using “Lua Uploader 184.108.40.206.” You can download it from here.
After downloading the zip file, please extract it to your local folder and double click on “LuaUploader.exe” to run the program.
Below is the code for ESP8266. Before uploading the code to ESP, you need to change two configurations in the following scripts. The configuration values are highlighted in RED and marked as TO DOs.
The two configuration values are:
Please change both the configuration and copy and paste the script in the Lua Uploader.
Now connect the FTDI Serial to the computer USB and find the USB Port in the device manager.
Once you know the port number, select that Port (mine is COM3 yours might be different), Baud rate and Line Delay as shown in the screenshot below:
Once USB connection settings are in place, click on “Save to ESP” button. You should see a blue LED flashing in ESP8266 while program is being uploaded.
The Lua Uploader should look like this:
Now ESP8266 is programmed and ready to upload the movement’s ‘state’ to Azure Service. But before that we need to connect the PIR sensor to the ESP8266 in the next step.
Now you need to connect the PIR sensor and power the device. You can power the device using FTDI if you want to monitor the serial log in Lua Uploader. This is useful for debugging purpose.
Disconnect the FTDI from computer and connect the PIR Sensor as below. Once PIR sensor is connected, then connect the FTDI to the computer again to power the device.
If you don’t want to debug the program, then you don’t need FTDI and can power the device using the batteries or external breadboard power device. You just need to make sure you supply 5v to the PIR sensor and 3.3v to the ESP8266.
This is how you can connect the device using external power source:
Once the device is powered on, and you put your hand in front of the PIR sensor, it’ll upload the movement’s status to the Azure.
Just go to the Mobile Application Service you created earlier and go to the DATA tab of the API. You should see the motion log as below:
With small changes in the program you can connect other sensor as well and upload the data on Azure.
This demonstration is a simple example of the power of IoT. The combination of low cost sensor components and cloud services such as Azure make this technology accessible to everyone.
To see how your organisation can benefit contact us to book your free workshop and Proof Of Concept.