-
Notifications
You must be signed in to change notification settings - Fork 0
/
MQTT433STKgit.ino
116 lines (102 loc) · 2.98 KB
/
MQTT433STKgit.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
const char* SSID = "SSID";
const char* PSK = "PASSWORD";
const char* MQTT_BROKER = "BROKER";
const char* mqttUser = "";
const char* mqttPassword = "";
const char* getTopic = "/home/data/433/stk/get";
const char* setTopic = "/home/data/433/stk/set";
const char* Hostname = "MQTT433STK";
int message = 0;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
mySwitch.enableTransmit(D4);
pinMode(13, OUTPUT);
pinMode(D4, OUTPUT);
Serial.begin(115200);
setup_wifi();
client.setServer(MQTT_BROKER, 1883);
client.setCallback(callback);
ArduinoOTA.setHostname(Hostname);
ArduinoOTA.onStart([]() {
Serial.println("Start");
});
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();
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.hostname(Hostname);
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Received message ");
Serial.println(topic);
char msg[length+1];
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
msg[i] = (char)payload[i];
}
Serial.println();
msg[length] = '\0';
Serial.println(msg);
message = atof(msg);
mySwitch.send(message, 24);
mySwitch.send(message, 24);
mySwitch.send(message, 24);
client.publish(getTopic, msg);
}
void reconnect() {
while (!client.connected()) {
Serial.println("Reconnecting MQTT...");
if (!client.connect(Hostname, mqttUser, mqttPassword)) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retrying in 5 seconds");
delay(5000);
}
}
client.subscribe(setTopic);
Serial.println("MQTT Connected...");
}
void loop() {
if (!client.connected()) {
reconnect();
}
ArduinoOTA.handle();
client.loop();
}