Johannes Smits
Published © GPL3+

Adding WiFi to my home ventilation system

In order to remotely turn my mechanical ventilation system in overdrive I added a wifi enabled switch to the system.

IntermediateShowcase (no instructions)2 hours3,078
Adding WiFi to my home ventilation system

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
esp8266, esp01
×1
AMS1117 3.3V DC-DC step down
×1
DC-DC step down 3.3V
×1
1 Channel Optocoupler Relay Module
×1

Software apps and online services

NodeMCU firmware
NodeMCU firmware

Story

Read more

Code

Ventilator.lua

Lua
To switch on or off the ventilator by a http call
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)

Init.lua

Lua
Do Some initial 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)

Credits

Johannes Smits

Johannes Smits

2 projects • 11 followers
Thanks to .

Comments