Skip to content

Instantly share code, notes, and snippets.

@blackcj

blackcj/Cron Secret

Created May 11, 2015 00:26
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 blackcj/5f92cefdc4da3beba29e to your computer and use it in GitHub Desktop.
Save blackcj/5f92cefdc4da3beba29e to your computer and use it in GitHub Desktop.
Control a window AC unit with a Spark Core.
<?php
$ch = curl_init("https://api.spark.io/v1/devices/DEVICE_ID/result?access_token=DEVICE_TOKEN");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$return = json_decode($output, true);
// Add error handing here {"error": "Timed out."}
$data = json_decode($return["result"], true);
$con=mysqli_connect("HOST","USER","PASS","DB_NAME");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else {
mysqli_query($con,"INSERT INTO spark_temp (humidity, temperature, ac_mode, day_mode)
VALUES ('".$data["data1"]."', '".$data["data2"]."', '".$data["data3"]."', '".$data["data4"]."')");
}
mysqli_close($con);
?>
*/10 * * * * /usr/bin/php /home/chris/cron/check_temp.php
#include "IRremote.h"
#include "HTU21D.h"
IRsend irsend(D3); // hardwired to pin 3; use a transistor to drive the IR LED for maximal range
unsigned int rawCodes[] = {8300, 4200, 450, 1650, 450, 1650, 450, 1650, 450, 600, 450, 1650, 450, 600, 450, 1650, 450, 1650, 450, 4250, 450, 1650, 450, 1650, 450, 600, 450, 1650, 450, 600, 450, 600, 450, 650, 450, 600, 500};
int toggleAc(String args);
HTU21D myHumidity;
char resultstr[128];
bool acRunning = false;
int acValue = 0;
int dayMode = 0;
int override = 0;
float lastTemp = 0;
float maxTemp = 76;
float minTemp = 72.5;
void setup()
{
myHumidity.begin();
Time.zone(-5);
// expose your char buffer to the Cloud API
Spark.variable("result", &resultstr, STRING);
Spark.function("toggleAc", toggleAc);
}
void setHome()
{
dayMode = 0;
maxTemp = 77.5;
minTemp = 74;
}
void setAway()
{
dayMode = 1;
maxTemp = 82;
minTemp = 75;
}
void setNight()
{
dayMode = 2;
maxTemp = 75.5;
minTemp = 73;
}
void loop()
{
if(Time.weekday() < 7 && Time.weekday() > 1) {
if(Time.hour() >= 16 && Time.hour() < 22) {
setHome();
} else if (Time.hour() >=22 || Time.hour() < 8) {
setNight();
} else {
setAway();
}
} else {
if (Time.hour() >=22 || Time.hour() < 8) {
setNight();
} else {
setAway();
}
}
float humd = myHumidity.readHumidity();
float temp = myHumidity.readTemperature() * 9 / 5 + 32;
if(!acRunning && temp > maxTemp) {
toggleAc("normal");
lastTemp = temp;
}
if(acRunning && temp < minTemp) {
toggleAc("normal");
lastTemp = temp;
}
sprintf(resultstr, "{\"data1\":%f,\"data2\":%f,\"data3\":%i,\"data4\":%d}", humd, temp, acValue, dayMode);
delay(30000); // Delay 30 seconds
}
int toggleAc(String args)
{
if(args == "normal") {
acRunning = !acRunning;
if(acRunning) {
acValue = 1;
} else {
acValue = 0;
}
}
irsend.sendRaw(rawCodes, 37, 38);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment