carlosvolt
Published © LGPL

Turn on light from thindspeak with esp32

In this tutorial, we will show you how to control lights over the Internet using an ESP32 and the ThingSpeak platform.

IntermediateProtip83
Turn on light from thindspeak with esp32

Things used in this project

Hardware components

ESP32
Espressif ESP32
×1

Story

Read more

Code

Source code

C/C++
Source Code
#include <WiFi.h>
#include <ThingSpeak.h>
// Configuración de red WiFi
const char* ssid = "Tu_red_wifi"; // Nombre de la red WiFi
const char* contrasena = "Tu_clave_wifi"; // Contraseña de la red WiFi
// Configuración de ThingSpeak
unsigned long idCanal = 0000000;  // Reemplaza con tu ID de canal
const char* claveAPIlectura = "Read_API_Keys";  // Reemplaza con tu clave de lectura
WiFiClient cliente;
int pinLuz = 12; // Pin donde está conectada la luz o el relé
void setup() {
  Serial.begin(115200);  
  // Configurar el pin como salida
  pinMode(pinLuz, OUTPUT);    
  // Conectar a la red WiFi
  WiFi.begin(ssid, contrasena);
  Serial.print("Conectando a WiFi...");    
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("Conectado a WiFi");    
  // Conectar a ThingSpeak
  ThingSpeak.begin(cliente);
}
void loop() {
  // Leer el valor del canal
  int estadoLuz = ThingSpeak.readIntField(idCanal, 1, claveAPIlectura); // Leer el campo 1    
  if (estadoLuz == 1) {
    digitalWrite(pinLuz, HIGH); // Encender la luz
    Serial.println("Luz encendida");
  } else if (estadoLuz == 0) {
    digitalWrite(pinLuz, LOW);  // Apagar la luz
    Serial.println("Luz apagada");
  } else {
    Serial.println("Error al leer el estado de la luz");
  }    
  delay(15000); // Esperar 15 segundos antes de la próxima lectura
}

Credits

carlosvolt
34 projects • 3 followers
Contact

Comments

Please log in or sign up to comment.