/
SocketIo-Example.ino
103 lines (80 loc) · 1.84 KB
/
SocketIo-Example.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
#include <SocketIOClient.h>
#define LedPin 2
#define ButtonPin 0
#define SOFTAP_MODE
#ifdef SOFTAP_MODE
const char* password = "myMinion";
#else
const char* ssid = "SkyNet";
const char* password = "myMaster";
#endif
const char HexLookup[17] = "0123456789ABCDEF";
String host = "192.168.4.2";
int port = 3000;
bool clicked = false;
SocketIOClient socket;
void setupNetwork() {
#ifdef SOFTAP_MODE
WiFi.disconnect();
byte mac[6];
WiFi.macAddress(mac);
char ssid[14] = "Minion-000000";
ssid[7] = HexLookup[(mac[3] & 0xf0) >> 4];
ssid[8] = HexLookup[(mac[3] & 0x0f)];
ssid[9] = HexLookup[(mac[4] & 0xf0) >> 4];
ssid[10] = HexLookup[(mac[4] & 0x0f)];
ssid[11] = HexLookup[(mac[5] & 0xf0) >> 4];
ssid[12] = HexLookup[(mac[5] & 0x0f)];
ssid[13] = 0;
WiFi.softAP(ssid, password);
#else
WiFi.begin(ssid, password);
uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500);
if(i == 21){
while(1) delay(500);
}
#endif
}
void click() {
clicked = true;
}
void light(String state) {
Serial.println("[light] " + state);
if (state == "\"state\":true") {
Serial.println("[light] ON");
digitalWrite(LedPin, HIGH);
}
else {
Serial.println("[light] off");
digitalWrite(LedPin, LOW);
}
}
//
// This code runs only once
//
void setup() {
// set up our pins
pinMode(LedPin, OUTPUT);
pinMode(ButtonPin, INPUT);
digitalWrite(LedPin, LOW);
Serial.begin(115200);
setupNetwork();
attachInterrupt(digitalPinToInterrupt(ButtonPin), click, FALLING);
socket.on("light", light);
socket.connect(host, port);
}
void clickCheck() {
if (clicked) {
Serial.println("[click]");
socket.emit("toggle", "{\"state\":true}");
clicked = false;
}
}
//
// This code runs over and over again
//
void loop() {
socket.monitor();
clickCheck();
}