Skip to content

Instantly share code, notes, and snippets.

@demogar
Last active May 2, 2022 17:34
Show Gist options
  • Save demogar/4e780e9aae9ef85f1f0e5352f17d14a8 to your computer and use it in GitHub Desktop.
Save demogar/4e780e9aae9ef85f1f0e5352f17d14a8 to your computer and use it in GitHub Desktop.
Arduino Crypto Price change with Metro M4 Airlift (WiFi)
// ...
// Display configuration
#define SRAM_CS 8
#define EPD_CS 10
#define EPD_DC 9
#define EPD_RESET -1
#define EPD_BUSY -1
#define NEOPIXELPIN 40
// 2.7 eInk Display Configuration
Adafruit_IL91874 gfx(264, 176 , EPD_DC, EPD_RESET, EPD_CS, SRAM_CS, EPD_BUSY);
Adafruit_NeoPixel neopixel = Adafruit_NeoPixel(1, NEOPIXELPIN, NEO_GRB + NEO_KHZ800);
void draw() {
setupDisplay();
gfx.setFont();
gfx.setTextColor(EPD_BLACK);
gfx.setTextSize(2);
gfx.setCursor(8, 4);
// This will create:
// CRYPTO PRICE
// ------------
gfx.print("PRICE CHANGE (24h)");
int maxLineWidth = gfx.width() - 8;
gfx.drawLine(8, 22, maxLineWidth, 22, EPD_BLACK);
// Finished drawing
gfx.display();
Serial.println("display update completed");
gfx.powerDown();
}
void setup() {
// Connect to WiFi
// ...
// Setup display
neopixel.setPixelColor(0, neopixel.Color(0, 0, 0));
neopixel.show();
gfx.begin();
Serial.println("ePaper display initialized");
gfx.clearBuffer();
gfx.setRotation(2);
// Draw
draw();
}
// Other constants
const char* restApiHost = "api.binance.com";
WiFiSSLClient client;
// Ticker Display Positions
int tickerX = 8;
int tickerY = 30;
int tickerYMargin = 20;
String getTickerApiUrl(String ticker) {
String apiUrl = "https://api.binance.com/api/v1/ticker/24hr?symbol=" + ticker;
return apiUrl;
}
void getUrlResponseForTicker(String ticker) {
String result;
client.stop();
Serial.println("Getting data for ticker:" + ticker);
if (client.connect(restApiHost, 443)) {
client.println("GET " + getTickerApiUrl(ticker) + " HTTP/1.1");
client.println("Host: api.binance.com");
client.println("Accept: application/json");
client.println("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua;)");
client.println("Connection: close");
client.println();
while (client.connected()) {
if (client.available()) {
char c = client.read();
// Serial.write(c);
result += c;
}
}
client.stop();
Serial.println("Disconnected");
// Payload
// --------
// The payload will be represented by everything contained between
// the first `{` and the last `}` received from the service.
int start = result.indexOf('{');
int end = result.lastIndexOf('}');
String body = result.substring(start, end + 1);
// After payload is received, we can transform it to a JSON doc
DynamicJsonDocument doc(4000);
deserializeJson(doc, body);
String symbol = doc["symbol"];
String priceChange = doc["priceChange"];
String priceChangePercent = doc["priceChangePercent"];
float priceChangeNumber = priceChange.toFloat();
Serial.println(symbol);
gfx.setCursor(tickerX, tickerY);
// If the value is negative, we need to set it to be red
// since the value has decreased on the last 24H
if (priceChangeNumber < 0) {
gfx.setTextColor(EPD_RED);
}
gfx.print(symbol.substring(0, 3) + ": " + priceChange.toFloat() + " (" + priceChangePercent.toFloat() + "%)");
gfx.setTextColor(EPD_BLACK);
gfx.display();
tickerY += tickerYMargin;
} else {
Serial.println(client.status());
Serial.println("Connection failed");
}
}
// ...
void draw() {
// ...
// Get data
// --------
// For getting the data, we need to compare the crypto
// symbol to a stable coin, for example USDT.
//
// Therefore, if we want to compare ETH to USDT, we need
// to pass ETHUSDT, where the first chars will represent
// the symbol to be compared, and the rest will represent
// which symbol to compare it to.
// Binance API will know how to split this eventually.
getUrlResponseForTicker("ETHUSDT");
gfx.powerDown();
}
// ...
#define WIFI_SSID "TU SSID"
#define WIFI_PASSWORD "TU PASSWORD"
// ...
bool connectWifi() {
Serial.println("Connecting to WiFi...");
WiFi.setPins(SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < "1.0.0") {
Serial.println("Please upgrade the firmware");
}
if (WiFi.begin(WIFI_SSID, WIFI_PASSWORD) == WL_CONNECT_FAILED) {
Serial.println("WiFi connection failed!");
return false;
}
int wifitimeout = 15;
int wifistatus;
while ((wifistatus = WiFi.status()) != WL_CONNECTED && wifitimeout > 0) {
delay(1000);
Serial.print(".");
wifitimeout--;
}
if (wifitimeout == 0) {
Serial.println("WiFi connection timeout with error " + String(wifistatus));
return false;
}
Serial.println("WiFi connected!!");
return true;
}
void setup() {
// Connect to WiFi
int retry = 6;
while (!connectWifi()) {
delay(5000);
retry--;
if (retry < 0) {
Serial.println("Cannot connect to WiFi, press reset to restart");
while (1);
}
}
}
@demogar
Copy link
Author

demogar commented Apr 27, 2022

price_tracker

@demogar
Copy link
Author

demogar commented May 2, 2022

20220502_123253
20220502_123303

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment