Skip to content

Instantly share code, notes, and snippets.

@xoan
Last active December 6, 2015 16:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save xoan/e99c87bf15eba2ef3207 to your computer and use it in GitHub Desktop.
Sending data from DHT22 temperature/humidity sensor and LDR luminosity sensor to ThingSpeak using Adafruit HUZZAH ESP8266 Breakout (NodeMCU 0.9.6). Wiring: http://imgur.com/eGqkAUY
my_thingspeak = {
api_key = ""; -- your ThingSpeak channel write API key
}
pin = 4 -- GPIO2
temp, humi, lumi = 0, 0, 0
url = "/update?"
maj_v, min_v, dev_v = node.info()
user_agent = "NodeMCU "..maj_v.."."..min_v.."."..dev_v.." (ESP8266 Lua)"
function send()
dht_status, temp, humi = dht.read(pin)
if dht_status == dht.OK then
print("Temperature: "..temp.." C")
print("Humidity: "..humi.." %")
url = url.."field1="..temp.."&field2="..humi.."&"
elseif dht_status == dht.ERROR_CHECKSUM then
print("DHT checksum error.")
elseif dht_status == dht.ERROR_TIMEOUT then
print("DHT time out.")
end
lumi = adc.read(0)
print("Luminosity: "..lumi)
url = url.."field3="..lumi
conn = net.createConnection(net.TCP, 0)
conn:connect(80, "184.106.153.149")
conn:on("receive", function(conn, payload)
print(payload)
end)
conn:on("connection", function(conn)
print("Sending data to ThingSpeak...")
conn:send("GET "..url.." HTTP/1.1\r\n")
conn:send("Host: api.thingspeak.com\r\n")
conn:send("X-THINGSPEAKAPIKEY: "..my_thingspeak.api_key.."\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: "..user_agent.."\r\n")
conn:send("\r\n")
end)
conn:on("sent", function(conn)
print("Done.")
conn:close()
end)
end
send()
tmr.alarm(0, 60000, 1, function()
send()
end)
my_network = {
essid = ""; -- your ESSID name
password = ""; -- your ESSID password
}
print("Setting up WiFi...")
wifi.setmode(wifi.STATION)
wifi.sta.config(my_network.essid, my_network.password)
wifi.sta.connect()
tmr.alarm(0, 1000, 1, function()
sta_status = wifi.sta.status()
if sta_status == 1 then --1: STATION_CONNECTING
print("Connecting...")
elseif sta_status == 5 then --5: STATION_GOT_IP
tmr.stop(0)
print("Done.")
dofile("dht_ldr.lua")
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment