DEV Community

Cover image for [10$] Arduino Twitter Follower Counter DIY ๐Ÿ”จ Wemos D1 mini ๐Ÿ’ก
Kaede Games ๐ŸŽฎ
Kaede Games ๐ŸŽฎ

Posted on • Originally published at qiita.com

[10$] Arduino Twitter Follower Counter DIY ๐Ÿ”จ Wemos D1 mini ๐Ÿ’ก

Prologue

Iโ€™d like to make my own Twitter follower counter box, but all of the codes that I referred are out of date.

So, I code by myself and finally, succeed! ๐ŸŽ

Image description

BTY, This is ๐Ÿ my twitter, follow me. (Over 30K followers now)

https://twitter.com/kaedeee_you/status/1393835529658015744

Prerequisites

  • Wemos D1 mini
  • Arduino Software
  • 20x4 LCD
  • mini size Box
  • Glue gun (Optional)

https://kaedeee.com/?p=2534

DIY

Wire design, etcโ€ฆ๐Ÿ‘‡

https://www.youtube.com/watch?v=evLzYZYiqXg

Code

//////////////////////////////////////////////
//                                          //
//   20x4 Subscriber Counter for Twitter    //
//                                          //
/////////////////////////////////////////////

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>

// #include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>

const char* ssid     = "XXX"; // SSID of local network
const char* password = "XXX"; // Password on network
String BEARER_TOKEN = "XXX";
String TWITTER_CHANNEL_ID = "XXX";   // Twitter id

long subscribers, subscribersBefore = 0;

LiquidCrystal_I2C lcd(0x27, 20, 4);  // Set the LCD I2C address

void setup()
{
  lcd.init();
  lcd.clear();
  lcd.backlight();
  Serial.begin(9600);
  int cursorPosition = 0;

  lcd.setCursor(0, 0);
  lcd.print("Connecting ....");

  createCustomChars();

  // wifi setting
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    lcd.setCursor(cursorPosition, 1);
    lcd.print(".");
    cursorPosition++;
  }

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Connected!");
  Serial.println("Connected!");
  delay(1000);
}

void loop()
{
  int length;
  int subscribers = getSubscribers();
  if (subscribers != subscribersBefore)
  {
    lcd.clear();
    String subscribersString = String(subscribers);
    length = subscribersString.length();
    printSubscribers(length, subscribersString);
    subscribersBefore = subscribers;
  }

  delay(60000);
}

byte LT[8] =
{
  B01111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};

byte UB[8] =
{
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000
};
byte RT[8] =
{
  B11110,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111
};

byte LL[8] =
{
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B01111
};

byte LB[8] =
{
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111,
  B11111
};

byte LR[8] =
{
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11110
};

byte UMB[8] =
{
  B11111,
  B11111,
  B11111,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111
};

byte LMB[8] =
{
  B11111,
  B00000,
  B00000,
  B00000,
  B00000,
  B11111,
  B11111,
  B11111
};

void printSubscribers(int length, String subscribersString)
{
  switch (length)
  {
    case 5: printDigits(subscribersString, 5); printDigits(subscribersString, 4); printDigits(subscribersString, 3); printDigits(subscribersString, 2); printDigits(subscribersString, 1); break;
    case 4: printDigits(subscribersString, 4); printDigits(subscribersString, 3); printDigits(subscribersString, 2); printDigits(subscribersString, 1); break;
    case 3: printDigits(subscribersString, 3); printDigits(subscribersString, 2); printDigits(subscribersString, 1); break;
    case 2: printDigits(subscribersString, 2); printDigits(subscribersString, 1); break;
    case 1: printDigits(subscribersString, 1); break;
    default: break;
  }
}

void printDigits(String subscribersString, int digit)
{
  int length = subscribersString.length();
  char digitToPrint = subscribersString.charAt(length - digit);
  Serial.println(digitToPrint);
  switch (digit)
  {
    case 1: printDigitToScreen(digitToPrint, 17); break;
    case 2: printDigitToScreen(digitToPrint, 13); break;
    case 3: printDigitToScreen(digitToPrint, 9); break;
    case 4: printDigitToScreen(digitToPrint, 5); break;
    case 5: printDigitToScreen(digitToPrint, 1); break;
  }
}

void printDigitToScreen(char digit, int position)
{
  int digitToPrint = digit - '0'; // Convert from char to int
  switch (digitToPrint)
  {
    case 1: digit1(position); break;
    case 2: digit2(position); break;
    case 3: digit3(position); break;
    case 4: digit4(position); break;
    case 5: digit5(position); break;
    case 6: digit6(position); break;
    case 7: digit7(position); break;
    case 8: digit8(position); break;
    case 9: digit9(position); break;
    case 0: digit0(position); break;
  }
}

void digit0(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write((byte)0);
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(position_x, 2);
  lcd.write(3);
  lcd.write(4);
  lcd.write(5);
}

void digit1(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(position_x + 1, 2);
  lcd.write(5);
}

void digit2(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write(6);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(position_x, 2);
  lcd.write(3);
  lcd.write(7);
  lcd.write(7);
}

void digit3(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write(6);
  lcd.write(6);
  lcd.write(2);
  lcd.setCursor(position_x, 2);
  lcd.write(7);
  lcd.write(7);
  lcd.write(5);
}

void digit4(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write(3);
  lcd.write(4);
  lcd.write(2);
  lcd.setCursor(position_x + 2, 2);
  lcd.write(5);
}

void digit5(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write((byte)0);
  lcd.write(6);
  lcd.write(6);
  lcd.setCursor(position_x, 2);
  lcd.write(7);
  lcd.write(7);
  lcd.write(5);
}

void digit6(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write((byte)0);
  lcd.write(6);
  lcd.write(6);
  lcd.setCursor(position_x, 2);
  lcd.write(3);
  lcd.write(7);
  lcd.write(5);
}

void digit7(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write(1);
  lcd.write(1);
  lcd.write(2);
  lcd.setCursor(position_x + 1, 2);
  lcd.write((byte)0);
}

void digit8(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write((byte)0);
  lcd.write((byte)6);
  lcd.write(2);
  lcd.setCursor(position_x, 2);
  lcd.write(3);
  lcd.write(7);
  lcd.write(5);
}

void digit9(int position_x)
{
  lcd.setCursor(position_x, 1);
  lcd.write((byte)0);
  lcd.write((byte)6);
  lcd.write((byte)2);
  lcd.setCursor(position_x + 2, 2);
  lcd.write((byte)5);
}

int getSubscribers()
{
  int subscribers = 0;

  BearSSL::WiFiClientSecure client;
  client.setTimeout(500);
  client.setInsecure();  // If you wouldn't use certificate.

  String host = "api.twitter.com";
  String url = "/2/users/by/username/" + TWITTER_CHANNEL_ID;
  String argument = "user.fields=public_metrics";

  const int httpPort = 443;
  const char* host2 = host.c_str();
  if (!client.connect(host2, httpPort)) {
    Serial.println("connection failed");
    return false;
  }
  client.print(String("GET ") + url + "?" + argument + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Authorization: Bearer " + BEARER_TOKEN + "\r\n" +
               "User-Agent: ESP8266/1.0\r\n" +
               "Accept: text/plain\r\n" +
               "Connection: close\r\n\r\n" +
               "\r\n" +
               "\0");
  client.flush();

  unsigned long timeout = micros();
  while (client.available() == 0) {
    if ( micros() - timeout  > 5000000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return false;
    }
  }

  String resultJson = "";
  while (client.available()) {
    resultJson = client.readStringUntil('\r');
  }

  Serial.println("resultJson: " + resultJson);

  DynamicJsonDocument doc(1024);
  DeserializationError err = deserializeJson(doc, resultJson);
  if ( err != DeserializationError::Ok )
  {
    Serial.printf("Deserialization Error:%s\n", err.c_str());
    return false;
  }
  JsonObject obj = doc.as<JsonObject>();

  subscribers = obj["data"]["public_metrics"]["followers_count"];
  Serial.println("subscribers: " + (String)subscribers);

  return subscribers;
}

void createCustomChars()
{
  lcd.createChar(0, LT);
  lcd.createChar(1, UB);
  lcd.createChar(2, RT);
  lcd.createChar(3, LL);
  lcd.createChar(4, LB);
  lcd.createChar(5, LR);
  lcd.createChar(6, UMB);
  lcd.createChar(7, LMB);
}
Enter fullscreen mode Exit fullscreen mode

Good job, bro๐Ÿฆ„

https://kaedeee.com/?p=2534

Top comments (0)