'Pimping' Your First IoT WiFi Device. Part 4 : IoT, Home Automation

by JohnSmith in Circuits > Arduino

5059 Views, 28 Favorites, 0 Comments

'Pimping' Your First IoT WiFi Device. Part 4 : IoT, Home Automation

SAM_1125.JPG

Ok, not so much pimping but extending your first IoT device

Introduction

This Instructable documents how simple it is to extend and further develop an IoT device (pictured above) and is based around the ESP8266-01 WiFi enabled module from the earlier Instructable 'Creating your first IoT WiFi Device. Part 3 : IoT, Home Automation'

The IoT device in question has a single led output and can read both local temperature and humidity levels via a DHT22 sensor and calculate the resultant heat index. Control functionality and a network connection is achieved via an ESP8266-01.

To set the system up, use the source code (provided) and create the necessary circuitry as before you will need the following;

  1. A minimal grasp of electronics,
  2. Knowledge of Arduino and it's IDE,
  3. Some Patience
  4. Some understanding of your home network

System entry requirements;

  1. Home network with WiFi
  2. An MQTT Broker correctly configured (See Part 1 and 2 : IoT, Home Automation)
  3. MQTTSpy installed and set up. (See Part 2 : IoT Home Automation)
  4. An ESP8266-01 programming circuit. (See Instructable 'Practical Circuit Construction with Strip Board') for details.
  5. Arduino IDE configured to compile software for ESP8266-01. (See Instructable 'Setting Up the Arduino IDE to Program The ESP8266-01')
  6. Though this Instructable can be read on its own though it was developed as an extension to 'Creating your first IoT WiFi Device. Part 3 : IoT, Home Automation'

You will also need the following parts;

  1. 1 off ESP8266-01
  2. 1 off 3v3 Power supply (I used a YuRobot 3v3/5v adpator)
  3. 3 off 10K resistors
  4. 1 off 320R resistor
  5. 1 off red led
  6. 1 off DHT22 Temperature and Humidity Sensor
  7. 1 off push fit perf board for prototyping
  8. Various interconnection wires.

The article will cover these topics;

  1. Brief overview of the circuit
  2. Brief overview of the software
  3. How to test your IoT device and see it in action
  4. References used

Series Links

To Part 3 : Creating Your First IoT WiFi Device. Part 3 : IoT, Home Automation

To Part 5 : Creating Your First IoT Ethernet Device. Part 5 : IoT, Home Automation

Circuit Overview

IoTPart4_bb.jpg
IoTPart4_schem.jpg
DHT22-PinOut.png
xesp8266-pinout_etch_copper_top_png_pagespeed_ic_SiFP37EJYg.png

The simple circuit layout and circuit diagram are pictured above.

There are three 10K pull up resistors attached to RSTn, CH_PD and GPIO2 respectively.

Output from the DHT22 Sensor is fed to GPIO2 via a 10K pull up resistor.

Output is signalled by turning on the led by sending pin GPIO2 low to sink current via a 320R resistor. Consequently the logic level is inverted ie o/p low = Led On, o/p Hi Led off and is corrected in software.

.

Note : The ESP8266-01 Is a 3v3 device. If you apply more than this you run the risk or permanently damaging the module.

Software Overview

Preamble

To successfully compile this source code you will need the following extra libraries;

PubSubClient.h

DHT.h

ESP8266WiFi.h

Full details also given in source code below.

You will also need to know;

  1. The IP address of your MQTT Broker (in IPv4 format AAA.BBB.CCC.DDD) : MQTT_SERVER
  2. Your WiFi Network SSID : ssid
  3. The Password for your Wifi Network : password

Code Overview

On startup the software connects to the WiFi network using the pre-supplied connection details above.

Once connected to a WiFi LAN the IoT device will publish its details on the following MQTT topic;

  1. /WiFiDevice/SwVerConfirm : This contains a concatenated string, comma separated, formed by the generic device name, unique MAC address of device and the name of the Arduino file used to program the device. in this case 'esp8266,18:fe:34:9f:46:a0,ESP8266TempHumi2.ino'. Notice here the response is dynamic and only known once the device has been powered up and read the MAC address specific to it.
  2. /WiFiDevice/Temp1Status : On initial connection to the WiFi this topic publishes a 'Connected' message to the broker. Subsequently it is used to publish the local temperature in Degrees Celsius accurate to 2 decimal places. Updates are only issued if there is a change in temperature to prevent this IoT from 'clogging' up the network.
  3. /WiFiDevice/Humd1Status : Similar to Temp1Status, but is a representation of the local humidity levels scaled as a percentage.
  4. /WiFiDevice/HeatInd1Status : Similar to both Temp1Status and Humd1Status, though in this instance the software computes the new heat index from the temperature and humidity readings.

It then subscribes to the following MQTT topics;

  1. /WiFiDevice/SwVerCommand : When notification is received of this topic the device will respond with a /WiFiDevice/SwVerConfirm publication. This is a broadcast response. ie. all devices could respond.
  2. /WiFiDevice/18:fe:34:9f:46:a0/SwVerCommand : When notification is received of this topic the device will respond with a /WiFiDevice/SwVerConfirm publication. This is a targeted response. ie. only this device will respond.
  3. /WiFiDevice/Led2Command : When notification is received of this topic the device led output is set accordingly. Payload '1' = Led on, Payload '0' = Led off. Once a command has been actioned the device will respond by publishing the Led status via the /WiFiDevice/Led2Confirm topic 'On' or 'Off'

Once completed the software now drops into a loop monitoring WiFi and Broker connections along with any changes in temperature or humidity levels or new publications such as turning the local Led on or off etc.

Testing Your IoT Device

IoT.jpg
Untitled.jpg

To test our new revised IoT device we will use the set up outlined in the first slide above.

That is we will use MQTTSpy on the PC to connect to the Mosquitto MQTT Broker on the Raspberry Pi which in turn will connect to our IoT device over WiFi.

The screen shot above shows the respective publications and subscriptions being exercised via MQTTSpy.

For further details on how to use MQTTSpy see Instructable Part 3 (Creating your first IoT WiFi Device. Part 3 : IoT, Home Automation)

References Used

I used the following sources to put this Instructable together;

PubSubClient.h

DHT.h

ESP8266WiFi.h

ESP8266 Community

ESP8266 Arduino Core