This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

  • 0 voto(s) - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
Subir datos a ubidots y thingspeak desde un dispositivo.
#1
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:
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!
  Responder
#2
Igual hay alguna variable que se llama igual.
  Responder
#3
Si, creo que el error está con client;
Ubidots client(TOKEN);
ThingSpeak.begin(client);

si es eso, no sé modificarlo.
  Responder
#4
Hola Alejayo,

Principalmente debes tomar en cuenta que estas inicializando el mismo cliente para enviar a ambas plataformas, por esta razón eres capaz de enviar datos siguiendo los ejemplo por separado pero no cuando los juntas. 

Si deseas enviar datos a ambas plataformas a el tiempo, debes hacerlo por separado inicializando distintos clientes, para ello te recomiendo construir tu propia libreria ya que ambas librerias estan usando el mismo cliente por lo que no te permitirá enviar datos. 

Espero sea de gran ayuda Sonrisa 

Saludos,
Maria C.
  Responder
#5
Muchas gracias María, para mi eso son palabras mayores.
Voy a buscar algo de información al respecto y lo intento.
Saludos,
Alejandro
  Responder
#6
Después de un varias pruebas, no he sido capaz de modificar correctamente las librerías.
Si alguien me pudiese orientar un poco más se lo agradecería.
  Responder


Posibles temas similares…
Tema Autor Respuestas Vistas Último mensaje
  CONSULTA Datos de Arduino a Base de datos. carama 2 1,181 23-11-2020, 08:34 PM
Último mensaje: asesorplaza1
  como combinar muchos datos con arduino homer32 3 1,633 31-08-2018, 06:55 AM
Último mensaje: homer32
  DUDA Subir Configuración Marlin Arduino Raimon Forns 3 3,955 16-08-2017, 09:38 PM
Último mensaje: Merenat
  Problema, no muestra decimales en thingspeak alejayo 14 3,862 05-07-2017, 09:56 AM
Último mensaje: alejayo
  envio coordenadas gps desde android a arduino jhon_bishop 7 4,297 13-10-2015, 09:00 PM
Último mensaje: Bayaz