-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir_temp_remote.ino
223 lines (206 loc) · 4.8 KB
/
ir_temp_remote.ino
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//DHT LIB
#include "DHT.h"
#define DHTPIN 10
#define DHTTYPE DHT22 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
//IR Commands
#include <IRremote.h>
IRsend irsend;
//AVR Watchdog sleeper
#include <Adafruit_SleepyDog.h>
//Digital pins as serial pins
#include <SoftwareSerial.h>
int RXPIN = 16;
int TXPIN = 15;
SoftwareSerial mySerial(RXPIN, TXPIN); //RX, TX
//Adjustment Variables
float daytemp = 66.00;
float nighttemp = 62.00;
int sleepmins = 4;
//Global Variable Initilization
float controltemp = daytemp;
boolean heaterstatus = false;
boolean btsignal = false;
String input = "false";
float heaterset = controltemp;
void setup() {
Serial.begin(9600);
Serial.setTimeout(10);
mySerial.begin(9600);
dht.begin();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
//Need to calibrate heaterset intially
heaterset = 80;
IRCommands("power");
heaterstatus = true;
while (heaterset > 59){
IRCommands("tempdown");
}
changetemp();
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
Serial.println("beginning");
delay(1000);
//Take temp measurements
float h = dht.readHumidity();
float f = dht.readTemperature(true);
float hif = dht.computeHeatIndex(f, h);
Serial.println(h);
Serial.println(f);
Serial.print(hif);
delay(500);
//Check for commands over serial port from bluetooth comm.
char header;
while(mySerial.available()) {
header = mySerial.read();// read the incoming data as string
if (header == 'x'){
btexecute();
}
}
//Decide if heater needs to be switched off if it is too hot
if (hif > controltemp) {
if (heaterstatus == true){
IRCommands("power");
heaterstatus = false;
}
}
//Decide if heater needs to be switched on if it is too cold
if (hif < controltemp) {
if (heaterstatus == false){
IRCommands("power");
heaterstatus = true;
}
}
delay(100);
//sleepytime();
}
void sleepytime(){
int loops = (sleepmins*60)/9;
loops = 2;
digitalWrite(LED_BUILTIN, LOW); // Show we're asleep
for (int s = 0; s < loops; s++){
int sleepMS = Watchdog.sleep();
#if defined(USBCON) && !defined(USE_TINYUSB)
USBDevice.attach();
#endif
}
digitalWrite(LED_BUILTIN, HIGH); // Show we're awake again
}
void btexecute(){
//Time to read the command string!
int btnum;
char commandchar;
String btnumstr = "";
while(mySerial.available()) {
char commandchar = mySerial.read();
delay(50);
//btnum value assign
char num1 = mySerial.read();
char num2 = mySerial.read();
btnumstr.concat(num1);
btnumstr.concat(num2);
btnum = btnumstr.toInt();
//Now executes received command
if (commandchar == 't'){
if (btnum > 12){
mySerial.println("invalid");
}
mySerial.println(btnum);
for (btnum; btnum > 0; btnum--){
IRCommands("timer");
}
}
if (commandchar == 'd'){
if (btnum < 59 || btnum > 80){
mySerial.println("invalid");
}
mySerial.println(btnum);
controltemp = btnum;
changetemp();
}
if (commandchar == 'n'){
mySerial.println("pm temp");
controltemp = nighttemp;
changetemp();
}
if (commandchar == 'm'){
mySerial.println("am temp");
controltemp = daytemp;
changetemp();
}
if (commandchar == 'p'){
IRCommands("power");
IRCommands("high");
int highmins = 10;
mySerial.println("preheating");
delay(60000*highmins);
IRCommands("low");
}
if (commandchar == 'o'){
IRCommands("power");
heaterstatus = true;
mySerial.println("on");
}
if (commandchar == 'f'){
IRCommands("power");
heaterstatus = false;
mySerial.println("off");
}
}
}
void changetemp(){
if(heaterstatus == false){
IRCommands("power");
heaterstatus = true;
}
while(heaterset != controltemp){
if (heaterset < controltemp){
IRCommands("tempup");
}
if (heaterset > controltemp){
IRCommands("tempdown");
}
}
}
void IRCommands(String input){
if (input == "power"){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x806F9867,32);
delay(40);
}
}
if (input == "tempup"){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x806F48B7,32);
delay(40);
}
heaterset++;
}
if (input == "tempdown"){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x806F6897,32);
delay(40);
}
heaterset--;
}
if (input == "low"){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x806F28D7,32);
delay(40);
}
}
if (input == "high"){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x806F08F7,32);
delay(40);
}
}
if (input == "timer"){
for (int i = 0; i < 3; i++) {
irsend.sendNEC(0x806F10EF,32);
delay(40);
}
}
}