When we founded Yoctopuce, one of the aims was to provide a fast, easy, and reliable solution for any geek DIY (controlling the coffee machine from your computer, knowing when to fetch the mail, and so on). Other solutions were already available, but none was simple enough for everything to work in half a day. This week, we had to quickly install a system to control humidity in a bedroom, and we had only an afternoon to do it. Here is what we did...
The issue
We want to maintain humidity between 45% and 55% in a small bedroom which usually has a humidity rate of about 30%. We have half a day to reach a working solution with a very simple humidifier bought in a department store.
As the humidifier doesn't have any regulation system or timer, we must be able to control when it is switched on and off. Naturally, we must be able to measure the humidity in the room and display it. The humidifier must remain in working order at the end of the project, without needing a diploma to know how to switch it on.
All the necessary components
Step 1: control of the humidifier
Our humidifier is very basic: it has only one button that switches it on and off. The most obvious solution is to keep it always on and to use a Yocto-PowerRelay to shut off the power supply when we need to turn it off. Unfortunately, if we shut off the power supply of this humidifier, it automatically switches back to the OFF state and we need to press the button to switch it on again. Therefore, we must simulate pressing on the button with a Yocto-Relay and thus disassemble the humidifier to access the button. The first good news is that there is enough room to put a Yocto-Relay and to have a USB cable coming out.
The humidifier has just engough free space to hide a Yocto-Relay inside
We solder two wires on the button and connect them on the first relay of the Yocto-Relay. When we shortly activate this relay, we simulate someone pressing on the button. The issue with this solution is that we are not able to detect the humidifier state. We could keep the state in our program and update it each time we simulate pressing on the button, but how would we do if someone actually pressed the humidifier button? The lavish solution would be to add a Yocto-Volt to detect the humidifier consumption, but there isn't enough space to put a second module.
Therefore we used the fact that the humidifier is always off after the power supply has been shut off. If we connect the second relay of the Yocto-Relay serially on the power supply wire, we can shut off the power supply. To switch off the humidifier, whatever its state, we only need to shut off the power supply for about half of a second. And when we want to switch it on, we first switch it off with this method and them simulate pressing on the button right afterwards. In this way, without knowing the initial state of the humidifier, we can always reliably switch it on or off. We must simply be careful to connect the button wires to state B of the relay and power to state A of the relay, in order for the humidifier to remain in working conditions even when it is not connected to a computer or a Hub (A is the relay default state).
Wiring of the Yocto-Relay to control the humidifier
Step 2: connectivity
Now that we can control our humidifier, we must connect it to something. One solution would be to set up a mini-computer in this room and to connect it by USB, but this solution is not very practical. We are rather going to use a YoctoHub-Wireless-SR and connect to it:
- the modified humidifier
- a Yocto-Meteo to measure the humidity rate
- a Yocto-MaxiDisplay to display the humidity rate
The strong point of this solution is that it consumes much less power than a computer and that it doesn't use a lot of space. When everything is connected, we configure the YoctoHub-Wireless-SR to connect itself on the house Wifi network so that we can access our modules from anywhere.
The YoctoHub-Wireless-SR and all the connected modules
Step 3: software
Instead of implementing a piece of software which would connect itself to our modules, we are going to configure an HTTP callback on our YoctoHub-Wireless-SR. The advantage of this solution is that it is very easy to implement: we only need to write a simple PHP (or Node.js or Java) script. Sure, we need to have access to a PHP server, but any self-respecting geek has at least one PHP server hosting a blog or a forum, right?
The code is very simple:
We initialize the API in HTTPCallback mode.
Error($errmsg);
We retrieve the object of all of our Yoctopuce modules.
$display = YDisplay::FirstDisplay();
$humPower = YRelay::FindRelay("hum_power");
$humSwitch = YRelay::FindRelay("hum_switch");
We switch the humidifier on or off, depending on the room humidity.
* switch on or off the connected humidifier. We cut the power for 500ms to
* reset the humidifier to his default state (OFF). Then to switch it on
* we need to simulate the push of the power button.
* @param bool $on
*/
function setHumidifierState($on=False)
{
global $humPower, $humSwitch;
$humPower->pulse(500);
if ($on) {
$humSwitch->delayedPulse(500,100);
}
}
...
$currentHum = (int)$humSenor->get_currentValue();
if ($currentHum < MIN_HUMIDITY) {
setHumidifierState(True);
} else if ($currentHum> MAX_HUMIDITY) {
setHumidifierState(false);
}
And we display the humidity rate on the screen.
$height = $display->get_displayHeight();
$layer4 = $display->get_displayLayer(4);
$layer4->reset();
$layer4->drawImage(0, 0, "drop.gif");
$lastHumText = sprintf("%d %%", $currentHum);
$layer4->selectFont('Large.yfm');
// draw lastHumText
$layer4->selectColorPen(0xffff);
$layer4->drawText($width, $height / 2, Y_ALIGN_CENTER_RIGHT, $lastHumText);
Naturally, it's possible to have a more lavish script, storing the values in a data base, or displaying a graph on the screen on top of the humidity rate. You can download our full script on GitHub.
Conclusion
The humidifier modified by Yoctopuce
Here we are, in an afternoon we completed this project without too much hassle. Obviously, some points could be bettered (esthetics, sending an alert on a smartphone, and so on...) depending on the amount of time available. But the basic aim was fulfilled: with a few Yoctopuce modules, you can control yourself through the network almost any kind of existing appliance, even the simplest ones.