I don't know yet how to clear the display, the ug8 world is kind of odd, i'm trying to learn
how to work with it.
My overall environment is a bunch of ESP modules sending mqtt messages with temperature, humidity, barometer, battery level, sometimes light level - all sent to a mosquitto broker running on a Raspberry Pi, which also runs Node Red to process the messages, save data to a mysql database, shoot data out to ThingSpeak to graph, etc.
I'm trying to use Node Red for the heavy processing to keep the ESPs from having to do too much more than collecting data and also controlling outlets & lights. Lots of fun!
Learning how to work with the ESPs, Lua, Node Red, and all is a fun challange!
I'd love to see how you get this into the flashmod setup - I've been following that thread but I just don't understand it yet.
--Init
DeviceID="ESP-12_Light_Control"
Broker="192.168.2.177"
mTopic = "home/#"
topic = mTopic
myStr0 = " mqtt Message Center"
myStr1 = " topic = "..topic
myStr2 = "Current Date/Time-UTC ="
myStr3 = "" -- place holders for the other lines
myStr4 = ""
myStr5 = ""
myStr6 = ""
myStr7 = ""
gotEm = 0
function init_i2c_display()
-- SDA and SCL can be assigned freely to available GPIOs
sda = 5 -- GPIO14
scl = 6 -- GPIO12
sla = 0x3c
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g.ssd1306_128x64_i2c(sla)
end
-- graphic test components
function prepare()
disp:setFont(u8g.font_6x10)
disp:setFontRefHeightExtendedText()
disp:setDefaultForegroundColor()
disp:setFontPosTop()
end
function draw(void)
--this gets called in an alarm loop to keep the display refreshed.
-- it prints the global strings myStr - so we just change those strings
-- to change what is displayed
prepare()
disp:drawStr(0,0,myStr0)
disp:drawStr(0,8,myStr1)
disp:drawStr(0,16,myStr2)
disp:drawStr(0,24,myStr3)
disp:drawStr(0,32,myStr4)
disp:drawStr(0,40,myStr5)
disp:drawStr(0,48,myStr6)
disp:drawStr(0,56,myStr7)
end
function loop(void)
-- this is the loop that u8g apparently needs for display
disp:firstPage()
repeat
draw()
until disp:nextPage() == false
end
function listen()
-- this is where the ESP listens for mqtt messages and deals with displaying them
m:on("message", function(conn, topic, data)
print(topic .. ":" ..data)
if (topic == "home/time") then -- my node-red server sends out the date/time every minute
myStr3 = data
elseif gotEm == 0 then -- this is for shuffling the messages on
myStr4 = topic -- the display. First message topic goes to
myStr5 = " "..data -- line 4, message to line 5
gotEm = 1
elseif gotEm == 1 then -- when the second message comes in put it below
myStr6 = topic -- the first
myStr7 = " "..data
gotEm = 2
elseif gotEm == 2 then -- after the first two when another comes in shuffle
myStr4 = myStr6 -- everything up a line
myStr5 = myStr7
myStr6 = topic
myStr7 = " "..data -- if memory was plentiful we could store more and maybe scroll
end -- thru them...
end)
end -- of listen function
function mqtt_sub() -- here's where we subscribe to the topic we want to follow
m:subscribe("home/#",0, function(conn)
print("Mqtt Subscribed home/# ")
listen() --run the subscription function
end)
end
m = mqtt.Client("ESP8266", 180, "uuuuu", "pppppp") -- stuff to deal
m:lwt("/lwt", "ESP8266", 0, 0) -- with mqtt reconnects
m:on("offline", function(con)
print ("Mqtt Reconnecting...")
tmr.alarm(1, 5000, 0, function()
print ("should connect here...")
m:connect(Broker, 1883, 0, function(conn)
print("Mqtt Connected to:" .. Broker)
mqtt_sub() --run the subscription function
end)
end)
end)
-- below is initial set up of the mqtt connection
tmr.alarm(0, 1000, 1, function()
if wifi.sta.status() == 5 and wifi.sta.getip() ~= nil then
tmr.stop(0)
m:connect(Broker, 1883, 0, function(conn)
print("Mqtt Connected to:" .. Broker)
mqtt_sub()
end)
end
end)
-- below is to set up the timer to keep hitting the display
init_i2c_display()
tmr.alarm(3,1000,1,function()
disp:firstPage()
repeat
draw()
until disp:nextPage() == false
end)