ESP8266/Lua Codez

From Playful Exploration
Jump to navigation Jump to search

Setup

Custom Lua Builds

https://www.playfulexploration.com/files/nodemcu-firmware/bin/

Esp8266 Flashing Latest Dev builds

  • Flashing latest NodeMCU Dev builds with esptool requires additional '-fm dio' option. Without it the chi will not boot after flashing e.g.: esptool.py --port COM4 write_flash -fm dio 0x000000 "nodemcu_float_dev_20160603-0606.bin"
  • Latest Dev builds use baud rate 115200

Dump BME280 Pressure, Humidity and Temperature

bme280.init(1,2)
tmr.alarm(1, 3000, 1, function()
    tmr.softwd(30)
    local h,t,b=bme280.humi(),bme280.temp(),bme280.baro()
    print('humi: '..(h/1000)..'%  temp: '..(t/100)..'C  baro: '..b..'PA  dewp: '..(bme280.dewpoint(h,t)/100)..'C')
end)

Log BME280 with influx

bme280.init(1,2)
host="playfulexploration.com"
mac=wifi.ap.getmac
sendToInflux = function(c)
    local h,t,b=bme280.humi(),bme280.temp(),bme280.baro()
    local params="baro,mac="..wifi.ap.getmac().." value="..(b/1000)
        .."\ntemp,mac="..mac().." value="..(t/100)
        .."\nhumi,mac="..mac().." value="..(h/1000)
        .."\ndewp,mac="..mac().." value="..(bme280.dewpoint(h,t)/100)
    local m="POST /write?db=weather HTTP/1.1"
        .."\r\nHost:"..host
        .."\r\nContent-Length: "..params:len()
        .."\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"
        ..params
    --print(m)
    c:send(m)
end
trigger = nil
trigger = function(c)
    conn = net.createConnection(net.TCP, 0)
    conn:on("connection", sendToInflux)
    conn:on("receive", function(conn,m) conn:close() conn=nil print(m) tmr.alarm(0, 3000, 0, trigger) tmr.softwd(30) end)
    conn:connect(8086,host)
end
trigger()

Log BMP085 and DHT22 with influx

bmp085.init(6,5)
print( bmp085.pressure(), bmp085.temperature()/10, dht.read(7) )

host="playfulexploration.com"
mac=wifi.ap.getmac
sendToInflux = function(c)
    local s,t,h=dht.read(7)
    local b=bmp085.pressure()
    local params="baro,mac="..wifi.ap.getmac().." value="..b
        .."\ndewp,mac="..mac().." value="..(bme280.dewpoint(h*1000,t*100)/100)
    if s==0 then
        params=params.."\ntemp,mac="..mac().." value="..t
            .."\nhumi,mac="..mac().." value="..h
    end
    local m="POST /write?db=weather HTTP/1.1"
        .."\r\nHost:"..host
        .."\r\nContent-Length: "..params:len()
        .."\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n"
        ..params
    print(m)
    c:send(m)
end
trigger = nil
trigger = function(c)
    conn = net.createConnection(net.TCP, 0)
    conn:on("connection", sendToInflux)
    conn:on("receive", function(conn,m) conn:close() conn=nil print(m) tmr.alarm(0, 3000, 0, trigger) tmr.softwd(30) end)
    conn:connect(8086,host)
end
trigger()