Created
December 11, 2017 15:38
-
-
Save gomako/bdac39e9616709aa952ac04d0d5de019 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Clear interval and timeout as they mess things up | |
clearInterval(); | |
clearTimeout(); | |
clearWatch(); | |
// Setup WiFi | |
digitalWrite(B9,1); // enable on Pico Shim V2 | |
Serial2.setup(115200, { rx: A3, tx : A2 }); | |
const proxy = 'http://httptohttps.mrtimcakes.com/'; | |
const api = 'https://api.cryptonator.com/api/ticker/'; | |
const symbols = [ 'btc', 'ltc', 'eth' ]; | |
const currency = 'gbp'; | |
var prices = { | |
btc: {}, | |
ltc: {}, | |
eth: {} | |
}; | |
var h = require("http"); | |
var g; | |
// Connect to wifi | |
var wifi = require("ESP8266WiFi_0v25").connect(Serial2, function(err) { | |
if (err) throw err; | |
wifi.reset(function(err) { | |
if (err) throw err; | |
console.log("Connecting to WiFi"); | |
wifi.connect("SSID","PASSWORD", function(err) { | |
if (err) throw err; | |
// Get prices every 30 seconds | |
setInterval(getPrices, 30000); | |
}); | |
}); | |
}); | |
// Get the prices | |
function getPrices() { | |
symbols.forEach(symbol => { | |
let url = `${proxy}${api}${symbol}-${currency}`; | |
h.get(url, function(res) { | |
let pricesRaw = ""; | |
res.on('data', function(d) { pricesRaw += d; }); | |
res.on('close', () => { | |
prices[symbol] = JSON.parse(pricesRaw); | |
}); | |
}); | |
}); | |
showPrices(); | |
} | |
// display the prices | |
function showPrices() { | |
if(!prices[symbols[0]].hasOwnProperty('timestamp')) { | |
return; | |
} | |
// Get pricetime from first symbol | |
let priceTime = formatPriceTime(prices[symbols[0]].timestamp); | |
g.clear(); | |
g.drawString(`Prices at: ${priceTime}`); | |
symbols.forEach((symbol, i) => { | |
let base = prices[symbol].ticker.base; | |
let price = parseFloat(prices[symbol].ticker.price).toFixed(2); | |
g.drawString(`${base}: ${price}`, 0, (i+1)*16); | |
}); | |
g.flip(); | |
} | |
// format the price string | |
function formatPriceTime(timestamp) { | |
let dt = new Date(timestamp * 1000); | |
let minutes = dt.getMinutes().toString(); | |
let hours = dt.getHours().toString(); | |
let seconds = dt.getSeconds().toString(); | |
if(seconds.length < 2) seconds = `0${seconds}`; | |
if(minutes.length < 2) minutes = `0${minutes}`; | |
if(hours.length < 2) hours = `0${hours}`; | |
return `${hours}:${minutes}:${seconds}`; | |
} | |
// Stuff for OLED | |
I2C1.setup({scl:B6,sda:B7}); | |
require("Font6x8").add(Graphics); | |
require("Font8x16").add(Graphics); | |
// Draw the vars to the screen | |
function doDraw() { | |
g.clear(); | |
g.drawString("Hello"); | |
g.drawString("Connecting...", 0,20); | |
g.flip(); | |
} | |
// Init the OLED Not really much point in this function | |
function initOLED() { | |
g = require("SSD1306").connect(I2C1,function(){ | |
g.setFont8x16(); | |
doDraw(); | |
}, { address: 0x3C }); | |
} | |
// Start everything off | |
initOLED(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment