jueves, 23 de julio de 2020

Adaptacion del data logger IOT a Thingspeak

Como la plataforma Xively /Pachube ha dejado de servir gratuitamente como repositorio de los datos de dispositivos IOT, he decidido migrar el sketch a Thingspeak. Esta nueva plataforma exige como Xively el crear una cuenta y definir en ella los campos a mostrar. Existen muchos tutoriales en la web para crear una cuenta en Thingspeak, asi que no entrare en ese tema. Lo único a destacar en la modificación del sketch es la parte que corresponde a la subida de datos y a la definición de los nombres de los campos de datos a subir, pues en Thingspeak el formato de nombres para la subida es fijo y es del tipo "field(numero)". El esquema de hardware del proyecto es el mismo que ya esta en el blog para Xively/Pachube, y solo cambia el sketch en la parte relacionada con la adaptacion a la subida de datos a Thingspeak. La compilacion es para el IDE arduino 1.0.4 .

El sketch es el siguiente:


// Sistema de control de temperaturas usando modulo ethernet ENC28J60 y sensor DS18B20
// 2011-07-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
//Compilado con Arduino IDE 1.0.4
//PIN CONNECTION FOR ENC28J60 ETHERNET CARD
//CS...8
//SI...11
//SO...12
//SCK..13
//VCC...3.3v
//GND...GND
// Conexion del sensor DS18B20:
// Se ha elegido el modo de alimentación parásita que consiste en conectar VCC (+3,3v) y GND
//juntos y unir DATA a +3,3v con una resistencia de 4K7 Ohm.Este modo permite conectar los
//tres polos a un conector GND-DATA-VCC actuando como desequivocador, ya que DATA nunca cambia
//de posicion central.
// arduino +3.3v-----R 4K7Ohm----DATA------Pin Digital 2 Arduino
// ****************************DS18B20***************************
// Arduino GND------------------VCC-GND

#include <EtherCard.h>
#include <stdio.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(9, 10, 4, 5, 6, 7);

float tempmin = 2;  //Variable donde guardamos la temperatura minima registrada
float tempmax = 8;  //Varibale donde guardamos la temperatura maxima registrada
float teston = 0;  //Variable del testeo de DNS ON
float teston1 = 0; //Variable de presentacion testeo OK
float temp01;      //Varibale donde guardamos la temperatura actual sensor 01
float temp02;      //Varibale donde guardamos la temperatura actual sensor 02
float temp03;      //Varibale donde guardamos la temperatura actual sensor 02

//Timers
unsigned long tAntes = 0;
unsigned long tAntes1 = 0;
unsigned long tAntes2 = 0;
unsigned long tAntes3 = 0;
unsigned long tAntes4 = 120000;// Timer 4 minutos estabilizacion sensores
long time_now = 0;

// change these settings to match your own setup (son de mi cuenta Thingspeak)
#define FEED    "valor de feed"
#define APIKEY  "valor de apikey"

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

// ethernet interface mac address, must be unique on the LAN
#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,1,32 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
// mask address
static byte mymask[] = { 255,255,255,0 };
// dnsip address
static byte dnsip[] = { 212,166,210,81 };
#endif

char website[] PROGMEM = "api.thingspeak.com";

#define BUFFER_SIZE 500 //antes 400

byte Ethernet::buffer[BUFFER_SIZE];
BufferFiller bfill;

Stash stash;

void setup () {

  lcd.begin(16, 2);              // start the library
  lcd.setCursor(0,0);
  lcd.print("Version 1.09"); // print a simple message
  delay(2000);
  lcd.clear();
  lcd.print("Starting...."); // print a simple message
  lcd.setCursor(0,1);
  lcd.print("Max/Min delay"); //Warning about 4 min delay in Max/Min
  delay(2000);

   // Start up the library
  sensors.begin();

  Serial.begin(38400);
  Serial.println("Trying to get an IP...");

  Serial.print("MAC: ");
    for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
    }
   Serial.println("\n[webClient]");
 
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Ethernet Start"); // print a simple message
   delay(2000);
 
   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
   {
    Serial.println( "Failed to access Ethernet controller");
    lcd.begin(16, 2);
    lcd.setCursor(0,0);
    lcd.print("Ethernet Start");
    lcd.setCursor(0,1);
    lcd.print("Ethernet Error");
 
   } else  {
    Serial.println( "Ethernet controller OK");
    lcd.begin(16, 2);
    lcd.setCursor(0,0);
    lcd.print("Ethernet Start");
    lcd.setCursor(0,1);
    lcd.print("Ethernet OK");
 
   }
    delay(2000);
 
  #if STATIC
  Serial.println( "Getting static IP.");
     if (!ether.staticSetup(myip, gwip)){
    Serial.println( "could not get a static IP");
     }
#else

  Serial.println("Setting up DHCP");
    if (!ether.dhcpSetup()){
    Serial.println( "DHCP failed");
     blinkLed();     // blink forever to indicate a problem
  }
#endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);
  ether.printIp("DNS: ", ether.dnsip);
  ether.printIp("SRV: ", ether.hisip);

  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Wait for DNS"); // print a simple message
 
  if (!ether.dnsLookup(website)) {
    Serial.println("DNS failed");
    //lcd.begin(16, 2);
    lcd.setCursor(0,1);
    lcd.print("DNS Error");
    //teston = 0;
   } else  {
    Serial.println("DNS OK");
    //lcd.begin(16, 2);
    lcd.setCursor(0,1);
    lcd.print("DNS OK");
   }
    delay(2000);
 
 
  lcd.begin(16, 2);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("1 minute to"); //Inicio loop de publicacion en Thingspeak
  lcd.setCursor(0,1);
  lcd.print("Start Publish.."); //Inicio loop de publicacion en Thingspeak
  delay(2000);
}

void loop () {

  ether.packetLoop(ether.packetReceive());

  long time_now = millis();

  if ((long)(time_now - tAntes) > 30000) // regulador de tiempo medida datos y presentacion lcd
  {
    tAntes = time_now;
 
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus

  Serial.print(" Requesting temperatures...\n");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
 
 float temp01 = sensors.getTempCByIndex(0);// Why "byIndex"?
    // You can have more than one IC on the same bus.
    // 0 refers to the first IC on the wire
  float temp02 = sensors.getTempCByIndex(1);
  float temp03 = sensors.getTempCByIndex(2);



  teston1 = teston/12;// Visualizacion tiempo ON en LCD limite 24 horas

  // Comparador temperaturas para saber Max y Min 
  if (temp01<tempmin)
      tempmin=temp01;
  if (temp01>tempmax)
      tempmax=temp01;
   
  //Timer para permitir estabilizarse la temperatura en el lugar donde colocar la sonda, para evitar
  // falsos maximos y minimos(notese que solo se ejecuta una vez)
  if ((long)(tAntes4 - time_now) > 30000)
  {
  tempmin=temp01;
  tempmax=tempmin;
  }


   //Presentador datos terminal serie

  Serial.print("Sensor 1: ");
  Serial.print(temp01);
  Serial.print("Min Sensor1: ");
  Serial.print(tempmin);
  Serial.print("Max Sensor1: ");
  Serial.print(tempmax);
  Serial.print("Sensor 2: ");
  Serial.print(temp02);
  Serial.print("Sensor 3: ");
  Serial.print(temp03);
  Serial.print("Test ON: ");
  Serial.print(teston1);
  Serial.print("\n");

  // FIN SERIE 
 
  //Escribe datos temperatura en el LCD

 if ((long)(time_now - tAntes2) > 10000) // regulador de tiempo presentacion lcd
 {
 tAntes2 = time_now;

 lcd.begin(16, 2);

 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("T.Sens2");
 lcd.setCursor(10,0);
 lcd.print(temp02);
 lcd.setCursor(0,1);
 lcd.print("T.Sens3");
 lcd.setCursor(10,1);
 lcd.print(temp03);
 delay(4000);
 lcd.clear();
 lcd.setCursor(0,0);
 lcd.print("T.Ac.");
 lcd.setCursor(5,0);
 lcd.print(temp01);
 lcd.setCursor(12,0);
 lcd.print(teston1);
 lcd.setCursor(0,1);
 lcd.print("Min");
 lcd.setCursor(3,1);
 lcd.print(tempmin); 
 lcd.setCursor(8,1);
 lcd.print("Max");
 lcd.setCursor(11,1);
 lcd.print(tempmax);
 delay(4000);
 }


  // Reseteador temparaturas cada 24 horas (228 paquetes de 5 minutos)
  if (teston > 288){
      tempmax=temp01; 
      tempmin=temp01;
      teston = 0;
    }
 
   //Envio de datos a Thingspeak
 
   if ((long)(time_now - tAntes1) > 30000) // regulador de tiempo envio datos
   {
    tAntes1 = time_now;
   
    // generate two fake values as payload - by using a separate stash,
    // we can determine the size of the generated message ahead of time
    // field1=(Field 1 Data)&field2=(Field 2 Data)&field3=(Field 3 Data)&field4=(Field 4 //Data)&field5=(Field 5 Data)&field6=(Field 6 Data)&field7=(Field 7 Data)&field8=(Field 8 //Data)&lat=(Latitude in Decimal Degrees)&long=(Longitude in Decimal Degrees)
//&elevation=(Elevation in meters)&status=(140 Character Message)
    byte sd = stash.create();
         
    stash.print("field1=");
    stash.print(temp01);
 
    stash.print("&field2=");
    stash.print(temp02);
 
    stash.print("&field3=");
    stash.print(temp03);
 
    stash.print("&field4=");
    stash.print(tempmax);
   
    stash.print("&field5=");
    stash.print(tempmin);
 
    stash.print("&field6=");
    stash.print(teston1);
         
    stash.save();
 
     
   // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("POST /update HTTP/1.0" "\r\n"
      "Host: $F" "\r\n"
      "Connection: close" "\r\n"
      "X-THINGSPEAKAPIKEY: $F" "\r\n"
      "Content-Type: application/x-www-form-urlencoded" "\r\n"
      "Content-Length: $D" "\r\n"
      "\r\n"
      "$H"),
    website, PSTR(APIKEY), stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();

    }


// Testeador conexion etherner usando dnslookup
if ((long)(time_now - tAntes3) > 300000) // regulador de tiempo testeo 5 min.
  {
    tAntes3 = time_now;
   
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Wait for DNS");
 
  if (!ether.dnsLookup(website)) {
    Serial.println("DNS failed");
    lcd.setCursor(0,1);
    lcd.print("Error");
    teston = 0;
   } else  {
    lcd.setCursor(0,1);
    lcd.print("OK");
    teston++;//Indicador creciente testeo positivo
   }
   }
  }
}

No hay comentarios:

Publicar un comentario