Created
June 17, 2017 17:22
-
-
Save gbougakov/f6d9bd8c03086b5a4f2854b542f2351b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <Event.h> | |
#include <Timer.h> | |
#include <EEPROM.h> | |
#include <ArduinoJson.h> | |
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include <YoutubeApi.h> | |
#include <SPI.h> | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_PCD8544.h> | |
// Please customize these: | |
const char* ssid = ""; // Wi-Fi SSID | |
const char* password = ""; // Wi-Fi password | |
String channel = "UCI4vrGWoQ_NEvfxWDcVF55w"; // Default YouTube Channel ID | |
const String apikey = "" | |
// Display connections | |
// pin 13- Serial clock out (SCLK) | |
// pin 15 - Serial data out (DIN) | |
// pin 5 - Data/Command select (D/C) | |
// pin 4 - LCD chip select (CS) | |
// pin 2 - LCD reset (RST) | |
Adafruit_PCD8544 display = Adafruit_PCD8544(13, 14, 5, 4, 15); // If you have other Adafruit compatible screen, initialize it here with a name "display" | |
//If you need to change the text color, see line 46 and line 94 | |
String configPanel = "<script src='http://code.jquery.com/jquery-3.2.1.min.js' integrity='sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=' crossorigin='anonymous'></script><style> @font-face{font-family:screen;src:url(https://georgebgk.github.io/assets/slkscr.ttf)}body{padding:0;font-family:Arial}display{display:block;width:168px;height:96px;background:#d4e157;word-wrap:break-word;overflow:hidden}display span{font-size:64px;font-family:screen;word-wrap:break-word} </style> https://www.youtube.com/channel/<input id='channel' type='text' placeholder='Channel ID (Not shown on the demo)'><br> Font size: <br><input id='size' type='range' value='4' min='1' max='5'><br> Contrast (Not shown on the demo): <br><input id='contrast' type='range' min='1' max='100'> <display> <span>12m</span> </display> <script>$('#size').change(function(){$('display>span').css({fontSize:16*$('#size').val()+'px'}),$.get('/setSize?val='+$('#size').val())}),$('#contrast').change(function(){$.get('/setContrast?val='+$('#contrast').val())}),$('#channel').change(function(){$.get('/changeChannel?val='+$('#channel').val())});</script>"; | |
Timer t; | |
int size = 4; | |
WiFiClientSecure client; | |
YoutubeApi api(apikey, client); | |
ESP8266WebServer server(80); | |
void hserv(); | |
void getChannelStats(); | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
display.begin(); | |
display.setContrast(45); | |
display.clearDisplay(); | |
display.setTextSize(1); | |
display.setTextColor(BLACK); | |
display.println("Connecting to " + String(ssid)); | |
display.display(); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(500); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
IPAddress ip = WiFi.localIP(); | |
Serial.println(ip); | |
display.clearDisplay(); | |
display.println("IP: " + String(ip)); | |
display.display(); | |
if (MDNS.begin("esp8266")) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/", []() { | |
server.send(200, "text/html", configPanel); | |
}); | |
server.on("/changeChannel", []() { | |
channel = server.arg(0); | |
server.send(200, "text/plain", "Channel changed to " + server.arg(0)); | |
Serial.println(channel); | |
}); | |
server.on("/setContrast", []() { | |
int contrast = server.arg(0).toInt(); | |
display.setContrast(contrast); | |
server.send(200, "text/plain", "Contrast changed to " + server.arg(0)); | |
}); | |
server.on("/setSize", []() { | |
size = server.arg(0).toInt(); | |
server.send(200, "text/plain", "Contrast changed to " + server.arg(0)); | |
}); | |
server.begin(); | |
t.every(2000, getChannelStats); | |
t.every(1, hserv); | |
delay(2000); | |
} | |
void hserv() { | |
server.handleClient(); | |
} | |
void getChannelStats() { | |
if (api.getChannelStatistics(channel)) { | |
display.clearDisplay(); | |
display.setCursor(0, 0); | |
display.setTextSize(size); | |
display.setTextColor(BLACK); | |
Serial.print("Subscriber Count: "); | |
Serial.println(api.channelStats.subscriberCount); | |
if (api.channelStats.subscriberCount > 1000000) { | |
String subcount = String(round(api.channelStats.subscriberCount / 100000) / 10); | |
display.println(subcount + "m"); | |
} else if (api.channelStats.subscriberCount > 1000) { | |
String subcount = String(round(api.channelStats.subscriberCount / 100) / 10); | |
display.println(subcount + "k"); | |
} else { | |
display.println(api.channelStats.subscriberCount); | |
} | |
display.display(); | |
delay(2000); | |
} | |
} | |
void loop() { | |
t.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment