-
Notifications
You must be signed in to change notification settings - Fork 0
/
DrinkingBuddyLocker.ino
272 lines (221 loc) · 6.58 KB
/
DrinkingBuddyLocker.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
#include <SPI.h>
#include <ArduinoJson.h>
#include <MFRC522.h>
#include <DNSServer.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include "WiFiManager.h" //https://github.com/tzapu/WiFiManager
#include <PubSubClient.h>
#include "Clock.h"
#include "HttpBuyTransaction.h"
#include "HttpClient.h"
#include "HttpSyncTransaction.h"
#include "RfidReader.h"
#include "Sound.h"
#include "mqttconf.h"
#include "lockerinterface.h"
static RfidReader rfid;
static Clock myclock;
static Sound sound;
static HttpClient http;
//MQTT stuff
//const char* mqtt_server = "mqtt.lan.posttenebraslab.ch";
const char* mqtt_server = "185.250.59.200";
WiFiClient espClient;
PubSubClient client(espClient);
char strbuf[200];
char* lastBadge = "";
/*
#define OLED_RESET 0 // GPIO0
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 48)
#error("Height incorrect, please get a good version of Adafruit_SSD1306.h! from github mcauser branch 64x48");
#endif
*/
bool getLocker(char* badge);
void configModeCallback (WiFiManager *myWiFiManager);
#define RESTART_RFID 30000UL
unsigned long lastRestartTime = millis();
unsigned long last_open_request = millis();
unsigned long menu_timeout = 20000;
const long GRACE_PERIOD = 20000; //20 secs
std::vector<std::string> locker_selection;
LockerInterface display;
enum iface_state_machine {
IFACE_STATE_STARTUP,
IFACE_STATE_SWIPE,
IFACE_STATE_MENU
};
iface_state_machine iface_state = IFACE_STATE_STARTUP;
void setup() {
iface_state = IFACE_STATE_STARTUP;
Serial.begin(115200);
Serial.println("Init Display");
display.init();
Serial.println("Starting display");
Serial.println("Starting wifi...");
display.status("Starting wifi...");
sound.begin();
sound.play("a1");
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
wifiManager.setAPCallback(configModeCallback);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//here "AutoConnectAP"
//and goes into a blocking loop awaiting configuration
if(!wifiManager.autoConnect()) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(1000);
}
//oledPrint("Connected! :)", false);
Serial.println("Wifi connected...)");
display.status("Wifi connected...");
Serial.println("Starting RFID...");
display.status("Starting RFID...");
SPI.begin(); // Init SPI bus
//mfrc522.PCD_Init(); // Init MFRC522
rfid.begin();
client.setServer(mqtt_server, 1883);
sound.play("b1");
sound.play("a1");
display.status("Ready");
/*
oledPrint("Ready :)");
getFoodCount();
*/
// delay(2000);
// display.set_selection(std::vector<std::string>{"Ta","Mere","En","String","A","La","Migros"});
// display.show_selector(0);
// display.status("Select Locker");
iface_state = IFACE_STATE_SWIPE;
display.clear_swipe();
}
void loop() {
// put your main code here, to run repeatedly:
switch (iface_state)
{
case IFACE_STATE_SWIPE:
{
// swipe state
char* badge = rfid.tryRead();
if (badge)
{
sound.play("b1");
lastBadge = badge;
Serial.print("badge found ");
Serial.println(badge);
Serial.print("Last badge changed to: ");
Serial.println(lastBadge);
// ignore all waiting badge to avoid unintended double buy
while (rfid.tryRead())
{
Serial.println("rfid.tryRead");
delay(1000);
}
if (getLocker(badge))
{
iface_state = IFACE_STATE_MENU;
display.show_selector(0);
last_open_request = millis();
return;
}
// move to menu selector state
} //end if badge
// Display Swipe
display.swipe_prompt();
}
case IFACE_STATE_MENU:
{
// checkfor timeout
if (millis() - last_open_request > menu_timeout)
{
display.clear_swipe();
iface_state = IFACE_STATE_SWIPE;
return;
}
int sel = 0;
if (display.check_selection(sel))
{
if (sel == -3)
{
// cancel button
display.clear_swipe();
iface_state = IFACE_STATE_SWIPE;
return;
}
sprintf(strbuf, "selected: %d", sel);
// send out locker open command.
if (!client.connected()) {
reconnect();
}
//client.loop();
delay(500);
client.publish("Locker", locker_selection[sel].c_str());
display.status(strbuf);
display.clear_swipe();
iface_state = IFACE_STATE_SWIPE;
}
}
}
}
bool getLocker(char* badge)
{
HttpBuyTransaction lockerTransaction(http);
Serial.println("Starting to get lockers...");
if (!lockerTransaction.performLocker(badge, myclock.getUnixTime()))
{
Serial.println("Error getting lockers...");
return false;
}
Serial.println("End get lockers...");
// Serial.println(lockerTransaction.getMessage(0));
// Serial.println(lockerTransaction.getMessage(1));
std::vector<std::string> message = lockerTransaction.getMessage();
if (message.size() > 0 && message[0] == "ERROR")
{
lastBadge = "";
Serial.print("Unknown badge or no lockers");
//sound.play(lockerTransaction.getMelody());
return false;
}
else
{
Serial.println("OK locker");
digitalWrite(PIN_RELAY, HIGH); // turn on relay with voltage HIGH
sound.play(lockerTransaction.getMelody());
//PLAY SOUND
locker_selection = message;
display.set_selection(message);
return true;
}
}
void configModeCallback (WiFiManager *myWiFiManager) {
//oledPrint("Plz config WiFi");
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client",MQTTUNAME, MQTTPASS)) {
Serial.println("connected");
client.subscribe("Locker");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}