#include "OneWire.h"
#include "spark-dallas-temperature.h"
OneWire oneWire(D0);
DallasTemperature dallas(&oneWire);
const String myID = Particle.deviceID();
const int ledPin = D1;
int ledValue = 255;
int friendTempMapped = 0;
int myTempMapped = 0;
float myTempRawPrev = -1;
double temperature = 0.0;
const String eventName = "80085-temp-change";
int tempLowerBound = 20;
int tempHigherBound = 30;
void setup(){
Particle.variable("temperature", &temperature, DOUBLE);
/*Particle.function("led", updateLed);*/
analogWrite( ledPin, ledValue);
dallas.begin();
pinMode(ledPin, OUTPUT);
Particle.subscribe(eventName, eventHandler);
Serial.begin(9600);
}
void loop(){
monitorSensor();
updateLed();
}
void eventHandler(const char *event, const char *data){
String dataString = String(data);
int delIndex = dataString.indexOf(',');
String firstValue = dataString.substring(0, delIndex);
String secondValue = dataString.substring(delIndex+1);
String theID = firstValue;
int theTemp = secondValue.toInt();
if( myID != theID ){
friendTempMapped = theTemp;
}
Serial.println("***RECEIVED DATA***");
Serial.println("theID");
Serial.println(theID);
Serial.println("theTemp");
Serial.println(theTemp);
Serial.println("-------/-----/---5----/--/------");
}
int monitorSensor(){
dallas.requestTemperatures();
float myTempRaw = dallas.getTempCByIndex(0);
temperature = (double)myTempRaw;
if( abs(myTempRaw - myTempRawPrev) < 10 || myTempRawPrev == -1 ){
myTempRawPrev = myTempRaw;
int myTempRawSnapped = constrain(myTempRaw, tempLowerBound, tempHigherBound);
myTempMapped = map( myTempRawSnapped, tempLowerBound, tempHigherBound, 255, 0);
String dataToSend = myID + "," + myTempMapped;
Particle.publish(eventName, dataToSend);
Serial.println("***LOCAL DATA***");
Serial.println("myTempRaw");
Serial.println(myTempRaw);
Serial.println("myTempMapped");
Serial.println(myTempMapped);
Serial.println("tempLowerBound");
Serial.println(tempLowerBound);
Serial.println("tempHigherBound");
Serial.println(tempHigherBound);
Serial.println("-------/-----/---5----/--/------");
}
}
int updateLed(){
ledValue = friendTempMapped;
if( ledValue > 255 || ledValue < 0 ) return -1;
analogWrite(ledPin, ledValue);
return 1;
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .