// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_LINES 2
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET -7
#define UTC_OFFSET_DST -8
#define CMC_URL "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?slug=dero&convert=USD&CMC_PRO_API_KEY=b52902d2-277c-4e9f-8442-18a02ec33ca4"
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printPrice(char *lineOne, char *lineTwo) {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
Serial.println(lineOne);
Serial.println(lineTwo);
LCD.setCursor(8, 0);
//LCD.println(&timeinfo, "%H:%M:%S");
LCD.println(lineOne);
LCD.setCursor(0, 1);
LCD.println(lineTwo);
//LCD.println(&timeinfo, "%m/%d/%Y %Z");
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%m/%d/%Y %Z");
}
WiFiClientSecure client; // or WiFiClientSecure for HTTPS
HTTPClient http;
char buffer[40];
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
client.setInsecure();
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}
void loop() {
//printLocalTime();
LCD.setCursor(0, 1);
LCD.println("Fetching... ");
/*
// Send request
http.useHTTP10(true);
http.begin(client, CMC_URL);
http.GET();
// Parse response
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, http.getStream());
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return;
}
// Read values
double price = doc["data"]["2665"]["quote"]["USD"]["price"].as<double>();
double change = doc["data"]["2665"]["quote"]["USD"]["percent_change_24h"].as<double>();
const char* updated = doc["data"]["2665"]["quote"]["USD"]["last_updated"].as<const char*>();
*/
double price = 0.00;
double change = -0.0;
struct tm timeinfo = {0};
char buf[100];
// Convert to tm struct
//strptime("2022-11-04T23:08:00.000Z", "%Y-%m-%dT%H:%M:%S.%fZ", &timeinfo);
if (getLocalTime(&timeinfo)) {
strftime(buf, sizeof(buf), "%H:%M:%S", &timeinfo);
Serial.println(buf);
}
if(change > 0) {
sprintf(buffer, "$%2.2f +%2.2f%%", price, change);
} else if (change < 0) {
sprintf(buffer, "$%2.2f %2.2f%%", price, change);
} else {
sprintf(buffer, "Unchanged");
}
printPrice(buf, buffer);
delay(60000);
// Disconnect
http.end();
}