-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrolTemp.cpp
More file actions
169 lines (113 loc) · 2.92 KB
/
controlTemp.cpp
File metadata and controls
169 lines (113 loc) · 2.92 KB
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
/*
* controlTemp.cpp
*
* Created on: Nov 7, 2012
* Author: christoph
*/
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <sstream>
//#include <vector>
#include <ctime>
extern "C" {
#include <rrd.h>
}
#include "tempSensor.h"
#include "RCSwitch.h"
using namespace std;
void writeRrd (float temp1, float temp2, float temp3) {
int rrd_argc=3;
char *rrd_argv[rrd_argc];
stringstream stream;
rrd_argv[0]="update";
rrd_argv[1]="/home/pi/stuff/readTemp/tempGrow.rrd";
stream << "N:" << temp1 << ":" << temp2 << ":" << temp3 << ends;
rrd_argv[2] = new char[stream.str().size()];
strcpy(rrd_argv[2],stream.str().c_str());
int ret = rrd_update(rrd_argc,rrd_argv);
stream.str("");
stream.clear();
delete[] rrd_argv[2];
}
string getTimestamp() {
char date[256];
time_t rawtime;
struct tm *tmp;
time (&rawtime);
tmp = localtime(&rawtime);
strftime(date, sizeof(date),"%Y/%m/%d %X", tmp);
return date;
}
int getHour() {
char hour[3];
time_t rawtime;
struct tm *tmp;
time (&rawtime);
tmp = localtime(&rawtime);
strftime(hour, sizeof(hour),"%H", tmp);
stringstream ss(hour);
int retHour;
ss >> retHour;
return retHour;
}
int main (void) {
ofstream logfile;
logfile.open ("controlTemp.log", ios::app);
int onoff = 0;
float targetTemp = 28.00f;
wiringPiSetup();
RCSwitch heizmatte = RCSwitch();
heizmatte.enableTransmit(1);
float temp1, temp2, temp3;
// vector<tempSensor> tempSensors;
tempSensor sens1("28-000003c31d58"), sens2("28-000004071f35"),
sens3("28-0000040743a0");
// tempSensors.push_back(sens1);
// tempSensors.push_back(sens2);
// tempSensors.push_back(sens3);
logfile << getTimestamp() << " Started control" << endl;
while (1) {
temp1 = sens1.getTemp();
temp2 = sens2.getTemp();
temp3 = sens3.getTemp();
/* for (vector<tempSensor>::iterator it = tempSensors.begin(); it != tempSensors.end(); ++it ) {
cout << it->getTemp() << endl;
}
*/
writeRrd(temp1, temp2, temp3);
if ( ( 06 <= getHour()) && ( getHour() < 21 )) {
heizmatte.switchOn("11111",4);
}
else if ( ( (( 21 <= getHour() ) && ( getHour() <= 24 )) || (( 00 <= getHour() ) && ( getHour() < 06 ))) ) {
heizmatte.switchOff("11111",4);
}
sleep(2);
cout << "Temp: " << temp1 <<" Target: " << targetTemp << endl;
if ( temp1 >= 30 )
digitalWrite(6, 1);
else
digitalWrite(6, 0);
if ( temp1 >= targetTemp ) {
heizmatte.switchOff("11111",5);
cout << "switch off" << endl;
if ( onoff == 1 ) {
logfile << getTimestamp() << " Switched off at temperature: " << temp1 << endl;
onoff = 0;
}
}
else if ( temp1 <= targetTemp - 0.05 ) {
heizmatte.switchOn("11111",5);
cout << "switch on" << endl;
if ( onoff == 0 ) {
logfile << getTimestamp() << " Switched on at temperature: " << temp1 << endl;
onoff = 1;
}
}
sleep(10);
}
logfile << getTimestamp() << " Stopped control" << endl;
logfile.close();
}