My home is equipped with an mechanical ventilation system intent to circulate fresh air using ducts and fans. not connected to heating or anything, the single purpose is to provide a constant airflow to optimize the inside air quality. A ventilator always runs in the background but can also run at a higher speed to provide some extra capacity while showering for instance.
The problem is that the control switch for the ventilation is in my kitchen, two floors down from the bathroom. Despite the fogs at long showering sessions I have never seen any family member (including myself) run down the stairs to turn up the ventilation before and after showering. The nice result is that I have to repaint to walls every year with fungus killing wallpaint to prevent growth like this.
So it would be nice to have the control also upstairs but the wiring is in de walls and the easiest solution is a remote control of sorts.
esp2866 to the rescue…or how to add WiFi to a ventilator
Although I do have a raspberry pi based solution for switching the lights with my phone, based on a 433MHz transmitter I wanted to have something more reliable and above all, a way to check the status of the switch. I read some positive blogs about some cheap tiny WiFi modules (esp8266) with GPIOs that can run scripts and thought that might do the trick. Since they are cheaper than the parking fees for parking 5 minutes in my city I ordered a few because well… always nice to have some spares in case i kill some.
ESP8266 offers a complete and self-contained Wi-Fi networking solution, allowing it to either host the application or to offload all Wi-Fi networking functions from another application processor.
After some experimentation I settled on NodeMCU as my firmware of choice. Thanks to the excellent Quickstart Guide I got my first esp up and running in no time. That settled, what do I want my esp to do?
Basically I wanted to replace this switch with something I could control with my iPhone, add it to the remote I made for my lights.
The ingredients:
- an old 5V phone charger
- DCDC stepdown 3.3V module
- FT232RL FTDI USB 3.3 V 5.5 V Seriële Adapter Module (for programming the esp01)
A bit of soldering gave me this
Just the esp01 with the 3.3V step-down and some leads to relay and power supply.
The trick is in the programming. The NodeMCU firmware launches the script init.lua at boot-time and and mine contains the basic network stuff.
--init.lua
wifi.sta.config("yourssid","yourwpakey")
wifi.sta.connect()
wifi.sta.setip({ip="192.168.178.205",netmask="255.255.255.0",gateway="192.168.178.1"})
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("Waiting for IP...")
else
tmr.stop(1)
print("ESP8266 mode is: " .. wifi.getmode())
print("The module MAC address is: " .. wifi.ap.getmac())
print("Config done, IP is "..wifi.sta.getip())
print("5 seconds to stop timer 0")
tmr.alarm(0, 5000, 0, function()
print("Starting web listener...")
dofile("ventilator.lua")
end)
end
end)
The last line calls the script that does the actual “smarts”. What does it do?
listen for a http get command: http://youripadress/?aan (“aan” is Dutch for “on”) should switch the ventilator on, http://ipadress/?uit should switch it off.
auto-shutdown: knowing my family members… they will not switch the ventilation off after showering so I decided that it should turn itself of after a while to prevent it being on for the next 24hrs
I decided not to give it a gui of its own but just let it respond to http calls. This makes it easier for me to add the device to my php-based remote I already had written to control the lights in my home.
relaisPin = 4 -- esp-01 GPIO2
gpio.mode(relaisPin,gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
local status = "";
print("Payload:"..payload)
conn:send('HTTP/1.1 200 OK\n\n')
if string.find(payload,"uit") ~= nil then
print("switch relaisPin to HIGH")
gpio.write(relaisPin, gpio.HIGH)
elseif string.find(payload,"aan") ~= nil then
print("switch relaisPin to LOW")
gpio.write(relaisPin, gpio.LOW)
-- ik wil dat de ventilatie na 15 minuten weer automatisch uitgezet wordt
tmr.alarm(1,900000,0,function()
gpio.write(relaisPin, gpio.HIGH)
print (" ...en weer uit gezet")
end)
elseif string.find(payload,"herstart") ~= nil then
node.restart()
elseif string.find(payload,"status") ~= nil then
print("relaisPin="..tostring(gpio.read(relaisPin)))
else
conn:send("Ventilatie\r\ncommando's: aan, uit, herstart, status\n\n")
end
if (gpio.read(relaisPin) == 1) then
conn:send("aan (1), uit (0) = 0" )
else
conn:send("aan (1), uit (0) = 1" )
end
conn:close();
collectgarbage();
end)
end)
Good luck!
Comments