Skip to content

Instantly share code, notes, and snippets.

@praeclarum
Created March 28, 2021 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save praeclarum/69ba4e89ad0e32a1998e3a93330561e1 to your computer and use it in GitHub Desktop.
Save praeclarum/69ba4e89ad0e32a1998e3a93330561e1 to your computer and use it in GitHub Desktop.
Arduino code to print James' subscriber count
// ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#include "secrets.h"
const char* ssid = SECRET_SSID; // SSID of local network
const char* password = SECRET_PASSWORD; // Password on network
const long UPDATE_INTERVAL_MILLIS = 10L * 60L * 1000L;
//const long UPDATE_INTERVAL_MILLIS = 1L * 1000L;
const char* API_URL = "https://getyoutubesubscribercount20210328114249.azurewebsites.net/api/GetStats";
// ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//#define DEBUG_ESP_PORT Serial
/*
The code for this project is based on code originally written by Pawel A. Hernik
https://youtu.be/mn9L85bhyjI
Revised by Lewis for DIY Machines
https://youtu.be/QWaVYCVoqbc
I made an error in the video - you need to input your YouTube Channel ID above, not your User ID.
VCC - 3.3v
GND - G
D8 - DataIn
D7 - LOAD/CS
D6 - CLK
If you have difficulties try going to your Arduino IDE Board Manager and rolling back the 'ESP8266 by ESP8266 Community' to
version 2.4.2
*/
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
ESP8266WiFiMulti WiFiMulti;
#include <WiFiClientSecureBearSSL.h>
#define NUM_MAX 4
#define ROTATE 90
// for NodeMCU 1.0
#define DIN_PIN 15 // D8
#define CS_PIN 13 // D7
#define CLK_PIN 12 // D6
#include "max7219.h"
#include "fonts.h"
const char HeartChar = ' ' + (sizeof(font)-1)/6 - 5;
const char SmileyChar = ' ' + (sizeof(font)-1)/6 - 8;
void setup()
{
Serial.begin(115200);
initMAX7219();
sendCmdAll(CMD_SHUTDOWN,1);
sendCmdAll(CMD_INTENSITY,0);
printStringWithShift((String("Load ") + String(SmileyChar)).c_str(), 0);
Serial.print("Connecting WiFi ");
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(SECRET_SSID, SECRET_PASSWORD);
}
// =======================================================================
void loop()
{
if (WiFiMulti.run() == WL_CONNECTED) {
Serial.println("Getting data ...");
//printStringWithShift(" ... YT ...",15);
int views, cnt = 0;
String subs;
String yt1;
if(getYTSubs(subs,&views)==0) {
Serial.println(subs);
yt1 = subs;
} else {
yt1 = " Error";
}
auto m = String(" ") + String(HeartChar) + String(" ") + yt1;
printStringWithShift(m.c_str(),50);
delay(UPDATE_INTERVAL_MILLIS);
}
}
// =======================================================================
int showChar(char indexIntoFont, const uint8_t *font)
{
int bytesPerFontChar = pgm_read_byte(font);
const auto fontDataOffset = font + 1 + indexIntoFont * bytesPerFontChar;
int numCols = pgm_read_byte(fontDataOffset);
for (int c = 0; c < numCols; c++)
scr[NUM_MAX*8 + c] = pgm_read_byte(fontDataOffset + 1 + c);
scr[NUM_MAX*8 + numCols] = 0;
return numCols;
}
// =======================================================================
int dualChar = 0;
unsigned char convertPolish(unsigned char _c)
{
unsigned char c = _c;
if(c==196 || c==197 || c==195) {
dualChar = c;
return 0;
}
if(dualChar) {
switch(_c) {
case 133: c = 1+'~'; break; // 'ą'
case 135: c = 2+'~'; break; // 'ć'
case 153: c = 3+'~'; break; // 'ę'
case 130: c = 4+'~'; break; // 'ł'
case 132: c = dualChar==197 ? 5+'~' : 10+'~'; break; // 'ń' and 'Ą'
case 179: c = 6+'~'; break; // 'ó'
case 155: c = 7+'~'; break; // 'ś'
case 186: c = 8+'~'; break; // 'ź'
case 188: c = 9+'~'; break; // 'ż'
//case 132: c = 10+'~'; break; // 'Ą'
case 134: c = 11+'~'; break; // 'Ć'
case 152: c = 12+'~'; break; // 'Ę'
case 129: c = 13+'~'; break; // 'Ł'
case 131: c = 14+'~'; break; // 'Ń'
case 147: c = 15+'~'; break; // 'Ó'
case 154: c = 16+'~'; break; // 'Ś'
case 185: c = 17+'~'; break; // 'Ź'
case 187: c = 18+'~'; break; // 'Ż'
default: break;
}
dualChar = 0;
return c;
}
switch(_c) {
case 185: c = 1+'~'; break;
case 230: c = 2+'~'; break;
case 234: c = 3+'~'; break;
case 179: c = 4+'~'; break;
case 241: c = 5+'~'; break;
case 243: c = 6+'~'; break;
case 156: c = 7+'~'; break;
case 159: c = 8+'~'; break;
case 191: c = 9+'~'; break;
case 165: c = 10+'~'; break;
case 198: c = 11+'~'; break;
case 202: c = 12+'~'; break;
case 163: c = 13+'~'; break;
case 209: c = 14+'~'; break;
case 211: c = 15+'~'; break;
case 140: c = 16+'~'; break;
case 143: c = 17+'~'; break;
case 175: c = 18+'~'; break;
default: break;
}
return c;
}
// =======================================================================
void printCharWithShift(unsigned char c, int shiftDelay) {
c = convertPolish(c);
if (c < ' ' || c > MAX_CHAR) return;
c -= 32;
int w = showChar(c, font);
for (int i=0; i<w+1; i++) {
delay(shiftDelay);
scrollLeft();
refreshAll();
}
}
// =======================================================================
void printStringWithShift(const char* s, int shiftDelay){
while (*s) {
printCharWithShift(*s++, shiftDelay);
}
}
// =======================================================================
unsigned int convToInt(const char *txt)
{
unsigned int val = 0;
for(int i=0; i<strlen(txt); i++)
if(isdigit(txt[i])) val=val*10+(txt[i]&0xf);
return val;
}
// =======================================================================
int getYTSubs(String &subs, int *pViews)
{
if(!pViews) return -2;
*pViews = 0;
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
//client->setFingerprint(fingerprint);
client->setInsecure();
HTTPClient https;
String url = API_URL;
// Serial.print("[HTTPS] begin...\n");
if (!https.begin(*client, url.c_str())) {
Serial.println("[HTTPS] Failed");
return -3;
}
int httpCode = https.GET();
// Serial.println("[HTTPS] Success");
// Serial.println(httpCode);
String payload = https.getString();
// Serial.println("PAYLOAD SIZE");
// Serial.println(payload.length());
// Serial.println(payload.c_str());
subs = payload;
https.end();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment