fab-lab.euSascha Wolter
Published

SmartSmell

A #SmartHome without sensors is not smart. Let's change this. Therefore, we added a sensor (for VOC) to sniff in our home!

IntermediateFull instructions provided3,214
SmartSmell

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
AirQuality Sensor (VOC)
×1

Software apps and online services

Telerik UI for Windows Universal

Story

Read more

Code

init.lua

Lua
init script will be started by autostart of nodemcu, scripted in LUA language, change the SSID and passkey according to your Wifi AP, uncomment dofile ("web.lua") if you want to autostart web.lua or the thing.lua script
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","passkey")
-- 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 ("thing.lua")
 end
end)

thing.lua

Lua
sending analog sensor data to ThingSpeak cloud for visualization, replace YOURWRITEKEY with your ThingSpeak write key
port = 80

function sendData()

t1=adc.read(0)
print("VOC:"..t1.." C\n")

-- conection to thingspeak.com
print("Sending data to thingspeak.com")
conn=net.createConnection(net.TCP, 0) 
conn:on("receive", function(conn, payload) print(payload) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149') 
conn:send("GET /update?key=YOURWRITEKEY&field1="..t1.." HTTP/1.1\r\n") 
conn:send("Host: api.thingspeak.com\r\n") 
conn:send("Accept: */*\r\n") 
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
                      print("Closing connection")
                      conn:close()
                  end)
conn:on("disconnection", function(conn)
          print("Got disconnection...")
  end)
end

-- send data every X ms to thing speak
tmr.alarm(0, 10000, 1, function() sendData() end )

Sensor value

JavaScript
Read the sensor value (VOC smell) via REST API (excerpt)
$.ajax({
    method: "GET",
    cache: false,
    url: "http://192.168.0.11/",
    dataType: "json",
    success: function (data) {
        console.log(data.analog);
    },
    error: function (xhr, textStatus, error) {
        console.log(error);
    }
});

Credits

fab-lab.eu

fab-lab.eu

22 projects • 275 followers
Maker!
Sascha Wolter

Sascha Wolter

2 projects • 4 followers
Sascha is a Developer, Innovation Manager, and Keynote Speaker with Passion for IoT, Rich Applications and Mobile Apps in all flavors.

Comments