English
Español
PCBWAY PCB service

PCBWAY PCB service

PCBONLINE PCB service






ESP8266 subscriber count code (HELLO included)


Download the .zip file below. Unzip it and open it in Arduino IDE. Compile mount this schematic and upload. You have the 7seg_hello.h library in the ZIP file as well. If you don't want the HELLO message at the beggining just download this code. Remember to select the WeMos mini board.








/*
 * http://www.electronoobs.com
 * http://www.youtube.com/c/ELECTRONOOBS
 * 
 * Like, Share and Subscribe, Thank you!!!!
 */

//Includes
#include <YoutubeApi.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h> // This Sketch doesn't technically need this, but the library does so it must be installed.
//This are just for the Hello message
#include "7seg_helo.h"
#include "shift.h"

//Outputs
#define MAX7219_Data_IN 2         //D4 of WeMos
#define MAX7219_Chip_Select  0    //D3 of WeMos
#define MAX7219_Clock 4           //D2 of WeMos

int buzzer = 15;                  //D8 of WeMos

//Variabels
byte adr = 0x08;
byte num = 0x00;
int i = 0;
long subs = 0;
String thisString_prev;


//////////////////////////////////////////////////////////////////////////////////
//------- Replace the following! ------
char ssid[] = "Your_WIFI_NAME_HERE";              // your network SSID (name)
char password[] = "WIFI_PASSWORD";                // your network key
#define API_KEY "YOUR_API_KEYHERE"
#define CHANNEL_ID "UCjiVhIvGmRZixSzupD0sS9Q"     // makes up the url of channel

WiFiClientSecure client;
YoutubeApi api(API_KEY, client);
unsigned long api_mtbs = 60000; //mean time between api requests
unsigned long api_lasttime;   //last time api request has been done




void setup() {  
  Serial.begin(115200);
  delay(100);
  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer, LOW);
  delay(200);
  
  //Define the pins as outputs and disable CS
  pinMode(MAX7219_Data_IN, OUTPUT);
  pinMode(MAX7219_Chip_Select, OUTPUT);
  pinMode(MAX7219_Clock, OUTPUT);
  digitalWrite(MAX7219_Chip_Select, HIGH);
  delay(200);
  //Setup of the 7seg module
  shift(0x0f, 0x00); //display test register - test mode off
  shift(0x0c, 0x01); //shutdown register - normal operation
  shift(0x0b, 0x07); //scan limit register - display digits 0 - 7
  shift(0x0a, 0x0f); //intensity register - max brightness
  shift(0x09, 0xff); //decode mode register - CodeB decode all digits

  //Print the hello message
  hello(0,2,100);


  
  // 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() {

  if (millis() - api_lasttime > api_mtbs)  {
    if(api.getChannelStatistics(CHANNEL_ID))
    {
      unsigned long Count = api.channelStats.subscriberCount;
      String thisString = String(Count, DEC);
      
      if(thisString != thisString_prev)
      {
        digitalWrite(buzzer,200);
        delay(1000);
        digitalWrite(buzzer,LOW);       
      }
      
      
      i =  thisString.length();//This variable will go character by cahracter and send the value to the 7 segment display
      if(i==8)
        {
          shift(0x0b, 0x07); //scan limit register - display digits 0 thru 7
          adr=0x01;
        }        
        if(i==7)
        {
          shift(0x0b, 0x06); //scan limit register - display digits 0 thru 7
          adr=0x01;
        }
        if(i==6)
        {
          shift(0x0b, 0x05); //scan limit register - display digits 0 thru 7
          adr=0x01;
        }
        if(i==5)
        {
          shift(0x0b, 0x04); //scan limit register - display digits 0 thru 7
          adr=0x01;
        }
        if(i==4)
        {
          shift(0x0b, 0x03); //scan limit register - display digits 0 thru 7
          adr=0x01;
        }
        if(i==3)
        {
          shift(0x0b, 0x02); //scan limit register - display digits 0 thru 7
          adr=0x01;
        }
        if(i==2)
        {
          shift(0x0b, 0x01); //scan limit register - display digits 0 thru 7
          adr=0x01;
        }
        if(i==1)
        {
          shift(0x0b, 0x00); //scan limit register - display digits 0 thru 7
          adr=0x01;
        }
        i=i-1;

   
      
      
      while (i >= 0)
      {
        if(thisString[i] == '0')
        {
        num = 0x00;
        }
        if(thisString[i] == '1')
        {
        num = 0x01;
        }
        if(thisString[i] == '2')
        {
        num = 0x02;
        }
        if(thisString[i] == '3')
        {
        num = 0x03;
        }
        if(thisString[i] == '4')
        {
        num = 0x04;
        }
        if(thisString[i] == '5')
        {
        num = 0x05;
        }
        if(thisString[i] == '6')
        {
        num = 0x06;
        }
        if(thisString[i] == '7')
        {
        num = 0x07;
        }
        if(thisString[i] == '8')
        {
        num = 0x08;
        }
        if(thisString[i] == '9')
        {
        num = 0x09;
        }

           
        shift(adr, num);  

              
        adr=adr+0x01;
        i=i-1;         
      }
    }
    api_lasttime = millis();
  }
}



subscriber counter tutorial arduino