-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEpsonRemote.ino
249 lines (203 loc) · 5.45 KB
/
EpsonRemote.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
#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <SoftwareSerial.h>
#include "wifi.h"
#include "mqtt.h"
#include "types.h"
#include "config.h"
#define MQTT_PREFIX_LOCATION ""
#define MQTT_PREFIX_ROOM "wohnzimmer"
#define MQTT_PREFIX MQTT_PREFIX_LOCATION MQTT_PREFIX_ROOM "/devices/projector"
#define MQTT_PROJECTOR_STATUS MQTT_PREFIX "/status"
#define MQTT_PROJECTOR_REFRESH MQTT_PREFIX "/refresh"
#define MQTT_PROJECTOR_POWER MQTT_PREFIX "/power"
#define DEFAULT_REFRESH_RATE 30
//#define BUILTIN_LED D7
String mqtt_client_id = "";
unsigned long lastMeasurementTime = 0;
unsigned int refresh_rate = DEFAULT_REFRESH_RATE; // seconds
WiFiClient wifiClient;
MQTTClient mqttClient;
SoftwareSerial swSer(5, 4);
const std::vector<const char*> mqtt_subscriptions = {
MQTT_PROJECTOR_REFRESH,
MQTT_PROJECTOR_POWER
};
void setup() {
pinMode(BUILTIN_LED, OUTPUT);
Serial.begin(115200);
Serial.println();
swSer.begin(9600);//, SERIAL_8N1);
mqtt_client_id = String(ESP.getChipId());
Serial.print("ESP Chip ID: ");
Serial.println(ESP.getChipId());
mqttClient.begin(MQTT_SERVER, MQTT_SERVERPORT, wifiClient);
mqttClient.onMessage(process_mqtt_subscriptions);
wifi::maintain_wifi_connection(WLAN_SSID, WLAN_PASS);
mqtt::maintain_connection(mqttClient, mqtt_client_id.c_str(), mqtt_subscriptions);
Serial.println("Switching off internal LED");
digitalWrite(BUILTIN_LED, HIGH);
}
void process_mqtt_subscriptions(String &topic, String &payload) {
if (topic.equals(MQTT_PROJECTOR_REFRESH)) {
Serial.println("Got MQTT topic '...projector/refresh'");
powerStatus();
if (!payload.equals("")) {
// TODO set new refresh rate
}
}
else if (topic.equals(MQTT_PROJECTOR_POWER)) {
Serial.println("Got MQTT topic '...projector/power'");
power(payload);
}
delay(100);
}
void loop() {
wifi::maintain_wifi_connection(WLAN_SSID, WLAN_PASS);
mqtt::maintain_connection(mqttClient, mqtt_client_id.c_str(), mqtt_subscriptions);
mqttClient.loop();
long now = millis();
if (now - lastMeasurementTime > refresh_rate * 1000) {
lastMeasurementTime = now;
powerStatus();
}
}
// **************
POWER getPower()
{
String result = sendCommand("PWR?");
if (detectError(result, "projector status check failed"))
return UNKNOWN;
if (result.equals("PWR=00"))
return OFF;
if (result.equals("PWR=01"))
return ON;
if (result.equals("PWR=02"))
return WARMUP;
if (result.equals("PWR=03"))
return COOLINGDOWN;
if (result.equals("PWR=04"))
return STANDBYNETWORKON;
if (result.equals("PWR=05"))
return ABNORMALSTANDBY;
report("unexpected status: " + result);
return UNKNOWN;
}
int power(String arg)
{
if (arg.equals("on") || arg.equals("1"))
return powerOn();
if (arg.equals("off") || arg.equals("0"))
return powerOff();
if (arg.equals("?") || arg.equals(""))
return powerStatus();
report("unexpected arg: '" + arg + "'");
return -1;
}
int powerOn()
{
const POWER status = getPower();
if (status == ON || status == WARMUP)
{
mqttClient.publish(MQTT_PROJECTOR_STATUS, "on", false, 1);
return 0;
}
const String result = sendCommand("PWR ON");
if (detectError(result, "projector turn on failed"))
{
return -1;
}
report("PWR ON: " + result);
const bool cond = result.equals("");
const String status_str = cond ? "on" : "off";
mqttClient.publish(MQTT_PROJECTOR_STATUS, status_str, false, 1);
cond ? report("projector turned on") : report("projector turn on failed");
return cond ? 0 : -1;
}
int powerOff()
{
const POWER status = getPower();
if (status == OFF || status == STANDBYNETWORKON)
{
mqttClient.publish(MQTT_PROJECTOR_STATUS, "on", false, 1);
return 0;
}
const String result = sendCommand("PWR OFF");
if (detectError(result, "projector turn off failed"))
{
return -1;
}
report("PWR OFF: " + result);
const bool cond = result.equals("");
String status_str = cond ? "off" : "on";
mqttClient.publish(MQTT_PROJECTOR_STATUS, status_str, false, 1);
cond ? report("projector turned off") : report("projector turn off failed");
return cond ? 0 : -1;
}
int powerStatus()
{
const POWER status = getPower();
if (status == UNKNOWN)
{
return -1;
}
String status_str = (status == ON || status == WARMUP) ? "on" : "off";
mqttClient.publish(MQTT_PROJECTOR_STATUS, status_str, false, 1);
return (status == ON || status == WARMUP) ? 1 : 0;
}
int checkError(String arg)
{
const String result = sendCommand("ERR?");
if (result.startsWith("ERR="))
result.substring(4);
return result.toInt();
}
int ok(String arg)
{
return checkError(arg) == 0 ? 0 : -1;
}
bool detectError(String result, String errorMessage)
{
const bool error = result.equals("ERR");
if (error)
report(errorMessage);
return error;
}
void report(String descr)
{
Serial.println(descr);
}
// **************
void write(String cmd)
{
digitalWrite(BUILTIN_LED, LOW);
swSer.print(cmd);
swSer.print("\r");
delay(100);
digitalWrite(BUILTIN_LED, HIGH);
}
String read()
{
digitalWrite(BUILTIN_LED, LOW);
String result = "";
while (swSer.available() > 0)
{
result.concat((char) swSer.read());
}
delay(100);
digitalWrite(BUILTIN_LED, HIGH);
return result;
}
String sendCommand(String cmd)
{
write(cmd);
String result = read();
Serial.print("read: >>");
Serial.print(result);
Serial.println("<<");
result.trim();
if (result.endsWith(":"))
result.remove(result.length() - 1, 1);
result.trim();
return result;
}