程式範例


pin = 4

apikey=xxxxxxxx

wifi.setmode(wifi.STATION);

wifi.sta.config("SSID" ,"PASSWORD");

function postDHT11()

status, temp, humi, temp_dec, humi_dec = dht.read(pin)

if status == dht.OK then

print("DHT Temperature:"..temp.."C ; ".."Humidity:"..humi.."%")

print("Sending data to thingspeak.com")

conn = nil

conn = net.createConnection(net.TCP, 0)

conn:on("receive", function(conn, payload)success = true print(payload)end)

conn:on("connection", function(conn, payload) print("Connected")

conn:send('GET /update?key='..apikey..'&field1='..temp..'&field2='..humi..'HTTP/1.1\r\n\

Host: api.thingspeak.com\r\nAccept: /\r\nUser-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n')end)

conn:on("disconnection", function(conn, payload) print('Disconnected') end)

conn:connect(80,'54.164.214.198')

end

end

tmr.alarm(1, 60000, 1, function() postDHT11() end)


自行修改apikey 跟 SSID , PASSWORD

程式60000毫秒會抓取溫濕度. 如成功會上傳網站

網站顯示畫面為