
Main theses or what this article is about
We continue the series of articles about ShIoTiny — a visually programmable controller based on a chip ESP8266.
This article discusses, using the example of a ventilation control project in a bathroom or other area with high humidity, how to build a program for ShIoTiny.
Previous articles in the series.
Links
Introduction
Understanding beyond experience does not exist. This is a truth proven by time and generations. Therefore, there is nothing better for learning practical skills than attempting to do something on your own. Examples that show what can and shouldn't be done are very timely here. While others' mistakes cannot prevent your own, they can help reduce their number.
Questions and letters from readers of previous articles prompted me to create a small project example for ventilation control to show how ShIoTiny nodes work.
The initial idea on which the controller was based ShIoTiny — a pump and irrigation station — will not suit or interest everyone. Therefore, I took a universally understandable and useful ventilation control system as an example.
I will say that the project idea is not mine, but and then adapted it to ShIoTiny.
First, understand what you want
The process of improvement is endless. This property has ruined many good ideas and projects. Instead of releasing a product that is not perfect, but functional, the developer continued to refine it. They refined it until competitors overtook them by releasing solutions that, even if not ideal (often frankly poor), were working.
Therefore, it is very important to know when to draw the line in a project. In other words, we need to determine what we want to achieve at the end of the project from what we have at the start. In the Russian language, there is a wonderful short and concise word 'plan' for a document created to describe the process of creating something, which, for some reason, mentally deficient translators and ineffective managers have recently started calling 'roadmap'. Well, let it be.
Our plan will be as follows. Suppose there is a room where humidity can sometimes rise significantly. For example, a bathroom or a kitchen. Humidity is unpleasant, and the age-old way to combat it is to ventilate the space. There are quite a few methods of ventilation. However, we will avoid exotic and old-fashioned methods like using fans made from feathers and will focus on a regular fan. Fans are cheaper, and they are also easier to find in our region.
In short, we want to control the fan: turning it on and off as needed. More precisely, we want it to turn on and off by itself when necessary.
The next step is to determine the conditions under which the fan should turn on and when it should turn off.
This is quite clear: if the humidity exceeds a certain threshold, the fan turns on and expels air; if the humidity returns to normal, the fan shuts off.
A careful reader will immediately notice the word "threshold." Threshold set by whom? How is it set?
The threshold humidity can be set in several ways. We will consider two of them: the first — using a variable resistor and the second — via the network using the MQTT protocol. Each of these methods has its advantages and disadvantages, which will be discussed later.
For those who are unclear, let me clarify that "threshold humidity" is the level of humidity that, when exceeded, requires the fan to be turned on.
The next question is whether to allow the user to turn on the fan directly? That is, regardless of the humidity level, at the push of a button? We will provide such an option. After all, the fan may be needed not only during high humidity but also for removing unpleasant odors from the room, commonly referred to as "stink."
So, we understand what we want and even a bit about how it will work. Let's briefly list all the functions of our ventilation control system:
- setting the threshold humidity level (two options);
- measuring humidity levels;
- automatic fan activation;
- automatic fan deactivation;
- manual fan activation (by pressing a button).
So, the plan is clear. We need to implement all the aforementioned functions in our program. Based on this "plan," we will proceed. First, let's draw the structural diagram of the device.
Structural Diagram of the Device
In general, we will have two such diagrams. The first one is for the option where the threshold humidity level is set by a variable resistor. The second diagram is for the option where the threshold humidity level is set via the network using the MQTT protocol.
However, since these diagrams will differ by only one element—the variable resistor "threshold humidity level setting"—we will draw only one structural diagram. Of course, the structural diagram according to GOST looks different. But we are not targeting veteran engineers, but rather the younger generation. Therefore, clarity is more important.

So, what do we see in the picture? The fan is connected to the relay Relay1 of the controller. ShIoTinyI would like to emphasize that the fan is a device that operates under high voltage. Therefore, if anyone attempts to make something like this themselves, please exercise caution. That is, at a minimum, before you stick your fingers or measuring devices into the circuit—ensure that the fan is powered off. And a second note: If your fan is more powerful than 250W, it is best not to connect it directly to ShIoTiny the controller—only through a starter.
We've clarified the fan. Now, concerning the "manual turn-on" button for the fan. It is connected to the input Input1. There’s nothing more to explain here.
Temperature and humidity sensor DHT-11 (or DHT-22 or their equivalents. A special input on the controller is provided for its connection. ShIoTinyAs seen in the diagram, connecting such a sensor is also not an issue.
And finally, the variable resistor that sets the threshold humidity level. More precisely, it's a divider consisting of a variable and a fixed resistor. There are no problems with its connection, but I should clarify that the built-in ADC on ESP8266 is rated for a maximum of 1 Volt. Hence, a voltage divider of about 5 times is required.
And once again, I remind you that this divider is unnecessary if the threshold humidity level is set via the network using the MQTT protocol.
Let's start creating the device operation algorithm in the ElDraw ShIoTiny editor. You can read how to access this editor in previous articles or in the manual, the link to which is at the beginning of the article.
The first option, the simplest one
Let's start with the basics: turning on the relay Relay1 when the moisture level exceeds a certain threshold for a specified period.

As we can see, there's nothing complicated: just four nodes, not counting the comment nodes. DHT11 — this is the actual temperature and humidity sensor (can be replaced with DHT22).
Constant CONST — the threshold moisture level, in percentage.
The comparator is the node that compares two numbers and outputs 1 if the specified condition is met and 0 if it is not.
In our case, this condition will be A>B, where A — the moisture level measured by the sensor, while B — is the threshold moisture level.
As soon as the measured moisture level (A) exceeds the threshold moisture level (B), a 1 will appear at the output of the comparator, and the relay will turn on. Conversely, as soon as the moisture level returns to normal (that is, A>B A<=B ) a 0 will appear, and the relay will turn off.), a 1 will appear at the output of the comparator, and the relay will turn on. Conversely, as soon as the moisture level returns to normal (that is, A>B Is everything clear? If not, read it again or check the node operation description in the manual.
I would like to note that the data from the sensor
is updated approximately once every 10 seconds. Therefore, the relay cannot turn on and off more frequently than once every 10 seconds. DHT11 All would be well, but we would like to set the threshold moisture level using a variable resistor. It's quite simple!
We will simply replace the constant node with an ADC node. After all, we connected the voltage divider with a variable resistor to the ADC.

The input voltage at the ADC changes from 0 to 1 Volt. However, the humidity output from the sensor ranges from 0 to 100%. So how do we compare them? It's simple. The ADC node in
not only measures the voltage at the input but also can ShIoTiny scale and shift In other words, the output value of the ADC1 node will be.
, calculated according to the formula X— input voltage of the ADC (from 0 to 1V);

, where
— the range (ADC range) and k -offset (ADC offset). Thus, if we set bk=100 b=0 and , then when the value ofchanges in the range from 0 to 1, the output value of the ADC node will change in the range from 0 to 100, numerically equal to the moisture variation range of 0 to 100%.
в диапазоне от 0 до 1, значение X на выходе узла АЦП будет изменяться в диапазоне от 0 до 100. То есть, численно равное диапазону изменения влажности от 0 до 100%.
In simple terms, by turning the variable resistance motor, you can set the threshold humidity level from 0 to 100. The only drawback is that there are no display devices. However, in practice, if you divide the variable resistance motor into 6 divisions (0%, 20%, 40%, 60%, 80%, 100%) — this is sufficient to set the threshold humidity level.
How do we set the coefficients k -offset (ADC offset). Thus, if we set b- offset (ADC offset)? It’s as easy as pie! Just click on the node ADC1 and an adjustment window will appear immediately. In this window, you can set everything you need. For our case, it will look like the one in the figure.

So, we have a basic working solution. Let's start improving it.
By the way, the simplest solution has one advantage — it doesn't need the internet. It is completely autonomous.
The second option is to connect a button.
Everything works and everyone is happy. But there's a catch, we can't turn on the ventilation manually. We agreed that a button will be connected to the input Input1 which will turn the fan on and off manually, ignoring the humidity sensor.
It's time to process this button in our scheme-program.

The button press processing block is highlighted with an orange line. It consists of a button press counter that resets to zero when the output value exceeds one (the green line, output of the node CT).
Everything here works just as straightforwardly as before: the counter CT counts the presses of the button connected to the input Input1. That is, the output value of this counter increases by 1 with each button press.
As soon as this value becomes two (that is, greater than 1), a 1 will appear at the comparator's output A>B which will reset the counter CT to zero. This refers to the comparator at the bottom of the diagram!
Thus, our button has two states — 0 and 1. If we needed more states (3, 4, or even more) — we would just need to change the constant CONST from one to another value.
So, we have two conditions for turning on the fan: exceeding the specified humidity level and a single button press. If either condition is met, the fan will turn on. It will keep running until the button is pressed again I can use or the humidity level returns to normal.
Of course, we could complicate the algorithm even more, but we won't do that — we'll leave room for creativity for those who wish.
Option three, let's connect to the internet
Everything we've described is quite viable. But what about the show-off factor? After all, any pimple-faced hipster-hacker-cracker will laugh at someone who twists knobs and presses buttons instead of controlling things from their smartphone! Twisting knobs is considered 'out of style.' But scrolling on a smartphone, even if it rubs your finger raw — that's the peak of a hipster-hacker-cracker's desires (I could never distinguish all of them — so if I got it wrong, I apologize).
But let's be lenient towards these individuals. Controlling via the internet does have real advantages. First, there's visibility. There are a multitude of applications for all platforms that allow you to create a quite usable control panel for our controller-Carlson with just a couple of taps. Second, there's the ability to remotely monitor humidity levels in a room. And third, you can see not only whether the fan is running — but also what the threshold humidity level is set to. And whether the fan was turned on automatically or manually. In general, everything you desire.
Of course, it seems like a lot of attention for a mere fan. But it's just an example.
So, to connect to the internet, we will use the technology MQTT and the corresponding protocol.
To utilize this technology, we need an MQTT broker. This is a special server that services MQTT clients, for example, ShIoTIny and your smartphone.
The essence of the technology MQTT is that any client publishes arbitrary data on the MQTT broker (server) under a specific name (called topic in the terminology MQTT). Other clients can subscribe to arbitrary data by its name (topic) and receive newly published data. So, the entire data exchange operates on the client-broker-client principle.
I I won't dwell on the details. There's a wealth of articles and tutorials on the internet about how it works and what programs exist for creating control panels. I'll just show you how to receive and publish data using MQTT As a broker, I used ShIoTiny.
, but the principle is the same everywhere. So, let's assume you've registered on the
MQTT broker MQTT-брокереIn general, the broker will issue (or require you to come up with) a username and password (for authorization), as well as a port for connection. Connect ShIoTiny to to the MQTT broker in two ways — regular connection and via TLS (SSL).
All these parameters ShIoTiny are entered on the tab Networking, section MQTT Connection to server.

If your MQTT broker does not require authorization — leave the username and password fields empty.
Parameter MQTT topic prefix requires a separate explanation.
The MQTT parameters prefix is a string added to the topic name (topic) when publishing and subscribing to the MQTT broker. To set the MQTT prefix for your controller, simply enter it in the input field “MQTT Topic Prefix» («MQTT topic prefix”). The prefix always starts with a slash («/»)! If you do not enter a slash in the input field — it will be added automatically. Special characters cannot be used in the prefix «#» and «+». There are no other restrictions.
For example, if you publish the parameter “status” (or subscribe to it), and your prefix is set as “/shiotiny/, then on the broker, this parameter will be published under the name “/shiotiny/status”. If you have set an empty prefix, then all parameters on the broker will start with a slash («/»): «status’’ will be published as “/status».
So, let’s assume you registered on MQTT-брокере and received your username, password, and port. Then you entered these parameters on the tab Networking, section MQTT Connection to server of the controller. ShIoTiny.
Let’s assume that the prefix is set to “/room/».
We will start by publishing the state of all key parameters: relay Relay1, the manual on state, the automatic on state, and finally, the threshold and current humidity levels. And as a bonus — the room temperature. To do this, see the diagram.

As you can see, the difference from the previous option — is only in the nodes “MQTT Publish”. Considering the prefix, the following parameters are published:

As we can see, the entire system state is at our fingertips!
But we want not just to see but also to manage. What to do? Very simple. We will abandon the threshold humidity level setting using ADC and a variable resistor and set this threshold humidity level directly from our smartphone! MQTT We remove the ADC node from the diagram and add three new nodes:

FLASH store FLASH restore, MQTT describe and The function of the node.
is obvious: it receives the parameter The function of the node (threshold humidity level) from /room/trigHset the MQTT broker . But what does it do with the data next? It simply forwards it to the node. FLASH restore, which in turn saves this data in non-volatile memory under the name trigH. After that, the node MQTT describe reads data from non-volatile memory under the name trigH , and what happens next we already know.
Why such complexities? Why not just pass the collected data directly to the comparator?
As my friend S. Holmes said — it's elementary! No one guarantees that after turning on your device, it will connect to to the MQTT broker. And humidity needs to be measured. And the fan needs to be turned on. But without information about the humidity threshold level, this is impossible! Therefore, our device retrieves the previously stored humidity threshold level from non-volatile memory upon startup and uses it to make decisions. Once a connection is established with the MQTT broker , and someone publishes a new value /room/trigHset, then this new value will be used.
From there, you can come up with anything. For example, in addition to humidity, you could also account for temperature. Or add 'smart' lighting control (we still have two relays and two inputs unused). Everything is in your hands!
Conclusion
So we have considered several examples of implementing a fundamentally simple controller based on ShIoTiny. It may be useful to someone.
As always, suggestions, wishes, questions, typos, and more — send to: shiotiny@yandex.ru
Source: habr.com
