29,99 €
There are many so-called 'Arduino compatible' platforms on the market. The ESP8266 – in the form of the WeMos D1 Mini Pro – is one that really stands out. This device includes WiFi Internet access and the option of a flash file system using up to 16 MB of external flash memory. Furthermore, there are ample in/output pins (though only one analogue input), PWM, I²C, and one-wire. Needless to say, you are easily able to construct many small IoT devices! This book contains the following builds: A colourful smart home accessory refrigerator controller 230 V power monitor door lock monitor and some further spin-off devices. All builds are documented together with relevant background information for further study. For your convenience, there is a small PCB for most of the designs; you can also use a perf board. You don't need to be an expert but the minimum recommended essentials include basic experience with a PC, software, and hardware, including the ability to surf the Internet and assemble PCBs. And of course: A handle was kept on development costs. All custom software for the IoT devices and PCB layouts are available for free download from at Elektor.com.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 238
Veröffentlichungsjahr: 2020
IoT Home Hacks
●
with ESP8266
an Elektor Publication
Hans Henrik Skovgaard
●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 ¦ [email protected]
Cover Art: Marie Skovgaard MikkelsenPrinted in the Netherlands by Wilco
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 (e.g., magazines, video, digital media, and social media) in several languages - relating to electronics design and DIY electronics. www.elektor.com
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
