Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SevSeg.h>
- #include <ArduinoJson.h>
- #include <ArduinoJson.hpp>
- #include <WiFi.h>
- #include <HTTPClient.h>
- // -- SETTINGS START --
- // Set to your WLAN credentials
- const char* ssid = "WLAN-Name";
- const char* password = "WLAN-Password";
- // If you want to display bullet rating instead, toggle to true
- const bool showBullet = false;
- // Refresh Time in seconds
- const int refreshTime = 60;
- // Chess.com username of which to display the ratings
- const std::string username = "Username";
- // -- SETTINGS END --
- // the following variables are unsigned longs because the time, measured in
- // milliseconds, will quickly become a bigger number than can be stored in an int.
- unsigned long lastTime = 0;
- // Timer set to 10 minutes (600000)
- //unsigned long timerDelay = 600000;
- // Set timer to 5 seconds (5000)
- unsigned long timerDelay = refreshTime * 1000;
- // Custom Pin Allocation
- int pinE = 15; //1
- int pinD = 2; //2
- int pinDP = 0; //3
- int pinC = 4; //4
- int pinG = 16; //5
- int D4 = 5; //6 (switched w 7)
- int pinB = 23; //7 (switched w 6)
- int D3 = 22; //8
- int D2 = 21; //9
- int pinF = 19; //10
- int pinA = 18; //11
- int D1 = 17; //12
- // Temporarily saved variable of what to display
- int showValue = 8888;
- void setup() {
- // Set all segments & digits as outputs
- pinMode(pinA, OUTPUT);
- pinMode(pinB, OUTPUT);
- pinMode(pinC, OUTPUT);
- pinMode(pinD, OUTPUT);
- pinMode(pinE, OUTPUT);
- pinMode(pinF, OUTPUT);
- pinMode(pinG, OUTPUT);
- pinMode(pinDP, OUTPUT);
- pinMode(D1, OUTPUT);
- pinMode(D2, OUTPUT);
- pinMode(D3, OUTPUT);
- pinMode(D4, OUTPUT);
- // Display value and dot, waiting for connection (if connected, no dot)
- digitalWrite(pinDP, LOW); // Turns off DP (if low)
- writeNumber(showValue);
- // Setup WIFI and Serial
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- Serial.println("Connecting");
- while(WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to WiFi network with IP Address: ");
- Serial.println(WiFi.localIP());
- Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
- }
- void loop() {
- digitalWrite(pinDP, HIGH); // Turns off DP (if low)
- writeNumber(showValue);
- if ((millis() - lastTime) > timerDelay || showValue == 8888) {
- //Check WiFi connection status
- if(WiFi.status()== WL_CONNECTED){
- HTTPClient http;
- // Construct the API URL
- std::string serverPath = "https://api.chess.com/pub/player/" + username + "/stats";
- // Your Domain name with URL path or IP address with path
- http.begin(serverPath.c_str());
- // Send HTTP GET request
- int httpResponseCode = http.GET();
- if (httpResponseCode>0) {
- Serial.print("HTTP Response code: ");
- Serial.println(httpResponseCode);
- String payload = http.getString();
- // Serial.println(payload);
- JsonDocument doc;
- deserializeJson(doc, payload);
- int lastRapidRating = doc["chess_rapid"]["last"]["rating"];
- int lastBulletRating = doc["chess_bullet"]["last"]["rating"];
- Serial.println("Last Rapid Rating: " + String(lastRapidRating));
- Serial.println("Last Bullet Rating: " + String(lastBulletRating));
- showValue = showBullet ? lastBulletRating : lastRapidRating;
- writeNumber(showValue);
- } else {
- Serial.print("Error code: ");
- Serial.println(httpResponseCode);
- }
- // Free resources
- http.end();
- }
- else {
- Serial.println("WiFi Disconnected");
- }
- lastTime = millis();
- }
- }
- void numberToArray(int value, int numberArray[]) {
- numberArray[3] = value % 10;
- numberArray[2] = ((value - numberArray[3]) % 100) / 10;
- numberArray[1] = ((value - numberArray[3] - numberArray[2]) % 1000) / 100;
- numberArray[0] = value / 1000;
- }
- void writeNumber(int x) {
- int vals[4];
- numberToArray(x, vals);
- for(int i = 0; i < 4; i++) {
- switch(i) {
- case 0:
- digit1();
- break;
- case 1:
- digit2();
- break;
- case 2:
- digit3();
- break;
- case 3:
- digit4();
- break;
- }
- switch(vals[i]) {
- case 0:
- zero();
- break;
- case 1:
- one();
- break;
- case 2:
- two();
- break;
- case 3:
- three();
- break;
- case 4:
- four();
- break;
- case 5:
- five();
- break;
- case 6:
- six();
- break;
- case 7:
- seven();
- break;
- case 8:
- eight();
- break;
- case 9:
- nine();
- break;
- }
- delay(1);
- }
- }
- //functions representing numbers 0-9
- void zero() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, HIGH);
- }
- void one() {
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, HIGH);
- }
- void two() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, HIGH);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, LOW);
- }
- void three() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, LOW);
- }
- void four() {
- digitalWrite(pinA, HIGH);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- }
- void five() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- }
- void six() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, HIGH);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- }
- void seven() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, HIGH);
- digitalWrite(pinG, HIGH);
- }
- void eight() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- }
- void nine() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, HIGH);
- digitalWrite(pinE, HIGH);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- }
- void allNumbers() {
- one();
- delay(500);
- two();
- delay(500);
- three();
- delay(500);
- four();
- delay(500);
- five();
- delay(500);
- six();
- delay(500);
- seven();
- delay(500);
- eight();
- delay(500);
- nine();
- delay(500);
- }
- void turnOffAllSegments() {
- digitalWrite(pinA, LOW);
- digitalWrite(pinB, LOW);
- digitalWrite(pinC, LOW);
- digitalWrite(pinD, LOW);
- digitalWrite(pinE, LOW);
- digitalWrite(pinF, LOW);
- digitalWrite(pinG, LOW);
- }
- //functions to select individual digits on the display
- void digit1() {
- digitalWrite(D1, HIGH);
- digitalWrite(D2, LOW);
- digitalWrite(D3, LOW);
- digitalWrite(D4, LOW);
- }
- void digit2() {
- digitalWrite(D1, LOW);
- digitalWrite(D2, HIGH);
- digitalWrite(D3, LOW);
- digitalWrite(D4, LOW);
- }
- void digit3() {
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- digitalWrite(D3, HIGH);
- digitalWrite(D4, LOW);
- }
- void digit4() {
- digitalWrite(D1, LOW);
- digitalWrite(D2, LOW);
- digitalWrite(D3, LOW);
- digitalWrite(D4, HIGH);
- }
- void all4Digits() {
- digitalWrite(D1, HIGH);
- digitalWrite(D2, HIGH);
- digitalWrite(D3, HIGH);
- digitalWrite(D4, HIGH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement