Skip to content

Instantly share code, notes, and snippets.

@padcom
Created March 4, 2017 21:41
Show Gist options
  • Select an option

  • Save padcom/6ad44be55a6423e3c8dbff564a2c83b8 to your computer and use it in GitHub Desktop.

Select an option

Save padcom/6ad44be55a6423e3c8dbff564a2c83b8 to your computer and use it in GitHub Desktop.
Minimalist NodeMCU application that sends GEFL logs to Graylog2
mqtt_connected = false
source = 'NodeMCU' .. node.chipid()
data = {
version = '1.1',
source = source,
short_message = 'Sensor data (temperature and pressure)',
_temperature = 0,
_pressure = 0
}
-- This function is called when WiFi connection is established
function start()
-- initialize MQTT client
local m = mqtt.Client()
m:connect("<mqtt-server>", 1883, 0, 1, function()
mqtt_connected = true
print("MQTT Connected")
end)
-- initialize BMP085 sensor
bmp085.init(2, 1)
-- every second read sensor values and publish them
-- onto system/logs in the form of a GEFL-formatted message
tmr.create():alarm(1000, tmr.ALARM_AUTO, function()
data._temperature = bmp085.temperature()
data._pressure = bmp085.pressure()
if mqtt_connected then
print("Sending MQTT GELF log")
m:publish("system/logs", cjson.encode(data), 0, 0)
else
print("MQTT not connected")
end
end)
end
-- Connect to access point
wifi.setmode(wifi.STATION)
wifi.sta.config("<SSID>", "<password>")
wifi.sta.eventMonReg(wifi.STA_GOTIP, function()
print("WIFI Connected " .. wifi.sta.getip())
wifi.sta.eventMonStop()
start()
end)
wifi.sta.eventMonStart()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment