-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbridge.ino
155 lines (131 loc) · 3.63 KB
/
bridge.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <RCSwitch.h>
#include <stdint.h>
#define WIFI_SSID "..."
#define WIFI_PASSWORD "..."
#define MQTT_TOPIC "switch/rf/+"
#define TX_PIN 4 // Receiver on GPIO4 / D2.
#define TX_REPEAT 15
RCSwitch mySwitch = RCSwitch();
WiFiClient espClient;
PubSubClient client(espClient);
char hostString[16];
IPAddress brokerIp;
uint16_t brokerPort;
void setup() {
Serial.begin(115200);
sprintf(hostString, "ESP_%06X", ESP.getChipId());
/* Setup transmitter */
mySwitch.enableTransmit(TX_PIN);
mySwitch.setRepeatTransmit(TX_REPEAT);
delay(10);
setup_wifi();
if(!find_raspberry()){
// reboot?
}
Serial.print("IP: ");
Serial.print(brokerIp);
Serial.printf(", Port: %d\n", brokerPort);
client.setServer(brokerIp, brokerPort);
client.setCallback(callback);
}
void callback(char* topic, byte* payload, unsigned int length) {
uint8_t protocol;
uint16_t pulselen;
char* code;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// In future handle out of bounds check on topic (strcmp + len)
int switchNr = atoi((const char*)(topic + sizeof(MQTT_TOPIC) - 2)); //-2, 1 for null-termination, 1 for wildcard +
Serial.printf("Switch: %d\n", switchNr);
// Parse using strtok in format `protocol|pulselen|bincode`
char *tmp = (char*)payload;
char* tok = strtok(tmp, "|");
if(tok){
protocol = atoi(tok);
tok = strtok(NULL, "|");
if(tok && protocol <= 5){
pulselen = atoi(tok);
tok = strtok(NULL, "|");
if(tok){
code = tok;
Serial.printf("prot: %u, pulselen: %u, code: %s\n", protocol, pulselen, code);
mySwitch.setProtocol(protocol,pulselen);
mySwitch.send(code);
}
}
}
}
/* Discover raspberry using MDNS */
boolean find_raspberry() {
if (!MDNS.begin(hostString)) {
Serial.println("Error setting up MDNS responder!");
return false;
} else {
Serial.println("MDNS responder started...");
delay(10);
/* Start discovery of MDNS (e.g. avahi) service advertisements for MQTT */
int n = -1;
do {
if(!n) {
delay(1000);
Serial.println("Did not find MQTT broker, retrying...");
}
n = MDNS.queryService("mqtt", "tcp");
} while(n == 0);
Serial.println("MDNS broker found!");
brokerIp = MDNS.IP(0);
brokerPort = MDNS.port(0);
return true;
}
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
WiFi.hostname(hostString);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe to the topic
client.subscribe(MQTT_TOPIC);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}