Created
October 14, 2020 12:40
-
-
Save gsampallo/67743d9a61908fa5bab2537220138913 to your computer and use it in GitHub Desktop.
DPRE 2020 - Plataformas IoT - Enviar datos a ThingSpeak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* El siguiente programa publica la temperatura en la plataforma ThingSpeak cada 20 segundos. | |
*/ | |
#include <ESP8266WiFi.h> | |
#include "ThingSpeak.h" | |
const char* ssid = "NOMBRE_RED"; | |
const char* password = "PASSWORD_RED"; | |
WiFiClient client; | |
// numeroCanal es Channel ID que lo encontramos en la pagina de ThingSpeak | |
unsigned long numeroCanal = CHANNEL_ID; | |
/* | |
* apiKey es un valor alfanumerico que lo encontramos en la pagina de | |
* ThingSpeak/Channels/Api Keys | |
*/ | |
const char * apiKey = "API_KEY"; | |
unsigned long ultimaActualizacion = 0; | |
unsigned long frecuenciaActualizacion = 20000; //20 segundos | |
float vref = 3.3; | |
float resolution = vref/1023; | |
void setup() { | |
Serial.begin(115200); | |
WiFi.begin(ssid,password); | |
Serial.print("Conectando"); | |
while(WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(100); | |
} | |
Serial.println("Conectado"); | |
Serial.print("Direccion IP: "); | |
Serial.println(WiFi.localIP()); | |
ThingSpeak.begin(client); | |
} | |
void loop() { | |
/* | |
* ThingSpeak no nos permite una actualización permanente, por lo que es necesario | |
* enviar los datos a la plataforma cada cierto periodo de tiempo. | |
*/ | |
if((millis() - ultimaActualizacion) > frecuenciaActualizacion) { | |
ultimaActualizacion = millis(); | |
//Obtenemos el valor de la temperatura | |
float temperatura = analogRead(A0); | |
temperatura = (temperatura*resolution)*100; | |
Serial.println(temperatura); | |
/* | |
* Enviamos el dato del sensor a ThingSpeak correspondiente a field1 | |
*/ | |
int x = ThingSpeak.writeField(numeroCanal, 1, temperatura, apiKey); | |
if(x == 200){ | |
Serial.println("Actualización correcta"); | |
} | |
else{ | |
Serial.println("Se produjo un problema. HTTP error code " + String(x)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment