Created
August 22, 2017 22:47
-
-
Save brynmathias/7512db617914716159905e0653aa8745 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Meta-defines | |
#pragma SPARK_NO_PREPROCESSOR | |
// Includes - particle | |
#include "spark-dallas-temperature/spark-dallas-temperature.h" | |
#include "OneWire/OneWire.h" | |
// Includes - Custom | |
#include "RingBuffer.h" | |
// Function declarations | |
void update18B20Temp(DeviceAddress deviceAddress, double &tempC); | |
int remoteControl(int pinID); | |
int setHighTemp_API(String args); | |
int setLowTemp_API(String args); | |
int setUpdateFreq_API(String args); | |
int setHeater_API(String args); | |
int setAuto_API(String args); | |
// Config - thermometer | |
#define ONE_WIRE_BUS D0 | |
#define TEMPERATURE_PRECISION 12 | |
DeviceAddress thermometer = { 0x28, 0xEE, 0x32, 0x9D, 0x12, 0x16, 0x01, 0x42 }; | |
// Config - remote control | |
#define REMOTE_MASTER D1 | |
#define REMOTE_BUTTON5_ON D2 | |
#define REMOTE_BUTTON5_OFF D3 | |
// Initialisation - thermometer | |
OneWire oneWire(ONE_WIRE_BUS); | |
DallasTemperature sensors(&oneWire); | |
// Initialisation - temperature control | |
double tempC = -1; | |
double lowTemp = 18; | |
double highTemp = 20; | |
unsigned long updateFreq = 60000; | |
bool heaterOn = false; | |
bool autoOn = true; | |
// Initialisation - time | |
unsigned long lastAction = 0; | |
void setup() | |
{ | |
// Basic IO setup | |
digitalWrite(REMOTE_MASTER, LOW); | |
digitalWrite(REMOTE_BUTTON5_ON, LOW); | |
digitalWrite(REMOTE_BUTTON5_OFF, LOW); | |
pinMode(REMOTE_MASTER, OUTPUT); | |
pinMode(REMOTE_BUTTON5_ON, OUTPUT); | |
pinMode(REMOTE_BUTTON5_OFF, OUTPUT); | |
// Turn off the heater in case it was on | |
remoteControl(REMOTE_BUTTON5_OFF); | |
// DS18B20 setup | |
sensors.begin(); | |
sensors.setResolution(thermometer, TEMPERATURE_PRECISION); | |
// API accessible variables | |
Particle.variable("temperature", &tempC, DOUBLE); | |
Particle.variable("highTemp", &highTemp, DOUBLE); | |
Particle.variable("lowTemp", &lowTemp, DOUBLE); | |
Particle.variable("freq", &updateFreq, INT); | |
Particle.variable("heater", &heaterOn, BOOLEAN); | |
Particle.variable("auto", &autoOn, BOOLEAN); | |
// API accessible functions | |
Particle.function("setHighTemp", setHighTemp_API); | |
Particle.function("setLowTemp", setLowTemp_API); | |
Particle.function("setFreq", setUpdateFreq_API); | |
Particle.function("setHeater", setHeater_API); | |
Particle.function("setAuto", setAuto_API); | |
//Serial.begin(9600); // open serial over USB | |
// On Windows it will be necessary to implement the following line: | |
// Make sure your Serial Terminal app is closed before powering your Core | |
// Now open your Serial Terminal, and hit any key to continue! | |
//while(!Serial.available()) SPARK_WLAN_Loop(); | |
} | |
void loop() | |
{ | |
// To let the user know that an update is taking place the RGB LED is set to WHITE during the update | |
RGB.control(true); | |
RGB.color(125, 255, 125); | |
// DS18B20 | |
RGB.color(0, 200, 125); | |
sensors.requestTemperatures(); | |
update18B20Temp(thermometer, tempC); | |
unsigned long now = millis(); | |
if (now-lastAction > updateFreq) { | |
lastAction = now; | |
if (autoOn) { | |
if (tempC < lowTemp) { | |
remoteControl(REMOTE_BUTTON5_ON); | |
heaterOn = true; | |
} else if (tempC > highTemp) { | |
remoteControl(REMOTE_BUTTON5_OFF); | |
heaterOn = false; | |
} | |
String tempStr(tempC, 5); | |
Particle.publish("temperature", tempStr, 60, PRIVATE); | |
if (heaterOn) { | |
Particle.publish("heater", "on", 60, PRIVATE); | |
} else { | |
Particle.publish("heater", "off", 60, PRIVATE); | |
} | |
} | |
} | |
//Serial.print("Measured temperature:"); | |
//Serial.print(tempC); | |
//Serial.print("C\n\r");*/ | |
// Release control of the RGB LED | |
RGB.control(false); | |
} | |
// Function to read temperature from a DS18B20 | |
void update18B20Temp(DeviceAddress deviceAddress, double &tempC) | |
{ | |
tempC = sensors.getTempC(deviceAddress); | |
} | |
// Function to activate the wireless remote and switch a plug socket | |
int remoteControl(int pinID) | |
{ | |
digitalWrite(REMOTE_MASTER, HIGH); | |
digitalWrite(pinID, HIGH); | |
delay(50); | |
digitalWrite(pinID, LOW); | |
digitalWrite(REMOTE_MASTER, LOW); | |
return 1; | |
} | |
// Function to set the max temperature | |
int setHighTemp_API(String args) | |
{ | |
highTemp = args.toFloat(); | |
return 1; | |
} | |
// Function to set the minimum temperature | |
int setLowTemp_API(String args) | |
{ | |
lowTemp = args.toFloat(); | |
return 1; | |
} | |
// Function to set the minimum temperature | |
int setUpdateFreq_API(String args) | |
{ | |
updateFreq = args.toInt(); | |
return 1; | |
} | |
// Function to turn the heater on via the web API | |
int setHeater_API(String args) | |
{ | |
if(strcmp(args,"on")==0) { | |
remoteControl(REMOTE_BUTTON5_ON); | |
heaterOn = true; | |
autoOn = false; | |
return 1; | |
} else if(strcmp(args,"off")==0) { | |
remoteControl(REMOTE_BUTTON5_OFF); | |
heaterOn = false; | |
autoOn = false; | |
return 0; | |
} | |
return -1; | |
} | |
// Function to turn the heater on via the web API | |
int setAuto_API(String args) | |
{ | |
if(strcmp(args,"on")==0) { | |
autoOn = true; | |
return 1; | |
} else if(strcmp(args,"off")==0) { | |
autoOn = false; | |
return 0; | |
} | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment