-
Notifications
You must be signed in to change notification settings - Fork 1
/
excellentea.cpp
233 lines (202 loc) · 5.77 KB
/
excellentea.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
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
224
225
226
227
228
229
230
231
232
#include "Classes/Actuator.h"
#include "Classes/DS18B20.h"
#include "Classes/LiquidCrystal_I2C.h"
#include "Classes/Sensor.h"
#include "Classes/Stepper.h"
#include "Classes/Tea.h"
#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <string>
#include <thread>
std::string readTag(std::string tag, std::string filename) {
std::ifstream readFile(filename.c_str());
std::string line = "";
std::string fileTag = "";
std::string fileValue = "";
int pos = 0;
while (getline(readFile, line)) {
pos = line.find("\t");
if (pos != 0) {
fileTag = line.substr(0, pos);
fileValue = line.substr(pos + 1);
if (tag == fileTag) {
return fileValue;
}
}
}
return "not found";
}
void writeTag(std::string tag, std::string value, std::string filename) {
std::string tempFile = "writeTag.tmp";
std::ifstream readFile(filename.c_str());
std::ofstream writeFile(tempFile.c_str());
std::string line = "";
std::string fileTag = "";
std::string fileValue = "";
int pos = 0;
bool replaced = false;
while (getline(readFile, line)) {
pos = line.find("\t");
if (pos != 0) {
fileTag = line.substr(0, pos);
fileValue = line.substr(pos + 1);
if (tag == fileTag) {
line = fileTag + '\t' + value;
replaced = true;
}
writeFile << line << std::endl;
}
}
writeFile.close();
readFile.close();
if (replaced) {
rename(tempFile.c_str(), filename.c_str());
//allow apache to read and modify contents
system("sudo chown -R www-data: /var/www/html");
} else {
remove(tempFile.c_str());
}
}
int main() {
//Define components***
Sensor water_sensor(0); //PIN 12
DS18B20 temp_sensor(7, "28-0516a06e58ff"); //PIN 7 and bus id
Stepper strainer(24, 23, 22, 21, 64); //coils terminals and steps
Actuator heat_elem(30);
//Define Display connection***
// i2c address
uint8_t i2c = 0x27;
// Control line PINs
uint8_t en = 2;
uint8_t rw = 1;
uint8_t rs = 0;
// Data line PINs
uint8_t d4 = 4;
uint8_t d5 = 5;
uint8_t d6 = 6;
uint8_t d7 = 7;
// Backlight PIN
uint8_t bl = 3;
// LCD display size
uint8_t rows = 2;
uint8_t cols = 16;
LiquidCrystal_I2C lcd("/dev/i2c-1", i2c, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
//Initialise components***
water_sensor.initialise();
temp_sensor.initialise();
strainer.initialise();
heat_elem.initialise();
heat_elem.switchOff();
lcd.begin(cols, rows);
std::cout << "Initialisation complete!!" << std::endl;
//Configurations file
std::string configFile = "/var/www/html/config";
//Turn on LCD
lcd.on();
std::ostringstream strs;
while (1) {
//Initialise config file
writeTag("READY", "NO", configFile);
writeTag("MAKE", "NO", configFile);
writeTag("PROGR", "NO", configFile);
writeTag("DONE", "NO", configFile);
lcd.clear();
//Check presence of water***
while (1) {
lcd.home();
if (water_sensor.readStatus()) {
//Print status on lcd
lcd.print("Not ready ");
//change flag in configuration file
if (readTag("READY", configFile) == "YES") {
writeTag("READY", "NO", configFile);
}
} else {
//Print status on lcd
lcd.print("READY! ");
//change flag in configuration file
if (readTag("READY", configFile) == "NO") {
writeTag("READY", "YES", configFile);
}
if (readTag("MAKE", configFile) == "YES") {
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
//Read tea characteristics
double desired_temp = std::stod(readTag("TEMP", configFile));
double desired_time = std::stod(readTag("TIME", configFile));
Tea myTea(desired_temp, desired_time);
//Turn ON heating element***
heat_elem.switchOn();
lcd.clear();
lcd.print("Activating...");
//Update progress flag
writeTag("PROGR", "YES", configFile);
//Check water temperature***
lcd.clear();
lcd.print("Heating up...");
std::cout<<"Heating up to "<< myTea.getBrewTemperature()<<"degrees."<<std::endl;
while (temp_sensor.readTemp() < myTea.getBrewTemperature()) {
lcd.setCursor(0, 1);
strs << "T= " << std::setprecision(5) << temp_sensor.readTemp() << " C ";
lcd.print(strs.str().c_str());
std::cout<<strs.str().c_str()<<std::endl;
strs.str(std::string());
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
//Turn off heating element***
heat_elem.switchOff();
std::cout<<"Heating element OFF"<<std::endl;
//Lower strainer***
lcd.clear();
lcd.print("Lowering");
lcd.setCursor(0,1);
lcd.print("strainer");
std::cout<<"Strainer DOWN"<<std::endl;
strainer.spin(350, 30, 0);
//Sleep for brewing time***
lcd.clear();
lcd.print("Brewing...");
std::cout<<"Brewing"<<std::endl;
int duration = myTea.getBrewTime() * 60 ;
clock_t begin = clock();
double time_elapsed = 0;
double time_left = 0;
int minutes_left = 0;
int seconds_left = 0;
while ((double(clock()-begin)/ CLOCKS_PER_SEC*1000)<duration) {
time_elapsed = double(clock()-begin)/ CLOCKS_PER_SEC*1000;
time_left = duration - time_elapsed;
minutes_left = time_left/60;
seconds_left = (time_left/60 - (double(minutes_left)))*60;
strs <<minutes_left<<":"<<seconds_left<<" ";
lcd.setCursor(0,1);
lcd.print(strs.str().c_str());
std::cout<<strs.str().c_str()<< "left"<<std::endl;
strs.str(std::string());
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
//Pull-up strainer***
lcd.clear();
lcd.print("Pulling up");
lcd.setCursor(0,1);
lcd.print("strainer");
std::cout<<"Strainer UP"<<std::endl;
strainer.spin(350, 60,1);
//Update on progress***
lcd.clear();
lcd.print("Your tea is");
lcd.setCursor(0, 1);
lcd.print("ready!!!");
std::cout<<"FINISHED"<<std::endl;
writeTag("DONE", "YES", configFile);
std::this_thread::sleep_for(std::chrono::milliseconds(3 * 60 * 1000));
}
}