Skip to content

Instantly share code, notes, and snippets.

@Bostwickenator
Created November 5, 2015 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bostwickenator/146f41fced58d4fdb5ff to your computer and use it in GitHub Desktop.
Save Bostwickenator/146f41fced58d4fdb5ff to your computer and use it in GitHub Desktop.
// This #include statement was automatically added by the Particle IDE.
#include "OneWire/OneWire.h"
// Thihttps://build.particle.io/build/560516a01ee97f78150016ef#s #include statement was automatically added by the Particle IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"
int heater = D0;
int led2 = D7;
int sensors = -1;
double temp = -99;
double target = 0;
DallasTemperature dallas(new OneWire(D1));
void setup()
{
int targetRead = 0;
targetRead = EEPROM.read(1);
target = targetRead;
Spark.function("setTarget",setTargetTemp);
Spark.function("readEEPROM",readEEPROM);
Spark.variable("temp", &temp, DOUBLE);
Spark.variable("target", &target, DOUBLE);
Spark.variable("sensors", &sensors, INT);
dallas.begin();
// Lets make sure we aren't heating
pinMode(led2, OUTPUT);
heaterOff("");
RGB.control(true);
RGB.color(255, 255, 255);
RGB.brightness(0);
}
int heaterOn(String command) {
pinMode(heater, OUTPUT);
digitalWrite(heater, HIGH);
digitalWrite(led2,HIGH);
}
int heaterOff(String command) {
pinMode(heater, INPUT_PULLDOWN);
digitalWrite(led2,LOW);
}
unsigned long lastPublish = 0;
void loop()
{
unsigned long now = millis();
sensors = dallas.getDeviceCount();
double newTemp = -127;
for(int i =0; i<10 && newTemp == -127; i++){
dallas.requestTemperatures();
newTemp = dallas.getTempCByIndex( 0 );
}
temp = newTemp;
if(temp < target){
heaterOn("");
} else if(temp > target + 0.2) {
heaterOff("");
}
if(now - lastPublish > 60 * 1000){
Spark.publish("temperature", String(temp));
lastPublish = now;
}
delay(5000);
}
int setTargetTemp(String command) {
target = command.toFloat();
uint8_t targetWrite = command.toInt();
EEPROM.update(1, targetWrite);
return 1;
}
int readEEPROM(String address){
return EEPROM.read(address.toInt());
}
// Randomly chosen bytes to store in EEPROM once a valid state is set
uint8_t validationBytes[] = {251,21,87,31,156};
bool validateEepromState(){
size_t endOfMemory = EEPROM.length();
for (uint32_t i=0; i < arraySize(validationBytes); i++ ){
if(validationBytes[i] != EEPROM.read(endOfMemory - i))
{
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment