2020-07-30 09:22:33 +02:00
|
|
|
/*
|
|
|
|
Cryptocurrency tracker example for e-radionica.com Inkplate 6
|
|
|
|
For this example you will need only USB cable and Inkplate 6.
|
|
|
|
Select "Inkplate 6(ESP32)" from Tools -> Board menu.
|
|
|
|
Don't have "Inkplate 6(ESP32)" option? Follow our tutorial and add it:
|
|
|
|
https://e-radionica.com/en/blog/add-inkplate-6-to-arduino-ide/
|
|
|
|
|
2020-07-30 13:52:43 +02:00
|
|
|
This example will show you how you can use Inkplate 6 to display API data.
|
|
|
|
Here we use Coingecko API to get latest cryptocurrency prices and display
|
|
|
|
them on the Inkplate screen. If you wish to change the currecny, you can
|
|
|
|
edit it below.
|
2020-07-30 09:22:33 +02:00
|
|
|
|
|
|
|
IMPORTANT:
|
|
|
|
Make sure to change your timezone and wifi credentials below
|
2020-07-30 13:52:43 +02:00
|
|
|
Also have ArduinoJSON installed in your Arduino libraries, download here: https://arduinojson.org/
|
2020-07-30 09:22:33 +02:00
|
|
|
|
|
|
|
Want to learn more about Inkplate? Visit www.inkplate.io
|
|
|
|
Looking to get support? Write on our forums: http://forum.e-radionica.com/en/
|
|
|
|
28 July 2020 by e-radionica.com
|
|
|
|
*/
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//---------- CHANGE HERE -------------:
|
2020-07-30 09:22:33 +02:00
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Adjust your time zone, 2 means UTC+2
|
2020-07-30 09:22:33 +02:00
|
|
|
int timeZone = 2;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Put in your ssid and password
|
2020-07-30 09:22:33 +02:00
|
|
|
char *ssid = "";
|
|
|
|
char *pass = "";
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//OPTIONAL:
|
|
|
|
//change to a different currency
|
2020-07-30 09:22:33 +02:00
|
|
|
char *currency = "bitcoin";
|
|
|
|
char *currencyAbbr = "BTC";
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//You can find your currency id here:
|
|
|
|
//https://api.coingecko.com/api/v3/coins
|
2020-07-30 13:52:43 +02:00
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//If it loads weirdly you can search the JSON using ctrl/command+f for
|
|
|
|
//your crypto by name and then find it's id next to it's name and copy those above
|
2020-07-30 09:22:33 +02:00
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//----------------------------------
|
|
|
|
|
|
|
|
//Include Inkplate library to the sketch
|
2020-07-30 09:22:33 +02:00
|
|
|
#include "Inkplate.h"
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Include fonts used
|
2020-07-30 09:22:33 +02:00
|
|
|
#include "Fonts/Roboto_Light_160.h"
|
|
|
|
#include "Fonts/Roboto_Light_40.h"
|
|
|
|
#include "Fonts/Roboto_Light_36.h"
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Our networking functions, declared in Network.cpp
|
2020-07-30 09:22:33 +02:00
|
|
|
#include "Network.h"
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//create object with all networking functions
|
2020-07-30 09:22:33 +02:00
|
|
|
Network network;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//create display object
|
2020-07-30 09:22:33 +02:00
|
|
|
Inkplate display(INKPLATE_3BIT);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Delay between API calls in miliseconds
|
2020-07-30 09:22:33 +02:00
|
|
|
#define DELAY_MS 5000
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Variable for counting partial refreshes
|
2020-07-30 09:22:33 +02:00
|
|
|
long refreshes = 0;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Constant to determine when to full update
|
2020-07-30 09:22:33 +02:00
|
|
|
const int fullRefresh = 20;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Used for storing raw price values
|
2020-07-30 13:52:43 +02:00
|
|
|
double data[64];
|
2020-07-30 09:22:33 +02:00
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Used to simplify UI design
|
2020-07-30 09:22:33 +02:00
|
|
|
struct textElement
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
const GFXfont *font;
|
|
|
|
char *text;
|
|
|
|
char align;
|
|
|
|
};
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Variables for storing all displayed data as char arrays
|
2020-07-30 09:22:33 +02:00
|
|
|
char date[64];
|
|
|
|
char fromToDate[64];
|
|
|
|
|
|
|
|
char dates[8 * 8];
|
|
|
|
char prices[16 * 16];
|
|
|
|
|
|
|
|
char current[16];
|
|
|
|
char minimum[16];
|
|
|
|
char maximum[16];
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//All months in a year, for finding current date
|
2020-07-30 09:22:33 +02:00
|
|
|
char *months[] = {
|
|
|
|
"Jan"
|
|
|
|
"Feb",
|
|
|
|
"Mar",
|
|
|
|
"Apr",
|
|
|
|
"May",
|
|
|
|
"Jun",
|
|
|
|
"Jul",
|
|
|
|
"Aug",
|
|
|
|
"Sep",
|
|
|
|
"Oct",
|
|
|
|
"Nov",
|
|
|
|
"Dec",
|
|
|
|
};
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Out UI elements data
|
2020-07-30 09:22:33 +02:00
|
|
|
textElement elements[] = {
|
|
|
|
{50, 130, &Roboto_Light_160, currencyAbbr, 0},
|
|
|
|
{390, 80, &Roboto_Light_40, date, 0},
|
|
|
|
{190, 185, &Roboto_Light_40, fromToDate, 0},
|
|
|
|
{570, 140, &Roboto_Light_40, "Current price:", 0},
|
|
|
|
{790, 190, &Roboto_Light_40, current, 1},
|
2020-07-30 13:52:43 +02:00
|
|
|
{630, 275, &Roboto_Light_40, "Minimum:", 0},
|
2020-07-30 09:22:33 +02:00
|
|
|
{790, 320, &Roboto_Light_40, minimum, 1},
|
2020-07-30 13:52:43 +02:00
|
|
|
{625, 420, &Roboto_Light_40, "Maximum:", 0},
|
2020-07-30 09:22:33 +02:00
|
|
|
{790, 466, &Roboto_Light_40, maximum, 1},
|
|
|
|
|
|
|
|
{18, 570, &Roboto_Light_36, dates, 0},
|
|
|
|
{122, 570, &Roboto_Light_36, dates + 8, 0},
|
|
|
|
{227, 570, &Roboto_Light_36, dates + 16, 0},
|
|
|
|
{342, 570, &Roboto_Light_36, dates + 24, 0},
|
|
|
|
{466, 570, &Roboto_Light_36, dates + 32, 0},
|
|
|
|
|
|
|
|
{450, 240, &Roboto_Light_36, prices, 0},
|
|
|
|
{450, 322, &Roboto_Light_36, prices + 16, 0},
|
|
|
|
{450, 401, &Roboto_Light_36, prices + 32, 0},
|
|
|
|
{450, 483, &Roboto_Light_36, prices + 48, 0},
|
|
|
|
};
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
// Our functions declared below setup and loop
|
|
|
|
void drawGraph();
|
|
|
|
void drawAll();
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
//Begin serial communitcation, sed for debugging
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
//Initial display settings
|
|
|
|
display.begin();
|
|
|
|
display.clean();
|
|
|
|
display.clearDisplay();
|
|
|
|
display.setTextWrap(false);
|
|
|
|
display.setTextColor(0, 7);
|
|
|
|
|
|
|
|
//Welcome screen
|
|
|
|
display.setCursor(70, 230);
|
|
|
|
display.setTextSize(2);
|
|
|
|
display.println(F("Welcome to Inkplate 6 cryptocurrency tracker example!"));
|
|
|
|
display.setCursor(70, 250);
|
|
|
|
display.println(F("Connecting to WiFi..."));
|
|
|
|
display.display();
|
|
|
|
|
|
|
|
delay(5000);
|
|
|
|
|
|
|
|
//Our begin function
|
|
|
|
network.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
//Do a new network request every fullRefresh times, defined above
|
|
|
|
if (refreshes % fullRefresh == 0)
|
|
|
|
while (!network.getData(data))
|
|
|
|
{
|
|
|
|
Serial.println("Retrying retriving data!");
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Our main drawing function
|
|
|
|
drawAll();
|
|
|
|
|
|
|
|
//Go to sleep before checking again
|
|
|
|
esp_sleep_enable_timer_wakeup(1000L * DELAY_MS);
|
|
|
|
(void)esp_light_sleep_start();
|
|
|
|
|
|
|
|
//Increment refresh count
|
|
|
|
++refreshes;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Function to draw our graph
|
2020-07-30 09:22:33 +02:00
|
|
|
void drawGraph()
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Edge Coordinates
|
2020-07-30 09:22:33 +02:00
|
|
|
int x1 = 10;
|
|
|
|
int y1 = 535;
|
|
|
|
|
|
|
|
int x2 = 500;
|
|
|
|
int y2 = 205;
|
|
|
|
|
|
|
|
int textMargin = 68;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Set min to a very high value, and max to very low, so that any real world data changes it
|
2020-07-30 13:52:43 +02:00
|
|
|
double minData = 1e9F;
|
|
|
|
double maxData = -1e9F;
|
2020-07-30 09:22:33 +02:00
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Find min and max in data
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int i = 0; i < 31; ++i)
|
|
|
|
{
|
|
|
|
minData = min(minData, data[i]);
|
|
|
|
maxData = max(maxData, data[i]);
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:52:43 +02:00
|
|
|
double span = max(0.3D, (double)abs(maxData - minData));
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Copy current, min and max data to char arrays to be displayed
|
2020-07-30 09:22:33 +02:00
|
|
|
dtostrf(data[30], 8, 2, current);
|
|
|
|
strcat(current, "$");
|
|
|
|
dtostrf(minData, 8, 2, minimum);
|
|
|
|
strcat(minimum, "$");
|
|
|
|
dtostrf(maxData, 8, 2, maximum);
|
|
|
|
strcat(maximum, "$");
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Temporary buffer
|
2020-07-30 09:22:33 +02:00
|
|
|
char temp[64];
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
2020-07-30 13:52:43 +02:00
|
|
|
dtostrf(minData + (double)i / 4 * span, 5, (maxData < 10.0D ? 3 : 0), temp);
|
2020-07-30 09:22:33 +02:00
|
|
|
strncpy(prices + 16 * (3 - i), temp, 16);
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Find current day in a month
|
2020-07-30 09:22:33 +02:00
|
|
|
int day;
|
|
|
|
sscanf(date + 3, "%d", &day);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Find current month
|
2020-07-30 13:52:43 +02:00
|
|
|
int month = 0;
|
|
|
|
for (int i = 0; i < 12; ++i)
|
|
|
|
{
|
|
|
|
if (strncmp(months[i], date, 3) == 0)
|
|
|
|
month = ((i + 2) % 12 ? i + 2 : 12);
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Find days to display underneath the graph
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
{
|
|
|
|
itoa(((day - i * 7) % 31 + 31) % 31, temp, 10);
|
2020-07-30 13:52:43 +02:00
|
|
|
itoa(month - ((day - i * 7) <= 0), temp + 32, 10);
|
|
|
|
|
2020-07-30 09:22:33 +02:00
|
|
|
strncpy(dates + 8 * (4 - i), temp, 8);
|
2020-07-30 13:52:43 +02:00
|
|
|
strcat(dates + 8 * (4 - i), ".");
|
|
|
|
strcat(dates + 8 * (4 - i), temp + 32);
|
|
|
|
strcat(dates + 8 * (4 - i), ".");
|
2020-07-30 09:22:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Used for drawing lines
|
2020-07-30 09:22:33 +02:00
|
|
|
int prev_x = -1;
|
|
|
|
int prev_y = -1;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Draw gradients
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int i = 0; i < 31; ++i)
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Calculate heights and current x value for a data point
|
2020-07-30 09:22:33 +02:00
|
|
|
int tx = x1 + i * (x2 - x1 - textMargin) / 31;
|
2020-07-30 13:52:43 +02:00
|
|
|
double v = data[i];
|
|
|
|
int h = (int)((double)(v - minData) * (double)abs(y1 - y2) / span);
|
2020-07-30 09:22:33 +02:00
|
|
|
int ty = y1 - h;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//If i is not 0, hence prev x and y exist so draw gradients under them
|
2020-07-30 09:22:33 +02:00
|
|
|
if (i)
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Rise over run for one pixel
|
2020-07-30 13:52:43 +02:00
|
|
|
double dy = (double)(ty - prev_y) / (double)((x2 - x1 - textMargin) / 31);
|
2020-07-30 09:22:33 +02:00
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Draw a gradient line from every pixel to bottom line in graph line
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int j = 0; j < (x2 - x1 - textMargin) / 31 + 1; ++j)
|
|
|
|
display.drawGradientLine(prev_x + j,
|
2020-07-30 13:52:43 +02:00
|
|
|
(int)round((double)prev_y + dy * (double)j),
|
2020-07-30 09:22:33 +02:00
|
|
|
prev_x + j,
|
|
|
|
y1, 3, 7);
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Set previous x and y
|
2020-07-30 09:22:33 +02:00
|
|
|
prev_x = tx;
|
|
|
|
prev_y = ty;
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//After drawing gradients, draw lines
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int i = 0; i < 31; ++i)
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Calculate heights and current x value for a data point
|
2020-07-30 09:22:33 +02:00
|
|
|
int tx = x1 + i * (x2 - x1 - textMargin) / 31;
|
2020-07-30 13:52:43 +02:00
|
|
|
double v = data[i];
|
|
|
|
int h = (int)((double)(v - minData) * (double)abs(y1 - y2) / span);
|
2020-07-30 09:22:33 +02:00
|
|
|
int ty = y1 - h;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//If i is not 0, hence prev x and y exsist so draw lines
|
2020-07-30 09:22:33 +02:00
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
display.drawThickLine(prev_x, prev_y, tx, ty, 0, 5.0);
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Set previous x and y
|
2020-07-30 09:22:33 +02:00
|
|
|
prev_x = tx;
|
|
|
|
prev_y = ty;
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Draw grid
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
display.drawFastHLine(x1, y2 + i * (y1 - y2) / 4, x2 - x1, 4);
|
|
|
|
for (int i = 0; i < 5; ++i)
|
|
|
|
display.drawFastVLine(x1 + i * (x2 - x1) / 5, y2, y1 - y2, 4);
|
|
|
|
|
|
|
|
display.drawFastVLine(x2 - textMargin + 2, y2, y1 - y2, 4);
|
|
|
|
display.drawThickLine(x1, y1, x2, y1, 0, 3);
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Our main drawing function
|
2020-07-30 09:22:33 +02:00
|
|
|
void drawAll()
|
2020-08-04 12:51:53 +02:00
|
|
|
{ //Do a full refresh every fullRefresh times, defined above
|
2020-07-30 09:22:33 +02:00
|
|
|
if (refreshes % fullRefresh == 0)
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Initial screen clear
|
2020-07-30 09:22:33 +02:00
|
|
|
display.clearDisplay();
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Save current date string, more about it in Network.cpp
|
2020-07-30 09:22:33 +02:00
|
|
|
network.getTime(date);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Find current day from string
|
2020-07-30 09:22:33 +02:00
|
|
|
int day;
|
|
|
|
sscanf(date + 3, "%d", &day);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Find what month is it numericly and display it
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int i = 0; i < 12; ++i)
|
|
|
|
{
|
|
|
|
if (strncmp(months[i], date, 3) == 0)
|
2020-07-30 13:52:43 +02:00
|
|
|
sprintf(fromToDate, "%d.%d. to %d.%d.", day, ((i + 1) % 12 ? i + 1 : 12), day, ((i + 2) % 12 ? i + 2 : 12));
|
2020-07-30 09:22:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Draw graph
|
2020-07-30 09:22:33 +02:00
|
|
|
drawGraph();
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Draw our UI elements
|
2020-07-30 09:22:33 +02:00
|
|
|
for (int i = 0; i < sizeof(elements) / sizeof(elements[0]); ++i)
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Text settings
|
2020-07-30 09:22:33 +02:00
|
|
|
display.setTextColor(0, 7);
|
|
|
|
display.setFont(elements[i].font);
|
|
|
|
display.setTextSize(1);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//0 is aligned by left bottom corner, 1 by right
|
2020-07-30 09:22:33 +02:00
|
|
|
if (elements[i].align == 0)
|
|
|
|
display.setCursor((int)(elements[i].x * 0.96), (int)(elements[i].y));
|
|
|
|
else if (elements[i].align == 1)
|
|
|
|
{
|
|
|
|
int16_t x, y;
|
|
|
|
uint16_t w, h;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Get hot much the textx offsets pointer and draw it that much more left
|
2020-07-30 09:22:33 +02:00
|
|
|
display.getTextBounds(
|
|
|
|
elements[i].text,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
&x, &y, &w, &h);
|
|
|
|
|
|
|
|
display.setCursor((int)(elements[i].x * 0.96) - w, (int)(elements[i].y));
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Print out text to above set cursor location
|
2020-07-30 09:22:33 +02:00
|
|
|
display.print(elements[i].text);
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Display all
|
2020-07-30 09:22:33 +02:00
|
|
|
display.display();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-04 12:51:53 +02:00
|
|
|
//Just draw time
|
2020-07-30 09:22:33 +02:00
|
|
|
int i = 1;
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Initial screen clear
|
2020-07-30 09:22:33 +02:00
|
|
|
display.clearDisplay();
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Save current date string, more about it in Network.cpp
|
2020-07-30 09:22:33 +02:00
|
|
|
network.getTime(date);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Text settings
|
2020-07-30 09:22:33 +02:00
|
|
|
display.setTextColor(0, 7);
|
|
|
|
display.setFont(elements[i].font);
|
|
|
|
display.setTextSize(1);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//0 is aligned by left bottom corner, 1 by right
|
2020-07-30 09:22:33 +02:00
|
|
|
if (elements[i].align == 0)
|
|
|
|
display.setCursor((int)(elements[i].x * 0.96), (int)(elements[i].y));
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Print out text to above set cursor location
|
2020-07-30 09:22:33 +02:00
|
|
|
display.print(date);
|
|
|
|
|
2020-08-04 12:51:53 +02:00
|
|
|
//Just update time
|
2020-07-30 09:22:33 +02:00
|
|
|
display.partialUpdate();
|
|
|
|
}
|
|
|
|
}
|