ESP8266

From Randomdata wiki
Jump to navigation Jump to search

LedClock

Has it's own page LedClock

Space automation open/closed

By Themba:

wifissid   = "randomdata"
wifipasswd = "<your wifi passws>"

-- thingspeak api key with write access:
apikey     = "<your apikey kere>"


function on_connect()
	print("wifi up!")
	conn=net.createConnection(net.TCP, 0) 
	conn:on("receive", function(conn, payload) print(payload) end)

	conn:on("connection",function(conn)
		print("tcp connected!")
		conn:send("GET /update?key="..apikey.."&field1=1 HTTP/1.1\r\n" 
		.."Host: api.thingspeak.com\r\n"
		.."Accept: */*\r\n"
		.."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
		.."\r\n",function()
			print("trasnfer done!")
	    	conn:close()
		end)
	end)

	conn:on("disconnection", function(conn)
	    print("connection closed")
	    node.dsleep(300000000) -- 5 minutes
	    node.restart()
	end)

	conn:connect(80,'144.212.80.11')
end


wifi.sta.autoconnect(0) 
wifi.setmode(wifi.STATION)

wifi.sta.eventMonReg(wifi.STA_GOTIP, on_connect)
wifi.sta.eventMonStart()

print("connecting...")
-- either
wifi.sta.config(wifissid,wifipasswd)
wifi.sta.connect()

Usefull wifi info: https://nodemcu.readthedocs.io/en/dev/en/modules/wifi/#wifiap-module