Me voici de retour.
J'ai réussi à récupèrer un code pour configurer un compteur youtube.
J'ai récupéré :
- ma clef API.
- Mon chanel ID.
Je possède toutes les librairies qui sont présentes dans le code et je sais que celui ci a été fait pour un autre écran de la marque ADAFRUIT.
VOICI LE CODE :
#include <YoutubeApi.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <Wire.h> // installed by default
#include <TM1637Display.h>
// For storing configurations
#include "FS.h"
#include <TM1637Display.h>
// WiFiManager Libraries
#include <DNSServer.h> //Local DNS Server used for redirecting all rs to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
const int resetConfigPin = D8; //When high will manually reset the wifi manager config
char apiKey[45] = "AIzaSyu_htq1VTmk";
char channelId[30] = "UCEFCscew";
// label the displays with their i2c addresses
struct {
uint8_t addr; // I2C address
Adafruit_7segment seg7; // 7segment object
} disp[] = {
{ 0x71, Adafruit_7segment() }, // High digits
{ 0x70, Adafruit_7segment() } // Low digits
};
WiFiClientSecure client;
YoutubeApi *api;
unsigned long api_mtbs = 60000; //mean time between api requests
unsigned long api_lasttime; //last time api request has been done
long subscriberCount = 0;
// flag for saving data
bool shouldSaveConfig = false;
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void setup() {
Serial.begin(115200);
for(uint8_t i=0; i<2; i++) { // Initialize displays
disp[i].seg7.begin(disp[i].addr);
disp[i].seg7.clear();
disp[i].seg7.writeDisplay();
}
if (!SPIFFS.begin()) {
Serial.println("Failed to mount FS");
return;
}
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
loadConfig();
WiFiManager wifiManager;
wifiManager.setSaveConfigCallback(saveConfigCallback);
// Adding an additional config on the WIFI manager webpage for the API Key and Channel ID
WiFiManagerParameter customApiKey("apiKey", "API Key", apiKey, 50);
WiFiManagerParameter customChannelId("channelId", "Channel ID", channelId, 35);
wifiManager.addParameter(&customApiKey);
wifiManager.addParameter(&customChannelId);
// If it fails to connect it will create a YouTube-Counter access point
wifiManager.autoConnect("YouTube-Counter", "supersecret");
strcpy(apiKey, customApiKey.getValue());
strcpy(channelId, customChannelId.getValue());
if (shouldSaveConfig) {
saveConfig();
}
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
// Force Config mode if there is no API key
if(strcmp(apiKey, "") > 0) {
Serial.println("Init YouTube API");
api = new YoutubeApi(apiKey, client);
} else {
Serial.println("Forcing Config Mode");
forceConfigMode();
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
IPAddress ip = WiFi.localIP();
Serial.println(ip);
}
bool loadConfig() {
File configFile = SPIFFS.open("/config.json", "r");
if (!configFile) {
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("Config file size is too large");
return false;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (!json.success()) {
Serial.println("Failed to parse config file");
return false;
}
strcpy(apiKey, json["apiKey"]);
strcpy(channelId, json["channelId"]);
return true;
}
bool saveConfig() {
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["apiKey"] = apiKey;
json["channelId"] = channelId;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing");
return false;
}
json.printTo(configFile);
return true;
}
void forceConfigMode() {
Serial.println("Reset");
WiFi.disconnect();
Serial.println("Dq");
delay(500);
ESP.restart();
delay(5000);
}
void loop() {
if (digitalRead(resetConfigPin) == HIGH) {
forceConfigMode();
}
if (millis() - api_lasttime > api_mtbs) {
if(api->getChannelStatistics(channelId))
{
Serial.println("---------Stats---------");
Serial.print("Subscriber Count: ");
Serial.println(api->channelStats.subscriberCount);
Serial.print("View Count: ");
Serial.println(api->channelStats.viewCount);
Serial.print("Comment Count: ");
Serial.println(api->channelStats.commentCount);
Serial.print("Video Count: ");
Serial.println(api->channelStats.videoCount);
// Probably not needed :)
//Serial.print("hiddenSubscriberCount: ");
//Serial.println(api->channelStats.hiddenSubscriberCount);
Serial.println("------------------------");
subscriberCount = api->channelStats.subscriberCount;
uint16_t hi = subscriberCount / 10000, // Value on left (high digits) display
lo = subscriberCount % 10000; // Value on right (low digits) display
disp[0].seg7.print(hi, DEC); // Write values to each display...
disp[1].seg7.print(lo, DEC);
// print() does not zero-pad the displays; this may produce a gap
// between the high and low sections. Here we add zeros where needed...
if(hi) {
if(lo < 1000) {
disp[1].seg7.writeDigitNum(0, 0);
if(lo < 100) {
disp[1].seg7.writeDigitNum(1, 0);
if(lo < 10) {
disp[1].seg7.writeDigitNum(3, 0);
}
}
}
} else {
disp[0].seg7.clear(); // Clear 'hi' display
}
disp[0].seg7.writeDisplay(); // Push data to displays
disp[1].seg7.writeDisplay();
}
api_lasttime = millis();
}
}
J'ai modifié ma clef api et chanel id pour ne pas divulger mes infos perso..
**Seulement, comment est il possible d'adapter ce code pour que mon écran soit compatible à la place de l'adafruit... Je ne trouve pas de documentation qui pourrait m'éclairer.. (c'est une phrase de noob je sais, mais il faut bien débuter, et essayer de mener ses projets à bout, même en ne comprenant pas tout, cela vient au fur et à mesure..)
Ma deuxième question, ce code possède un wifimanager, j'amagine qu'une fois le code implanté, la carte vas créer un réseau wifi, avec une page web qui me permettra d'entrer la configuration wifi ?
Merci pour vos réponses..