#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
const char* ssid = "Galaxy A14 addd";
const char* password = "arij1234";
const String url = "http://api.thingspeak.com/channels/2306661/status.json?api_key=8HG0TWSQDVY1197R";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
Serial.print("Sending GET request to: ");
Serial.println(url);
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
Serial.printf("Server responded with code: %d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
Serial.println("Response payload:");
Serial.println(payload);
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, payload);
if (error) {
Serial.print("Deserialize JSON failed: ");
Serial.println(error.c_str());
} else {
float temperature = doc["channel"]["field1"];
float humidity = doc["channel"]["field2"];
long channelId = doc["channel"]["id"];
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
Serial.print("Channel ID: ");
Serial.println(channelId);
}
}
} else {
Serial.printf("Error sending GET request. HTTP error code: %d\n", httpCode);
}
http.end();
} else {
Serial.println("WiFi not connected");
}
delay(2000);
}