-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow-blinds.ino
302 lines (246 loc) · 6.8 KB
/
window-blinds.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*******************************************
* Window blinds
* April, 2022
*
* Author: Piotr J. Węgrzyn
* Email: piotrwegrzyn@protonmail.com
*******************************************/
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <EEPROM.h>
#include <Servo.h>
static const char *AP_SSID = "ssid";
static const char *AP_PASS = "pass";
static const uint16_t STD_DELAY_L = 2000;
static const uint16_t STD_DELAY_M = 1000;
static const uint16_t STD_DELAY_S = 500;
static const uint16_t STD_DELAY_XS = 100;
static const uint16_t STD_DELAY_XXS = 10;
static const uint16_t STD_PORT = 4210;
static const size_t STD_PKT_SIZE = 5;
static const uint8_t CMD_BRD = 0xF0;
static const uint8_t CMD_SET_REQ = 0x01;
static const uint8_t CMD_GET_REQ = 0x02;
static const uint8_t CMD_GET_RES = 0x10;
static WiFiUDP _udp_instance;
static Servo _microservo;
static uint8_t SRV_ROT_RIGHT = 180;
static uint8_t SRV_ROT_LEFT = 0;
static uint8_t SRV_ROT_NONE = 89;
static bool _configured = false;
static uint16_t _id = 0x0;
static uint8_t _srv_pos = 0;
static uint32_t _srv_time = 0;
/* Function send_pkt() sends packets
* Returns true if packet was sent correctly
*/
bool send_pkt(IPAddress rcv_addr, uint16_t port=STD_PORT, uint8_t cmd=CMD_BRD) {
char buffer[6];
buffer[0] = cmd; // CMD
buffer[1] = _id >> 8; // ID
buffer[2] = _id & 0xFF; // ID
buffer[5] = '\0';
switch (cmd) {
case CMD_GET_RES:
buffer[3] = get_phr_val(); // PHR
buffer[4] = _srv_pos; // SER
break;
default:
buffer[3] = 0x0;
buffer[4] = 0x0;
break;
}
_udp_instance.beginPacket(rcv_addr, STD_PORT);
_udp_instance.print(buffer);
return _udp_instance.endPacket() == 1 ? true : false;
}
/* Function handle_pkt() serves incoming packets
* Returns true if packet was handled correctly
*/
bool handle_pkt() {
char pkt[STD_PKT_SIZE];
uint8_t len = _udp_instance.read(pkt, STD_PKT_SIZE);
if (len > 0) {
switch (pkt[0]){
case CMD_SET_REQ:
return set_params(pkt);
case CMD_GET_REQ:
return send_pkt(_udp_instance.remoteIP(), STD_PORT, CMD_GET_RES);
default:
return false;
}
}
Serial.println("Empty packet");
return false;
}
/* Function srv_time_conf()
* Configures microservo's rotation time
*/
void srv_time_conf() {
Serial.println("Configuration has started");
while (analogRead(D2) < 1) {
blink_led(1, STD_DELAY_XS);
}
while (analogRead(D2) > 0) {
blink_led(1, STD_DELAY_XS);
}
blink_led(3, STD_DELAY_S);
uint32_t start_time = millis();
_microservo.write(SRV_ROT_RIGHT);
while (analogRead(D2) < 1) {
delay(STD_DELAY_XXS);
continue;
}
_microservo.write(SRV_ROT_NONE);
_srv_time = millis() - start_time;
blink_led(1, STD_DELAY_M);
_srv_pos = 0xFF;
set_ser_pos(0x0);
Serial.printf("Configuration has ended. Rotate time: %d\n", _srv_time);
_configured = true;
blink_led(3, STD_DELAY_S);
}
/* Function blink_led()
* Blinks LED for given times and delay
*/
void blink_led(uint8_t times, uint32_t ms) {
while (times > 0) {
digitalWrite(D1, HIGH);
delay(ms);
digitalWrite(D1, LOW);
delay(ms);
times--;
}
}
/* Function set_eeprom_id()
* Copy EEPROM ID to program
*/
void set_eeprom_id() {
EEPROM.begin(4);
_id = EEPROM.read(1) << 8 | EEPROM.read(2);
}
/* Function set_led()
* Setup LED for work
*/
void set_led() {
pinMode(D1, OUTPUT);
digitalWrite(D1, LOW);
}
/* Function set_udp_rcv() starts listening on given port
* Returns true if listening started correctly
*/
bool set_udp_rcv(uint16_t port=STD_PORT) {
Serial.printf("Listening on UDP port %d\n", port);
return _udp_instance.begin(port) == 1 ? true : false;
}
/* Function set_wifi_con() establish connection to access point
* Returns true if connection established correctly
*/
bool set_wifi_con(uint8_t timeout = 60) {
WiFi.begin(AP_SSID, AP_PASS);
Serial.println("Connecting...");
for (uint8_t t = 0; t < timeout; t += 2) {
if (WiFi.status() == WL_CONNECTED) {
Serial.printf("Connected, IP address: %s\n", WiFi.localIP().toString().c_str());
return true;
}
delay(STD_DELAY_M);
}
Serial.println("Connection failed (timeout)");
return false;
}
/* Function set_params() set received parameters
* Returns true if params are set correctly
*/
bool set_params(char *params) {
if (params[1] & 0x10) {
uint16_t new_id = params[2] << 8 | params[3];
if (new_id != _id) {
_id = new_id;
EEPROM.write(1, params[2]);
EEPROM.write(2, params[3]);
EEPROM.commit();
}
}
if (params[1] & 0x01) {
return set_ser_pos(params[4]);
}
return true;
}
/* Function set_ser_con()
* Set up microservo conectivity
*/
void set_ser_con() {
_microservo.attach(D4);
_microservo.write(SRV_ROT_NONE);
}
/* Function set_ser_pos()
* Rotates microservo
*/
bool set_ser_pos(uint8_t pos) {
uint32_t rot_time = static_cast<uint32_t>((_srv_time * abs(_srv_pos - pos)) / 255);
if (_srv_pos > pos) {
_microservo.write(SRV_ROT_LEFT);
}
else if (_srv_pos < pos) {
_microservo.write(SRV_ROT_RIGHT);
}
delay(rot_time);
_microservo.write(SRV_ROT_NONE);
_srv_pos = pos;
return true;
}
/* Function get_pkt_size()
* Returns received packet size
*/
size_t get_pkt_size() {
return _udp_instance.parsePacket();
}
/* Function get_brd_addr()
* Returns broadcast address for current network
*/
IPAddress get_brd_addr() {
uint32_t network = WiFi.localIP();
uint32_t subnet = WiFi.subnetMask();
return IPAddress(network | (~subnet));
}
/* Function get_phr_val()
* Returns value of resistance on photoresistor (0-255)
*/
uint8_t get_phr_val() {
return static_cast<uint8_t>((analogRead(A0) * 255) / 1023);
}
void setup() {
Serial.begin(9600);
set_led();
set_ser_con();
set_eeprom_id();
if (!set_wifi_con()) {
Serial.println("Error while WiFi establishment");
while (true) {
blink_led(1, STD_DELAY_XS);
}
}
if (!set_udp_rcv(STD_PORT)) {
Serial.println("Error while UDP start");
while (true) {
blink_led(1, STD_DELAY_M);
}
}
blink_led(1, STD_DELAY_L);
}
void loop() {
if (!_configured) {
Serial.println("Microservo is not configured");
}
if (analogRead(D2) < 1) {
srv_time_conf();
}
if (_configured && get_pkt_size()) {
if(!handle_pkt()) {
Serial.println("Error occured while handling packet");
}
}
send_pkt(get_brd_addr());
delay(STD_DELAY_L);
}