-
Notifications
You must be signed in to change notification settings - Fork 0
/
esp_uart_mqtt.ino
174 lines (147 loc) · 4.21 KB
/
esp_uart_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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "Streaming.h"
#include <SoftwareSerial.h>
//#define SERIAL_DEBUG
#define FAST_LOOP 200
#define SLOW_LOOP 10000
#define INFO_HEADER "[INFO] "
#define ERROR_HEADER "[ERROR] "
#define DEBUG_HEADER "[DEBUG] "
#define MQTT_HEADER "[MQTT] "
#define RF_SETUP_TOPIC "rf/config"
#define WIFI_SSID "***"
#define WIFI_PASSWORD "***"
#define MQTT_CLIENT_NAME "esp_uart_mqtt"
#define SERIAL_BAUD 57600
#define MQTT_SERVER "192.168.1.200"
#define MQTT_PORT 1883
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
#ifdef SERIAL_DEBUG
Serial << endl;
Serial << endl;
Serial << INFO_HEADER << "Connecting to: " << WIFI_SSID;
#endif
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
#ifdef SERIAL_DEBUG
Serial.print(".");
#endif
}
#ifdef SERIAL_DEBUG
Serial << endl << INFO_HEADER << "WiFi connected, ip: " << WiFi.localIP()
<< endl;
#endif
}
void reconnect() {
while (!client.connected()) {
#ifdef SERIAL_DEBUG
Serial.print("Attempting MQTT connection...");
#endif
// Create a random client ID
String clientId = MQTT_CLIENT_NAME;
clientId += String(random(0xffff), HEX);
// Attempt to connect
#ifdef SERIAL_DEBUG
Serial.print("Attempting MQTT connection...");
#endif
if (client.connect(clientId.c_str())) {
#ifdef SERIAL_DEBUG
Serial.println("connected");
#endif
client.subscribe(RF_SETUP_TOPIC);
} else {
#ifdef SERIAL_DEBUG
Serial.print("failed, rc=");
#endif
Serial.print(client.state());
#ifdef SERIAL_DEBUG
Serial.println(" try again in 5 seconds");
#endif
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(SERIAL_BAUD);
Serial.swap();
setup_wifi();
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback);
if (!client.connected()) {
reconnect();
}
client.loop();
}
void parseAndPublish() {
char buffer[100];
Serial.setTimeout(500);
size_t bytesRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer));
buffer[bytesRead] = '\0';
#ifdef SERIAL_DEBUG
Serial << DEBUG_HEADER << buffer << endl;
#endif
if ((bytesRead != 0) && (buffer[bytesRead-1] == '*')) {
String inMessage(buffer);
if (inMessage.startsWith(MQTT_HEADER, 0)) {
String mqttMessage = inMessage.substring(String(MQTT_HEADER).length());
#ifdef SERIAL_DEBUG
Serial << DEBUG_HEADER << mqttMessage << endl;
#endif
int spacePos = mqttMessage.indexOf(' ');
String mqttTopic = mqttMessage.substring(0, spacePos);
String mqttValue = mqttMessage.substring(spacePos+1, mqttMessage.length() -1);
#ifdef SERIAL_DEBUG
Serial << INFO_HEADER << "Publishing topic: " << mqttTopic << ", value: " << mqttValue << endl;
#endif
client.publish(mqttTopic.c_str(), mqttValue.c_str(), mqttValue.length());
}
} else {
#ifdef SERIAL_DEBUG
Serial << ERROR_HEADER << "Error reading serial!" << endl;
#endif
}
}
void callback(char* topic, byte* payload, unsigned int length) {
char message[length+1];
for (unsigned int i = 0; i < length; i++) {
message[i] = payload[i];
}
message[length] = '\0';
Serial << MQTT_HEADER << topic << " " << message << "*" << "\n";
}
void fastLoop() {
static unsigned long timeLoop = 0;
client.loop();
if ((millis() - timeLoop) > FAST_LOOP) {
timeLoop = millis();
if (!client.connected()) {
reconnect();
}
if (Serial.available()) {
parseAndPublish();
}
}
}
void slowLoop() {
static unsigned long timeLoop = 0;
if ((millis() - timeLoop) >= SLOW_LOOP) {
timeLoop = millis();
String topic = "esp/heartbeat";
String message(millis());
#ifdef SERIAL_DEBUG
Serial << INFO_HEADER << "Publishing heartbeat: " << millis() << endl;
#endif
client.publish(topic.c_str(), message.c_str());
}
}
void loop() {
fastLoop();
slowLoop();
}