Hola!
Quería subir los datos de temperatura y humedad de un DHT22 a thingspeak y a ubidots desde el mismo dispositivo, pero no soy capaz de hacerlo.
De forma indepediente si que lo he hecho, pero cuando quiero hacerlo de forma conjunta me sale error en Arduino IDE con el client (ThingSpeak.begin(client)) de thingspeak.
Este es el código que estoy probando basado en el código que uso para subir a ubidots:
Agradecería mucho si alguien me puede ayudar a corregir el error.
Gracias!
Quería subir los datos de temperatura y humedad de un DHT22 a thingspeak y a ubidots desde el mismo dispositivo, pero no soy capaz de hacerlo.
De forma indepediente si que lo he hecho, pero cuando quiero hacerlo de forma conjunta me sale error en Arduino IDE con el client (ThingSpeak.begin(client)) de thingspeak.
Este es el código que estoy probando basado en el código que uso para subir a ubidots:
Código:
/****************************************
* Include Libraries
****************************************/
#include "UbidotsESPMQTT.h"
#include "DHT.h"
#include <ThingSpeak.h> // thingspeak
/****************************************
* Define Constants
****************************************/
#define TOKEN "xxxxxxxxx" // Your Ubidots TOKEN
#define WIFINAME "xxxxx" //Your SSID
#define WIFIPASS "xxxxxxx" // Your Wifi Pass
#define DHTPIN D4 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
unsigned long myChannelNumber = 307971; // thingspeak
const char* myWriteAPIKey = "xxxxxxxxxxxx"; // thingspeak
Ubidots client(TOKEN);
DHT dht(DHTPIN, DHTTYPE);
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
/****************************************
* Main Functions
****************************************/
void setup() {
/// client.wifiConnection(WIFISSID, PASSWORD);
Serial.begin(115200);
Serial.println("DHT test!");
client.setDebug(true); // Pass a true or false bool value to activate debug messages
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
ThingSpeak.begin(client); // thingspeak
dht.begin();
}
unsigned long tiempoUltimaLectura=0;; //Para guardar el tiempo de la última lectura
void loop() {
//---------Lectura del Sensor--------------------------
// Wait a few seconds between measurements.
if(millis()-tiempoUltimaLectura>60000) //Tiempo entre lecturas (en este caso 60 segundos)
{
tiempoUltimaLectura=millis(); //actualizamos el tiempo de la última lectura
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Fallo de lectura desde el sensor DHT!");
return;
}
// put your main code here, to run repeatedly:
if(!client.connected()){
client.reconnect();
}
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
//--------Enviamos las lecturas por el puerto serie-------------
Serial.print("Temperatura:");
Serial.print(t);
Serial.print(" ");
Serial.print("Humedad:");
Serial.println(h);
// Serial.print(" ");
// Serial.print("Sensacion_termica:");
// Serial.println(hic);
//--------Enviamos las lecturas a Ubidots-------------
client.add("temperatura", t);
client.add("humedad", h);
client.ubidotsPublish("prueba1");
client.loop();
//--------Enviamos las lecturas a Thingspeak-------------
ThingSpeak.setField(7,t);
ThingSpeak.setField(8,h);
// ThingSpeak.setField(3,hic);
ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
}
//----Fin de la lectura---------------------------
}
Agradecería mucho si alguien me puede ayudar a corregir el error.
Gracias!