ESP8266/Lua Codez

From Playful Exploration
Jump to navigation Jump to search

Setup

Firmware Mods

source new old purpose
app/modules/mqtt.c #define MQTT_BUF_SIZE 2048 #define MQTT_BUF_SIZE 1024 increase MQTT buffer size so that larger LUA scripts can be send
app/lua/luaconf.h #define LUA_MAXINPUT 2048 #define LUA_MAXINPUT 256 increase input buffer so that larger LUA scripts can be directly send to node.input(script)
app/include/user_modules.h #define ... //#define ... Enable all modules to be included
app/include/user_version.h #define BUILD_DATE "playfulexploration.com build" #define BUILD_DATE "unspecified" A string that will be shown when LUA boots up (this ideally will also show all includes modules)
app/modules/wifi.c static int wifi_station_getconfig( lua_State* L )

lua_pushstring( L, "" );

static int wifi_station_getconfig( lua_State* L )

lua_pushstring( L, sta_conf.password );

Disable the ability to read out the Wifi password

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()

Same using NodeMCU's http module:

bme280.init(1,2)
trigger = nil 
trigger = function(c)
    local h,t,b=bme280.humi(),bme280.temp(),bme280.baro()
    local params="baro,mac="..wifi.ap.getmac().." value="..(b/10)
        .."\ntemp,mac="..wifi.ap.getmac().." value="..(t/100)
        .."\nhumi,mac="..wifi.ap.getmac().." value="..(h/1000)
        .."\ndewp,mac="..wifi.ap.getmac().." value="..(bme280.dewpoint(h,t)/100)
    --print(params)
    http.post("http://www.playfulexploration.com:8086/write?db=weather", nil, params, function(code,body) print(body) tmr.alarm(0, 3000, 0, trigger) tmr.softwd(30) end)
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()

Same using NodeMCU's http module:

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

trigger = nil
trigger = function(c)
    local s,t,h=dht.read(7)
    local b=bmp085.pressure()
    local params="baro,mac="..wifi.ap.getmac().." value="..b
             .."\ndewp,mac="..wifi.ap.getmac().." value="..(bme280.dewpoint(h*1000,t*100)/100)
    if s==0 then
        params=params.."\ntemp,mac="..wifi.ap.getmac().." value="..t
                     .."\nhumi,mac="..wifi.ap.getmac().." value="..h
    end
    --print(params)
    http.post("http://www.playfulexploration.com:8086/write?db=weather", nil, params, function(code,body) print(body) tmr.alarm(0, 3000, 0, trigger) tmr.softwd(30) end)
end
trigger()