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

This Instructable documents how to create a simple IoT device (pictured above) based around the ESP8266-01 WiFi enabled module and is the third part in a series of Instructables detailing how to set up your own home IoT network.

The IoT device in question has a single input push button and led output to interact with its surroundings. 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 you will need the following;

  1. A minimal grasp of electronics,
  2. Knowledge of Ardunio 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')

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 tactile spst button
  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 4 : https://www.instructables.com/id/Pimping-Your-First...

To Part 2 : https://www.instructables.com/id/Setting-Up-an-MQTT...

Step 1: Circuit Overview

The simple circuit layout and circuit diagram are pictured above.

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

By way of Input to pull the voltage level on GPIO0 low a button is connected to ground at the junction of R4 (10K pull up) and GPIO0. Consequently the logic level on this input pin is opposite to button press state and must be inverted in software when the pin is read. ie. 0v = Button Pressed. VCC = Button Released.

Output is signalled by turning on the led by sending pin GPIO2 low to sink current. Again opposite to logic state.

.

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

Step 2: Software Overview

Preamble

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

PubSubClient.h

Bounce2.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:95:9a,ESP8266Proto3.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.

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:95:9a/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/Button1Status : When notification is received of this topic the device will respond by publishing the current button status via a /WiFiDevice/Button1Command topic 'Pressed' or 'Released'. In this way the status of the button can be checked at any given time. Note also if the button state changes independently, via a button press then the device will automatically publish a /WiFiDevice/Button1Command topic.
  4. /WiFiDevice/Led1Command : 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/Led1Confirm topic 'On' or 'Off'

Once completed the software now drops into a loop monitoring WiFi and Broker connections along with any changes in button state or new publications.

Step 3: Testing Your IoT Device

To test our new 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 second picture depicts the subscription/publication message flows and the Video shows the respective publications and subscriptions being exercised.

Step 4: References Used

I used the following sources to put this Instructable together;

PubSubClient.h

Bounce2.h

ESP8266WiFi.h

ESP8266 Community

http://esp8266.com

ESP8266 Arduino Core

http://esp8266.github.io/Arduino/versions/2.2.0-rc1

Finally, I drew inspiration from a you tube video by a youtuber named ItKindaWorks here;

https://www.youtube.com/channel/UCOnMlI1Si4i0O2Wkz-XReww

Comments