![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/9c2d168a13de1fb7cb7d7880ab5d2faf.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/b70c12455ee71cbe30b3da546221dd8d.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/9a9beb9c08e9cabd0bca3545c63a0ea3.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/a736f1135251ac31f5b888a43200f0e6.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/60118d52982b046fb1a6d3fa9b2679eb.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/fc23da943822134f57fdbe7240c0e046.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/0bd21bc961597a390d2dd5f403a975f2.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/071ac8ce31bf282fc20fae841ec05ca1.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/40a97f1570afc410d0d0238bb3c28ef5.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/48d3f3feba752b0a961bc931c890592b.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/6eedad24c57ba713bd07b4c9dcf73e99.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/da4c9dc8dc7f84a13fbd0d961f11fed2.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/9e4690ea70a02a56adb3ee1c0bde5d9e.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/dcde070b50310febe069da4ac8f98dbc.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/3e45901449d61cfc1972e8abf33b631e.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/10675dac5a0605814ddb79519ed3fa4e.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/72766077e2dd7bce847b02375ace7232.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/28c3cd85dca1d25aa35c660132327b94.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/b9096c79b2175383d9d6ce484c5ce15d.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/6aefa6523c19ab1346c4349f1745a9e8.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/b02137896430975bdb9859f4f9cdeb33.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/b2c8923be93c0cb3485dcfc10060be26.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/13d61a6b8f902ae2ba4717e656c63fbf.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/8f2668c4350f3dfa2e1b6ec59f40b1a1.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/4a715f15d7bd9e671b96c7b058e3cc3f.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/7e7e3ad381bf2a12e56f8306e01b8611.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/41402415503b135eaa7ce7445f6aa5c9.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/33172330f56807eb6f6524212a733ed5.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/f63d1c140ee6ce565e0baa5cfd5c08d2.jpeg)
![](https://assets.isu.pub/document-structure/230106123534-032973511b3d433655599f7e628f9780/v1/243f4015d689a9ba939ca437c008130b.jpeg)
● This is an Elektor Publication. Elektor is the media brand of Elektor International Media B.V.
PO Box 11, NL-6114-ZG Susteren, The Netherlands Phone: +31 46 4389444
● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication, without the written permission of the copyright holder except in accordance with the provisions of the Copyright Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licencing Agency Ltd., 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder's permission to reproduce any part of the publication should be addressed to the publishers.
● Declaration
The Author and Publisher have used their best efforts in ensuring the correctness of the information contained in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident, or any other cause.
All the programs given in the book are Copyright of the Author and Elektor International Media. These programs may only be used for educational purposes. Written permission from the Author or Elektor must be obtained before any of these programs can be used for commercial purposes.
● British Library Cataloguing in Publication Data
A catalogue record for this book is available from the British Library
● ISBN 978-3-89576-536-0 Print ISBN 978-3-89576-537-7 eBook
● © Copyright 2022: Elektor International Media B.V. Editor: Jan Buiting, MA Prepress Production: D-Vision, Julian van den Berg
Elektor is part of EIM, the world's leading source of essential technical information and electronics products for pro engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers high-quality content - via a variety of media channels (including magazines, video, digital media, and social media) in several languages - relating to electronics design and DIY electronics. www.elektormagazine.com
Contents
Chapter 1 • Introduction ............................................. 15
1.1. Why this book? ............................................... 15
1.2. Who is this book for? 15 1.3. How to use this book ........................................... 16 1.4. The organization of the book ...................................... 17 1.4.1. Chapter 2: Components ........................................ 17 1.4.2. Chapter 3: Arduino IDE installation ................................ 17 1.4.3. Chapter 4: emoncms Installation 18 1.4.4. Chapter 5: MQTT (mosquitto) Installation on a Raspberry Pi ............... 18 1.4.5. Chapter 6: OpenSCAD 18 1.4.6. Chapter 7: ESP8266 Security .................................... 18 1.4.7. Chapter 8: ESP8266 Watchdogs .................................. 18 1.4.8. Chapter 9: ESP8266 Over The Air – Update/Programming (OTA) 18 1.4.9. Chapter 10: ESP8266 WiFi Antenna ................................ 18 1.4.10. Chapter 11: ESP8266 Housing 19 1.4.11. Chapter 12: VPN Gateway ...................................... 19 1.4.12. Chapter 13: Software/Firmware design............................. 19 1.4.13. Chapter 14: IKEA Hack ........................................ 19 1.4.14. Chapter 15: Door Lock Monitor .................................. 19 1.4.15. Chapter 16: Energy Sensor 19 1.4.16. Chapter 17: Refrigerator Control ................................. 19 1.4.17. Chapter 18: Greenhouse Thermostat 20 1.4.18. Chapter 19: 230-VAC Relay Control ............................... 20 1.4.19. Chapter 20: Standalone Temperature Sensor ........................ 20 1.4.20. Chapter 21: Temperature, Pressure, and Humidity Sensor 20 1.4.21. Chapter 22: IKEA Trådfri Controller ............................... 20 1.4.22. Chapter 23: Troubleshooting 20 1.4.23. Chapter 24: PCB Layout ....................................... 20 1.5. Abbreviations ................................................. 20 1.6. References ................................................... 21
1.7. Feedback .................................................... 21
Chapter 2 • Components ............................................. 22
2.1. ESP8266 22
2.1.1. Wemos/Lolin, general notes ..................................... 23 2.1.2. Sleep Mode 24
2.2. Raspberry Pi ................................................. 25 2.2.1. Raspberry Pi configuration ...................................... 25
2.3. Neopixel Jewel 7............................................... 31 2.4. IKEA Grönö lamp .............................................. 32 2.5. SSR 33 2.6. Maxim DS18B20 ............................................... 34
2.7. QRE1113 (Analog) 34 2.8. Silabs MGM210L ............................................... 35 2.9. I/O devices .................................................. 36 2.9.1. SHT30 Shield ............................................... 36 2.9.2. HP303B Shield............................................... 37
Chapter 3 • Arduino IDE Installation .................................... 38
3.1. Standard Arduino IDE install ...................................... 38 3.2. ESP8266 support 38 3.3. SPI Flash File (SPIFFS) system install ................................ 41 3.4. Alternatives to the SPI Flash File ................................... 45 3.5. Additional libraries 46
Chapter 4 • emoncms Installation ...................................... 47
4.1 Update and upgrade the Raspberry Pi 48 4.2. Install dependencies ............................................ 49 4.2.1. MariaDB & apache ............................................ 49 4.2.2. phpMyAdmin ................................................ 51 4.2.3. PECL ..................................................... 54 4.3. emoncms code 58 4.3.1. Configure emoncms ........................................... 59 4.4. emoncms moduless 65 4.4.1. Dashboard ................................................. 65
4.4.2. Graph ..................................................... 67
4.4.3. Apps ..................................................... 69
4.5. Documentation 70
Chapter 5 • MQTT (mosquitto) Installation on a Raspberry Pi ................ 74
5.1. Installation of the Mosquitto broker 76
5.2. Installation of the Mosquitto client (RPI) .............................. 80
5.3. Verification of the Mosquitto installation .............................. 81
5.4. Installation of the Mosquitto client (Windows PC) ........................ 83
Chapter 6 • OpenSCAD ............................................... 86
Chapter 7 • ESP8266 Security ......................................... 88
7.1. VPN ....................................................... 88
Chapter 8 • ESP8266 Watchdogs ....................................... 89
8.1. Watchdogs in general ........................................... 89 8.2. Loop watchdog ................................................ 89 8.2.1.Boot reason ................................................. 93
Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA) ............. 96
9.1. web browser OTA 96 9.1.1. Configure the Arduino IDE ...................................... 97 9.1.2. Prepare already working software for OTA update. 97 9.1.3. OTA update examples .......................................... 98 9.1.3.1. Firmware build ............................................. 99 9.1.3.2. Firmware update 100 9.1.3.3. Filesystem build ........................................... 104 9.1.3.4. Filesystem update 106 9.1.3.5. OTA update with password protection ............................ 107 9.1.4. CLI OTA .................................................. 108
Chapter 10 • ESP8266 WiFi Antenna ................................... 110
10.1. Dimensions for external antennas ................................. 111
10.2. Ready-made antennas 113
10.3. Salvaged antennas ........................................... 113 10.4. DIY antenna 116 10.5. DIY Yagi antenna (2.4 GHz) ..................................... 119
10.5.1. Mechanics ................................................ 122 10.6. Antenna measurements ........................................ 126 10.7. Further studying 127
Chapter 11 • ESP8266 Housing ....................................... 128
11.1. LOLIN D1 mini standard housing 128 11.2. LOLIN D1 mini pro-standard housing ............................... 129 11.3. LOLIN D1 mini door-lock housing ................................. 131 11.4. LOLIN D1 mini refrigerator control housing .......................... 132 11.5. LOLIN D1 mini energy sensor housing .............................. 133 11.6. LOLIN D1 mini IKEA Trådfri controller housing 133
Chapter 12 • VPN Gateway .......................................... 137
12.1. Firewall configuration 138 12.2. Raspberry Pi configuration ..................................... 140 12.2.1. Basic configuration .......................................... 140 12.2.2. OpenVPN installation ........................................ 143 12.2.3. OpenVPN configuration ...................................... 145 12.2.3.1. OpenVPN Authorization file 145 12.2.3.2. Start at boot time ......................................... 146 12.2.3.3. Configuration of the VPN gateway (fun with IP tables!) 147 12.3. Use of the VPN gateway ....................................... 151 12.3.1. Improvements ............................................. 151
Chapter 13 • Software/firmware Design ................................ 152
13.1. Use of EEPROM .............................................. 153 13.2. Utility functions 153 13.2.1. Installation of the utility functions ............................... 153 13.2.2. formatBytes() ............................................. 155 13.2.3. printFloat() .............................................. 155 13.2.4 isNumeric() ............................................... 155 13.2.5. errorState(int state) 156 13.2.6. keypressWait() ............................................ 156 13.2.7. CACHE_RAM_ATTR lwdtcb() 156 13.2.8. lwdtFeedInit() ............................................. 156
13.2.9. lwdtFeed() ............................................... 156 13.2.10. getBootDevice() 157 13.2.11. startWiFi() ............................................... 157 13.2.12. showWiFiStatus() .......................................... 157 13.2.13. doWifiConnect() 158 13.2.14. MQTT_connect()........................................... 159
Chapter 14 • IKEA Hack ............................................. 160
14.1. Schematic ................................................. 160 14.2. Mechanical assembly .......................................... 160 14.2.1. Grönö lamp .............................................. 161 14.2.2. ESP8266 housing ........................................... 165 14.3. Software 166 14.3.1. Additional libraries .......................................... 166 14.3.2. Features 167 14.3.3. Design .................................................. 167 14.3.3.1. web interface ............................................ 167 14.3.4. Wemos firmware 170 14.3.4.1. setup() ................................................ 170 14.3.4.2. loop() 171 14.3.5. HTTP POST requests ......................................... 171 14.3.5.1. The HTTP POST request code in general .......................... 172 14.3.5.2. HTTP POST request commands ................................ 172 14.3.6. Implementation of the illumination............................... 174 14.3.6.1. Rainbow 174 14.3.6.2. Rainbowcycle ............................................ 175 14.3.6.3. Static 175 14.3.6.3. Fade .................................................. 176 14.4. Operation ................................................. 178 14.4.1. Daily use................................................. 178 14.4.1.1. Power-up ............................................... 178 14.4.2. Configuration 178
Chapter 15 • Door Lock Monitor ....................................... 179
15.1. Schematic ................................................. 179 15.2. Mechanical assembly .......................................... 180 15.3. Software 184 15.3.1. Emoncms wemos firmware .................................... 184 15.3.1.1. setup() 184 15.3.1.2. loop() ................................................. 185 15.3.2. Emoncms dashboard ........................................ 186 15.3.3. MQTT wemos firmware ....................................... 193 15.3.3.1. MQTT client publisher ...................................... 195 15.3.3.2. MQTT client subscriber 197 15.3.4. MQTT broker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Chapter 16 • Energy Sensor
.......................................... 200
16.1. Schematic ................................................. 201 16.2. Mechanical assembly .......................................... 203 16.3. Software .................................................. 204 16.3.1. Wemos firmware ........................................... 204 16.3.1.1. setup() 204 16.3.1.2. loop() ................................................. 206 16.3.1.3. Functions 206 16.4. Operation ................................................. 208 16.5. Dashboard ................................................. 209
Chapter 17 • Refrigerator Control
..................................... 224
17.1. Schematic ................................................. 226 17.1.1. SSR 228 17.1.2. DS18B20 ................................................ 228 17.2. Mechanical assembly .......................................... 231 17.2.1. No modifications to the refrigerator at all .......................... 231 17.2.2. Modifying the refrigerator ..................................... 232 17.3. Software 240 17.3.1. Additional libraries .......................................... 240 17.3.2. Wemos firmware 240 17.3.2.1 setup() ................................................. 240
17.3.2.2. loop() ................................................. 242 17.3.3. Temperature regulation ...................................... 242 17.3.4. Watchdog functionality 244 17.3.5. web interface ............................................. 244 17.3.5.1. Set temperature and hysteresis 245 17.3.5.2. Open temperature logger .................................... 245 17.3.5.3. Save configuration ........................................ 247 17.3.6. SPI Flash File System ....................................... 247 17.3.7. Compile ................................................. 247 17.4. Operation 248 17.5. Dashboard ................................................. 248
Chapter 18 • Greenhouse Thermostat .................................. 260
18.1. Schematic ................................................. 264 18.1.1. SSR .................................................... 265 18.1.2. DS18B20 ................................................ 265 18.2. Mechanical assembly .......................................... 266 18.3. Software 270 18.3.1. Additional libraries .......................................... 270 18.3.2. Wemos firmware 271 18.3.2.1. setup() ................................................ 272 18.3.2.2. loop() ................................................. 272 18.3.3. Temperature regulation 272 18.3.4. Watchdog functionality ....................................... 274 18.3.5. web interface 274 18.3.5.1 Set temperature and hysteresis ................................ 275 18.3.5.2 Open temperature logger .................................... 276 18.3.5.3. Save configuration ........................................ 277 18.3.6. SPI Flash File System ....................................... 277 18.3.7. Compile 278 18.4. Operation ................................................. 278 18.5. Dashboard 278
Chapter 19 • 230-VAC Relay Control ................................... 292
19.1. Software .................................................. 292 19.1.1. Wemos firmware ........................................... 292 19.1.1.1.setup() 292 19.1.1.2. loop() ................................................. 292 19.1.2. Web interface 293 19.2. Hardware .................................................. 294 19.2.1. SSR .................................................... 294 19.2.2.1. Housing ................................................ 294 19.2.2. Relay ................................................... 294 19.2.2.1 Housing 295 19.2.3. Relay Shield .............................................. 295 19.2.3.1. Housing 296 19.2.4. Alternative 230-VAC relay On/Off construction ...................... 297
Chapter 20 • Standalone Temperature Sensor............................ 299 20.1. Housing ................................................... 300 20.2. Software .................................................. 301 20.2.1. Additional libraries 301 20.2.2. OTA-Enabled Software ....................................... 302 20.2.2.1. Wemos firmware 302 20.2.2.2. Dashboard .............................................. 303 20.2.3. Sleep-mode enabled software .................................. 304 20.2.3.1. Hardware sleep-mode differences and setup 304 20.2.3.2. Wemos firmware .......................................... 306 20.2.3.3. Dashboard 308
Chapter 21 • Temperature, Pressure, and Humidity Sensor ................. 309 21.1. Schematic ................................................. 309 21.2. Software .................................................. 310 21.2.1. Additional libraries .......................................... 311 21.2.1.1. HP303B 311 21.2.1.2. SHT3x ................................................ 311 21.2.2. Firmware 312 21.2.2.1. setup() ................................................ 312
21.2.2.2. loop() ................................................. 312 21.2.3. Web interface ............................................. 313 21.3. Dashboard 314
Chapter 22 • IKEA Trådfri Controller ................................... 318
22.1. Description of the concept 318 22.2. Getting to the IKEA trådfri modules ............................... 321 22.3. Schematic ................................................. 328 22.4. Mechanical assembly .......................................... 330 22.4.1. I2C Address jumpers ........................................ 331 22.4.2. Outside connectors 331 22.4.3. ZigBee power jumpers ....................................... 332 22.4.4. LED series resistors 332 22.4.5. ZigBee modules ............................................ 332 22.4.6. Miscellaneous ............................................. 334 22.5. Software .................................................. 335 22.5.1. Wemos firmware ........................................... 335 22.5.1.1. "Test-and-debug" setup() 335 22.5.1.2. "Test-and-debug" loop() ..................................... 336 22.5.1.3. Controller setup() 336 22.5.1.4. Controller loop() .......................................... 338 22.5.2. web interface ............................................. 338 22.5.3. Additional libraries 339 22.6. Operation ................................................. 340 22.6.1. Configuration of the controller (adding the ZigBee modules) 340 22.6.1.1. Adding ZigBee module with remote ............................. 341 22.6.1.2. Adding ZigBee module without remote........................... 345 22.6.2. Daily use of the IKEA Trådfri controller ............................ 347
Chapter 23 • Troubleshooting ........................................ 348
23.1. Hardware Issues 348 23.1.1. DS18B20 ................................................ 348 23.1.2. Red / Green LED 349 23.1.3. Compressor ............................................... 349
23.1.4. USB cables ............................................... 349 23.2. Network issues .............................................. 350 23.3. Wemos firmware 351 23.3.1. Reset ................................................... 351 23.4. emoncms 351 23.4.1. Graph installation........................................... 352 23.4.2. Input / Feeds.............................................. 352 23.5. IKEA ZigBee modules ......................................... 352 23.5.1. Software upload............................................ 352 23.5.2. Adding ZigBee modules. 352 23.5.3. Testing the IKEA Trådfri controller ............................... 353 23.5.4. Interrupts 353
Chapter 24 • PCB Layout ............................................ 354 24.1. Door lock monitor PCB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 24.2. Energy sensor .............................................. 357 24.3. Refrigerator regulator ......................................... 360 24.3.1. Regulator PCB 360 24.3.2. DS18B20 PCB ............................................. 362 24.4. Greenhouse thermostat 363 24.5. IKEA Trådfri controller ........................................ 363
Chapter 25 • Software Listing ........................................ 366 25.1. HTTP POST request utility programs 366 25.1.1. PHP .................................................... 366 25.1.2. c-code 368 25.1.3. python .................................................. 371 25.2. Loop watchdog timer .......................................... 372 25.2.1. Located in hhsUtility.h/cpp .................................... 373 25.2.2. To be added in setup(): ...................................... 374 25.2.3. To be added in loop(): 375
Index ........................................................... 376