-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKakuReadPilight.ino
176 lines (151 loc) · 4.75 KB
/
KakuReadPilight.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
#include <ESPiLight.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#else
#include <WiFi.h>
#include <ESPmDNS.h>
#endif
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#define RECEIVE false
#define RECEIVER_PIN 4
#define BUILTIN_LED 2
#define ADDRESS 'M'
#define DEVICE 12
#define TRANSMITTER_PIN 15
#define SUBSCRIBE_TOPIC "kaku/#"
const char* ssid = "XXXX";
const char* password = "XXXX";
const char* mqtt_server = "192.168.10.2";
/*
* !!! The current device in use is an ESP32 !!
*/
WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (256)
char msg[MSG_BUFFER_SIZE];
String clientId;
ESPiLight rf(TRANSMITTER_PIN); // use -1 to disable transmitter
bool startsWith(const char *str, const char *start) {
return strncmp(start, str, strlen(start)) == 0;
}
bool endsWith(const char *str, const char *end) {
int n = strlen(str);
int m = strlen(end);
return strcmp(str+n-m, end) == 0;
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("Received MQTT topic: ");
Serial.println(topic);
Serial.println("Payload:");
Serial.println((char*)payload);
int unit = topic[5] - 65;
int id = atoi(topic+7) - 1; // strtol keeps going until the first non-int character
if (endsWith(topic, "set")) {
bool isOn = strncmp((char*)payload, "ON", length) == 0;
bool isOff = strncmp((char*)payload, "OFF", length) == 0;
Serial.print("ON? "); Serial.println(isOn);
Serial.print("OFF? "); Serial.println(isOff);
if(isOn) {
snprintf(msg, MSG_BUFFER_SIZE, "{\"id\":%d,\"unit\":%d,\"on\":1}", id, unit); // the value of "on" does not matter
} else {
snprintf(msg, MSG_BUFFER_SIZE, "{\"id\":%d,\"unit\":%d,\"off\":1}", id, unit);
}
rf.send("arctech_switch_old", msg);
Serial.println(msg);
}
}
void rfCallback(const String &protocol, const String &message, int status,
size_t repeats, const String &deviceID) {
Serial.print("RF signal arrived [");
Serial.print(protocol); // protocoll used to parse
Serial.print("][");
Serial.print(deviceID); // value of id key in json message
Serial.print("] (");
Serial.print(status); // status of message, depending on repeat, either:
// FIRST - first message of this protocoll within the
// last 0.5 s
// INVALID - message repeat is not equal to the
// previous message
// VALID - message is equal to the previous message
// KNOWN - repeat of a already valid message
Serial.print(") ");
Serial.print(message); // message in json format
Serial.println();
char topic[32];
snprintf(topic, 32, "kaku/%s", protocol);
client.publish(topic, message.c_str());
// check if message is valid and process it
if (status == VALID) {
Serial.print("Valid message: [");
Serial.print(protocol);
Serial.print("] ");
Serial.print(message);
Serial.println();
}
}
void setup() {
Serial.begin(115200);
Serial.println("Starting up...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("Connected to WIFI");
ArduinoOTA.setHostname("kaku");
ArduinoOTA
.onStart([]() {
});
ArduinoOTA.onError([](ota_error_t error) {
(void)error;
ESP.restart();
});
ArduinoOTA.begin();
// set callback funktion
rf.setCallback(rfCallback);
// inittilize receiver
if(RECEIVE) rf.initReceiver(RECEIVER_PIN);
randomSeed(micros());
clientId = "ESPClient-";
clientId += String(random(0xffff), HEX);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void mqttReconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
boolean success = client.connect(clientId.c_str(), "kaku/status", 0, true, "offline");
if (success) {
client.subscribe(SUBSCRIBE_TOPIC);
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
delay(100);
}
}
}
long last = 0;
void loop() {
ArduinoOTA.handle();
int i=0;
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
mqttReconnect();
i++;
if(i > 1024) ESP.restart();
}
client.loop();
if(RECEIVE) rf.loop();
delay(10);
if(millis() > last + 2000) {
last=millis();
client.publish("kaku/status", "online");
}
}