/********* Rui Santos Complete project details at https://RandomNerdTutorials.com *********/ #define VERSION "14-05-2022" #include #include #include #include #include #include const char *ssid ="ON_AIR2021"; // attention aux probleme avec guillemets (anglais) orientés !!! const char *password ="cGT>1_@?LOdBqXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXoG6}v4"; String openWeatherMapApiKey ="b05d3fc50b32cd28727fe2422552b975"; //http://api.openweathermap.org/data/2.5/weather?q=Meximieux,fr&appid=b05d3fc50b32cd28727fe2422552b975&units=metric String city = "Meximieux"; String countryCode = "fr"; String units="metric"; // THE DEFAULT TIMER IS SET TO 20 SECONDS FOR TESTING PURPOSES // For a final application, check the API call limits per hour/minute to avoid getting blocked/banned unsigned long lastTime = 0; // Set timer to 20 seconds (20000) unsigned long timerDelay = 60000; String jsonBuffer; int Result; int i=1; int j=0; int status = WL_IDLE_STATUS; byte mac[6]; long rssi ; byte encryption; void setup() { delay(5000); // le temps de stopper et ré-armer le terminal pour voir le debut du programme Serial.begin(115200); Serial.println(" "); delay(200); Serial.println("VSC ESP32_HTTP_GET_OpenWeatherMap"); Serial.print("Version :"); Serial.println(VERSION); delay(1000); // Serial.println("Wifi disconnect"); // WiFi.disconnect(); WiFi.begin(ssid,password); Serial.print("Connection en cours :"); j=0; while(WiFi.status() != WL_CONNECTED); { // Result=WiFi.status(); // Serial.print(" Wifi Status = "); // Serial.println(Result); Serial.print("."); if (j>32) { j=0; Serial.println("."); } j++; delay(250); } Serial.println(""); Serial.print("Connection au reseau Wifi "); Serial.print(ssid); Serial.print(" et adresse IP : "); Serial.println(WiFi.localIP()); Serial.println("Timing =60sec (timerDelay variable), ...rafraichissement dans 1mn."); // print your MAC address: WiFi.macAddress(mac); Serial.print("MAC address: "); Serial.print(mac[0], HEX); Serial.print(":"); Serial.print(mac[1], HEX); Serial.print(":"); Serial.print(mac[2], HEX); Serial.print(":"); Serial.print(mac[3], HEX); Serial.print(":"); Serial.print(mac[4], HEX); Serial.print(":"); Serial.println(mac[5],HEX); delay(1000); rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.println(rssi); } void loop() { // Send an HTTP GET request if ((millis() - lastTime) > timerDelay) { // Check WiFi connection status if(WiFi.status()== WL_CONNECTED) { String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&APPID=" + openWeatherMapApiKey+"&units="+units; jsonBuffer = httpGETRequest(serverPath.c_str()); //Serial.println(jsonBuffer); JSONVar myObject = JSON.parse(jsonBuffer); // JSON.typeof(jsonVar) can be used to get the type of the var if (JSON.typeof(myObject) == "undefined") { Serial.println("Parsing input failed!"); return; } if (i<2) { Serial.println(" "); Serial.print("JSON object = "); Serial.println(myObject); } Serial.print(" Lieu: "); Serial.println(myObject["name"]); Serial.print(" mesure # "); Serial.println(i); Serial.print("Couverture nuageuse : "); Serial.println(myObject["clouds"]["all"]); Serial.print("Temperature: "); Serial.println(myObject["main"]["temp"]); Serial.print("Pression: "); Serial.print(myObject["main"]["pressure"]); Serial.println(" mBar"); Serial.print("Humidite: "); Serial.println(myObject["main"]["humidity"]); Serial.print("Visibilité : "); Serial.println(myObject["visibility"]); Serial.print("Vitesse du vent: "); Serial.println(myObject["wind"]["speed"]); i++; // Serial.print("Pluie : "); // Serial.println(myObject["rain"]["3H"]); } else { Serial.println("WiFi Disconnected"); } lastTime = millis(); } } String httpGETRequest(const char* serverName) { WiFiClient client; HTTPClient http; // Your Domain name with URL path or IP address with path http.begin(client, serverName); // Send HTTP POST request int httpResponseCode = http.GET(); String payload = "{}"; if (httpResponseCode>0) { Serial.println(""); Serial.print("HTTP Response code: "); Serial.println(httpResponseCode); payload = http.getString(); } else { Serial.print("Error code: "); Serial.println(httpResponseCode); } // Free resources http.end(); return payload; } /* * Resultats sur terminal YAT COM57 115200bds . (18:14:35.250) VSC ESP32_HTTP_GET_OpenWeatherMap (18:14:35.250) Version :14-05-2022 (18:14:36.375) Connecting. (18:14:39.250) Connected to WiFi network with IP Address: 192.168.0.103 (18:14:39.250) Timer set to 30sec n (timerDelay variable), it will take 30sec before publishing the first reading. (18:14:39.250) MAC address: 94:C9:8D:F2:3A:8 (18:14:40.250) signal strength (RSSI):-67 (18:15:13.251) (18:15:13.267) HTTP Response code: 200 (18:15:13.267) (18:15:13.267) JSON object = {"coord":{"lon":5.1953,"lat":45.9084},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":23.86,"feels_like":24.1,"temp_min":22.84,"temp_max":23.94,"pressure":1019,"humidity":69,"sea_level":1019,"grnd_level":990},"visibility":10000,"wind":{"speed":3.82,"deg":344,"gust":5.53},"clouds":{"all":98},"dt":1652458512,"sys":{"type":1,"id":6498,"country":"FR","sunrise":1652415004,"sunset":1652468471},"timezone":7200,"id":2994094,"name":"Meximieux","cod":200} (18:15:13.314) Lieu: "Meximieux" (18:15:13.314) mesure # 1 (18:15:13.314) Couverture nuageuse : 98 (18:15:13.314) Temperature: 23.86 (18:15:13.314) Pression: 1019 mBar (18:15:13.314) Humidite: 69 (18:15:13.314) Visibilité : 10000 (18:15:13.314) Vitesse du vent: 3.82 (18:15:46.544) (18:15:46.556) HTTP Response code: 200 (18:15:46.556) Lieu: "Meximieux" (18:15:46.722) mesure # 2 (18:15:46.722) Couverture nuageuse : 98 (18:15:46.722) Temperature: 23.86 (18:15:46.722) Pression: 1019 mBar (18:15:46.722) Humidite: 69 (18:15:46.722) Visibilité : 10000 (18:15:46.722) Vitesse du vent: 3.82 (17:49:51.764) HTTP Response code: 200 (17:49:51.795) Lieu: "Meximieux" (17:49:51.795) mesure # 2 (17:49:51.795) Couverture nuageuse : 98 (17:49:51.795) Temperature: 23.86 (17:49:51.795) Pression: 1019 mBar (17:49:51.795) Humidite: 69 (17:49:51.795) Visibilité : 10000 (17:49:51.795) Vitesse du vent: 3.82 (17:50:21.889) (17:50:21.898) HTTP Response code: 200 (17:50:21.898) Lieu: "Meximieux" (17:50:21.943) mesure # 3 (17:50:21.943) Couverture nuageuse : 98 (17:50:21.943) Temperature: 23.86 (17:50:21.943) Pression: 1019 mBar (17:50:21.943) Humidite: 69 (17:50:21.943) Visibilité : 10000 (17:50:21.943) Vitesse du vent: 3.82 HTTP Response code: 200 Lieu: "Meximieux" mesure # 34 Couverture nuageuse : 98 Temperature: 23.86 Pression: 1019 mBar Humidite: 69 Visibilité : 10000 Vitesse du vent: 3.82 HTTP Response code: 200 Lieu: "Meximieux" mesure # 35 Couverture nuageuse : 98 Temperature: 23.86 Pression: 1019 mBar Humidite: 69 Visibilité : 10000 Vitesse du vent: 3.82 */