-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coffeemachine.ino
144 lines (119 loc) · 3.21 KB
/
Coffeemachine.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
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <SoftwareSerial.h>
#include <NTPClient.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#define WIFINAME "JURA"
#define GPIORX 3
#define GPIOTX 1
#define GPIOLEDIO 4
#define GPIOLEDPULSE 0
#define NTP_OFFSET 60 * 60 // In seconds
#define NTP_INTERVAL 60 * 1000 // In miliseconds
#define NTP_ADDRESS "europe.pool.ntp.org"
SoftwareSerial softserial(GPIORX, GPIOTX);
ESP8266WebServer webserver(5000);
int runAPI = 0;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL);
String cmd2jura(String outbytes) {
String inbytes;
int w = 0;
while (softserial.available()) {
softserial.read();
}
outbytes += "\r\n";
for (int i = 0; i < outbytes.length(); i++) {
for (int s = 0; s < 8; s += 2) {
char rawbyte = 255;
bitWrite(rawbyte, 2, bitRead(outbytes.charAt(i), s + 0));
bitWrite(rawbyte, 5, bitRead(outbytes.charAt(i), s + 1));
softserial.write(rawbyte);
}
delay(8);
}
int s = 0;
char inbyte;
while (!inbytes.endsWith("\r\n")) {
if (softserial.available()) {
byte rawbyte = softserial.read();
bitWrite(inbyte, s + 0, bitRead(rawbyte, 2));
bitWrite(inbyte, s + 1, bitRead(rawbyte, 5));
if ((s += 2) >= 8) {
s = 0;
inbytes += inbyte;
}
} else {
delay(10);
}
if (w++ > 500) {
return "";
}
}
return inbytes.substring(0, inbytes.length() - 2);
}
void handle_api() {
String result;
if (webserver.method() != HTTP_POST) {
webserver.send(405, "text/plain", "Method Not Allowed");
return;
}
String json = webserver.arg("plain");
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
String cmd = root["koffie_type"];
if (cmd.length() < 3) {
webserver.send(400, "text/plain", "Bad Request");
return;
}
digitalWrite(GPIOLEDIO, HIGH);
Serial.println(cmd);
result = cmd2jura(cmd);
HTTPClient client;
client.begin("**********************");
int httpCode = client.GET();
if(httpCode > 0) {
String payload = client.getString();
Serial.println(payload);
}
client.end();
digitalWrite(GPIOLEDIO, LOW);
if (result.length() < 3) {
webserver.send(503, "text/plain", "Service Unavailable");
return;
}
webserver.send(200, "text/plain", result);
}
void handle_web() {
String html;
html = "<!DOCTYPE html><html><body><h1>☕ Jura Coffee Machine Gateway</h1>";
html += "</body></html>";
webserver.send(200, "text/html", html);
}
void setup() {
// put your setup code here, to run once:
wifi_station_set_hostname(WIFINAME);
WiFiManager wifimanager;
wifimanager.autoConnect(WIFINAME);
timeClient.begin();
webserver.on("/", handle_web);
webserver.on("/api", handle_api);
webserver.begin();
softserial.begin(9600);
pinMode(GPIOLEDIO, OUTPUT);
pinMode(GPIOLEDPULSE, OUTPUT);
}
int i = 0;
void loop() {
// put your main code here, to run repeatedly:
webserver.handleClient();
timeClient.update();
i = (i + 1) % 200000;
digitalWrite(GPIOLEDPULSE, (i < 100000) ? LOW : HIGH);
}