Home Appliance Hack-and-IoT Guidebook: Affordable Solutions with the ESP8266 and 3D Printing 3895765368, 9783895765360


196 22 53MB

English Pages 377 Year 2023

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

Home Appliance Hack-and-IoT Guidebook: Affordable Solutions with the ESP8266 and 3D Printing
 3895765368, 9783895765360

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

books books

books

Affordable solutions with the ESP8266 and 3D printing If you are looking for a small yet powerful IoT device, you are likely to come across the ESP8266 and compatible products on the market today. One of these, the Wemos/Lolin D1 Mini Pro board strikes a remarkable balance between cost and performance. A small and very affordable prototype board, the D1 Mini Pro stands out with its WiFi functionality and a 16-Mbytes flash memory for easy creation of a flash file system. In addition, there are sufficient input and output pins (only one analog input though) to support PWM, I2C, and One-Wire systems to mention but a few. The book describes the operation, modding, construction, and programming of home appliances including a colorful smart home accessory, a refrigerator/greenhouse controller, an AC powerline monitor, a door lock monitor, and an IKEA Trådfri controller.

Hans Henrik Skovgaard has over 25 years of management experience in the mobile phone industry and holds an MSc. in Electronic Engineering. He has authored four top-selling books on electronics design and embedded software.

As a benefit, all firmware developed for these DIY, “IoT-ized” devices can be updated over-the-air (OTA). For most of the designs in the book, a small printed circuit board (PCB) and an enclosure are presented so readers can have a finished and attractive-looking product. Readers having – or with access to! – a 3D printer can “print” the suggested enclosures at home or in a shop. Some of the constructions benefit from a Raspberry Pi configured as a gateway or cms server. This is also described in detail with all the necessary configuring. You don’t need to be an expert but the prerequisites to successful replication of the projects include basic skills with PC software including the ability to surf the Internet. In terms of hardware, you should be comfortable with soldering and generally assembling the PCBs presented in the book. All custom software written for the IoT devices, the PCB layouts, and 3D print files described in the book are available for free downloading from the books section of Elektor Store website, www.elektor.com/books – simply search for the book title.

Elektor International Media BV www.elektor.com

Home Appliance Hack-and-IoT Guidebook • Hans Henrik Skovgaard

Home Appliance Hack-and-IoT Guidebook

Home Appliance Hack-and-IoT Guidebook Affordable solutions with the ESP8266 and 3D printing

Hans Henrik Skovgaard

Cover Home Appliance Hack-and-IoT Guidebook_v02.indd Alle pagina's

08-12-2022 14:18

Home Appliance Hack-and-IoT Guidebook Affordable solutions with the ESP8266 and 3D printing

● Hans Henrik Skovgaard

Home Appliance Hack and IoT Guidebook - UK.indd 3

13-12-2022 11:07

● 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 ISBN 978-3-89576-537-7

Print 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

●4

Home Appliance Hack and IoT Guidebook - UK.indd 4

13-12-2022 11:07

Contents

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

●5

Home Appliance Hack and IoT Guidebook - UK.indd 5

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook 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

●6

Home Appliance Hack and IoT Guidebook - UK.indd 6

13-12-2022 11:07

Contents 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

●7

Home Appliance Hack and IoT Guidebook - UK.indd 7

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook 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

●8

Home Appliance Hack and IoT Guidebook - UK.indd 8

13-12-2022 11:07

Contents 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

●9

Home Appliance Hack and IoT Guidebook - UK.indd 9

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook 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

● 10

Home Appliance Hack and IoT Guidebook - UK.indd 10

13-12-2022 11:07

Contents 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

● 11

Home Appliance Hack and IoT Guidebook - UK.indd 11

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook 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

● 12

Home Appliance Hack and IoT Guidebook - UK.indd 12

13-12-2022 11:07

Contents 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

● 13

Home Appliance Hack and IoT Guidebook - UK.indd 13

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook 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

● 14

Home Appliance Hack and IoT Guidebook - UK.indd 14

13-12-2022 11:07

Chapter 1 • Introduction Welcome to the Home Appliance Hack-and-IoT Guidebook. Disclaimer The projects and builds I am about to describe in this book have been assessed as thoroughly as possible. I have not personally damaged any of my equipment (or hurt myself), but I am sure it's possible. In addition, the technology described herein is purely for fun and should not be used for medical, mission-critical, or safety-critical applications in any circumstances. Neither I nor the Publisher accept any responsibility for damages of any kind due to your actions taken after reading this book. Having said this, let's continue.

1.1. Why this book? Like many others, when I first laid my eyes on the ESP8266, it immediately captured my imagination, not unlike when the Raspberry Pi came along. Before the arrival of the ESP8266, I completed many Arduino projects, some with Internet connections. It was therefore obvious to me that I should have a go with ESP8266. Some of the projects and findings are described in this book. I hope you will enjoy working with ESP8266 as much as I did.

1.2. Who is this book for? To use this book, I expect some basic skills and awareness from you: • You have some experience with PC software. That is, you are familiar with operating a Windows PC and know how to navigate a file system, edit a file, and install/configure software. • You have experience in surfing the Internet. You need to be able to locate and download the files mentioned in the book. After downloading, you should be able to find and install the downloaded files. • You know how to assemble a PCB. Since many of the constructions need additional hardware compared to the ESP8266 (Wemos/LOLIN boards), you will need to be able to assemble the required circuit boards. They are not complicated and I have avoided using SMD components. • You have some basic mechanical skills. Most of the constructions need to be incorporated into present mechanical constructions but some may need mechanical changes (e.g., the IKEA Lamp Hack).

● 15

Home Appliance Hack and IoT Guidebook - UK.indd 15

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

You will also need to disassemble some constructions but this should be relatively straightforward as with the IKEA Trådfri controller. Please be careful!

1.3. How to use this book Even though this book is about the ESP8266, you may need to install and configure an embedded Linux system (e.g., a Raspberry Pi) Some basic skills are required to do this. Everything will be described in detail, or through links to additional information on the internet. When configuring Linux, you will see specific commands listed like this: pi@raspberrypi: $

cd /var/www/emoncms/Modules

pi@raspberrypi: $

git clone https://github.com/emoncms/app.git

Commands are printed in bold characters. The last command is listed on two lines but should be entered as one long command like "git clone https://github.com/emoncms/app.git"

Throughout the book, there will be parts that will be highlighted as shown below. Warning: This is what a warning will look like. This is where you, your surroundings, or equipment may be damaged if not overseen with care and attention: Don't worry there will not be many of these. Note: This is what a note looks like. Be sure to pay attention to notes. Comment: This is where I may "side-track" occasionally and give additional information for further reading on the subject. Files of interest will be shown like this: /opt/lamp/htdocs

● 16

Home Appliance Hack and IoT Guidebook - UK.indd 16

13-12-2022 11:07

Chapter 1 • Introduction

Finally, I will show program file listings in boxes like this: #include main() { printf("hello world\n"); }

I encourage you to read the section describing the construction you want to build in advance and follow links if available. This should give you an idea about what lies ahead. I also encourage you to make notes about what you are doing just in case you need to do it all over again. This is especially important when you need to make a larger software installation — I speak from experience.

1.4. The organization of the book To avoid reading the entire book to discover what's covered, there is a very short description of each Chapter.

1.4.1. Chapter 2: Components This book is mainly about the Espressif ESP8266 microcontroller. I have chosen the Wemos/ LOLIN D1 mini pro, but you will most likely be able to use other variants. Since the ESP8266 and Raspberry Pi complement each other quite well, there will also be design ideas that incorporate the Raspberry Pi. In this chapter, the following components are briefly described: • • • • • • • • •

Adafruit Neopixel Jewel 7 Raspberry Pi Grönö lamp SSR Maxim DS18B20 QRE1113 (Analog) Silabs MGM210L or similar SHT30 Shield HP303B Shield

1.4.2. Chapter 3: Arduino IDE installation This chapter will guide you through the installation of the Arduino IDE. To compile software for ESP8266, you will need to install additional libraries. If you are familiar with the Arduino IDE, there shouldn't be any surprises.

● 17

Home Appliance Hack and IoT Guidebook - UK.indd 17

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

1.4.3. Chapter 4: emoncms Installation I have chosen to use emoncms for logging and visualization. This chapter describes how to install emoncms on a Raspberry Pi, but it is not a requirement that the installation is on a Raspberry Pi. At the time of writing, my emoncms runs on a larger Linux PC. To make emoncms work, additional software packages will have to be installed, but all will be described here. If you don't want a local installation of emoncms, there is the option of the cloud at: https://emoncms.org

1.4.4. Chapter 5: MQTT (mosquitto) Installation on a Raspberry Pi The installation will cover the basic MQTT broker but also a publisher and subscriber client. If in favor of it, a Windows MQTT client will be installed as well. https://guide.openenergymonitor.org/technical/mqtt/

1.4.5. Chapter 6: OpenSCAD I have used OpenSCAD for my housing designs. A brief introduction will be provided.

1.4.6. Chapter 7: ESP8266 Security A short walkthrough of why I, so far, have not secured my ESP8266 devices.

1.4.7. Chapter 8: ESP8266 Watchdogs A description of the various watchdog types available within ESP8266. These watchdogs can be included in your designs or left out. It is up to you how critical your system is. If used, they may also help you debug constant reboot scenarios.

1.4.8. Chapter 9: ESP8266 Over The Air – Update/Programming (OTA) A description of how to make OTA via a web browser. Two other methods will briefly be described.

1.4.9. Chapter 10: ESP8266 WiFi Antenna A walkthrough of the external WiFi antenna possibilities within the Wemos design. A description of a remarkably simple DIY antenna and a more sophisticated Yagi antenna will be provided.

● 18

Home Appliance Hack and IoT Guidebook - UK.indd 18

13-12-2022 11:07

Chapter 1 • Introduction

1.4.10. Chapter 11: ESP8266 Housing Most of the constructions in this book will be accomplished using a simple housing designed using the OpenSCAD program. The designs will be described so you may change them according to your specific needs.

1.4.11. Chapter 12: VPN Gateway The setup of a small Raspberry Pi VPN gateway will be given. It is a standard setup so it can be used for other purposes as well. My home firewall is based on pfsense so the configuration of OpenVPN will be part of the description.

1.4.12. Chapter 13: Software/Firmware design This Chapter describes how I structured the ESP8266 Arduino design. It is here merely for you to understand what is going on. It is up to you to follow or redesign it.

1.4.13. Chapter 14: IKEA Hack It's called an "IKEA Hack" because a Neopixel Jewel 7 is placed inside a Grönö lamp and controlled by an ESP8266. It is a cheap and easy solution for a project housing. Other lamps can also be modified. A web interface to control the lamp is also shown.

1.4.14. Chapter 15: Door Lock Monitor With the help of a QRE1113 and ESP8266, it is possible to monitor the status of a door lock – or indeed any other moving object 1 mm away from the QRE1113. The status of the door lock is stored in a central location, so the information can be used for surveillance. The status can be shown at "emoncms" or via the MQTT server.

1.4.15. Chapter 16: Energy Sensor This design is hooked up to an S0 interface on a power consumption meter. Since the S0 interface outputs a specific amount of pulses per kWh, it is possible to measure the power consumption of your household. Data will be gathered at a central server, which in this case is a Raspberry Pi.

1.4.16. Chapter 17: Refrigerator Control This chapter will describe a small control device that can keep a modified refrigerator at a constant temperature of between 4 oC and the ambient temperature. It includes a small web interface and log functionality.

● 19

Home Appliance Hack and IoT Guidebook - UK.indd 19

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

1.4.17. Chapter 18: Greenhouse Thermostat Based on the Refrigerator control, a greenhouse thermostat is described. This design will include two temperature sensors, so the outside temperature can be measured as well as the inside temperature – for comparison. Like the refrigerator control, it includes a small web interface and log functionality.

1.4.18. Chapter 19: 230-VAC Relay Control This is a spinoff of the refrigerator/greenhouse control together with an ordinary relay design; you will be able to turn 230-VAC equipment on and off.

1.4.19. Chapter 20: Standalone Temperature Sensor This is also a spinoff of the refrigerator/greenhouse control. In both cases a temperature sensor is the key component, so why not make it a standalone construction which can be used in multiple cases?

1.4.20. Chapter 21: Temperature, Pressure, and Humidity Sensor This construction uses the SHT30 and the HP303B shields to make a combined measurement of the temperature, barometric pressure, and humidity.

1.4.21. Chapter 22: IKEA Trådfri Controller Using this construction, devices can be turned on or off using the IKEA home smart app. A description of how to get hold of the ZigBee MGM210L device from Silabs will also be given.

1.4.22. Chapter 23: Troubleshooting Includes my findings during the development of the constructions and the writing of this book. Most likely it will not solve all of your problems.

1.4.23. Chapter 24: PCB Layout For most constructions mentioned in this book, a small PCB has been put together to make them easier to build. The PCB layout will be shown here and the Gerber files will be available for download on Elektor's support and resources page for this book. The PCBs should fit into the provided housings if available.

1.5. Abbreviations In this book, I am using selected abbreviations, some of which may not be well known. Below is a list with additional explanations.

● 20

Home Appliance Hack and IoT Guidebook - UK.indd 20

13-12-2022 11:07

Chapter 1 • Introduction

Abbreviation

Description

RPi

Raspberry Pi

DIY

Do-It-Yourself (occasionally with good results)

PCB

Printed Circuit Board

GCC

GNU Compiler Collection

LED

Light Emitting Diode

RGB

Red, Green, Blue

cURL

A computer software project providing a library and command-line tool for transferring data using various protocols.

GIT

A distributed revision control system developed by Linus Torvalds

RTS

Real-Time Clock.

VPN

Virtual Private Network. It routes all of your internet activity through a secure, encrypted connection,

DTIM

Delivery Traffic Indication Message.

Table 1-1: Abbreviations.

1.6. References I would not have been able to author this book without the Internet. If you know where to look, you can find answers to almost every question. Since many of my findings are the result of other people's demanding work, I also want to give them credit and mention their work throughout the book where appropriate. You will also find testimonials in my code either to the borrowed code or to where I got help to be able to proceed. There will also be code or ideas which I simply have forgotten where I picked it up – this is not intentional. For your convenience, there is a file together with the web software containing the links in this book (it is, after all, easier to cut and paste instead of just pasting). All links have been verified at the time of writing but I cannot promise they will be available when you actually need them. Instead, try and search the Internet for the last part of the URL. Sometimes you may find the documentation elsewhere.

1.7. Feedback As I value any feedback on what I can do to improve, I encourage you to send me comments, improvements, or any calls for help. I will try and answer as soon as possible – nomally within one week. Please e-mail me at [email protected]

● 21

Home Appliance Hack and IoT Guidebook - UK.indd 21

13-12-2022 11:07

Chapter 2 • Components

Home Appliance Hack-and-IoT Guidebook

Chapter 2 • Components This chapter briefly describes each of the components or boards used in this book. There will be a brief description only. If more information is required, you should look into data sheets for each component or board. Please note that component descriptions are based on available information at the time of writing. Things change rapidly in this business, so some of the descriptions may be obsolete or subject to improvement at the time you read this book. I have tried to build the constructions in a way that allows upgrading to the latest version.

2.1. ESP8266 When I started using the ESP8266-based Wemos family of development boards, I kicked off with the Wemos D1 mini Pro as shown in Figure 2-1. Today, the family has been enlarged and you need to determine your requirement before you continue.

Figure 2-1: Wemos D1 mini Pro (ver 1.0). The following table shows the differences.

Operating Voltage

D1 mini Pro

D1 mini

D1 mini Lite

3.3V

3.3V

3.3V

Digital I/O Pins

11

11

11

Analog Input Pins

1 (3.2V max.)

1 (3.2V max.)

1 (3.2V max.)

Clock Speed

80/160 MHz

80/160 MHz

80/160 MHz

Flash

16 MBytes

4 MBytes

1 MBytes

Size

48*25.4 mm

34.2*25.6 mm

34.2*25.6 mm

Weight

4.5 g

3g

5g

Internal

X

X

X

External

X

Antenna

Table 2-1: Wemos differences.

● 22

Home Appliance Hack and IoT Guidebook - UK.indd 22

13-12-2022 11:07

Chapter 2 • Components

They all share the same pin layout. The main decision is to determine the size of your program, as you will be able to use the internal antenna in most cases. If you want to power your construction with a Lithium battery, you can use a built-in LiPo charging circuit (500 mA max) in the D1 mini Pro version. The battery option should be combined with the sleep functionality mentioned in section 2.1.2.

2.1.1. Wemos/Lolin, general notes As mentioned above, the controller boards chosen for the projects in this book are based on the Wemos family of development boards. Although arguably "Arduino clones", these boards • come cheap (approx. US$ 5.00) • are small (see above table) • comprise WiFi • comprise a Lithium battery interface (D1 mini pro) Making them perfect as IoT controllers – but that's another story.

Figure 2-2: Lolin D1 mini Pro, D1 mini and D1 mini Lite. Throughout this book, you can use an ordinary Arduino with or without a WiFi shield (if you don't like the Wemos controller). Note The software has not been tested on an ordinary Arduino. It should, however, be possible to make it run with modifications.

● 23

Home Appliance Hack and IoT Guidebook - UK.indd 23

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

The latest versions of the D1 boards can be found here: https://www.wemos.cc/en/latest/d1/index.html

2.1.2. Sleep Mode During the description of the projects in this book, it should be noted that most are not optimized for Sleep Mode. In this document: https://fwww.espressif.com/sites/default/files/documentation/9b-esp8266-low_ power_solutions_en.pdf The following can be found regarding Sleep Mode: The ESP8266 series chip provides three configurable sleep modes. I also provide some low-power solutions related to these sleep modes. Users can choose and configure the sleep mode as required. The 3 sleep modes are: • Modem-sleep • Light-sleep • Deep-sleep The differences between Sleep Modes can be seen in the below table: Item

Modem-sleep

Light-sleep

Wi-Fi

OFF

OFF

OFF

System clock

ON

OFF

OFF

ON

ON

RTC

ON

Deep-sleep

CPU

ON

Pending

OFF

Substrate

15 mA

0.4 mA

~ 20 µA

DTIM = 1

16.2 mA

1.8 mA

-

DTIM = 3

15.4 mA

0.9 mA

-

DTIM = 10

15.2 mA

0.55 mA

-

Average current

Table 2-2: ESP8266 Sleep Mode differences. As evident from the table, power savings are achievable if the right Sleep Mode is selected and the software is designed for it. This is important if you power your device using a battery. In each case, you should carefully investigate what fits your needs and how to bring the ESP8266 into Sleep Mode, and equally important, how to bring the ESP8266 out of Sleep Mode. ● 24

Home Appliance Hack and IoT Guidebook - UK.indd 24

13-12-2022 11:07

Chapter 2 • Components

2.2. Raspberry Pi

Figure 2-3: Raspberry Pi 3 Model B. This is not a book about Raspberry Pi (RPi) proper. I will only show the basics on what is required to get it working as a server or gateway. Fortunately, there is a galaxy of documentation on the internet about the RPi, which you may consult if you want to know more. One such place is, of course, the Raspberry Pi homepage: https://www.raspberrypi.org/ or Wikipedia: https://en.wikipedia.org/wiki/Raspberry_Pi The latter gives a good overview of its history and evolution. For the projects in this book, any of the Raspberry models can be used although I recommend using one of the later models, 3 or 4. Your RPi should not be hard to obtain, either online or from a local store.

2.2.1. Raspberry Pi configuration To get the RPi up and running, you will need some software — the operating system (OS) — installed. The following is a link to all the Raspberry Pi operating system images: https://www.raspberrypi.org/software/operating-systems/ I usually use the "Raspberry Pi OS with desktop and recommended software" version. At the time of writing, the release is called "bullseye". This may have changed by the time you are reading this. Former names have included "Buster", "Stretch", "Wheesy" and "Jessie". Please keep this in mind when looking for help on the Internet. There may be differences between the releases.

● 25

Home Appliance Hack and IoT Guidebook - UK.indd 25

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

The recommended installation process can be found here: https://www.raspberrypi.org/documentation/installation/ Together with "bullseye" a new installation process is needed, if you like me want to run the system in the so-called "headless mode". The update to "bullseye" has removed the default "pi" user to make it harder for attackers to find and compromise Internet-exposed Raspberry Pi devices using default credentials. In the following, I will show how I install the "bullseye" operating software with "headless mode" enabled. As mentioned here: https://www.raspberrypi.com/documentation/computers/getting-started.html#usingraspberry-pi-imager you should start by downloading the "Raspberry Pi Imager". https://www.raspberrypi.com/software/ Once downloaded and installed, insert you SD-card and start the "Raspberry Pi Imager". It is fairly easy to use. Once you have selected the "Operating System version" and "Storage" you should enter the "Advanced options" since some necessary configuration need to be done. Below the full "Advanced options" is shown:

● 26

Home Appliance Hack and IoT Guidebook - UK.indd 26

13-12-2022 11:07

Chapter 2 • Components

Figure 2-4: RPi Advanced options. As a bare minimum, please pay attention to: • Enable SSH • Set username and password

● 27

Home Appliance Hack and IoT Guidebook - UK.indd 27

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

It would also make sense to: • Set local settings. After having saved the settings, press "Write" and a standard warning for data loss will be shown.

Figure 2-5: Start writing the operating system. Press YES to continue. Depending of the Operating system selected, it may take a long time to finish the writing of the operating system. Once finished, the following will be shown:

Figure 2-6: Operating system successfully written to SSD. If Microsoft Windows is used, two new disk drives will be created together with some comments from Microsoft Windows.

● 28

Home Appliance Hack and IoT Guidebook - UK.indd 28

13-12-2022 11:07

Chapter 2 • Components

Figure 2-7: Two new disk drives.

Figure 2-8: Newly created disk drive not recognized. Press Cancel and then OK. You are now ready to place the SD-card in the RPi. Connect the RPi to your home network, on the same LAN as your PC, and power up the RPi for the first time. I am using PuTTY to connect to my RPi: https://www.putty.org/ You should, however, be able to use any SSH client. When you connect to your newly configured RPi for the first time, you will get the following security alert:

● 29

Home Appliance Hack and IoT Guidebook - UK.indd 29

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

Figure 2-9: SSH warning during your first Raspberry Pi login. Answer Yes to continue. Log in using the "username" and "password" entered in Figure 2-4. The screenshot below shows what it may look like (assuming user "pi" !).

Figure 2-10: Raspberry Pi SSL login. You should keep the "username" and "password" to your Raspberry Pi in a safe location for future use. There are other ways of making a "headless" connection to the RPI: Xming: https://sourceforge.net/projects/xming/ VNC: https://www.raspberrypi.org/documentation/remote-access/vnc/ ...just to mention a few.

● 30

Home Appliance Hack and IoT Guidebook - UK.indd 30

13-12-2022 11:07

Chapter 2 • Components

2.3. Neopixel Jewel 7 From Adafruit's homepage about the Neopixel Jewel 7: https://www.adafruit.com/product/2226 The following can be found: Be the belle of the ball with the NeoPixel Jewel! Fit seven of our tiny 5050 (5mm x 5mm) smart RGB LEDs onto a beautiful, round PCB with mounting holes and a chainable design to create what you think is our most elegant (and evening-wear appropriate) NeoPixel board yet.

Figure 2-11: Adafruit NeoPixel Jewel 7. As mentioned, the Adafruit Neopixel Jewel consists of seven individually controllable smart RGB LEDs. Each RGB LED has an integrated driver, which makes it controllable from a microcontroller running the right software. A schematic of the Adafruit Neopixel Jewel is pictured below.

Figure 2-12: Adafruit NeoPixel Jewel 7 schematics.

● 31

Home Appliance Hack and IoT Guidebook - UK.indd 31

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

In this project, you use the PWR, GND, and INPUT pins. You can put several Jewels in serial by connecting the OUTPUT from one to the next Jewel's INPUT. The Jewel cannot be operated on its own — it needs some kind of controller to send instructions to each RGB LED.

2.4. IKEA Grönö lamp I looked at several options on how to make a lamp in which to house my Wemos/NeoPixel. As usual, I may say, IKEA came to my rescue. I wanted something that could soften the colors of the NeoPixel and look nice at the same time. My choice so far is the IKEA Grönö lamp.

Figure 2-13: IKEA Grönö lamp. It is quite easy to modify the bulb socket so it can hold the NeoPixel jewel. Also, the lamp is cheap ... at least in Denmark. The lamp can be found here: https://www.ikea.com/gb/en/p/groenoe-table-lamp-frosted-glass-white-70373223/ Like many other companies, IKEA also changes its product range so you may find the Grönö lamp is not available. At the time of writing, the Grönö lamp is no longer available in Denmark on their homepage. Instead, Ikea have an updated version called ISBRYTARE which is constructed like the Grönö lamp. https://www.ikea.com/gb/en/p/isbrytare-table-lamp-frosted-glass-white-30504947/ I don't benefit in any way from mentioning IKEA here.

● 32

Home Appliance Hack and IoT Guidebook - UK.indd 32

13-12-2022 11:07

Chapter 2 • Components

2.5. SSR The following description of an SSR is reproduced from: https://en.wikipedia.org/wiki/Solid-state_relay A solid-state relay (SSR) is an electronic switching device that switches on or off when a small external voltage is applied across its control terminals. SSRs consist of a sensor which responds to an appropriate input (control signal), a solid-state electronic switching device which switches power to the load circuitry, and a coupling mechanism to enable the control signal to activate this switch without mechanical parts. The relay may be designed to switch either AC or DC to the load. It serves the same function as an electromechanical relay but has no moving parts. Packaged solid-state relays use power semiconductor devices such as thyristors and transistors, to switch currents up to around a hundred amperes. Solid-state relays have fast switching speeds compared with electromechanical relays, and have no physical contacts to wear out. Application of solid-state relays must consider their lower ability to withstand momentary overload, compared with electromechanical contacts, and their higher "on" state resistance. Unlike an electromechanical relay, a solid-state relay provides only limited switching arrangements (SPST switching). The SSR used in this book can be seen here: https://www.sparkfun.com/products/13015

Figure 2-14: SSR from Sparkfun. The chosen SSR can switch current loads of up to 40 A with a 3-32 VDC input. It is conservatively rated, making a heatsink superfluous. I don't have any preferences for selecting an SSR, and any similar device will do.

● 33

Home Appliance Hack and IoT Guidebook - UK.indd 33

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

2.6. Maxim DS18B20 The temperature sensor used in this book is the type DS18B20 from Maxim. The only reason for this is that I have used it in many previous projects and builds.

Figure 2-15: DS18B20 pin configuration. (Courtesy Maxim IC). The datasheet can be seen here: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements and has an alarm function with non-volatile user-programmable upper and lower trigger points. The DS18B20 communicates over a 1-Wire bus that by definition requires only one data line (and ground) for communication with a central microprocessor. In addition, the DS18B20 can derive power directly from the data line ("parasite power"), eliminating the need for an external power supply. Each DS18B20 has a unique 64-bit serial code, which allows multiple DS18B20s to function on the same 1-Wire bus.

2.7. QRE1113 (Analog) This is a small line sensor breakout board, with an easy-to-use analog output, which will vary depending on the amount of IR light reflected to the sensor. The board is pictured below (size: 7.62 x 13.97 mm).

● 34

Home Appliance Hack and IoT Guidebook - UK.indd 34

13-12-2022 11:07

Chapter 2 • Components

Figure 2-16: QRE1113 (analog). More information can be found here: https://www.sparkfun.com/products/9453 Below is a description taken from the homepage. The board's QRE1113 IR reflectance sensor is comprised of two parts — an IR emitting LED and an IR sensitive phototransistor. When you apply power to the VCC and GND pins the IR LED inside the sensor will illuminate. A 100-Ω resistor is on-board and placed in series with the LED to limit current. A 10-kΩ resistor pulls the output pin high, but when the light from the LED is reflected back onto the phototransistor the output will begin to drop. The more IR light sensed by the phototransistor, the lower the output voltage of the breakout board.

Figure 2-17: QRE1113 schematic.

2.8. Silabs MGM210L This is the ZigBee device that IKEA uses in their Trådfri products.

● 35

Home Appliance Hack and IoT Guidebook - UK.indd 35

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

It is a little standalone device that you can read more about here: https://www.silabs.com/documents/public/data-sheets/mgm210l-datasheet.pdf

Figure 2-18: Silabs MGM210L module. As shown in Chapter 22., the devices used in this book have been salvaged from (cheap) IKEA lamps.

2.9. I/O devices The following two shields are used in a small in-door meter station to be developed out in the future. It is possible to measure: • temperature, • barometric pressure, and • humidity.

2.9.1. SHT30 Shield This digital temperature and humidity sensor shield uses the SHT3x-DIS sensor to measure, expectedly… temperature and humidity. Data on the sensor can be found here: https://www.wemos.cc/en/latest/_static/files/sht30-dis_datasheet.pdf The shield itself can be found here: https://www.wemos.cc/en/latest/d1_mini_shield/sht30.html

Figure 2-19: SHT30 shield.

● 36

Home Appliance Hack and IoT Guidebook - UK.indd 36

13-12-2022 11:07

Chapter 2 • Components

It uses the I2C Interface digital interface and has two user-selectable addresses, 0x44 or 0x45.

2.9.2. HP303B Shield This barometric pressure & temperature shield is based on the HP303B sensor. Data on the sensor can be found here: https://www.wemos.cc/en/latest/_static/files/hp303b_datasheet.pdf The shield proper can be found here: https://www.wemos.cc/en/latest/d1_mini_shield/barometric_pressure.html

Figure 2-20: HP303B shield. It uses the I2C Interface digital interface and has two user-selectable addresses, 0x77 or 0x76.

● 37

Home Appliance Hack and IoT Guidebook - UK.indd 37

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

Chapter 3 • Arduino IDE Installation There are many ways to program the Wemos board, but I have chosen the same method as for any other Arduino-compatible board. In the following, the standard Arduino IDE installation will be described. If you are already familiar with the Arduino IDE, you can skip this section.

3.1. Standard Arduino IDE install The standard Arduino IDE software can be downloaded from the Arduino homepage: https://www.arduino.cc/en/Main/Software Once downloaded on your favorite operating system, you should follow the installation process described here: https://www.arduino.cc/en/Guide/HomePage It should be straightforward and I so far have not faced any challenges running either Windows or Linux. Note If you work on a Windows 10 machine and have chosen to install the Arduino IDE via the Microsoft Store, you may face challenges. There is a note saying: IMPORTANT: This app performs with core functionality on Windows 10 S but some limited plugins do not work. At the time of writing (Q4/2022), I have observed this to not work with the Wemos plugin.

3.2. ESP8266 support Once the default Arduino IDE is installed, you need to add support for the ESP8266 processor. The easiest way to do this is to add the following link: https://arduino.esp8266.com/stable/package_esp8266com_index.json to the "Additional Boards Manager URL" in "Preferences".

● 38

Home Appliance Hack and IoT Guidebook - UK.indd 38

13-12-2022 11:07

Chapter 3 • Arduino IDE Installation

Figure 3-1: Arduino IDE preferences.

Figure 3-2: Additional Boards Manager URLs. As shown in Figure 3-2 additional URLs can be added. You should now be able to go to "Tools -> Board -> Boards Manager" (found at the top of the list of boards) In there, search for "ESP8266". The following should show up:

● 39

Home Appliance Hack and IoT Guidebook - UK.indd 39

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

Figure 3-3: Arduino IDE Board's manager. Press "Install". Once installed, you should see this:

Figure 3-4: ESP8266 Community installed. Under "Tools -> Board" you should be able to select "LOLIN(WEMOS) D1mini pro".

● 40

Home Appliance Hack and IoT Guidebook - UK.indd 40

13-12-2022 11:07

Chapter 3 • Arduino IDE Installation

Figure 3-5: Wemos D1 mini pro selected as board. You are now ready to compile software for the ESP8266. Things change rapidly and when you are about to add support for the ESP8266, new versions may have arrived. With the above installation, you should always get the latest stable version. If you are interested to see the differences between versions, follow this link: https://github.com/esp8266/Arduino/releases Since the Wemos/LOLIN boards are used in this book, more information on Arduino and the D1/D1 mini-series can be found here: https://www.wemos.cc/en/latest/tutorials/d1/get_started_with_arduino_d1.html

3.3. SPI Flash File (SPIFFS) system install In Figure 3 -5, you may notice the following:

● 41

Home Appliance Hack and IoT Guidebook - UK.indd 41

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

Figure 3-6: ESP8266 flash size. This indicates that the ESP8266 microprocessor used features a total of 16Mbytes of flash – where 14 Mbytes can be used as SPIFFS and OTA: ~1019 KB. What this indicates is that, with the right software, it is possible to create a small flash file system. This file system can be used as a normal file system to hold a homepage or logs. It is all up to you. The "Arduino ESP8266 filesystem uploader" can be found here: https://github.com/esp8266/arduino-esp8266fs-plugin#arduino-esp8266-filesystemuploaderOn this web page you can find the following description: Arduino plugin which packs sketch data folder into SPIFFS filesystem image, and uploads the image to ESP8266 flash memory. You will also find a description of how to install the filesystem uploader. This installation will be shown below with my comments. • Download the tool archive from: https://github.com/esp8266/arduino-esp8266fs-plugin/releases/tag/0.4.0 Comment: Select the .zip file. At the time of writing: ESP8266FS-0.5.0.zip • In your Arduino sketchbook directory, create a tools directory if it doesn't already exist. You can find the location of your sketchbook directory in the Arduino IDE at "File -> Preferences -> Sketchbook location". Comment: If in Windows, create the tools directory with File Explorer. • Unpack the tool into the tools directory (the path will look like /tools/ESP8266FS/tool/esp8266fs.jar). • Restart the Arduino IDE

● 42

Home Appliance Hack and IoT Guidebook - UK.indd 42

13-12-2022 11:07

Chapter 3 • Arduino IDE Installation

After the Arduino IDE has been restarted, a new menu item will show up under "Tools"

Figure 3-7: New "ESP8266 Sketch Data Upload" menu item. To use the newly installed file system uploader, the following steps need to be taken: • Open a sketch (or create a new one and save it). • Go to the sketch directory (choose "Sketch -> Show Sketch Folder"). This is achieved using a file manager or terminal window. • Create a directory named data and place any files you want in the file system there. The figure below shows the content of the directory: RefrigiratorControl_20180531_11a.

Figure 3-8: Content of the Arduino sketch directory. The content, in this case, of the data directory can be seen below:

Figure 3-9: Refrigerator control WEB-page files. • Make sure that the correct board is selected and the port is configured properly, then check that the serial monitor is closed.

● 43

Home Appliance Hack and IoT Guidebook - UK.indd 43

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

• When ready to upload, select the "Tools -> ESP8266 Sketch Data Upload" menu item. This should start uploading the files into the ESP8266 flash file system. Once the upload is complete, something like the below information should be displayed in the bottom window of the Arduino IDE.

Figure 3-10: SPIFFS upload information. It should be more or less self-explanatory. I have removed my specific paths and substituted them with . . as how they look will be individual. As shown, it may take some time to upload the data – in this case, 16.1 seconds. Note You still need to upload the actual code in the standard way.

● 44

Home Appliance Hack and IoT Guidebook - UK.indd 44

13-12-2022 11:07

Chapter 3 • Arduino IDE Installation

3.4. Alternatives to the SPI Flash File I have used the SPI Flash File system for quite some time without any problems and intend to continue to do so but the SPI Flash File is deprecated. A substitute to consider is the LittleFS as it focuses on higher performance and directory support, but has higher filesystem and per-file overhead (4 K minimum vs. SPIFFS' 256byte minimum file allocation unit). A good reason for selecting the LittleFS as a substitute for SPIFFS is that it is quite easy to change. Support for LittleFS is already included in your Arduino IDE installation so no additional libraries are needed. Instead the following will be added to the already working SPIFFS code: //#include "FS.h" #include #define SPIFFS LITTLEFS

All instances of SPIFFS.read() SPIFFS.write() SPIFFS.close() SPIFFS.open()

Will be redefined to LITTLEFS.read() LITTLEFS.write() LITTLEFS.close() LITTLEFS.open()

This is possible because the usage of the functions matches 1:1. There is one thing you need to install and that is a new filesystem uploader to LittleFS. This is done the same way as described in section 3.3. except you should download version 2.6.0 or later of the software here: https://github.com/earlephilhower/arduino-esp8266littlefs-plugin/releases Once correctly installed, a new menu will show up as in Figure 3-11:

● 45

Home Appliance Hack and IoT Guidebook - UK.indd 45

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

Figure 3-11: LittleFS Data Upload menu. The "ESP8266 LittleFS Data Upload" is used the same way as the "ESP8266 Sketch Data Upload" menu as described in section 3.3.

3.5. Additional libraries Throughout this book, additional or special software libraries are needed to support the hardware or features of some of the constructions introduced. The additional libraries can be installed on a "need to use" basis. They don't all need to be installed from the beginning. If you forget, you will be given a compiler warning indicating a missing #include file but each section will contain a sub-section about additional libraries.

● 46

Home Appliance Hack and IoT Guidebook - UK.indd 46

13-12-2022 11:07

Chapter 4 • emoncms Installation

Chapter 4 • emoncms Installation For logging and visualizing data, I have used emoncms for quite some time. You can use other systems but I have now gained a lot of experience with it so it will also be part of this book. At https://emoncms.org/ you can read the following: Emoncms is a powerful open-source web-app for processing, logging and visualizing energy, temperature, and other environmental data. This is exactly what is required: visualize temperature readings from the refrigerator and the "wh" collected from the energy meter. There are several ways of getting an emoncms installation up and running: • Ubuntu/Debian Linux via git on a PC • Raspberry Pi • Pre-built emonSD SD-card Image download • Purchase pre-loaded SD-card • Do it all by yourself. If you go for a Raspberry Pi installation like in this book, the easiest installation is to go with the emonSD SD-card image. This installation can be seen here: https://github.com/openenergymonitor/emonpi/wiki/emonSD-pre-built-SD-cardDownload-&-Change-Log Everything is preconfigured and all the apps and widgets are ready for use. On the other hand, if you like to tweak things, you can install everything yourself. Another reason why I prefer to do it myself is that I will not be using a full emonPi with the add-on-board as shown here: https://openenergymonitor.com/ Instead, I followed this instruction: https://github.com/emoncms/emoncms/blob/master/docs/RaspberryPi/readme.md This description installs emoncms on Raspberry Pi with "Raspbian Stretch" installed. There is a different installation for "Raspbian Jessie". Before you start the installation, install the latest version of Raspbian using this link: https://www.raspberrypi.org/downloads/raspbian/

● 47

Home Appliance Hack and IoT Guidebook - UK.indd 47

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

If you are new to working with Raspberry Pi, you should follow this installation instruction: https://www.raspberrypi.org/documentation/installation/installing-images/ README.md I am using the Win32DiskImager program to install the image. The etcher program is equally good. If you are into Linux or macOS use the "dd" command. Once finished, you need to configure Raspberry Pi to accept access via SSH. This is described in section 2.2. When everything is in place (SD card inserted, Ethernet cable inserted and attached to your router/firewall), power up your Raspberry Pi and enter your router/firewalls DHCP client overview. There you will see something like what is shown below.

Figure 4-1: Example of Firewall/Routers DHCP client overview. In my case, the Raspberry PI's IP address is 192.169.0.122. Installation and configuration of the emoncms will be achieved via an SSH connection only. You may install Xming or use VNC if you like a GUI interface, but this is not strictly necessary. All commands shown below will be entered via a terminal window. Now that the IP address of your Raspberry Pi is known, make an SSH connection to it. Default login in as "pi" with password "raspberry". As shown in section 2.2, you should change the default password to something different of your choosing.

4.1 Update and upgrade the Raspberry Pi Before any major new installations, always update and upgrade the present installation. This is achieved using the following command: pi@raspberrypi:~ $ sudo apt-get update && sudo apt-get upgrade

● 48

Home Appliance Hack and IoT Guidebook - UK.indd 48

13-12-2022 11:07

Chapter 4 • emoncms Installation

If during the process you are asked: "Do you want to continue Y/n" Answer Y (press enter) The process can take several minutes and a lot of relevant information will be displayed while upgrading. Once finished, reboot the Raspberry Pi using the command: pi@raspberrypi:~ $ sudo reboot

and log in again when ready. The next upgrade command to issue is: pi@raspberrypi:~ $ sudo apt-get dist-upgrade && sudo rpi-update

Once again, if during the process you are asked: "Do you want to continue Y/n" answer Y (press enter) The process can take several minutes and a lot of relevant information will be displayed whi le upgrading. Once finished, reboot your Raspberry Pi and log in again when ready. Your Raspberry Pi should now be fully updated, with hopefully no errors.

4.2. Install dependencies Comment In the following, a reference to MySQL and MariaDB will be made. If you want to know more about the history and differences between MySQL and MariaDB, search the Internet for "MySQL vs MariaDB". A lot of additional programs are required for the emoncms server to work. These programs depend on other programs and libraries.

4.2.1. MariaDB & apache Install these dependencies using the following command: pi@raspberrypi:~ $ sudo apt-get install -y apache2 mariadb-server mysql-client php7.0 libapache2-mod-php7.0 php7.0-mysql php7.0-gd php7.0-opcache php7.0-curl php-pear php7.0-dev php7.0-mcrypt php7.0-common redis-server php-redis git buildessential php7.0-mbstring

● 49

Home Appliance Hack and IoT Guidebook - UK.indd 49

13-12-2022 11:07

Home Appliance Hack-and-IoT Guidebook

Again this process can take several minutes and a lot of "relevant" information will be displayed while upgrading. Before you continue, changes to the MariaDB installation are required. The reason can be seen here: https://stackoverflow.com/questions/43379892/mariadb-cannot-login-as-root You need to change the default authentication for root. Unlike native MariaDB packages (those provided by MariaDB itself), packages generated by Ubuntu have unix_socket authentication for the local root by default. To check this, you need to enter MariaDB using the command: pi@raspberrypi:~ $ sudo mysql

Figure 4-2: CLI MySQL interface. Check the default authentication for root using this command: MariaDB [(none)]> SELECT user, host, plugin FROM mysql.user;

If you get the same result as shown in the following image, you will not be able to log in as root to the database. You need to change this.

Figure 4-3: Default MySQL authentication – before the change. To return to the usual password authentication, run

● 50

Home Appliance Hack and IoT Guidebook - UK.indd 50

13-12-2022 11:08

Chapter 4 • emoncms Installation

MariaDB [(none)]> UPDATE mysql.user SET plugin = '' WHERE plugin = 'unix_socket'; FLUSH PRIVILEGES;

Verify the changes are complete using the following command: MariaDB [(none)]> SELECT user, host, plugin FROM mysql.user;

Figure 4-4: Default MySQL authentication – after the change. When complete, leave the MariaDB database program using "exit".

4.2.2. phpMyAdmin To make the installation easier, the phpMyAdmin package will be installed as I prefer to make database changes this way. The required changes can be completed from the command line interface as well, but I prefer the other. Choose whatever you like. The installation is performed using the command: pi@raspberrypi:~ $ sudo apt-get install phpmyadmin

Once asked: "Do you want to continue Y/n" answer Y (press enter) When asked: "Web server to reconfigure automatically:" Chose apache2 and press . The next question to answer is: "Configure database for phpmyadmin with dbconfig-common?" select "yes" and press enter. Next is to select a password for phpMyAdmin... just follow the process.

● 51

Home Appliance Hack and IoT Guidebook - UK.indd 51

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

After this, the installation procedure will continue and take some time. When complete, verify the phpMyAdmin installation is in place by entering the following link in a browser on your PC: http:///phpmyadmin The following should be seen:

Figure 4-5: phpMyAdmin login. Log in with user: phpmyadmin and the password provided earlier. If you'd like to log in as root via phpMyAdmin to MariaDB, you need to change the root password as it is blank. A blank password is not accepted by the phpMyAdmin interface as shown below.

Figure 4-6: Failed phpMyAdmin login with a blank password. It is possible to change the MariaDB root password using a Linux command but since several other security issues also need to be fixed it is recommended to run the following command: pi@raspberrypi:~ $ sudo mysql_secure_installation

● 52

Home Appliance Hack and IoT Guidebook - UK.indd 52

13-12-2022 11:08

Chapter 4 • emoncms Installation

As indicated below, the first thing to take care of is the password.

Figure 4-7: MySQL secure installation script part 1. Unless you are experienced in setting up MariaDB, simply answer Y to the following questions: • Anonymous user • Remote login • Drop test database • Privileged tables.

● 53

Home Appliance Hack and IoT Guidebook - UK.indd 53

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 4-8: MySQL secure installation script part 2. The security level of MariaDB has now been upgraded and the database should have a nonblank password so you can use phpMyAdmin to log in.

4.2.3. PECL The next dependency library to install is the PHP Extension Community Library (PECL) dependencies. This is achieved using the following commands: pi@raspberrypi:~ $ sudo pear channel-discover pear.swiftmailer.org

Figure 4-9: PECL installation part one. pi@raspberrypi:~ $ sudo pecl channel-update pecl.php.net

● 54

Home Appliance Hack and IoT Guidebook - UK.indd 54

13-12-2022 11:08

Chapter 4 • emoncms Installation

Figure 4-10: PECL installation part two. pi@raspberrypi:~ $ sudo pecl install swift/swift redis

The last command will take a while to finish. During installation, you will be asked the following questions: enable igbinary serializer support? [no] : enable lzf compression support? [no] : In both cases, I usually answer NO.

Figure 4-11: Redis installation. Comment: If you want to read more about igbinary, which is a method of storing PHP data structures in a compact binary form, go to the following URL: https://github.com/igbinary/igbinary At the end of the last command, the following comment is shown: configuration option "php_ini" is not set to php.ini location

You should add "extension=redis.so" to php.ini This indicates that you need to add the sentence: extension=redis.so

● 55

Home Appliance Hack and IoT Guidebook - UK.indd 55

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

to the file php.ini. This is achieved using the following command: pi@raspberrypi:~ $ sudo nano /etc/php/7.0/apache2/php.ini

There is a section with a heading called "Dynamic Extensions". Just below is a good place to enter the following sentence: extension=redis.so.

When complete, it may look like this: ;;;;;;;;;;;;;;;;;;;;;; ; Dynamic Extensions ; ;;;;;;;;;;;;;;;;;;;;;; ; Added by HHS extension=redis.so

Save the file by pressing X and "y" and "enter". There is an additional php.ini file located in /etc/php/7.0/cli/. Since you are not going to issue any PHP command line commands, this file does not need to be updated. You continue by issuing more commands: pi@raspberrypi:~ $ sudo a2enmod rewrite

Figure 4-12: Enabling module rewrite. pi@raspberrypi:~ $ printf "extension=redis.so" | sudo tee /etc/php/7.0/modsavailable/redis.ini 1>&2 pi@raspberrypi:~ $ sudo phpenmod redis

Comment: There is more information about mod_rewrite here: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html In short, it allows us to rewrite URLs in a cleaner fashion, translating human-readable paths into code-friendly query strings

● 56

Home Appliance Hack and IoT Guidebook - UK.indd 56

13-12-2022 11:08

Chapter 4 • emoncms Installation

More information about redis.so can be found here: https://redis.io/topics/introduction In short, Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. The final configuration before installing emoncms is to configure apache2.conf. You need to change and From: AllowOverride None To: AllowOverride All. This is achieved using the following command: pi@raspberrypi:~ $ sudo nano /etc/apache2/apache2.conf

Figure 4-13: Apache.conf update. Enter the changes and Save & Exit as described earlier. More information about the AllowOverride Directive can be found here: https://httpd.apache.org/docs/2.4/mod/core.html

● 57

Home Appliance Hack and IoT Guidebook - UK.indd 57

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Finally, restart apache: pi@raspberrypi:~ $ sudo systemctl restart apache2

You can verify apache is running by entering the IP of your Raspberry Pi in a browser. You should then see something like the following:

Figure 4-14: Apache default web page.

4.3. emoncms code The emoncms application will be installed via Git. You will use it to download and install the application only. If you want to know more about Git, go to this site: https://git-scm.com/ First set the permissions for the /var/www directory: pi@raspberrypi:~ $ sudo chown $USER /var/www

Then enter the /var/www directory and git clone emoncms. pi@raspberrypi:~ $ cd /var/www && git clone -b stable https://github.com/emoncms/ emoncms.git

The next screendump shows the last three commands issued.

Figure 4-15: Installation of the emoncms git code.

● 58

Home Appliance Hack and IoT Guidebook - UK.indd 58

13-12-2022 11:08

Chapter 4 • emoncms Installation

You can verify that emoncms is installed using the command: pi@raspberrypi:~ $ ls -al

Figure 4-16: list directory content with cmd: "ls –al". Having emoncms installed, proceed to the configuration of emoncms.

4.3.1. Configure emoncms To install the emoncms database, you need to perform the following tasks: • Secure the database server – if not already completed with the mysql_secure_installation script earlier. • Create the emoncms database • Create a database user for emoncms to use. Before issuing any MariaDB commands the MariaDB session is started using the following command: pi@raspberrypi:~ $ sudo mysql --user=root --password=

The is the one that was entered earlier when the mysql_secure_installation script was executed. Secure the database and cleanse it using the following: MariaDB [(none)]> DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1'); DELETE FROM mysql.user WHERE User=''; DROP DATABASE IF EXISTS test; DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%'; FLUSH PRIVILEGES;

● 59

Home Appliance Hack and IoT Guidebook - UK.indd 59

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 4-17: Emoncms database clean up. (password redacted) The above figure shows how MariaDB is entered and the clean-up is performed. The emoncms database using utf8 character decoding is created using the following MariaDB command: MariaDB [(none)]> CREATE DATABASE emoncms DEFAULT CHARACTER SET utf8;

Figure 4-18: Creation of emoncms database. In the command below, the database "user" named "emoncms" is created, together with a "" of your choice for that user. MariaDB [(none)]> CREATE USER 'emoncms'@'localhost' IDENTIFIED BY ''; GRANT ALL ON emoncms.* TO 'emoncms'@'localhost'; flush privileges;

Figure 4-19: Creation of emoncms database user.

● 60

Home Appliance Hack and IoT Guidebook - UK.indd 60

13-12-2022 11:08

Chapter 4 • emoncms Installation

Please make a note for further use of both the database "username" ('emoncms') and the "", as they will be inserted into the settings.php file in a later step. You are now finished with the database and user creation. If you enter phpMyAdmin as root, you can verify the creations.

Figure 4-20: emoncms database displayed by phpMyAdmin. Data repositories for the emoncms feed engine are created as follows: pi@raspberrypi:~ $ sudo mkdir /var/lib/{phpfiwa,phpfina,phptimeseries}

This will create the following directories: /var/lib/phpfiwa /var/lib/phpfina /var/lib/phptimeseries

You need to set the permission for the directories using the following command: pi@raspberrypi:~ $ sudo chown www-data:root /var/lib/ {phpfiwa,phpfina,phptimeseries}

It is now time to configure the emoncms itself. First, copy default.settings.php into settings.php:

● 61

Home Appliance Hack and IoT Guidebook - UK.indd 61

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

pi@raspberrypi:~ $ cd /var/www/emoncms && cp default.settings.php settings.php

The settings need to be updated to reflect the emoncms-"user" and emoncms-"password" entered earlier. This will enable emoncms to access the database. pi@raspberrypi:/var/www/emoncms

$ nano settings.php

Change the following: $server

= "localhost";

$database = "emoncms"; $username = "emoncms"; $password = "";

Further down in the settings is an optional "data structure store" - Redis, which acts as a cache for the data produced by emoncms to ensure that it is efficiently written to disk. To activate Redis, change "false" to "true". : //2 #### Redis $redis_enabled = true;

Save and exit as before. A symbolic link is required to reference the emoncms installation from within the webroot folder. This is achieved using this command: pi@raspberrypi: $ cd /var/www/html && sudo ln -s /var/www/emoncms

Figure 4-21: List the newly created symbolic link to emoncms. Next, write permissions for the emoncms log file are set: pi@raspberrypi: $ sudo touch /var/log/emoncms.log && sudo chmod 666 /var/log/ emoncms.log

Within the emoncms user interface, there is a possibility to reboot or shut down the Raspberry Pi. This is only possible if the web server has sufficient privileges to do so.

● 62

Home Appliance Hack and IoT Guidebook - UK.indd 62

13-12-2022 11:08

Chapter 4 • emoncms Installation

This is done by editing the sudoers file: pi@raspberrypi: $ sudo visudo

Look for the: # User privilege specification root

ALL=(ALL:ALL) ALL

And add: www-data ALL=(ALL) NOPASSWD:/sbin/shutdown

as shown below.

Figure 4-22: Changed sudoers content. Save and exit as done previously. There should be no errors while doing so. If you receive errors, reopen and fix the error. If you make an error it will be flagged as pictured below:

Figure 4-23: sudoers edit error. It is now time to connect to emoncms for the first time.

● 63

Home Appliance Hack and IoT Guidebook - UK.indd 63

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

This is achieved by entering the following in a browser at your PC: http:///emoncms

Figure 4-24: emoncms login screen. When the above login screen is displayed, the emoncms database has been set up and you are ready to register to sign in. To do so, create an account by entering your email, user name, and password of your choice and click register.

Figure 4-25: emoncms login.

● 64

Home Appliance Hack and IoT Guidebook - UK.indd 64

13-12-2022 11:08

Chapter 4 • emoncms Installation

You will then see an empty emoncms as shown below:

Figure 4-26: Initial emoncms interface before feeds creation. Figure 4-26 is a rudimentary emoncms installation. To show something "fancy", you need to install some additional modules, which by default come with emonPI.

4.4. emoncms moduless 4.4.1. Dashboard The first module to install is the dashboard module. This is required to create all the fancy boards, which show the data collected. The installation is straightforward. Start by opening a terminal window and issue the following commands: pi@raspberrypi: $ cd /var/www/emoncms/Modules pi@raspberrypi:/var/www/emoncms/Modules $ git clone

https://github.com/emoncms/

dashboard

The result is shown below:

● 65

Home Appliance Hack and IoT Guidebook - UK.indd 65

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 4-27: Git clone of the Dashboards module. When complete, the installation of the dashboard module can be verified using the following command: pi@raspberrypi:/var/www/emoncms/Modules $ ls -al

This lists all the modules and also the new dashboard module. Each time emoncms is updated or a new module is installed, it is necessary to check the emoncms database. This is done via the Administration interface. Go to Setup -> Administration and click "Update & Check" as shown below.

Figure 4-28: Update & Check menu item in the Administration interface. Something like Figure 4-29 will be listed:

● 66

Home Appliance Hack and IoT Guidebook - UK.indd 66

13-12-2022 11:08

Chapter 4 • emoncms Installation

Figure 4-29: Database update message after Dashboards module install. Press Apply changes and a "Success" message should hopefully appear. A new menu item will now be displayed if Setup is selected:

Figure 4-30: Dashboards added to the Setup drop-down menu. You are now ready to create individual dashboards. The dashboards I made for this book will be shown under each project. If you want to read more about the newly installed dashboard, go to: /var/www/emoncms/Modules/dashboard/Readme.md

Or visit this link, where some dashboards are shown: https://github.com/emoncms/dashboard/

4.4.2. Graph Installation of the graph module follows the same installation method as shown for the dashboard. Start by opening a terminal window and issue the following commands:

● 67

Home Appliance Hack and IoT Guidebook - UK.indd 67

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

pi@raspberrypi: $ cd /var/www/emoncms/Modules pi@raspberrypi:/var/www/emoncms/Modules $ sudo git clone https://github.com/ emoncms/graph

Figure 4-31 shows the git clone command.

Figure 4-31: git clone of the Graphs module. Verify the installation of the graph module using: pi@raspberrypi:/var/www/emoncms/Modules $ ls -al

This lists all the modules and also the new graph module. As shown in Figure 4-28, update the database via the administration interface.

Figure 4-32: Database update message after Graphs module install. This time, the updates required are different but again press Apply changes. Once again, a success message should hopefully show up. To make emoncms use the graph module as default feed viewer, be sure to update the emoncms file: /var/www/emoncms/settings.php

This is achieved using the following command: pi@raspberrypi: $ nano /var/www/emoncms/settings.php

● 68

Home Appliance Hack and IoT Guidebook - UK.indd 68

13-12-2022 11:08

Chapter 4 • emoncms Installation

Add or change $feedviewpath = "graph/";

Once complete, save & exit. A new menu item will now be displayed if Setup is selected:

Figure 4-33: Graphs added to the Setup drop-down menu. If you want to read more about the newly installed graph module, go to: /var/www/emoncms/Modules/graph/Readme.md

Or visit this link – where a lot of graphs are shown: https://github.com/emoncms/graph As shown later, the graph module is a vital part of the data displayed on the dashboards.

4.4.3. Apps It is possible to install application-specific dashboards which add several pre-formatted templates, enabling data to be displayed across a wide range of devices such as desktops, tablets, and smartphones. This is achieved using the following commands: pi@raspberrypi: $ cd /var/www/emoncms/Modules pi@raspberrypi: $ git clone https://github.com/emoncms/app.git

Figure 4-34: Git clone of the Apps.

● 69

Home Appliance Hack and IoT Guidebook - UK.indd 69

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

As before, update the emoncms database:

Figure 4-35: Database update message after Apps install. The result should be a green success message. In the emoncms web interface to the left, there is now a link:

Figure 4-36: The emoncms Apps icon. More information about the newly installed app module can be found here: /var/www/emoncms/Modules/app/Readme.md

Or via this link, where a brief description of the apps is shown: https://github.com/emoncms/app

4.5. Documentation Once installed, it's time to configure emoncms and start using it. This can be quite challenging before you figure out the terminology and inner logic. I will try and explain the terminology here and refer you to the description related to each of the constructions. The place to start looking for information/documentation is here: https://github.com/emoncms and the subpage https://github.com/emoncms/emoncms in particular.

● 70

Home Appliance Hack and IoT Guidebook - UK.indd 70

13-12-2022 11:08

Chapter 4 • emoncms Installation

On this page, the following is listed: Emoncms Terminology • Inputs: An incoming data source. Each input has an associated "node" identifier and a "key" sub-identifier. Inputs are entry points, only the last value and time of the input is recorded. To record historical data, a feed needs to be created from an input. • Input: Node: A grouping identifier for inputs or feeds. • Input: Key: A sub-identifier for items within each Node. • Input process list (or input processing): A list of processes* performed sequentially on each input value as it is received on that input. • Process: A function that can be attached to the process list of an input to change the value or to save the value to a feed*. • Feeds: (Feed) A place where data is recorded, a time-series of data points. The standard time-series databases used by emoncms are PHPFina and PHPTimeSeries and were written as part of the emoncms project. To relate the terminology, I will show how input from the Wemos becomes data and graphs. The string that is sent from Wemos is shown below:

Figure 4-37: JSON HTTP string from Wemos. As soon as Wemos starts transmitting data, it will be visible under "Setup->Inputs" as shown below.

Figure 4-38: Inputs received from the remote devices.

● 71

Home Appliance Hack and IoT Guidebook - UK.indd 71

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Before you can use the inputs received from the Wemos device, you need to create one or more processes. This is achieved by pressing the spanner related to the input you want to create a process for. This will reveal a lengthy list of possibilities that can then be combined. Each one of them will have a small description of what it will be doing. Usually, each process performs a task on the input and passes the result to the next process until there are no more processes in the list. It will be described in more detail under each section. One example of a process list can be seen in Figure 16-29. Once the process list is in place, the result of the processes will show up under the Feeds list (Setup -> Feeds) One such list can be seen in Figure 16-30. Note If you want to delete the process list created under Inputs, you also need to delete the similar entries under Feeds to completely delete it all. Having created the Feeds, it is now possible to create dashboards showing the status of your system. Two examples of dashboards you will be guided to create are shown in Figure 16-18 and Figure 18-49. If you need inspiration just look on the internet: some of them are quite fancy. Configuration of the dashboard is achieved via the dashboard configuration toolbox (Setup -> Dashboard). To make life a little easier, a description of the Toolbox has been included below. If you are familiar with graphical design this should not be a challenge.

● 72

Home Appliance Hack and IoT Guidebook - UK.indd 72

13-12-2022 11:08

Chapter 4 • emoncms Installation

Figure 4-39: Toolbox button descriptions. Note It takes some time to gather sufficient data to show something that makes sense watching. It typically takes a day or two to collect enough data to display. For more inspiration go to: https://community.openenergymonitor.org/c/emoncms/showcase

● 73

Home Appliance Hack and IoT Guidebook - UK.indd 73

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 5 • MQTT (mosquitto) Installation on a Raspberry Pi It is not strictly necessary to install the MQTT broker to use the builds in this book. It will, however, make life a lot easier and pick up and distribute information gathered with the door lock monitors. Once installed you will be able to use it for many other purposes in addition to door lock monitoring. By visiting: http://mqtt.org/faq you can read the following about MQTT: MQTT stands for MQ (Message Queue) Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimize network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging "machine-to-machine" (M2M) or "Internet of Things" world of connected devices, and for mobile applications where bandwidth and battery power are at a premium. There you will also find more about the protocol specifications and how to use MQTT. As indicated above, the MQTT protocol is ideal for the setup that will be used for lock sensors. The ESP8266 can be thought of being an IoT device and the involved parties are certainly performing "machine-to-machine" communication. Once the idea is captured, it is extremely easy to use, at least in the basic configuration. I recommend searching the Internet for more as there is a vast amount of additional documentation ready to download. The MQTT implementation that will be used in this content is mosquitto from: https://mosquitto.org/ where you can read the following: Eclipse Mosquitto is an open-source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 5.0, 3.1.1 and 3.1. Mosquitto is lightweight and is suitable for use on all devices from low power single board computers to full servers.

● 74

Home Appliance Hack and IoT Guidebook - UK.indd 74

13-12-2022 11:08

Chapter 5 • MQTT (mosquitto) Installation on a Raspberry Pi

The MQTT protocol provides a lightweight method of conducting messaging using a publish/ subscribe model. This makes it suitable for Internet of Things messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers. The Mosquitto project also provides a C library for implementing MQTT clients, and the extremely popular mosquitto_pub and mosquitto_sub command line MQTT clients. Mosquitto is part of the Eclipse Foundation and is an iot.eclipse.org project. The MQTT setup that will be used in this construction is quite simple and does not make use of all the functionality which is available. The overall network setup can be seen below:

Figure 5-1: MQTT network setup. It consists of the following components: • MQTT broker • MQTT client publisher • MQTT client subscriber • Gateway: The wireless gateway that binds it all together. Change IP address, SSID, and password according to your needs. In this design, the MQTT client subscriber is another ESP8266, but you can use any kind of platform, as long as it is possible to make a visual display of the status of the MQTT client publisher. The MQTT publisher-subscriber setup can be seen below:

● 75

Home Appliance Hack and IoT Guidebook - UK.indd 75

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 5-2: MQTT publisher-subscriber setup. "MQTT client publisher" (located at the door) will, regularly, publish the state of the door lock (OPEN/CLOSED) and a door number (D1) to the MQTT broker. Since the "MQTT client subscriber" is subscribing to information within "lock/esp8266", it will receive all publications within this topic. In this implementation the topic is called "lock/esp8266", but it could be given any name. There are a few rules that need to be followed: lock: is the topic level /: is the topic level separator. The topic name is case sensitive, so lock/esp8266 and lock/ESP8266 are two different topics. With the basic topics in place, you are ready to install the backbone of the MQTT system. This is completed in the form of Mosquitto's implementation of the MQTT.

5.1. Installation of the Mosquitto broker As you are going to install the Mosquitto software on a Raspberry Pi, you should perform the update and upgrade of the Raspberry Pi software as mentioned in section 4.1. The installation will be performed via an SSH connection to the Raspberry Pi and a standard terminal as described in section 2.2. Install the Mosquitto software directly from the mosquito repository. To do so, you first need to import the repository package signing key. This is done using the following commands: pi@raspberrypi:~ $ wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key

● 76

Home Appliance Hack and IoT Guidebook - UK.indd 76

13-12-2022 11:08

Chapter 5 • MQTT (mosquitto) Installation on a Raspberry Pi

Figure 5-3: Mosquito signing key import. pi@raspberrypi:~ $ sudo apt-key add mosquitto-repo.gpg.key

Figure 5-4: Add the mosquito repository package signing key. Next, go to the "/etc/apt/sources.list.d/" directory using the command: pi@raspberrypi:~ $ cd /etc/apt/sources.list.d/

You are now ready to download the actual software. The next commands, however, depend on the version of Raspberry Pi Debian. If you are using the "Jessie" version, issue the following command: pi@raspberrypi:~ $ sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie. list

If you are using the "Stretch" version, issue the following command: pi@raspberrypi:~ $ sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch. list

If the version of the Raspberry Pi Debian is neither Jessie nor Stretch, you can go to: http://repo.mosquitto.org/debian/ and look up the correct name of the list file.

● 77

Home Appliance Hack and IoT Guidebook - UK.indd 77

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

I am using the "Stretch" version so the installation will look like the following:

Figure 5-5: Download mosquitto software. Before you continue, update the system using the command: pi@raspberrypi:~ $ sudo apt-get update

Figure 5-6: Update the RPI system. You are now ready to install the actual Mosquitto software using the following command: pi@raspberrypi:~ $ sudo apt-get install mosquitto

● 78

Home Appliance Hack and IoT Guidebook - UK.indd 78

13-12-2022 11:08

Chapter 5 • MQTT (mosquitto) Installation on a Raspberry Pi

Figure 5-7: Install the Mosquitto software. The basic Mosquitto installation is now complete. However, you still need to add a minimum level of security in the form of a user with a login/password. This is achieved using the following command: pi@raspberrypi:~ $ sudo mosquitto_passwd -c /etc/mosquitto/passwd doorlock

In this case, the username is "doorlock" but can be anything as long as you change it accordingly in the MQTT client software. The name of the file to store the password in is named "/etc/mosquitto/passwd" but can be anything as long as you update the Mosquitto configuration file with the name as shown below. You need to update "/etc/mosquitto/mosquitto.conf" with password_file /etc/mosquitto/passwd allow_anonymous false

● 79

Home Appliance Hack and IoT Guidebook - UK.indd 79

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

as shown below.

Figure 5-8: Update Mosquitto config file. You can verify the Mosquitto broker is running by using the following command: pi@raspberrypi:~ $ ps –A | grep mosquitto

Figure 5-9: The mosquitto broker is running.

5.2. Installation of the Mosquitto client (RPI) For test purposes, you will install the Mosquitto client on the Raspberry Pi/ this, however, is not strictly necessary for this to work. Installation of the Mosquitto client is straightforward and is achieved by issuing the following command in a terminal window: pi@raspberrypi:~ $ sudo apt-get install mosquitto-clients

● 80

Home Appliance Hack and IoT Guidebook - UK.indd 80

13-12-2022 11:08

Chapter 5 • MQTT (mosquitto) Installation on a Raspberry Pi

Figure 5-10: Mosquitto client install. It is now possible to verify the installation is working as expected. Go to section 5.3. to see how this can be done before the esp8266 MQTT clients are available.

5.3. Verification of the Mosquitto installation You will most likely want to verify your Mosquitto installation is working. This can be done via the Raspberry Pi if you have completed the installations mentioned in sections 5.1. and 5.2. Open two terminal windows and start by subscribing to the topic: lock/esp8266 with the command: pi@raspberrypi:~ $ mosquitto_sub -d -t lock/esp8266 -u doorlock -P area#51

You should recognize the user(doorlock) and password(area#51) from the ESP8266 client software. Once opened, the terminal will start displaying messages sent to the topic (lock/esp8266).

● 81

Home Appliance Hack and IoT Guidebook - UK.indd 81

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

In the figure below the lock, the sensor is publishing data about status so the broker is working and both the subscriber and publisher are using the same topic.

Figure 5-11: Raspberry Pi MQTT subscriber. If you don't have the lock sensor running, you can issue the following publication command in the other terminal window you opened earlier: pi@raspberrypi:~ $ mosquitto_pub -d -t lock/esp8266 –m "D3:CLOSED" -u doorlock -P area#51

In the next image, you can see how a publication is recorded on the Raspberry Pi. If you look at the above image, you should see the message "D3:CLOSED".

Figure 5-12: Raspberry Pi MQTT publisher. You can write any message you like to the topic (lock/esp8266) and it will show up on all the MQTT clients subscribing to the same topic. You should hopefully have an idea of how things work for now. If it does not work, verify the following: • The topic is the same • The password and user name are the same

● 82

Home Appliance Hack and IoT Guidebook - UK.indd 82

13-12-2022 11:08

Chapter 5 • MQTT (mosquitto) Installation on a Raspberry Pi

5.4. Installation of the Mosquitto client (Windows PC) If you'd like to use a Windows MQTT client tool instead of using the one provided via the Raspberry Pi, you could search the Internet for the topic: MQTT windows tools One of the tools that should pop up is the MQTT.fx client tool. It is a fairly new tool (from 2014) with quite an impressive feature set. There are other equally good tools but I decided to try out the MQTT.fx. MQTT.fx is an MQTT Client written in Java based on Eclipse Paho and can be found here: http://www.mqttfx.org/ Installation is straightforward: • Download the latest version (1.7.1 at the time of writing) which fits your Windows operating system. • Once downloaded, execute the installation file. Before you can connect to the broker, you need to make an initial configuration:

Figure 5-13: MQTT.fx configuration setup. You need to enter: • a suitable Profile name of your choosing. • the broker address (the same used by the ESP8266 subscriber and publisher) • the broker port. If the default is used (1883), then there is no need to change it.

● 83

Home Appliance Hack and IoT Guidebook - UK.indd 83

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The rest of the values should be left unchanged. You then need to enter the User Credentials as found in the ESP8266 software: #define MQTT_USERNAME "doorlock" #define MQTT_PASSWORD "area#51"

Figure 5-14: MQTT.fx user credential configuration. SSL/TLS, Proxy, and LWT should not be changed – unless you need a special setup. Once complete, remember to apply the changes. To start publishing or subscribing, press the Connect button:

Figure 5-15: Connect to the broker from MQTT.fx. You should then connect to the broker. A successful connection is indicated by the green LED in the top right corner:

Figure 5-16: MQTT.fx successful connection.

● 84

Home Appliance Hack and IoT Guidebook - UK.indd 84

13-12-2022 11:08

Chapter 5 • MQTT (mosquitto) Installation on a Raspberry Pi

Enter the topic that you want to subscribe to. In case of no changes, it should be lock/ esp8266:

Figure 5-17: MQTT.fx subscriber topic. If the ESP8266 publisher is running, you should see something like this:

Figure 5-18: MQTT.fx working Windows client. This was a very brief walkthrough of the MQTT.fx software. You should be underway and ready to watch and debug.

● 85

Home Appliance Hack and IoT Guidebook - UK.indd 85

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 6 • OpenSCAD While working with the Wemos ESP8266 development boards I soon discovered that I needed some kind of casing/housing for my constructions. At first, I was using a small box like the one shown below found at Aliexpress:

Figure 6-1: Initial default Wemos housing. It was OK in the beginning but soon a need for custom-made housing emerged. Since the housings for my constructions are remarkably simple and had to be printed on a (cheap) 3D printer, they were not complicated to draw. I therefore turned my attention to the OpenSCAD program.

Figure 6-2: OpenSCAD interface. The above figure shows how the small housing for the standard LOLIN D1 mini was made. On the left, you "program" your design, and on the right, you see what it will look like.

● 86

Home Appliance Hack and IoT Guidebook - UK.indd 86

13-12-2022 11:08

Chapter 6 • OpenSCAD

Being an old programmer I found it much easier than making the design in a tool like SolidWorks or FreeCAD. To use the OpenSCAD tool, you need to have a good understanding of how to break the design up into smaller 3D parts. For your convenience, both the "*.scad" and resulting "*.stl" files for each construction will be available. In each OpenSCAD design, there will be variables like "ANTENNA = 0;" which can be set to "1" or "0". If set to "1" there will be a hole made for the external antenna. If set to "0" there will be no hole for the external antenna. You don't need to install OpenSCAD to 3D-print the housing presented here, just use the provided "*.stl" files together with your favorite 3D-slicer. If you on the other hand want to adapt my design to your needs, you should install OpenSCAD. I have tried to comment the "*.scad" files so you can locate each part of the design. In Chapter 11. the two "standard" Wemos/Lolin housings are explained in more detail. There will also be specific 3D designs, which will be mentioned together with the construction they apply to. One such 3D design is the Yagi antenna design. While programming your 3D design use "F5" to see your changes. When ready with the design, press "F6" to render the changes. When this is done you may export your design to a "*.STL" file for later processing.

● 87

Home Appliance Hack and IoT Guidebook - UK.indd 87

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 7 • ESP8266 Security The ESP8266 is an Internet of Things (IoT) device and will most likely, somehow, be connected to the Internet to be of any use... maybe not the big Internet but at least your LAN. You should, therefore, secure your device. I must admit: I have not secured my ESP8266 devices so far. The lame excuse has been that it is difficult to configure. A more in-depth excuse is that no one has access to my wireless network unless they're actually in my house, and also I don't like to expose the devices to the outside world. Espressif has provided an "ESP8266 SSL User manual" and there are examples of how to generate SSL certificates (HelloServerSecure) so it is just a matter of getting started. When looking at ESP8266 security, there are several areas to cover: • Secure your WEB-servers running on the device. • Secure access to configure your devices. • Secure the MQTT communication. You can search the Internet for references and most likely find one that suits your needs. So my warning, for now, is to be careful on how you expose your devices and have a good firewall on your private network.

7.1. VPN Since I looked into this topic for the first time, a lot has happened and one of the things I have done to improve security was to install a VPN server at my pfSense firewall. By installing a VPN client (OpenVPN) on your remote device (PC, tablet, or mobile phone) you can contact your ESP8266 devices when outside your private LAN. You should be able to find documentation on how to install OpenVPN clients and VPN servers on your pfSense firewall on the Internet and therefore I will not go through it here. Elsewhere in this book, I have described how to make a VPN gateway if your devices are located outside your private LAN.

● 88

Home Appliance Hack and IoT Guidebook - UK.indd 88

13-12-2022 11:08

Chapter 8 • ESP8266 Watchdogs

Chapter 8 • ESP8266 Watchdogs 8.1. Watchdogs in general When building small embedded systems like the ones in this book, it is a clever idea to know how the system can recover from an error situation. Mainly because they are often placed where they are hard to get in case of malfunction. There are several ways of doing this: • Manually pressing the reset button (if available like on the Wemos PCB) • Manually perform a power cycle • Relying on a built-in watchdog. In the first two situations, you are the watchdog, like when you press on a Windows PC. In the last method, you rely on the built-in watchdog that will hopefully restore your device to normal operation by performing a software reset (most often). A watchdog, in our case, can be thought of as a counter, which will regularly be decremented until it reaches zero and reacts...or "bite" if you like. You can avoid the counter reaching zero by regularly "hitting" it, thus bringing it back to max value. You may have already experienced the presence of the built-in watchdogs as an endless loop of resets without any obvious reason. The most likely reason is that your software has triggered the software watchdog, due to an endless loop or deadlock situation in your code. By default, the ESP8266 contains two watchdog timers: a hardware timer, controlled via registers, and a software watchdog serviced by one of the hardware timers dedicated to the WiFi stack. The hardware timer has 7 to 8 seconds of lifespan. The software timer has a shorter lifespan of slightly over three seconds. Like many other parts of the ESP8266, it is difficult to find any documentation on built-in watchdogs. Fortunately enough, many individuals have explored the area in-depth, which you can benefit from. In this section, I will walk through a design I came across on the Internet.

8.2. Loop watchdog The watchdogs present are built-in and live their own life. You don't need to pay any attention to them. If you want to know more about what is going on, hints on reverse-engineering the ESP8266 watchdog timers can be found here:

● 89

Home Appliance Hack and IoT Guidebook - UK.indd 89

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

https://mongoose-os.com/blog/esp8266-watchdog-timer/ On the other hand, if you have a system that needs additional protection or reliability, you need to add a (loop) watchdog that you control yourself. This is called a "watchdog managed by sketch". The reason you may want to have a sketch-managed watchdog is that all it takes to freeze a microcontroller is an endless loop that accidentally updates any of the ESP watchdogs. The sketch-managed watchdog I'm about to use in the upcoming projects is described in detail here: https://sigmdel.ca/michel/program/esp8266/arduino/watchdogs2_en.html The reference has many code examples that illustrate how you may (accidentally) trigger built-in watchdogs. The sketch-managed watchdog will also be a timer, which increments in this case. Since it is expected to be called at the beginning of the loop()- function, it will be called "lwdt" (loop-watchdog-timer). The "lwdt" is implemented using a Ticker object, lwdTicker, which will invoke a call back function attached to it at a particular interval, in this case, every 15 seconds. The Ticker files used (Ticker.h and Ticker.cpp) are part of the esp8266 core for the Arduino environment so no additional libraries are needed. The call-back routine, lwdtcb() checks the elapsed time since the last time the watchdog was updated(kicked) and if it is greater than the timeout period, performs a software reset of the ESP8266, thereby bringing the device back to normal operation. You can read more about the Ticker object here: https://github.com/sstaub/Ticker This link also describes how you should install the "library" if you don't want to use the built-in part of the esp8266 core for the Arduino environment. Initialization of the call-back routine with an associate variable can be seen in the below listing. // Reboot if loop watchdog timer reaches this time out value #define LWD_TIMEOUT

15*1000

// To be used with lwTicker callback routine #include

● 90

Home Appliance Hack and IoT Guidebook - UK.indd 90

13-12-2022 11:08

Chapter 8 • ESP8266 Watchdogs

/* ----- lwTicker callback routine variables --- */ Ticker

lwdTicker;

unsigned long lwdTime

= 0;

unsigned long lwdTimeout = LWD_TIMEOUT; /* ----- lwTicker callback routine variables END */ Void setup() { // Init watchdog -------------------------------// feed loop watchdog (restart timeout timer) lwdtFeed(); // attach lwdt callback routine to Ticker object lwdTicker.attach_ms(LWD_TIMEOUT, lwdtcb); // Init watchdog -------------------------------}

Figure 8-1: Initialization of the watchdog callback routine with associated variables. To ensure that your loop()-function is continuously executed, you should only feed the "lwdt" call-back routine at the beginning of the loop()-function. The trigger interval LWD_TIMEOUT should be greater than the built-in ESP8266 watchdogs, and greater than the worst-case scenario for executing the full loop()-function. You may need to do some experimentation here. Just in case, add a couple of extra seconds. The people behind the reference suggest 15 seconds, which has worked for the builds in this book. The callback function can be seen below. void ICACHE_RAM_ATTR lwdtcb(void) { if ((millis() - lwdTime > LWD_TIMEOUT) || (lwdTimeout - lwdTime != LWD_TIMEOUT)) { //could perform other actions before restarting the ESP Serial.println("** WARNING loop Blocked! check for deadlock!"); ESP.restart(); } }

Figure 8-2: The watchdog callback function.

● 91

Home Appliance Hack and IoT Guidebook - UK.indd 91

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Warning: You should not try to simplify the following: if ((millis()-lwdTime > LWD_TIMEOUT) || (lwdTimeout-lwdTime != LWD_TIMEOUT))

To this: if ((millis() > lwdTimeout) || (lwdTimeout - lwdTime != LWD_TIMEOUT))

It will not work when rollover occurs: lwdTime > 0xFFFFFFFF - LWD_TIMEOUT

and lwdTimeout (which would be > 0xFFFFFFFF) becomes a small value i.e., < LWD_TIMEOUT. The warning is present in the code just in case you forget. During development, you should comment out the ESP.restart() to get a warning and not a restart of your system. Once you are comfortable with your design, you can remove the comments. The routine to update or "kick" the "lwdt" can be seen below. void lwdtFeed(void) { lwdTime

= millis();

lwdTimeout = lwdTime + LWD_TIMEOUT; }

Figure 8-3: Function to "kick" the lwdt. This is very straightforward and should not pose any challenges. If you have made C-like programs you may have experienced that a loose pointer can overwrite your memory and your variables with unpredictable results. To avoid this, the present "lwdt" uses two global variables for the timer: lwdTime: which holds the time the watchdog was last fed, and lwdTimeout: which will contain the value in lwdTime plus a constant, which is LWD_TIMEOUT. The reason for this is to increase reliability. By having two global variables you are less vulnerable if the newly flashed firmware starts ruining the memory. As mentioned, all it takes is a faulty pointer somewhere in your program.

● 92

Home Appliance Hack and IoT Guidebook - UK.indd 92

13-12-2022 11:08

Chapter 8 • ESP8266 Watchdogs

While talking about watchdogs and reset of the ESP8266, it is possible to obtain the reason the ESP8266 has come to be started or restarted at the beginning of the sketch ( setup() ) This is achieved by calling the following (assembler)-function: int getBootDevice(void) { int bootmode; asm ( "movi %0, 0x60000200\n\t" "l32i %0, %0, 0x118\n\t" : "+r" (bootmode) /* Output */ : /* Inputs (none) */ : "memory" /* Clobbered */ ); return ((bootmode >> 0x10) & 0x7); }

Figure 8-4: getBootDevice() - function. The code can be found here: https://www.esp8266.com/viewtopic.php?p=2096#p2112 Now that you know the reason the ESP8266 was started/restarted, it is possible to take the necessary action.

8.2.1.Boot reason For now, I just write the reason and determine if it is the first time after the firmware upload. This can be done by including the following software at the start of the setup()- function. // Boot information -----------------------// To be used in case some of the Watchdogs fires. Serial.printf("\n\nReason for reboot: %s\n", ESP.getResetReason().c_str()); Serial.println("-------------------------------------"); if ( getBootDevice() == 1 ) { Serial.println("\nThis sketch has just been uploaded over the UART."); Serial.println("The ESP8266 will freeze on the first restart."); Serial.println("Press the reset button or power cycle the ESP now"); Serial.println("and operation will be resumed thereafter."); while (1) { yield(); }

● 93

Home Appliance Hack and IoT Guidebook - UK.indd 93

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

} // Boot information END --------------------

Figure 8-5: Boot information in the

setup()-function.

With the above code included, a restart will look like this:

Figure 8-6: Initial reboot message. More on "ESP8266 Reset Causes and Common Fatal Exception Causes" can be absorbed here: https://www.espressif.com/sites/default/files/documentation/esp8266_reset_causes_ and_common_fatal_exception_causes_en.pdf More on handling boot information and restart codes can be found here: https://www.sigmdel.ca/michel/program/esp8266/arduino/watchdogs_en.html #ESP8266_WDT_RECOV The "https://github.com/esp8266/Arduino/releases" may be improved even further as mentioned here: https://www.sigmdel.ca/michel/program/esp8266/arduino/watchdogs3_en.html By saving a (watchdog)-code in the EEPROM, you have a chance of finding out why your ESP8266 keeps resetting. Up to now I have not taken the "sketch-managed watchdog" that far.

● 94

Home Appliance Hack and IoT Guidebook - UK.indd 94

13-12-2022 11:08

Chapter 8 • ESP8266 Watchdogs

Note If you are using an "ESP8266 Community" before ver. 2.5.0 you will experience a different sketch upload and therefore a different restart of the device. You should upgrade to the latest version, which will give you an esptool.py. The default always ends the upload with a "Hard resetting via RTS pin".

● 95

Home Appliance Hack and IoT Guidebook - UK.indd 95

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA) Over the years, I have made many ESP8266 constructions, which from time to time call for recent updates. If they are easy to get hold of, you can of course simply attach them to a PC and do a serial update. You can also do an "Over The Air" update or programming (OTA) and leave them as is. So, in short: "OTA update is the process of loading the software to the ESP8266 module using the WiFi connection rather than the serial port. This is extremely useful in case of limited or no physical access to the ESP8266 module". OTA comes in three variants: • Arduino IDE Mainly to be used during development. This will not be covered here. • web browser To be used after deployment to provide a manual update with a web browser. This is the method I will describe here. • HTTP Server To be used after deployment to provide an automatic update via a dedicated server. This will not be covered here. I have chosen the update process via the web browser since I don't have that many devices to update and have 24/7 access to all my devices. Surely, my devices that enter sleep mode could use the "HTTP server" update version but for now, it has not been worth the effort. If you want to know more about the "Arduino IDE" update or the "HTTP server" update, trawl the Internet. You should be able to recoup plenty of documentation. One such place could be: https://www.arduino.cc/reference/en/libraries/arduinoota/

9.1. web browser OTA To use the web browser OTA functionality you don't need to install additional software via the Arduino library manager. You just need to include some additional special ESP8266 libraries, which in most cases should add easily to your already evaluated and verified software. I will show this later.

● 96

Home Appliance Hack and IoT Guidebook - UK.indd 96

13-12-2022 11:08

Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA)

9.1.1. Configure the Arduino IDE First, you need to configure the Arduino IDE so you can locate the compiled software for the OTA update. This is done by changing the Arduino preferences as follows: • In the Arduino IDE select: "File -> Preferences". • In "Preferences" check out "Show verbose output during compilation" as shown below:

Figure 9-1: "Show verbose output during compilation". This setting will show some valuable verbose output during compilation, among other things the location of the -file to be uploaded to the ESP8266 board. One other setting which may come in handy is the system-debug-information. To get more information about the update process, activate "Updater" under "Tools->Debug level". Remember to activate "Tools->Debug port Serial".

9.1.2. Prepare already working software for OTA update. To prepare already working software for OTA update, the following code may need to be added to your working software: #includes: #include #include #include #include

New variables: const char* host= "esp8266-webupdate" const char* update_path = "/firmware"; const char* update_username = "admin"; // OTA:Username const char* update_password = "admin"; // OTA:Password ESP8266WebServer httpServer(80); ESP8266HTTPUpdateServer httpUpdater;

● 97

Home Appliance Hack and IoT Guidebook - UK.indd 97

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

You may need to change the ESP8266WebServer variable name (httpServer) to something else if you already have declared one earlier. All variables can be changed to fit your needs. After WiFi has been initialized and started up, execute the following: MDNS.begin(host); httpUpdater.setup(&httpServer, update_path, update_username, update_password); httpServer.begin(); MDNS.addService("http", "tcp", 80);

I usually do this in the StartWiFI(void) function. Finally add the following at the top of the loop()-function: httpServer.handleClient(); MDNS.update();

To make it easier to discover the changes to the working code, I have added the comment "// OTA" in the code I have made. In some cases, the libraries or variables are already included. In these cases, I add the comment "// (OTA)" The present installation of the OTA update works under the following conditions/limitations: • The PC with the new software and the ESP8266 device should be able "to see each other". • The ESP8266 device is only protected by a user id and password. • The new software is not binary-signed. • The connection between the PC and ESP8266 board is not encrypted via SSL.

9.1.3. OTA update examples One good ready-made example to evaluate out the OTA firmware update functionality is the "WEBUpdater.ino" found under: "Examples -> ESP8266HTTPUpdateServer". Once compiled, upload it to the ESP8266 board via the serial interface as usual. You always need to upload the OTA-enabled software for the first time to use the functionality.

● 98

Home Appliance Hack and IoT Guidebook - UK.indd 98

13-12-2022 11:08

Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA)

9.1.3.1. Firmware build From here on, it should be possible to update the software via a web browser. First, however, the firmware to be updated needs to be located. This is where "did the Arduino IDE save the compiled file". When the compilation is complete, the following should be displayed:

Figure 9-2: Windows binary file location. In my case the file location is as follows (on a Windows PC): C:\Users\HANSHE~1\AppData\Local\Temp\arduino_build_355039/WebUpdater.ino.bin

The Arduino build number may vary. On a Linux PC, the location is as shown below:

Figure 9-3: Linux binary file location. /tmp/arduino_build_23323/WebUpdater.ino.bin

If you find it difficult to fetch the "*.bin" file in the "arduino_build" directory, you can make an export to the sketch directory where your "*.ino" file is located. This is done by selecting "Sketch -> Export Compiled Binary" as shown below.

● 99

Home Appliance Hack and IoT Guidebook - UK.indd 99

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 9-4: Export Compile Binary firmware.

9.1.3.2. Firmware update The following figures show how the firmware update is done via a web browser with the "WEBUpdater.ino" as an example. After the first upload of the OTA enabled software, you should see something like this:

Figure 9-5: WEBUpdater output during the boot process. If you want to do an OTA update, enter the shown URL in your favorite web browser. Enter "http://webupdate.local/update" in your web browser to view the figure below:

Figure 9-6: Firmware / Filesystem update web page. My ESP8266 board and the updated PC are not on the same LAN; that's why the URL is an :.

● 100

Home Appliance Hack and IoT Guidebook - UK.indd 100

13-12-2022 11:08

Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA)

Press Firmware: "Browse" and select the file with the new firmware.

Figure 9-7: Firmware update web pager with the selected firmware file. Press "Update Firmware" and the following is hopefully displayed in your web browser after a while.

Figure 9-8: Firmware update success information. If the ESP8266 device is connected to the PC via the serial terminal, you should be able to follow the update, depending on how informative your firmware is. Note The screendump in Figure 9-8 contains the following HTML code:

Update Success! Rebooting...

Figure 9-9: Content of the Update success html page. What will happen is that after 15 seconds the browser will request a refresh with "URL=/" which is "http://webupdate.local" – or an IP-address in my case. If the ESP8266 firmware does not react to "http://webupdate.local/", the web browser will continue requesting the shown URL. Several of my ESP8266 firmware web interfaces are accessed via http://.html page. The IKEA hack is one of them: http:///NEOPixel_new_20210228.html

● 101

Home Appliance Hack and IoT Guidebook - UK.indd 101

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

So what you see in Figure 9-8 will never be updated. To fix this you need to make a change in the file: ESP8266HTTPUpdateServer-impl.h The file is located in the following (Linux or Windows) location: \packages\esp8266\hardware\esp8266\2.6.3\libraries\ESP8266HTTPUpdateServer\ src\ from where the preferences.txt is located, which can be seen in the following figure:

Figure 9-10: Location of "preferences.txt". In my case, the full (Windows) path reads: c:\Users\Hans Henrik\AppData\Local\ Arduino15\\packages\esp8266\hardware\ esp8266\2.6.3\libraries\ESP8266HTTPUpdateServer\src\

Once located, load the "ESP8266HTTPUpdateServer-impl.h" file in your preferred editor and look for the code shown in Figure 9-11. static const char successResponse[] PROGMEM = "Update Success! Rebooting...";

Figure 9-11: ESP8266HTTPUpdateServer-impl.h change. In Figure 9-11 I already added the "update" to the URL so the firmware update page (as shown in Figure 9-7) is correctly loaded after the OTA update. Within my software, the update page is stored in the variable: const char* update_path

as shown in section 9.1.2. Remember to always synchronize the variable and the "ESP8266HTTPUpdateServer-impl.h"-file if you want the update page to load after each OTA update.

● 102

Home Appliance Hack and IoT Guidebook - UK.indd 102

13-12-2022 11:08

Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA)

Depending on what you did in section 9.1.1. when you configured the firmware to include debug information, you will see something like the following on the serial terminal while uploading new firmware. SDK:2.2.2-dev(38a443e)/Core:2.6.3=20603000/lwIP:STABLE-2_1_2_RELEASE/ glue:1.2-16-ge23a07e/BearSSL:89454af Booting Sketch... Setup started scandone scandone state: 0 -> 2 (b0) state: 2 -> 3 (0) state: 3 -> 5 (10) add 0 aid 2 cnt connected with Area51, channel 12 dhcp client start... ip:192.168.3.103,mask:255.255.255.0,gw:192.168.3.1 HTTPUpdateServer ready! Open http://webupdate.local/update in your browser

Figure 9-12: Serial readout during OTA update. Debug: HTTP_UPDATE. sleep disable [begin] roundedSize:

0x001AD000 (1757184)

[begin] updateEndAddress:

0x00200000 (2097152)

[begin] currentSketchSize: 0x00052000 (335872) [begin] _startAddress:

0x00053000 (339968)

[begin] _currentAddress:

0x00053000 (339968)

[begin] _size:

0x001AD000 (1757184)

Header: 0xE9 1 2 90 Staged: address:0x00053000, size:0x00051A10 state: 5 -> 0 (0) rm 0 del if0 bcn 0 del if1 usl !⸮⸮Lr⸮D⸮⸮⸮ SDK:2.2.2-dev(38a443e)/Core:2.6.3=20603000/lwIP:STABLE-2_1_2_RELEASE/ glue:1.2-16-ge23a07e/BearSSL:89454af Booting Sketch... Setup started

● 103

Home Appliance Hack and IoT Guidebook - UK.indd 103

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

scandone scandone state: 0 -> 2 (b0) state: 2 -> 3 (0) state: 3 -> 5 (10) add 0 aid 2 cnt connected with Area51, channel 12 dhcp client start... ip:192.168.3.103,mask:255.255.255.0,gw:192.168.3.1 HTTPUpdateServer ready! Open http://webupdate.local/update in your browser

Figure 9-13: Serial readout during OTA-update. Debug: UPDATER. Remember to remove debug information from the software by reversing what was done in Figure 9-1. The reason for this is that it includes a lot of additional code, which is not needed during daily use.

9.1.3.3. Filesystem build If your firmware includes a filesystem, like the "Refrigerator control" and "Greenhouse thermostat", a method to update this is also required. Before you can upload any new filesystem to the ESP8266 board, you need to generate the "*.bin" file that will be uploaded. There are at least two ways to generate a "*.bin" file of your filesystem to be uploaded by OTA. "Arduino IDE" filesystem-file generation The first is to simply use the Arduino IDE. Select "Tools -> ESP8266 Sketch Data Upload" as shown below:

● 104

Home Appliance Hack and IoT Guidebook - UK.indd 104

13-12-2022 11:08

Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA)

Figure 9-14: ESP8266 Sketch Data Upload. Note Do it without any devices connected to the PC. This will generate an error but the file will be created and located in the "arduino_build" directory as shown below:

Figure 9-15: Windows 10 spiffs.bin file location. On a Windows PC, the location of the just generated "*.bin" is as follows: C:\Users\HANSHE~1\AppData\Local\Temp\arduino_build_78606/New_IKEAHack_ wOTA_20210228_1.spiffs.bin

Manual filesystem-file generation The program on a Windows PC that generates the filesystem "*.bin" upload file is: mkspiffs.exe

This file can be found here: C:\Users\Hans Henrik\AppData\Local\Arduino15\packages\esp8266\tools\ mkspiffs\2.5.0-4-b40a506\

You have to figure out where this is on your PC because the version numbers may have changed since the writing of this book. For convenience I copied the mkspiffs.exe program into the directory where I keep the "*.ino" file and then issued the following command in a terminal window:

● 105

Home Appliance Hack and IoT Guidebook - UK.indd 105

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

D:\New_IKEAHack_wOTA> mkspiffs -p 256 -b 4096 -s 0x0FB000 -c data/ spiffs.bin

Figure 9-16: mkspiffs program execution. As shown in Figure 9-16, the files included in your filesystem will be listed during the creation of the upload file.

9.1.3.4. Filesystem update Once you are ready to upload the Flash file system to your Wemos/LOLIN board, start a browser and enter your device URL as shown below.

Figure 9-17: Firmware / Filesystem update web page In the following, I will show how the filesystem update is done via a web browser with the "IKEA hack" as an example. Note I strongly recommend turning on the option for system debug information. Remember, it takes some time to update the filesystem. By turning on debug information, the process can be followed as shown below. pm open,type:2 0 [begin] Update Filesystem. sleep disable [begin] _startAddress:

0x00200000 (2097152)

[begin] _currentAddress:

0x00200000 (2097152)

[begin] _size:

0x00DFA000 (14655488)

Figure 9-18: Filesystem update debug output from the ESP8226 board.

● 106

Home Appliance Hack and IoT Guidebook - UK.indd 106

13-12-2022 11:08

Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA)

Press Filesystem: "Browse" and select the file with the new filesystem. Press "Update Firmware" and the following will hopefully be displayed in your web browser.

Figure 9-19: Filesystem update success information. If the ESP8266 device is connected to your PC via the serial terminal, you should be able to follow the update, depending on how informative your firmware is. If you have made the changes to the file, ESP8266HTTPUpdateServer-impl.h as mentioned in section 9.1.3.2., the update page shown in Figure 9-17 should reappear after 15 seconds. If something goes wrong, you can always clear the complete flash at the Wemos/LOLIN board via the Arduino IDE.

9.1.3.5. OTA update with password protection When I made the OTA-enabled software, I included the option to password protect the update page. This is achieved with the following declaration within the code: httpUpdater.setup(&httpServer, update_path, update_username, update_password); // OTA

The two variables of interest are declared as follows: const char* update_username = "admin"; const char* update_password = "admin";

So when you enter http://webupdate.local/update in your favorite Internet browser, the following will show up:

● 107

Home Appliance Hack and IoT Guidebook - UK.indd 107

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 9-20: Firmware update password protection. Enter the username and password as hardcoded in your firmware.

9.1.4. CLI OTA The files generated in the above sections can be sent to the ESP8266 device via a CURL command, as mentioned in the WEBUpdater.ino example. (Examples -> ESP8266HTTPUpdateServer->WebUpdater) In the WEBUpdater.ino example, the following can be seen at the top: To upload through terminal you can use: curl -F "[email protected]" esp8266webupdate.local/update

Figure 9-21: CURL OTA update. Before you continue, be sure to verify that "curl" is installed on your PC. At the time of writing, "curl" is included with Windows 10 and Linux by default. If you are running an old Windows 10 or Windows 8 or even older, you have to install it yourself. Look on the Internet for installation guides - they are quite good. If you open a terminal window and execute the following command, c:\Users\Hans Henrik>curl –V

the present "curl" version should be displayed if "curl" is available:

Figure 9-22: curl version. A similar result can be seen on a Linux system.

● 108

Home Appliance Hack and IoT Guidebook - UK.indd 108

13-12-2022 11:08

Chapter 9 • ESP8266 Over The Air – Update/Programming (OTA)

As an example of how to upload firmware via curl, the firmware for the IKEA hack will be used. If the command from Figure 9-21 is executed like this: c:\Users\Hans Henrik> curl -F "image=@New_IKEAHack_wOTA_20210228_1.ino.d1_mini. bin" "172.16.0.11:8148/firmware"

Nothing happens because the system is password protected. So a way to pass the password and user id to the system is needed. The following needs to be added: -u user:password

Instead, the following command should be executed: c:\Users\Hans Henrik> curl -F image=@New_IKEAHack_wOTA_20210228_1.ino.d1_mini.bin –u admin:admin "172.16.0.11:8148/firmware"

The result can be seen below:

Figure 9-23: curl firmware update cmd. By using this command, automated updates can be performed or a bulk update of several devices.

● 109

Home Appliance Hack and IoT Guidebook - UK.indd 109

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 10 • ESP8266 WiFi Antenna The Wemos D1 mini pro-PCB comes with a connector permitting an external antenna to be connected.

Figure 10-1: Zero-ohm resistor to relocate. Close to the external connector, you will find a zero-ohm resistor. In Figure 10-1, the resistor is indicated by the arrow.

Figure 10-2: Built-in antenna enabled. By default, the internal antenna is used. Figure 10-2 shows how the radio signal is guided to the internal antenna. To switch the radio signal so that it reaches the external antenna adaptor, the zero-ohm resistor needs to be rotated. In Figure 10-3, the rotation has been done and the radio signal is now fed to the external antenna socket.

● 110

Home Appliance Hack and IoT Guidebook - UK.indd 110

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

Figure 10-3: External antenna enabled. If you want to use the external antenna adaptor, you can change the connection by rotating or removing the resistor and simply soldering the two pads together, as long as you don't introduce a resistance as it will weaken the signal being transmitted. If you want to make the change, you should take some measurements before and after. To do this, you can use the example "WiFiScan" found under ESP8266WiFi. Every 5 seconds, the program will show acceptable access points in the area together with their RSSI (Received Signal Strength Indication). See the following image: scan start scan done 4 networks found 1: 1426238 (-90)* 2: AhMePer9001 (-65)* 3: ESP_3C8C85 (-65) 4: Area51 (-75)*

Figure 10-4: WiFiScan output. You should run the program before and after modification of your antenna design so that the values can be compared. Note Although not the correct way of measuring any improvement, you have an indication of how much has been gained.

10.1. Dimensions for external antennas In the following, I will show how to attach an external antenna to your ESP8266 device, either ready-made or DIY. Most commonly, a built-in ceramic antenna should be sufficient, but there may be situations where additional antenna gain is required or the device has been located where an external antenna is needed.

● 111

Home Appliance Hack and IoT Guidebook - UK.indd 111

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

So far, I have not had this requirement, but have nonetheless tried out several designs simply to gain knowledge. One final warning before getting technical: You may think it is a promising idea to add a small switch so you can easily switch between the built-in and external antenna. This may work, but it may not as you are switching a 2.4 GHz signal. I imagine that in most cases it will not work as the signal may "leak" and disappear before reaching the antenna. When looking for an external antenna, please note the WiFi frequencies used are 2.4 GHz. There are newer WiFi standards that use 5-GHz frequencies or higher but not in this design. When designing an antenna, you need to know the wavelength of the signal you design the antenna for. The wavelength of the WiFi signal can be calculated using the following formula: speed of light [m/s] / frequency [Hz] = wavelength [m] The speed of light is: 299,792,458 m/s This gives a wavelength of 0.1249 m = 12.49 cm This value is important to know because the antennas are built to sizes that are a fraction of the wavelength. For example, 1/4 (3.12 cm) or 1/2 (6.25 cm) of the wavelength. The final item is a way of verifying your new external antenna. This can be done using the following program: Examples -> ESP8266WiFiWiFiScan The extended WiFiScan program mentioned earlier can also be used. It will show you a list of available access points and their "Received signal strength indication" (RSSI). This is not a very scientific approach but will give you an indication of your new external antenna. With all this in mind, let's look at some antennas.

● 112

Home Appliance Hack and IoT Guidebook - UK.indd 112

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

10.2. Ready-made antennas If you look at all the popular (South East Asian-marked) places to find electronics, you may find examples of antennas, which can be used together with the Wemos D1 mini pro. The only requirements are: • Built for WiFi frequency of 2.4 GHz • Connector type Female I-PEX MHF You will most likely be able to find the exact connectors in laptops, cellphones, or tablets. See the next section for salvaged antennas. An example of a ready-made antenna can be seen below.

Figure 10-5: External antenna seen at aliexpress.com. As indicated in the above picture, you may need an adaptor as most standard antennas come with an "SMA" connector. Note In case you want to build your own adaptors or antennas, pay attention to the difference between SMA and RP-SMA connectors. I will not go over the topic here since the Internet has the answers if you search for "What is the difference between SMA and RP-SMA?"

10.3. Salvaged antennas Over time, I have salvaged WiFi antennas from old PCs and routers, which are good fits if an external antenna is required there and then. Since most of my equipment is rather old, it typically has antennas made for the 2.4 GHz frequency area. It can be time-consuming to salvage antennas and sometimes you will be disappointed as shown below.

● 113

Home Appliance Hack and IoT Guidebook - UK.indd 113

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Router antennas The first one is common and will be inside most of the dipole-style antennas you will find for sale.

Figure 10-6: Salvaged ¼ dipole antenna from an old router. This is taken from a similar router shown in Figure 10-9. It is a so-called ¼ - wave dipole antenna, which I will show you how to build in the next section. Unfortunately, the wire is slightly bent and is therefore unusable. The next antenna is also from a router.

Figure 10-7: 2.4 GHz antenna salvaged from an old router. In this case, the antenna "exists" on a small PCB. With the right skills, it is also possible to make such an antenna yourself, but I have not yet tried it.

● 114

Home Appliance Hack and IoT Guidebook - UK.indd 114

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

Below is another example of a familiar PCB antenna design:

Figure 10-8: Example of PCB antennas. In Figure 10-6 and Figure 10-7, you will notice the I-PEX connector is present in both cases. It makes life much easier to recoup antennas, which are ready for use with the right connector. In the next figure, the antenna is still attached to the router.

Figure 10-9: Internals of a router ready to donate its WiFi antenna. This is because even though there is a socket on the PCB, the antenna is soldered to the PCB as shown in Figure 10-10. I only kept it because I am using it when I teach about the ESP8266.

● 115

Home Appliance Hack and IoT Guidebook - UK.indd 115

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 10-10: WiFi antenna soldered to the PCB. Remember to use WiFiScan programs to measure your new antenna's performance.

10.4. DIY antenna You can rightly argue: "why make your own WiFi antennas, with all these cheap readymade antennas available". Simply because I wanted to see how difficult it was. I also need to emphasize that this design is based on what was available and could be improved in many ways. Before you start, you need to be fully accurate and careful with your work to make improvements compared to the built-in ceramic antenna... and readily available antennas. For the cables used in this experiment, I bought 20 "IPX IPEX u.fl Female 1.13mm Connector Cable Single-head Adapter Connector 15cm IPX 1.13 Cable" from aliexpress.com.

● 116

Home Appliance Hack and IoT Guidebook - UK.indd 116

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

Figure 10-11: Antenna cable. I will try to make a similar antenna like the one shown in Figure 10-6, which can be made out of the cable itself. With the figures calculated at the beginning of this section, you need to cut the cable as shown below:

Figure 10-12: DIY 1/4–wave dipole description. First, cut 3.12 cm of the plastic shielding as shown below.

● 117

Home Appliance Hack and IoT Guidebook - UK.indd 117

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 10-13: DIY ¼-wave dipole with plastic shield cut off. The metal shield is now exposed. This needs to be carefully debraided from the inner plastic of the cable and twisted around the outer plastic as shown below:

Figure 10-14: DIY ¼-wave dipole metal shield twisted around the cable. Hopefully, you now have a simple ¼-wave dipole antenna. Unfortunately, I do not have any kind of antenna test equipment so instead, I am using the WifiScan program to see if there are any improvements. The easiest way is to place the antenna at a fixed location and then establish how many WiFi access points can be "seen".

● 118

Home Appliance Hack and IoT Guidebook - UK.indd 118

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

Do the same with a Wemos PCB using the internal antenna. After several uses of the WifiScan program and adjustments to the metal shield, I saw an improvement in the RSSI (Received Signal Strength Indication) dropping from 53 to 47. Note RSSI is a term used to measure the relative quality of a received signal to a client device (the ESP8266) but has no absolute value. The RSSI value also varies greatly between chipset manufacturers. Therefore, it will only indicate if your design is heading in the right direction. This was a very quick and cheap DIY antenna and it is up to you and your skills as to how good it will be.

10.5. DIY Yagi antenna (2.4 GHz) The antenna description mentioned in section 10.4. is what Elektor calls a "Poor Man's External 2.4 GHz Wi-Fi Antenna". Indeed, it is cheap, quick to make, and it works. If you want more gain, a solution could be a Yagi antenna. Fortunately enough there are already constructions available on the Internet. I have chosen to follow this: https://www.ab9il.net/wlan-projects/wifi6.html The link describes how to make a DIY Yagi antenna out of (almost) regular household materials. You need to be able to do some drilling cutting and bending of wires, but you should be able to get a usable result. First, you need to find the dimensions of your antenna. To do this a small jar file has been made, which can be found here: http://fermi.la.asu.edu/w9cf/yagipub/index.html The easiest way to run the .jar file is to do it from a command line. This can be done in Windows and Linux – java has to be installed. In the terminal window, type the following command: java -jar yagi.jar If everything goes well you should see the following:

● 119

Home Appliance Hack and IoT Guidebook - UK.indd 119

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 10-15: Java antenna design program. I'm using 230-VAC mains rated copper wire with 1.7-mm diameter. The following describes how you can start out making your calculations. When you are more familiar with the .jar file you can make adjustments. • Start with the 15 or 20 elements "K1FO 70cm" example. (found under the "Examples" menu) • In the "Units" menu, select "Radians". • In the "Conductivity" menu, select "Copper" • In the "Frequency" field, enter 2450 (MHz) for the center of the wi-fi networking band. • For "Element Diameter", enter 0.08729 (radians). • Click the "Calculate" button. • In the "Units" menu, select "Millimeters." • Note that the element diameter perfectly matches 14 gauge wires! • In the "File" menu, select "list elements."

● 120

Home Appliance Hack and IoT Guidebook - UK.indd 120

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

Figure 10-16: Java antenna design program with calculations. Once finished, you should have a listing like the one shown below: Element

Length (mm)

Position (mm)

1 (Reflector)

59.61

0.00

2 (Driven Element)

58.55

18.34

3 (Director)

55.20

25.75

4 (Director)

53.62

39.51

5 (Director)

52.38

58.55

6 (Director)

51.68

82.19

7 (Director)

50.97

109.70

8 (Director)

50.62

140.74

9 (Director)

50.26

174.60

10 (Director)

49.91

210.94

11 (Director)

49.56

249.38

12 (Director)

49.21

289.60

13 (Director)

48.85

331.39

14 (Director)

48.68

374.25

15 (Director)

48.50

418.52

Table 10-1: Yagi antenna elements.

● 121

Home Appliance Hack and IoT Guidebook - UK.indd 121

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

All positions are relative to the reflector in position 1. The driven element is number 2 and marked by green in Figure 10-16. The driven element should have the following dimensions:

Figure 10-17: Driven element. With the calculated data, you should now be able to build the actual antenna as described in the original URL.

10.5.1. Mechanics As I wanted to have several antennas for test purposes, I made a 3D drawing of the boom, which can be seen below:

Figure 10-18: 3D antenna boom with bracket. The 3D drawing is made in OpenSCAD, so it is possible to adapt the boom to fit your 3D printer. A small bracket to hold the antenna is also available. I have initially calculated the values for a 15-element antenna. The calculated values are placed in an array: Elements

=

[ 0.0, //

1) Reflector

18.3, //

2) Driven Element

25.8, //

3) Director

39.5, //

4) Director

58.6, //

5) Director

82.2, //

6) Director

109.7, //

7) Director

140.7, //

8) Director

● 122

Home Appliance Hack and IoT Guidebook - UK.indd 122

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

174.6, //

9) Director

210.9, // 10) Director 249.4, // 11) Director 289.6, // 12) Director 331.4, // 13) Director 374.3, // 14) Director 418.5 ];

// 15) Director

// 15 elements

Figure 10-19: Yagi element locations. The length and height of the antenna and width of the boom can be adjusted to your needs with the variables: Length

= 245;// Length of beam.

AntennaHW = 8;

// Height and Width of beam.

For convenience, there is also a small guide for making the driven element:

Figure 10-20: Guide for the driven element. You place the guide across the boom near the second hole in the boom. Cut approximately 123 mm of (copper) wire and place it in the hole and bend as shown below.

● 123

Home Appliance Hack and IoT Guidebook - UK.indd 123

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 10-21: Driven element guide in place. Note If you change the height and width of the boom adjust the driven element guide as well. Now cut the rest of the elements to size as calculated and place them in the appropriate holes. Pay attention to having all the elements centered in the boom and aligned. Once finished it should look like what is shown below.

Figure 10-22: Yagi antenna without coax. To feed the signal from the Wemos antenna connector, some kind of cable is needed. The picture below shows how it should be done.

● 124

Home Appliance Hack and IoT Guidebook - UK.indd 124

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

Figure 10-23: Yagi antenna cable connection. Modify the coax cable so you have two "connections": • the inner cobber core and • the woven copper shield. In my case, the inner core was soldered to "A" and the woven shield was soldered to "B". You may change them as you like. Pay attention not to solder the woven shield together with the inner core. Below is how I terminated the coax cable.

Figure 10-24: Driven element coax cable termination.

● 125

Home Appliance Hack and IoT Guidebook - UK.indd 125

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

If you find it too difficult to either follow the original construction details or the one presented here, just search the Internet for a "wifi yagi" antenna. Figure 10-25 shows what my Yagi prototypes s11-magnitude look like. As expected there is a dip around 2.4 GHz – but there is room for improvement.

Figure 10-25: Yagi s11-magnitude.

10.6. Antenna measurements To compare the different external antennas more easily, I have extended the Arduino ESP8266 WiFiScan example. The extended WiFiScan program has an added "history" and some more read-outs to it. The WiFiScan_Extended programs readout can be seen below: scan start scan done 5 networks found on last scan ESP8266 Chip id = 006E3F4A WiFi MAC = 5C:CF:7F:6E:3F:4A ---- RSSI ---Last

Average MAC address

Chan SSID

(-28)u

(-29)

8C:04:FF:01:FB:72

1

(-87)

(-87)

84:17:EF:9A:55:58

1

(-95)u

(-94)

A0:63:91:BF:DE:98

6

(-49)u

(-52)

60:E3:27:58:34:AE

11

(-79)u

(-78)

00:23:69:1A:35:13

12

(-92)u

(-92)

CC:40:D0:10:C6:5A

11

(-90)

(-90)

00:1E:80:CC:C1:A4

6

(-97)

(-97)

18:E8:29:AA:57:AF

11

Total networks found: 8

Figure 10-26: Extended WiFiScan program readout.

● 126

Home Appliance Hack and IoT Guidebook - UK.indd 126

13-12-2022 11:08

Chapter 10 • ESP8266 WiFi Antenna

The program keeps track of access points found and the data about each one of them. The "u" after the RSSI value indicates that the SSID was seen during the last scan and the RSSI value has been updated. You will also see which channels the access points are using. As shown above, improvements could be made if channels 1, 6, 11, and 12 are not used in your setup.

10.7. Further studying If you would like to learn more, you should take a look at all the descriptions available over the Internet. Here are a few I came across after a 5-minute search: All About Omnidirectional Wi-Fi Antennas: https://www.allaboutcircuits.com/projects/all-about-omnidirectional-wi-fi-antennas/ DIY Cheap Internal WiFi Antenna https://fabiobaltieri.com/2012/06/02/diy-cheap-internal-wifi-antenna/ Selecting Antennas for Embedded Designs https://www.digikey.com/en/articles/techzone/2012/nov/selecting-antennas-forembedded-designs How to Design a PCB Antenna for 2.4GHz: https://circuitdigest.com/article/how-to-design-a-pcb-antenna-for-24ghz AN1088: Designing with an Inverted-F 2.4 GHz PCB Antenna: https://www.silabs.com/documents/public/application-notes/an1088-designing-withpcb-antenna.pdf AN5129 Low cost PCB antenna for 2.4 GHz radio: meander design for STM32WB Series https://www.st.com/content/ccc/resource/technical/document/application_note/ group0/ce/a6/df/76/3e/71/45/b2/DM00470410/files/DM00470410.pdf/jcr:content/ translations/en.DM00470410.pdf AN043: Small Size 2.4 GHz PCB antenna https://www.ti.com/lit/pdf/swra117?keyMatch=AN043 Simply search for: "pcb wifi antenna design 2.4GHz" or "DIY wi-fi 1/2 dipole" You should be able to find descriptions from some of the larger companies together with designs from skillful radio amateurs. I get no benefit from mentioning the above application notes.

● 127

Home Appliance Hack and IoT Guidebook - UK.indd 127

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 11 • ESP8266 Housing When designing electronics, you need regularly need nice enclosures or housings to give the project the finishing touch. My housing design is amazingly simple but fulfills the main purpose: protecting the electronics from its surroundings. All ESP8266 housing designs are made of simple boxes with holes for wires and LEDs. They all include a bottom and top part – the "lid" if you like. By changing the value of the following variables: TOP

= 1; // possible values [0,1]

BOTTOM = 1; // possible values [0,1]

the two parts can be separated and so can be printed individually. This section contains descriptions of the housing made for most of the builds in this book.

11.1. LOLIN D1 mini standard housing This housing is designed for the LOLIN D1 mini: https://www.wemos.cc/en/latest/d1/d1_mini.html The housing also fits the LOLIN D1 mini pro-V1.0.0 and V1.1.0 but not V2.0.0. There you need the housing shown in section 11.2. The LOLIN D1 mini housing is shown below.

Figure 11-1: LOLIN D1 mini housing. The two parts are made to fit together without the need for screws.

● 128

Home Appliance Hack and IoT Guidebook - UK.indd 128

13-12-2022 11:08

Chapter 11 • ESP8266 Housing

The basic parts are: • Box fastener • USB cable mounts Depending on your needs, additional holes for • External wire • Reset • Antenna • LED can be made. Varioust possibilities can • SENSOR = 1; • RESET = 1; • ANTENNA = 1; • LED = 1;

be changed with the following variables: // possible values [0,1] // possible values [0,1] // possible values [0,1] // possible values [0,1]

I have added a few more variables to control the design: • POS = 50; // Distance between top and bottom • FASTENER = 0; // possible values [0,1] • TOP = 1; // possible values [0,1] • BOTTOM = 1; // possible values [0,1] A description of the variables can be found in the "scad" file. One example of the housing can be seen in Figure 20-3. If you want to add a D1 mini shield on top of the LOLIN PCB, increase the following variable: • height = thickness + 13; // height inside the box to suit your needs.

11.2. LOLIN D1 mini pro-standard housing This housing is designed for the LOLIN D1 mini pro V2.0.0: https://www.wemos.cc/en/latest/d1/d1_mini_pro.html The LOLIN D1 mini pro-V2.0.0 housing is shown below.

● 129

Home Appliance Hack and IoT Guidebook - UK.indd 129

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 11-2: LOLIN D1 mini pro-V2.0.0 housing. The basic parts are: • Box fastener • USB cable mounts Depending on your needs additional holes for • External wire • Reset • Antenna • LED • Battery can be made. The diverse options can • SENSOR • RESET • ANTENNA • LED • USB_SUPPORT • BATTERY

be changed with the following variables: = 1; // possible values [0,1] = 1; // possible values [0,1] = 1; // possible values [0,1] = 1; // possible values [0,1] = 1; // possible values [0,1] = 1; // possible values [0,1]

I added a few more variables • FASTENER = 0; // • TOP = 1; // • BOTTOM = 1; //

to control the design: possible values [0,1] possible values [0,1] possible values [0,1]

● 130

Home Appliance Hack and IoT Guidebook - UK.indd 130

13-12-2022 11:08

Chapter 11 • ESP8266 Housing

If you want to run your design on a battery alone, you can remove the USB cable mount with the USB_SUPPORT variable. If USB_SUPPORT = 0 then BATTERY equals 1. If not, the PCB will have no supply power. If you want to add a D1 mini shield on top of the LOLIN PCB, increase the following variable • height = thickness + 13; // height inside the box to suit your needs.

11.3. LOLIN D1 mini door-lock housing Based on the housing in section 11.1., a special variant of the LOLIN D1 mini housing has been made for the door lock construction. The housing is shown below.

Figure 11-3: Door lock housing. As for the standard LOLIN D1 mini housing, the basic parts are: • Box fastener • USB cables mount Depending on your needs additional holes for • Reset • Antenna • Power LED • LED1 – LED4. can be made.

● 131

Home Appliance Hack and IoT Guidebook - UK.indd 131

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Diverse options can be changed • PWRLED = 1; // 1: • LED1 = 1; // 1: • LED2 = 1; // 1: • LED3 = 1; // 1: • LED4 = 1; // 1: • ANTENNA = 1; // 1: • RESET = 1; // 1:

with the following variables: print LED hole 0: np LED hole print LED hole 0: np LED hole print LED hole 0: np LED hole print LED hole 0: np LED hole print LED hole 0: np LED hole print antenna hole 0: np antenna hole print reset hole 0: np reset hole

11.4. LOLIN D1 mini refrigerator control housing Based on the housing in section 11.1., a special variant of the LOLIN D1 mini housing has been made for the refrigerator control. The housing is shown below.

Figure 11-4: Refrigerator control housing. The basic • • • •

parts for the refrigerator control housing are: Box fastener USB cables mount Temp sensor hole SSR hole

Depending on your needs, additional holes can be made: • Reset. • Power LED • LED1. Diverse options can be changed • PWRLED = 1; // 1: • LED1 = 1; // 1: • RESET = 1; // 1:

with the following variables: print LED hole 0: np LED hole print LED hole 0: np LED hole print reset hole 0: np reset hole

The box can be seen in Figure 17-24 and Figure 17-25.

● 132

Home Appliance Hack and IoT Guidebook - UK.indd 132

13-12-2022 11:08

Chapter 11 • ESP8266 Housing

11.5. LOLIN D1 mini energy sensor housing The PCB (Figure 16-5) for this construction will not fit any of the standard boxes mentioned earlier in chapter 11. A distinctive design has been made instead, which can be seen below.

Figure 11-5: Energy sensor housing. The energy sensor housing has been designed with the same options as with the standard boxes mentioned earlier in chapter 11. That is, the following can be configured: • Hole for the antenna wire • Hole to activate reset • USB cables mount • Battery support • Hole for LED • With or without box fastener Assorted options can be changed • ANTENNA = 1; • ANTENNA_HOLE = 1.5; • RESET = 1; • USB_SUPPORT = 1; • BATTERY = 1; • LED = 1; • FASTENER = 1;

with the following variables: // 1: Antenna hole 0: no Antenna hole // Radius of the antenna hole. // 1: Reset hole 0: no Reset hole // 1: USB support 0: no USB support // 1: battery hole 0: no battery hole // 1: LED hole 0: no LED hole // 1: fastener 0: no fastener hole

Beside the above, by default, the S0 interface wires have holes.

11.6. LOLIN D1 mini IKEA Trådfri controller housing The PCB (See Figure 24-19) for this construction will not fit any of the standard boxes mentioned earlier in chapter 11. A distinctive design has been made instead, which can be seen here.

● 133

Home Appliance Hack and IoT Guidebook - UK.indd 133

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 11-6: IKEA Trådfri controller housing bottom.

Figure 11-7: IKEA Trådfri controller housing top. The IKEA Trådfri controller housing has been designed with the same options as with the standard boxes mentioned earlier in section 11. That is, the following can be configured: • Hole for the antenna wire • Hole to activate reset • USB cables mount • Hole for all the LEDs • With or without box fastener • ZigBee holes if they don't fit inside the housing. The diverse options can be • ANTENNA = • ANTENNA_HOLE = • RESET = • USB_SUPPORT = • LED =

changed through the following variables: 1; // 1: print Antenna hole 0: np Antenna hole 1.5;// Radius of the antenna hole. 1; // 1: print Reset hole 0: np Reset hole 1; // 1: print USB support 0: np USB support 1; // 1: print LED hole 0: np LED hole

● 134

Home Appliance Hack and IoT Guidebook - UK.indd 134

13-12-2022 11:08

Chapter 11 • ESP8266 Housing

• • • • •

LED_HOLE FASTENER TOP BOTTOM ZIGBEE

= = = = =

1.7;// 1; // 1; // 1; // 1; //

Radius of the LED holes. 1: print fastener 0: np fastener hole 1: print top 0: do not print top 1: print bottom 0: do not print bottom Holes for the ZigBee modules.

Beside the above, it is default to have holes for external connections and USB power. During the prototyping period, I needed some additional headroom for the ZigBee modules so I introduced the ZigBee holes. If not required, simply remove them with the variable "ZIGBEE". I'm using 3-mm diameter LEDs and the PCB has been consequently. If you use larger LEDs, increase the variable "LED_HOLE" to increase the radius of the holes. If larger LEDs are used, pay attention to the space around the ON/OFF LEDs. Just to be sure, a hole for an external antenna has been added. In most cases, an external antenna will not be necessary. If you want to power the construction via the Wemos/LOLIN PCB, you have to make a hole. Below is one of the housing prototypes.

Figure 11-8; IKEA Trådfri controller housing without lid.

● 135

Home Appliance Hack and IoT Guidebook - UK.indd 135

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 11-9: IKEA Trådfri controller housing with the lid on.

● 136

Home Appliance Hack and IoT Guidebook - UK.indd 136

13-12-2022 11:08

Chapter 12 • VPN Gateway

Chapter 12 • VPN Gateway If you, like me, have devices placed outside your home, it may be a challenge to get (temperature-related) data back to, say, the emoncms-server. One way of getting data back could be to make a NAT configuration on your firewall or by setting up a VPN gateway. I have a pfsense firewall at home with OpenVPN installed, so it is remarkably simple to set up a Raspberry Pi to function as a remote VPN gateway. Most of what I will go through in the following is found on the Internet – and I haven't recorded where. Search for "RPI VPN-gateway". Note To get this working, your firewall needs to have a static, public IP address.

Figure 12-1: VPN gateway overview. The above figure gives an overview of the way the VPN gateway is going to be used. Under normal circumstances, the Wemos boards employ the WiFi router to access the internet, but to reach the local LAN the Raspberry Pi VPN-Gateway will function as a routing device to the Internet.

● 137

Home Appliance Hack and IoT Guidebook - UK.indd 137

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

To get Internet packages from the Wemos boards to the emoncms server, the following occurs: • the wemos boards access the remote LAN via the WiFi router; • enter the RPI VPN-GW via the eth0 interface; • leave the RPI VPN-GW via the (logical) tun0 interface; • move across the Internet to the pfsense firewall; • enters the local LAN; • and finally the emoncms server. In conclusion, a so-called VPN tunnel has been created. The following image shows what the setup will look like from a logical perspective.

Figure 12-2: VPN gateway from a logical perspective. The only thing to remember while designing the Wemos firmware is that from now on the gateway for the Wemos PCBs should be IP address: 192.168.1.123 instead of 192.168.1.1 (as shown in Figure 12-1). The Wemos boards will have an IP address: 172.17.0.2 when entering the "local LAN". With this overview, it's time to configure the RPI VPN gateway.

12.1. Firewall configuration For the RPI VPN gateway to connect to your firewall, you need to generate the OpenVPN configuration file or files. In my case, it is one file. The OpenVPN configuration file will look something like this ".ovpn", which includes the information the RPI VPN gateway needs to make the secure connection.

● 138

Home Appliance Hack and IoT Guidebook - UK.indd 138

13-12-2022 11:08

Chapter 12 • VPN Gateway

Below you can see the structure of the (ASCII) ".ovpn" file. dev tun persist-tun persist-key cipher AES-256-CBC auth SHA1 tls-client client resolv-retry infinite remote 1194 udp verify-x509-name "RPI VPN" name auth-user-pass remote-cert-tls server

-----BEGIN CERTIFICATE----[REMOVED] -----END CERTIFICATE----

-----BEGIN CERTIFICATE----[REMOVED] -----END CERTIFICATE----

-----BEGIN PRIVATE KEY----[REMOVED] -----END PRIVATE KEY----

# # 2048 bit OpenVPN static key # -----BEGIN OpenVPN Static key V1----[REMOVED] -----END OpenVPN Static key V1---- key-direction 1

Figure 12-3: ovpn-config file content. I cannot comment on how you should do it in your case, but on the pfsense firewall you: • add a new user; • generate a certificate for the user; • export the configuration;

● 139

Home Appliance Hack and IoT Guidebook - UK.indd 139

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

You can verify the configuration is working by installing the OpenVPN client application on your PC and setting up a secure connection using the OpenVPN configuration file generated just now. OpenVPN clients can be found here: https://openvpn.net/community-downloads/ I managed to get it working seamlessly on Windows, Linux, and macOS. In case you experience difficulties, there is help to be found on the Internet – this was how I got started.

12.2. Raspberry Pi configuration You are now ready to set up the RPI VPN gateway. You can do it with any kind of Raspberry Pi as long as it has an Ethernet interface. I do however recommend using one of the later versions (Generation 3 or 4). I will run a "headless" version without a keyboard or monitor. My experience is that a keyboard and monitor are only needed in difficult debugging situations. Download the latest Raspberry Pi OS lite from: https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-32-bit and follow the instructions in section 2.2. If you followed the instructions, you should now have a default RPi installation with a new password.

12.2.1. Basic configuration To make life easier later on during the installation of the VPN gateway, you need to take out the SD-card of the RPI and stick it in your PC. Having inserted the SD-card in your PC you should copy your OpenVPN configuration file ".ovpn" onto the SD-card. After the copying, you will make some basic configurations of the RPI once powered up. Connect to the RPi and enter the following command in a terminal window: pi@raspberrypi:~ $ sudo raspi-config

● 140

Home Appliance Hack and IoT Guidebook - UK.indd 140

13-12-2022 11:08

Chapter 12 • VPN Gateway

You should then see the following:

Figure 12-4: raspi-config: System Options. First, change the Hostname: Enter: 1. System Options -> S4 Hostname

Figure 12-5: raspi-config: hostname. Enter a name you like so you can distinguish the RPi from your other installations and press "Ok". For your convenience, you may also enter "5. Localization Options" and change "Local" and "Timezone".

● 141

Home Appliance Hack and IoT Guidebook - UK.indd 141

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

When done press to exit and reboot.

Figure 12-6: raspi-config: reboot. Next, the RPi needs to have a static IP address, otherwise it cannot easily be used as a gateway. This is achieved by editing the file: /etc/dhcpcd.conf This is done by entering the following command within a terminal window: pi@ RPIVPNGateway:~ $ sudo nano /etc/dhcpcd.conf

In this file, you should find a section like the following: #Example static IP configuration: #interface eth0 #static ip_address=192.168.0.10/24 #static ip6_address=fd51:42f8:caae:d92e::ff/64 #static routers=192.168.0.1 #static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1

Figure 12-7: /etc/dhcpd.conf Remove the "#" at the beginning of the line and change the IP addresses so it matches your network. If you don't want to assign IPV6 addresses, you can simply remove them.

● 142

Home Appliance Hack and IoT Guidebook - UK.indd 142

13-12-2022 11:08

Chapter 12 • VPN Gateway

This is what my configuration looks like: # Example static IP configuration: interface eth0 static ip_address=192.168.1.123/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1 8.8.8.8

Figure 12-8: Working /etc/dhcpd.conf When ready, save the changes by pressing -x and Y for yes. Finally, reboot to have the new network configuration take place pi@ RPIVPNGateway:~ $ sudo reboot

If you have "many" (RPI) devices connected to the same network, you may find it easier instead to set address reservations on your DHCP server. This way, each Raspberry Pi will keep the same IP address, but they will all be managed in one place, making reconfiguring your network in the future more straightforward.

12.2.2. OpenVPN installation The RPi is now ready to be turned into a VPN gateway. Before you continue, the RPI needs to be fully upgraded. This is done by the following commands: pi@ RPIVPNGateway:~ $ sudo apt-get update

and pi@ RPIVPNGateway:~ $ sudo apt-get upgrade

Installation of the OpenVPN software is done as follows: pi@ RPIVPNGateway:~ $ sudo apt-get install openvpn

● 143

Home Appliance Hack and IoT Guidebook - UK.indd 143

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 12-9: Install openvpn. Hopefully, it's all done without any errors. Navigate to the folder of the OpenVPN configuration files with the command: pi@ RPIVPNGateway:~ $ cd /etc/openvpn

Copy the OpenVPN configuration file ".ovpn" copied from the "/boot" directory with the following command: pi@ RPIVPNGateway:~ $ sudo cp /boot/.ovpn RPIVPNGateway.ovpn

You can verify it was a success with the following command: pi@ RPIVPNGateway:~ $ ls -al

Figure 12-10: installation success. It is now possible to assess everything is working. This is done with the following command: pi@ RPIVPNGateway:~ $ sudo openvpn -–config RPIVPNGateway.ovpn --daemon

Everything should be entered on one line and notice the double "-" in front of both the "config" and the "daemon" as shown below.

● 144

Home Appliance Hack and IoT Guidebook - UK.indd 144

13-12-2022 11:08

Chapter 12 • VPN Gateway

Figure 12-11: Start openvpn. A successful connection can be verified with the following command: pi@ RPIVPNGateway:~ $ sudo ifconfig

In this listing, there should be a connection marked "tun" as shown below.

Figure 12-12: openvpn tun0. You can also check your firewall to see if a new OpenVPN connection has been made. If the firewall is pfsense, it is shown under "Status / OpenVPN".

12.2.3. OpenVPN configuration The way the OpenVPN gateway was started in the previous section is OK, but you need to have the gateway start automatically when booting. So you need to be able to • Start the OpenVPN application at boot time, • Enter the userid and password automatically.

12.2.3.1. OpenVPN Authorization file You start by resolving the userid & password issue first, which is done by creating an authorization file. In a terminal window go to /etc/openvpn and enter the following command: pi@ RPIVPNGateway:/etc/openvpn $ sudo nano vpngwlogin

This should open a new file called "vpngwlogin". In this file, enter your "userid" in line 1 and "password" in line 2 as shown below:

● 145

Home Appliance Hack and IoT Guidebook - UK.indd 145

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 12-13: openvpn configuration. Press CTRL-x, Y, and "enter" to save. Verify the file has been added with the "ls – al" command. Now open the .ovpn with the following command: pi@ RPIVPNGateway:/etc/openvpn $ sudo nano .ovpn

In there change: "auth-user-pass" to "auth-user-pass /etc/openvpn/vpngwlogin"

Press CTRL-x, Y, and "enter" to save. Note It is important to enter the full path to the "vpngwlogin" file. If you have an OpenVPN connection running, terminate it with the following (crude) command: pi@ RPIVPNGateway:/etc/openvpn $ sudo killaall openvpn

Verify the "tun" connection is gone with "sudo ifconfig". With the earlier issued openvpn, connect command: pi@ RPIVPNGateway:~ $ sudo openvpn -–config RPIVPNGateway.ovpn --daemon

The VPN connection should be established without asking for "userid" and "password".

12.2.3.2. Start at boot time The final thing to do is to establish the OpenVPN connection at boot time.

● 146

Home Appliance Hack and IoT Guidebook - UK.indd 146

13-12-2022 11:08

Chapter 12 • VPN Gateway

This is done by entering the following at the bottom before "exit 0": sleep 5 sudo openvpn --config /etc/openvpn/RPIVPNGateway.ovpn --daemon --keepalive 2 60 exit 0

Figure 12-14: openvpn start at boot time. sudo cmd entered on one line. in the following file: /etc/rc.local. Use the following command: pi@ RPIVPNGateway: $ sudo nano /etc/rc.local

Save the file as usual. The argument "—keepalive 2 60" will "reboot" OpenVPN if the VPN gateway cannot get in contact with the VPN server. Once done with the edit, reboot the gateway: pi@ RPIVPNGateway: $ sudo reboot

and verify the "tun" connection is established when the OpenVPN gateway is up and running. If anything goes wrong during the configuration process, look for information/help in the "/ var/log/syslog" file.

12.2.3.3. Configuration of the VPN gateway (fun with IP tables!) The following can be a true killer and take some time to set up but the following is how my OpenVPN gateway is configured: The default is that the OpenVPN gateway is closed for all traffic going in or out, so you need to open up. Just to be on the safe side, do clear old stuff with the following commands: pi@ RPIVPNGateway: $ sudo iptables -F pi@ RPIVPNGateway: $ sudo iptables -t nat -F pi@ RPIVPNGateway: $ sudo iptables -X

You are flushing all chains, packet matching tables, and all optional user-defined chains.

● 147

Home Appliance Hack and IoT Guidebook - UK.indd 147

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

To open the RPI VPN gateway to the outside world, the following commands have to be executed: pi@ RPIVPNGateway: $ sudo iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE

Send packets via the "tun0" interface and hide that the message is coming from another machine than the Raspberry Pi. pi@ RPIVPNGateway: $ sudo iptables -A FORWARD -i tun0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT

Accept packets coming from tun0 and send them out on eth0 but only on already related and established connections – from eth0 to tun0. If this is not made, packets cannot be returned via the VPN tunnel back to the Wemos board that initiated the original packet. pi@ RPIVPNGateway: $ sudo iptables -A FORWARD -i eth0 -o tun0 -j ACCEPT

Forward all packets received at eth0 to tun0. Each command should be entered on one line. "–A" appends a rule. The entered configuration can be verified with the following command: pi@ RPIVPNGateway: $ sudo iptables -L -v

Below is the output of the command: Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in

out

source

destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in 0

0 ACCEPT all

out

source

destination

--

tun0

etho0 anywhere

anywhere state

--

eth0

tun0

anywhere

RELATED,ESTABLISHED 0

0 ACCEPT all

anywhere

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in

out

source

destination

Figure 12-15: Output of "sudo iptables -L -v" To save work done so far, issue the following command: pi@ RPIVPNGateway: $ sudo sh -c "iptables-save > /etc/iptables/rules.v4"

● 148

Home Appliance Hack and IoT Guidebook - UK.indd 148

13-12-2022 11:08

Chapter 12 • VPN Gateway

If you don't do this, the changes you made to your iptables rules will be scrapped the next time the iptables service gets restarted or you reboot the Raspberry Pi. The above command only saves your iptables entries and does not reload them after the next (re)boot of the RPI. To reload your arduous work, additional software is needed. It is installed as follows: pi@ RPIVPNGateway: $ sudo apt-get install netfilter-persistent iptables-persistent

Figure 12-16: Install netfilter-persistent. During the installation of iptables-persistent, you will be asked to save the present iptables – both IPV4 and IPV6. Please note the locations: IPV4: /etc/iptables/rules.v4 IPV6: /etc/iptables/rules.v6

● 149

Home Appliance Hack and IoT Guidebook - UK.indd 149

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 12-17: Configuration of iptables-persistent. From now on, each time you reboot the RPI VPN gateway, the configuration saved in the two files mentioned above will be reloaded. Each time you make IPTables configurations that you want to save for the future, the above files need to be updated. This can be done in several ways. 1) Update the two files mentioned above with the commands: pi@ RPIVPNGateway: $ sudo sh -c "iptables-save > /etc/iptables/rules.v4" pi@ RPIVPNGateway: $ sudo sh -c "ip6tables-save > /etc/iptables/rules.v6"

2) Run the following command: pi@ RPIVPNGateway: $ sudo netfilter-persistent save

This command also updates the two files but with one common command. 3) Run the following command: pi@ RPIVPNGateway: $ sudo dpkg-reconfigure iptables-persistent

Here you need to answer yes twice as you did when the iptables-persistent application was installed. Once again, the two files will be updated. It is up to you and what you find most convenient.

● 150

Home Appliance Hack and IoT Guidebook - UK.indd 150

13-12-2022 11:08

Chapter 12 • VPN Gateway

Note As long as you don't "save" the configuration as mentioned above, you can always revert to the previous (working) configuration by simply booting your RPI VPN gateway.

12.3. Use of the VPN gateway It is now time to put your RPI VPN gateway into use. This is quite simple but you may need to rewrite your Wemos code a little to get it working. If you, like me, have configured your Wemos board to use DHCP while configuring WiFi you now need to specify it all manually. This is done as follows: Make the following or similar variable definitions: // Enter a unique TCP address for the server IPAddress ip( 192, 168, 1, 62 ); IPAddress gateway( 192, 168, 1, 123 ); IPAddress subnet( 255, 255, 255, 0 );

When getting your WiFi connection up and running, use the following command: WiFi.config(ip, gateway, subnet);

Don't forget to start the WiFi connection with: WiFi.begin(ssid, password);

Instead of using the default gateway of the WiFi router (192.168.1.1), you use the newly created RPI VPN gateway address: 192.168.1.123 The Wemos board is blissfully unaware of the change but you should be able to see your data ending up at the end of your VPN tunnel.

12.3.1. Improvements If you use one of the Raspberry Pi variants that include WiFi support, you can have the RPI VPN gateway function as a WiFi access point for the Wemos board. You only need an Internet connection for the RPI VPN gateway to work. If you have a 4G LTE USB dongle you can make a fully self-contained system as the 4G LTE dongle will provide you with Internet access with the right SIM card and subscription. The only thing required is a power source.

● 151

Home Appliance Hack and IoT Guidebook - UK.indd 151

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 13 • Software/firmware Design The software/firmware presented in this book has been made over several years so there may be differences in structure. In general, I have tried to fill in as many comments as possible with links to where the software was obtained from the Internet. The overall structure is as follows #defines: Pay attention to the DEBUG #define. #includes: Some with a description if not evident what they do. Global variables Will be grouped into sections related to the function they are used in (Network, OneWire, etc). Some could be turned into local variables if needed. Functions They usually have a concise description of what they do. setup() and loop() They are placed at the end so they are easily located. Within the code I have made comments around the code blocks like this: // Init Refrigerator -----------------------------// Init Refrigerator END --------------------------

to indicate what kind of task the firmware is performing. For convenience, my firmware includes information on name and version like: String name

= "\Refrigerator Control ";

String version = "Ver: 2018-06-05:13";

It is much easier to see what's running on an unknown ESP8266 and you can tell which specific version is loaded on the device.

● 152

Home Appliance Hack and IoT Guidebook - UK.indd 152

13-12-2022 11:08

Chapter 13 • Software/firmware Design

You need to start the terminal in the Arduino IDE to see it.

13.1. Use of EEPROM I tend to save a lot of configurations in EEPROM. To see if there is valid information stored, I station 70 or 72 in EEPROM position 0. By checking for the content in position 0, I know if I should read the content or store new information. Note If you saved 70 or 72 in EEPROM position 0, it does not help to change the hardcoded variables in the code, as the firmware will get those stored in the EEPROM. An example of this is float tempSetpoint = 14.0

used in the refrigerator control software. If you want to erase the EEPROM, you can do this by changing the "Tools -> Erase flash" setting from "Erase flash: Only sketch" to "Erase flash: All flash content". This will take some time. Once complete, remember to revert to "Erase flash: Only sketch". Another way is to simply insert the following code at the beginning of setup() EEPROM.write(0, 255); EEPROM.commit(); ESP.restart();

Once the ESP8266 restarts, the EEPROM has been updated and you can erase the code again.

13.2. Utility functions Over time I have separated as much functionality in functions to keep the code simple. The utility programs have been picked up on the Internet or designed by myself. In the following, a description of the utility functions will be described together with the installation of the utility functions.

13.2.1. Installation of the utility functions To maintain the utility functions in one place and make easy use of them in my constructions, they have been assembled in what I call the "hhsutility"-library. They consist of the following files: • hhsutility.h, and • hhsutility.cpp They are placed in the "hhs"-library.

● 153

Home Appliance Hack and IoT Guidebook - UK.indd 153

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

To use the utility functions, simply unzip the "hhsutility"-file and place the "hhs"-library together with all the other Arduino libraries you use. Your Arduino libraries will be located here: /libraries The can be found in Arduinos preferences. Each utility function has a header that describes the function. It is done in a way so that a "Project documentation" can be made by "doxygen". This is why two subdirectories (HTML and latex) can be seen together with the "hhsutility" files as shown below:

Figure 13-1: /libraries/hhs content. The "hhs" file without extension is the doxygen configuration file. If you want to view the documentation, open /libraries/hhs/html/index.html in a browser.

Figure 13-2: doxygen generated project documentation. The project documentation is generated by issuing the following command in a (Windows) terminal:

● 154

Home Appliance Hack and IoT Guidebook - UK.indd 154

13-12-2022 11:08

Chapter 13 • Software/firmware Design

C:\Appl\arduino\libraries\hhs>doxygen hhs

As shown, issue the command in the /libraries/hhs directory. You will most likely see some errors but they can for the most part be ignored. The argument "hhs" is the doxygen configuration file. Doxygen can be found here: https://www.doxygen.org I will not go through the installation as there is a good description here for your favorite operating system: https://www.doxygen.nl/manual/install.html In the following, each utility function will briefly be described.

13.2.2. formatBytes() Input consists of: • the bytes to convert. This utility function is used to convert bytes to KB and MB. I am using it when I'm printing directory listings of the SPI flash file system.

13.2.3. printFloat() Input consists of: • the decimal value to print; • the number of desired decimal places This is a function I find quite handy as it prints a float to "places"-places after the decimal. It rounds the value instead of truncating. It is found on the Arduino Playground where many other interesting functions can be found.

13.2.4 isNumeric() Input consists of: • numeric string to check. This function is used when a new temperature or hysteresis is sent back to the Wemos via the web interface.

● 155

Home Appliance Hack and IoT Guidebook - UK.indd 155

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The temperature and hysteresis will be received as a string and needs to be checked and converted.

13.2.5. errorState(int state) Input consists of: • Error state; • LEDs to flash. If the schematics within this book are followed, all the builds will have several LEDs mounted. To determine any error state of the build, this function has been created so a visual signal can be made. It should not be difficult for you to change the behavior of the LED in case of an error state.

13.2.6. keypressWait() There is no input here. This is a small utility function which I find handy in situations where debug information is coming too fast or where I need time to measure things. The function halts the firmware and waits for a key to be pressed.

13.2.7. CACHE_RAM_ATTR lwdtcb() There is no input here. This is the actual interrupt routine that will restart the device if not regularly reset by the lwdtFeed() function. It simply checks if the LWD_TIMEOUT has passed.

13.2.8. lwdtFeedInit() There is no input here. This initializes the lwdtcb() as a lwdTicker interrupt call-back routine. Should be called at the end of the setup() routine.

13.2.9. lwdtFeed() There is no input here. Resets the loop watchdog counter. It may also be called "to kick the watchdog". To be used at the beginning of the loop() routine – or elsewhere if some of the functionality takes too long.

● 156

Home Appliance Hack and IoT Guidebook - UK.indd 156

13-12-2022 11:08

Chapter 13 • Software/firmware Design

If you use this routine somewhere else than at the top of the loop() routine, make a note somewhere as there is a tendency to forget, which can make debugging troublesome later.

13.2.10. getBootDevice() There is no input here. To be used with old updates which do not resetting by issuing a hardware reset.

13.2.11. startWiFi() Input consists of: • the static IP address of the device; • the IP address of the DNS (in many cases the same as the gateway); • the IP address of the gateway; • subnet to the gateway; • WiFi SSID and password; • unique hostname of the device. To be able to find my ESP8266 devices, I decided to give them static IP addresses. Over the last couple of years, I found that it was necessary to have control of the other input variables mentioned here. I decided to declare them all at the beginning – for all the devices, even though in some cases it's overkill. The utility function starts the WiFi with the given input parameters. Be sure to verify that the Static IP address of the device does not conflict with another device – because you are reusing code!

13.2.12. showWiFiStatus() Input consists of • the status number to convert to clear text. You can get the status of WiFi connection by calling: WiFi.status()

In return you get just a number, which isn't very informative. I made the below utility function so the status could be displayed in a more human-friendly way. It is useful when debugging. The relevant information is found in: /libraries/ESP8266WiFi/src/include/wl_definitions.h

● 157

Home Appliance Hack and IoT Guidebook - UK.indd 157

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

case 255: Serial.println("WL_NO_SHIELD"); break; case 0: Serial.println("WL_IDLE_STATUS"); break; case 1: Serial.println("WL_NO_SSID_AVAIL"); break; case 2: Serial.println("WL_SCAN_COMPLETED"); break; case 3: Serial.println("WL_CONNECTED"); break; case 4: Serial.println("WL_CONNECT_FAILED"); break; case 5: Serial.println("WL_CONNECTION_LOST"); break; case 6: Serial.println("WL_DISCONNECTED"); break; default: Serial.println("_UNKNOWN"); break;

13.2.13. doWifiConnect() Input consists of: • WiFi SSID and password. Since most constructions in this book are dependent on a stable WiFi connection, it is vital to be able to discover any abnormalities and to be able to restore broken connections. I noticed that if you are not transmitting any data for a period (30 sec) the connection was closed. There also seems to be an issue when doing an analogue-read of I/O A0. The WiFi connection is lost after the analogue-read and needs to be restored. Therefore I made the showWiFiStatus() function and combined it with the doWifiConnect() function to always ensure a working WiFi connection. Each time a WiFi connection is required, Wifi.Status() is checked and the function is called if required. This removed many errors.

● 158

Home Appliance Hack and IoT Guidebook - UK.indd 158

13-12-2022 11:08

Chapter 13 • Software/firmware Design

13.2.14. MQTT_connect() Input consists of: • Adafruit_MQTT_Client class. This utility function connects to the MQTT server/broker. It verifies if there is already a connection and if not tries three times before an error is returned. The input includes: • a WiFi client; • information about the MQTT server.

● 159

Home Appliance Hack and IoT Guidebook - UK.indd 159

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 14 • IKEA Hack

This is a construction which got improved considerably over the past few years. The basics are the same but the Web interface has been improved significantly from what I started with. The firmware itself has also been improved. A small custom-designed housing has also been made.

14.1. Schematic The schematic for this design is quite simple as shown below.

Figure 14-1: High-level schematic. You need three wires between the ESP8266 board and the Neopixel: • 5-volt power supply • Ground; • Data. The final construction will be powered by a standard 5-volt USB power supply (500 mA current capacity will do).

14.2. Mechanical assembly Be prepared. There is quite a lot of mechanical engineering and modding ahead to reach the final result.

● 160

Home Appliance Hack and IoT Guidebook - UK.indd 160

13-12-2022 11:08

Chapter 14 • IKEA Hack

14.2.1. Grönö lamp Note After modifying the Grönö lamp, it will not be possible to use it as originally intended. The original assembly instructions are shown in Figure 14-2. You will need to remove almost everything except the two parts marked with an arrow.

Figure 14-2: Original assembly of the Grönö lamp. Figure 14-3 shows what it looked like before modification.

Figure 14-3: The electrical parts of the Grönö lamp.

● 161

Home Appliance Hack and IoT Guidebook - UK.indd 161

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

I cannot advise on any special method for disassembly apart from carefully taking the parts apart with the help of a small screwdriver. An approach could be to cut off the AC mains cord, then carefully twist the two parts marked with the arrow in Figure 14-4 apart. When finished, they need to be cut off as well.

Figure 14-4: Removal of parts. After modification, the leftovers will look as shown in Figure 14-5.

Figure 14-5: After parts removal. The lamp is now ready to receive the Neopixel Jewel. I have modified several lamps and come across two types of sockets. They are more or less designed equal althoiugh the bottom may be a bit different than shown here. It is now time to solder the three wired connections shown in Figure 14-1 to the Neopixel Jewel. When complete, insert the wire and the Neopixel Jewel as shown in Figure 14-6.

● 162

Home Appliance Hack and IoT Guidebook - UK.indd 162

13-12-2022 11:08

Chapter 14 • IKEA Hack

Figure 14-6: Neopixel wiring. Gently pull in the wire until the Neopixel Jewel is positioned on top of the erstwhile socket as shown in Figure 14-7.

Figure 14-7: Neopixel secured in the socket. The bottom view should be as in the next image.

Figure 14-8: Bottom view of the socket.

● 163

Home Appliance Hack and IoT Guidebook - UK.indd 163

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The final assembly is shown in Figure 14-9.

Figure 14-9: Final assembly of the Neopixel in the socket. Pull the other end of the wire connecting the ESP8266 board and the Neopixel through the "sensor" hole in the housing as shown below.

Figure 14-10: IKEA hack housing with ESP8266.

● 164

Home Appliance Hack and IoT Guidebook - UK.indd 164

13-12-2022 11:08

Chapter 14 • IKEA Hack

The white wire goes to the Neopixel and the black wire should be attached to a suitable USB power supply.

14.2.2. ESP8266 housing For the housing use the following: • LOLIN D1 mini shown in Figure 11-1, or • LOLIN D1 mini pro v:2.0 shown in Figure 11-2 Configuration of the housing. LOLIN D1 mini: • LED • ANTENNA • FASTENER • RESET

= = = =

0 0 0 0

(The Neopixel do all the lighting) (I expect the lamp to be within reach of WiFi) (I don't expect the housing to be fastened to anything) (This is more or less up to you)

The result of the above configuration can be seen below.

Figure 14-11: IKEA Hack Wemos housing. A finished housing can be seen in Figure 14-10. LOLIN D1 mini pro v:2.0: • LED = 0 (The Neopixel does all the lighting) • ANTENNA = 0 (I expect the lamp to be within reach of WiFi) • FASTENER = 0 (I don't expect the housing to be fastened to anything) • BATTERY = 0 (It does not make sense to power the lamp with a small battery) • RESET = 0 (This is more or less up to you)

● 165

Home Appliance Hack and IoT Guidebook - UK.indd 165

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The result of the above configuration can be seen below.

Figure 14-12: IKEA Hack Wemos housing.

14.3. Software The software described below may seem complicated given the simple task of turning the LEDs ON and OFF. The reason for the apparent complexity is the ESP8266 not having an option for an Internet connection. Therefore, the overall tasks of the software are as follows: • Maintaining special light effects • WiFi internet connection • A small web server, which can be used to configure and operate a lamp The following describes the software made for the modded Lamp.

14.3.1. Additional libraries For this to work, the following libraries need to be installed: Adafruit Neopixel:

Figure 14-13: Adafruit NeoPixel library install.

● 166

Home Appliance Hack and IoT Guidebook - UK.indd 166

13-12-2022 11:08

Chapter 14 • IKEA Hack

The software can also be found here: https://github.com/adafruit/Adafruit_NeoPixel More about the Arduino Adafruit library use can be found here: https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-use

14.3.2. Features The lamp can operate in a standalone mode where it will cycle through a collection of Rainbow colors at a particular pace. (20 µs between each color). If you'd like to configure the Lamp during operation, you need to configure WiFi settings so the lamp can connect to your local WiFi. This can be done either by recompiling the Wemos firmware or during the power-up of the lamp. To connect to your local WiFi, once configured it will be possible to: • turn the lamp On and Off; • change brightness; • change the delay between color cycles in use; • change Color cycles (currently: Rainbow, Rainbow cycle, Static and Fade); • change static color. This is done via a web interface. The configuration can be saved in non-volatile memory, so it can be restored during power-up. Examples of how to automatically change the configuration of the Lamp from another Internet-connected device will be given in a separate section. It is done by sending HTTP POST requests to the Lamp.

14.3.3. Design 14.3.3.1. web interface At present, the Lamp includes the rudimentary web interface shown below.

● 167

Home Appliance Hack and IoT Guidebook - UK.indd 167

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 14-14: IKEA hack web interface. The web interface is shown when you enter the following URL in a standard browser. http:///NEOPixel_new_20210228.html or http://IKEA-webupdate.local/NEOPixel_new_20210228.html The above URL is shown during the power-up of the Lamp if the Arduino IDE is connected to the lamp as shown below. **

Connected to: Area51

**

IP address..: 192.168.3.144

**

RSSI........: -79

**

WiFi.status.: WL_CONNECTED

**

HTTP server started [OK]

**

Use the following URLs in your browser to

**

- Control the lamp: http://192.168.3.144/NEOPixel_new_20210228.html

**

- Firmware update.: http://IKEA-webupdate.local/firmware Login with username 'admin' and password 'admin'

** WiFi setup done

Figure 14-15: Output during boot-process.

● 168

Home Appliance Hack and IoT Guidebook - UK.indd 168

13-12-2022 11:08

Chapter 14 • IKEA Hack

The web interface is designed to fit a mobile phone without any scaling. The web page shown in Figure 14-14 is made as a standard HTML web page consisting of the following files: • /brightnessHigh.svg, size: 2.80KB • /brightnessLow.svg, size: 4.52KB • /delayLeft.svg, size: 1.68KB • /delayRight.svg, size: 6.03KB • /main_background.svg, size: 12.02KB • /NEOPixel_new_20210228.css, size: 1.86KB • /NEOPixel_new_20210228.html, size: 7.38KB • /NEOPixel_new_20210228.js, size: 3.51KB • (/NEOPixel_new_20210228_load.js) • /sliderCircle.svg, size: 1.09KB The overall directory structure is shown below.

Figure 14-16: IKEA hack directory structure.

Figure 14-17: Data subdirectory of the IKEA hack. Going through the source code of the file "NEOPixel_new_20210228.html" it may be noted that a file is missing in Figure 14-17: "NEOPixel_new_20210228_load.js"

● 169

Home Appliance Hack and IoT Guidebook - UK.indd 169

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

In order to show the present status of the lamp and the stored status at start-up, updated variables need to be inserted into the "_load.js" file before being sent to the lamp. That's why it is missing in the data directory as all the files there are static. The lamp firmware updates the "_load.js" file each time a request for the file is made. Within the static files, comments have been added to make it easier to navigate the files but in general, the variables should be self-explanatory. Each of the buttons or sliders shown in Figure 14-14 can be "activated" by sending a POST request to the IKEA hack. The individual POST requests utility programs are described in section 25.1. and the actual requests are described in section 14.3.5.

14.3.4. Wemos firmware The Wemos firmware is a standard Arduino firmware with a setup() and loop() function. Together with special Adafruit Neopixel functions, they form the complete lamp firmware. The following sections briefly describe how the lamp firmware is designed.

14.3.4.1. setup() The below flow chart shows the overall design of the setup() function:

Figure 14-18: setup() flow diagram.

● 170

Home Appliance Hack and IoT Guidebook - UK.indd 170

13-12-2022 11:08

Chapter 14 • IKEA Hack

Serial communication is brought up first to be able to send info to the Arduino IDE serial monitor. Next, EEPROM settings are loaded, and then the LED strip gets initialized. Initialization of the LED strip is performed roughly at the beginning so a visual indication of the setup process can be given via the lamp. The final default init is to try and connect Wemos to a live WiFi network. During the WiFi connection, the lamp will show circulating blue dots. If the WiFi connection fails, the LEDs in the Neopixel will turn red one by one.

14.3.4.2. loop() The below flow diagram shows the overall design of the loop() function: There is not much to it. If the lamp is on, maintain the selected animation.

Figure 14-19: loop() flow diagram. Within each animation function, a test for web activity will be performed. If not, it is not possible to communicate with the lamp via WiFi. The ESP8266 is not working as a multitasking processor.

14.3.5. HTTP POST requests Communication with the Wemos web server is done via

Usually, this is performed via a web browser where the user clicks on a few buttons or similar and the HTTP POST request is sent.

● 171

Home Appliance Hack and IoT Guidebook - UK.indd 171

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

This can be emulated in software, so a program can send HTTP POST requests without any user interaction. The following shows how this can be achieved in different programming languages, using a Linux PC.

14.3.5.1. The HTTP POST request code in general Each of the three examples below are used in the same way. http_post_request [-s] [-s]

Silent mode only.

Enter a valid URL address. Enter a valid HTTP POST command.

http_post_request is your php/c-code/python program and the rest is arguments to the program. If for some reason you do not want to receive the response from the lamp, use the [-s] option. The rest is straightforward. You need to provide the URL of the Lamp like: http://172.16.0.10 together with one of the HTTP POST request commands shown in section 14.3.5.2.

14.3.5.2. HTTP POST request commands In the following, the POST request commands that can be sent to the lamp will be described. Lamp On/Off This is done by transmitting: LAMP=1 or LAMP=0. as an HTTP command. Set Brightness This is done by transmitting: BRIGHTNESS= = [0::255] as an HTTP command.

● 172

Home Appliance Hack and IoT Guidebook - UK.indd 172

13-12-2022 11:08

Chapter 14 • IKEA Hack

Note In the product documentation, Adafruit states the following: The overall brightness of all the LEDs can be adjusted using setBrightness(). This takes a single argument, a number in the range 0 (off) to 255 (max brightness). setBrightness() was intended to be called once, in setup(), to limit the current/ brightness of the LEDs throughout the life of the sketch. It is not intended as an animation effect itself! So it is not the intention to make any animations using this effect, even though the present LAMP interface can be used as such. Set Delay This is done by transmitting: DELAY= = [0::255] as an HTTP-command. The delay value will be in usec. Accuracy is dependent on the Wemos and has not been verified. Select animation DROPDOWN= = [1::4] Anything else will return an ERROR. The implementation of each animation can be seen in section 14.3.6. Enter static colors This is done by transmitting: RED=&GREEN=&BLUE= = [0::255] as an HTTP command. Anything else will return an ERROR. Save configuration This is done by transmitting: SAVE=Save configuration as an HTTP command.

● 173

Home Appliance Hack and IoT Guidebook - UK.indd 173

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

14.3.6. Implementation of the illumination In the following, the present four modes of illumination are described. Most of the original designs are taken from Adafruit's homepage and modified slightly to fit my needs. Any other borrowed code in the firmware will have its references mentioned in the code. It is easy • • •

to add more if you follow the following design rules: Calculate the new RGB value Update the 7 LEDs Check for incoming HTTP REQUESTS.

14.3.6.1. Rainbow All seven LEDs show the same color at the same time. In this mode, they cycle through a rainbow of colors with a delay of "Delay" in µs. Default is 20 µs. The code to implement this is shown below: for( j = 0; j < 256; j++) { for( i = 0; i < strip.numPixels(); i++) { strip.setPixelColor(i, Wheel((i+j) & 255)); } strip.show(); server.handleClient(); if( loopBreak ) { loopBreak = 0; return; } delay(wait); }

Figure 14-20: Code implanting the rainbow illumination. To be able to manage requests from the Internet, the italicized code was introduced. The server.handleClient() call checks to see if a request needs to be overseen. The delay(wait) is controlled by the "Delay" shown in the web interface.

● 174

Home Appliance Hack and IoT Guidebook - UK.indd 174

13-12-2022 11:08

Chapter 14 • IKEA Hack

14.3.6.2. Rainbowcycle The six LEDs in the outer ring will cycle through a rainbow of colors. The center LED will be turned off. The server.handleClient() call checks to see if a request needs to be handled. The delay(wait) is controlled by the "Delay" shown in the web interface. The program code is given below. strip.setPixelColor(0, 0, 0, 0); for(j=0; j 5000).

● 207

Home Appliance Hack and IoT Guidebook - UK.indd 207

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

When new interrupts are detected at time "pulseMicros #2" and "pulseMicros #3", nothing happens since the 5 ms has not expired (pulseMicros – lastpulseMicros < 5000). One final thing to consider is to make the interrupt routine as short as possible. The code below takes on average 1usec to execute. Therefore the software should be able to handle interrupts on four pins without any problems. void handleInterrupt1() { pulseMicros1 = micros(); if( pulseMicros1 - lastpulseMicros1 > 5000 ) { interruptCounter1++; lastpulseMicros1 = pulseMicros1; } }

Figure 16-13: Interrupt routine. What the function does is check the time passed and if it's more than 5000 µs, the variable "interruptCounter1" is increased by one. All other handling (i.e., communication with the emoncms) takes place in the loop() function as this can cope with being interrupted. So why all this fuss about contact bounce suppression when the energy meter's output is an open-collector output? Well, you now have a standard hardware/software configuration that can manage all kinds of external hardware signals. The 5000 µs will have to be adapted to your situation: the easiest way to estimate the time is to perform a measurement like the one shown in Figure 16-11.

16.4. Operation When it comes to operating the energy control, there is not much to do. There is no web server included in the firmware and no configuration required during the power-up of the device. If you want to change the settings (IP address, etc.) you need to change the variables and recompile the program. You will, of course, get the standard debug information (if not turned off) during the power-on of the device.

● 208

Home Appliance Hack and IoT Guidebook - UK.indd 208

13-12-2022 11:08

Chapter 16 • Energy Sensor

16.5. Dashboard In Chapter 4., the basic installation of an emoncms server was described. To display any data from the energy sensor, a dashboard has to be configured. When a new dashboard is going to be designed, the image below may help you with configuration:

Figure 16-14: Energy sensor inputs vs. feeds. Pulsecount is the input to the emoncms from the energy sensor. Each pulse represents a Wh. The following will be applied to the Pulsecount input: • logged to feed; • multiplied (by 1 in this case); • accumulated; • multiplied by 0.001 to show kWh; • Logged to feed. The processes listed above are what you will be creating in the following. At first, the emoncms will be empty as shown in the two images below:

Figure 16-15: No inputs created.

● 209

Home Appliance Hack and IoT Guidebook - UK.indd 209

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Unless you have something else running, of course.

Figure 16-16: No feeds created. Data is entered by sending it from the energy sensor to the emoncms server via the Input API. The Input API can be found here: http:///emoncms/input/api On this page, you can find the Read&Write key and a description of the string you need to send from the Energy sensor (in emoncms terms: the remote device) to the emoncms server to post data. The Read&Write key for my emoncms installation is shown below:

Figure 16-17: emoncms read and Read & write API key. The Read&Write key is inserted into the ESP8266 code as shown below: const char* apiKey = "4eaed071560b279737c4db67341ffc38"; // Insert own write emoncms apikey

Figure 16-18: API key variable. Given the description of the HTTP string in Figure 4-37, you should be able to decipher the below HTTP string which will be sent to the emoncms server each time a pulse is detected.

● 210

Home Appliance Hack and IoT Guidebook - UK.indd 210

13-12-2022 11:08

Chapter 16 • Energy Sensor

http://172.16.0.11/emoncms/input/post?node=power1&fulljson= {"PULSECOUNT":1}&apikey=4eaed071560b279737c4db67341ffc38 The ESP8266 code that implements this functionality is shown below: sprintf(tempStr, "%d", 1); sprintf(httpUrl, "%s%s%s%s", "/emoncms/input/ post?node=power1&fulljson={%22PULSECOUNT%22:", tempStr, "}&apikey=",apiKey); emoncmsSend();

Figure 16-19: coding of the HTTP string for the emoncms. The first sprint() assigns the value ( 1 ) to transmit. The next sprint() builds the JSON input string. The name "PULSECOUNT" is what you will see under Inputs at the emoncms server as shown below:

Figure 16-20: Inputs from the energy sensor. You will notice in Figure 16-20 that the process list is empty. To see the listing shown in Figure 16-20, click on the Input you want to build a process list for. Click on the little spanner, which will reveal the following:

● 211

Home Appliance Hack and IoT Guidebook - UK.indd 211

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 16-21: Empty process list interface. Using this interface, the process list will be created. Start by adding a "Log to feed":

Figure 16-22: Log to feed entry in energy sensor process list. Since you don't know when each pulse will be received by the emoncms, select "PHPTIMESERIES". The name "rawpulse" can be anything else. When done, press "Add" to insert your selection into the process list. Note You may run into the following error message:

Figure 16-23: Error information due to create permissions. This is due to missing write permissions to the directory. This should have been set in section 4.3.1.

● 212

Home Appliance Hack and IoT Guidebook - UK.indd 212

13-12-2022 11:08

Chapter 16 • Energy Sensor

Figure 16-24: Multiplication entry in the energy sensor process list. Add a multiplicator after the "Log to feed" in case your energy meter does not output 1000 pulses per kWh. After the adjustment, the input will be accumulated by the following process:

Figure 16-25: Accumulator entry in the energy sensor process list. If you want to see it as kWh, the accumulated input will have to be multiplied by 0.001 as shown below:

Figure 16-26: 2nd multiplication entry in the energy sensor process list. You end the process list setup by logging the kWh accumulated input as shown here:

● 213

Home Appliance Hack and IoT Guidebook - UK.indd 213

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 16-27: Final log to feed in the energy sensor process list. As shown in the above picture, you may have noticed that for each process there is a small description of how to use the applied process. The final process list should look like the one below.

Figure 16-28: Final Node power 1 process list. Remember to save ("Changed, press to save") before you leave, or your process list will be lost. The Inputs will now show your Process list as a list of icons as pictured below.

● 214

Home Appliance Hack and IoT Guidebook - UK.indd 214

13-12-2022 11:08

Chapter 16 • Energy Sensor

Figure 16-29: Node power1 process list under inputs. Under Feeds you will see the following:

Figure 16-30: Feeds as a result of the created process list. I have just demonstrated how to create feeds for one of the "nodes". You should do the same for all to be able to view them in your dashboard. If you experience EmonCMS errors, try to look for a solution in the Troubleshooting section 23.4. Once all the feeds needed for the dashboard are in place, you can start configuring the dashboard. Press "New" to start your new Dashboard.

Figure 16-31: New Energy monitor dashboard (no name). Press "no name" to change the name of the dashboard. Press the 3rd icon from the right to start editing the new dashboard.

● 215

Home Appliance Hack and IoT Guidebook - UK.indd 215

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 16-32: Empty dashboard toolbox configuration screen. It is now a matter of taste as to how your dashboard will be configured. There is plenty of inspiration on the Internet. Below I reveal what I have done. Start by adding a container.

Figure 16-33: Insert container. Place and drag it to the size you need. It can be changed later.

● 216

Home Appliance Hack and IoT Guidebook - UK.indd 216

13-12-2022 11:08

Chapter 16 • Energy Sensor

Figure 16-34: White container inserted. Place a description by adding a text (heading).

Figure 16-35: Insert heading text. You should be guided through the process of entering the text.

Figure 16-36: Paragraph text inserted in the container. It is now time to place some of your feeds. Before you do this, be sure to configure a "graph" to show.

● 217

Home Appliance Hack and IoT Guidebook - UK.indd 217

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Enter the Graph interface Setup -> Graphs. There you will see an empty Data Viewer with your Feeds to the left.

Figure 16-37: Feeds data viewer. One method of showing the energy used in your household goes as follows:

Figure 16-38: Household energy consumption.

● 218

Home Appliance Hack and IoT Guidebook - UK.indd 218

13-12-2022 11:08

Chapter 16 • Energy Sensor

Two energy consumers are listed: • Kitchen area (peach) • Bedroom area (purple) This is easily achieved: In Feeds, select the feeds to show. In this case: • Gr 2 Wh acc Kitchen, and • Gr 1 Wh acc Bedroom

Figure 16-39: Feeds selection for the graph. Under the Data viewer in the "Feeds in view", select Delta for each of the feeds:

Figure 16-40: "Feeds in view" configuration. Select Reload to show the curves:

Figure 16-41: Data viewer date configuration. You should now see something like Figure 16-38 in the Data viewer area. If you don't like the colors, simply change the settings in Figure 16-40. One final thing to do is give your graph a name and save it. This can be done in the "My Graphs" area.

● 219

Home Appliance Hack and IoT Guidebook - UK.indd 219

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 16-42: Naming the graph for further use. My graph's name is "Energy monitor new". It is now possible to place the newly designed graph on your dashboard.

Figure 16-43: Insert a graph in the dashboard. Select "graph" and the location of the graph on your dashboard. Double-click within the graph to select which graph to display.

Figure 16-44: Configuration of the graph. Select the newly designed graph and press "Save changes".

● 220

Home Appliance Hack and IoT Guidebook - UK.indd 220

13-12-2022 11:08

Chapter 16 • Energy Sensor

Figure 16-45: The "Energy monitor new" graph inserted in the container. Finally, press the orange button "Changed, press to save", and you are done. If you want to place some of the Feed values, select "feedvalue" as shown below:

Figure 16-46: Insert at specific feed. Within the container, select the location of the "feedvalue" and enter the config menu:

● 221

Home Appliance Hack and IoT Guidebook - UK.indd 221

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 16-47: Configure the feed value entry. Select the Feed you want to show: here "Gr 2 Wh Kitchen". Add the following configuration: • "Prepend Text" write a descriptive text ("Kitchen"!) • "Append Text" enter "Wh". • Make left "Alignment" Finally, save changes. By adding a grey box, it may look like the following:

Figure 16-48: The final dashboard for the energy sensor. In this graph, you can see what is going on in your household.

● 222

Home Appliance Hack and IoT Guidebook - UK.indd 222

13-12-2022 11:08

Chapter 16 • Energy Sensor

I have tried to illustrate some of my findings in the following image:

Figure 16-49: Energy consumption findings. The above graph does not show power consumption from the stove as the power is supplied by another phase in the mains network.

● 223

Home Appliance Hack and IoT Guidebook - UK.indd 223

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 17 • Refrigerator Control In my spare time I deal with a few beehives, which yield honey. For the honey to crystallize, I need to add some old crystallized honey, keep it at temperatures between 12o - 18o degrees Celsius, and regularly stir it. Since I had an old spare refrigerator, I decided to give it a new electronic regulator to see if I could keep the temperature stable within ±1 degree celcius. The inspiration for this design came from this link: https://www.brewpi.com/fridge-hacking-guide/ I do not rework the refrigerator as much as they do. It is possible to remove the electronic regulator and reinstall the old one if required. Any refrigerator type should be applicable for this rebuild. Warning: You are about to work with hazardous voltages and you can become seriously injured if you are not careful about what you are doing. There will most likely also be countries where this rebuild is illegal. Please investigate local regulations and no matter what, DO NOT touch live 230 Volt wires and don't blame me for any injuries. The regulation used is a simple hysteresis, which has proven successful for one of my friends who hatched chicken eggs with the same kind of regulation.

Figure 17-1: Definition of hysteresis. The above drawing shows how the hysteresis works.

● 224

Home Appliance Hack and IoT Guidebook - UK.indd 224

13-12-2022 11:08

Chapter 17 • Refrigerator Control There is the temperature you want to maintain (15oC) and then there is a window around the stable temperature, which is defined by the amount of hysteresis (±0.8oC). The hysteresis is added to prevent unwanted rapid switching of the relay or in this case the Solid State Relay. When the temperature is rising within the refrigerator, the controller will activate the compressor when the temperature is equal to or above 15.8 oC. The compressor will start cooling and when the temperature is equal to or below 14.2 oC it will be turned off again. In real life, it will look like this:

Figure 17-2: emoncms-generated temperature curve. The above graph is taken from the emoncms server. The data presented by the built-in-homepage of the controller looks like the following:

Figure 17-3: Wemos temperature curve.

● 225

Home Appliance Hack and IoT Guidebook - UK.indd 225

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

In both figures, it is obvious that there is quite a large undershoot. It averages to 0.5 – 0.6 degrees. For my purposes, it is not a problem, but if you want it to be more accurate, you will have to add two hysteresis values: • An upper value (0.8), and • A lower value (0.5). One other thing that needs to be taken into consideration is that once the compressor is turned off, you need to wait at least 1 minute (in my case) before you activate it again. If the interval is shorter, the compressor will not start. In the figure below, I changed the temperature from 14 oC to 6 oC degrees via the web interface. The regulator kept the compressor on for a longer period until the new temperature minus the hysteresis was reached. After this, it started regulating around the new temperature. The large overshoot (as shown) was because I opened the door and inserted the item that required a good 6 oC degrees cooling.

Figure 17-4: Change of temperature via the web interface.

17.1. Schematic The schematic is plain sailing as shown in Figure 17-5.

● 226

Home Appliance Hack and IoT Guidebook - UK.indd 226

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-5: Refrigerator control schematics. In the center of the schematic is a small Wemos PCB. The I/O is simple and should not pop any surprises if you are familiar with the Arduino family. For switching the 230 VAC lime voltage, a Solid State Relay (SSR) is used. This device is described in section 18.1.1. T1 in the schematic turns the SSR On and Off. LED D1 indicates the status of the SSR. Since there are no mechanical parts in the SSR, it may be difficult to hear when the fridge compressor is expected to be turned On and Off. If you have a faulty compressor, LED D1 will indicate when the compressor is expected to be working. LED D2 is fully controlled by the software and is not linked to any hardware functionality. For the moment D2 indicates the state of the regulator. • Steady on: the regulator is working as expected. • Flashing: the regulator cannot find the temperature sensor. You may add additional flashing patterns if required. The SSR is connected to J1. Please pay attention to the polarity. The DS18B20 temperature sensor is connected to J2. Please, also pay attention to the polarity of the supply voltage. The device can become quite hot if the polarization is not correct. If you make this mistake, please allow the sensor to cool off before using it, as it will give false temperature readings due to overheating.

● 227

Home Appliance Hack and IoT Guidebook - UK.indd 227

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

A small PCB was made which houses all the components from Figure 17-5. The PCB layout can be seen in section 24.2.

Figure 17-6: Refrigerator control PCB.

17.1.1. SSR To turn the refrigerator On and Off, a Solid State Relay (SSR) is used. A description of the preferred SSR can be seen in section 2.3.

17.1.2. DS18B20 The temperature sensor used is the DS18B20 from Maxim. A description of the device can be found in section 2.6. As the temperature sensor is most likely positioned somewhere in the refrigerator and the main control board outside, the DS18B20 is not co-mounted with the Wemos.

Figure 17-7: DS18B20 schematic. The above figure shows the DS18B20 schematic. It is a direct duplicate of the design shown in the datasheet.

● 228

Home Appliance Hack and IoT Guidebook - UK.indd 228

13-12-2022 11:08

Chapter 17 • Refrigerator Control

The DS18B20 comes in several packages. In this construction, the TO-92 package is used. The darwing below shows the layout of the TO-92 DS18B20 package.

Figure 17-8: DS18B20 TO-92 housing. Depending on your refrigerator, you could mount the DS18B20 and the resistor on a small separate PCB. I decided to create the construction shown below, which is then wrapped in heat-shrink tubing.

Figure 17-9: Temperature probe. The final hand-built probe can be seen below.

● 229

Home Appliance Hack and IoT Guidebook - UK.indd 229

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 17-10: Final DS18B20 probe. Please be careful not to create short-circuits. If you don't like the DIY probe, I have created a small PCB to hold the temperature sensor, resistor, and the small connector.

Figure 17-11: PCB for the DS18B20. It is now time to find a way to fit the sensor wiring into the refrigerator. There are wires already going from the compressor to the light and the original temperature sensor, however, I found a much easier way to do it. The refrigerators I have come across have a kind of drain hole (condensation run-off hole), which leads condensation down into a tray, which is on top of the compressor. This hole can be used to mount the temperature sensor inside the refrigerator while keeping the electronics outside.

● 230

Home Appliance Hack and IoT Guidebook - UK.indd 230

13-12-2022 11:08

Chapter 17 • Refrigerator Control

The photo below shows how I did it:

Figure 17-12: Mounting of the DS18B20 inside the refrigerator. You can also run the cable past the door seal, but make sure you use a flat cable.

17.2. Mechanical assembly There are several ways you can make use of the refrigerator control. Here are some options

17.2.1. No modifications to the refrigerator at all Most refrigerators cool down to about 4 °C without the need to hack them. As I am going to operate the refrigerator at a much higher temperature (15 °C). Consequently, if the original thermostat is set to its lowest setting it will not interfere at all. All that is required is to switch power to the entire refrigerator. This can be done by inserting a mechanical relay or SSR into a power cable connected to the refrigerator. The downside of this approach is that the light in the refrigerator will not work when power is cut off... no surprise here. Another downside is if you want to maintain a temperature close to 4 °C, the thermostat may interfere. I have not tried the above solution so I don't have any experience with it. However, there is no doubt it is the safest approach of the two mentioned here.

● 231

Home Appliance Hack and IoT Guidebook - UK.indd 231

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

If you want to pursue this approach you should build the hardware of the greenhouse controller mentioned in section 18.2. – with only one temperature sensor.

17.2.2. Modifying the refrigerator If you'd like to control the compressor in the refrigerator directly (like me), you can follow the following description. Warning Don't attempt this unless you know exactly what you are doing. In the following, I will try to describe how you should modify your refrigerator to work with the electronic refrigerator controller described here. Before starting your modification, you need to examine the compressor and PTC starter relay in your refrigerator. The PTC starter relay will most likely be sitting on the side of the compressor behind a protective shield. Being a Dane I'm used to working with Danfoss Compressors. I will, therefore, use them in a description of how things usually work. Below is a schematic of the wiring in a typical household PTC starting relay.

Figure 17-13: Schematic PTC starting relay. The "Main winding", "Start winding" and the "Winding protector" are located inside the compressor. The rest is placed in the PTC starting relay device. The "Start winding" is used to start the compressor when it is cold. As soon as it has reached operating temperature, the start winding will cut off and the "Main winding" will continue alone. The picture below shows what the PTS starter relay may look like in real life.

● 232

Home Appliance Hack and IoT Guidebook - UK.indd 232

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-14: PTC starting relay. Courtesy of eBay (appliancehelp) When you look at all the wires going to the PTC starter relay, it may look complicated but it is because the relay serves two functions: • start the compressor; • function as a function block for all the ingoing and outgoing wires to the compressor. With all the wires connected it will probably look as shown below:

Figure 17-15: Wiring of the PTC starting relay. In some cases, you may have a fan connected as well. My compressor and PTC starter relay construction is a little different from the one described above, which fortunately makes the modification simpler. As shown below, all that is required is two wires: "Live" and "Neutral" to control the compressor.

● 233

Home Appliance Hack and IoT Guidebook - UK.indd 233

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 17-16: My final PTC starting relay wiring. In my case, "Live" is brown, and "Neutral" is blue. The wires I removed are shown below.

Figure 17-17: Wires removed from PTC starting relay.

● 234

Home Appliance Hack and IoT Guidebook - UK.indd 234

13-12-2022 11:08

Chapter 17 • Refrigerator Control

In Figure 17-17, "Neutral" was connected to "Neutral" in Figure 17-16 and "Switched Live" was connected to "Live". You can leave the wires unmodified in a place where they don't do any harm. With this modification, it is possible to restore the refrigerator to its original functionality. The modified diagram can be seen below:

Figure 17-18: PTC starting relay and SSR together. "To Wemos" connects to J1 in Figure 17-5. When the SSR modification is in place, it can be evaluated by applying a 9-volt battery to the DC side of the SSR ("To Wemos"). The compressor should start working when the 9 volts is applied. Warning: You are about to work with hazardous voltages and you can sustain serious injuries if you are not careful about what you are doing. There will also most likely be countries where this rebuild is illegal. Please investigate local regulations and no matter what, DO NOT touch live 230 VAC wires and blame me for any injuries. Below is a diagram showing how to connect it all.

● 235

Home Appliance Hack and IoT Guidebook - UK.indd 235

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 17-19: Refrigerator PCB assembly diagram. Before connecting the PCBs and SSR you should have made the modifications to the refrigerator as described earlier in this section. Of course, you should take care • to prevent personal injury; • to connect the SSR correctly to the controller board; • to connect the DS18B20 correctly to the controller board. If you, like me, assembled the DS18B20 as shown in Figure 17-10, you are on your own in correctly connecting the DS18B20 to the controller board. If you confuse the GND and 3.3 V, the DS18B20 will most likely survive if only for a short period but it will get very hot. This will eventually affect the measured temperature until the device has cooled off. Figure 17-19 might give the impression that all PCBs are in the same box. For safety, you should keep the SSR in one (isolated) box and the actual controller PCB in another. The DS18B20 will most likely be located inside the refrigerator away from everything else. I keep my SSR as shown below:

● 236

Home Appliance Hack and IoT Guidebook - UK.indd 236

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-20: SSR wiring. There will most likely be more suitable boxes but this was the one available. For safety reasons, also keep the "Neutral" wire inside the box as well. The images below show how to assemble the refrigerator controller. The PCB below together with the box in Figure 11-4 will be the parts used in the assembly description.

Figure 17-21: Refrigerator PCB.

● 237

Home Appliance Hack and IoT Guidebook - UK.indd 237

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Solder the wires for the temperature sensor and the SSR to the refrigerator control PCB shown at the top in Figure 17-21. Mount the resistors and the transistor. When mounting the transistor, secure that the spacers for the Wemos PCB are high enough to make room for the transistor. The transistor can be placed flat on the PCB if needed. Mount the Wemos PCB together with the spacers. The result is shown below.

Figure 17-22: Mounted refrigerator PCB. Mount the two LEDs at the "back" of the PCB as shown below.

Figure 17-23: Location of the On/Off LED and Status LED.

● 238

Home Appliance Hack and IoT Guidebook - UK.indd 238

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Pull the temperature sensor and SSR wires/cables through the holes and place the PCBs inside the box. See the picture below.

Figure 17-24: Cable location. The assembly shown in Figure 17-24 was made by adjusting the heights several times. The final assembled refrigerator control box and prototype can be seen below.

Figure 17-25: Final refrigerator prototype. It is a prototype and will most likely not comply with a lot of regulations, but it has been working flawlessly for 3 years and has been used to cool my honey and as a wine refrigerator – and of course as a standard refrigerator.

● 239

Home Appliance Hack and IoT Guidebook - UK.indd 239

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

17.3. Software The software described below may seem complicated given the simple task of keeping the temperature stable in a refrigerator. The reason for the added complexity is that the ESP8266 has the option of internet connectivity. Therefore, the overall functionality of the software is as follows: • Keep the temperature stable. • Provide an advanced watchdog functionality • Provide a WiFi-driven internet connection. • Function as a small web server that can be used to configure the regulator during operation. • Provide a small file system that collects the temperatures of the refrigerator and displays them via the webserver. • Function as an agent that transmits the temperatures measured to an emoncms server hosted on a Raspberry Pi.

17.3.1. Additional libraries For this to work, the following libraries need to be installed: DallasTemperature:

Figure 17-26: DallaTemperature library installation. OneWire:

Figure 17-27: OneWire library installation. All of that is done via the Library Manager (Tools -> Manage Libraries)

17.3.2. Wemos firmware 17.3.2.1 setup() The setup() function has been split up as shown below.

● 240

Home Appliance Hack and IoT Guidebook - UK.indd 240

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-28: Refrigerator control setup(). It should be simple to follow what is going on. During setup, useful debug information is available via the Arduino IDE serial monitor. Before you compile the software for the first time, several variables need to be configured according to your installation. The network overview below shows which variables need to be changed and what they relate to.

Figure 17-29: Refrigerator control network layout. The variables can be found in the "Network variables" section at the top of the source code. You may want to change the time server IP and the mDNS Name as well, but it is not necessary.

● 241

Home Appliance Hack and IoT Guidebook - UK.indd 241

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The "apikey" is given when you install the emoncms server. This can be seen in Chapter 4.

17.3.2.2. loop() The loop() function has been split up as shown below.

Figure 17-30: Refrigerator control loop(). The actual code is slightly more complicated but you should be able to navigate the code with the above drawing.

17.3.3. Temperature regulation At the beginning of this section (covering the refrigerator control), the overall temperature regulation method was described. In Figure 17-30, the temperature to maintain and the hysteresis were shown. In the software these definitions are stored in: float tempHysteresis = 2.0; float tempSetpoint = 14.0; They can be found in the "Refrigerator variables" area. You should change them to suit your needs. Figure 17-30 shows in overall terms how the loop() works. The following describes how it is performed in more detail:

● 242

Home Appliance Hack and IoT Guidebook - UK.indd 242

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-31: Refrigerator regulation. A temperature reading is achieved in three steps: • request temperature' • wait until the temperature is ready; • read the actual temperature. It is done this way because it takes some time to get the temperature from the DS18B20 device. This delay is held in the variable DS_DELAY, which by default is set to 750 ms. Once the temperature reading is available, it is uploaded to the emoncms (if available) and actual regulation takes place. There is one more thing to take into consideration: before the compressor is turned on, I verify that at least one minute has passed since the last time it was turned on. If I don't wait, my refrigerator will not start. I have never experienced this being a real-life issue though. I noticed the behavior when I evaluated the SSR alone together with the compressor. If an emoncms-server has been set up to receive the temperature readings, each compressor activation will be uploaded to the emoncms server. This information is needed for the emoncms dashboard.

● 243

Home Appliance Hack and IoT Guidebook - UK.indd 243

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

17.3.4. Watchdog functionality The refrigerator control uses the same watchdog functionality as described in Chapter 8. If you see the LED blink or the device constantly reboot, attach it to the Arduino IDE to see if any error messages are displayed at the terminal interface. Of course, you should fix the error before resuming operation.

17.3.5. web interface At present, the Refrigerator control includes the following interface:

Figure 17-32: Refrigerator control web interface. The web interface is shown when you enter the IP address of the refrigerator control in a standard browser. The IP address is shown during the power-up of the Refrigerator control if the Arduino IDE is connected to the controller. The web interface is made purely for test purposes and it will most likely be possible to make a fancier interface. The web interface is hardcoded into the Wemos firmware and can only be changed by editing and recompiling the code.

● 244

Home Appliance Hack and IoT Guidebook - UK.indd 244

13-12-2022 11:08

Chapter 17 • Refrigerator Control

The HTML code for the web page is made with the function: getPage (string str) Each of the input areas is made as an HTML . Compared to the other web interfaces used in the other constructions, an additional web interface is introduced. This is located in the SPI flash file system described in section 18.3.6. I could have placed it all in the SPI flash file system, but this would have prevented you from using the construction if your ESP8266 lacked flash memory. Now you can leave out the SPI flash file system and still have the web interface to configure the Refrigerator controller.

17.3.5.1. Set temperature and hysteresis The form to set temperature and hysteresis is shown below:

Enter new triger temperature:
Temperature:


Hysteresis:

Figure 17-33: HTML set temperature and hysteresis code. The transmitted request will look like this: • temp=&hyst= • =[0.00::99.00] • =[0.00::14.99] Anything else will return an ERROR. 'tempSetpoint', 'tempHysteresis' are internal Wemos Firmware variables that will be included when the HTML code is transmitted from the Wemos to the browser.

17.3.5.2. Open temperature logger If it is implemented, it is possible to save all the measured temperature values in an SPI flash file system together with HTML files to display the temperatures as a graph. To see the graph, simply press the "Open temperature logger." button. When pressed, a new tab in the browser will be launched.

● 245

Home Appliance Hack and IoT Guidebook - UK.indd 245

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The code to do this is shown below:

Open temperature logger. . .

Figure 17-34: HTML Open temperature logger. When pressed, the ESP8266 temperature logger interface will be shown but only after some time as it needs to be loaded first:

Figure 17-35: Temperature logger loading JavaScript code and data. Once loaded it will look like this:

Figure 17-36: ESP8266 Temperature logger interface. What you will see is an example of Google Charts.

● 246

Home Appliance Hack and IoT Guidebook - UK.indd 246

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-36 shows the start-up of the Refrigerator controller until the requested temperature is reached. More on this interface and how it's made can be read in section 17.3.6.

17.3.5.3. Save configuration The temperature and the hysteresis settings will be lost if the Refrigerator control is powered off. If the configuration should survive a power cut, the "Save configuration" command needs to be issued. When issued, temperature and hysteresis variables will be saved in non-volatile memory (EEPROM) and read back during power on. The form to save the configuration looks like this:



"";

Figure 17-37: HTML save configuration code. The transmitted request will look like this: • SAVE=Save configuration

17.3.6. SPI Flash File System The firmware for the Refrigerator Control requires an SPI flash file system to work. You should simply follow the descriptions in section 3.3. to prepare this file system. If this feature is not needed it may be left out by removing the code shown in Figure 17-34. Within the control firmware, additional code related to the file flash system should be deleted but I will leave that up to you how to figure that out.

17.3.7. Compile "Compilation" of the software is a little different compared to the other software presented in this book. You should do the following during the "compilation" process: • Prepare the files for the web page as described in section 3.3. • Update the content of the files • Upload the content of the data-dir • Compile and upload the actual software for the ESP8266. If you don't need to change the content of the data-dir, you can continue to make changes and upload the software as normal.

● 247

Home Appliance Hack and IoT Guidebook - UK.indd 247

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

17.4. Operation As shown in Figure 17-32, the web interface gives the option of setting the temperature and hysteresis while the refrigerator control is running. Setting the temperature is what is expected in a modern refrigerator. As the hysteresis is part of the regulation, I found it convenient to be able to control this as well. Remember to save when done or else the setting will be lost and the hardcoded or EEPROM saved temperature and hysteresis will be loaded.

17.5. Dashboard In Chapter 4., the basic installation of an emoncms server is described. To display any data from the energy sensor, a dashboard has to be configured. At first, the emoncms will be absolutely empty as shown in the next image.

Figure 17-38: No inputs created. ...Unless. of course. you have something else running. Data is entered by sending data from the Refrigerator controller to the emoncms server via the input API. The input API can be found here: http://rpi-ip-address/emoncms/input/api On this page, you can find the Read & Write key and a description of the string you need to send from the Refrigerator controller (in emoncms terms: the remote device) to the emoncms server to post data. The Read&Write key of my emoncms installation is shown below:

Figure 17-39: emoncms read and Read & write API key.

● 248

Home Appliance Hack and IoT Guidebook - UK.indd 248

13-12-2022 11:08

Chapter 17 • Refrigerator Control

The Read & Write key is inserted into the ESP8266 code as shown below: const char* apiKey = "4eaed071560b279737c4db67341ffc38"; // Insert own write emoncms apikey

Figure 17-40: The API key to Read & Write. In this example, two kinds of information are transmitted to the emoncms server: • the status of the compressor (on/off); • the refrigerator temperature. You therefore need to send two kinds of HTTP strings. Compressor status The first one on compressor status is shown below: http://172.16.0.11/emoncms/input/post?node=refrigerator&fulljson= {"ONOFF":1}&apikey=4eaed071560b279737c4db67341ffc38

The ESP8266 code that implements this is this: sprintf(tempStr, "%d", 1); sprintf(httpUrl, "%s%s%s%s", "/emoncms/input/ post?node=refrigerator&fulljson={%22ONOFF%22:", tempStr, "}&apikey=",apiKey); emoncmsSend();

Figure 17-41: Coding of the HTTP string for the emoncms. The first sprintf() assigns the value (1 or 0) to transmit. The next sprintf() builds the JSON input string. The name "ONOFF" is what you will see under Inputs at the emoncms server. This information will only be transmitted when the compressor changes state from ON to OFF or OFF to ON. Don't be alarmed that there will be long intervals between information about the compressor state. Temperature The next information to transmit is the temperature within the refrigerator. This is done using this HTTP string: http://172.16.0.11/emoncms/input/post?node= refrigerator&fulljson= {"temp":14.19}&apikey=4eaed071560b279737c4db67341ffc38

● 249

Home Appliance Hack and IoT Guidebook - UK.indd 249

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The ESP8266 code that implements this is shown below: sprintf(tempStr, "%d.%02d", (int)tempValue, (int)(tempValue*100)%100); sprintf(httpUrl, "%s%s%s%s", "/emoncms/input/ post?node=refrigerator&fulljson={%22temp%22:", tempStr, "}&apikey=",apiKey); emoncmsSend();

Figure 17-42: Coding of the HTTP string for the emoncms. The first sprintf() assigns the temperature value to transmit. The next sprintf() builds the JSON input string. The name "refrigerator" is what you will see under Inputs at the emoncms server. You can combine the two HTTP strings in one, but the information is not related. I therefore made two separate HTTP strings. When it all works, you should see something like this:

Figure 17-43: Input nodes from the refrigerator controller. In the above screendump, the Process list is empty. To configure the Process list you need to click on the spanner. This will have to be done for both entries. You start with temp(erature) input.

● 250

Home Appliance Hack and IoT Guidebook - UK.indd 250

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-44: Empty refrigerator node process list. Since it is the first time, there will be no process defined. The only thing you will have to do is log the input. This is done by selecting "Log to feed".

Figure 17-45: New refrigerator node process. Since the temperature reading is expected to be received every 30 seconds, PHPFINA Fixed int / 30s is selected. Press "Add" to create your new process. Once done, it will look like this:

● 251

Home Appliance Hack and IoT Guidebook - UK.indd 251

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 17-46: First refrigerator node process (temp) added. Since this is the only process to add, you will leave the configuration setup. Remember to save ("Changed, press to save") before you leave or your process list will be lost. You now do the same for the ONOFF input in Figure 17-43. Here you also "Log to feed" but this time you do it a little differently.

Figure 17-47: Refrigerator node ONOFF process. Since you don't know when the compressor will be turned on or off, you need to select the PHPTIMESERIES for our "Log to feed". Press "Add" to enter the process and leave the entry by pressing "Changed, press to save". You should now see the following:

● 252

Home Appliance Hack and IoT Guidebook - UK.indd 252

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-48: Refrigerator node inputs with newly added processes. If you go to the Feeds page you should see:

Figure 17-49: Refrigerator node feeds. If you experience emoncms errors during this configuration, look for a solution in section 23.4. on Troubleshooting. Once all the feeds needed for the dashboard are in place, you can start configuring the dashboard. Press "New" to start your new Dashboard.

Figure 17-50: New refrigerator dashboard (no name). Press the "no name" to change the name of the dashboard. Press the 3rd icon from the right to start editing the new dashboard.

● 253

Home Appliance Hack and IoT Guidebook - UK.indd 253

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 17-51: Configuration of the refrigerator dashboard. It is now a matter of taste how you configure your dashboard. There is plenty of inspiration to be found on the Internet. I will show you the very basics in the following. Start by adding a container.

Figure 17-53: Adding a container to the refrigerator dashboard. Place and drag it to the size you need. It can be changed later if you change your mind. Place another container below the one you just made. It should look like this:

● 254

Home Appliance Hack and IoT Guidebook - UK.indd 254

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-53: Two empty containers in the refrigerator dashboard. To show the actual temperature, add a nice dial:

Figure 17-54: Adding a (temperature) dial to the dashboard. Place the dial and enter the configuration menu. There are a vast number of variables to set. I changed the following compared to the standard values. Those not mentioned are not changed.

● 255

Home Appliance Hack and IoT Guidebook - UK.indd 255

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Variable

Value

Comment

Feed

Refrigerator:temp

Select the temperature node from the dropdown menu.

Max. value

25

Enter the max temperature value you expect to reach.

Scale

1

The value to multiply the feed with before

Type

Green Red, zero at

displayed Select which type of dial to show.

the left Min. / Max.

Yes

Display the min and max value

Min. feed

Refrigerator:temp

The feed for the min value

Max feed

Refrigerator:temp

The feed for the min value

Table 17-1: emoncms dial configuration. Once changed, save the configuration and you should see something like this: Add a descriptive text of your own choice.

Figure 17-55: The temperature dial fully configured. Each time you enter the dashboard, it will show you the actual temperature of the refrigerator. To be precise, the last temperature measurement received from the refrigerator controller. Add a descriptive text to the temperature dial.

● 256

Home Appliance Hack and IoT Guidebook - UK.indd 256

13-12-2022 11:08

Chapter 17 • Refrigerator Control

Figure 17-56: The temperature with a heading. As another feedback of the system, the state of the compressor can be shown by adding an LED. The only thing necessary to configure is the use of the ONOFF Feed as feed value. The state of the compressor also needs a heading as shown in the image below.

Figure 17-57: Compressor state added as an LED. The final thing is to add a graph showing temperature over time. Select "rawdata" and place it in the bottom container. Configure it as shown in the table below. I changed the following values compared to the standard values (again, those not mentioned are not changed): Variable

Value

Comment

Feed

Refrigerator:temp

Select the temperature node from the dropdown menu

Units

1

Enter units

dp

.1

Decimal point

Table 17-2: emoncms temperature graph.

● 257

Home Appliance Hack and IoT Guidebook - UK.indd 257

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Once complete, your new dashboard should look like as in the next image. The image shows that the system has been collecting data for at least a day. Consequently, at first, you will most likely see nothing unless you collected data before the dashboard was created.

Figure 17-58: Final refrigerator control dashboard. When you are satisfied with the dashboard, press "Changed, press to save". It is now possible to find it under the dashboard listing.

Figure 17-59: Dashboard listing. This was just a straightforward way of setting things up. There are plenty of examples on the emoncms homepage. I have made two separate dashboards: • household energy consumption; • refrigerator control unit.

● 258

Home Appliance Hack and IoT Guidebook - UK.indd 258

13-12-2022 11:08

Chapter 17 • Refrigerator Control

If you like to have it all in one dashboard, you can, of course, combine the two in one dashboard. This can then be displayed at a central location in your house. The refrigerator control has on-board logging of the temperature if you implemented it. You should try and compare the two side by side.

Figure 17-60: ESP8266 on-board temperature logging. You may have noticed that there is an overshoot of the temperature even though the hysteresis should be symmetrical around the temperature. This is because it takes some time for the temperature to settle in the refrigerator after the compressor has been turned off. If you want to avoid this, you should change the code so you have one hysteresis above the temperature and one below.

● 259

Home Appliance Hack and IoT Guidebook - UK.indd 259

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 18 • Greenhouse Thermostat I have a small greenhouse in my garden, which includes plants that cannot survive the Danish winter – at least the ones we had 5 to 10 years ago with temperatures down to minus 15 degrees Celsius. Therefore I have made a smaller greenhouse in the larger greenhouse.

Figure 18-1: Mini greenhouse. It is a simple timber frame covered with bubble-plastic. As you may imagine, it is not enough to keep the cold out so some kind of heating is required. I have chosen to use a simple heater like the one below.

● 260

Home Appliance Hack and IoT Guidebook - UK.indd 260

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

Figure 18-2: Off-the-shelf heater. Having introduced the 230 Volt AC heater you may imagine: "I am about to work with 230 Volt AC", so here is a warning. Warning: You are about to work with hazardous voltages and you can become seriously injured if you are not careful about what you are doing. There will most likely also be countries where this rebuild is illegal. Please investigate local regulations and no matter what, DO NOT touch live 230 Volt wires and don't blame me for any injuries. Having made the refrigerator control mentioned in Chapter 17. it is not at all difficult to make the greenhouse thermostat. The only difference is that this time you want to keep the temperature up instead of down. All the components used in Chapter 17. will be reused except that I have added a DS18B20 temperature sensor and can measure the temperature inside the small greenhouse and outside as well.

● 261

Home Appliance Hack and IoT Guidebook - UK.indd 261

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 18-3: Definition of greenhouse controller hysteresis. The above drawing shows how the hysteresis works There is the temperature you want to maintain (10 oC) and then there is a window around the stable temperature, which is defined by the amount of hysteresis (±1.5 oC). The hysteresis is added to prevent unwanted rapid switching ("chatter") of the Solid State Relay. When the temperature drops in the mini greenhouse, the controller will activate the heater when the temperature drops under 8.5 oC. The heater will start heating and when the temperature rises above 11.5 oC, will be turned off again. In real life, it will look like this:

Figure 18-4: emoncms temperature curve. The above image is taken from the emoncms server.

● 262

Home Appliance Hack and IoT Guidebook - UK.indd 262

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

The data presented by the built-in-homepage of the controller looks like this:

Figure 18-5: Wemos temperature curve. In both images, it is obvious that there is an overshoot of 1.0 to 1.5 degrees C on average. For my purposes, it is not a problem, but as an improvement, consider adding two hysteresis values: • upper value; • lower value. The greenhouse thermostat turns the heater on/off based on the principle shown in Figure 18-3. One thing the thermostat does not do is regulate the warm air sent into the greenhouse. You need to figure this out by yourself but start low. I ran some tests where the mini greenhouse was heated for 15 min, waited 15 min and then heated again for 15 min. By doing this you can figure out if the heater can maintain the required temperature. One such test run is shown in the figure below.

Figure 18-6: Temperature test run.

● 263

Home Appliance Hack and IoT Guidebook - UK.indd 263

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook The temperature drops from 18.1 oC to 10.1 oC in 17 minutes. The heater used in this setup is a 2000 W unit but was configured to use 1000 W. I managed to keep the temperature as expected with -10 oC outside. By using two temperature sensors you can see the warm air exiting the mini greenhouse as shown below.

Figure 18-7: Greenhouse temperature in/out-side.

18.1. Schematic The schematic is the same as shown in Figure 17-5 except that I am using two temperature sensors. The overall diagram of the entire construction is shown here.

Figure 18-8: Greenhouse control diagram. Pay attention to the following: • There are two temperature sensors but only one resistor (R6) is needed as shown in Figure 18-19.

● 264

Home Appliance Hack and IoT Guidebook - UK.indd 264

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

• I am using an external antenna as I need additional gain for my WiFi signal. If you don't need the additional gain, leave the Wemos/LOLIN board as is. Otherwise, modify the board as described in Chapter 9.

18.1.1. SSR Just as in Chapter 17., I am using a Solid State Relay (SSR) to turn the heater on and off. You may, of course, use a standard relay rated 230 VAC and 10 Amps.

18.1.2. DS18B20 To be able to measure the temperature inside and outside of the small greenhouse I am using two of the temperature sensors mentioned in section 17.1.2. If you don't need information about the outside temperature, simply omit the outside temperature sensor. Note Compared with the temperature sensor assembly from section 17.1.2., the resistor should not be mounted. Instead, one centrally mounted resistor – R6 - is needed as shown in Figure 18-9.

Figure 18-9: Greenhouse control schematic. How the temperature sensors are mounted can be seen in Figure 18-11. There will most likely be better ways of doing it!

● 265

Home Appliance Hack and IoT Guidebook - UK.indd 265

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

18.2. Mechanical assembly The actual implementation of the greenhouse control system can be seen in the following images. Figure 18-10 shows how the (prototype) system for my greenhouse control is implemented. It has been working flawlessly for three winters, so I intend to keep it as is. Since my greenhouse is on the edge of my WiFi coverage area, I'm using an external antenna. Any 2.4 GHz antenna will do. The chosen box for the power supply and electronics should be "Dust protected – limited ingress" and "Protected against low-pressure jets of water from any direction – limited ingress permitted" Admittedly, I have not sealed the wire holes as I don't have an issue here so far. The box was what was available at the time I made the greenhouse controller. Any box that can keep your electronics away from dust and water should do.

Figure 18-10: Greenhouse control system assembled. The inside of the box is arranged (layered) in a way such that mains voltage (230 VAC) is kept well separated from low voltage (5 VDC). Figure 18-11 shows the upper 5 VDC part of the greenhouse controller.

● 266

Home Appliance Hack and IoT Guidebook - UK.indd 266

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

You will find the Wemos board with: • USB power • 1-wire bus • SSR wire • external antenna all connected to the board.

Figure 18-11: Low power section of the greenhouse controller. You may omit the external antenna if your WiFi coverage is sufficient. You may also omit one of the temperature sensors if you don't need to monitor the temperature outside the greenhouse. Figure 18-12 shows the high voltage 230 VAC section of the greenhouse controller, which contains a USB power supply for the Wemos board and the SSR.

● 267

Home Appliance Hack and IoT Guidebook - UK.indd 267

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 18-12: High power section of the greenhouse controller. I could have kept the USB power supply outside of the box but preferred to have it all in one box due to the greenhouse environment. Both the 230 VAC and 5 VDC parts are mounted on a small piece of plywood. Combining Figure 18-8 and Figure 18-12 you get what you see below (230 VAC wiring diagram).

Figure 18-13: 230 VAC wiring diagram. I followed the following assembly order when I assembled my box: • Place a piece of plywood at the bottom of the box. • Attach the 230 VAC IN/OUT cables in the box. • Attach a 230 VAC socket for the USB power supply to the 230 VAC IN. • Attach the 230 VAC "neutral" (the dotted) wire from 230 VAC IN to the 230 VAC OUT. • Attach the 230 VAC "phase" wire from 230 VAC IN to the 230 VAC side of the SSR.

● 268

Home Appliance Hack and IoT Guidebook - UK.indd 268

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

• Attach the 230 VAC "phase" wire from 230 VAC OUT to the other 230 VAC side of the SSR. • Check your 230 VAC wiring by attaching a 9 VDC battery to the SSR. If the SSR does not turn on, change "+" and "-". • Place a new piece of plywood on top of the 230 VAC section. Remember to place the USB 5 VDC cable and the SSR wire above the plywood. • Solder the SSR wire and the 1-Wire to the greenhouse controller PCB (Figure 24-11). I expect that you have fully assembled the PCB with components and the Wemos at the present stage. • Attach the greenhouse controller PCB to the plywood. • Attach the rest of the temperature sensors to the 1-Wire. This can be done as shown in Figure 18-11 in case you want two temperature sensors, or if just one temperature sensor is needed as shown in Figure 17-9. • Check everything one more time. Then you are ready to turn on the 230 VAC. The mechanical assembly so far has mainly been about the electronics involved in the construction. There is still one key component that needs to be assembled before you can heat the greenhouse. The heater needs to be able to pass the "hot air" to the greenhouse. How this is done can be seen in the next image. With some luck, all parts can be found in a regular DIY store.

Figure 18-14: Heater and greenhouse connection. The "air-guide" is made of a flex hose with two adapters fitted on the heater and greenhouse. If the nearby surroundings are really cold for an extended period, you may consider using an insulated flex hose.

● 269

Home Appliance Hack and IoT Guidebook - UK.indd 269

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The heater is lifted from the tiles to not suck any dirt into the greenhouse and prevent any condensate inside the flex hose from entering the heater.

18.3. Software The overall software design is equal to the one described in the refrigerator section 17.3. Overall tasks of the software: • Keep the temperature stable. • Provide advanced watchdog functionality • Provide a WiFi-driven internet connection. • Function as a small web server that can be used to configure the regulator during operation. • Provide a small file system that collects the temperatures of the refrigerator and displays them via the webserver. • Function as an agent that transmits the temperatures measured to an emoncms server hosted on a Raspberry Pi. In the following, I will highlight the few differences between the "refrigerator control" and the "greenhouse control".

18.3.1. Additional libraries For this to work, the following libraries need to be installed: DallasTemperature:

Figure 18-15: Dallas Temperature library installation. OneWire:

Figure 18-16: OneWire library installation. This is all done via the Library Manager (Tools -> Manage Libraries)

● 270

Home Appliance Hack and IoT Guidebook - UK.indd 270

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

18.3.2. Wemos firmware When fully equipped, the greenhouse control will include two temperature sensors. To address them individually, you need to figure out their unique 64-bit serial code. To determine the unique 64-bit serial code, I am using this program: https://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html The DS18B20 temperature sensors to be tested are attached to I/O-pin D1 as shown in Figure 18-9. Remember the 4.7-kohm resistor. When you're ready to find the 64-bit serial code, do this in sequence: start the Arduino IDE; load the "GetOneWireDevice_20200101.ino" sketch into the Wemos/LOLIN board; open the serial terminal within the Arduino IDE. You should then see something like this: Get One Wire Device address 2020-01-01 ver:1 Setup Done. Insert new DS18B20 device. ** Hit a key to continue ** Looking for 1-Wire devices... ** Found '1-Wire' device with address: **

0x28, 0x29, 0xE8, 0x29, 0x08, 0x00, 0x00, 0x1C

** Found '1-Wire' device with address: **

0x28, 0x0F, 0x09, 0x55, 0x01, 0x00, 0x00, 0x3B

** DS18B20 device is connected. ** Resolution of the device: 12 ** Getting temperature: 20.44 Done Insert new DS18B20 device. ** Hit a key to continue

Figure 18-17: DS18B20 64 bit code discovery. The DS18B20 address or 64-bit serial codes are marked in red.

● 271

Home Appliance Hack and IoT Guidebook - UK.indd 271

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The two 64-bit serial codes are placed in the greenhouse control source code, as follows: // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); DeviceAddress DS18B20_0 = { 0x28, 0x29, 0xE8, 0x29, 0x08, 0x00, 0x00, 0x1C }; DeviceAddress DS18B20_1 = { 0x28, 0x0F, 0x09, 0x55, 0x01, 0x00, 0x00, 0x3B }; /* ----- One Wire variables END ---------------------- */

Figure 18-18: DS18B20 64-bit codes in the greenhouse source code. The 64-bit serial codes are unique to my devices, so you need to find your codes before you can continue.

18.3.2.1. setup() The same setup() as described in section 17.3.2.1.

18.3.2.2. loop() The loop() function is identical to the one described in section 17.3.2.2. except that you are heating instead of cooling. The overall control loop() will look like this:

Figure 18-19: Greenhouse control loop().

18.3.3. Temperature regulation At the beginning of this section, the overall temperature regulation method was described. In Figure 18-3, the temperature to maintain and the hysteresis were shown. In the software these definitions are stored in: float tempHysteresis = 2.0; float tempSetpoint = 8.0;

● 272

Home Appliance Hack and IoT Guidebook - UK.indd 272

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

They can be found in the "Greenhouse variables" area. You should change them to suit your needs. Figure 18-19 shows in overall terms how the loop() works. The following describes how it is performed in more detail:

Figure 18-20: Greenhouse regulation. As shown above, there will be two DS18B20 temperature sensors used. In the code, they are called: DeviceAddress DS18B20_0={64bitcode};//Controls the heater. DeviceAddress DS18B20_1={64bitcode};//Outside temperature. A temperature reading is achieved in three steps: • request temperature; • wait until the temperature is ready; • read the actual temperature. It is done this way because it takes some time to get the temperature from the DS18B20 device. This delay is held in the variable DS_DELAY, which by default is set to 750 ms. Once the temperature reading is available, it is uploaded to the emoncms (if available) and actual regulation takes place.

● 273

Home Appliance Hack and IoT Guidebook - UK.indd 273

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

If an emoncms-server has been set up to receive the temperature readings, each heater activation will be uploaded to the emoncms server as well. This information is needed for the emoncms dashboard.

18.3.4. Watchdog functionality The greenhouse control uses the same watchdog functionality as described in Chapter 8. If you see the LED blink or the device constantly reboot, attach it to the Arduino IDE to see if any error messages are displayed on the terminal interface. Of course, you should fix the error before resuming operation.

18.3.5. web interface At present, the greenhouse control includes the following interface:

Figure 18-21: Greenhouse control web interface. The web interface is shown when you enter the IP address of the refrigerator control in a standard browser. The IP address is shown during the power-up of the Greenhouse control if the Arduino IDE is connected to the controller. The web interface is spartan and made purely for test purposes — it is entirely possible to create a more attractive interface.

● 274

Home Appliance Hack and IoT Guidebook - UK.indd 274

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

The web interface is hardcoded into the Wemos firmware and can only be changed by editing and recompiling the code. The HTML code for the web page is made with the function: getPage (string str) Each of the input areas is made as an HTML . Compared to the other web interfaces used in the other constructions, an additional web interface is introduced. This is located in the SPI flash file system described in section 18.3.6. I could have placed it all in the SPI flash file system, but this would have prevented you from using the construction if your ESP8266 lacked flash memory. You can leave out the SPI flash file system and still have the web interface to configure the greenhouse controller.

18.3.5.1 Set temperature and hysteresis The Form to set temperature and hysteresis is shown below:

Enter new triger temperature:
Temperature:


Hysteresis:




Figure 18-22: HTML set temp setpoint and hysteresis. The transmitted request will look like this: temp=&hyst= =[0.00::99.00] =[0.00::14.99] Anything else will return an ERROR.

● 275

Home Appliance Hack and IoT Guidebook - UK.indd 275

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

'tempSetpoint', 'tempHysteresis' are internal Wemos Firmware variables that will be included when the HTML code is transmitted from the Wemos to the browser.

18.3.5.2 Open temperature logger If it is implemented, it is possible to save all measured temperature values in an SPI flash file system together with some HTML files to display the measured temperatures as a graph. The graph is displayed by pressing the "Open temperature logger." button shown in Figure 18-21. This will launch a new tab in the browser. The code to launch the new tab is shown below: Open temperature logger."; . .

Figure 18-23: HTML Open temperature logger. When pressed, the ESP8266 temperature logger interface will be shown but only after some time as it needs to be loaded first:

Figure 18-24: Temperature logger loading JavaScript code and data. Once loaded, it will display an example of Google Charts as shown in the next image.

● 276

Home Appliance Hack and IoT Guidebook - UK.indd 276

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

Figure 18-25: ESP8266 Temperature logger interface. Only the "inside temperature" gets logged as this is used to control the heater. More on this interface and how it's made can be seen in section 18.3.6.

18.3.5.3. Save configuration The temperature and the hysteresis settings will be lost if the greenhouse control is powered off. If the configuration should survive a power cut, the "Save configuration" command needs to be issued. When issued, the temperature and hysteresis variables will be saved in non-volatile memory (EEPROM) and read back during power on. The form to save the configuration looks like this:



Figure 18-26: HTML save configuration code. The transmitted request will look like this: SAVE=Save configuration

18.3.6. SPI Flash File System The firmware for the greenhouse temperature logger requires an SPI flash file system to work.

● 277

Home Appliance Hack and IoT Guidebook - UK.indd 277

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

You should simply follow the descriptions in section 3.3. to prepare this file system. If this feature is not needed, it can be left out by removing the following lines in String getPage(): temp += "Open temperature

logger.";

temp += " temp += " temp += "

function myFunction() {"; window.open(\"index.html\")"; }";

Figure 18-27: Removal of temperature logger code. Within the control firmware, additional code related to the flash file system should be deleted. I will leave this up to you.

18.3.7. Compile "Compilation" of the software is a little different when compared to other software presented in this book. You should do the following during the "compilation" process: • Prepare the files for the web page as described in section 3.3. • Update the content of the files. • Upload the content of the data-dir. • Compile and upload the actual software for the ESP8266. If you don't need to change the content of the data-dir, you can continue to make changes and upload the software as normal.

18.4. Operation As shown in Figure 18-21, the web interface provides the option of setting the temperature and hysteresis while the Greenhouse control is running. Remember to save when complete or the setting will be lost and the hardcoded or EEPROM saved temperature and hysteresis will be loaded.

18.5. Dashboard The basic installation of an emoncms server is described In Chapter 4. To display any data from the Greenhouse control, a dashboard should be configured. The following shows how I designed my dashboard in emoncms.

● 278

Home Appliance Hack and IoT Guidebook - UK.indd 278

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

At first, the emoncms will be empty as shown in this image:

Figure 18-28: No inputs created. ... Unless, of course you have something else running. Data is entered by sending data from the greenhouse controller to the emoncms server via the input API. The input API can be found here: http:///emoncms/input/api On this page, the Read & Write key is found and a description of the string you need to send from the greenhouse controller (in emoncms terms: the remote device) to the emoncms server to post data. The Read&Write key of my emoncms installation is shown below:

Figure 18-29: emoncms read and Read & write API key. The Read & Write key is dropped into the ESP8266 code as shown below: const char* apiKey = "4afb11973ac2b3375ccf3589fca6d32e"; // Insert own write emoncms apikey

Figure 18-30: The API key to Read & Write. In this example, the following information is transmitted to the emoncms server: • the status of the greenhouse heater (on/off); • the inside temperature of the greenhouse temperature; • the outside temperature of the greenhouse.

● 279

Home Appliance Hack and IoT Guidebook - UK.indd 279

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Consequently, you need to send two types of HTTP string: Heater status The first HTTP string with the heater status is this: http://172.16.0.11/emoncms/input/post?node=greenhouse&fulljson= {"ONOFF":1}&apikey=4afb11973ac2b3375ccf3589fca6d32e The ESP8266 code that to implement it: sprintf(tempStr, "%d", 1); sprintf(httpUrl, "%s%s%s%s","/emoncms/input/post?node= greenHouse&fulljson={%22ONOFF%22:",tempStr, "}&apikey= ",apiKey); emoncmsSend();

Figure 18-31: coding of the HTTP string for the emoncms. The first sprintf() assigns the value (1 or 0) to transmit. The next sprintf() builds the JSON input string. The name "ONOFF" is what you will see under Inputs at the emoncms server. This information will only be transmitted when the heater changes state from ON to OFF or OFF to ON. Don't be alarmed by long intervals between sets of information on the heater state. Temperature The next bit of information to transmit are the two temperatures around the greenhouse. This is performed using the following HTTP string: http://172.16.0.11/emoncms/input/post?node= greenhouse&fulljson= {"tempIn":14.19, "tempOut":12.10}&apikey=4eaed071560b279737c4db67341ffc38

The code to implement this is a little tricky as the outside temperature can be negative. The HTTP string generation is therefore divided into four scenarios: Inside temperature

Outside temperature

#1

Temp < 0

Temp < 0

#2

Temp < 0

Temp >= 0

#3

Temp >= 0

Temp < 0

#4

Temp >= 0

Temp >= 0

Table 18-1: Inside/Outside temperature.

● 280

Home Appliance Hack and IoT Guidebook - UK.indd 280

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

The ESP8266 code implementing scenario #4 is shown here: sprintf(tempStr, "\"tempIn\":%d.%02d,\"tempOut\":%d.%02d", (int)tempValue[0], (int)(tempValue[0]*100)%100, (int)tempValue[1], (int)(tempValue[1]*100)%100 ); sprintf(httpUrl, "%s%s%s%s", "/emoncms/input/post?node=greenHouse&fulljson={", tempStr, "}&apikey=",apiKey); emoncmsSend();

Figure 18-32: coding of the HTTP string for the emoncms. The first sprintf() assigns the temperature values to transmit. The next sprintf() builds the JSON input string. The rest of the above scenarios should be self-explanatory. The only difference is that a "-" (minus sign) is assigned relevant places. The name "greenhouse" is what you will see under Inputs at the emoncms server. You can combine the two HTTP strings into one but the information is not related. Therefore, I made the two separate strings. When it all works, you should see something like this:

Figure 18-33: Input nodes from the greenhouse controller. In the above figure, the Process list is empty. To configure the Process list, click the little spanner. Do this for all three entries. Let's start with tempIn input.

● 281

Home Appliance Hack and IoT Guidebook - UK.indd 281

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 18-34: Empty tempIn greenhouse node process list. Since this is the first time, there will be no process defined. The only thing you will have to do is log the input. This is done by selecting "Log to feed".

Figure 18-35: New greenhouse node process. Since the temperature reading is to be received every 2 minutes, "PHPFINA Fixed int / 2m" is selected. Adjust the fixed interval according to what is stated in the ESP8266 variable: const unsigned long intervalTemp Press "Add" to create your new process. Once complete, it will look like this:

● 282

Home Appliance Hack and IoT Guidebook - UK.indd 282

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

Figure 18-36: First greenhouse node process (temp) added. Since this is the only process to add, you will leave the configuration setup. Remember to save ("Changed, press to save") before you leave or your process list will be lost. Do the same for the tempOut and ONOFF (Figure 18-33) inputs. Here you also "Log to feed" but this time you do it a little differently.

Figure 18-37: Greenhouse node ONOFF process. Since you don't know when the heater will be turned ON or OFF, you need to select the PHPTIMESERIES for our "Log to feed". Press "Add" to enter the process and leave the entry by pressing "Changed, press to save". You should now see the following:

● 283

Home Appliance Hack and IoT Guidebook - UK.indd 283

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 18-38: Greenhouse node inputs with newly added processes. If you go to the Feeds page you should see:

Figure 18-39: Greenhouse node feeds. If you experience emoncms errors during this configuration, look for a solution in section 23.4. on Troubleshooting. Once all feeds required for the dashboard are in place, get set to start configuring the dashboard. In my case, I already created two dashboards. Press New to start your new Dashboard.

Figure 18-40: New greenhouse dashboard. Press the "no name" button to change the name of the dashboard. Press the 3rd icon from the right to start editing the new dashboard.

● 284

Home Appliance Hack and IoT Guidebook - UK.indd 284

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

Figure 18-41: Configuration of the greenhouse dashboard. It is now a matter of taste as to how you configure your dashboard. There is plenty of inspiration to be found on the Internet. I will show you the very basics in the following: Start by adding a (white) container.

Figure 18-42: Adding a container to the greenhouse dashboard. Place and drag it to the size you need. It can be altered later if you change your mind. Place another container below the one you just made. It should look like this:

● 285

Home Appliance Hack and IoT Guidebook - UK.indd 285

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 18-43: Two empty containers in the greenhouse dashboard. To show the actual temperature, let's add a nice dial:

Figure 18-44: Adding a (temperature) dial to the dashboard. Place the dial and enter the configuration menu. There are a vast number of variables to set. I changed the following compared to the standard values (those not mentioned here are not changed).

● 286

Home Appliance Hack and IoT Guidebook - UK.indd 286

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

Variable

Value

Comment

Feed

greenhouse:tempIn

Select the temperature node from the dropdown menu.

Max value

25

Enter the max temperature value you expect to reach.

Scale

1

The value to multiply the feed with before displayed

Type

Green Red, zero

Select which type of dial to show.

at the left Min / Max

Yes

Display the min and max value

Min. feed

greenhouse:tempIn

The feed for the min value

Max feed

greenhouse:tempIn

The feed for the min value

Table 18-2: emoncms dial configuration. Once changed, save the configuration and you should see something like this:

Figure 18-45: The temperature dial is fully configured. Each time you enter the dashboard, it will show you the actual temperature of the inside of the greenhouse. To be accurate, the last temperature measurement received from the greenhouse controller. Add a descriptive caption to the temperature dial.

Figure 18-46: The temperature dial with a heading.

● 287

Home Appliance Hack and IoT Guidebook - UK.indd 287

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

As another feedback of the system, the state of the heater can be shown by adding an LED. The only thing necessary to configure is the use of the ONOFF Feed as feed value. The state of the compressor also needs a heading as shown in the next image.

Figure 18-47: Heater state added as an LED. The final thing is to add a graph showing temperature over time. Select rawdata and place it in the bottom container. Configure it as shown in the table below.

Figure 18-48: Configure raw data graph. I changed the following values compared to the standard values (those not mentioned are not changed): Variable

Value

Comment

Feed

Refrigerator:temp

Select the temperature node from the dropdown menu.

Units

1

Enter units

dp

.1

Decimal point

Table 18-3: emoncms temperature graph.

● 288

Home Appliance Hack and IoT Guidebook - UK.indd 288

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

Once complete, your new dashboard should look as shown in the next image. The figure shows a system having collected data for at least a day. Consequently, at first, you're bound to see nothing unless you collected data before the dashboard was created.

Figure 18-49: Greenhouse control dashboard with inside temperature. When you are satisfied with your dashboard, press "Changed, press to save". The new dashboard should appear in the list:

Figure 18-50: Dashboard listing. This was just a rather effortless way of setting things up. There are plenty of examples on the emoncms homepage. The final layout I ended up with is this:

● 289

Home Appliance Hack and IoT Guidebook - UK.indd 289

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 18-51: Final Greenhouse dashboard. Based on the earlier walkthrough, you should be able to figure out how it is made. The greenhouse control has on-board logging of the temperature if you implemented it. You should try to compare the two.

● 290

Home Appliance Hack and IoT Guidebook - UK.indd 290

13-12-2022 11:08

Chapter 18 • Greenhouse Thermostat

Figure 18-52: ESP8266 on-board temperature logging. In the image below, I "zoomed in" on the actual temperature recording while the heater was working.

Figure 18-53: Heater overshoot. You may have noticed that there is an overshoot of the temperature even though the hysteresis should be symmetrical around the required temperature. It's because it takes some time for the temperature to settle in the greenhouse after the heater is turned off. If you want to avoid this, you should change the code so you have one hysteresis above the temperature and another, below it.

● 291

Home Appliance Hack and IoT Guidebook - UK.indd 291

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 19 • 230-VAC Relay Control The refrigerator control and greenhouse thermostat both turn something connected to the 230-VAC mains powerline On and Off – based on a temperature input. The following construction uses the basic software of the refrigerator/greenhouse controller to turn a 230-VAC Relay On/Off. Some of the hardware solutions are for inspiration only. Others will be described in more detail.

19.1. Software 19.1.1. Wemos firmware 19.1.1.1.setup() The setup() is quite simple and is given below.

Figure 19-1: On/Off control setup(). The EEPROM stores the status of the relay output, so the status will be remembered in case the 230-VAC Relay control is power cycled. Besides setting the network variables, you need to change #define RELAY_OUT D1 according to your hardware design. In my case, I am using pin D1 to drive the On/Off output.

19.1.1.2. loop() The loop() is as simple as the setup() and can be seen below.

● 292

Home Appliance Hack and IoT Guidebook - UK.indd 292

13-12-2022 11:08

Chapter 19 • 230-VAC Relay Control

Figure 19-2: On/Off control loop(). The main task is to check for incoming requests from the internet.

19.1.2. Web interface Currently, the 230-VAC Relay control includes the web interface shown below.

Figure 19-3: Lamp control web interface. The web interface is shown when you enter the IP address of the 230-VAC Relay control in a standard browser. The IP address is shown during the power-up of the 230-VAC Relay control if the Arduino IDE is connected. The web interface is made purely for test purposes and it will most likely be possible to make a better interface. The web interface is hardcoded into the Wemos firmware and can only be changed by editing and recompiling the code. The HTML code for the web page is made with the function getPage (string str)

● 293

Home Appliance Hack and IoT Guidebook - UK.indd 293

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The ON/OFF functionality is made as HTML .

19.2. Hardware 19.2.1. SSR My preferred hardware for a 230-VAC On/Off switch includes the refrigerator control that uses the SSR as described in Chapter 17. If you leave the temperature sensors out this construction is ready to switch 230-VAC on and off based on input from the Internet.

19.2.2.1. Housing Use the same as described in section 17.2. Like in section 17.2., you have to place the SSR in a separate suitable box.

19.2.2. Relay If you don't want to use an SSR to turn the 230-VAC On or Off, go for a standard relay. The relay may be a 230 VAC, a 12 VDC, or a 5 VDC type — it all depends on your needs. The schematic in Figure 17-5 from the refrigerator control can be used with just one modification.

Figure 19-4: Relay On/Off control schematic.

● 294

Home Appliance Hack and IoT Guidebook - UK.indd 294

13-12-2022 11:08

Chapter 19 • 230-VAC Relay Control

The main difference is diode D3 (a type 1N4148). This diode acts as a back-emf suppressor. It is required as a counter-electromotive force and is effectively introduced in the relay circuit when it is turned off. Take a look at: http://progeny.co.uk/back-emf-suppression/ Components related to the temperature sensor have also been removed. Pay attention to the fact that you only have 5 volts DC at your disposal when selecting the relay. Depending on the current required to trigger the relay, you may also need to change resistor R4, or simply remove it. The PCB in Figure 17-6 can be used if you remember to mount D3 across the terminals for the relay.

19.2.2.1 Housing Use the same as described in section 17.2. As in section 17.2. You have to place the 230-VAC relay in a separate suitable box.

19.2.3. Relay Shield Instead of using the solutions suggested in the previous sections, consider a ready-made design available as a Relay Shield like the one shown here: https://wiki.wemos.cc/products:d1_mini_shields:relay_shield The image below shows the diagram of the relay shield. The only difference compared to the solution in section 19.2.2. is that you can change the data output pin from the ESP8266, which controls the relay.

● 295

Home Appliance Hack and IoT Guidebook - UK.indd 295

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 19-5: Wemos Relay Shield schematic. The following image shows how this can be achieved:

Figure 19-6: Wemos Relay Shield IO settings. You may also come across relay shields where the Data-Out pin is hardwired through the PCB layout. When you buy your shield, pay attention to how it is done. In any case, it is possible to adapt to the actual design in the software by changing the define: #define RELAY_OUT D1

19.2.3.1. Housing There is no ready-made housing for the construction where the relay shield is used.

● 296

Home Appliance Hack and IoT Guidebook - UK.indd 296

13-12-2022 11:08

Chapter 19 • 230-VAC Relay Control

It should be possible to use the standard housing in sections 11.1. or 11.2. if the height of the housing is increased with the height of the relay shield. Holes for the 230-VAC wires should also be added. Alternatively, the 230-VAC relay can be placed in a separate housing like with the SSR.

19.2.4. Alternative 230-VAC relay On/Off construction The 230-VAC relay On/Off construction shown here is purely for inspiration and most of the (mechanical) construction you will have to figure out yourself. Warning Once again I need to state that what is shown here is even riskier than what was made for the refrigerator and greenhouse control. You are dealing with live 230VAC mains. It is entirely at your own risk to try and follow what is shown here. I had some old remote 230-VAC socket controls lying around and decided to try them to see if it was possible to install the necessary components to make it controllable from the Internet in the same housing. The result can be seen here.

Figure 19-7: Compact 230VAC relay switch. The 230-VAC fuse and relay are reused from the original electronics inside the original controller. The 230-VAC / 5-VDC power supply is ready-made and for sure can be found on the Internet — I can't say if it complies with your local standards. Together with a few additional components, the circuit is built on a piece of Vero board, a.k.a. perfboard.

● 297

Home Appliance Hack and IoT Guidebook - UK.indd 297

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Others have ventured to do the same. A few constructions can be seen here: https://www.instructables.com/ESP8266-5-internet-controlled-switch/ https://myesp8266.blogspot.com/2015/04/first-iot-device.html?spref=pi&m=1

● 298

Home Appliance Hack and IoT Guidebook - UK.indd 298

13-12-2022 11:08

Chapter 20 • Standalone Temperature Sensor

Chapter 20 • Standalone Temperature Sensor Having used the DS18B20 sensor in both the Refrigerator Control and the Greenhouse Thermostat projects, it was straightforward to make a standalone construction with the DS18B20. I soon discovered that there are plenty of situations where a wireless temperature sensor can come in handy. Especially if you power it via battery or a powerbank. The schematic below should be familiar.

Figure 20-1: DS18B20 temperature standalone schematic. Compared to Figure 17-5 the "power-LED" has moved from D2 to D5 but this was due to ease of soldering. The LED can be placed at any of the non-used pins. The DS18B20 is illustrated in Figure 17-9 and Figure 17-10. Pay attention to connecting the DS18B20 to 3.3 V and not 5 V. Also, do not invert VDD and GND. The PCB shown in Figure 17-6 can be used. I soldered the components directly onto the LOLIN/Wemos PCB as shown here.

● 299

Home Appliance Hack and IoT Guidebook - UK.indd 299

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 20-2: DS18B20 temperature sensor housing.

20.1. Housing The housing for the DS18B20 temperature sensor is shown in Figure 20-2. The variables to print the housing shown in Figure 20-2 are as follows: • SENSOR = 1; // possible values [0,1] • RESET = 1; // possible values [0,1] • ANTENNA = 0; // possible values [0,1] • LED = 1; // possible values [0,1] • FASTENER = 1; // possible values [0,1] The fully assembled housing appears below.

Figure 20-3: DS18B20 temperature sensor fully assembled.

● 300

Home Appliance Hack and IoT Guidebook - UK.indd 300

13-12-2022 11:08

Chapter 20 • Standalone Temperature Sensor

If you are using the LOLIN D1 mini pro PCB as housing, use the housing described in section 11.2. The variables to print the LOLIN D1 mini pro housing could be as follows: • SENSOR = 1; // possible values [0,1] • RESET = 1; // possible values [0,1] • ANTENNA = 0; // possible values [0,1] • LED = 1; // possible values [0,1] • FASTENER = 1; // possible values [0,1] • USB_SUPPORT = 1; // possible values [0,1] • BATTERY = 0; // possible values [0,1]

20.2. Software There are two variants of the software for a standalone DS18B20 temperature sensor: Software with OTA enabled and 5V DC powered Software with sleep functionality and battery-powered Both variants transmit the temperature to the emoncms server — this should be up and running as described in Chapter 4. There will be no webpage hosted on the device. The only way to see the temperature is via the emoncms server.

20.2.1. Additional libraries For this to work, the following libraries need to be installed: DallasTemperature:

Figure 20-4: DallasTemperature library installation.

● 301

Home Appliance Hack and IoT Guidebook - UK.indd 301

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

OneWire:

Figure 20-5: OneWire library installation. This is all done via the Library Manager (Tools -> Manage Libraries)

20.2.2. OTA-Enabled Software The software for the temperature sensor is OTA enabled by default as described in section 9.1.2.

20.2.2.1. Wemos firmware If you are familiar with how the Refrigerator Control firmware got designed, there should be no surprises with the standalone temperature sensor software. Below is the setup() shown:

Figure 20-6: OTA enabled temperature sensor setup(). The flowchart below shows the loop()structure where the temperature measurement takes place.

● 302

Home Appliance Hack and IoT Guidebook - UK.indd 302

13-12-2022 11:08

Chapter 20 • Standalone Temperature Sensor

Figure 20-7: OTA enabled temperature sensor loop(). To access your device for OTA-update, enter the following in your preferred browser: http:// or http://esp8266-webupdate.local/firmware Remember to change the following variables to fit your needs: const char* ssid const char* password const char* emoncmsSRV const char* host const char* update_username const char* update_password IPAddress ip( x, x, x, x ); IPAddress gateway( x, x, x, x ); const char* apiKey const char* node Descriptions of the variables can be found in the source code of the firmware.

20.2.2.2. Dashboard You should make a dashboard where you can display the readings from your temperature sensor.

● 303

Home Appliance Hack and IoT Guidebook - UK.indd 303

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Follow the descriptions in sections 17.5. or 18.5. with the information on the temperature Input. I made the following dashboard for my summer house:

Figure 20-8: Ferren temperature dashboard. Note In Figure 17-45 and Figure 18-35, the expected time between temperature sensor readings is set. Please adjust the value to the content of the variable: const int sleepTimeS in your source code.

20.2.3. Sleep-mode enabled software If you are unable to power the DS18B20 temperature sensor using a standard 230-VAC power supply but need some kind of battery support, the lifetime of the battery needs to be extended as much as possible. One way of doing this is to place the ESP8266 in sleep mode in between temperature measurements as described in section 2.1.2.

20.2.3.1. Hardware sleep-mode differences and setup If you want an easy implementation of a battery-powered application, the LOLIN PCB shown to the left in Figure 2-2 is an easy choice. There is a ready-made battery interface available on the PCB, so all that is needed is to attach a Lithium battery.

● 304

Home Appliance Hack and IoT Guidebook - UK.indd 304

13-12-2022 11:08

Chapter 20 • Standalone Temperature Sensor

If you select one of the other PCBs shown in Figure 2-2, a battery shield is required like this one: https://www.wemos.cc/en/latest/d1_mini_shield/battery.html To enable sleep mode, the two pads beside the "SLEEP" text need to be shortened. The pads can be found on the bottom side / back of the PCB.

Figure 20-9: LOLIN SLEEP pads. The LOLIN diagram shows the RST pin connected to GPIO16 by shortening the pads.

Figure 20-10: LOLIN SLEEP pads schematic. The same applies to the Wemos battery shield PCBs. If there is a need for monitoring the voltage level of the attached Lithium battery, the two pads next to "BAT-A0" should be shortened. Refer to the image below:

Figure 20-11: LOLIN BAT-A0 pads.

● 305

Home Appliance Hack and IoT Guidebook - UK.indd 305

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The LOLIN diagram shows that the VBAT is connected to A0 by shortening the pads. By doing this, it is possible to monitor the VBAT voltage of the Lithium battery.

Figure 20-12: LOLIN BAT-A0 pads schematic. Note Pay attention to the polarity of the Lithium battery when connected to "PH-2-WO" in Figure 20-12. If the polarity is not correct, the TP4054 chip gets fried and will not recover after the removal of the battery. If you want to measure the Lithium battery level, use the following command: analogRead(A0); You will have to figure out the relationship between the measured value and the actual battery level by taking measurements directly at the Lithium battery. I mainly use it as an indication of the delta change in the battery level. When it suddenly starts dropping, it is time to charge the battery.

20.2.3.2. Wemos firmware The temperature firmware with sleep-mode enabled is not that much different from the standard OTA enabled temperature firmware – except the loop() only measures the temperature once and then enters sleep mode. Below, is the setup() shown:

● 306

Home Appliance Hack and IoT Guidebook - UK.indd 306

13-12-2022 11:08

Chapter 20 • Standalone Temperature Sensor

Figure 20-13: Sleep-enabled temperature sensor setup(). Since it is difficult to perform an OTA update of the firmware while the device is asleep, this feature has been removed. Below, the loop() is shown:

Figure 20-14: Sleep enabled temperature sensor loop(). The main difference is "Enter sleep mode", which, as it says, enters the Wemos device into sleep for a predefined period. The sleep period is defined via the following variable: // Time to sleep (in seconds): const int sleepTimeS = 60;

● 307

Home Appliance Hack and IoT Guidebook - UK.indd 307

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Sleep mode is achieved via the following function call: ESP.deepSleep(sleepTimeS * 1000000);

20.2.3.3. Dashboard Use the same dashboard as the OTA-enabled temperature sensor. Your tool "emoncms" cannot tell how the reading was transmitted except the time between the measurements needs to be adjusted from seconds to minutes/hours/days depending on the value of sleepTimeS.

● 308

Home Appliance Hack and IoT Guidebook - UK.indd 308

13-12-2022 11:08

Chapter 21 • Temperature, Pressure, and Humidity Sensor

Chapter 21 • Temperature, Pressure, and Humidity Sensor Compared to the other designs in this book, this does not include a PCB and housing. The design is included anyhow because I needed to measure barometric pressure and humidity. Both shields include a temperature sensor but only one of them should be used as they don't provide the same measured value. There is also the option of using the DS18B20 sensor used elsewhere and skipping the built-in temperature sensors.

21.1. Schematic The setup is extremely simple:

Figure 21-1: Schematic. It consists of the Wemos/LOLIN board as the main board and two shields: • Barometric Pressure (HP303B); • Digital temperature and humidity sensor (SHT30). both connected via the I2C bus. The figure below shows the actual setup I am using:

● 309

Home Appliance Hack and IoT Guidebook - UK.indd 309

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 21-2: Combined temperature / pressure / humidity setup. Since the setup was used for other test purposes as well, it has an external antenna. The connection of an external antenna can be seen in Chapter 10. and is not required if there is sufficient WiFi coverage. The TFT & I2C connector shield is described here: https://www.wemos.cc/en/latest/d1_mini_shield/tft_i2c_connector.html It is not necessary but makes life easier by connecting the shields. Information on the SHT30 can be found in section 2.9.1. and information about the HP303B in section 2.9.2. For convenience, the Barometric Pressure Shield will in the future just be mentioned as the "Pressure Shield".

21.2. Software The software for this design consists of firmware for the Wemos/LOLIN, a small web interface, and an emoncms-created dashboard. Both the web interface and the dashboard show the measured values, with the option for the dashboard to display graphs. Choose whichever suits your needs. For now, I have not set up the SPIFFS or LittleFS to collect data — if you follow some of the other designs, it should be fairly simple.

● 310

Home Appliance Hack and IoT Guidebook - UK.indd 310

13-12-2022 11:08

Chapter 21 • Temperature, Pressure, and Humidity Sensor

21.2.1. Additional libraries Some additional libraries need to be installed. Unfortunately, they cannot be installed via the Arduino Library Manager.

21.2.1.1. HP303B Support for the HP303B can be found by downloading the library from this link: https://github.com/wemos/LOLIN_HP303B_Library The download contains a library named: LOLIN_HP303B_Library-master The full library is copied to the folder /libraries. The can be found in the Arduino IDE "Preferences". If the GitHub link is not available, go to the shields homepage and see if they provide a working link: https://www.wemos.cc/en/latest/d1_mini_shield/barometric_pressure.html The library is used by including the following file: LOLIN_HP303B.h in your code.

21.2.1.2. SHT3x Support for the SHT3x can be found by downloading the library from this link: https://github.com/wemos/WEMOS_SHT3x_Arduino_Library The download contains a library named: WEMOS_SHT3x_Arduino_Library-master The full library is copied to the /libraries. The can be found in the Arduino IDE "Preferences".

● 311

Home Appliance Hack and IoT Guidebook - UK.indd 311

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

If the GitHub link is not available, go to the shields homepage and see if they provide a working link: https://www.wemos.cc/en/latest/d1_mini_shield/sht30.html The library is used by including the following file: WEMOS_SHT3X.h in your code.

21.2.2. Firmware For this to work, the two shields need to be assigned an I2C address, which should be different from each other. I am using the default I2C addresses: • SHT30: 0x45, and • HP303B: 0x77 The present firmware is straightforward and requires a 5-volt DC power supply. For whatever reason, if you need to use the sensors in a remote place without 230 VAC, they can be battery powered and the guidelines in section 20.2.3. can be followed to save power.

21.2.2.1. setup() The setup() function has been split up as shown below:

Figure 21-3: setup() for temp press and hum sensors. There should be no surprises with the setup() as everytyhing is explained in the code. There is no separate configuration/setup of the SHT30 sensor.

21.2.2.2. loop() The loop() is quite straightforward and can be seen below.

● 312

Home Appliance Hack and IoT Guidebook - UK.indd 312

13-12-2022 11:08

Chapter 21 • Temperature, Pressure, and Humidity Sensor

Every 60 seconds, the three values are recorded and sent to the emoncms server. The loop-time is controlled by the variable: sleepTimeS as shown at the beginning of the code.

Figure 21-4: loop() for temp press and hum sensors. To make the code more readable, three small functions have been created: • GetPressure() • GetTemperature() • GetHumidity() If the built-in temperature sensors in one of the shields are used, pay attention to selecting the correct temperature sensor as an argument: [1,2] when the function GetTemperature() is called. Refer to the comments in the code.

21.2.3. Web interface The web page is straightforward and made using the same design guidelines as the others in this publication.

Figure 21-5: Combine Temp, Press, and humidity web page.

● 313

Home Appliance Hack and IoT Guidebook - UK.indd 313

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

This web page is also hard-coded into the firmware and implemented through the function: String getPage (String str) As it is really easy to implement OTA into the firmware, it's done here as well. The firmware is updated OTA-style by entering the following in a browser: http://< IP address>/firmware. This will present the following webpage:

Figure 21-6: Combi OTA update. Like with the other designs, I am using password protection.

21.3. Dashboard The dashboard can be seen below:

Figure 21-7: Temp, Press, and Humid emoncms dashboard. The input values received from the Wemos/LOLIN are:

Figure 21-8: Inputs received from Temp, Press, and Humid sensors.

● 314

Home Appliance Hack and IoT Guidebook - UK.indd 314

13-12-2022 11:08

Chapter 21 • Temperature, Pressure, and Humidity Sensor

As shown in Figure 21-8, all recorded values are received at the same time. The HTTP-string with the temp, pressure and humidity status is given below: http://172.16.0.11/emoncms/input/post?node=Combi&fulljson= {%22temp%22:22.46,%22press%22:100735,%22humid%22:45.64}&apikey= 4afb11973ac2b3375ccf3589fca6d32e The ESP8266 code implementing this looks like this: pressure = GetPressure( 1 ); sprintf(pressStr, "%d", pressure); temperature = GetTemperature( 2 ); sprintf(tempStr, "%d.%02d", (int)temperature, (int)(temperature*100)%100); _humidity = GetHumidity( 1 ); sprintf(humidStr, "%d.%02d", (int)_humidity, (int)(_humidity*100)%100); String httpURL

= "/emoncms/input/post?node=";

httpURL += node; httpURL += "&fulljson={"; httpURL += "%22temp%22:"; httpURL += tempStr; httpURL += ",%22press%22:"; httpURL += pressStr; httpURL += ",%22humid%22:"; httpURL += humidStr; httpURL += "}&apikey="; httpURL += apiKey;

Figure 21-9: coding of the HTTP string for the emoncms. The first three sprintf()assign the recorded values to pressStr, tempStr, and humidStr to be used in the String httpURL. The variable "node" holds the node-name (Combi) to be displayed in emoncms and can easily be changed at the top of the source code. The configuration for each of the dials/graphs will be shown in the following.

● 315

Home Appliance Hack and IoT Guidebook - UK.indd 315

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Temperature dial: Variable

Value

Comment

Feed

temp

Select the temperature node from the dropdown menu

Max value

40

Enter the max temperature value you expect to reach

Scale

1

The value to multiply the feed with before displayed

Units

Celsius (0C)

Type

Green Red,

Select which type of dial to show

zero at the left Min. / Max.

Yes

Display the min and max value

Min. feed

temp

The feed for the min. value

Max. feed

temp

The feed for the min. value

Table 21-1: emoncms temperature dial configuration. Temperature graph: Variable

Value

Comment

Feed

temp

Select the temperature node from the dropdown menu

1

Scale by

Scale

Table 21-2: emoncms temperature graph configuration. Humidity dial: Variable

Value

Comment

Feed

humid

Select the temperature node from the dropdown menu.

Max. value

100

Enter the max temperature value you expect to reach.

Scale

1

The value to multiply the feed with before displayed

Units

Percent (%)

Type

Green Red,

Select which type of dial to show

zero at the left Min. / Max.

Yes

Display the min and Max value

Min. feed

humid

The feed for the min. value

Max. feed

humid

The feed for the min. value

Table 21-3: emoncms humidity dial configuration. Humidity graph: Variable

Value

Comment

Feed

humid

Select the temperature node from the dropdown menu.

Scale

1

Scale by

Table 21-4: emoncms humidity graph configuration.

● 316

Home Appliance Hack and IoT Guidebook - UK.indd 316

13-12-2022 11:08

Chapter 21 • Temperature, Pressure, and Humidity Sensor

Pressure dial: Variable

Value

Comment

Feed

press

Select the temperature node from the dropdown menu.

Max value

1200

Enter the max temperature value you expect to reach.

Scale

.01

The value to multiply the feed with before displayed

Units

Hectopascal (hPa)

Type

Light dark royal

Select which type of dial to show.

blue, zero at the left Min / Max

Yes

Display the min and max value

Min. feed

press

The feed for the min value

Max feed

press

The feed for the min value

Table 21-5: emoncms pressure dial configuration. Pressure graph: Variable

Value

Comment

Feed

press

Select the temperature node from the dropdown menu.

Scale

.01

Scale by

Table 21-6: emoncms pressure graph configuration. With the above information, I believe you should be able to build the dashboard shown in Figure 21-7. If not, look at some of the more detailed descriptions.

● 317

Home Appliance Hack and IoT Guidebook - UK.indd 317

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 22 • IKEA Trådfri Controller I know this is a huge promotion for the IKEA Trådfri wireless products, and just to make it clear for the last time: "I don't benefit from doing this in any way". I came across a lot of people on the Internet — including some of my university colleagues who performed these hacks with ZigBee modules and could not resist looking into their hacks. You should go out and investigate yourself. Yes, there are pros and cons to using IKEA Trådfri products — for now, it's mainly pros for my sake.

22.1. Description of the concept Over the years, I've owned several remote controls for 230-VAC (AC line) switches. Sooner rather than later it was evident that they were not up to present-day standard – and I got tired of maintaining my software to control them! I therefore turned my attention to the IKEA Trådfri family of accessories. This was mainly because of their attractive price and the number of available hacks. Since I wanted to keep some of the (dependable) hardware from the old 230-VAC switches and only replace the control software, I needed some kind of link between the old hardware and the new IKEA Trådfri control software. The solution became the "IKEA Trådfri Controller", which will be described in the remainder of this Chapter.

Figure 22-1: IKEA Trådfri controller diagram. To make this setup work, the bare minimum of IKEA components is: • one IKEA Trådfri gateway; • one or more Trådfri ZigBee modules as described in section 22.2.; • an IKEA Trådfri remote control (not shown in the image and not strictly necessary).

● 318

Home Appliance Hack and IoT Guidebook - UK.indd 318

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

The remote control is used to connect the ZigBee module to the gateway, and the gateway is used to control the "IKEA Trådfri controller" via the mobile app. If you have tried to set up a gateway and lamp before, there should be no surprises here, else, refer to section 22.6. I recommend reading section 22.6. in any case if you haven't tried to add a new IKEA Trådfri bulb before. I am using the following remote control to configure the ZigBee modules. I expect though that any of the Trådfri remote controls does the trick.

Figure 22-2: IKEA Trådfri remote control. As you will see in section 22.6., it is not strictly necessary to use a remote control to add a new device. However, I find it easier this way. You could rightly argue that the Wemos is not needed in the IKEA Trådfri Controller but it does control the way the On/Off pulses are treated/interpreted as received from the ZigBee module. The output of the ZigBee module is PWM-style and looks like this:

Figure 22-3: ZigBee module PWM output.

● 319

Home Appliance Hack and IoT Guidebook - UK.indd 319

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

From the start to the beginning, the signal is around 500 ms. At the beginning, it looks like this:

Figure 22-4: ZigBee module PWM signal at the start. At the end, it looks like this:

Figure 22-5: ZigBee module PWM signal at the start. If you are familiar with PWM signals there should be no surprises.

● 320

Home Appliance Hack and IoT Guidebook - UK.indd 320

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

Since the signal is PWM, you will see that the bulb comes on and fades gradually when it is turned on or off. This is a nice feature but not in this case as a clean on/off is needed. The PWM has to be removed like the "contact bounce" discussed in section 16.3.1.3. When a clean ON or OFF pulse has been detected, there are different possibilities for interpretation of the ZigBee pulses: • A ZigBee ON signal is detected: - make the output of assigned MCP23017 I/O permanently high - generate a pulse at the assigned MCP23017 On output • A ZigBee OFF signal is detected: - make the output of the assigned MCP23017 I/O permanently low - generate a pulse at the assigned MCP23017 Off output. The generated pulses can be delayed or made to consist of several pulses. Figure 22-1 indicates that the Wemos controls some kind of switch. The switch can be of any type and it is up to you to make the necessary interface based on the 3.3 VDC output. The Wemos module can also be used to collect data for further processing if required. So, if required, the ZigBee pulses received can be collected and sent to the emoncms server for visualization.

22.2. Getting to the IKEA trådfri modules This section will show how an IKEA bulb is taken apart to get access to the ZigBee wireless control PCB. The bulb in question is the one below:

● 321

Home Appliance Hack and IoT Guidebook - UK.indd 321

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-6: IKEA Trådfri bulb. which was found here at the time of writing: https://www.ikea.com/gb/en/p/tradfri-led-bulb-e27-250-lumen-wireless-dimmablewarm-white-globe-clear-00517630/ The little PCB to be salvaged ("repurposed") is this one:

Figure 22-7: MGM210L Silabs module. It is called "MGM210L" and a standard module from Silabs. You can read more about the module here: https://www.silabs.com/documents/public/data-sheets/mgm210l-datasheet.pdf

● 322

Home Appliance Hack and IoT Guidebook - UK.indd 322

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

and here: https://github.com/basilfx/TRADFRI-Hacking/blob/master/MODULES.md#mgm210l The module can be bought at several places including: https://uk.farnell.com/search?st=mgm210l This will save you the hassle of taking the IKEA bulb apart. As you will find out, it is far cheaper to salvage an IKEA bulb than to buy a new MGM210L module. Note Before continuing, I have to emphasize that after this dig-out of the MGM210L module, the IKEA bulb will no longer work. You will have a small nifty 230 VAC to 3.3 VDC adaptor though. I also have to stress that the dismantling of the IKEA bulb may require some craft and skills and there may be a potential risk of hurting yourself depending on how you manage the task of dismantling the bulb. The glass bulb itself may implode and shatter in your hands if you are not careful — see below.

Figure 22-8: Broken IKEA bulb. Having said this, let's get started. First, remove the contact point at the bottom of the bulb. It is a push fit and should easily be removable using a small knife.

● 323

Home Appliance Hack and IoT Guidebook - UK.indd 323

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-9: Removed contact point. Next, remove the metal socket.

Figure 22-10: Removed metal socket. This may require pliers or similar and as you can see, will damage the metal socket. So far, the procedure should have been fairly easy. You are now going to remove the black plastic part which holds the actual electronics controlling the LED strip in the bulb. The black plastic part is somehow glued or melted to the glass bulb and needs to be cut off.

● 324

Home Appliance Hack and IoT Guidebook - UK.indd 324

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

This is done by cutting away plastic where the black plastic meets the glass bulb. Once removed, you may notice that there is no contact between the glass and the black plastic, but something is in the way preventing it from being dismantled. This is because the LED strip is mounted to the PCB using two wires. These have to be taken out of the PCB using brute force... or if you can find the space, simply by cutting them. When the black plastic part has been removed from the glass, it should look like this:

Figure 22-11: Electronic parts removed. The next challenge is to get the control PCB out of the black plastic. It is somehow glued in place and can be tricky/fiddley to get out. By pressing on the end where the split was placed in Figure 22-9, it should be possible to get the PCB out. The main concern during this process is to NOT to damage the "MGM210L". After successful dismantling, the following parts should be available:

● 325

Home Appliance Hack and IoT Guidebook - UK.indd 325

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-12: IKEA Trådfri bulb dismantled into parts. It is now time to release the MGM210L module from the bulb control PCB, which can be seen below with the MGM210L module in the middle of the assembly.

Figure 22-13: IKEA Trådfri control board. In doing so, a soldering iron with ample sufficient heating power is required so the MGM210L can be released from the control PCB. This is a tricky process and some experience of dismantling things without breaking will come in handy. This is what it should look like after a successful dismantling.

● 326

Home Appliance Hack and IoT Guidebook - UK.indd 326

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

Figure 22-14: The MGM210L device was removed from the control board. If you want an easy separation and don't want to reuse the remaining electronics, simply extend the gap left by the MGM210L module by cutting the top and bottom of the main electronic PCB. See the image below.

Figure 22-15: Easy separation of the MGM210L module.

● 327

Home Appliance Hack and IoT Guidebook - UK.indd 327

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

22.3. Schematic The schematics of the IKEA Trådfri controller can be seen here:

Figure 22-16: IKEA Trådfri controller schematic. Located in the center of Figure 22-16 is the Wemos PCB with some support circuitry around it. The present board supports 4 ZigBee modules which are attached via J8-J11. Diodes D1–D4 show what is happening at the output of the ZigBee modules. Each output from the ZigBee modules goes to the Wemos PCB where the signal is sampled for further processing. Since four ZigBee modules can be attached to the IKEA Trådfri controller, the U2 I/O-expansion module is required since the Wemos PCB does not have enough I/O pins. U2 is an MCP23017 "16-Bit I2C I/O Expander with Serial Interface". The datasheet can be found here: https://www.microchip.com/en-us/product/MCP23017

● 328

Home Appliance Hack and IoT Guidebook - UK.indd 328

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

Only Port-A is used, so if you like you can make use of Port-B for whatever you like. Remember to install relevant Arduino libraries to use the module. Refer to section 22.5.3. For each of the I/O-pins at Port-A, an LED, and resistor are attached. This indicates the status of the I/O pin. A small solder pad is available to bring out the signal to additional external electronics. Remember to bring GND to peripheral electronics as well. Since the communication to and from the MCP23017 module is done via I2C, the module needs an address. This is done using jumpers J1, J2, and J3. They can be connected to either GND or 3.3 V. Address

A2

A1

A0

0x00

GND

GND

GND

0x01

GND

GND

3.3V

0x02

GND

3.3 V

GND

0x03

GND

3.3 V

3.3 V

0x04

3.3 V

GND

GND

0x05

3.3 V

GND

3.3 V

0x06

3.3 V

3.3 V

GND

3.3 V

3.3 V

3.3 V

0x07

Table 22-1: MCP23017 addressing. Finally, the power supply around U3 (L78L33) should be familiar. A PCB was constructed to accommodate all components, see Figure 22-17.

● 329

Home Appliance Hack and IoT Guidebook - UK.indd 329

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-17: 3D IKEA Trådfri controller PCB. The associated PCB layout can be seen in section 24.5.

22.4. Mechanical assembly The assembly of the PCB should be straightforward since there are no SMD components. The only challenge are the Trådfri ZigBee modules. The photo below shows the prototype of the IKEA Trådfri controller PCB.

Figure 22-18: Prototype of the IKEA Trådfri controller PCB. The PCB is equipped with a LOLIN PCB. A Wemos will also fit.

● 330

Home Appliance Hack and IoT Guidebook - UK.indd 330

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

22.4.1. I2C Address jumpers The address jumpers mentioned in Table 22-1 can be omitted and small wire shortages soldered instead. The I2C address of the MCP23017 should not change when it is first selected. I have chosen the address 0x00.

Figure 22-19: I2C address coding and connection pads.

22.4.2. Outside connectors J16 – J23 are outside connections to the devices that need to be controlled by the IKEA Trådfri controller. Descriptions of the connections can be found on the PCB. There are no "nice" connectors to the outside. Instead, a wire has to be soldered directly to the PCB from the controlled device. Remember to also bring out GND to the outside electronics.

● 331

Home Appliance Hack and IoT Guidebook - UK.indd 331

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-20: Power jumpers to the ZigBee modules.

22.4.3. ZigBee power jumpers Jumpers J4 – J7 are required to be able to power-off any ZigBee modules that are not to be configured. There is more about this in section 22.6.1.

22.4.4. LED series resistors Although I am giving some suggestions for the LED series resistors in Figure 22-16 - the schematics, you have to evaluate and calculate the correct value based on the actual LEDs you plan to use. As shown in Figure 22-18 I am using 3-mm green, red, yellow, and blue LEDs and they need different resistors. Also, keep in mind that the LEDs operate from a 3.3 VDC supply rail.

22.4.5. ZigBee modules The layout of the PCB is such that the ZigBee module can be mounted in the same way as the IKEA lamp it was extracted from.

● 332

Home Appliance Hack and IoT Guidebook - UK.indd 332

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

Figure 22-21: The MGM210L ZigBee module mounted in IKEA control PCB. For simplicity, I prefer to mount three small wires on the module as shown in Figure 22-22. If you need to desolder the ZigBee device, that's easier to do with the three small wires.

Figure 22-22: IKEA ZigBee module with small wires solder at the PCB. Figure 22-23 shows the ZigBee module mounted on the PCB with the three small wires connected by soldering.

● 333

Home Appliance Hack and IoT Guidebook - UK.indd 333

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-23: ZigBee module suggested mounting.

22.4.6. Miscellaneous The Wemos/LOLIN board is mounted in single-row pin sockets to be able to remove the device. The same applies to the MCP23017, which is also inserted in a socket.

Figure 22-24: Socket mount for the Wemos/LOLIN and MCP23017.

● 334

Home Appliance Hack and IoT Guidebook - UK.indd 334

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

The IKEA Trådfri controller can be powered from the USB interface at the Wemos/LOLIN board or via the USB-B mini adaptor (J24). If powered via the Wemos/LOLIN board, J24 can be left out but the 3.3-VDC power supply is still required.

22.5. Software The software for the IKEA Trådfri controller consists of: • the usual firmware to drive the Wemos/LOLIN; • an onboard web interface; • an emoncms dashboard. You could argue that the onboard web interface and the emoncms dashboard are not required because the IKEA app is controlling the device. On the other hand, I made the interfaces available just in case you need something that's not found in the IKEA app.

22.5.1. Wemos firmware There are two types of firmware for the IKEA Trådfri controller: • "test-and-debug" firmware; • the actual controller firmware. They will each be described in detail below. Note When you upload firmware to the IKEA Trådfri controller, turn off the Trådfri1 ZigBee module. If you don't, the Arduino IDE will not be able to contact the Wemos/LOLIN device.

22.5.1.1. "Test-and-debug" setup() The test-and-debug firmware has to be used when the PCB has been assembled to verify that the LEDs are mounted correctly with the correct resistor size. It is to be used only once and then you only need to pay attention to the actual controller firmware. Note If the ZigBee modules are already mounted when you want to run the test, remove the jumpers to the 3.3 VDC rail. If not, you don't, you will not be able to light up the LEDs marked D1–D4. The setup() is as simple as it can be and is shown below:

● 335

Home Appliance Hack and IoT Guidebook - UK.indd 335

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-25: IKEA Trådfri controller test setup() The only new step is the initialization of the MCP27017, which is very straightforward.

22.5.1.2. "Test-and-debug" loop() The loop() function is equally simple as the setup() and is shown below.

Figure 22-26: IKEA Trådfri controller test loop() During the test, messages will be printed on the serial communication in case nothing blinks.

22.5.1.3. Controller setup() To make the overall controller firmware simpler to read and maintain, all information about the ZigBee modules is located in the "struct" shown here: struct IKEA_module { unsigned int

input;

unsigned int

MCP_pin_on;

unsigned int

MCP_pin_off;

unsigned long pulseMillis; unsigned long lastpulseMillis; int

state;

byte

sendMessage;

byte

interruptCounter;

int

numberOfInterrupts; };

Figure 22-27: struct IKEA_module.

● 336

Home Appliance Hack and IoT Guidebook - UK.indd 336

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

There is information on the Wemos/LOLIN input I/O, the allocated MCP27017 I/O pins, and interrupt handling. It is much easier to have it this way and control it all in a "for-loop". The following image shows the setup() function and is very straightforward.

Figure 22-28: IKEA Trådfri controller setup(). The only thing to mention is that the IKEA Trådfri controller uses interrupts to detect the output of the ZigBee modules. Compared to the Energy sensor described in Chapter 16., which only detects interrupts going from "high to low", this construction needs to detect interrupts going from "low to high" and "high to low". The interrupt routines are initialized using the following code: // Interrupt 1 pinMode(INPUT_1, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(INPUT_1), handleInterrupt_1, CHANGE );

Figure 22-29: IKEA Trådfri controller interrupt initialization. The following is done: • The interrupt pin "INPUT_1" is configured as input. • The interrupt function "handleInterrupt_1" is assigned to "INPUT_1"

● 337

Home Appliance Hack and IoT Guidebook - UK.indd 337

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

• The interrupt mode is set to "CHANGE" to detect the I/O pin goes from "high to low" and from "low to high". This is done for each of the four active interrupts (limited by the #define NUMBER_OF_MODULES). More about interrupts can be found on the Arduino homepage. The interrupt I/Os that are not used to detect ZigBee input (controlled by the #define NUMBER_OF_MODULES) will be set as output and set LOW) Each of the interrupt routines ignores additional interrupts for a period of 600 ms after the first has been detected. This is due to the PWM signal shown in Figure 22-3. When a new level change has been fully detected, the final high or low level is remembered.

22.5.1.4. Controller loop() The following image shows the loop() function and is very straightforward.

Figure 22-30: IKEA Trådfri controller loop(). Based on what has been detected from the ZigBee modules, the appropriate external connections are activated. Not busy with this task, the loop() checks for incoming requests from the Internet.

22.5.2. web interface At the time of writing, the web interface of the IKEA Trådfri controller can be found here: http:///index.html and looks like this:

● 338

Home Appliance Hack and IoT Guidebook - UK.indd 338

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

Figure 22-31: web interface for the IKEA Trådfri controller. The web page is implemented via the void handleRoot(void) function. The firmware update is reached by pressing the "LINK" text where you will need to enter a user id and password. Since it is the basic web interface for the IKEA Trådfri controller, there may be a more informative update on the Elektor support & resource page for this book.

22.5.3. Additional libraries To easily use the MCP23017 module, additional libraries need to be installed. The following libraries should be installed: • the Adafruit MCP23017 Arduino library; • the Adafruit BusIO library. You do this by launching the "Library Manager" ("Tools -> Manage Libraries") Search for "MCP23017" as shown below. Select the latest version and press install.

Figure 22-32: MCP23017 library installation. Next, search for "Adafruit BusIO" as shown below.

● 339

Home Appliance Hack and IoT Guidebook - UK.indd 339

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-33: Adafruit BusIO library installation. When both are installed, you are ready to use the MCP23017 I/O-expander.

22.6. Operation Like any other IKEA Trådfri device, the ZigBee module needs to be added to the IKEA gateway. Before you continue, the "IKEA home smart" app needs to be installed on your mobile phone. This is done like any other App on your mobile phone. Look for this icon in your App Store or Google Play:

Figure 22-34: IKEA home smart. Once installed, follow the installation and configuration of the IKEA Trådfri gateway. It is now time to add the ZigBee modules.

22.6.1. Configuration of the controller (adding the ZigBee modules) If you haven't tried to add new IKEA Trådfri bulbs before, I recommend experimenting with one of the bulbs that later on will be used for the donation of the ZigBee module. There are two ways of adding a new device: • Without the IKEA remote control; • with the IKEA remote control. Both methods will be described in the coming sections.

● 340

Home Appliance Hack and IoT Guidebook - UK.indd 340

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

22.6.1.1. Adding ZigBee module with remote Before you continue, the IKEA remote control to be used to add the ZigBee module should be added to the IKEA gateway. At the time of writing, this can be done as follows:

Figure 22-35: IKEA Home Smart App "My Home". Press the gear icon in the top right-hand corner and select "Add new device".

Figure 22-36: IKEA Home Smart App "Settings". In the "Add devices" menu, select "Remote & Sensor".

● 341

Home Appliance Hack and IoT Guidebook - UK.indd 341

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-37: IKEA Home Smart App "Add devices". In the "Remote & sensor", select the remote control you want to use. In my case, it is the "On-Off switch/Dimmer".

Figure 22-38: IKEA Home Smart App "Remote & sensor".

● 342

Home Appliance Hack and IoT Guidebook - UK.indd 342

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

Follow the instructions in the IKEA Home Smart App and the IKEA remote control of your choice should hopefully be added. You are now ready to add ZigBee modules like any other IKEA Trådfri bulb with the help of the recently added IKEA remote control. Note Before you continue, only the ZigBee module that is going to be added should be connected to 3.3 VDC, so remove appropriate jumpers J4 – J7. If this is not the case the device-adding process will not succeed. Go to the menu shown in Figure 22-36 and select "Add new device". This time select "Light".

Figure 22-39: IKEA Home Smart App "Add devices". In the "Add devices – Light" menu select "With remote".

Figure 22-40: IKEA Home Smart App "Light". Select the IKEA remote control that has just been added – in my case, the "IKEA hack".

● 343

Home Appliance Hack and IoT Guidebook - UK.indd 343

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 22-41: IKEA Home Smart App "Light – IKEA hack". Follow the instructions and notice what happens to the LED connected to the ZigBee module (LED D1-D4). After the correct adding process, you should be able to turn the ZigBee module On and Off by means of the IKEA remote control and the "Home Smart App". Continue this process until all ZigBee devices have been added for use with the IKEA Trådfri control. Once finished, check that the IKEA Home Smart App sets the output of the Zigbee module to fully On and Off. If dimmed, the Wemos interrupt software will be "confused". This is done via the slider in the IKEA Home Smart App.

Figure 22-42: IKEA bulb hack slider control. The above figure shows the correct configuration of the sliders for the added ZigBee modules.

● 344

Home Appliance Hack and IoT Guidebook - UK.indd 344

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

If the Wemos firmware was loaded in the IKEA Trådfri controller and connected to the serial terminal while the Zigbee modules were added, something like this can be seen. Interrupt 0 has occurred. Total...............: 0 Remaining interrupts: 18 Input level.........: 0 Interrupt 1 has occurred. Total...............: 0 Remaining interrupts: 1 Input level.........: 0

Figure 22-43: Output during ZigBee adding. The output indicates that what's happening at Interrupt 1 is correct while what's happening at Interrupt 0 is not. This is most likely because the output of the ZigBee module is dimmed. See Figure 22-4 and Figure 22-5 for an explanation.

22.6.1.2. Adding ZigBee module without remote If you don't need an IKEA remote control for your IKEA Trådfri installation, you can add ZigBee devices to the IKEA gateway "Without remote". Before this can be done, a small switch has to be crafted as it is too difficult to turn the ZigBee module on and off using the power jumper alone.

Figure 22-44: On-Off switch. This switch will replace the power jumper (J4 – J7) as you need to turn the ZigBee module on and off during the adding process. This can be rather tricky and I recommend trying the process on a fully functional bulb. Note Before you continue, only the ZigBee module that is going to be added should be connected to 3.3 VDC, so remove appropriate jumpers J4 – J7. If this is not done, the adding process will not succeed.

● 345

Home Appliance Hack and IoT Guidebook - UK.indd 345

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Go to Figure 22-40 and select "Without remote" and follow the instructions. During the actual pairing process shown below, you must follow the timing in turning the ZigBee module on and off very closely.

Figure 22-45: IKEA Home Smart App "Pairing" process. You may need to try the process a couple of times before the ZigBee modules are added correctly. After a correct device-adding process, you should be able to turn the ZigBee module on and off via the "Home Smart App". Continue this process until all ZigBee modules have been added to the IKEA Trådfri controller. Once finished, check that the "IKEA Home Smart App" fully turns the output of the Zigbee module on and off. If dimmed, the Wemos interrupt software may become "confused". This is done via the slider in the "IKEA Home Smart App" as shown in Figure 22-42. If the Wemos firmware is loaded in the IKEA Trådfri controller and connected to the serial terminal whilst the Zigbee modules are added, something like as shown in Figure 22-43 can be seen.

● 346

Home Appliance Hack and IoT Guidebook - UK.indd 346

13-12-2022 11:08

Chapter 22 • IKEA Trådfri Controller

22.6.2. Daily use of the IKEA Trådfri controller After having added two ZigBee devices, the control should look like the image below.

Figure 22-46: IKEA Home Smart app with added ZigBee devices. The added devices are now ready to be part of any configuration within the IKEA Home Smart app like any other device in the IKEA Trådfri family. Note The ZigBee modules used in this construction will turn on when the IKEA Trådfri controller is powered up like an ordinary IKEA Trådfri bulb. The devices you have connected to the IKEA Trådfri controller will be able to manage this situation. Once powered on, the IKEA Home Smart App will control what happens with the ZigBee modules.

● 347

Home Appliance Hack and IoT Guidebook - UK.indd 347

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 23 • Troubleshooting This is the section where I will try to describe my troubleshooting findings while working with the ESP8266 and Wemos/LOLIN boards. Even if you are not experiencing any problems, the following FAQ from espressif should be downloaded: https://www.espressif.com/sites/default/files/documentation/Espressif_FAQ_EN.pdf It includes a lot of information that may be of use. One of the overall findings is that things change rapidly in this line of business. One of the many ways of starting a question on the Internet is often: "I was using xx a little while ago and now that I want to continue it gives me the error: yy" I have seen it while updating the Arduino IDE, ESP8266 libraries, or related tools and updating the Raspbian OS for the Raspberry Pi. Please keep track of your software updates and read the release notes which often include valuable information.

23.1. Hardware Issues 23.1.1. DS18B20 When you look at the DS18B20, it's very easy to confuse GND and 3.3V.

Figure 23-1: DS18B20 TO-92 housing.

● 348

Home Appliance Hack and IoT Guidebook - UK.indd 348

13-12-2022 11:08

Chapter 23 • Troubleshooting

If you do, the DS18B20 will most likely survive but it will get very hot. This will also affect the measured temperature until the device has cooled.

23.1.2. Red / Green LED Throughout the builds and projects, I am using many Red/Green bi-color LEDs. If you, like me, mount them so they light up red when expecting green, you can fix it in the software. Just locate the I/O-pin that controls the LED in question and swap the names. In the below, #defines red is linked to D1 and green is linked to D2. #define LED_RED D1 #define LED_GREEN D2 In the case of a swap, change the #defines to: #define LED_RED D2 #define LED_GREEN D1 You may also come across what you think is a bi-color LED but is just a normal single-color LED!

23.1.3. Compressor When I performed the initial test on my modified refrigerator, I found out that there is a built-in delay for the compressor on-off time. I did not investigate why, but simply noted the fact and built a check in the software. You should be aware of any similar behavior in your compressor.

23.1.4. USB cables On a few occasions, I have come across USB cables that do not bring sufficient current to the Wemos device. The result was that the device constantly reset for no obvious reason. What saved me was a small USB tester as shown below:

Figure 23-2: UM25C USB tester with color LCD and Bluetooth.

● 349

Home Appliance Hack and IoT Guidebook - UK.indd 349

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

A review of the device can be read here: https://www.elektormagazine.com/news/review-the-um25c-usb-tester-with-color-lcdand-bluetooth/18299 Just for the record, I don't benefit in any way from mentioning the device here. From time to time, I do some training where I need to purchase many micro USB B cables. Recently I came across a "new" version. The difference can be seen below:

Figure 23-3: Two types of micro USB B connectors. It is the "old" type to the right. The "new" version can be connected upside down so to speak but does not connect as well as the "old" type. This is just a warning if you experience "loose connections".

23.2. Network issues The document from espressif mentioned at the beginning of this section includes a section about WiFi which covers the most common problems. Additional information about WiFi channel selection can be found here: https://espressif.com/sites/default/files/documentation/esp8266_wi-fi_channel_ selection_guidelines_en.pdf If you still experience network issues, try to follow the ideas behind the two functions:

● 350

Home Appliance Hack and IoT Guidebook - UK.indd 350

13-12-2022 11:08

Chapter 23 • Troubleshooting

bool doWifiConnect() void showWiFiStatus(int i)

23.3. Wemos firmware 23.3.1. Reset If you see the power LED blink or the device constantly resets, attach it to the Arduino IDE and see if any error messages are displayed on the terminal interface. Using the watchdog software implemented in the constructions, you may experience the watchdog continually resetting the device. In most cases, you should get sufficient information to locate the problem using the DEBUG messages in the software. You can find additional information as follows: During the start of Setup(), a debug line has been commented out: // Init serial communication -------------------Serial.begin(9600); // If additional debug information is // needed remove the "//" //Serial.setDebugOutput(true); delay(10); // only need to be set once Serial.setTimeout(timeout); // Init serial communication END ---------------

Figure 23-4: Additional debug information setup(). Under Tools in the Arduino IDE, additional debug information needs to also be enabled:

Figure 23-5: Additional debug information. Under Tools -> Debug port: select Serial. Under Tools -> Debug Level: select the area that you want to debug.

23.4. emoncms When it comes to emoncms documentation, a reliable website to start with is: https://github.com/emoncms

● 351

Home Appliance Hack and IoT Guidebook - UK.indd 351

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Especially the subpage: https://github.com/emoncms/emoncms This should be able to guide you in the right direction. Below are the few findings that I have discovered for myself:

23.4.1. Graph installation In section 4.4.2. the graph module installation was shown. During this process I discovered the following error:

Figure 23-6: emoncms error. This was due to a cache issue in Firefox and possibly other browsers. The problem was solved by clearing the cache and updating the database. Updating the database is something that needs to be done each time a new module is installed or updated.

23.4.2. Input / Feeds During a dashboard design process, you may sometimes need to rename an input process. The easiest thing to do would be to simply delete it. Before you do this, remember to delete any feeds that use the input or you will face errors.

23.5. IKEA ZigBee modules During the walkthrough of the IKEA Trådfri controller, many "Notes" are mentioned. They are all gathered here for convenience.

23.5.1. Software upload When you upload firmware to the IKEA Trådfri controller, remember to turn off the ZigBee module "Trådfri1". If you don't, the Arduino IDE will not be able to contact the Wemos/ LOLIN device.

23.5.2. Adding ZigBee modules. Make sure that whenever a ZigBee module is going to be added to the IKEA gateway, it is the only module connected to 3.3 VDC – remove the appropriate jumper(s) J4–J7.

● 352

Home Appliance Hack and IoT Guidebook - UK.indd 352

13-12-2022 11:08

Chapter 23 • Troubleshooting

If this is not the case, the adding process will not succeed. This is not something special about this construction; it is how IKEA designed it.

23.5.3. Testing the IKEA Trådfri controller Remove the 3.3 VDC supply from the ZigBee modules which are already mounted during testing. If you don't, the ZigBee output LEDs cannot be controlled by Wemos/LOLIN.

23.5.4. Interrupts Once you are finished adding ZigBee devices, check the "IKEA Home Smart App" sets the output of the Zigbee module fully on and off. If it is even a little dimmed, the Wemos interrupt software will get "confused". This can be done via the slider in the "IKEA Home Smart App" as shown in Figure 22-42. If the Wemos firmware was loaded into the IKEA Trådfri controller and connected to the serial terminal while the Zigbee modules were added, something similar to Figure 22-43 can be seen.

● 353

Home Appliance Hack and IoT Guidebook - UK.indd 353

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 24 • PCB Layout Today it is cheap and easy to have PCBs manufactured by a professional manufacturer. If you are an experienced DIY'er you can also make them yourself. This section presents the layouts of many of my constructions. You don't need to use them to benefit from the constructions, but in many cases, it makes life easier and gives a better finish. I am using KiCad to design the PCBs but it is not a requirement to use this to create the PCBs as there will be Gerber files available for each board. If interested, KiCad can be found here: http://www.kicad-pcb.org/ All PCB designs should fit one of the housings shown in Chapter 11.

24.1. Door lock monitor PCB The door lock monitor construction is described in Chapter 15. This section describes the PCB that was made to go with the build. A set of files should be available for download if you want to have the PCB manufactured by a professional or a PCB service provider. The schematics from Figure 15-1 are repeated here for reference.

● 354

Home Appliance Hack and IoT Guidebook - UK.indd 354

13-12-2022 11:08

Chapter 24 • PCB Layout

Figure 24-1: Door lock schematic. Below is the PCB seen from the bottom side, which in KiCad terms means it is mirrored.

Figure 24-2: Door lock PCB seen from the bottom.

● 355

Home Appliance Hack and IoT Guidebook - UK.indd 355

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 24-3: Door lock components layout. The schematic shown in Figure 24-1 was designed to cover both the publishing and subscribing clients. Publishing Client

Figure 24-4: Door lock PCB populated as publishing client. In the above figure, the PCB was stuffed as a publishing client. Here only R1, D1, R5, D5, the QRE1113, and the Wemos D1 mini pro should be mounted or connected. Please note that the PCB design was made so that D1 should be mounted opposite the other components to save space.

● 356

Home Appliance Hack and IoT Guidebook - UK.indd 356

13-12-2022 11:08

Chapter 24 • PCB Layout

Subscribing Client

Figure 24-5: Door lock PCB populated as subscribing client. In the above figure, the PCB was stuffed as a subscribing client. Here all components were mounted except the QRE1113. If you wish to monitor less than four door locks, reduce the number of LEDs accordingly. Also, adapt the software to the actual number of LEDs. Once again, the LEDs from D1 to D4 were mounted opposite the other components to save space. In both cases, you may decide to mount LED D5 opposite to the components as well. Both the constructions are powered by the Wemos D1 mini pro.

24.2. Energy sensor The energy sensor construction is described in Chapter 16. This section describes the PCB that has been made to go with the build. A set of files should be available for download if you want to have the PCB manufactured by a professional or a PCB service provider. The schematic from Figure 16-5 is repeated here for reference.

● 357

Home Appliance Hack and IoT Guidebook - UK.indd 357

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Figure 24-6: Energy sensor schematics. The 3D view of the PCB given in Figure 16-5 is also shown here for reference.

Figure 24-7: Energy sensor PCB. Below is the PCB as seen from the bottom side, which in KiCad terms means it is mirrored.

● 358

Home Appliance Hack and IoT Guidebook - UK.indd 358

13-12-2022 11:08

Chapter 24 • PCB Layout

Figure 24-8: Energy Sensor PCB as seen from the bottom.

Figure 24-9: Energy sensor component layout. With reference to the schematic, LED D1, R9 and the Wemos PCB should always be mounted. Depending on how many energy sources need to be monitored, the equivalent number of components should be mounted. As indicated by the grouping of components in the schematics, it should not be difficult to figure out which components are to be mounted and which to omit. If only one energy resource needs to be monitored, only R1, R2, C1, and U2 need to be mounted. It is up to you to decide if you want to mount screw terminals or solder the wires from the energy meters directly to the PCB. If you manage to mount LED D1 so "red" becomes "green" and "green" becomes "red" you can fix it in software by swapping the below #define: #define STATUS_LED1 #define STATUS_LED2

D2 D1

● 359

Home Appliance Hack and IoT Guidebook - UK.indd 359

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The entire construction is powered by the Wemos D1 mini pro.

24.3. Refrigerator regulator The refrigerator regulator construction is described in Chapter 18. This describes PCBs that have been made to go with the construction. Out of the two PCBs available for this construction, the controller PCB is the one of interest. If you don't want to assemble the DS18B20 as described in section 18.1.2., you should mount the DS18B20 and resistor on the DS18B20 PCB. A set of files should be available for download for the regulator PCB if you want to have the PCB manufactured by a professional or a PCB service provider. For the DS18B20, there will only be some copper mask Gerber files available.

24.3.1. Regulator PCB The schematic from Chapter 18. is repeated here for reference.

Figure 24-10: Refrigerator control schematics. The 3D view of the PCB in Figure 17-6 is also shown here for reference.

● 360

Home Appliance Hack and IoT Guidebook - UK.indd 360

13-12-2022 11:08

Chapter 24 • PCB Layout

Figure 24-11: Refrigerator control PCB. Below is the PCB as seen from the bottom side, which in KiCad terms means it is mirrored.

Figure 24-12: Refrigerator controller PCB as seen from the bottom.

Figure 24-13: Refrigerator control component layout.

● 361

Home Appliance Hack and IoT Guidebook - UK.indd 361

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Since T1 is mounted below the Wemos PCB, together with R1, R2, R3, R4, and R5, a spacer of some sort is required for the Wemos PCB.

24.3.2. DS18B20 PCB The schematic from section 18.1.2. is repeated here for reference.

Figure 24-14: DS18B20 schematic. together with the 3D view of the PCB:

Figure 24-15: PCB for the DS18B20. Below is the PCB as seen from the bottom side, which in KiCad terms means it is mirrored.

Figure 24-16: DS18B20 PCB as seen from the bottom. You will most likely be able to make it smaller with the right skills, but as mentioned before, I made what is shown in Figure 17-10 instead.

Figure 24-17: DS18B20 components layout.

● 362

Home Appliance Hack and IoT Guidebook - UK.indd 362

13-12-2022 11:08

Chapter 24 • PCB Layout

The component layout is simple. Both U1 and R1 should be mounted. It is up to you to decide if the wires to the PCB should be soldered to the board or connected via a connector. It depends on your implementation in the refrigerator. There will only be Gerber files for the copper mask and no files ready for production.

24.4. Greenhouse thermostat The PCBs for the Greenhouse thermostat completely reuse the refrigerator regulator construction. The only difference is that there are two DS18B20 temperature sensors connected to the regulator PCB.

24.5. IKEA Trådfri controller The IKEA Trådfri controller construction is described in Chapter 22. This section describes the PCB that has been made to go with the construction. A set of files should be available for download if you want to have the PCB manufactured by a professional or a PCB service provider. The schematic from Figure 22-16 is repeated here for reference.

Figure 24-18: IKEA Trådfri controller schematic.

● 363

Home Appliance Hack and IoT Guidebook - UK.indd 363

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

The 3D view of the PCB is shown below.

Figure 24-19: 3D IKEA Trådfri controller PCB. Below is the PCB seen from the bottom side, which in KiCad terms means it is mirrored.

Figure 24-20:IKEA Trådfri controller PCB, mirrored.

● 364

Home Appliance Hack and IoT Guidebook - UK.indd 364

13-12-2022 11:08

Chapter 24 • PCB Layout

Figure 24-21: IKEA Trådfri controller component layout.

● 365

Home Appliance Hack and IoT Guidebook - UK.indd 365

13-12-2022 11:08

Home Appliance Hack-and-IoT Guidebook

Chapter 25 • Software Listing 25.1. HTTP POST request utility programs In this section, utility programs to send HTTP POST requests are described. They were originally designed for the IKEA hack but can be used to control the other constructions with a web interface as well. By having three different implementations of a HTTP POST request program, you can see the differences in size. They have all been verified and assessed on a Linux platform (Raspberry Pi w. Rasbian).

25.1.1. PHP This PHP script is executed from a terminal and requires that the PHP command line library is installed. This is achieved as follows from a terminal: pi@raspberrypi: $ sudo apt-get install php-common

php-cli

The PHP version can be checked with the following command: pi@raspberrypi: $ php --version

25.1.2. c-code Before you can use this utility function, a small number of GNU libraries need to be installed. This can be done from a terminal on a (Raspberry Pi) Linux platform: pi@raspberrypi: $ sudo apt-get install libcurl4-gnutls-dev The program needs to be compiled before it can be used. This is done using the following command: pi@raspberrypi: $ sudo gcc http-post_curl_1.c -lcurl -o ard /**************************************************************** * *

_ Project

_ ____

___| | | |

*

_

_ \| |

/ __| | | | |_) | |

*

| (__| |_| |

*

_