More info at: https://tolgakurt.github.io/esp01-dual-relay/pcb/ https://TeguhTeknisi.github.io/esp01-dual-relay/pcb/
This project is about a dual channel relay circuit driven by an ESP8266-01 IC which connects to Azure IoT Hub. The device can be controlled by a progressive web app which communicates with serverless Azure functions in the backend which then communicates with the IoT Hub. We developed and built the circuit, and implemented the code nearly from scratch.
This is a pet project and we aimed to build a nice gadget, but we also aimed to learn new things; so we implemented features and took precautions which are hardly necessary. So expect more detail than required. You may also find some already marketed products for that purpose, but they lack this feature or that we think that we must have, and moreover, where is the fun using such a product directly?
This project began when we first encountered a specific type of curtain which is opened and closed by an AC motor. If you haven't encountered them yet, there are curtains which are like projector curtains in shape, which also include a cylindirical motor in the shaft that collects the curtain which can turn both ways depending on which input line of the motor is connected to AC phase line. The motor has 4 connections: 1 for ground, 1 for neutral, 2 for phase. One of the phase connections rotates the motor clock-wise and the other rotates anti clock-wise. So, why not develop a circuit that controls this curtain?
We implemented this project with Kutalmış İnce and mobilized the curtains in our bedrooms so that they can be dynamically programmed by a mobile app to open up at (say) 7AM (best way to wake up gracefully) and close down at (say) 6PM, or can be opened or closed at the moment. We also wanted them to be manually controllable via simple switches. This became a long journey in which we first developed a master/slave configuration where a Raspberry Pi acted as the main hub directing some Atmel based controllers. We would put Raspberry Pi in close proximity of the router so that it can be accessed online, and it will connect with the controllers using nRF24L01+ ICs. However it became a bit expensive, and we thought that we could construct the same system using ESP8266 so that we get rid of slave/master configuration. This was cheaper and working, but we were using nodeMCU devboards (which use ESP8266-12 IC with lots of GPIO pins) and this could still get cheaper by using ESP8266-01 modules. This wasn't easy as said, since ESP8266-01 is a very limited and delicate (and almost unstable) module. Rest of this document and other documents in the folders explains the whole procedure to get such a system up and running steady.
We tried to be sure about all the components we used and each line of code we wrote, but please comment if you notice something wrong or inappropriate. Needless to say, what we developed is a relay circuit controlled online, which means you can control any common AC (or DC, for that matter) utility with it.
We modeled the circuit using EAGLE. The details about the design and implementation of the circuit can be found in pcb
folder. This folder also contains gcode files so that you can etch the pcb using a CNC machine. You can export the trace masks from EAGLE as well. More information can be found here.
Embedded code is contained in esp
folder. This software connects the device to Azure IoT Hub using MQTT protocol. An overall and detailed information can be found here.
Azure Functions are implemented to handle user requests and manage the devices in the IoT Hub. All related files are in azureFunctions
folder. The details about Azure Functions, related Azure Storage, and implemented API endpoints are defined here.
A frontend for the users is implemented as a progressive web app. Frontend files are in frontend
folder. The usage details and business logic of the user interface are explained here.
-
Use a 5V power supply and design a 3V3 voltage regulator phase (using LM1117-3V3) to power up ESP-01. At the current version, the main source of the instability of the circuit is the voltage spikes generated by the relay coils when they are triggered and released. Now, a zener-rectifier combination is used to dissipate coil charge and a zener-capacitor combination is used to smooth and clamp the voltage before ESP-01. However, a regulator will do that better than a zener-capacitor couple. (And according to a rumor, 5V relays create less spikes when they are triggered and released.) However, this change also effects the driving transistors. When the GPIO pins are high, they are 3V3. But the relays should be triggered using 5V. If we use a single PNP in that condition, the transistor will always be open since (5V - 3V3 = 1V7 > 0V6). So, we need to shift 3V3 to 5V with a Sziklai pair, where the first transistor is a PNP fed with 3V3 and triggers the base of the second transistor which is an NPN which is fed with 5V and drives the relays. On the other side, this configuration will render diodes connected to the GPIO pins useless, so we can get rid of them.
-
No matter how well the code is written and how reliable the electronic components and the design, the circuit can become unresponsive after a while for this or that reason. So, an additional hardware watchdog built with 555 may restart the device in such a condition. A good design can be found here. Basically, what we need to do is to trigger 555 periodically from ESP-01 so that its countdown timer is reset. If we don't, it will trigger the
RST
pin of the ESP-01 after a while, which will cause a hard reset and the device hopefully becomes responsive again. The countdown resetting signal should be done as a result of a successfulpong
for aping
to Azure IoT Hub, so that we ensure that the device will restart if the device loses connection with Azure IoT Hub. However, there may be internet disconnections for a while, so we should let the hardware watchdog wait for a reasonable amount time before it restarts ESP-01.