Skip to content

Instantly share code, notes, and snippets.

@ericterpstra
Last active July 28, 2016 01:09
Show Gist options
  • Save ericterpstra/9c54a4bb280e21b48aa9bb75ab1a1c71 to your computer and use it in GitHub Desktop.
Save ericterpstra/9c54a4bb280e21b48aa9bb75ab1a1c71 to your computer and use it in GitHub Desktop.
Ethereum Ticker on Wemos D1 Mini with OLED Shield
btc = 0
usd = 0
json = {}
doGetData = true
currencyLabel = "BTC"
currencyValue = btc
-- setup I2c and connect display
function init_i2c_display()
-- SDA and SCL can be assigned freely to available GPIOs
local sda = 2 -- GPIO14
local scl = 1 -- GPIO12
local sla = 0x3c -- 0x3c or 0x3d
i2c.setup(0, sda, scl, i2c.SLOW)
disp = u8g.ssd1306_64x48_i2c(sla)
prepare()
end
-- graphic test components
function prepare()
disp:setFont(u8g.font_6x10)
disp:setFontRefHeightExtendedText()
disp:setDefaultForegroundColor()
disp:setFontPosTop()
disp:setScale2x2()
end
function draw()
disp:drawStr(0, 0, currencyLabel)
disp:drawStr(0, 12, currencyValue)
end
function updateDisplay()
-- Draws one page and schedules the next page, if there is one
local function drawPages()
draw()
if (disp:nextPage() == true) then
node.task.post(drawPages)
end
end
-- Restart the draw loop and start drawing pages
disp:firstPage()
node.task.post(drawPages)
end
function getData(callback)
json = {}
http.get("http://ethereumwisdom.com/data/ethereumwisdom.json?" .. tostring(math.random(), nil, function(code, data)
if (code < 0) then
print("HTTP request failed")
else
json = cjson.decode(data);
btc = string.sub(tostring(json.currencies.BTC.markets.poloniex.price),2,6)
usd = string.sub(tostring(json.currencies.USD.markets.poloniex.price),1,5)
callback()
end
end)
end
function showNextCurrency()
if ( currencyLabel == "BTC" ) then
currencyLabel = "USD"
currencyValue = usd
else
currencyLabel = "BTC"
currencyValue = btc
end
updateDisplay()
end
function runloop()
if ( doGetData ) then
getData(showNextCurrency)
doGetData = false
else
showNextCurrency()
doGetData = true
end
end
function init()
init_i2c_display()
tmr.alarm(1,5000,tmr.ALARM_AUTO,runloop)
end
init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment