-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysousvide.ino
388 lines (334 loc) · 9.76 KB
/
mysousvide.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "WiFiManager.h"
#include <ESP8266HTTPClient.h>
#include <SoftwareSerial.h>
#include "FirebaseESP8266.h"
#include <time.h>
//Firebase connection params, you can set up Firebase according to this guide https://circuitdigest.com/microcontroller-projects/iot-firebase-controlled-led-using-esp8266-nodemcu
//Also, you need to create those 3 fields in the database: isControllingTemp, targetTemp and targetTempError (Firebase_realtime_database_setup.png)
#define FIREBASE_HOST "https://FIREBASEHOST.com/"
#define FIREBASE_AUTH "FIREBASEAUTH"
#define TEMP_SENSOR D4
#define RELAY_PUMP D5
#define RELAY_HEATER D6
#define MAX_FAIL_TEMPERATURE 10
FirebaseData firebaseData;
OneWire barramento(TEMP_SENSOR);
DallasTemperature sensor(&barramento);
bool isControllingTemp;
float targetTemp;
float targetTemp_error;
float currentTemperature;
float lastTemperature;
float startTemperature = -1;
bool heaterStatus = false;
bool pumpStatus = false;
bool hitTargetTemp = false;
bool wasControllingTemp = false;
//Get the current time with a user friendly timestamp (UTC)
String getCurrentTime(){
String timestamp = "";
time_t now = time(nullptr);
struct tm * timeinfo;
time(&now);
timeinfo = localtime(&now);
static char charTime[26];
sprintf(charTime, "%04d-%02d-%02d %02d:%02d:%02d",
1900 + timeinfo->tm_year,
1 + timeinfo->tm_mon,
timeinfo->tm_mday,
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec);
timestamp = String(charTime);
return timestamp;
}
//This function is called when the device can not acquire reliable temperature read. The pump and heater are turned off and the bi-color led start blinking red.
void emergencyShutdown(){
digitalWrite(RELAY_HEATER, LOW);
digitalWrite(RELAY_PUMP, LOW);
digitalWrite(D1, LOW);
while(true){
digitalWrite(D2, HIGH);
delay(1000);
digitalWrite(D2, LOW);
delay(1000);
}
}
//Gets first reading of current time
void setupTime(){
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
Serial.print("\nAguardando horário");
while (!time(nullptr)) {
Serial.print(".");
delay(500);
}
while (getCurrentTime().substring(0,4) == "1970") {
Serial.print(".");
delay(500);
}
Serial.print("OK!");
Serial.println(getCurrentTime());
}
//With callback setup
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
}
//Update current temperature (after getting first reading)
void updateCurrentTemperature(){
float diff = -100;
int count = 0;
while((diff >= 5) | (diff <= -5)){
sensor.requestTemperatures();
currentTemperature = sensor.getTempCByIndex(0);
diff = currentTemperature - lastTemperature;
if(!((diff >= 5) | (diff <= -5))){
break;
}
delay(1000);
count = count + 1;
if(count > MAX_FAIL_TEMPERATURE){
emergencyShutdown();
}
}
lastTemperature = currentTemperature;
}
//Gets reliable temperature (check reading more than one time) and check if there is big difference between readings
void getReliableTemperature(){
int loops = 3;
int i = 1;
float sumOfReadings = 0;
float reading;
Serial.println("Getting reliable temp...");
while(i <= loops){
sensor.requestTemperatures();
reading = sensor.getTempCByIndex(0);
sumOfReadings = sumOfReadings + reading;
i = i + 1;
Serial.println(reading);
delay(1000);
}
float meanReading = sumOfReadings/loops;
lastTemperature = meanReading;
sensor.requestTemperatures();
currentTemperature = sensor.getTempCByIndex(0);
Serial.println(currentTemperature);
float diff = currentTemperature - lastTemperature;
if((diff >= 5) | (diff <= -5)){
getReliableTemperature();
}
}
//Get Firebase data
int getSettingsFromWeb(){
if (Firebase.getFloat(firebaseData, "/targetTemp")) {
if ((firebaseData.dataType() == "float") | (firebaseData.dataType() == "int")) {
targetTemp = firebaseData.floatData();
}else{
return 1;
}
}else{
return 1;
}
if (Firebase.getFloat(firebaseData, "/targetTempError")) {
if (((firebaseData.dataType() == "float") | (firebaseData.dataType() == "int")) & firebaseData.floatData() > 0) {
targetTemp_error = firebaseData.floatData();
}else{
return 1;
}
}else{
return 1;
}
if (Firebase.getBool(firebaseData, "/isControllingTemp")) {
if (firebaseData.dataType() == "boolean") {
isControllingTemp = firebaseData.boolData();
}else{
return 1;
}
}else{
return 1;
}
return 0;
}
//Records times to Firebase when the device starts controlling the temperature
int recordStartTimeIntoWeb(){
if (!Firebase.setString(firebaseData, "/startTime", getCurrentTime())) {
return 1;
}
if (!Firebase.setString(firebaseData, "/stopTime", "")) {
return 1;
}
if (!Firebase.setString(firebaseData, "/targetTemperatureHitTime", "")) {
return 1;
}
return 0;
}
//Records to Firebase the time when the device stopped controlling the temperature
int recordStopTimeIntoWeb(){
if (!Firebase.setString(firebaseData, "/stopTime", getCurrentTime())) {
return 1;
}
return 0;
}
//Records to Firebase the time when the device first hit the target temperature
int recordTargetTemperatureHitIntoWeb(){
if (!Firebase.setString(firebaseData, "/targetTemperatureHitTime", getCurrentTime())) {
return 1;
}
return 0;
}
//Records to Firebase the current status
int recordDataIntoWeb(){
String currDate = getCurrentTime();
if (!Firebase.setFloat(firebaseData, "/currentTemp", currentTemperature)) {
return 1;
}
if (!Firebase.setFloat(firebaseData, "/startTemp", startTemperature)) {
return 1;
}
if (!Firebase.setBool(firebaseData, "/isHeaterOn", heaterStatus)) {
return 1;
}
if (!Firebase.setBool(firebaseData, "/isPumpOn", pumpStatus)) {
return 1;
}
if (!Firebase.setString(firebaseData, "/lastActivityTime", currDate)) {
return 1;
}
return 0;
}
//Turn the bi-color LED to red
void double_led_red(){
digitalWrite(D2, HIGH);
digitalWrite(D1, LOW);
}
//Turn the bi-color LED to green
void double_led_green(){
digitalWrite(D2, LOW);
digitalWrite(D1, HIGH);
}
void setup()
{
//Pins Setup
pinMode(LED_BUILTIN, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(RELAY_PUMP, OUTPUT);
pinMode(RELAY_HEATER, OUTPUT);
double_led_red();
//WiFi Setup (At first will create a hot spot called "mySousVide", you have to connect to it using your pc or smartphone and put wifi credentials). Then the nodemcu will reset and try to connect to the provided wifi, if it fails, the hotspot will be created again.
//To configure the wifi open the browser at 192.168.4.1
//The wifi setting will be recorded to persintent memory, so when you turn the device on again, it will try to connect to the same wifi.
Serial.begin(115200);
WiFiManager wifiManager;
wifiManager.setAPCallback(configModeCallback);
if(!wifiManager.autoConnect("mySousVide")) {
Serial.println("Failed to connect and hit timeout, reseting ESP...");
delay(1000);
ESP.restart();
delay(1000);
}
Serial.println("Wifi Connected!");
//Setup time for the first time
setupTime();
//Setup Firebase connection
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
//Get instructions from Firebase
int settingStatus = 1;
while(settingStatus == 1){
settingStatus = getSettingsFromWeb();
if(settingStatus == 0){
break;
}
delay(2000);
}
Serial.println("Firebase OK!");
//startTemp
getReliableTemperature();
double_led_green();
//Temperature Sensor
sensor.begin();
}
void loop()
{
//Get settings from WEB
int settingStatus = getSettingsFromWeb();
if (settingStatus == 0){
double_led_green();
}else{
double_led_red();
}
Serial.println("Setting STATUS: ");
Serial.println(settingStatus);
Serial.println("\nControllingTemp: ");
Serial.println(isControllingTemp);
Serial.println("\nTargetTemp: ");
Serial.println(targetTemp);
Serial.println("\nerror: ");
Serial.println(targetTemp_error);
if(isControllingTemp){
if(wasControllingTemp == false){
if(recordStartTimeIntoWeb() == 0){
wasControllingTemp = true;
getReliableTemperature();
hitTargetTemp = false;
startTemperature = currentTemperature;
}
}else{
//UpdateTemperature
updateCurrentTemperature();
}
//UpdatePump
digitalWrite(RELAY_PUMP, HIGH);
pumpStatus = true;
//UpdateHeater
if(heaterStatus){
if(currentTemperature>(targetTemp + targetTemp_error)){
digitalWrite(RELAY_HEATER, LOW);
heaterStatus = false;
if(hitTargetTemp == false){
if(recordTargetTemperatureHitIntoWeb() == 0){
hitTargetTemp = true;
}
}
}
}else{
if(currentTemperature<(targetTemp - targetTemp_error)){
digitalWrite(RELAY_HEATER, HIGH);
heaterStatus = true;
}else{
if(hitTargetTemp == false){
if(recordTargetTemperatureHitIntoWeb() == 0){
hitTargetTemp = true;
}
}
}
}
}else{
digitalWrite(RELAY_HEATER, LOW);
digitalWrite(RELAY_PUMP, LOW);
heaterStatus = false;
pumpStatus = false;
if(wasControllingTemp == true){
if(recordStopTimeIntoWeb() == 0){
wasControllingTemp = false;
}
}
}
Serial.println(isControllingTemp);
Serial.println(targetTemp);
Serial.println(targetTemp_error);
Serial.println(currentTemperature);
int settingStatusRecord = recordDataIntoWeb();
if (settingStatusRecord == 0){
double_led_green();
}else{
double_led_red();
}
delay(2000);
}