Skip to content

Instantly share code, notes, and snippets.

@abobija
Created September 24, 2019 12:14
Show Gist options
  • Save abobija/5e42c70bb067b61372322a6ffc282ebc to your computer and use it in GitHub Desktop.
Save abobija/5e42c70bb067b61372322a6ffc282ebc to your computer and use it in GitHub Desktop.
Code written from YouTube video https://youtu.be/00gPK3rfXZA
local username = 'ADAFRUIT_IO_USERNAME'
local aioKey = 'ADAFRUIT_IO_KEY'
local feed = 'ADAFRUIT_IO_MQTT_FEED_NAME'
local sda = 26
local scl = 27
local sla = 0x3c
i2c.setup(i2c.HW0, sda, scl, i2c.FAST)
local disp = u8g2.ssd1306_i2c_128x64_noname(i2c.HW0, sla)
disp:setFont(u8g2.font_6x10_tf)
disp:setFontRefHeightExtendedText()
disp:setDrawColor(1)
disp:setFontPosTop()
disp:setFontDirection(0)
disp:clearBuffer()
local messages = {}
local maxMessages = 6
local function appendMessage(msg)
if #messages >= maxMessages then
for i = 1, maxMessages - 1 do
messages[i] = messages[i + 1]
end
messages[maxMessages] = msg
else
table.insert(messages, msg)
end
disp:clearBuffer()
for i, m in pairs(messages) do
disp:drawStr(0, (i - 1) * 10, m)
end
disp:sendBuffer()
end
appendMessage('~Initializing...')
local client = mqtt.Client('clientx', 120, username, aioKey)
client:on('connect', function() appendMessage('~Connected') end)
client:on('offline', function() appendMessage('~Disconnected') end)
client:on('message', function(_, topic, data)
print(topic, '>', data)
appendMessage(data)
end)
function connect()
client:connect('io.adafruit.com', 1883, 0, function()
client:subscribe(username .. '/f/' .. feed, 0, function()
appendMessage('~Subscribed')
end)
end,
function(_, reason)
print('mqtt error', reason)
appendMessage('~Error')
end)
end
wifi.mode(wifi.STATION)
wifi.sta.config({
ssid = 'Renault 1.9D',
pwd = 'renault19',
auto = false
})
wifi.sta.on('got_ip', connect)
wifi.start()
wifi.sta.connect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment