m5paper_wifi_weather_json-7.ino

M5Paper: IoT Development Kit with Tactile e-Ink Display · File : m5paper_wifi_weather_json-7.ino · Language : C++ · Use case : Se connecter au Wi-Fi, récupérer des données météo via une API JSON et les afficher sur l'écran e-Ink.

Illustrates Wi-Fi and HTTP client usage to fetch external data and render it on the e-Ink screen with network error handling.

Requires WiFi.h, HTTPClient.h, and ArduinoJson. Replace Wi-Fi credentials and OpenWeatherMap API key. Display uses update() for full e-Ink rendering.

m5paper_wifi_weather_json-7.ino

#include <M5Paper.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>

const char* ssid = "VOTRE_WIFI_SSID";
const char* password = "VOTRE_WIFI_PASSWORD";
const char* url = "http://api.openweathermap.org/data/2.5/weather?q=Paris&appid=VOTRE_API_KEY&units=metric";

void setup() {
  M5.begin(true, false, false);
  M5.Display.init();
  M5.Display.setRotation(1);
  
  M5.Display.drawString("Connexion Wi-Fi...", 10, 10);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    M5.Display.drawString(".", M5.Display.getCursorX(), M5.Display.getCursorY());
  }
  
  M5.Display.clear();
  M5.Display.drawString("Connecte! Recuperation data...", 10, 10);
  M5.Display.update();
  fetchWeather();
}

void loop() {
  delay(60000); // Mise à jour chaque minute
  fetchWeather();
}

void fetchWeather() {
  HTTPClient http;
  http.begin(url);
  int httpCode = http.GET();
  
  if (httpCode == HTTP_CODE_OK) {
    String payload = http.getString();
    DynamicJsonDocument doc(1024);
    deserializeJson(doc, payload);
    
    float temp = doc["main"]["temp"];
    String desc = doc["weather"][0]["description"].as<String>();
    String city = doc["name"].as<String>();
    
    M5.Display.fillScreen(WHITE);
    M5.Display.setTextSize(3);
    M5.Display.drawString(city, 10, 10);
    M5.Display.setTextSize(2);
    M5.Display.drawString(String(temp) + " C", 10, 60);
    M5.Display.drawString(desc, 10, 100);
    M5.Display.update();
  } else {
    M5.Display.drawString("Erreur Reseau: " + String(httpCode), 10, 10);
    M5.Display.update();
  }
  http.end();
}

← Back to the board