Advertisement
SilvanM

Display chess.com rating on 4 piece 7-segmented display

Mar 14th, 2024
1,055
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.16 KB | None | 0 0
  1. #include <SevSeg.h>
  2.  
  3. #include <ArduinoJson.h>
  4. #include <ArduinoJson.hpp>
  5.  
  6. #include <WiFi.h>
  7. #include <HTTPClient.h>
  8.  
  9. // -- SETTINGS START --
  10.  
  11. // Set to your WLAN credentials
  12. const char* ssid = "WLAN-Name";
  13. const char* password = "WLAN-Password";
  14.  
  15. // If you want to display bullet rating instead, toggle to true
  16. const bool showBullet = false;
  17.  
  18. // Refresh Time in seconds
  19. const int refreshTime = 60;
  20.  
  21. // Chess.com username of which to display the ratings
  22. const std::string username = "Username";
  23.  
  24. // -- SETTINGS END --
  25.  
  26. // the following variables are unsigned longs because the time, measured in
  27. // milliseconds, will quickly become a bigger number than can be stored in an int.
  28. unsigned long lastTime = 0;
  29. // Timer set to 10 minutes (600000)
  30. //unsigned long timerDelay = 600000;
  31. // Set timer to 5 seconds (5000)
  32. unsigned long timerDelay = refreshTime * 1000;
  33.  
  34. // Custom Pin Allocation
  35. int pinE = 15;  //1
  36. int pinD = 2;   //2
  37. int pinDP = 0;  //3
  38. int pinC = 4;   //4
  39. int pinG = 16;  //5
  40. int D4 = 5;     //6 (switched w 7)
  41. int pinB = 23;  //7 (switched w 6)
  42. int D3 = 22;    //8
  43. int D2 = 21;    //9
  44. int pinF = 19;  //10
  45. int pinA = 18;  //11
  46. int D1 = 17;    //12
  47.  
  48. // Temporarily saved variable of what to display
  49. int showValue = 8888;
  50.  
  51.  
  52. void setup() {  
  53.   // Set all segments & digits as outputs
  54.   pinMode(pinA, OUTPUT);
  55.   pinMode(pinB, OUTPUT);
  56.   pinMode(pinC, OUTPUT);
  57.   pinMode(pinD, OUTPUT);
  58.   pinMode(pinE, OUTPUT);
  59.   pinMode(pinF, OUTPUT);
  60.   pinMode(pinG, OUTPUT);
  61.   pinMode(pinDP, OUTPUT);
  62.   pinMode(D1, OUTPUT);
  63.   pinMode(D2, OUTPUT);
  64.   pinMode(D3, OUTPUT);
  65.   pinMode(D4, OUTPUT);
  66.  
  67.   // Display value and dot, waiting for connection (if connected, no dot)
  68.   digitalWrite(pinDP, LOW); // Turns off DP (if low)
  69.   writeNumber(showValue);
  70.  
  71.   // Setup WIFI and Serial
  72.   Serial.begin(115200);
  73.  
  74.   WiFi.begin(ssid, password);
  75.   Serial.println("Connecting");
  76.   while(WiFi.status() != WL_CONNECTED) {
  77.     delay(500);
  78.     Serial.print(".");
  79.   }
  80.   Serial.println("");
  81.   Serial.print("Connected to WiFi network with IP Address: ");
  82.   Serial.println(WiFi.localIP());
  83.  
  84.   Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
  85. }
  86.  
  87.  
  88. void loop() {
  89.   digitalWrite(pinDP, HIGH); // Turns off DP (if low)
  90.   writeNumber(showValue);
  91.  
  92.   if ((millis() - lastTime) > timerDelay || showValue == 8888) {
  93.     //Check WiFi connection status
  94.     if(WiFi.status()== WL_CONNECTED){
  95.       HTTPClient http;
  96.  
  97.       // Construct the API URL
  98.       std::string serverPath = "https://api.chess.com/pub/player/" + username + "/stats";
  99.      
  100.       // Your Domain name with URL path or IP address with path
  101.       http.begin(serverPath.c_str());
  102.      
  103.       // Send HTTP GET request
  104.       int httpResponseCode = http.GET();
  105.      
  106.       if (httpResponseCode>0) {
  107.         Serial.print("HTTP Response code: ");
  108.         Serial.println(httpResponseCode);
  109.         String payload = http.getString();
  110.         // Serial.println(payload);
  111.  
  112.         JsonDocument doc;
  113.         deserializeJson(doc, payload);
  114.  
  115.         int lastRapidRating = doc["chess_rapid"]["last"]["rating"];
  116.         int lastBulletRating = doc["chess_bullet"]["last"]["rating"];
  117.  
  118.         Serial.println("Last Rapid Rating: " + String(lastRapidRating));
  119.         Serial.println("Last Bullet Rating: " + String(lastBulletRating));
  120.  
  121.         showValue = showBullet ? lastBulletRating : lastRapidRating;
  122.         writeNumber(showValue);
  123.  
  124.       } else {
  125.         Serial.print("Error code: ");
  126.         Serial.println(httpResponseCode);
  127.       }
  128.       // Free resources
  129.       http.end();
  130.     }
  131.     else {
  132.       Serial.println("WiFi Disconnected");
  133.     }
  134.    
  135.     lastTime = millis();
  136.   }
  137.  
  138.  
  139. }
  140.  
  141. void numberToArray(int value, int numberArray[]) {
  142.   numberArray[3] = value % 10;
  143.   numberArray[2] = ((value - numberArray[3]) % 100) / 10;
  144.   numberArray[1] = ((value - numberArray[3] - numberArray[2]) % 1000) / 100;
  145.   numberArray[0] = value / 1000;
  146. }
  147.  
  148.  
  149. void writeNumber(int x) {
  150.   int vals[4];
  151.   numberToArray(x, vals);
  152.  
  153.   for(int i = 0; i < 4; i++) {
  154.     switch(i) {
  155.       case 0:
  156.         digit1();
  157.         break;
  158.       case 1:
  159.         digit2();
  160.         break;
  161.       case 2:
  162.         digit3();
  163.         break;
  164.       case 3:
  165.         digit4();
  166.         break;
  167.     }
  168.    
  169.     switch(vals[i]) {
  170.       case 0:
  171.         zero();
  172.         break;
  173.       case 1:
  174.         one();
  175.         break;
  176.       case 2:
  177.         two();
  178.         break;
  179.       case 3:
  180.         three();
  181.         break;
  182.       case 4:
  183.         four();
  184.         break;
  185.       case 5:
  186.         five();
  187.         break;
  188.       case 6:
  189.         six();
  190.         break;
  191.       case 7:
  192.         seven();
  193.         break;
  194.       case 8:
  195.         eight();
  196.         break;
  197.       case 9:
  198.         nine();
  199.         break;
  200.     }
  201.  
  202.     delay(1);
  203.   }
  204. }
  205.  
  206.  
  207.  
  208. //functions representing numbers 0-9
  209. void zero() {
  210.   digitalWrite(pinA, LOW);
  211.   digitalWrite(pinB, LOW);
  212.   digitalWrite(pinC, LOW);
  213.   digitalWrite(pinD, LOW);
  214.   digitalWrite(pinE, LOW);
  215.   digitalWrite(pinF, LOW);
  216.   digitalWrite(pinG, HIGH);
  217. }
  218.  
  219. void one() {
  220.   digitalWrite(pinA, HIGH);
  221.   digitalWrite(pinB, LOW);
  222.   digitalWrite(pinC, LOW);
  223.   digitalWrite(pinD, HIGH);
  224.   digitalWrite(pinE, HIGH);
  225.   digitalWrite(pinF, HIGH);
  226.   digitalWrite(pinG, HIGH);
  227. }
  228.  
  229. void two() {
  230.   digitalWrite(pinA, LOW);
  231.   digitalWrite(pinB, LOW);
  232.   digitalWrite(pinC, HIGH);
  233.   digitalWrite(pinD, LOW);
  234.   digitalWrite(pinE, LOW);
  235.   digitalWrite(pinF, HIGH);
  236.   digitalWrite(pinG, LOW);
  237. }
  238.  
  239. void three() {
  240.   digitalWrite(pinA, LOW);
  241.   digitalWrite(pinB, LOW);
  242.   digitalWrite(pinC, LOW);
  243.   digitalWrite(pinD, LOW);
  244.   digitalWrite(pinE, HIGH);
  245.   digitalWrite(pinF, HIGH);
  246.   digitalWrite(pinG, LOW);
  247. }
  248.  
  249. void four() {
  250.   digitalWrite(pinA, HIGH);
  251.   digitalWrite(pinB, LOW);
  252.   digitalWrite(pinC, LOW);
  253.   digitalWrite(pinD, HIGH);
  254.   digitalWrite(pinE, HIGH);
  255.   digitalWrite(pinF, LOW);
  256.   digitalWrite(pinG, LOW);
  257. }
  258.  
  259. void five() {
  260.   digitalWrite(pinA, LOW);
  261.   digitalWrite(pinB, HIGH);
  262.   digitalWrite(pinC, LOW);
  263.   digitalWrite(pinD, LOW);
  264.   digitalWrite(pinE, HIGH);
  265.   digitalWrite(pinF, LOW);
  266.   digitalWrite(pinG, LOW);
  267. }
  268.  
  269. void six() {
  270.   digitalWrite(pinA, LOW);
  271.   digitalWrite(pinB, HIGH);
  272.   digitalWrite(pinC, LOW);
  273.   digitalWrite(pinD, LOW);
  274.   digitalWrite(pinE, LOW);
  275.   digitalWrite(pinF, LOW);
  276.   digitalWrite(pinG, LOW);
  277. }
  278.  
  279. void seven() {
  280.   digitalWrite(pinA, LOW);
  281.   digitalWrite(pinB, LOW);
  282.   digitalWrite(pinC, LOW);
  283.   digitalWrite(pinD, HIGH);
  284.   digitalWrite(pinE, HIGH);
  285.   digitalWrite(pinF, HIGH);
  286.   digitalWrite(pinG, HIGH);
  287. }
  288.  
  289. void eight() {
  290.   digitalWrite(pinA, LOW);
  291.   digitalWrite(pinB, LOW);
  292.   digitalWrite(pinC, LOW);
  293.   digitalWrite(pinD, LOW);
  294.   digitalWrite(pinE, LOW);
  295.   digitalWrite(pinF, LOW);
  296.   digitalWrite(pinG, LOW);
  297. }
  298.  
  299. void nine() {
  300.   digitalWrite(pinA, LOW);
  301.   digitalWrite(pinB, LOW);
  302.   digitalWrite(pinC, LOW);
  303.   digitalWrite(pinD, HIGH);
  304.   digitalWrite(pinE, HIGH);
  305.   digitalWrite(pinF, LOW);
  306.   digitalWrite(pinG, LOW);
  307. }
  308.  
  309. void allNumbers() {
  310.   one();
  311.   delay(500);
  312.   two();
  313.   delay(500);
  314.   three();
  315.   delay(500);
  316.   four();
  317.   delay(500);
  318.   five();
  319.   delay(500);
  320.   six();
  321.   delay(500);
  322.   seven();
  323.   delay(500);
  324.   eight();
  325.   delay(500);
  326.   nine();
  327.   delay(500);
  328. }
  329.  
  330. void turnOffAllSegments() {
  331.   digitalWrite(pinA, LOW);
  332.   digitalWrite(pinB, LOW);
  333.   digitalWrite(pinC, LOW);
  334.   digitalWrite(pinD, LOW);
  335.   digitalWrite(pinE, LOW);
  336.   digitalWrite(pinF, LOW);
  337.   digitalWrite(pinG, LOW);
  338. }
  339.  
  340.  
  341. //functions to select individual digits on the display
  342. void digit1() {
  343.   digitalWrite(D1, HIGH);
  344.   digitalWrite(D2, LOW);
  345.   digitalWrite(D3, LOW);
  346.   digitalWrite(D4, LOW);
  347. }
  348.  
  349. void digit2() {
  350.   digitalWrite(D1, LOW);
  351.   digitalWrite(D2, HIGH);
  352.   digitalWrite(D3, LOW);
  353.   digitalWrite(D4, LOW);
  354. }
  355.  
  356. void digit3() {
  357.   digitalWrite(D1, LOW);
  358.   digitalWrite(D2, LOW);
  359.   digitalWrite(D3, HIGH);
  360.   digitalWrite(D4, LOW);
  361. }
  362.  
  363. void digit4() {
  364.   digitalWrite(D1, LOW);
  365.   digitalWrite(D2, LOW);
  366.   digitalWrite(D3, LOW);
  367.   digitalWrite(D4, HIGH);
  368. }
  369.  
  370. void all4Digits() {
  371.   digitalWrite(D1, HIGH);
  372.   digitalWrite(D2, HIGH);
  373.   digitalWrite(D3, HIGH);
  374.   digitalWrite(D4, HIGH);
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement