subir información de los sensores a XIVELY

Este es el código para subir la información de los sensores a la plataforma XIVELY. En este caso hay dos sensores de temperatura y uno de temperatura y humedad.

Los que tengais cuenta en XIVELY creo podréis ver las gráficas de la información que ha estado subiendo toda la noche.

https://xively.com/develop/lrYdWZAmMGptbRToBa70

Creo que cuando mañana consigamos comunicar master-esclavo habremos avanzado bastante porque añadir esclavos al sistema no supondrá excesiva complicación...

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <math.h>
#include "DHT.h"

#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>

#define tipo_DHT DHT11 // DHT 11

int a;
float Sensor_Temperatura_01;
float Sensor_Temperatura_02;
float Sensor_Temperatura_DTH;
float Sensor_Humedad_DTH;
int B=3975;                  //B value of the thermistor
float resistance;
#define PIN_DTH A4 // what pin we're connected to

// Your Xively key to let you upload data
char xivelyKey[] = "sKvPHoXFv8Mr46Jb460ANpWQxEwWxqwWrWzKECN5dd7lCl13";

// Analog pin which we're monitoring (0 and 1 are used by the Ethernet shield)
int sensorPin = A0;

// Define the strings for our datastream IDs
char sensorId1[] = "Sensor_Temperatura01";

// Define the strings for our datastream IDs
char sensorId2[] = "Sensor_Temperatura02";
char sensorId3[] = "Sensor_Temperatura03";
char sensorId4[] = "Sensor_Humedad";


IPAddress ip(192,168,100,85);

// MAC address for your Ethernet shield  
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

XivelyDatastream XS_SensorTemperatura_01[] = {
  XivelyDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
};
XivelyDatastream XS_SensorTemperatura_02[] = {
  XivelyDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
};

XivelyDatastream XS_SensorTH[] = {
  XivelyDatastream(sensorId3, strlen(sensorId3), DATASTREAM_FLOAT),
  XivelyDatastream(sensorId4, strlen(sensorId4), DATASTREAM_FLOAT),
};

// Finally, wrap the datastreams into a feed
XivelyFeed feed_temperatura_01  (1724979296, XS_SensorTemperatura_01, 1 /* number of datastreams */);
XivelyFeed feed_temperatura_02  (1724979296, XS_SensorTemperatura_02, 1 /* number of datastreams */);
XivelyFeed feed_TH  (1724979296, XS_SensorTH, 2 /* number of datastreams */);

EthernetClient client;
XivelyClient xivelyclient(client);


DHT dht(PIN_DTH, tipo_DHT);
    
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("DHT11 test!");
 
  dht.begin();  
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
}

void loop() {
  //lectura sensor de temperatura conectado en A0
  a=analogRead(0);
  resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor;
  Sensor_Temperatura_01=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet ;
  delay(1000);

  //se imprime por pantalla la información
  Serial.print("TEMPERATURA 1: ");
  Serial.println(Sensor_Temperatura_01);
  Serial.print("LECTURA ANTERIOR: ");
  Serial.println(XS_SensorTemperatura_01[0].getFloat());

//------------------------------------------------------------------------------------------------------------------

  //lectura sensor de temperatura conectado en A0
  a=analogRead(2);
  resistance=(float)(1023-a)*10000/a; //get the resistance of the sensor;
  Sensor_Temperatura_02=1/(log(resistance/10000)/B+1/298.15)-273.15;//convert to temperature via datasheet ;
  delay(1000);

  //se imprime por pantalla la información
  Serial.print("TEMPERATURA 2: ");
  Serial.println(Sensor_Temperatura_02);
  Serial.print("LECTURA ANTERIOR: ");
  Serial.println(XS_SensorTemperatura_02[0].getFloat());

//------------------------------------------------------------------------------------------------------------------

  //lectura sensor humedad y temperatura
  float Sensor_HumedadTH = dht.readHumidity();
  float Sensor_TemperaturaTH = dht.readTemperature();

  //se imprime por pantalla la información
  Serial.print("TEMPERATURA actual TH: ");
  Serial.println(Sensor_TemperaturaTH);
  Serial.print("lectura XS_SensorTH[0]");
  Serial.println(XS_SensorTH[0].getFloat());
  Serial.print("HUMEDAD actual TH: ");
  Serial.println(Sensor_HumedadTH);
  Serial.print("lectura XS_SensorTH[1]");
  Serial.println(XS_SensorTH[1].getFloat());


//------------------------------------------------------------------------------------------------------------------

  XS_SensorTemperatura_01[0].setFloat(Sensor_Temperatura_01);
  XS_SensorTemperatura_02[0].setFloat(Sensor_Temperatura_02);
  XS_SensorTH[0].setFloat(Sensor_TemperaturaTH);
  XS_SensorTH[1].setFloat(Sensor_HumedadTH);

  //envío de información a XIVELY
  Serial.println("ENVIO A XIVELY...");
  int ret = xivelyclient.put(feed_temperatura_01, xivelyKey);
  int ret1 = xivelyclient.put(feed_temperatura_02, xivelyKey);
  int ret2 = xivelyclient.put(feed_TH, xivelyKey);

  Serial.println("ENVIO temperatura_1 200=OK: ");
  Serial.println(ret);
  Serial.println("ENVIO temperatura_2 200=OK: ");
  Serial.println(ret1);
  Serial.println("ENVIO temperatura y humedad 200=OK: ");
  Serial.println(ret2);
  Serial.println();
  delay(10000);

}

Tipo de post
Blog
Autor
joan_leo