Timer - Start

It all started with him. This was my first purchase on Aliexpress for the first automation in the country - I wanted to make autowatering in the greenhouse. The timer came in a rumpled box, with a cracked protective cap, but working. He did a great job watering the cucumbers the whole summer season. For the winter he was filmed and hidden in a warm and dry place. But the next season an unpleasant surprise awaited me - the timer began to freeze, stopped responding to the control buttons and opened the water. At first I sinned on cheap batteries and replaced them with branded Duracell. I thought the problem was power and inrush currents. Did not help. Then I took out a soldering iron and soldered everything I could in it, even added some missing capacitors. But he stubbornly continued to hang. Unfortunately, the product is not very maintainable - in that I used a frameless microcircuit drop and, apparently, something was dismantled precisely under the compound with the microcircuit. Somehow the season was over with such a flimsy timer and I began to think what should I replace it with.

My first thoughts were to take an Arduino Mini module, a clock module, an indicator, and somehow shove it all into the case of a broken timer and use its native motor and ball valve. But somehow it didn’t really go there, it would consume clearly more than the original filling (which means you can forget about the batteries in the case) and it was boring and not scalable - I wanted to water not only the greenhouse, but only one broken timer.
 
And then on Ali, I came across a normal motorized ball valve. Here is such a handsome man:


 
Yes, it cost decent money, but it wasn’t scary to connect it with a summer water supply and leave it unattended for a week. It is generally quite solid, albeit with a plastic gearbox - now a couple of such taps are working on my main water supply, where the pressure is 5 atm.
 
The idea was simple. In a desk drawer, a handkerchief with an ESP8266 WiFi module was waiting in the wings. It was decided to abandon power consumption in favor of a used car battery, which, according to calculations, should have been enough for at least a month of battery life. In order not to miss the battery discharge moment, the circuit provided a divider through which the built-in ESP8266 ADC continuously measured the supply voltage . The DS3231 module with a battery should have served as a clock in the circuit, and a handkerchief per m / s MP1584EN should have lowered the voltage from 12V to 3.3V. 
 
Here is a prototype block diagram:


And here is a photo of its implementation


already a little disassembled implementation - the DC-DC module went into the crane, the clock module on the back of the breadboard

The crane motor control was done here simply - which transistors were found in the drawer of the table, they were installed. In reality, the motor can be controlled, for example, via m / s ULN2003 by connecting several channels in parallel either with field-effect transistors with logical control or in general through a relay module with opto-isolation. It is only necessary to take into account that the motor with an operating current of about 70 mA, has an inrush current (and the current at the time of rotation blocking) of about 300-350 mA.
 
A sketch would have been written quickly on Arduino with a simple Web-interface:


 
And the prototype got up on test duty. I forwarded the web interface of the crane through the router and the device was constantly available online.
 
And thoughts ran on. One prototype is good, but I want several of these cranes and doing it all on breadboard is not an option. The crane has been opened. And it became clear that there is a lot of space inside under a sealed cover and there are even fixing places:


 
In the crane used in the prototype, there was a small circuit board with a relay under the cover that provided control logic. And in the photo there is a crane with index CR05. There is no logic or circuit board in these taps. The motor control wires and the limit switch wires are simply withdrawn. And you need to control such cranes by changing the polarity of the voltage on the motor. At the same time, it is highly advisable to control the closing / opening end moments at the trailer ends, we remember that the current at these moments is already 300-350mA.
 
The block diagram began to loom like this:


 
Now we take a caliper, a sheet of paper in our hands and begin to take measurements, draw the outline of the printed circuit board and try to place the components on it. A large CR2032 battery was abandoned and a small CR1220 (or 1225) was used. For a very long time, an H-bridge chip was selected to control the motor. The choice seems to be large, but a lot of these microcircuits either can not work with a 12V supply voltage, or they use bipolar transistors with a very large voltage drop, well, or the case is unsuccessful. The TB6612 chip at first did not catch my eye for a long time, and then it seemed very redundant and inconvenient for soldering. But in the end I focused on her - she is affordable and inexpensive on Ali. The H-bridge there is built on field-effect transistors and can operate at voltages up to 15 volts. The DC-DC converter module (MP1584EN) was left by the module - it turned out to be simply cheaper in details and it was easy to solder it. The main thing, for reliability, is to replace the tuning resistor in it with a constant one (27 kOhm - it will give an output voltage of 3.4 V). The microcircuit of the clock could be used in a more compact case, but there was one caveat - I was going to order all the components for Ali and there was a risk of getting fake or broken circuits. Therefore, at the initial stage, it was planned to buy all the microcircuits as part of the finished modules and solder to the developed board. And in the modules, the clock was only in the SO16 case. In fact, only one watch microcircuit turned out to be defective or bad forgery - the frequency of quartz in it was 32727 with 32768 kHz set. replace the tuning resistor in it with a constant one (27 kOhm - it will give an output voltage of 3.4 V). The clock microcircuit could be used in a more compact case, but there was one caveat - I was going to order all the components and there was a risk of getting fake or broken circuits. Therefore, at the initial stage, it was planned to buy all the microcircuits as part of the finished modules and solder to the developed board. And in the modules, the clock was only in the SO16 case. In fact, only one watch microcircuit turned out to be defective or bad forgery - the frequency of quartz in it was 32727 with 32768 kHz set. replace the tuning resistor in it with a constant one (27 kOhm - it will give an output voltage of 3.4 V). The clock microcircuit could be used in a more compact case, but there was one caveat - I was going to order all the components and there was a risk of getting fake or broken circuits. Therefore, at the initial stage, it was planned to buy all the microcircuits as part of the finished modules and solder to the developed board. And in the modules, the clock was only in the SO16 case. In fact, only one watch microcircuit turned out to be defective or bad forgery - the frequency of quartz in it was 32727 with 32768 kHz set. but there was one caveat - I was going to order all the components for Ali and there was a risk of getting fake or broken microcircuits. Therefore, at the initial stage, it was planned to buy all the microcircuits as part of the finished modules and solder to the developed board. And in the modules, the clock was only in the SO16 case. In fact, only one watch microcircuit turned out to be defective or bad forgery - the frequency of quartz in it was 32727 with 32768 kHz set. but there was one caveat - I was going to order all the components for Ali and there was a risk of getting fake or broken microcircuits. Therefore, at the initial stage, it was planned to buy all the microcircuits as part of the finished modules and solder to the developed board. And in the modules, the clock was only in the SO16 case. In fact, only one watch microcircuit turned out to be defective or bad forgery - the frequency of quartz in it was 32727 with 32768 kHz set.
 
After all the preparatory work, we take KiKad, google a bit in search of missing seats, draw some components ourselves and begin to breed the board: We
 


check it, printing it in a 1: 1 scale. We attach parts and modules and, if everything matches, we prepare Gerber-files for production and send to EasyEda. After 3 weeks we get 10 cute handkerchiefs and collect them.


There are 5 of them left on the photo, all the others are working.


The module in the assembly. The
 
holes for the fasteners, of course, immediately didn’t match a bit, some of the seats did not turn out as they wanted, but in general the product, after assembly and firmware, worked right away. Hermetic power tails were bought on Ali, passed through a standard pressure seal in the building, and the WiFi crane timer got its factory look:



Here are a couple of cranes on combat duty in the country:

at the post

 

But then the thought ran on again. I wanted more automation in the country, and the module turned out to be quite compact and universal. Through the H-bridge you can quite easily control a conventional relay. For DIY automation, many take Sonoff, but it turns out I myself can do no worse.
 
And here begins the transformation of a simple Wi-Fi tap into what is codenamed SHAPEsp  - short for Esp 8266 based S mart H ome A utomation P latform. As planned, this should become an inexpensive universal module for home automation. Moreover, all this should be reliable and look like a finished product after assembly in an affordable case.
 
Surprisingly, it turned out that the Chinese case with Aliexpress on a 2DIN unit is fine, the body in the body, the popular AC / DC 220V / 12V HiLink converter (well, or its clone), the relay and the power connector, and of course my slightly converted module with ESP8266, clock and H-bridge.
 
Do it once
 

Do two


Do three

the module can be installed both in the crane body and in the DIN rack housing

And we get:


 
Slightly changed the shape of the module. I added edge connectors to it so that it could just stand upright in the 2DIN package. I placed indicator LEDs on the upper end of the board, which are visible in the light in a plastic case. Well, I spread the media board for the relay and power source. To make it cheaper, all this was



put into production by one board: I assembled two such prototypes and put them in the cottage to control the inclusion of convectors for winter runs:
 
In another post

One of the modules in the panel, next to a pair of contactors that include convectors

But then he decided to improve it even more and make it more universal. For a more reliable and convenient firmware, I placed the firmware scheme from NodeMcu on the module. He deduced all possible pins and added contact pads for the convenience of connecting different sensors. All edge pins are placed in 2.54 mm increments, so that the module can be inserted into the breadboard breadboard. Naturally I tested the connection of the ds1820 thermometer, the combined BME280 sensor and the humidity sensor on the comparator. It turned out that, in addition to a simple timer with a relay or a crane, you can easily build a weather station or for example an aquastop system. Well, all sorts of other notification and control systems ...
 
Carried away, I drew a model of my module for Fritzing. So you can evaluate and rotate the various application options virtually:
 

 
A link to the GitHub model is at the bottom of the article.
 
Well, then the most difficult part begins - software support for the module. Initially, I tested and tested everything with my rather simple Arduino sketch. Simple HTML, a bit of Java script, various simple ways of transferring data, only the functionality of a timer and some sensors that I need. But quite quickly it became clear that it would not be so easy to master everything and everything in the modern world of IoT and smart house building. And also just laziness (here is a smiling smiley). And sometimes I don’t want to reinvent the wheel.
 
Therefore, it was decided to see what there are of ready-made firmware in which you can add support for your module. As a starting point, alternative firmwares for Sonoff products were taken: Sonoff-Tasmota, ESPurna, ESPEasy. Search can find comparative reviews of these firmware.

For example:

https://lobradov.github.io/FOSS-Firmware-comparison-overview/

https://lobradov.github.io/FOSS-Firmware-comparison-developers/

In reality, I somehow ran through the source code in the repositories of these firmware with my eye, I realized that the easiest way for me would be to add my module to the ESPurna firmware. The firmware code was fairly reasonably structured and initially assumed the addition of new modules and functions. In the firmware was the timer functionality I needed, the standalone Sheduler. Moreover, it can be said out of the box, simply describing my configuration with a simple relay in the hardware.h file, I got a working version of the WiFi relay firmware.
 
However, my module had richer and more complex functionality. And it was decided to contribute to the firmware. On the one hand, it’s quite simple - we write functionality and do a pull-request, but in reality it is a boring and long process with not always a positive or fast result. These are the costs of the fact that the proposed functionality by and large is needed only by me and my unknown board.
 
I began with the support of the watch. In the ESPurna firmware, everything worked through NTP and the Time library for arduino - well, historically, you need to know the time, but Sonoff products do not have an autonomous clock and it is assumed that they always have Internet access. To support the RTC clock, I wrote a simple module that, if desired, replaced the function of the time provider from pure NTP to NTP + RTC. The principle was simple - if NTP synchronization is not available, then we try to read the time from the local RTC clock. When access to NTP servers appears, we restore synchronization and, if desired, synchronize the local clock. So the clock on my module got into work. The pull request was pretty quickly accepted, but it went to a separate espurna-rtc branch.
 
The next was a trivial pull request. Since I measure the supply voltage to the DC-DC converter in order to track the battery discharge, I needed to monitor not the supply voltage of the ESP8266 module, but some user-defined one. Which I designed as a pull request "add support for custom VCC monitoring". But this request somehow hung in the air ... and the desire to contribute somehow subsided.
 
In addition, it became obvious that adding your module to the list of supported finished devices will not be so easy - it has a lot of different configurations possible. Therefore, it was decided to simply develop its fork of firmware. And if possible and desire to maintain synchronization, make pull requests or offer cherry-pick in the main firmware repository.
 
After such a decision, everything became simpler. In the firmware, the relay control system was partially rewritten. The H-bridge control mode was added to it and it became more convenient in terms of adding custom relay operation modes.
 
Further study of the firmware code showed that it is not possible to refactor it. The style of writing and using resources in some places is very cruel for a microcontroller (although it is rather smart 32-bit). For example, the system for issuing debugging messages devoured the stack with great speed, but at the same time, to just not drop the entire system, it was blocked when the stack size became less than 10kB. I rewrote the code in my thread a bit so that now all debugging messages and prompts in the console are displayed completely.
 
Well, at the moment, a calculation subsystem has been added to the firmware
Sunrises / sunsets and a virtual SunriseSensor, so you can build a simple astronomical relay from the SHAPEsp module. It will be necessary to take courage and commit to the main repository. I think that this is useful functionality.
 
Here is a story of the transformation of DIY ideas from a prototype to an almost finished product. Perhaps a product in demand. The most amazing thing is that I have not tried any smart home systems yet - all the modules work for me completely autonomously from each other and are simply accessible via web-interfaces on the Internet. And once bought the OrangePi PC board lies in a drawer and is waiting in the wings to become a smart home controller.
 
List of links:

  1. Fritzing model and how I get together I think there will be a circuit and module wiring in Kikad
  2. Simple test firmware for the module
  3. ESPurna firmware fork with module support
  4. The main ESPurna firmware repository

Also popular now: