Skip to content

Instantly share code, notes, and snippets.

@jeanfbrito
Created November 30, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanfbrito/f247083e0de193837b8b to your computer and use it in GitHub Desktop.
Save jeanfbrito/f247083e0de193837b8b to your computer and use it in GitHub Desktop.
Sensor da Estufa V0.1
--- Config
SSID = "Dark side of the Force"
PASSWORD = "senhasupersecreta"
TIMEOUT = 30000000 -- 30s
--- Station modes
STAMODE = {
STATION_IDLE = 0,
STATION_CONNECTING = 1,
STATION_WRONG_PASSWORD = 2,
STATION_NO_AP_FOUND = 3,
STATION_CONNECT_FAIL = 4,
STATION_GOT_IP = 5
}
--- Connect to WIFI and then periodically send data to ThingSpeak.com
function connect(timeout)
local time = tmr.now()
wifi.sta.connect()
-- Wait for IP address; check each 1000ms; timeout
tmr.alarm(1, 1000, 1,
function()
if wifi.sta.status() == STAMODE.STATION_GOT_IP then
tmr.stop(1)
print("Station: connected! IP: " .. wifi.sta.getip())
tmr.alarm(0, 60000, 1, function() sendData() end )
else
if tmr.now() - time > timeout then
tmr.stop(1)
print("Timeout!")
if wifi.sta.status() == STAMODE.STATION_IDLE
then print("Station: idling") end
if wifi.sta.status() == STAMODE.STATION_CONNECTING
then print("Station: connecting") end
if wifi.sta.status() == STAMODE.STATION_WRONG_PASSWORD
then print("Station: wrong password") end
if wifi.sta.status() == STAMODE.STATION_NO_AP_FOUND
then print("Station: AP not found") end
if wifi.sta.status() == STAMODE.STATION_CONNECT_FAIL
then print("Station: connection failed") end
end
end
end)
end
--- Get temp
function getTemp()
local r = adc.read(0)
local c = r * 285 / 1024
return c
end
function getLux()
local l
SDA_PIN = 6 -- sda pin, GPIO12
SCL_PIN = 5 -- scl pin, GPIO14
bh1750 = require("bh1750")
bh1750.init(SDA_PIN, SCL_PIN)
bh1750.read()
l = bh1750.getlux()
print("lux: "..(l / 100).."."..(l % 100).." lx")
-- release module
bh1750 = nil
package.loaded["bh1750"]=nil
return l
end
function getTempHumi()
pin = 4
local status,temp,humi,temp_decimial,humi_decimial = dht.read(pin)
if( status == dht.OK ) then
-- Float firmware using this example
print("DHT Temperature:"..temp..";".."Humidity:"..humi)
elseif( status == dht.ERROR_CHECKSUM ) then
print( "DHT Checksum error." );
elseif( status == dht.ERROR_TIMEOUT ) then
print( "DHT Time out." );
end
return temp, humi
end
--- Get temp and send data to thingspeak.com
function sendData()
local t, h = getTempHumi()
local lux = getLux()
print("Temp:"..t .." C\n")
print("Humi:"..h .." RH\n")
print("Lux:"..lux .." lx\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=5DL2THYRD4ZTNPH2&field1="..t.."&field2="..h.."&field3="..lux.." 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.1)\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
--- Main
print("Setting up Wi-Fi connection..")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PASSWORD)
connect(TIMEOUT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment