Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *****************************************************************************
- *
- * ESP32 oLED 128x64 - TFT TICKER
- *
- *****************************************************************************
- Il vous faut : 1 ESP32 S1,
- 1 breadboard,
- des fils de connexion.
- * Web : https://www.makertronic-yt.com
- * version : 1.0
- * Copyright 2024
- */
- /* *******************************************************************************************
- *
- * Libs
- *
- * *******************************************************************************************/
- #include <Arduino.h>
- #include <SPI.h>
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <WiFi.h>
- #include "time.h"
- #include <HTTPClient.h>
- #include <ArduinoJson.h>
- /* *******************************************************************************************
- *
- * Variables
- *
- * *******************************************************************************************/
- // connexion wifi
- const char* ssid = "xxxxxxxxxxxxxx"; // A changer
- const char* password = "xxxxxxxxxxxxxxx"; // A changer
- // codes crypto
- String code1 = "BTC";
- String code2 = "KAS";
- String code3 = "WART";
- // serveur NTP
- const char* ntpServer = "pool.ntp.org";
- const long gmtOffset_sec = 3600 * 1;
- const int daylightOffset_sec = 3600 * 0;
- // taille écran oled
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- // écran oled config : SSD1306 - I2C (SDA, SCL pins)
- #define OLED_RESET -1 // Reset pin
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // url API ( doc : https://livecoinwatch.github.io/lcw-api-docs/?java#coinslist )
- const char* url = "https://api.livecoinwatch.com/coins/map";
- const char* apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // A changer
- /* *******************************************************************************************
- *
- * SETUP
- *
- * *******************************************************************************************/
- void setup(void) {
- Serial.begin(9600);
- delay(1000);
- WiFi.mode(WIFI_STA); //Optional
- WiFi.begin(ssid, password);
- Serial.println("\nConnection Wifi ...");
- while(WiFi.status() != WL_CONNECTED){
- Serial.print(".");
- delay(100);
- }
- Serial.println("\nConnecté au réseau Wifi !");
- Serial.print("IP locale: ");
- Serial.println(WiFi.localIP());
- // On configure le seveur NTP
- configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
- // ecran OLED
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
- Serial.println(F("SSD1306 ecran non trouvé"));
- for(;;); // arret du programme
- }
- display.display(); // logo de démarrage
- delay(100);
- display.clearDisplay(); // efface le buffer
- }
- /* *******************************************************************************************
- *
- * Boucle principale
- *
- * *******************************************************************************************/
- void loop() {
- // initialisation de l'écran
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(WHITE);
- display.setCursor(30, 0);
- display.println(F("CRYPTO Bingo"));
- display.drawLine( 0, 15, 128, 15, WHITE);
- HTTPClient http;
- http.begin(url);
- http.addHeader("Content-Type", "application/json");
- http.addHeader("x-api-key", apiKey);
- // Créer le corps de la requête
- String json = "{\"currency\": \"USD\",\"codes\": [\"" + code1 + "\", \"" + code2 + "\", \"" + code3 + "\"],\"sort\": \"rank\",\"order\": \"ascending\",\"offset\": 0,\"limit\": 0,\"meta\": false}";
- // Envoyer la requête POST
- int httpCode = http.POST(json);
- // Vérifier la réponse
- if (httpCode > 0) {
- String payload = http.getString();
- Serial.println("HTTP POST Response Code: " + String(httpCode));
- Serial.println("HTTP POST Response: " + payload);
- // Analyser la réponse JSON
- DynamicJsonDocument doc(1024);
- DeserializationError error = deserializeJson(doc, payload);
- if (error) {
- Serial.print(F("deserializeJson() failed: "));
- Serial.println(error.c_str());
- return;
- }
- int i = 0;
- // Extraire et imprimer les valeurs de rate et volume pour chaque coin
- for (JsonObject coin : doc.as<JsonArray>()) {
- display.setTextColor(WHITE);
- Serial.print("Code: ");
- Serial.println(coin["code"].as<String>()); // Convertir en String
- display.setCursor(10, 25 + i);
- display.print(coin["code"].as<String>());
- Serial.print("Rate: ");
- Serial.println(coin["rate"].as<String>()); // Convertir en String
- display.setCursor(40, 25 + i);
- display.print(coin["rate"].as<String>());
- Serial.print("Volume: ");
- Serial.println(coin["volume"].as<String>()); // Convertir en String
- //display.setCursor(35, 25);
- //display.print(F(coin["rate"].as<String>()));
- i = i + 15;
- Serial.println();
- }
- } else {
- Serial.println("Error on sending POST: " + String(httpCode));
- }
- http.end();
- display.display();
- //delay(10000); // Attendre 1 seconde
- delay(300000); // Attendre 5 minutes avant de recommencer
- }
Add Comment
Please, Sign In to add comment