I have been keen on making a few IOT projects around the home using the fantastic ESP8266 and one of my first thoughts was to get a notification from my washing machine when the load is done. That’s when I stumbled across the fab-lab.eu project on hackster.io and using this as inspiration I have created my take on this project. To recap, we are using a LDR to read the washers ON LED, when it turns off we send a message via IFTTT.
My personal preferred version of ESP8266 is the Wemos D1 Mini, it’s small, easily programmed and powered and the optional prototype shield makes for a neat project. You can get cheaper versions of the ESP8266 but the Wemos is still pretty cheap and less mucking around.
Once you get you gear, flash the Nodemcu firmware to your ESP8266 with the NodeMCU Flasher Tool. A good tutorial on how to do this this is here. I used the default firmware that comes with the tool, as earlier experiments with some custom builds gave me nothing other than a value of 65535 on the ADC pin. This project needs to read the value of the ADC pin to figure out if our LDR can see if the LED on or off.
Once flashed you can load the Lua files below to get the program operation. I personally like using ESPlorer to do this. The only changes you will need to make to my Lua files is the SSID and password of your WiFi network in
init.lua and your IFTTT maker key in
To find your maker key go to IFTTT and find and click on
maker, then click on
settings. You need the string of characters at the end of your
maker.ifttt URL as shown below:
When it comes to setting up IFTTT, go to My
New Apptlet, search for
Maker, click on
Receive a web request and enter
WashDone in the Event Name Field. Create the trigger and then add whatever action you require like send a email.
I have tried to make the code as simple as possible, so there is no fancy OTA config or low power modes. This thing just sits there all day waiting for the washing machine LED to turn on. When the state of the LED changes from on to off the
ifttt.lua code fires to send a
WashDone event to IFTTT.
Speaking of IFTTT, I have taken the IFTTT code out of the original project file and put it in its own
lua file for simplicity. The
ifttt.lua file is borrowed from Noel Portugal’s excellent ESP8266 IFTTT Easy Button project.
Wiring up is as simple as putting the LDR from the 3V3 pin to the ADC Pin (labelled A0 on the Wemos) and a 10k resistor from the same ADC pin to ground.
print('init.lua ver 1.2') wifi.setmode(wifi.STATION) print('set mode=STATION (mode='..wifi.getmode()..')') print('MAC: ',wifi.sta.getmac()) print('chip: ',node.chipid()) print('heap: ',node.heap()) -- wifi config start wifi.sta.config("ssid","password") -- wifi config end wifi.sta.connect() tmr.alarm(1, 1000, 1, function() if wifi.sta.getip()== nil then print("IP unavaiable, Waiting...") 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()) dofile ("washer.lua") end end)
LightStatus = 0 function sendData() ldr=adc.read(0) -- reads value from Analog Input (max 1V) print (ldr) -- print value on screen if ldr > 500 then -- If sensor >500 detect status LED is on print("LED on") LightStatus = 1 print (ldr) end if ldr < 500 and LightStatus == 1 then -- If sensor >500 detect status LED changed from on to off print("LED off") LightStatus = 0 print (ldr) dofile("ifttt.lua") print ("ifttt triggered") end end tmr.alarm(0, 10000, 1, function() sendData() end )
conn = nil conn=net.createConnection(net.TCP, 0) conn:on("receive", function(conn, payload) print(payload) end) conn:on("connection", function(conn, payload) print('\nConnected') conn:send("GET /trigger/WashDone/with/key/paste-maker-string-here" .." HTTP/1.1\r\n" .."Host: maker.ifttt.com\r\n" .."Accept: */*\r\n" .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n" .."\r\n") end) conn:on("disconnection", function(conn, payload) print('\nDisconnected') end) print('Posting to ifttt.com') conn:connect(80,'maker.ifttt.com')