Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT

Connecting IoT devices to an Azure web service using $3 WiFi modules

5.00/5 (3 votes)
5 Mar 2015CPOL3 min read 26.6K  
One of the biggest problems in making simple devices into IoT devices is cost. The ESP-8266 module solves that problem.

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.

Introduction

One of the biggest problems in making simple devices into IoT devices is cost. The ESP-8266 module solves that problem. Connecting numerous devices with Arduino, Netduino or Raspberry Pi units can easily run into hundreds of dollars. Those devices are also power hungry and adding to that the cost of cabling makes those solutions less attractive. For the minimal cost of about $3 each, the ESP-8266 modules you get the benefits of simplicity, WiFi connectivity and compact size of less than one-inch square.

Image 1

ESP-8266-01 module

Background

The software stack is open source and utilizes the installable Lua programming language interpreter from NodeMCU. Utilities for uploading programs include the LuaLoader and LuaUploader applications. Lua and Python have many similarities. The ESP-8266 can be a WiFi Access Point, Client or both. This makes it ideal for IoT implementations. It has a 32bit microprocessor as well as the built-in WiFi stack. The basic model has two General Purpose I/O pins and serial data in and out. It's a 3.3 volt device so it could be run from a single 3.3v Lithium Ion battery or a pair of conventional 1.5v batteries, even rechargeable ones.

Using the code

To install the Lua interpreter and to upload programs, a 3.3v compatible USB to serial adapter is required. Most USB adapters are limited to 100ma supply current, so a 5v to 3.3v regulator or battery pack are needed in additional to the USB adapter when transmitting. Once programmed, the USB adapter is not needed. There is a file that the user creates named init.lua that the device loads automatically on boot-up or reset and can be the complete client solution. This file can also load a secondary file that contains other program code. The file init.lua is convenient to make the initial WiFi connection..

wifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
ip = wifi.sta.getip()

To conect to your website:

conn=net.createConnection(net.TCP) 
conn:dns("mywebsite.com",function(conn,ip) 
print(ip) 
conn:connect(80,ip)
conn:send("GET / HTTP/1.1\r\nHost: mywebsite.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n") 
end) 
 
conn:on("receive",function(conn,payload)
  print (payload)  
end)

Programming to the I/O pins is quite simple in Lua. It's easy to set a pin for input or output, set its state, or leave floating. There are also several models of the ESP-8266. Some have a printed-circuit trace for the antenna, some use a chip antenna, and some an external antenna. WiFi range is very good with all models. Some of the models have more I/O pins and even analog-to-digital and pulse-width-modulation capability. A few companies make development boards with LEDs or photocell sensors.

The Azure Solution

Each ESP-8266 will send it's data over HTTP to the Azure web service which will store the data in a database. The database then can be queried for any and all data for visual status, charting or other analysis. Parameters such as on/off or light level are just a few of the many possibilities.

Other Ideas

Finally, if an Arduino based solution is still desired, the stock ESP-8266 can be used as the WiFi link with serial data exchanged with the Arduino using AT commands.

Finally

If the ESP-8266 gets stuck in an endless loop and a reset fails to fix it, the firmware can be reinstalled using the NodeMCUFlasher application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)