Skip to content

Instantly share code, notes, and snippets.

@blackcoffeexbt
Created February 4, 2022 15:28
Show Gist options
  • Save blackcoffeexbt/a82fa9e53dbbc75c9836ec0fdb39ee04 to your computer and use it in GitHub Desktop.
Save blackcoffeexbt/a82fa9e53dbbc75c9836ec0fdb39ee04 to your computer and use it in GitHub Desktop.
mempool_epaper.ino
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#ifndef BOARD_HAS_PSRAM
#error "Please enable PSRAM !!!"
#endif
#include <HTTPClient.h>
#include <Arduino.h>
#include <esp_task_wdt.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "epd_driver.h"
#include "firasans.h"
#include "esp_adc_cal.h"
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include "logo.h"
#include <esp_sleep.h>
using namespace std;
#define BATT_PIN 36
#define SD_MISO 12
#define SD_MOSI 13
#define SD_SCLK 14
#define SD_CS 15
uint8_t *framebuffer;
int vref = 1100;
void setup()
{
char buf[128];
Serial.begin(115200);
// // Set WiFi to station mode and disconnect from an AP if it was previously connected
// WiFi.mode(WIFI_STA);
// WiFi.disconnect();
// delay(100);
/*
* SD Card test
* Only as a test SdCard hardware, use example reference
* https://github.com/espressif/arduino-esp32/tree/master/libraries/SD/examples
* * */
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI);
bool rlst = SD.begin(SD_CS);
if (!rlst) {
Serial.println("SD init failed");
snprintf(buf, 128, "➸ No detected SdCard");
} else {
snprintf(buf, 128, "➸ Detected SdCard insert:%.2f GB", SD.cardSize() / 1024.0 / 1024.0 / 1024.0);
}
epd_init();
framebuffer = (uint8_t *)ps_calloc(sizeof(uint8_t), EPD_WIDTH * EPD_HEIGHT / 2);
if (!framebuffer) {
Serial.println("alloc memory failed !!!");
while (1);
}
memset(framebuffer, 0xFF, EPD_WIDTH * EPD_HEIGHT / 2);
}
void loop()
{
initWiFi();
mempoolSpace();
Serial.println("Going to sleep");
esp_sleep_enable_timer_wakeup(60 * 1000 * 1000);
esp_deep_sleep_start();
Serial.println("Waking up");
sleep(60000);
}
// TODO: Refactor this mess!
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin("SSID", "APPASSWORD");
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(1000);
}
Serial.println(WiFi.localIP());
}
WiFiClientSecure client;
void mempoolSpace() {
HTTPClient http;
client.setInsecure();
Serial.println("Getting data");
// Send request
//http.begin(client, "http://arduinojson.org/example.json");
http.begin(client, "https://mempool.space/api/v1/fees/recommended");
http.GET();
// Print the response
Serial.println("Got data");
// Print the response
String data = http.getString();
Serial.print(data);
Serial.print("Getting JSON");
StaticJsonDocument<200> doc;
Serial.println("Declared doc");
DeserializationError error = deserializeJson(doc, data);
Serial.println("deserialised");
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
Serial.println("Error deserializing");
return;
}
String fastestFee = String(doc["fastestFee"].as<long>());
String halfHourFee = String(doc["halfHourFee"].as<long>());
String hourFee = String(doc["hourFee"].as<long>());
String minimumFee = String(doc["minimumFee"].as<long>());
Serial.println("Fastest fee " + fastestFee + " sats / vbyte");
// Disconnect
http.end();
displayData(fastestFee, halfHourFee, hourFee, minimumFee);
}
void displayData(String fastestFee,String halfHourFee, String hourFee,String minimumFee) {
int cursor_x = 200;
int cursor_y = 150;
String feeEnd = " sats/vbyte\n";
String string1 = "Fastest: " + fastestFee + feeEnd;
String string2 = "Half hour: " + halfHourFee + feeEnd;
String string3 = "Hour: " + hourFee + feeEnd;
String string4 = "Minimum: " + minimumFee + feeEnd;
epd_poweron();
epd_clear();
displayVoltage();
// clearLine(cursor_x, cursor_y);
writeln((GFXfont *)&FiraSans, "Mempool stats", &cursor_x, &cursor_y, NULL);
cursor_x = 200;
cursor_y += 50;
// clearLine(cursor_x, cursor_y);
writeln((GFXfont *)&FiraSans, string1.c_str(), &cursor_x, &cursor_y, NULL);
cursor_x = 200;
cursor_y += 50;
//clearLine(cursor_x, cursor_y);
writeln((GFXfont *)&FiraSans, string2.c_str(), &cursor_x, &cursor_y, NULL);
cursor_x = 200;
cursor_y += 50;
// clearLine(cursor_x, cursor_y);
writeln((GFXfont *)&FiraSans, string3.c_str(), &cursor_x, &cursor_y, NULL);
// clearLine(cursor_x, cursor_y);
cursor_x = 200;
cursor_y += 50;
writeln((GFXfont *)&FiraSans, string4.c_str(), &cursor_x, &cursor_y, NULL);
epd_poweroff();
}
void displayVoltage() {
// When reading the battery voltage, POWER_EN must be turned on
epd_poweron();
delay(10); // Make adc measurement more accurate
uint16_t v = analogRead(BATT_PIN);
float battery_voltage = ((float)v / 4095.0) * 2.0 * 3.3 * (vref / 1000.0);
String voltage = "➸ Voltage :" + String(battery_voltage) + "V";
Serial.println(voltage);
Rect_t area = {
.x = 200,
.y = 460,
.width = 320,
.height = 50,
};
int cursor_x = 200;
int cursor_y = 500;
epd_clear_area(area);
writeln((GFXfont *)&FiraSans, (char *)voltage.c_str(), &cursor_x, &cursor_y, NULL);
}
void clearLine(int xPos, int yPos) {
Rect_t area = {
.x = xPos,
.y = yPos,
.width = 500,
.height = 70,
};
int cursor_x = 200;
int cursor_y = 500;
epd_clear_area(area);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment