-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP32_Brew_Controller.ino
149 lines (127 loc) · 3.6 KB
/
ESP32_Brew_Controller.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
#include <ArduinoJson.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <Update.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
///////////////////////////
//// Connections ////
///////////////////////////
const char *host = "BrewController";
const char *ssid = "******";
const char *password = "********";
/////////////////////////
//// GPIO Pins ////
/////////////////////////
#define BOIL_PRIMARY 16
#define BOIL_SECONDARY 17
#define MASH_PRIMARY 18
#define PUMP 4
//////////////////////////////////////////
//// Relay High/Low Definitions ////
//////////////////////////////////////////
#define RELAY_ON HIGH
#define RELAY_OFF LOW
/////////////////////////
//// Variables ////
/////////////////////////
int dualTemp = 4; // the degrees below mash temp when both elements are on
int targetTemp = 152;
float currentTemp;
int mode = 0; // 0 - off, 1 - mash, 2 - boil
//////////////////////////
//// Web Server ////
//////////////////////////
AsyncWebServer server(80);
bool shouldReboot = false;
void setup()
{
Serial.begin(115200);
Serial.print("Booting...");
Serial.println();
// setup the GPIOs as input/output
pinMode(BOIL_PRIMARY, OUTPUT);
pinMode(BOIL_SECONDARY, OUTPUT);
pinMode(MASH_PRIMARY, OUTPUT);
pinMode(PUMP, OUTPUT);
setup_wifi();
if (WiFi.waitForConnectResult() == WL_CONNECTED)
{
server.on("/", handleRoot);
server.on("/data", HTTP_GET, handleData);
server.on("/firmware", HTTP_GET, handleFirmware);
server.onNotFound(handleNotFound);
server.on("/update", HTTP_POST, [](AsyncWebServerRequest *request) {
shouldReboot = !Update.hasError();
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", shouldReboot?"OK":"FAIL");
response->addHeader("Connection", "close");
request->send(response); }, [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
if(!index){
Serial.printf("Update Start: %s\n", filename.c_str());
uint32_t maxSketchSpace = 0x1E0000;
if(!Update.begin(maxSketchSpace)){
Update.printError(Serial);
}
// Update.runAsync(true);
}
if(!Update.hasError()){
if(Update.write(data, len) != len){
Update.printError(Serial);
}
}
if(final){
if(Update.end(true)){
Serial.printf("Update Success: %uB\n", index+len);
} else {
Update.printError(Serial);
}
} });
server.begin(); // Start the server
Serial.println("");
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("WiFi Failed");
}
Serial.println("Ready");
}
void loop()
{
if (shouldReboot)
{
Serial.println("Rebooting...");
delay(100);
ESP.restart();
}
// ****************GET TEMPERATURE READING HERE***********************
switch (mode) {
case 0:
break:
case 1:
control_mash();
break;
case 2:
control_boil();
break;
}
}
void setup_wifi()
{
WiFi.mode(WIFI_AP_STA);
WiFi.softAP(host);
WiFi.begin(ssid, password);
}
control_mash(){
if ((targetTemp - currentTemp > dualTemp)) {
digitalWrite(BOIL_SECONDARY, RELAY_ON); // turn secondary element on if temp is too low
} else {
digitalWrite(BOIL_SECONDARY, RELAY_OFF); // turn secondary element off if temp is near mash temp
}
}
control_boil(){
digitalWrite(BOIL_PRIMARY, RELAY_ON); // turn primary ON
digitalWrite(BOIL_SECONDARY, RELAY_ON); // turn secondary ON
digitalWrite(PUMP, RELAY_OFF); // turn pump OFF
}