forked from interactionresearchstudio/ESP32-SOCKETIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32-SOCKETIO.ino
282 lines (237 loc) · 6.58 KB
/
ESP32-SOCKETIO.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
//#define DEV
//#define STAGING
#define FADE_3 22
#define FADE_1 21
#define EXTERNAL_BUTTON 23
#define CAPTOUCH T0
#define LED_BUILTIN 2
#define LED_BUILTIN_ON HIGH
int BUTTON_BUILTIN = 0;
bool disconnected = false;
unsigned long wificheckMillis;
unsigned long wifiCheckTime = 5000;
enum PAIRED_STATUS {
remoteSetup,
localSetup,
pairedSetup
};
int currentPairedStatus = remoteSetup;
enum SETUP_STATUS {
setup_pending,
setup_client,
setup_server,
setup_finished
};
int currentSetupStatus = setup_pending;
#define PROJECT_SLUG "ESP32-SOCKETIO"
#define VERSION "v0.2"
#define ESP32set
#define WIFICONNECTTIMEOUT 240000
#define SSID_MAX_LENGTH 31
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
//socketIO
#include <SocketIoClient.h>
/* HAS TO BE INITIALISED BEFORE WEBSOCKETSCLIENT LIB */
SocketIoClient socketIO;
//Local Websockets
#include <WebSocketsClient.h>
#include <Preferences.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <WiFiAP.h>
#include <DNSServer.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <HTTPUpdate.h>
#include <AceButton.h>
using namespace ace_button;
//rgb led variables
#include <FastLED.h>
#define WS2812PIN 5
#define NUMPIXELS 2
#define PIXELUPDATETIME 30
#define PIXELUPDATETIMELONG 5000
#define USERLED 0
#define REMOTELED 1
#define RGBLEDPWMSTART 120
#define FASTLONGFADE 120
unsigned long LONGFADEMINUTESMAX = 360;
#define LONGFADECHECKMILLIS 120000
unsigned long prevLongFadeVal[NUMPIXELS] = {0, 0};
uint8_t hue[NUMPIXELS];
uint8_t saturation[NUMPIXELS];
uint8_t value[NUMPIXELS];
bool ledChanged[NUMPIXELS] = {false, false};
unsigned long prevPixelMillis;
bool isLongFade[NUMPIXELS] = {false, false};
unsigned long prevlongPixelMillis;
unsigned long longFadeMinutes[NUMPIXELS];
unsigned long prevLongFadeMillis[NUMPIXELS];
bool isRemoteLedFading = false;
CRGB leds[NUMPIXELS];
bool readyToFadeRGB[NUMPIXELS] = {false, false};
bool isFadingRGB[NUMPIXELS] = {false, false};
unsigned long fadeTimeRGB[NUMPIXELS];
#define RGBFADEMILLIS 6
#include "SPIFFS.h"
//Access Point credentials
String scads_ssid = "";
String scads_pass = "blinkblink";
const byte DNS_PORT = 53;
DNSServer dnsServer;
IPAddress apIP(192, 168, 4, 1);
Preferences preferences;
//local server
AsyncWebServer server(80);
AsyncWebSocket socket_server("/ws");
//local websockets client
WebSocketsClient socket_client;
byte webSocketClientID;
WiFiMulti wifiMulti;
String wifiCredentials = "";
String macCredentials = "";
/// Led Settings ///
bool isBlinking = false;
bool readyToBlink = false;
unsigned long blinkTime;
int blinkDuration = 200;
// Touch settings and config
class CapacitiveConfig: public ButtonConfig {
public:
uint8_t _pin;
uint16_t _threshold;
CapacitiveConfig(uint8_t pin, uint16_t threshold) {
_pin = pin;
_threshold = threshold;
}
void setThreshold(uint16_t CapThreshold) {
_threshold = CapThreshold;
}
protected:
int readButton(uint8_t /*pin*/) override {
uint16_t val = touchRead(_pin);
return (val < _threshold) ? LOW : HIGH;
}
};
int TOUCH_THRESHOLD = 60;
int TOUCH_HYSTERESIS = 20;
#define LONG_TOUCH 1500
#define LONG_PRESS 10000
CapacitiveConfig touchConfig(CAPTOUCH, TOUCH_THRESHOLD);
AceButton buttonTouch(&touchConfig);
bool isSelectingColour = false;
//Button Settings
AceButton buttonBuiltIn(BUTTON_BUILTIN);
AceButton buttonExternal(EXTERNAL_BUTTON);
void handleButtonEvent(AceButton*, uint8_t, uint8_t);
int longButtonPressDelay = 5000;
//reset timers
bool isResetting = false;
unsigned long resetTime;
int resetDurationMs = 4000;
String myID = "";
/// Socket.IO Settings ///
#ifndef STAGING
char host[] = "irs-socket-server.herokuapp.com"; // Socket.IO Server Address
#else
char host[] = "irs-socket-server-staging.herokuapp.com"; // Socket.IO Staging Server Address
#endif
int port = 80; // Socket.IO Port Address
char path[] = "/socket.io/?transport=websocket"; // Socket.IO Base Path
void setup() {
setupPixels();
Serial.begin(115200);
setupPins();
LONGFADEMINUTESMAX = checkFadingLength();
setupCapacitiveTouch();
//create 10 digit ID
myID = generateID();
SPIFFS.begin();
preferences.begin("scads", false);
wifiCredentials = preferences.getString("wifi", "");
macCredentials = preferences.getString("mac", "");
preferences.end();
setPairedStatus();
if (wifiCredentials == "" || getNumberOfMacAddresses() < 2) {
Serial.println("Scanning for available SCADS");
boolean foundLocalSCADS = scanAndConnectToLocalSCADS();
if (!foundLocalSCADS) {
//become server
currentSetupStatus = setup_server;
createSCADSAP();
setupCaptivePortal();
setupLocalServer();
}
else {
//become client
currentSetupStatus = setup_client;
setupSocketClientEvents();
}
}
else {
Serial.print("List of Mac addresses:");
Serial.println(macCredentials);
//connect to router to talk to server
digitalWrite(LED_BUILTIN, 0);
Serial.print("Last connected WiFi SSID: ");
Serial.println(getLastConnected());
connectToWifi(wifiCredentials);
//checkForUpdate();
setupSocketIOEvents();
currentSetupStatus = setup_finished;
Serial.println("setup complete");
}
}
void setPairedStatus() {
int numberOfMacAddresses = getNumberOfMacAddresses();
if (numberOfMacAddresses == 0) {
Serial.println("setting up JSON database for mac addresses");
preferences.clear();
addToMacAddressJSON(myID);
}
else if (numberOfMacAddresses < 2) {
//check it has a paired mac address
Serial.println("Already have local mac address in preferences, but nothing else");
}
else {
currentPairedStatus = pairedSetup;
Serial.println("Already has one or more paired mac address");
}
}
String getCurrentPairedStatusAsString() {
String currentPairedStatusAsString = "";
switch (currentPairedStatus) {
case remoteSetup: currentPairedStatusAsString = "remoteSetup"; break;
case localSetup: currentPairedStatusAsString = "localSetup"; break;
case pairedSetup: currentPairedStatusAsString = "pairedSetup"; break;
}
return (currentPairedStatusAsString);
}
void loop() {
switch (currentSetupStatus) {
case setup_pending:
break;
case setup_client:
socket_client.loop();
break;
case setup_server:
socket_server.cleanupClients();
dnsServer.processNextRequest();
break;
case setup_finished:
if (!disconnected) {
socketIO.loop();
rgbLedHandler();
}
ledHandler();
wifiCheck();
break;
}
buttonBuiltIn.check();
if (!disconnected) {
buttonExternal.check();
buttonTouch.check();
}
checkReset();
}