Introduction: IoT Mains Controller. Part 9 : IoT, Home Automation

Picture of IoT Mains Controller. Part 9 : IoT, Home Automation

Disclaimer

READ THIS FIRST

This Instructable details a project which uses mains power (in this instance UK 240VAC RMS), whilst every care has been taken to use safe practice and good design principles there is always a risk of potentially lethal electrical shock when working with these supply voltages and for which the author cannot accept any liability if personal injury or damage to property occurs when following its content. Consequently, you make this project at your own risk.

Preamble

This article, the 9th in a series on home automation, documents how to create and integrate a Sonoff 10A IoT mains controller into an existing home automation system including all the necessary software functionality to enable the successful deployment in a domestic environment.

Introduction

As mentioned above this Instructable details how to make and integrate an IoT mains controller using the Sonoff 10A from iTead. The device itself is quoted as being rated for 10amps @ 90~250VAC, however this implementation de-rates it to that of 5amps via the fused plug providing domestic UK main supply of 240VAC RMS.

The design methodology integrates seamlessly into the MQTT/OpenHAB based IoT network detailed in this series on home automation building on reused code taken from here. It can also handle the loss of any IoT network element and is fully capable of being operated standalone. Whilst in standalone mode, control of the device is achieved by simply pressing the control button on the top of the enclosure, which toggles the onward supply output.

To enable this local control of the Sonoff device, GPIO14 is brought out of the case and is used as a trigger input. To ensure safety, this input is fed via an opto-coupler circuit and housed in a plastic enclosure such that at no time is the operator exposed to mains supply voltages.

Finally, the prose also outlines how to re-programme the ESP8266 device in the Sonoff 10A using the Arduino IDE and gives full circuit details of a device which can be used to reliably programme target code.

What parts do I need?

Sonoff Mains Controller

  1. 1 off Sonoff 10A here
  2. 1 off 7805L 5v voltage regulator here
  3. 1 off 240/6VAC 1.5VA Transformer here
  4. 2 off 0.1 ceramic capacitors here
  5. 1 off 1000uF @25v Electrolytic Capacitor here
  6. 1 off Bridge rectifier 2W01 here
  7. 2 off 4K7 resistors here
  8. 1 off 330R resistor here
  9. 1 off SPST button here
  10. 1 off Mulitcomp BM12W ABS Enclosure here
  11. 1 off TIL111 opto-coupler here
  12. 1 off 3-Way terminal block here
  13. 1 off 2-Way coded molex connector here/here
  14. 1 off 3-Way coded molex connector here/here
  15. 1 off 5-Way coded molex connector here/here
  16. 1 off 5-Way molex pins here
  17. 1 off Winbond SPI Flash (W25Q32FVSIG) here
  18. 1 off 20mm fuse holder + cap here
  19. 1 off 20mm fast blow 500mA fuse here
  20. 2 off Polyamide cable glands here
  21. 1 off UK Mains plug (BS1363/A) here
  22. 1 off UK Main socket (BS1363/A) here
  23. 7 off M3 16mm CS nylon screws, (inc. 10 off nuts) here/here
  24. 2 off Zip ties here
  25. 1 off veroboard (0.1" pitch) here
  26. 1 off Various lengths 22swg tinned copper wire here
  27. 1 off 3M White UK mains cable here
  28. 10 off Molex socket crimps here

Sonoff Programmer

  1. 1 off LD33CV 3v3 voltage regulator here
  2. 1 off TO-220 heatsink here
  3. 1 off Heatsink paste here
  4. 1 off 10uF @16v Electrolytic capacitor here
  5. 1 off 0.1 ceramic capacitor here
  6. 1 off SPDT button here
  7. 1 off 4K7 resistor here
  8. 1 off 2-Way coded molex connector here/here
  9. 1 off 3-Way coded molex connector here/here
  10. 5 off Molex socket crimps here
  11. 1 off 6-Way molex socket here
  12. 1 off SPST button here
  13. 1 off 2.1mm PSU socket here
  14. 1 off veroboard (0.1" pitch) here
  15. 1 off USB to serial adapter (FTDI) here

.

What software do I need?

  1. Arduino IDE 1.6.9 here
  2. Arduino IDE configured to programme the ESP8266. See here; Setting Up the Arduino IDE to Program the ESP8266-01

What tools do I need?

  1. Soldering Iron,
  2. Drill & various bits (including stepped hole cutter for cable glands and control button),
  3. Screwdrivers (various),
  4. Adjustable spanners (two off, jaw width >25mm, for cable glands),
  5. Files (various),
  6. Sturdy vice,
  7. Heatgun,
  8. DMM (preferably CAT IV).

What skills do I need?

  1. A good grasp of electronics and home electrical safety/design/wiring etc.,
  2. Knowledge of Arduino and it's IDE,
  3. Good fabrication skills (soldering, filing, drilling etc.),
  4. Some Patience,
  5. Some understanding of your home network.

Topics covered

  • Introduction
  • Circuit Overview
  • Sonoff RetroMods
  • Construction and Assembly Details
  • Sonoff Programming Adapter
  • Software System Overview
  • Software Overview
  • OpenHAB Configuration
  • Testing Your IoT Device
  • Conclusion
  • References Used

.

Series Links

To Part 8 : WiFi IoT Temperature and Humidity Sensor. Part : 8 IoT, Home Automation

Step 1: Circuit Overview

Picture of Circuit Overview

Overview

As mentioned in the introduction above, in order to be able to switch the mains controller on and off locally an input to the Sonoff's on-board ESP8266 was required. Introducing such an external input requires the Sonoff ABS enclosure be breached and therefore creates a potential shock hazard. To overcome this I used optical isolation such that there is no possibility of exposure to mains electricity outside of the mains controller system enclosure.

What follows is a description of the opto-isolation circuitry (in pic 1 above).

Circuit Details

The opto-isolation circuit receives it's supply direct from the mains applied to the unit. The 240VAC RMS is applied to stepdown/isolation transformer TR1 via J1 a pheonix contact MKDSN2,5/3-5.08 polyamide 3-Way terminal rated at 16A at 400V able to carry a cable of 2.5mm(sq) CSA and F1 a 500mA 20mm fast blow fuse. The 6VAC available on the secondary windings of TR1 is full wave rectified by diode bridge B1.

This full wave rectified output is then stabilised and regulated by C1, C2 C3, R3 and IC1 a 7805L series shunt regulator, giving a good, clean 5v supply rail.

The 5v rail is then used to control the input to OK1 a TIL111 opto-isolator via an externally mounted white SPST button connected across J3. The output of the TIL111 is coupled to the Sonoff GPIO14 input via R2 a 4K7 pull up resistor. Thus better than 340V of isolation is achieved (ie Peak voltage = (240VAC*sqroot(2))).

Step 2: Sonoff RetroMods

Picture of Sonoff RetroMods

In order to integrate the Sonoff 10A device it is necessary to make some retrospective modifications.

The first is to add a 5-way 0.1" pitch molex connector as shown in pic 1 above. This allows access to GPIO14 on the Sonoff once the protective cover has been replaced as in pics 2 and 3 above.

Though not shown above I also brought out the serial TX/RX lines to allow for in-situ programming (refer to cable harness SK1..3 in Step 1 above).

The second modification is to increase the size of the SPI Flash device from the default 1MByte to 4MBytes, this is to allow sufficient room for the IoT web server files to be held in SPIFFS.

I purchased the SMD SPI flash device (W25Q32FVSIG) from Ebay here

To replace the flash I temporarily removed the Sonoff LED as in picture 4 to give better access to the SMD device. To de-solder the flash I used a heat gun as shown in pic 5 above. Then re-soldered both the 4MByte Flash and LED respectively (pic 6).

Step 3: Construction and Assembly Details

Picture of Construction and Assembly Details

I enclosed the mains controller in a Mulitcomp BM12W ABS Box (Pic 1 above). This enclosure has isolated brass M3 inserts allowing for multi-access to the unit without compromising the fastening threads such that the internal fuse can be replaced if necessary or internal inspection can be made over time (the same cannot be said for the Sonoff device, which is effectively a one time only close using self tappers).

Primary strain relief for the mains carrying supply cable was achieved via an M16 Nylon/Polyamide 6/6 white cable gland supporting a cable OD Min/Max 5mm/10mm.

Secondary strain relief was via a single zip tie placed on the cable should excessive strain be applied and the cable gland fail, the zip tie will keep the cable in place.

To fit the cable glands and provision sufficient space to mount the Sonoff and opto-isolation electronics I stripped the internal PCB mounting ribs as shown above (Pic 2).

All electronics were securely mounted via M3 nylon CS screws to ensure isolation with the exterior of the enclosure was maintained. The opto-isolation electronics being mounted with 5 fixing points to ensure sound mechanical strength should the unit be dropped, thus preventing the mass of the isolation transformer from breaking the veroboard circuit.

Supply to the unit was achieved via UK Standard colour coded white 3 core PVC insulated mains multi-strand (32/0.2mm sq) cable 1mm(sq) CSA. with an OD of 7.2mm capable of carrying 10A.

The unit was connected to UK mains supply (240VAC RMS) via a standard 3 pin safety plug (BS 1363/A) approved. The plug was fused at 5A.

All mains supply cables to the opto-isolation circuit were connected via pheonix contact MKDSN2,5/3-5.08 polyamide terminals rated at 16A at 400V able to carry a cable of 2.5mm(sq) CSA, thus provisioning ample capacity for two cables in each position.

No mains cables were tinned, just twisted to prevent splaying of the cores before insertion into the connector block. Tinning mains cables is a dangerous practice as the solder 'relaxes' over time eventually causing the cable to become lose in the connector block.

.

Note :

  • OD = Outside diameter.
  • VAC = Volts Alternating Current
  • RMS = Root Mean Square
  • CSA = Cross Sectional Area
  • CS = Counter Sunk

Step 4: Sonoff Programming Adapter

Picture of Sonoff Programming Adapter

There are two aspects to be considered when re-programming the Sonoff 10A via the Arduino IDE;

  1. Configuring your Arduino IDE to programme the ESP8266,
  2. The act of programming the hardware itself.

Configuring your Arduino IDE to programme the ESP8266

To configure your Ardino IDE follow the instructions here Setting Up the Arduino IDE to Program the ESP8266-01

Programming the hardware

This is a multi-step process as in all cases with the ESP8266. Here, the Sonoff power is applied to the board via an external stabilised 3v3 DC supply and NOT from mains supply. A USB to serial device will be required to send and receive data to and from the Sonoff. Connect TX and RX as shown in Pic 2 & 4.

Programming Steps (general)

  1. First ensure there is no external mains power applied to the Sonoff,
  2. Press and hold the button on the Sonoff device. (pic 1 above, marked re-flash button),
  3. Apply external DC 3v3 supply to pin 1. (pic 2 above),
  4. Release the Sonoff button,
  5. The device can now be reprogrammed in the usual way via the Arduino IDE.

.

To make things a little easier I created the programming device above (pics 3 and 4) which interfaced to the Sonoff via the cable harness SK1...3 (as described in this Instructable Step 1.). This allowed for easier programming of the ESP8266. It also provided a means of testing GPIO14 as an input through the use of R1 a 4K7 pull up resistor and button S1.

Using the programming device above (Pics 3 & 4) Programming Steps are,

  1. Press and hold the re-flash button on the Sonoff,
  2. Pulse the 3v3 supply by momentarily pressing S2,
  3. Release the re-flash button,
  4. The device can now be programmed.

.

NOTE - WARNING

Under NO circumstances should power be supplied via Mains during the Sonoff re-programming activity.

Step 5: Software System Overview

Picture of Software System Overview

This IoT Mains Controller device for the most part contains the same six key software components as in Instructable WiFi IoT Temperature and Humidity Sensor. Part : 8 IoT, Home Automation and shown in pic 1 above, with some customisation.

SPIFFS

This is the (upgraded to 4MBytes) on-board SPI Flash Filing System and is used to hold the following information (see pic 2 above);

  • Icons and 'Mains Controller Configuration Home Page' html: Served up by the IoT device when it is unable to connect to your IoT WiFi network (usually due to incorrect security information) and provides the user with a means of remotely configuring the mains controller without the need to re-programme or upload new SPIFFS content.
  • Security Information: This holds the information used at power up by the IoT device to connect to your IoT WiFi network and MQTT Broker. Information submitted via the 'Mains Controller Configuration Home Page' is written to this file ('secvals.txt').

Note : To initially set up the device see here for full details on how to use SPIFFS with the Arduino IDE.

.

mDNS Server

This functionality is invoked when the IoT device has failed to connect to your WiFi network as a WiFi station and instead has become a WiFi access point something akin to a domestic WiFi router. In the case of such a router you would typically connect to it by entering the IP Address of something like 192.168.1.1 (usually printed on a label affixed to the box) directly into your browser URL bar whereupon you would receive a login page to enter the username and password to allow you to configure the device. For the ESP8266 in AP mode (Access Point mode) the device defaults to the IP address 192.168.4.1, however with the mDNS server running you only have to enter the human friendly name 'MAINSCON.local' into the browser URL bar to see the 'Mains Controller Configuration Home Page'.

.

MQTT Client

The MQTT client provides all the necessary functionality to; connect to your IoT network MQTT broker, subscribe to the topics of your choice and publish payloads to a given topic. In short it provisions IoT core functionality.

.

HTTP Web Server

As mentioned above, if the IoT device is unable to connect to the WiFi network whose SSID, P/W etc. is defined in the Security Information file held in SPIFFS the device will become an Access Point. Once connected to the WiFi network provided by the Access Point, the presence of an HTTP Web Server allows you to directly connect to the device and change it's configuration via the use of an HTTP Web Browser it's purpose being to serve up the 'Mains Controller Configuration Home Page' web page which is also held in SPIFFS.

.

WiFi Station

This functionality gives the IoT device the capability to connect to a domestic WiFi network using the parameters in the Security Information file, without this your IoT device will not be able to subscribe/publish to the MQTT Broker

.

WiFi Access Point

The ability to become a WiFi Access Point is a means by which the IoT device allows you to connect to it and make configuration changes via a WiFi station and a browser (such as Safari on the Apple iPad). This access point broadcasts an SSID = "MAINSCON" + the last 6 digits of the MAC address of the IoT device. The password for this closed network is imaginatively named 'PASSWORD' .

Step 6: Software Overview

Picture of Software Overview

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

PubSubClient.h

Bounce2.h

Code Overview

The software makes use of the state-machine as shown in pic 1 above (full copy of source given below). There are 5 main states as follows;

  • INIT
    • This initialisation state is the first state entered after power up.
  • NOCONFIG
    • This state is entered if after power up an invalid or missing secvals.txt file is detected
  • PENDING NW
    • This state is transitory, entered whilst there exists no WiFi network connection
  • PENDING MQTT
    • This state is transitory, entered after a WiFi network connection has been made and whilst there exists no connection to an MQTT broker on that network.
  • ACTIVE
    • This is the normal operational state entered once both a WiFi network connection and an MQTT Broker connection has been established. It is during this state the Mains Controller will publish to the MQTT Broker and receive commands via subscribed topics.

The events controlling transitions between states are described in pic 1 above. Transitions between states is also governed by the following SecVals parameters;

  • 1st MQTT Broker IP Address. In dotted decimal form AAA.BBB.CCC.DDD
  • 2nd MQTT Broker Port. In Integer form.
  • 3rd MQTT Broker connection attempts to make before switching from STA mode to AP mode. In Integer form.
  • 4th WiFi Network SSID. In free form text.
  • 5th WiFi Network Password. In free form text.

As mentioned above if the IoT device is unable to connect as a WiFi Station to the WiFi network who's SSID and P/W is defined in secvals.txt held in SPIFFS the device will become an Access Point. Once connected to this access point it will serve up the 'Mains Controller Configuration Home Page' as shown above in Pic 2 (by entering either 'MAINSCON.local' or 192.168.4.1 into your browsers URL address bar). This home page allows the reconfiguration of the mains controller via an HTTP browser.

MQTT Topic naming convention

Outlined in pic 3 above is the naming convention used for the MQTT topics and is consistent with the pattern used in my earlier Instructable (here Step 5).

MQTT Topics used by this IoT device

For clarity I have documented (pic 4) the topics and associated message sequences this device publishes/subscribes to. The picture also depicts the interaction with the white control button on the exterior of the enclosure (though ironically the button is shown in red).

Remote Config Access whilst in the ACTIVE state

Once connected to the MQTT Broker it is possible to remotely reconfigure the security parameters for the device via MQTT topic publications. The associated file secvals.txt has write only access exposed.

.

User debug

During the boot sequence the Sonoff device led gives the following debug feedback, though it should be noted, to view this you will need to remove the cover and expose the circuitry so it is only advisable to do so whilst developing your code and powering the device with a 3v3 supply;

  • 1 Short flash : No Config file located in SPIFFS (secvals.txt),
  • 2 Short flashes : IoT device is attempting to connect to WiFi network,
  • Continuous illumination : Sonoff IoT device is attempting to connect to MQTT Broker,
  • Off : Device is active and connected to MQTT Broker.

.

Note 1 : The 'Mains Controller Configuration Home Page' does not use secure sockets and therefore relies on your network being secure.

Note 2 : In order to programme multiple IoT devices the MQTT string will require editing prior to downloading to each device. This is because the id number of the mains controller has been embedded into the MQTT topic string. ie. in the published software I chose the value of 100 : 'WFD/MainsCont/100/Relay/Command/1' and for my 2 devices they are numbered 1 & 2 respectively.

  • 'WFD/MainsCont/1/Relay/Command/1'
  • 'WFD/MainsCont/2/Relay/Command/1'

Note 3 : For completeness when in the ACTIVE state the IoT software allows for the control of the Sonoff LED and publishing of the status of the re-flash button. Though these are only of value during the debug process as neither are exposed to the user during normal operation.

Step 7: OpenHAB Configuration

Picture of OpenHAB Configuration

For testing purposes I decided to notionally deploy the two mains controllers in the 'Living Room' of my house. This OpenHAB page can be reached via the main site page as in pic 1.

I modified the OpenHAB .sitemap configuration given in my earlier Instructable (here) and added individual entries for 'Mains Controller 1' and 'Mains Controller 2' (pic 2 above) . I also added entries (Living Room Mains Cont. 1 & 2) to display RSSI Trends measured at the receiver of the two new IoT devices (pic 3).

Finally, I added in entries to the .rules and .items files to allow dynamic state synchronisation of the Sonoff and the updating/animating of my poor attempt at a switch graphic (the switch closes when active and opens when inactive). Pic 2 gives an example of MC1 active and MC2 inactive.

.

Note 1 : If you're not sure how to use OpenHAB see here 'Setting Up and Configuring OpenHAB. Part 6 : IoT, Home Automation'

Note 2 : A copy of the modified sitemap, rules and items files, Icons etc. is given in the zip file below.

Note 3 : RSSI = Received Signal Strength Indication. This is a measure of how well the IoT device can see your WiFi network.

Step 8: Testing Your IoT Device

Picture of Testing Your IoT Device

As described in Instructable WiFi IoT Temperature and Humidity Sensor. Part : 8 IoT, Home Automation Step 7, initial testing of the IoT device was executed over an MQTT connection via MQTT Spy (as in system block diagram pic 1 above), monitoring led output, button inputs (both Sonoff re-flash button and the white external button) and debug traffic on the serial interface. This allowed me to exercise all of the available subscribed topics and check the published responses. Although again, this was carried manually and was time consuming, though it did enable 100% coverage of messages/topic publications.

As the main software state machine (Step 6 above) was inherited from the earlier Instructable (Part : 8) other than sanity checking the software could connect to the WiFi N/W and MQTT Broker it was assumed this was functioning correctly.

Full system level testing was then completed using the mains controller and the IoT infrastructure (again pic 1) this time using OpenHAB to control interaction with the IoT device. The IoT hardware and dummy load set up can be seen in pic 2 above.

The video gives complete details of system tests and clearly shows the synchronisation being maintained between OpenHAB devices (PC/Chrome and iPad/OpenHAB APP) in real time. It also shows live messaging to the Mains Controllers via MQTTSpy (see here for further details Setting Up an MQTT Broker. Part 2 : IoT, Home Automation) and OpenHAB tailed system log from raspberry pi server over a PuTTY SSH connection (see here for further details Setting Up and Configuring OpenHAB. Part 6 : IoT, Home Automation).

.

Note : Debug traffic was compiled out for the final software release.

Step 9: Conclusion

General

The project was relatively easy to complete and worked well. The embedded software was simple to produce, being a cut down version of the code used for the Temperature and Humidity Sensors of Part 8 in this series.

I initially intended to acquire only white component parts purely for their aesthetic quality. I achieved this in all but the control button, try as I could, I was unable to source a good/cheap completely white button.

Sonoff 10A device

I've listed out below what I felt were reasonable Pros and Cons of the Sonoff device

Pros

  • Cheap.
  • Good community support.
  • Can re-programme via the Arduino IDE.

Cons

  • Flimsy enclosure.
  • Minimal I/O (brought out to usable connectors).
  • It runs hot in it's quiescent state.
  • Only has 1MByte of on-board SPI flash.
  • Is a PITA to reprogram once wired in place.
  • When integrating new code into the Sonoff testing the relay closure was problematic given the relay is 5v and the supply applied to the Sonoff for programming is 3v3. Relay activation is only just perceptible to the ear.

Concerns

  • It doesn't switch the Neutral line. Uses an SPST relay.
  • Isn't fused.
  • Poor cable strain relief.
  • The PCB is not secured within the Sonoff enclosure.

Comment on engineering design

Given this IoT device was to be used to switch live UK mains (240VAC RMS) I followed both good mechanical and electrical design practice and ensured the risk of shock was minimised by not exposing any electrically conductive materials, over specifying all components, de-rating output load, applying fuse protection to both the Mains Controller and Opto-Coupled Sub-System, inclusion of good unbroken earthing, and using optical/galvanic isolation.

Possible Improvement

With hindsight it would have been useful to include a visual indication the Mains Controller output was active (LED or Neon). Though not an issue in everyday use, given it's standard practice to isolate load from supply before any maintenance is carried out, or a simple press of the local control button will toggle output in the case where a lamp may illuminate when plugged in.

.

Final note

If you wish to see two very poor examples of dealing with mains electricity check out the links below. Their Darwin awards will be in the post very soon, I'm quite sure;

Step 10: References Used

I used the following sources to put this Instructable together;

PubSubClient.h

Bounce2.h

.

SPIFFS

Upgrading Sonoff flash

Sonoff Circuit Diagram

USB UART Module (aka. FTDI)

Darwin awards (light relief)

TIL111 Opto-isolator datasheet

.

Comments

sny2ksa (author)2017-08-29

Hi,

I try to build a basic project using espeasy + RPI3 with openhabian 2,1 and mosquito. everything is going fine but relay not working, I can turn on and off relay using espeasy commands using web browser all screens shots are attache can some one tell me where i am wrong.

additionally i want to add timer on this relay with save the relay state during power failure