Najad
Published © GPL3+

Hackster.io Follower Counter

We are going to learn about how to create a custom API to pull follower counter from hackster.io and display it on an OLED.

BeginnerFull instructions provided957

Things used in this project

Hardware components

UTSOURCE Electronic Parts
UTSOURCE Electronic Parts
×1
UTSOURCE ESP8266
×1
UTSOURCE Breadboard
×1
UTSOURCE OLED I2C
×1

Software apps and online services

Arduino IDE
Arduino IDE
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Circuit

Code

hacksterFollowerCounter.ino

C/C++
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClient.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET LED_BUILTIN // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

ESP8266WiFiMulti WiFiMulti;

// 'Hackster_Lockups_Stacked_Blue_WhiteBg_450x-2', 40x40px
const unsigned char myBitmap [] PROGMEM = {
    0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x07, 0xff, 0xe0, 0x00, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x00, 
  0x7f, 0x00, 0xfe, 0x00, 0x00, 0xf8, 0x00, 0x1f, 0x00, 0x01, 0xe0, 0x00, 0x07, 0x80, 0x03, 0xc0, 
  0x00, 0x03, 0xc0, 0x07, 0x80, 0x00, 0x01, 0xe0, 0x0f, 0x00, 0x00, 0x00, 0xf0, 0x1e, 0x00, 0x00, 
  0x00, 0x78, 0x1c, 0x00, 0x00, 0x60, 0x38, 0x38, 0x00, 0x01, 0xe0, 0x1c, 0x38, 0x00, 0x01, 0xe0, 
  0x1c, 0x70, 0x03, 0x81, 0xe0, 0x0e, 0x70, 0x07, 0x81, 0xe0, 0x0e, 0x70, 0x00, 0x01, 0xe0, 0x0e, 
  0x60, 0x07, 0x81, 0xe0, 0x06, 0xe0, 0x3f, 0x80, 0x00, 0x07, 0xe0, 0x07, 0xff, 0xe0, 0x07, 0xe0, 
  0x07, 0xff, 0xe0, 0x07, 0xe0, 0x07, 0xff, 0xe0, 0x07, 0xe0, 0x07, 0xff, 0xe0, 0x07, 0xe0, 0x00, 
  0x01, 0xfc, 0x07, 0x60, 0x07, 0x81, 0xc0, 0x06, 0x70, 0x07, 0x80, 0x00, 0x0e, 0x70, 0x07, 0x81, 
  0xe0, 0x0e, 0x70, 0x07, 0x81, 0xc0, 0x0e, 0x38, 0x07, 0x80, 0x00, 0x1c, 0x38, 0x07, 0x80, 0x00, 
  0x1c, 0x1c, 0x06, 0x00, 0x00, 0x38, 0x1e, 0x00, 0x00, 0x00, 0x78, 0x0f, 0x00, 0x00, 0x00, 0xf0, 
  0x07, 0x80, 0x00, 0x01, 0xe0, 0x03, 0xc0, 0x00, 0x03, 0xc0, 0x01, 0xe0, 0x00, 0x07, 0x80, 0x00, 
  0xf8, 0x00, 0x1f, 0x00, 0x00, 0x7f, 0x00, 0xfe, 0x00, 0x00, 0x1f, 0xff, 0xf8, 0x00, 0x00, 0x07, 
  0xff, 0xe0, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00
};


void setup() {

  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.println();

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("Honor", "najad123");

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { 
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

}

void loop() {
  // wait for WiFi connection
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    WiFiClient client;

    HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    if (http.begin(client, "api url")) {  // HTTP


      Serial.print("[HTTP] GET...\n");
      // start connection and send HTTP header
      int httpCode = http.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = http.getString();
          payload.replace("<a class=\"user_card__stat__1Lwyp\" href=\"javascript:void(0);\">","");
          payload.replace("Followers","");
          payload.replace("</a>","");
          Serial.println(payload);

          display.clearDisplay();

         display.setTextSize(3);             // Normal 1:1 pixel scale
         display.setTextColor(WHITE);        // Draw white text
         display.setCursor(55,7);             // Start at top-left corner
         display.println((payload));
         display.setCursor(10,45);             // Start at top-left corner
         display.setTextSize(1);  
         display.println("Hackster Followers");
         display.drawBitmap(0, 0, myBitmap, 40, 40, WHITE); // display.drawBitmap(x position, y position, bitmap data, bitmap width, bitmap height, color)
         display.display();
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
  }

  delay(10000);
}

Credits

Najad

Najad

30 projects • 94 followers
Just crazy stuff​

Comments