An inexpensive IoT enabler using ESP8266
An inexpensive IoT enabler using ESP8266

A major component of or rather the very concept, Internet of Things, is about how to connect various devices to the network so that they can both send data and receive commands. Various technologies to address the last mile connectivity, such as bluetooth, wifi, NFC, etc... already exist, but most of these are complicated to deploy and often need additional hardware such as a local control server or appliance.

In this instructable, I show you how to build and configure a simple standalone board that with the help of a wifi network can send environmental data to the internet and receive control commands to turn on/off a switch. This project, which can be built for less than 10 or 15 US$ does the following:

- Send temperature/humidity readings from a DHT11 sensor to a MQTT broker and a Thingspeak channel.

- Listen for MQTT messages and turn on or off a relay connected to ESP8266.

- Check a Thingspeak channel periodically and turn on/off a relay if the field has been updated.

In simple terms, this is a internet enabled temperature/humidity sensor and relay. The relay acts a two-way switch and can be used to turn on/off any AC appliance connected to it. The parts needed cost only less than 15 US$ and absolutely no other hardware is needed. The software used is all open source and only free online services have been used to connect the board to the internet. It is assumed there is a working wifi network available for the board to connect to.

Although a few instructables that show you how to configure a ESP8266 module to push data to the internet already exist, I have not seen a solution which also lets you control a device attached to the ESP8266 module by receiving commands from the internet. This instructable shows ways to do that easily.

Step 1: Basics

What is ESP8266?

ESP8266 is a highly integrated chip designed for the needs of a new connected world. It offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.

https://espressif.com/en/products/esp8266/.

http://mcuoneclipse.com/2014/10/15/cheap-and-simpl...

https://scargill.wordpress.com/?s=esp8266

What is Thingspeak?

MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. For example, it has been used in sensors communicating to a broker via satellite link, over occasional dial-up connections with healthcare providers, and in a range of home automation and small device scenarios.

http://mqtt.org/

http://en.wikipedia.org/wiki/MQTT

What is Thingspeak?

ThingSpeak is an open source "Internet of Things" application and API to store and retrieve data from things using HTTP over the Internet or via a Local Area Network. With ThingSpeak, you can create sensor logging applications, location tracking applications, and a social network of things with status updates.

https://thingspeak.com/

Step 2: Parts needed

An inexpensive IoT enabler using ESP8266
An inexpensive IoT enabler using ESP8266

1. ESP8266 ESP-01 module

2. DHT11 or DHT22 sensor

3. 5v relay

4. LM1117 3.3v LDO voltage regulator

5. 1 x 470 uF capacitor

6. 2 x 10 uF Tantalum capacitor

7. 2 x 1K resistor

8. 1 LED

9. male breakaway headers

10. PCB Board

11. FTDI USB to TTL adapter cable

The ESP8266 module, the DHT11/22 sensor and the USB to TTL cable can be ordered from ebay.com and all three of them together should not cost more than $9.00. And the rest of the components can be bought from any electronic shop for a couple of dollars.

A working wifi access point and its login details should be available for the ESP8266 module to connect to.

Step 3: Circuit

An inexpensive IoT enabler using ESP8266
An inexpensive IoT enabler using ESP8266

The circuit is pretty simple.

The board can be powered either by just the USB cable or through a 5v DC supply connected to the power socket at the top. A LM1117-3.3 LDO is used to get 3.3v needed for the ESP8266 module. A DHT11 sensor is connected to GPIO2 pin of ESP8266.

The GPIO0 pin can be connected to the ground or relay based on the jumper position.

Step 4: Accounts on Cloudmqtt.com and Thingspeak.com

An inexpensive IoT enabler using ESP8266
An inexpensive IoT enabler using ESP8266
An inexpensive IoT enabler using ESP8266

The board pushes temperature and humidity readings as well as the relay state to:

  • MQTT broker: Any MQTT (http://mqtt.org/) broker can be used. In my case, I have used a free online broker at www.cloudmqtt.com. You would need to create an account as well as an instance. And enter the instance details in user_config.h. An advantage of using an online broker is that it can be accessed from any network. If a local broker is used (for example, running on a local Raspberry Pi box), then it may be difficult to access it from outside the home network since incoming ports are usually blocked by ISPs.
  • Thingspeak.com: An account needs to be created on Thingspeak.com. Additionally, a channel with these three channels also needs to be created and its details entered in user_main.c:
  • field1 - relay state
  • field2 - temperature
  • field3 - humidity

    Although in its present form, the board needs both cloudmqtt and Thingspeak connectivity for all functions to work properly, the code can be easily tweaked to use only one of these options.

    Step 5: Code

    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
    Show All 7 Items

    The code is available at:

    https://github.com/tangophi/esp_mqtt

    Before compiling:

  • Configure ESP8266 build environment using steps in http://signusx.com/esp8266-windows-compilation-tut...
  • Update user_config.h and change MQTT host and login details as well as WiFi login details.

    #define MQTT_HOST "your cloudmqtt instance name"
    #define MQTT_PORT your cloudmqtt instance port

    #define MQTT_USER "your cloudmqtt instance username" #define MQTT_PASS "your cloudmqtt instance password" #define STA_SSID "your WiFi SSID" #define STA_PASS "your WiFi password"
  • Update following two global variables in user_main.c to your values: char YOUR_THINGSPEAK_API_KEY[]= "xxxxxxxxxxxxxxxxx"; char YOUR_THINGSPEAK_CHANNEL[]= "xxxxxxxxx";
  • Build target 'all'.

    Step 6: Flashing the firmware to the module

    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266

    * Connect GPIO0 to ground by putting the jumper in the proper position as shown in the figure.

    * Disconnect and reconnect the USB cable to reset the ESP8266 module.

    * Flash the firmware.

    Step 7: Running the board

    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266
  • Remove the jumper.
  • Power the board using either the USB cable or a 5v DC power supply. Make sure the power to the ESP8266 module is reset after the jumper is removed.
  • After a couple of seconds, put the jumper in a position to connect GPIO0 to the relay.
  • Open CoolTerm or RealTerm window and connect to the correct COM port. An output like the following indicates everything is running fine.

    WIFI_INIT

    MQTT_InitConnection

    MQTT_InitClient

    System started ...

    mode : sta(18:fe:34:9d:f5:36)

    add if0

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    scandone

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    STATION_IDLE

    add 0

    aid 4

    pm open phy_2,type:2 0 0

    cnt

    connected with dlink, channel 2

    dhcp client start...

    STATION_IDLE

    STATION_IDLE

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    STATION_IDLE

    STATION_IDLE

    STATION_IDLE

    STATION_IDLE

    ip:192.168.0.104,mask:255.255.255.0,gw:192.168.0.1

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    TCP: Connect to domain m11.cloudmqtt.com:19449

    DNS: found ip 107.22.157.224

    TCP: connecting...

    MQTT: Connected to broker m11.cloudmqtt.com:19449

    MQTT: Sending, type: 1, id: 0000

    TCP: Sent

    TCP: data received 4 bytes

    MQTT: Connected to m11.cloudmqtt.com:19449

    MQTT: Connected

    MQTT: queue subscribe, topic"/esp8266/temperature", id: 1

    MQTT: queue subscribe, topic"/esp8266/humidity", id: 2

    MQTT: queue subscribe, topic"/esp8266/relay", id: 3

    MQTT: Sending, type: 8, id: 0001

    TCP: Sent

    TCP: data received 5 bytes

    MQTT: Subscribe successful

    MQTT: Sending, type: 8, id: 0002

    TCP: Sent

    TCP: data received 5 bytes

    MQTT: Subscribe successful

    MQTT: Sending, type: 8, id: 0003

    TCP: Sent

    TCP: data received 5 bytes

    MQTT: Subscribe successful

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 24 00 1b 00 [3f] CS: 3fTemperature = 2700 *C, Humidity = 3600 %

    DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

    DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

    DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

    Temperature: 27.0 *C, Humidity: 35.0 %

    MQTT: queuing publish, length: 28...

    MQTT: queuing publish, length: 25...

    MQTT: Sending, type: 3, id: 0000

    TCP: Sent

    MQTT: Published

    TCP: data received 28 bytes

    MQTT topic: /esp8266/temperature, data: 27.0

    MQTT: Sending, type: 3, id: 0000

    TCP: Sent

    MQTT: Published

    TCP: data received 25 bytes

    MQTT topic: /esp8266/humidity, data: 35.0

    DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

    In http_post_callback... http_status=200

    response===82===

    DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

    In http_get_relay_state_callback... http_status=200

    response===3

    1.0

    0

    ===

    Sending on to /esp8266/relay...

    MQTT: queuing publish, length: 20...

    MQTT: Sending, type: 3, id: 0000

    TCP: Sent

    MQTT: Published

    TCP: data received 20 bytes

    MQTT topic: /esp8266/relay, data: on

    DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

    In http_post_callback... http_status=200

    response===0===

    DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

    DHT: 23 00 1b 00 [3e] CS: 3eTemperature = 2700 *C, Humidity = 3500 %

    Step 8: View and control through Thingspeak.com

    An inexpensive IoT enabler using ESP8266

    View

    The temperature/humidity readings as well as the relay state can be seen at

    https://thingspeak.com/channels/21370

    Control

    For switching on the relay, the field1 of the Thingspeak channel should be updated to 1.0 using the following URL:

    https://api.thingspeak.com/update?key=YF2DC4HXFSUQ...

    Simply copy/paste the above URL in a browser window. If the response returned is a non-zero integer, it means that the field has been updated correctly. If a zero is returned, then try copy/paste the URL again till you get a non zero integer.

    For switching off the relay, the field1 of the Thingspeak channel should be updated to -1.0 using the following URL:

    https://api.thingspeak.com/update?key=YF2DC4HXFSUQ...

    Try it out yourselves !!!!

    The board is online and you are welcome to turn on/off the switch by sending commands using the above URLs. I have connected a low power AC bulb to the relay and it will turn on or off :)

    Step 9: View and Control using MQTT

    An inexpensive IoT enabler using ESP8266
    An inexpensive IoT enabler using ESP8266

    View

    Any MQTT client can be used to connect to the cloudmqtt broker to display the messages published by the board. I have used a Chrome App called MQTTlens.

    Control

    A message can be 'published' to the /esp8266/relay topic using a MQTT client to turn on/off the relay.

    Step 10: Issues

    There is a small issue in the circuit. If the board is powered on with the jumper connecting GPIO0 to the relay, then the relay switches on immdiately, but the board does not boot up properly. I suspect that the relay may be momentarily drawing too much current.

    Workaround

    A workaround for this issue is to power on the board with the jumper removed. After a couple of seconds, the jumper can be inserted to connect GPIO0 to the relay and everything works fine after that.

    Proper solution

    I guess a inverter can be between GPIO0 and the relay. So initially GPIO0 is HIGH and hence the relay should not turn on automatically on power up and the ESP8266 module should boot up fine. Later on to turn on the relay, GPIO0 can be made LOW and to turn off the relay it can be made HIGH.

    Step 11: Next steps and Conclusion

    In this project, we have seen how a standalone ESP8266 module can be used to talk to the Internet to publish data as well as to receive commands. A ESP-01 module was used which has two GPIO ports only.

    Instead of a ESP-01 module, if a ESP-03 module is used, then more sensors can be attached. However, there are still only 6 GPIO ports on ESP-03 modules. ESP-12 has 8 GPIO ports.

    Anyway, a ESP-01 module can be used in conjunction with an Arduino board to connect more sensors and even a display. The ESP-01 module should still have the standalone firmware such as the one used in this project (and not the AT firmware) and can talk to the Arduino through the UART port.

    Plans for next project include having a ESP8266 module connected to an Arduino with LCD display, motion detector, more relays and other sensors. Also develop an Android app to control the board through thingspeak or mqtt.

    All suggestions/comments are welcome. Please feel to use the code/design in this instructable as you may please. If you make something interesting out of this instructable, a message on what it is would be nice :)

  •  
     

    Tag cloud

    make build easy simple arduino making homemade solar laser printed portable cheap mini building custom cardboard wooden create super lego turn paracord chocolate your paper light intel