-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalkomix.ino
191 lines (154 loc) · 4.04 KB
/
alkomix.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
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266mDNS.h>
#include "webHandler.h"
#include "PumpList.h"
#define PUMPS_LEN 4
#define PERCENT_DIFF 1
char device_name[50];
WiFiManager wifiManager;
ESP8266WebServer webServer(80);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 lcd(128, 32, &Wire, -1);
bool lcdOk;
// For nodemcu:
// PUMPS
// 5 - D1
// 4 - D2
// 4 - D2
// 13 - D7
// 15 - D8
// LCD
// SCL - D6 - 12
// SDA - D7 - 14
struct WebData webData = {
.percents = {25, 25, 25, 25},
.isReady = false
};
int pumpPins[PUMPS_LEN] = {5, 4, 13, 15};
PumpList pumpList(PUMPS_LEN);
void initSerial() {
Serial.begin(115200);
Serial.println("Init Alkomix...");
}
void initPumps () {
Serial.println("Init pump...");
for(int i=0; i < PUMPS_LEN; i++) {
int pin = pumpPins[i];
Pump *pump = new Pump(pin);
pumpList.addPump(i, pump);
}
bool isOk = pumpList.setupPumps();
if (!isOk) {
Serial.println("Something is wrong with pumps :(");
}
}
void initLCD() {
Serial.println("Init LCD...");
Wire.begin(14, 12);
lcdOk = lcd.begin(SSD1306_SWITCHCAPVCC, 0x3C);
if(!lcdOk) {
Serial.println("LCD init failed");
return;
}
lcd.display();
delay(500);
lcd.clearDisplay();
lcd.display();
}
void showTxtWithoutDisplay(String txt) {
if (!lcdOk) {
return;
}
lcd.clearDisplay();
lcd.setTextSize(2);
lcd.setTextColor(WHITE);
lcd.setCursor(0, 0);
lcd.println(txt);
}
void showTxt(String txt){
showTxtWithoutDisplay(txt);
lcd.display();
}
void showPercent(int index, int percent) {
if (!lcdOk) {
return;
}
showTxtWithoutDisplay(String("PUMP ") + String(index) + String("\n") + String(percent) + String("%"));
int width = lcd.width();
int height = lcd.height();
int rectHeight = percent * height / 100;
lcd.fillRect(width - 32, height - rectHeight, width, height, WHITE);
lcd.display();
}
void showIp() {
showTxt(String("IP: ") + WiFi.localIP().toString());
}
void initWifi() {
Serial.println("Init Wifi...");
sprintf(device_name, "Alkomixer-%06X", ESP.getChipId());
Serial.print("Device name: ");
Serial.println(device_name);
wifiManager.autoConnect(device_name);
delay(250);
if (!wifiManager.autoConnect(device_name)) {
Serial.println("failed to connect...");
delay(1000);
ESP.reset(); //reset and try again
delay(5000);
} else {
Serial.println("connected to wifi!");
}
}
void initWeb() {
webServer.on("/", HTTP_GET, [](){ handleMainPage(webServer, &webData); });
webServer.on("/", HTTP_POST, [](){ handlePostData(webServer, &webData); });
webServer.onNotFound([](){ handleNotFound(webServer, &webData); });
webServer.begin();
MDNS.begin("esp8266");
MDNS.addService("http", "tcp", 80);
Serial.printf("HTTPServer ready! http://%s.local/\n", device_name);
delay(300);
}
void setup() {
initSerial();
initPumps();
initLCD();
initWifi();
initWeb();
showIp();
}
long counterToast = 0;
void loop() {
webServer.handleClient();
delay(10);
switch (counterToast) {
case 0: break;
case 1: showIp(); counterToast = 0; break;
default: counterToast--;
}
if (!webData.isReady) {
return;
}
pumpList.setSpeed(50);
pumpList.setCupMilliliter(300);
int percentSum = 0;
for(int i=0; i < PUMPS_LEN; i++) {
int percent = webData.percents[i];
Serial.printf("Running pump:%d with %d %%...\n", i, percent);
for(int percentStart=0; percentStart < percent; percentStart += PERCENT_DIFF) {
int percentDiff = min(PERCENT_DIFF, percent - percentStart);
percentSum += percentDiff;
showPercent(i, percentSum);
pumpList.runPump(i, percentDiff);
}
}
webData.isReady = false;
counterToast = 300;
showTxt("TOAST!");
Serial.println("Done.");
}