· 6 years ago · Sep 18, 2019, 09:28 AM
1#include <YoutubeApi.h>
2#include <ESP8266WiFi.h>
3#include <WiFiClientSecure.h>
4#include <ArduinoJson.h> // This Sketch doesn't technically need this, but the library does so it must be installed.
5//This are just for the Hello message
6#include "7seg_helo.h"
7#include "shift.h"
8
9//Outputs
10#define MAX7219_Data_IN 2 //D4 of WeMos
11#define MAX7219_Chip_Select 0 //D3 of WeMos
12#define MAX7219_Clock 4 //D2 of WeMos
13
14int buzzer = 15; //D8 of WeMos
15
16//Variabels
17byte adr = 0x08;
18byte num = 0x00;
19int i = 0;
20long subs = 0;
21String thisString_prev;
22
23
24//////////////////////////////////////////////////////////////////////////////////
25//------- Replace the following! ------
26char ssid[] = "You shall not pass!"; // your network SSID (name)
27char password[] = "0889565101"; // your network key
28#define API_KEY "AIzaSyAYP811eRHcoPBCqodOIA2reTb2UWa870E"
29#define CHANNEL_ID "UCu7_D0o48KbfhpEohoP7YSQ" // makes up the url of channel
30
31WiFiClientSecure client;
32YoutubeApi api(API_KEY, client);
33unsigned long api_mtbs = 60000; //mean time between api requests
34unsigned long api_lasttime; //last time api request has been done
35
36
37
38
39
40
41
42
43void setup() {
44 Serial.begin(115200);
45 delay(100);
46 pinMode(buzzer, OUTPUT);
47 digitalWrite(buzzer, LOW);
48 delay(100);
49
50 //Define the pins as outputs and disable CS
51 pinMode(MAX7219_Data_IN, OUTPUT);
52 pinMode(MAX7219_Chip_Select, OUTPUT);
53 pinMode(MAX7219_Clock, OUTPUT);
54 digitalWrite(MAX7219_Chip_Select, HIGH);
55 delay(200);
56 //Setup of the 7seg module
57 shift(0x0f, 0x00); //display test register - test mode off
58 shift(0x0c, 0x01); //shutdown register - normal operation
59 shift(0x0b, 0x07); //scan limit register - display digits 0 - 7
60 shift(0x0a, 0x0f); //intensity register - max brightness
61 shift(0x09, 0xff); //decode mode register - CodeB decode all digits
62
63 //Print the hello message
64 hello(0,2,100);
65
66
67
68 // Set WiFi to station mode and disconnect from an AP if it was Previously
69 // connected
70 WiFi.mode(WIFI_STA);
71 WiFi.disconnect();
72 delay(100);
73 // Attempt to connect to Wifi network:
74 Serial.print("Connecting Wifi: ");
75 Serial.println(ssid);
76 WiFi.begin(ssid, password);
77 while (WiFi.status() != WL_CONNECTED) {
78 Serial.print(".");
79 delay(500);
80 }
81 Serial.println("");
82 Serial.println("WiFi connected");
83 Serial.println("IP address: ");
84 IPAddress ip = WiFi.localIP();
85 Serial.println(ip);
86}
87
88void loop() {
89
90 if (millis() - api_lasttime > api_mtbs) {
91 if(api.getChannelStatistics(CHANNEL_ID))
92 {
93 unsigned long Count = api.channelStats.subscriberCount;
94 String thisString = String(Count, DEC);
95
96 if(thisString != thisString_prev)
97 {
98 digitalWrite(buzzer,HIGH);
99 delay(1000);
100 digitalWrite(buzzer,LOW);
101 }
102
103 thisString_prev = thisString;
104
105
106 i = thisString.length();//This variable will go character by cahracter and send the value to the 7 segment display
107 if(i==8)
108 {
109 shift(0x0b, 0x07); //scan limit register - display digits 0 thru 7
110 adr=0x01;
111 }
112 if(i==7)
113 {
114 shift(0x0b, 0x06); //scan limit register - display digits 0 thru 7
115 adr=0x01;
116 }
117 if(i==6)
118 {
119 shift(0x0b, 0x05); //scan limit register - display digits 0 thru 7
120 adr=0x01;
121 }
122 if(i==5)
123 {
124 shift(0x0b, 0x04); //scan limit register - display digits 0 thru 7
125 adr=0x01;
126 }
127 if(i==4)
128 {
129 shift(0x0b, 0x03); //scan limit register - display digits 0 thru 7
130 adr=0x01;
131 }
132 if(i==3)
133 {
134 shift(0x0b, 0x02); //scan limit register - display digits 0 thru 7
135 adr=0x01;
136 }
137 if(i==2)
138 {
139 shift(0x0b, 0x01); //scan limit register - display digits 0 thru 7
140 adr=0x01;
141 }
142 if(i==1)
143 {
144 shift(0x0b, 0x00); //scan limit register - display digits 0 thru 7
145 adr=0x01;
146 }
147 i=i-1;
148
149
150
151
152 while (i >= 0)
153 {
154 if(thisString[i] == '0')
155 {
156 num = 0x00;
157 }
158 if(thisString[i] == '1')
159 {
160 num = 0x01;
161 }
162 if(thisString[i] == '2')
163 {
164 num = 0x02;
165 }
166 if(thisString[i] == '3')
167 {
168 num = 0x03;
169 }
170 if(thisString[i] == '4')
171 {
172 num = 0x04;
173 }
174 if(thisString[i] == '5')
175 {
176 num = 0x05;
177 }
178 if(thisString[i] == '6')
179 {
180 num = 0x06;
181 }
182 if(thisString[i] == '7')
183 {
184 num = 0x07;
185 }
186 if(thisString[i] == '8')
187 {
188 num = 0x08;
189 }
190 if(thisString[i] == '9')
191 {
192 num = 0x09;
193 }
194
195
196 shift(adr, num);
197
198
199 adr=adr+0x01;
200 i=i-1;
201 }
202 }
203 api_lasttime = millis();
204 }
205}