Wednesday 18 December 2019

ChipKit Max32 en Thingspeak Ejemplo

Tenia una cuenta de Ubidots que hice hace años, hoy dia me doy cuenta que no funciona mas el modo free, al menos no encontré la opcion, por tanto migre el ejemplo a Thingspeak, de la cual tambien tenia cuenta vieja, despues de que formo parte de MathWorks me di cuenta que tiene mas posibilidades y al menos en 2020 aun dan cuenta free para evaluacion y posteo de la data


En este ejemplo use un Chipkit Max 32 con un Ethernet shield + un sensor de Temperatura para graficar en ciertas temporadas del año 

Se agregaron nuevos Widgets personalizables, reglas para notificaciones y para comandos entre dispositivos, justo como ubidots, posteo de tweets automáticos etc asi como integracion para mensajes entre dispositivos WS esto si seria un M2M realmente


 Para depurar que se cargaran los datos use el hyper terminal, que por si no saben tiene conexion con sockets para poder validar las respuestas desde el server de thingspeak, a veces tambien uso hypertermenial en este modo para comunicarme con servidores de prueba y /o correo para depurar proyectos que tienen que ver con sockets  comunicaciones etc, es linda herramienta a pesar de su vieja edad, creo que es de los 90

Estos son lso datos en tiempo real el iFrame





El codigo


#include <chipKITEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {  0x00, 0x04, 0xA3, 0x52, 0x93, 0xD3 };
byte ip[] = { 192,168,0,18 };
byte gateway[] = { 192,168,0,1};
byte subnet[] = { 255,255,255,0 };
byte dns1[] = {0,0,0,0};
byte dns2[] = {24,113,32,29};

char * szIPServer = "api.thingspeak.com";    // server to connect to
//char * szIPServer = "192.168.1.68";    // server to connect to

String token="tu KEY";
String idPot="550f26817625421d2d457e5c";
unsigned short portServer = 80; 
Client client(szIPServer, 80);

const int sampleWindow=50;
const int delayer_2=300;
unsigned tStart = 0;
double  value;

void setup() {
  

   // Ethernet.begin();                                   // DHCP is used, default ENCX24J600 (ENC24J60) MAC address 
 //Ethernet.begin(mac);                                // DHCP is used
// Ethernet.begin(mac, ip);                            // Static IP, gateway equal to the IP with the last byte set to 1 => IP[3].IP[2].IP[1].1
//  Ethernet.begin(mac, ip, gateway);                   // default subnet 255.255.255.0
//  Ethernet.begin(mac, ip, gateway, subnet);           // default dns1 is equal to gateway, some router/gateways act as DNS servers
//  Ethernet.begin(mac, ip, gateway, subnet, dns1);     // default dns2 is 0.0.0.0
//  Ethernet.begin(mac, ip, gateway, subnet, dns1, dns2);

  // start the serial library:
  // PLEASE NOTE THE SERIAL MODEM SPEED!
  // the speed is not the typical 9600; this is because
  // the serial monitor can not keep up with the page update
  // and will drop characters, the serial monitor must run faster
  // When you open the serial monitor, go to the bottom right and select 11500 as the speed
  Serial.begin(115200);
   int retval = 0;
 Serial.println("Locating O Wire devices...");
  sensors.begin();
  Serial.println("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println("devices.");
  
  // give the Ethernet shield a second to initialize:
  
  Serial.println("Setting up ethernet...");
  //
  delay(1000);
  Ethernet.begin();
  Serial.println("Trying to connect. Function return ");
  retval = client.connect();
  Serial.println(retval);
  delay(1000);
  client.stop();
  Serial.println("Closing client");
  delay(1000);
  
    

  if (client.connect()) {
    Serial.println("Client connected");
  } 
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }



}


void loop()// main loop, do a reading, and send its value to the save_value fun
{

  int retvalues=50;
  sensors.begin();
  int timed_delay;
  int minutes_upload;
  const int period_upload=1;
  delay(100);
  sensors.requestTemperatures(); 
  float tempC = sensors.getTempCByIndex(0);
  int value = analogRead(A0);
  Serial.println("Sending Temp: ");
  Serial.print(tempC);
  Serial.println("");
  save_value(String(tempC));
 // int value = analogRead(A0);
 // Serial.println("Sending value : ");
 // Serial.print(value);
 // Serial.prindelay(60000);
 // save_value(alue));
 Serial.println("3m pausing ");
  delay(60000);
  
}



 void save_value(String value)
   {
       // if you get a connection, report back via serial:
       int num=0;
       String var = "field1= "+ String(value);
       num = var.length();

       if (client.connect()) {
         Serial.println("in:save_value client connected!");
         client.println("GET /update.json?api_key="+token+"&"+var+" HTTP/1.0");
         
         if (client.available()!=0) {
            
                                 char c = client.read();
                                 Serial.print(c);
                                } Serial.println();
         
         Serial.print("Built MSG = GET api.thingspeak.com/update.json?api_key="+token+"&"+var+"HTTP/1.0 \n"
         );
         
        delay(200);
        client.println();
         
        if (client.available()!=0) {
            
         char c = client.read();
         Serial.print(c);
     } Serial.println();
 }
       else {
         // if you didn't get a connection to the server:
         
         Serial.println("connection failed");

      
     }
       

       if (!client.connected()) {
       Serial.println();
       Serial.println("disconnecting.");
       client.stop();
       client.connect();
       Serial.println("Reconnecting...");
       //       donothing forevermore:
       //for(;;)
       
        //
        Ethernet.PeriodicTasks();
     }
     
     
    
     
     
}