IoT Home Hacks
●
with ESP8266
an Elektor Publication
● This is an Elektor Publication. Elektor is the media brand of Elektor International Media B.V.78 York StreetLondon W1H 1DP, UKPhone: (+44) (0)20 7692 8344© Elektor International Media BV 2020First published in the United Kingdom 2020
● 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 Licensing Agency Ltd, 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder’s written permission to reproduce any part of this publication should be addressed to the publishers. The publishers 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.
● British Library Cataloguing in Publication DataCatalogue record for this book is available from the British Library
● ISBN 978-1-907920-83-7
● EISBN 978-3-89576-386-1
● EPUB 978-3-89576-387-8
Prepress production: DMC ¦ dave@daverid.com
Cover Art: Marie Skovgaard MikkelsenPrinted in the Netherlands by Wilco
Table of Contents
Chapter 1 ● Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.1 ● Why this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.2 ● Who the book is designed for. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3 ● How to use the book. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.4 ● Organisation of the book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.1 ● Section 2: Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.2 ● Section 3: Arduino IDE Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.3 ● Section 4: emoncms Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.4 ● Section 5: MQTT (mosquitto) Installation On A Raspberry Pi. . . . . . . . . . . 11
1.4.5 ● Section 6: ESP8266 Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.4.6 ● Section 7: ESP8266 Watchdogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.7 ● Section 8: WiFi Antennas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.8 ● Section 9: General Software Design. . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.9 ● Section 10: IKEA Hack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.10 ● Section 11: Door Lock Monitor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.11 ● Section 12: Energy Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.12 ● Section 13: Refrigerator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.4.13 ● Section 14: Lamp Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.14 ● Section 15: Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.4.15 ● Section 16: PCB Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.5 ● Abbreviations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.6 ● References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.7 ● Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Chapter 2 ● Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.1 ● ESP8266 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.1.1 ● Wemos D1 mini Pro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.1.2 ● Sleep Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2 ● Raspberry Pi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.2.1 ● Raspberry Pi Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.3 ● Neopixel Jewel 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.4 ● IKEA Grönö lamp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.5 ● SSR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.6 ● Maxim DS18B20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.7 ● QRE1113 (Analog). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.8 ● Wemos Shields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Chapter 3 ● Arduino IDE Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.1 ● Standard Arduino IDE Install. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2 ● ESP8266 support. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.3 ● SPI Flash File (SPIFFS) System Install. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Chapter 4 ● emoncms Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.1 ● Update and Upgrade the Raspberry Pi. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
4.2 ● Install Dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.2.1 ● MariaDB & apache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4.2.2 ● phpMyAdmin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.2.3 ● PECL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.3 ● emoncms Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4.3.1 ● Configure emoncms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
4.4 ● emoncms Modules. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.4.1 ● Dashboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.4.2 ● Graph. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.4.3 ● Apps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
4.5 ● Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Chapter 5 ● MQTT (Mosquitto) Installation on a Raspberry Pi . . . . . . . . . . . . . . . . . 63
5.1 ● Installation of the Mosquitto Broker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5.2 ● Installation of the Mosquitto Client (Raspberry Pi). . . . . . . . . . . . . . . . . . . . . 69
5.3 ● Verification of Mosquitto Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
5.4 ● Installation of the Mosquitto Client (Windows PC) . . . . . . . . . . . . . . . . . . . . . 72
Chapter 6 ● ESP8266 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Chapter 7 ● ESP8266 Watchdogs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.1 ● Watchdogs in General. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
7.2 ● Loop Watchdog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
7.2.1 ● Boot Reason . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Chapter 8 ● ESP8266 WiFi Antenna . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
8.1 ● External Antennas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
8.1.1 ● Readymade Antennas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
8.1.2 ● Salvaged Antennas. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
8.1.3 ● DIY Antenna . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Chapter 9 ● General Software Design. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Chapter 10 ● IKEA Hack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
10.1 ● Schematic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
10.2 ● Mechanical Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
10.2.1 ● Grönö Lamp. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
10.2.2 ● Wemos Housing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
10.3 ● Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
10.3.1 ● Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
10.3.2 ● Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
10.3.2.1 ● WEB interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
10.3.2.2 ● Lamp On/Off. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
10.3.2.3 ● Set Brightness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
10.3.2.4 ● Set Delay. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
10.3.2.5 ● Select Animation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
10.3.2.6 ● Enter static colours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
10.3.2.7 ● Save Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
10.3.3 ● Wemos Firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
10.3.3.1 ● setup() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
10.3.3.2 ● loop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
10.3.4 ● HTTP POST Requests. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
10.3.4.1 ● The HTTP POST Request Code in General. . . . . . . . . . . . . . . . . . 114
10.3.4.2 ● PHP-code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
10.3.4.3 ● C-Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
10.3.4.4 ● Python Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
10.3.5 ● Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
10.3.5.1 ● Rainbow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
10.3.5.2 ● Rainbow Cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
10.3.5.3 ● Static . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
10.3.5.4 ● Fade. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
10.4 ● Operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
10.4.1 ● Daily Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
10.4.1.1 ● Power Up. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
10.4.2 Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
Chapter 11 ● Door Lock Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
11.1 ● Schematic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
11.2 ● Mechanical Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
11.3 ● Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
11.3.1 ● Emoncms Wemos Firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
11.3.1.1 ● setup() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
11.3.1.2 ● loop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
11.3.2 ● Emoncms Dashboard. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
11.3.3 ● MQTT Wemos Firmware. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
11.3.3.1 ● MQTT Client Publisher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
11.3.3.2 ● MQTT Client Subscriber . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
11.3.4 ● MQTT Broker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Chapter 12 ● Energy Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
12.1 ● Schematic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
12.2 ● Mechanical Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
12.3 ● Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
12.3.1 ● Wemos Firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
12.3.1.1 ● setup() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
12.3.1.2 ● loop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
12.3.1.3 ● Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
12.4 ● Operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
12.5 ● Dashboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Chapter 13 ● Refrigerator Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
13.1 ● Schematic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
13.1.1 ● SSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
13.1.2 ● DS18B20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
13.2 ● Mechanical Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
13.3 ● Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
13.3.1 ● Wemos Firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
13.3.1.1 ● setup() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
13.3.1.2 ● loop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
13.3.2 ● Temperature Regulation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
13.3.3 ● Watchdog Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
13.3.4 ● WEB Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
13.3.4.1 ● Set Temperature and Hysteresis . . . . . . . . . . . . . . . . . . . . . . . 190
13.3.4.2 ● Open Temperature Logger . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
13.3.4.3 ● Save Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
13.3.5 ● SPI Flash File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
13.3.6 ● Compile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
13.4 ● Operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
13.5 ● Dashboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
Chapter 14 ● Lamp Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
14.1 ● Schematic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
14.1.1 ● SSR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
14.1.2 ● Relay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
14.1.3 ● Shield. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
14.2 ● Mechanical Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
14.2.1 ● SSR or Mechanical Relay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
14.2.2 ● Relay Shield. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
14.3 ● Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
14.3.1 ● Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
14.3.1.1 ● WEB interface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
14.3.2 ● Wemos firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
14.3.2.1 ● setup() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
14.3.2.2 ● loop() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
14.4 ● Operation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Chapter 15 ● Troubleshooting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
15.1 ● Hardware Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
15.1.1 ● DS18B20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
15.1.2 ● Red/Green LED. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
15.1.3 ● Compressor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
15.1.4 ● USB Cables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
15.2 ● Network issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
15.3 ● Wemos firmware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
15.3.1 ● Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
15.4 ● emoncms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
15.4.1 ● Graph Installation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
15.4.2 ● Input / Feeds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Chapter 16 ● PCB Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
16.1 ● IKEA hack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
16.2 ● Door Lock Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
16.3 ● Energy sensor. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226
16.4 ● Refrigerator Regulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
16.4.1 ● Regulator PCB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
16.4.2 ● DS18B20 PCB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
16.4.3 ● Assembly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
16.5 ● Lamp Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Chapter 17 ● Software Listings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
17.1 ● Utility Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
17.1.1 ● formatBytes(size_t bytes) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
17.1.2 ● printFloat(float value, int places). . . . . . . . . . . . . . . . . . . . . . . . . . . 235
17.1.3 ● isNumeric(String str). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
17.1.4 ● keypressWait(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
17.1.5 ● showWiFiStatus(int i) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
17.1.6 ● doWifiConnect(). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
17.2 ● HTTP POST Request Utility Programs. . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
17.2.1 ● PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
17.2.2 ● C-Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
17.2.3 ● Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
17.3 ● Loop Watchdog Timer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Index 252
IoT Home Hacks ● with ESP8266
● 9
Chapter 1 ● Introduction
Welcome to the "ESP8266 IoT"-book
Disclaimer: The constructions I describe in this book have been tested as thoroughly as possible. I have not managed to damage any of my equipment (or myself), but I am sure there's a way. In addition, the technology described hereinafter is purely for fun and should not in any circumstances be used for medical, mission-critical or safety-critical applications.
I will not accept any responsibility for damages of any kind due to actions taken by you after reading this book.
Having said that, let's continue.
1.1 ● Why this book
Like many others, when I first laid my eyes on the ESP8266 it captured my imagination immediately, like when the Raspberry Pi came along.
Before the arrival of the ESP8266, I made a lot of Arduino constructions, some with Internet connections. It was therefore obvious that I should have a go with ESP8266.
Some of the constructions and findings are described in this book. It is my hope that you enjoy working with ESP8266 as much as I have.
1.2 ● Who the book is designed for
In order to use this book, I expect some basics from you:
• You have some experience with PC software. That is, you are familiar with operat-ing at least 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 down-load the files I mention in the book. After download, 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, you will need to be able to assemble the required PCBs. 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. It shouldn't be difficult it requires that you operate tools which may cause injury.
Please be careful about what you are doing and take care!
Chapter 1 ● Introduction
● 10
1.3 ● How to use the book
Even though this book is about the ESP8266 microcontroller, you may need to install and configure an embedded Linux system (a Raspberry Pi).
Some basic skills are required to do so, but everything will be described in details, 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 in bold.
The last command is listed on two lines but you should enter it as one long command like:
“git clone https://github.com/emoncms/app.git”
Throughout the book there will be parts which will be highlighted as shown below:
This is what a warning will look like. This is where you or your surroundings or the equipment may be damaged if not handled with care and attention: Don’t worry there will not be many of these.
This is how a note looks. You should pay attention to these.
This is where I may “side-track” a little and give additional information for further reading on the subject.
Files of interest will be shown like this:
/opt/lamp/htdocs
Finally, I will show file listings in boxes like the following:
I encourage you to go through the entire book before you start your own project. It is always good to be well prepared. You should at the very least read a section in advance.
I also encourage you to make notes about what you are doing, just in case you need to do it all over again...I talk from experience.
#include <stdio.h>
main()
{
printf(“hello world\n”);
}
Note:
Warning:
Comment:
IoT Home Hacks ● with ESP8266
● 11
1.4 ● Organisation of the book
In order to avoid reading the entire book to discover what’s covered, here is a very short description of each section.
1.4.1 ● Section 2: Components
This book is mainly about the Espressif ESP8266 microcontroller. I have chosen the Wemos D1 mini pro, but you will most likely be able to use other variants out there.
Since the ESP8266 and Raspberry Pi complement each other very well, there will also be design ideas which incorporate the Raspberry Pi.
In this section, the following components are briefly described:
• Adafruit Neopixel Jewel 7.
• Raspberry Pi
• Grönö lamp
• SSR
• Maxim DS18B20
• QRE1113 (Analog)
1.4.2 ● Section 3: Arduino IDE Installation
This section will guide you through the installation of the Arduino IDE. In order to compile software for ESP8266, you will need to install additional libraries.
If you are familiar with the Arduino IDE there should be no surprises here.
1.4.3 ● Section 4: emoncms Installation
I have chosen to use emoncms for logging and visualisation.
This section describes how to install emoncms on a Raspberry Pi.
In order to make emoncms work, additional software packages will have to be installed but all will be described here.
1.4.4 ● Section 5: MQTT (mosquitto) Installation On A Raspberry Pi
The installation will cover the basic MQTT broker but also a publisher client and subscriber client.
If in favour of it, a Windows MQTT client will be installed as well.
1.4.5 ● Section 6: ESP8266 Security
A very short walkthrough of why I, so far, have not secured my ESP8266 devices.
Chapter 1 ● Introduction
● 12