Makertronic

esp32 ticker TFT32

Mar 25th, 2024
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.36 KB | Cryptocurrency | 0 0
  1. /*
  2. *****************************************************************************
  3. *
  4. *                        ESP32 oLED 128x64 - TFT TICKER
  5. *
  6. *****************************************************************************
  7.  
  8. Il vous faut : 1 ESP32 S1,
  9.                1 breadboard,
  10.                des fils de connexion.
  11.  
  12.  
  13. * Web : https://www.makertronic-yt.com
  14. * version : 1.0
  15. * Copyright 2024
  16. */
  17.  
  18. /* *******************************************************************************************
  19. *
  20. *  Libs
  21. *  
  22. * *******************************************************************************************/
  23. #include <Arduino.h>
  24. #include <SPI.h>
  25. #include <Wire.h>
  26. #include <Adafruit_GFX.h>
  27. #include <Adafruit_SSD1306.h>
  28. #include <WiFi.h>
  29. #include "time.h"
  30. #include <HTTPClient.h>
  31. #include <ArduinoJson.h>
  32.  
  33.  
  34. /* *******************************************************************************************
  35. *
  36. *  Variables
  37. *  
  38. * *******************************************************************************************/
  39.  
  40. // connexion wifi
  41. const char* ssid = "xxxxxxxxxxxxxx";  // A changer
  42. const char* password = "xxxxxxxxxxxxxxx"; // A changer
  43.  
  44. // codes crypto
  45. String code1 = "BTC";
  46. String code2 = "KAS";
  47. String code3 = "WART";
  48.  
  49. // serveur NTP
  50. const char* ntpServer = "pool.ntp.org";
  51. const long  gmtOffset_sec = 3600 * 1;
  52. const int   daylightOffset_sec = 3600 * 0;
  53.  
  54. // taille écran oled
  55. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  56. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  57.  
  58. // écran oled config : SSD1306 - I2C (SDA, SCL pins)
  59. #define OLED_RESET     -1 // Reset pin
  60. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  61.  
  62. // url API ( doc : https://livecoinwatch.github.io/lcw-api-docs/?java#coinslist )
  63. const char* url = "https://api.livecoinwatch.com/coins/map";
  64. const char* apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // A changer
  65.  
  66.  
  67. /* *******************************************************************************************
  68. *
  69. *  SETUP
  70. *  
  71. * *******************************************************************************************/
  72.  
  73. void setup(void) {
  74.  
  75.   Serial.begin(9600);
  76.   delay(1000);
  77.  
  78.   WiFi.mode(WIFI_STA); //Optional
  79.   WiFi.begin(ssid, password);
  80.   Serial.println("\nConnection Wifi ...");
  81.  
  82.   while(WiFi.status() != WL_CONNECTED){
  83.       Serial.print(".");
  84.       delay(100);
  85.   }
  86.  
  87.   Serial.println("\nConnecté au réseau Wifi !");
  88.   Serial.print("IP locale: ");
  89.   Serial.println(WiFi.localIP());
  90.  
  91.   // On configure le seveur NTP
  92.   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  93.  
  94.   // ecran OLED
  95.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  96.     Serial.println(F("SSD1306 ecran non trouvé"));
  97.     for(;;); // arret du programme
  98.   }
  99.  
  100.   display.display(); // logo de démarrage
  101.   delay(100);
  102.   display.clearDisplay(); // efface le buffer
  103. }
  104.  
  105. /* *******************************************************************************************
  106. *
  107. *  Boucle principale
  108. *  
  109. * *******************************************************************************************/
  110.  
  111. void loop() {
  112.   // initialisation de l'écran
  113.   display.clearDisplay();
  114.   display.setTextSize(1);
  115.   display.setTextColor(WHITE);
  116.   display.setCursor(30, 0);
  117.   display.println(F("CRYPTO Bingo"));
  118.   display.drawLine( 0, 15, 128, 15, WHITE);
  119.  
  120.     HTTPClient http;
  121.     http.begin(url);
  122.     http.addHeader("Content-Type", "application/json");
  123.     http.addHeader("x-api-key", apiKey);
  124.  
  125.     // Créer le corps de la requête
  126.     String json = "{\"currency\": \"USD\",\"codes\": [\"" + code1 + "\", \"" + code2 + "\", \"" + code3 + "\"],\"sort\": \"rank\",\"order\": \"ascending\",\"offset\": 0,\"limit\": 0,\"meta\": false}";
  127.  
  128.     // Envoyer la requête POST
  129.     int httpCode = http.POST(json);
  130.  
  131.     // Vérifier la réponse
  132.     if (httpCode > 0) {
  133.       String payload = http.getString();
  134.       Serial.println("HTTP POST Response Code: " + String(httpCode));
  135.       Serial.println("HTTP POST Response: " + payload);
  136.      
  137.       // Analyser la réponse JSON
  138.       DynamicJsonDocument doc(1024);
  139.       DeserializationError error = deserializeJson(doc, payload);
  140.       if (error) {
  141.         Serial.print(F("deserializeJson() failed: "));
  142.         Serial.println(error.c_str());
  143.         return;
  144.       }
  145.  
  146.       int i = 0;
  147.  
  148.       // Extraire et imprimer les valeurs de rate et volume pour chaque coin
  149.       for (JsonObject coin : doc.as<JsonArray>()) {
  150.  
  151.         display.setTextColor(WHITE);
  152.  
  153.         Serial.print("Code: ");
  154.         Serial.println(coin["code"].as<String>()); // Convertir en String
  155.         display.setCursor(10, 25 + i);
  156.         display.print(coin["code"].as<String>());
  157.  
  158.         Serial.print("Rate: ");
  159.         Serial.println(coin["rate"].as<String>()); // Convertir en String
  160.         display.setCursor(40, 25 + i);
  161.         display.print(coin["rate"].as<String>());
  162.  
  163.         Serial.print("Volume: ");
  164.         Serial.println(coin["volume"].as<String>()); // Convertir en String
  165.         //display.setCursor(35, 25);
  166.         //display.print(F(coin["rate"].as<String>()));
  167.  
  168.         i = i + 15;
  169.         Serial.println();
  170.       }
  171.  
  172.     } else {
  173.       Serial.println("Error on sending POST: " + String(httpCode));
  174.     }
  175.  
  176.     http.end();
  177.  
  178.   display.display();  
  179.  
  180.   //delay(10000); // Attendre 1 seconde
  181.  
  182.   delay(300000); // Attendre 5 minutes avant de recommencer
  183.  
  184. }
Tags: ESP32
Add Comment
Please, Sign In to add comment