-
Notifications
You must be signed in to change notification settings - Fork 6
/
esp8266-benq-rs232-mqtt.ino
253 lines (225 loc) · 7.89 KB
/
esp8266-benq-rs232-mqtt.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
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <ArduinoOTA.h>
#include <Regexp.h>
// BEN Q SPECIFICS
#define serial_command_prepend "\r*"
#define serial_command_append "#\r"
#define serial_baud_rate 115200
// WIFI
#define WLAN_SSID "ssid"
#define WLAN_PASS "pass"
WiFiClient client;
//MQTT SERVER
#define AIO_SERVER "mqtt-server"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "mqtt-user"
#define AIO_KEY "mqtt-pass"
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish benq_status_pub = Adafruit_MQTT_Publish(&mqtt, "stat/projector/STATUS");
Adafruit_MQTT_Publish benq_any_command_pub = Adafruit_MQTT_Publish(&mqtt, "stat/projector/COMMAND");
Adafruit_MQTT_Subscribe benq_power_sub = Adafruit_MQTT_Subscribe(&mqtt, "cmnd/projector/POWER");
Adafruit_MQTT_Subscribe benq_volume_sub = Adafruit_MQTT_Subscribe(&mqtt, "cmnd/projector/VOLUME");
Adafruit_MQTT_Subscribe benq_source_sub = Adafruit_MQTT_Subscribe(&mqtt, "cmnd/projector/SOURCE");
Adafruit_MQTT_Subscribe benq_lamp_mode_sub = Adafruit_MQTT_Subscribe(&mqtt, "cmnd/projector/LAMP_MODE");
Adafruit_MQTT_Subscribe benq_any_command_sub = Adafruit_MQTT_Subscribe(&mqtt, "cmnd/projector/COMMAND");
Adafruit_MQTT_Subscribe benq_mute_sub = Adafruit_MQTT_Subscribe(&mqtt, "cmnd/projector/MUTE");
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT connected!");
}
void setup() {
Serial.begin(115200);
Serial1.begin(serial_baud_rate);
// Connect to WiFi access point.
Serial.print("Connecting to ");
Serial.print(WLAN_SSID);
WiFi.hostname("benq-controller");
wifi_station_set_hostname("benq-controller");
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
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();
// Setup MQTT subscriptions
mqtt.subscribe(&benq_power_sub);
mqtt.subscribe(&benq_volume_sub);
mqtt.subscribe(&benq_source_sub);
mqtt.subscribe(&benq_lamp_mode_sub);
mqtt.subscribe(&benq_any_command_sub);
mqtt.subscribe(&benq_mute_sub);
}
void loop() {
ArduinoOTA.handle();
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &benq_power_sub) {
benq_send_any_command("pow="+String((char *)benq_power_sub.lastread));
benq_publish_status();
}
if (subscription == &benq_volume_sub) {
benq_set_volume(atoi((char *)benq_volume_sub.lastread));
benq_publish_status();
}
if (subscription == &benq_source_sub) {
benq_send_any_command("sour="+String((char *)benq_source_sub.lastread));
benq_publish_status();
}
if (subscription == &benq_lamp_mode_sub) {
benq_send_any_command("lampm="+String((char *)benq_lamp_mode_sub.lastread));
benq_publish_status();
}
if (subscription == &benq_any_command_sub) {
benq_send_any_command((char *)benq_any_command_sub.lastread);
benq_publish_status();
}
if (subscription == &benq_mute_sub) {
benq_send_any_command("mute="+String((char *)benq_mute_sub.lastread));
benq_publish_status();
}
}
benq_publish_status();
}
String serial_send_command(String serial_command) {
Serial1.print(serial_command_prepend);
Serial1.print(serial_command);
Serial1.print(serial_command_append);
String serial_response = Serial.readString();
return serial_response;
}
String benq_send_any_command(String command) {
String response = serial_send_command(command);
String status_response = "{\"COMMAND\":\"" + command + "\",\"RESPONSE\":\"" + response + "\"}";
benq_any_command_pub.publish(status_response.c_str());
return response;
}
String regex(char match_array[], char match_string[]) {
MatchState parse_result;
char match_result[100];
parse_result.Target(match_array);
if(parse_result.Match(match_string) == REGEXP_MATCHED) {
parse_result.GetCapture(match_result, 0);
return String(match_result);
} else {
return "UNKNOWN";
}
}
String benq_get_power_status() {
char current_power_status[50];
serial_send_command("pow=?").toCharArray(current_power_status,50);
String result = regex(current_power_status, "POW=([^#]*)");
if(result == "UNKNOWN") {
return "OFF";
} else {
return result;
}
}
String benq_get_source_status() {
char current_source_status[50];
serial_send_command("sour=?").toCharArray(current_source_status,50);
return regex(current_source_status, "SOUR=([^#]*)");
}
int benq_get_volume_status() {
char current_volume_status[50];
serial_send_command("vol=?").toCharArray(current_volume_status,50);
return (regex(current_volume_status, "VOL=([^#]*)")).toInt();
}
String benq_get_lamp_mode() {
char current_lamp_status[50];
serial_send_command("lampm=?").toCharArray(current_lamp_status,50);
return regex(current_lamp_status, "LAMPM=([^#]*)");
}
int benq_get_lamp_hours() {
char current_lamp_hours[50];
serial_send_command("ltim=?").toCharArray(current_lamp_hours,50);
return (regex(current_lamp_hours, "LTIM=([^#]*)")).toInt();
}
String benq_get_mute_status() {
char current_mute_status[50];
serial_send_command("mute=?").toCharArray(current_mute_status,50);
return regex(current_mute_status, "MUTE=([^#]*)");
}
String benq_collect_status() {
String current_status;
current_status += "{";
current_status += "\"POWER\":\"" + benq_get_power_status() + "\"";
current_status += ",";
current_status += "\"SOURCE\":\"" + benq_get_source_status() + "\"";
current_status += ",";
current_status += "\"VOLUME\":" + String(benq_get_volume_status());
current_status += ",";
current_status += "\"LAMP_MODE\":\"" + String(benq_get_lamp_mode()) + "\"";
current_status += ",";
current_status += "\"LAMP_HOURS\":" + String(benq_get_lamp_hours());
current_status += ",";
current_status += "\"MUTE\":\"" + String(benq_get_mute_status()) + "\"";
current_status += "}";
return current_status;
}
void benq_publish_status() {
benq_status_pub.publish(benq_collect_status().c_str());
}
void benq_set_volume(int target_volume) {
int delta = target_volume - benq_get_volume_status();
for(int i = 0; i <= abs(delta); i++) {
if(delta > 0) {
benq_send_any_command("vol=+");
} else {
benq_send_any_command("vol=-");
}
}
}