-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp32-simple-blinds.cpp
104 lines (97 loc) · 2.79 KB
/
esp32-simple-blinds.cpp
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
/* Control horizontal blinds using a 28BYJ-48
* stepper motor. Using my custom library for
* stepper control. Send position as a binary
* encoded integer via the websocket.
*/
#include <Arduino.h>
#include <ArduinoOTA.h>
#include <WiFi.h>
#include <WebSocketsServer.h>
#include <Simple28BYJ48.h>
#include "credentials.h"
// Globals
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASSWORD;
Simple28BYJ48 stepper(12, 14, 27, 26);
WebSocketsServer webSocket = WebSocketsServer(80);
int bin_to_int_le(uint8_t* bin)
{
// Convert binary data to int (little-endian representation)
return (bin[0] << 24) | (bin[1] << 16) | (bin[2] << 8) | (bin[3]);
}
void websocket_event(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
{
switch (type) {
case WStype_BIN: {
stepper.set_target_pos(bin_to_int_le(payload));
// webSocket.sendBIN(num, payload, length); //optional response
} break;
// Other types ignored at the moment!
case WStype_TEXT:
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
case WStype_DISCONNECTED:
case WStype_CONNECTED:
default:
break;
}
}
void setup()
{
Serial.begin(9600);
pinMode(stepper.in1, OUTPUT);
pinMode(stepper.in2, OUTPUT);
pinMode(stepper.in3, OUTPUT);
pinMode(stepper.in4, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
}
else { // U_SPIFFS
type = "filesystem";
}
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
}
else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
}
else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
}
else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
}
else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
// Start WebSocket server and assign a callback function
webSocket.begin();
webSocket.onEvent(websocket_event);
}
void loop()
{
ArduinoOTA.handle();
webSocket.loop();
stepper.keep_target_pos();
}