tempmon.ino

// This #include statement was automatically added by the Particle IDE.
#include "OneWire/OneWire.h"

// This #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"



// This #include statement was automatically added by the Spark IDE.
// #include "HttpClient/HttpClient.h"
 
#define ONE_WIRE_BUS D2

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
float temperature = 0.0;
double tempHome = 273;
String resultstr , tempstr, addrstr; //Move resultstr -> tempstr once compiled.

//pes
    uint8_t i,h,c;
    DeviceAddress dev,deviceAddress,addr;
    long cntLoop = 0;
    String addrarray[25] = {
        "28:C1:4B:57:03:00:00:95",
        "28:71:48:57:03:00:00:DC",
        "28:FB:4A:57:03:00:00:7A"

        };
    String history[10];
    int historycount=0;
//HttpClient http;
 
//#define VARIABLE_ID "548xxxe2a"
//#define TOKEN "pSZxxx0NK"
int led = D7;
int flashTime=50;


// This routine runs only once upon reset
void setup() {
    // Expose fnRouter() to the cloud
    // Particle.function("fnInfo", fnInfo, STRING);
    
    // Initialize D0 pin as output
    pinMode(led, OUTPUT);
    // expose your char buffer to the Cloud API
    //Spark.variable("result", &resultstr, STRING);
    Particle.variable("temp", tempstr);
    Particle.variable("tempHome", tempHome);
    //# EXAMPLE REQUEST IN TERMINAL
    //# curl "https://api.particle.io/v1/devices/53fxxx287/temp?access_token=f62xxx5c1"
}


char c2h(char c)
    {  return "0123456789ABCDEF"[0x0F & (unsigned char)c];
    }

void loop() {
    //discoverOneWireDevices(); 
    sensors.begin();
    Serial.println("");
    cntLoop = cntLoop + 1;
    Serial.print("Hello spark/particle ... ");
    int devcnt=sensors.getDeviceCount();
    Serial.print(devcnt);
    Serial.print(" dev ... cntLoop=");
    Serial.println(cntLoop);    
    
    Serial.print("Temp:");
    resultstr=String("{\"time\":") +Time.now()+","; //Save unix time as first var.
    for( i = 0; i < devcnt; i++) {
            //sensors.requestTemperaturesByIndex(i);
            //temp = sensors.requestTemperaturesByAddress(dev);
            sensors.getAddress(addr, i);
            temperature=sensors.getTempCByIndex( i );
            addrstr="";
            for( h = 0; h < 8; h++) {
                addrstr=addrstr+c2h(addr[h]>>4);
                addrstr=addrstr+c2h(addr[h]);
                if (h<7) { addrstr=addrstr+':'; }
            }
            Serial.print(temperature);
            Serial.print("[");
            Serial.print(addrstr);
            Serial.print("] : ");
            if (i>0) { resultstr=resultstr+","; }
            //resultstr=resultstr+"\"id"+i+"\":"+addrstr+",";
            c = 0;
            while ((addrarray[c] != addrstr) and (addrarray[c] != "")) {
                c=c+1;
            }
            if (c == 1) { tempHome = temperature; } //Export home temp as simple float.
            addrarray[c]=addrstr; //Save incase it was blank.
            resultstr=resultstr+"\"t"+c+"\":"+temperature;
            
            
    }
    resultstr=resultstr+"}";
    tempstr=resultstr;
    Serial.println("---");
    Serial.println(tempstr);
    // format your data as JSON, don't forget to escape the double quotes
    //sprintf(resultstr, "{\"data1\":%d,\"data2\":%d}", data1, data2); 
    
    Serial.println();
    sensors.requestTemperatures();    
    

    //sprintf(resultstr, "{\"value\":%.4f}",temperature); 

    //Serial.println(response.status); //For debug only
    //Serial.println(response.body);
    digitalWrite(led, HIGH);   // Turn ON the LED
    delay(flashTime);
    digitalWrite(led, LOW);    // Turn OFF the LED
    delay(1000-flashTime);
}

...

particle.io/code/tempmon.ino (last edited 2016-01-04 06:07:50 by PieterSmit)