-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirmware.ino
269 lines (228 loc) · 6.08 KB
/
firmware.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
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ESP8266WiFiMulti.h>
#define MAXNUMOFPPL 6
#define NULLPATH "0000000_0000000_00000000000"
#define MSDELAY 290
//WiFi
const char* ssid = "VK_Hackathon_Legacy";
const char* password = "hackathon2018";
ESP8266WiFiMulti WiFiMulti;
WiFiUDP udp;
//Server ip and port
const char* servIp = "95.213.28.151";
//const char* servIp = "172.20.38.86";
const int port = 45050;
//Packets
char incomingPacket[64];
char replyPacket[39];
//Used pins
int Blinkd = D3;
int Rd = D7;
int Bd = D5;
int Gd = D6;
//Time stamps to make timer
unsigned long lastTime;
//Path section
struct path {
char id[28] = NULLPATH;
int r = 0;
int g = 0;
int b = 0;
};
//Temp
path temp;
//All pathes
path pathesList[MAXNUMOFPPL];
//Index of current path to make color looping
int currentPath = 0;
//Device id
int zone = 7;
int roomNum = 24; //24 or 38
int devNum = 1; //1 or 3
//Encoded bites to send via led
unsigned int blinkAr;
//WiFi connecting
void wifiSetUp() {
Serial.begin(115200);
delay(10);
WiFi.mode(WIFI_STA);
WiFiMulti.addAP(ssid, password);
Serial.println();
Serial.println();
Serial.print("Wait for WiFi... ");
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//Main setup function
void setup() {
wifiSetUp();
pinMode(Blinkd, OUTPUT);
pinMode(Rd, OUTPUT);
pinMode(Gd, OUTPUT);
pinMode(Bd, OUTPUT);
blinkAr = convert();
whiteBlinking();
udp.begin(port);
lastTime = millis();
}
//Getting, parsing, handling packets
void getPackage() {
int packetSize = udp.parsePacket();
if (packetSize) {
Serial.printf("Received %d bytes from %s, port %d\n",
packetSize, udp.remoteIP().toString().c_str(), udp.remotePort());
int len = udp.read(incomingPacket, 64);
if (len > 0) {
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s|\n", incomingPacket);
if (incomingPacket[0] == 'H') {
sscanf(incomingPacket, "H;%27s;%d %d %d",
&temp.id, &temp.r, &temp.g, &temp.b);
Serial.printf("|%s|\n", temp.id);
Serial.printf("%d\n", temp.r);
Serial.printf("%d\n", temp.g);
Serial.printf("%d\n", temp.b);
for(int j = 0; j < MAXNUMOFPPL; ++j) {
if(strcmp(pathesList[j].id, temp.id) == 0) {
pathesList[j].r = temp.r;
pathesList[j].g = temp.g;
pathesList[j].b = temp.b;
sprintf(replyPacket, "H;%s;%d_2%d_%d", temp.id, zone, roomNum, devNum);
sendPack();
return;
}
}
int i = 0;
while(strcmp(pathesList[i].id, NULLPATH) != 0) {
++i;
if (i > MAXNUMOFPPL - 1) {
Serial.println("ERROR 1");
return;
}
}
memcpy(pathesList[i].id, temp.id, 27);
pathesList[i].r = temp.r;
pathesList[i].g = temp.g;
pathesList[i].b = temp.b;
sprintf(replyPacket, "H;%s;%d_2%d_%d", pathesList[i].id, zone, roomNum, devNum);
sendPack();
for(int j = 0; j < 12; ++j) {
Serial.print(pathesList[j].id);
Serial.print(" ");
Serial.print(pathesList[j].r);
Serial.print(" ");
Serial.print(pathesList[j].g);
Serial.print(" ");
Serial.print(pathesList[j].b);
Serial.println(" ");
}
}
if (incomingPacket[0] == 'L') {
Serial.printf("L: %s|\n", incomingPacket+2);
sscanf(incomingPacket, "L;%27s", &temp.id);
int i = 0;
sprintf(replyPacket, "L;%s;%d_2%d_%d", temp.id, zone, roomNum, devNum);
sendPack();
for(; i < MAXNUMOFPPL; ++i) {
Serial.printf("H: %s|\n", pathesList[i].id);
if(strcmp(pathesList[i].id, temp.id) == 0) {
memcpy(pathesList[i].id, NULLPATH, sizeof(pathesList[i].id));
return;
}
}
if (i == MAXNUMOFPPL) {
Serial.println("ERROR");
return;
}
}
}
}
//Set rgb color
void setRGB(int r = 0, int g = 0, int b = 0) {
digitalWrite(Rd, r);
digitalWrite(Gd, g);
digitalWrite(Bd, b);
}
//Changing color in loop over all active sessions
void changeColor() {
currentPath++;
for(int i = 0; i < MAXNUMOFPPL + 1; ++i, ++currentPath) {
if (currentPath > MAXNUMOFPPL - 1) currentPath = 0;
if (strcmp(pathesList[currentPath].id, NULLPATH) != 0) {
Serial.printf("Num is: %d\n", currentPath);
setRGB(pathesList[currentPath].r, pathesList[currentPath].g, pathesList[currentPath].b);
return;
}
}
setRGB();
}
//Blinking to send data via led
//1 - high signal
//0 - low signal
void whiteBlinking() {
for (int i = (sizeof(blinkAr)*8)-2; i >= 0; i--) {
if (blinkAr & (1 << i)) {
digitalWrite(Blinkd, HIGH);
delayMicroseconds(MSDELAY);
} else {
digitalWrite(Blinkd, LOW);
delayMicroseconds(MSDELAY);
}
}
}
//Send packet to server
void sendPack() {
udp.beginPacket(servIp, port);
udp.write(replyPacket);
udp.endPacket();
}
//MFM-encoding(modified)
unsigned int convert() {
int r = (zone << 9) | ((roomNum << 3) | devNum);
int count = 0;
for(int i = 0; i < 13; ++i) {
if (1U & (r >> i)) {
count++;
}
}
r = r << 1;
if (count % 2 == 1) r |= 1 << 0;
int a = r;
int c = ~(a | a << 1);
unsigned int res = 0;
for (int i = 12; i >= 0; --i) {
if ((a >> i) & 1ULL) {
res |= 1ULL << i * 2 + 1;
}
if ((c >> i) & 1ULL) {
res |= 1ULL << i * 2;
}
}
res = res << 2;
res |= 1U << 30;
res |= 1U << 0;
return res;
}
//Main loop
void loop() {
whiteBlinking();
if (abs(lastTime - millis()) > 3000) {
sprintf(replyPacket, "A;0000000_0000000_00000000;%d_2%d_%d", zone, roomNum, devNum);
sendPack();
lastTime = millis();
getPackage();
changeColor();
while (WiFiMulti.run() != WL_CONNECTED) {
Serial.println("Wifi disconected. Reconnectig");
wifiSetUp();
}
}
}