#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* server = "api.thingspeak.com";
const unsigned int channelID = 2491239;
const char* apiKey = "IYFGH0KKSF1QM4B6";
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi!");
}
void loop() {
WiFiClient client;
HTTPClient http;
String url = "http://" + String(server) + "/channels/" + String(channelID) + "/feed/last.json?api_key=" + String(apiKey);
http.begin(client, url);
int httpResponseCode = http.GET();
if (httpResponseCode == HTTP_CODE_OK) {
String response = http.getString();
Serial.println("Response from ThingSpeak:");
Serial.println(response);
DynamicJsonDocument jsonDoc(1024);
DeserializationError error = deserializeJson(jsonDoc, response);
if (error) {
Serial.print("Error parsing JSON: ");
Serial.println(error.c_str());
return;
}
int A = jsonDoc["field1"]; // Lecture de la valeur de A
int B = jsonDoc["field2"]; // Lecture de la valeur de B
Serial.print("Valeur de A: ");
Serial.println(A);
Serial.print("Valeur de B: ");
Serial.println(B);
} else {
Serial.print("Error reading data from ThingSpeak. Error code: ");
Serial.println(httpResponseCode);
}
http.end();
delay(2000);
}