ESP8266 API Confusion

 #include <YoutubeApi.h>

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>


//------------LED------------//
#include "DigitLedDisplay.h"

/* Arduino Pin to Display Pin
   D1 to DIN,
   D3 to CS,
   D2 to CLK */
   
DigitLedDisplay ld = DigitLedDisplay(5, 0, 4);



//------- Replace the following! ------
char ssid[] = "[REMOVED FOR PRIVACY]"; 
char password[] = "[REMOVED FOR PRIVACY]";  
#define API_KEY "[REMOVED FOR PRIVACY]"  
#define CHANNEL_ID "[REMOVED FOR PRIVACY]" 



//------------LED------------//

/////////////////////////////////






WiFiClientSecure client;
YoutubeApi api(API_KEY, client);

unsigned long api_mtbs = 1000; //mean time between api requests
unsigned long api_lasttime;   //last time api request has been done

long subs = 0;

void setup() {

  Serial.begin(9600);

//------------LED------------//

  /* Set the brightness min:1, max:15 */
  ld.setBright(10);

  /* Set the digit count */
  ld.setDigitLimit(8);



  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  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);


}

void loop() {


  ld.printDigit(api.channelStats.subscriberCount);
  delay(500);



  if (millis() > api_lasttime + api_mtbs)  {
    if(api.getChannelStatistics(CHANNEL_ID))
    {
      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("------------------------");

    }
    api_lasttime = millis();
  }




  ld.clear();

 
  
}

I’m currently using an ESP8266 connected to a MAX7218 7-segment display with the intent of displaying a YouTube channel’s subscriber counter live.
I’ve run some testing on the board and I added Serial.print(api.channelStats.subscriberCount); to the code under loop (just after the delay command) and after monitoring the port it seems to be returning “0”. I’m not sure where to start searching for a problem.

(Currently attending grade school still and thus replies may be somewhat delayed.)

#include <YoutubeApi.h>


/*******************************************************************
 *  An example of bot that echos back any messages received        *
 *                                                                 *
 *  written by Giacarlo Bacchio (Gianbacchio on Github)            *
 *  adapted by Brian Lough                                         *
 *******************************************************************/


#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>


//------------LED------------//
#include "DigitLedDisplay.h"

/* Arduino Pin to Display Pin
   D1 to DIN,
   D3 to CS,
   D2 to CLK */
   
DigitLedDisplay ld = DigitLedDisplay(5, 0, 4);



//------- Replace the following! ------
char ssid[] = "REMOVED FOR PRIVACY/SECURITY"; // your network SSID (name)
char password[] = "REMOVED FOR PRIVACY/SECURITY";  // your network key
#define API_KEY "REMOVED FOR PRIVACY/SECURITY"  // your google apps API Token
#define CHANNEL_ID "REMOVED FOR PRIVACY/SECURITY" // makes up the url of channel 



//------------LED------------//

/////////////////////////////////






WiFiClientSecure client;
YoutubeApi api(API_KEY, client);

unsigned long api_mtbs = 1000; //mean time between api requests
unsigned long api_lasttime;   //last time api request has been done

long subs = 0;

void setup() {

  Serial.begin(9600);

//------------LED------------//

  /* Set the brightness min:1, max:15 */
  ld.setBright(10);

  /* Set the digit count */
  ld.setDigitLimit(8);



  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  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);


}

void loop() {


  ld.printDigit(api.channelStats.subscriberCount);
  Serial.print(api.channelStats.subscriberCount);
  delay(500);



  if (millis() > api_lasttime + api_mtbs)  {
    if(api.getChannelStatistics(CHANNEL_ID))
    {
      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("------------------------");

    }
    api_lasttime = millis();
  }




  ld.clear();

 
  
}

This is an update to a previous thread I had made (which has started to become buried). After some testing I’ve narrowed down that the connection between the board and display is alright. I have minimal experience working the the JSON library’s and such though by what I’ve been reading it seems API’s and Arduino boards don’t like to behave correctly. I added “Serial.print(api.channelStats.subscriberCount);” to check what the return was via the COM port and it endlessly returns “0”.

Testing also showed that the WiFi connected without issue as well.

Does anybody here have experience with API’s (YouTube Data API V3) & Json libraries/code?

Duplicate topics merged

Do not start duplicate threads on a whim. If you update an old thread it will appear at the start of the thread list, just like a new one

Duplicate posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Repeated duplicate posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board.

Thanks in advance for your cooperation.

  ld.printDigit(api.channelStats.subscriberCount);
  Serial.print(api.channelStats.subscriberCount);
  delay(500);

At this point in the code, getChannelStatistics() has not yet been called, so subscriberCount is probably defaulting to zero. Don't print the value until after a successful call to getChannelStatistics().

  if (millis() > api_lasttime + api_mtbs)  {

This line will not be a cause of problem you are having, but needs correcting because it could cause issues when millis() returns to zero, which happens every ~50 days. Change this to

  if (millis() - api_lasttime > api_mtbs)  {

This line

    if(api.getChannelStatistics(CHANNEL_ID))

could be returning false/zero, indicating that getting the channel data failed for some reason. You should add an "else" part where you print some error message to serial monitor so you know that this has happened. Also, any return value which is not zero will be interpreted as success, but some non-zero return values may indicate a failure of some kind. The library documentation should describe the meaning of the return value. If you are having problems understanding the library documentation, post a link here and we can try to help.

Your code is set to request an update from the API every 1000ms. Some API set a limit on how frequently a user is allowed to request updates. Maybe you are exceeding that limit?

ArduinoJson.h

YoutubeApi.h

The ArduinoJson library is the 5.x version as the YouTubeApi doesn't support version 6.x

As some recommend on this (Not working anymore? · Issue #18 · witnessmenow/arduino-youtube-api · GitHub) they recommended adding client.setInsecure(); which after trying didn't seem to solve this issue.
One user says they added in BearSSL clinet, any ideas what that is? I supposedly found a BearSSL library though after trying to put it into Arduino it came back with no "Specified folder/zip file does not contain a valid library".

Arduino: 1.8.12 (Windows 10), Board: "NodeMCU 0.9 (ESP-12 Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

Executable segment sizes:


IROM   : 355260          - code in flash         (default or ICACHE_FLASH_ATTR) 


IRAM   : 26928   / 32768 - code in IRAM          (ICACHE_RAM_ATTR, ISRs...) 


DATA   : 1284  )         - initialized variables (global, static) in RAM/HEAP 


RODATA : 1584  ) / 81920 - constants             (global, static) in RAM/HEAP 


BSS    : 25272 )         - zeroed variables      (global, static) in RAM/HEAP 


Sketch uses 385056 bytes (36%) of program storage space. Maximum is 1044464 bytes.
Global variables use 28140 bytes (34%) of dynamic memory, leaving 53780 bytes for local variables. Maximum is 81920 bytes.
esptool.py v2.8
Serial port COM5
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: dc:4f:22:50:e7:c4
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 389216 bytes to 287054...

Writing at 0x00000000... (5 %)
Writing at 0x00004000... (11 %)
Writing at 0x00008000... (16 %)
Writing at 0x0000c000... (22 %)
Writing at 0x00010000... (27 %)
Writing at 0x00014000... (33 %)
Writing at 0x00018000... (38 %)
Writing at 0x0001c000... (44 %)
Writing at 0x00020000... (50 %)
Writing at 0x00024000... (55 %)
Writing at 0x00028000... (61 %)
Writing at 0x0002c000... (66 %)
Writing at 0x00030000... (72 %)
Writing at 0x00034000... (77 %)
Writing at 0x00038000... (83 %)
Writing at 0x0003c000... (88 %)
Writing at 0x00040000... (94 %)
Writing at 0x00044000... (100 %)
Wrote 389216 bytes (287054 compressed) at 0x00000000 in 25.6 seconds (effective 121.6 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
Specified folder/zip file does not contain a valid library
Specified folder/zip file does not contain a valid library

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

And the output you se on Serial Monitor?

SUCCESS

Apparently one of the changes I made to the code today seemingly fixed it. Perhaps the solution was in fact adding "client.setInsecure();". Quite literally, I left on a walk for awhile and returned to the board functioning and after uploading the same program to a few others it seems to be fixed.

I wish I could state the exact solution for others though all I can really recommend is.

  1. Use ArduinoJson version 5.x (5.13.5 I believe) as opposed to version 6.x as the YoutubeApi has yet to be updated and compatible with it.
  2. Apparently the YoutubeApi was updated and caused some issues for some users. Adding "client.setInsecure();" to the setup seemingly fixed the issue.
  3. It may take some time for your API to respond. I disconnected mine for a few hours and after reconnecting it, it's been +45 minutes and has yet to connect.

This is how you put a link in your posts in this forum. The forum's software is a little quirky and won't automatically detect and parse URLs. Annoyingly, it will automatically detect and parse smilies, even when they are actually bits of C code which the poster has failed to put code tags around.

class YoutubeApi
{
  public:
    YoutubeApi (String apiKey, Client &client);
    String sendGetToYoutube(String command);
    bool getChannelStatistics(String channelId);
    channelStatistics channelStats;
    bool _debug = false;

The above tells us that getChannelStatistics() returns a Boolean, not a numeric status code. But printing a message in your code if that returns false would have helped you figure out what was going on more quickly.

Also there is a "_debug" flag. You could try setting that to true, it might print some useful clues to serial monitor.