local flash_pin = 3 --> GPIO0 function debounce_button (func) local last = 0 local delay = 50000 -- 50ms * 1000 as tmr.now() has μs resolution return function (...) local now = tmr.now() local delta = now - last if delta < 0 then delta = delta + 2147483647 end; -- proposed because of delta rolling over, https://github.com/hackhitchin/esp8266-co-uk/issues/2 if delta < delay then return end; last = now return func(...) end end function read_button () if gpio.read(flash_pin) == 0 then print("Rebooting back to original wifi configuration mode...") file.remove('config.lc') node.restart() end end led_pin = 4 --> GPIO2 value = gpio.HIGH -- value = gpio.LOW duration = 50 --> 1 second function toggleLED () if value == gpio.LOW then value = gpio.HIGH else value = gpio.LOW end gpio.write(led_pin, value) end gpio.mode(led_pin, gpio.OUTPUT) gpio.write(led_pin, value) sda = 2 scl = 3 alt=330 -- altitude of the measurement place function read_bme280() mode = nil mode = bme280.init(sda, scl) -- print('mode=',mode,'\n') tmr.create():alarm(1000, tmr.ALARM_SINGLE, function() H, T = bme280.humi() h = H / 1000 D = bme280.dewpoint(H, T) P = bme280.baro() QNH = bme280.qfe2qnh(P, alt) curAlt = bme280.altitude(P, QNH) P, T = bme280.baro() h = H / 1000 t = T / 100 d = D / 100 p = P / 10000 q = QNH / 10000 a = curAlt tf = (t*9/5+32) df = (d*9/5+32) pi = (p*0.2953) qi = (q*0.2953) end) gpio.mode(flash_pin, gpio.INT, gpio.PULLUP) gpio.trig(flash_pin, 'both', debounce_button(read_button)) end function upload_data() read_bme280() -- print("Temperature:" .. string.format("%.3f", t) .. " °C") -- print("Humidity:" .. string.format("%.3f", h) .. " %") -- print("Pressure:" .. string.format("%.3f", q) .. " KPa") -- print("Dewpoint:" .. string.format("%.3f", d) .. " °C\n") value = gpio.LOW gpio.write(led_pin, value) if (tsapikey ~= nil) then -- push to ThingSpeak host = "api.thingspeak.com" param = '/update?key=' .. tsapikey .. '&field1=' .. string.format("%.3f", t) .. '&field2=' .. string.format("%.3f", h) .. '&field3=' .. string.format("%.3f", q) .. '&field4=' .. string.format("%.3f", d) -- print(host .. param) port = 80 -- print('Sending to ThingSpeak') send_request(host, port, param) end if (wuapiid ~= nil and wuapikey ~= nil) then -- push to WUnderground host = "rtupdate.wunderground.com" param = "/weatherstation/updateweatherstation.php?ID="..wuapiid.."&PASSWORD=".. wuapikey .. "&dateutc=now&tempf="..string.format("%.1f",tf).."&baromin="..string.format("%.3f",qi) .. "&humidity="..string.format("%.1f",h).."&dewptf="..string.format("%.1f",df) .. "&softwaretype=NodeMCU%20version%20.01&action=updateraw&realtime=1&rtfreq=2.5" -- print(host .. param) port = 80 -- print('Sending to Wunderground') send_request(host, port, param) end value = gpio.HIGH gpio.write(led_pin, value) end function send_request(host, port, param) conn = nil conn = net.createConnection(net.TCP, 0) conn:on("receive", function (conn, payload) -- print("Posted") -- print(payload) end) -- once connected, request page conn:on ("connection", function(conn, payload) -- print("Connected") if port == 80 then conn:send("GET " .. param.. " HTTP/1.1\r\nHost: " ..host.. " Connection: close\r\nAccept: */*\r\n User-Agent: Mozilla/5.1 (compatible; esp8266 Lua; Windows NT 5.1)\r\n\r\n") else conn:send(param) end end) conn:on("disconnection", function(conn, payload) -- print("Disconnected") end) conn:connect(port, host) -- release connection objects conn = nil payload = nil end local unescape = function (s) s = string.gsub(s, "+", " ") s = string.gsub(s, "%%(%x%x)", function (h) return string.char(tonumber(h, 16)) end) return s end -- First reading data read_bme280() -- Periodic reading of the sensor tmr.alarm(1, 20000, 1, function() upload_data() end) srv=net.createServer(net.TCP) srv:listen(wsport, function(conn) conn:on("receive",function(conn,payload) local _, _, method, path, vars = string.find(payload, "([A-Z]+) (.+)?(.+) HTTP") if(method == nil)then _, _, method, path = string.find(payload, "([A-Z]+) (.+) HTTP") end local _GET = {} if (vars ~= nil)then for k, v in string.gmatch(vars, "(%w+)=([^%&]+)&*") do _GET[k] = unescape(v) end end if (_GET.pin == "reboot") then print("Reboot!") node.restart() end if (_GET.pin == "reconfigure") then print("Reconfigure!") file.remove('config.lc') node.restart() end read_bme280() -- print("Connection to webserver!") --print(payload) if string.match(payload, "machine") then conn:send("\"Temperature\": \""..temp.."\", \"Humidity\": \""..humi.."\", \"Pressure\": \""..q.."\"") else buf = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n" buf = buf .. "" buf = buf .. "" buf = buf .. "DareWeather BME280-Sensor ESP8266-Based Mini Weather Station" buf = buf .. "" buf = buf .. "" buf = buf .. "" buf = buf .. "" buf = buf .. "" buf = buf .. "" buf = buf .. "" if (tschanid ~= nil and tsapikey ~= nil) then buf = buf .. "" end if (wuapiid ~= nil and wuapikey ~= nil) then buf = buf .. "" end -- buf = buf .. "" -- buf = buf .. "" buf = buf .. "
DareWeather Station
Temperature"..string.format('%.1f', t).."°C
Humidity"..string.format('%.1f', h).."%
Dewpoint"..string.format('%.1f', d).."°C
Pressure"..string.format('%.1f', q).."kPa
ThingSpeak"..tschanid.."
Wunderground"..wuapiid.."
" buf = buf .. "" conn:send(buf) conn:close() collectgarbage() end conn:on("sent",function(conn) conn:close() end) conn = nil end) end)