#include <WiFi.h>
const char* ssid = "Wokwi-GUEST"; // write your wifi name
const char* password = ""; // write your wifi password
const char* host = "api.thingspeak.com";
int value = 1;
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Conectándose a la red: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");}
Serial.println("");
Serial.println("Se ha conectado al WiFi correctamente: ");
Serial.println("Su dirección IP es: ");
Serial.println(WiFi.localIP());
}//end setup
void loop() {
delay(5000);
Serial.print("Conectándose a la página: ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient Client;
const int httpPort = 80;
if (!Client.connect(host, httpPort)) {
Serial.println("Ha fallado la conexión a la página!");
return;
}
// We now create a URI for the request
String url = "/apps/thinghttp/send_request?api_key=XIZFNX04LM1NVC0F"; // paste url of your api
Serial.print("Realizando la petición a la dirección: ");
Serial.println(host + url);
// This will send the request to the server
Client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
delay(1000); // change delay to 2000
// Read all the lines of the reply from server and print them to Serial
while (Client.connected() || Client.available())
{
if (Client.available())
{
String line = Client.readStringUntil('\n');
Serial.println(line);
}
}
Client.stop();
Serial.println("\n[Disconnected]");
}