-
Notifications
You must be signed in to change notification settings - Fork 0
/
crockpi.cpp
96 lines (81 loc) · 2.16 KB
/
crockpi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <wiringPi.h>
#include <string>
#include <getopt.h>
#include "temperature.cpp"
// All time measured in seconds
const int SECONDSINHOUR = 3600;
const int DELAYTIMEOFF = 600;
const int DELAYTIMEON = 90;
// On the 'warm' setting, the outside of the crockpot is ~31.5 celcius, so give or take 1 degree.
const int MAXTEMPERATURE = 32500;
const int MINTEMPERATURE = 30500;
void getCmdOpts(float& hours, int& gpioPin, bool& sensor, int argc, char** argv);
void keepWarmBasic(int gpioPin);
void keepWarmSensor(int gpioPin);
int main(int argc, char** argv) {
// default values for time, gpio pin, and use of sensor
float hours = 8.0;
int gpioPin = 2;
bool sensor = false;
getCmdOpts(hours, gpioPin, sensor, argc, argv);
wiringPiSetupGpio();
pinMode(gpioPin, OUTPUT);
digitalWrite(gpioPin, HIGH);
// The delay is measured in milliseconds, so multiply by 1000
delay(1000 * hours * SECONDSINHOUR);
digitalWrite(gpioPin, LOW);
while(true) {
if (sensor) {
keepWarmSensor(gpioPin);
}
else {
keepWarmBasic(gpioPin);
}
}
return 0;
}
void getCmdOpts(float& hours, int& gpioPin, bool& sensor, int argc, char** argv) {
// set up to read in command line args
opterr = true;
int choice;
int optionIndex = 0;
option options[] = {
{ "hours", required_argument, nullptr, 'h' },
{ "pin", required_argument, nullptr, 'p' },
{ "sensor", no_argument, nullptr, 's' },
{ nullptr, 0, nullptr, '\0' }
};
while((choice = getopt_long(argc, argv, "h:p:s", options,
&optionIndex)) != -1) {
switch(choice) {
case 'h':
hours = std::atof(optarg);
break;
case 'p':
gpioPin = std::atoi(optarg);
break;
case 's':
sensor = true;
break;
}
}
}
void keepWarmBasic(int gpioPin) {
digitalWrite(gpioPin, LOW);
// Turn the pot off for 10 minutes
delay(1000 * DELAYTIMEOFF);
// Turn it back on for 90 seconds to keep it warm
digitalWrite(gpioPin, HIGH);
delay(1000 * DELAYTIMEON);
}
void keepWarmSensor(int gpioPin) {
int temperature = stoi(getTemperature());
if (temperature > MAXTEMPERATURE) {
digitalWrite(gpioPin, LOW);
}
if (temperature < MINTEMPERATURE) {
digitalWrite(gpioPin, HIGH);
}
// check every 10 seconds
delay(10000);
}