#include <WiFi.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String payload;
const char* server = "http://api.thingspeak.com/update?";
String myApiKey = "IYFGH0KKSF1QM4B6";
int A = 18;
int B = 33;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Starting...");
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" Connection done");
}
void loop() {
Serial.print("Valeurs envoyées: ");
Serial.print(", A= ");
Serial.print(A);
Serial.print(", B= ");
Serial.println(B);
WiFiClient client;
HTTPClient http;
if (http.begin(client, server)) {
http.addHeader("Content-Type", "application/json");
String httpResponseData = "{\"api_key\":\"" + myApiKey + "\",\"field1\":\"" + String(A) + "\",\"field2\":\"" + String(B) + "\"}";
Serial.printf("Le fichier est: %s\n", httpResponseData.c_str());
int httpResponseCode = http.POST(httpResponseData);
if (httpResponseCode > 0) {
Serial.printf("[HTTP] POST... code: %d\n", httpResponseCode);
if (httpResponseCode == HTTP_CODE_OK || httpResponseCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
} else {
Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpResponseCode).c_str());
}
} else {
Serial.printf("[HTTP] Unable to connect\n");
}
http.end();
} else {
Serial.println("[HTTP] Unable to begin connection to server");
}
delay(2000);
}