-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmmqt.cpp
238 lines (159 loc) · 5.91 KB
/
mmqt.cpp
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
/*********
Based on :
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#include "mmqt.h"
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include "tools.h"
// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
//const char* mqtt_server = MMQT_BROKER ;
WiFiClient espClient;
long lastMsg = 0;
char msg[50];
int value = 0;
mqtt_Struct mqtt_cfg = {
MMQT_DEF_USER,
MMQT_DEF_PASSWORD,
{172,16,222,19},
MQTT_DEF_PORT,
MQTT_DEF_PUBLISH_TIMEOUT
};
// from leds.cpp
extern void LEDS_set_bri(uint8_t bri);
extern uint8_t LEDS_get_bri();
extern uint8_t LEDS_get_FPS_setting();
extern void LEDS_set_FPS(uint8_t fps_setting);
extern uint8_t LEDS_get_playNr();
extern void LEDS_set_playNr(uint8_t setNr);
// from wifi
extern String WiFi_Get_Name();
// from FS
extern boolean FS_mqtt_read();
extern void FS_mqtt_write();
PubSubClient client(espClient);
void MMQT_publish()
{
String deviceName = WiFi_Get_Name() ;
String OutTopicString = deviceName + MMQT_TOPIC_FUNCTION_BRI + MMQT_TOPIC_SEND ; client.publish(OutTopicString.c_str(), String(LEDS_get_bri()).c_str() );
OutTopicString = deviceName + MMQT_TOPIC_FUNCTION_FPS + MMQT_TOPIC_SEND ; client.publish(OutTopicString.c_str(), String(LEDS_get_FPS_setting()).c_str() );
OutTopicString = deviceName + MMQT_TOPIC_FUNCTION_PLAY + MMQT_TOPIC_SEND ; client.publish(OutTopicString.c_str(), String(LEDS_get_playNr()).c_str() );
OutTopicString = deviceName + MMQT_TOPIC_FUNCTION_DEVICE + MMQT_TOPIC_SEND ; if (LEDS_get_bri() != 0) client.publish(OutTopicString.c_str(), "on") ; else client.publish(OutTopicString.c_str(), "off");
OutTopicString = deviceName + MMQT_TOPIC_FUNCTION_TEMP + MMQT_TOPIC_SEND ; client.publish(OutTopicString.c_str(), String(temperatureRead()).c_str() );
}
void callback(char* topic, byte* message, unsigned int length) {
debugMe("Message arrived on topic: ");
debugMe(topic);
String messageTemp;
for (int i = 0; i < length; i++) {
// debugMe((char)message[i]);
messageTemp += (char)message[i];
}
debugMe("Message: ", false); debugMe(messageTemp);
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
String deviceName = WiFi_Get_Name() ;
String MMQTopicDeviceStatusSet = deviceName + MMQT_TOPIC_FUNCTION_DEVICE + MMQT_TOPIC_RECIVE ;
String MMQTopicFPSStatusSet = deviceName + MMQT_TOPIC_FUNCTION_FPS + MMQT_TOPIC_RECIVE ;
String MMQTopicBrightnessControll = deviceName + MMQT_TOPIC_FUNCTION_BRI + MMQT_TOPIC_RECIVE ;
String MMQTopicPlaySet = deviceName + MMQT_TOPIC_FUNCTION_PLAY + MMQT_TOPIC_RECIVE ;
if (String(topic) == MMQTopicDeviceStatusSet)
{
debugMe("Changing state ");
if (message[0] == 'o' && message[1] == 'n' )
{
if (LEDS_get_bri() == 0 ) LEDS_set_bri(255);
debugMe("mqtt on");
MMQT_publish();
}
else if (message[0] == 'o' && message[1] == 'f' )
{
LEDS_set_bri(0);
debugMe("mqtt off");
MMQT_publish();
}
}
else if (String(topic) == MMQTopicBrightnessControll)
{
uint8_t inval = messageTemp.toInt();
debugMe("Changing BRI to ", false); debugMe(inval);
LEDS_set_bri(inval);
}
else if (String(topic) == MMQTopicPlaySet)
{
uint8_t inval = messageTemp.toInt() ; //short_msg.toInt();
debugMe("Changing play to ", false); debugMe(inval);
LEDS_set_playNr(inval);
}
else if (String(topic) == MMQTopicFPSStatusSet)
{
//String short_msg = String(message[0]+message[1]);
uint8_t inval = messageTemp.toInt() ; //short_msg.toInt();
debugMe("Changing fps to ", false); debugMe(inval);
LEDS_set_FPS(inval);
}
else
{
{ debugMe("unknown mqtt topic : ", false) ; debugMe(topic) ;
}
}
}
void MMQT_subscribe()
{
debugMe("subscribing");
String deviceName = WiFi_Get_Name() ;
String INTopicString = deviceName + MMQT_TOPIC_FUNCTION_BRI + MMQT_TOPIC_RECIVE ; client.subscribe(INTopicString.c_str());
INTopicString = deviceName + MMQT_TOPIC_FUNCTION_FPS + MMQT_TOPIC_RECIVE ; client.subscribe(INTopicString.c_str());
INTopicString = deviceName + MMQT_TOPIC_FUNCTION_PLAY + MMQT_TOPIC_RECIVE ; client.subscribe(INTopicString.c_str());
INTopicString = deviceName + MMQT_TOPIC_FUNCTION_DEVICE + MMQT_TOPIC_RECIVE ; client.subscribe(INTopicString.c_str());
}
boolean reconnect() {
if (client.connect(WiFi_Get_Name().c_str() , mqtt_cfg.username, mqtt_cfg.password ))
{
debugMe(client.state( ));
debugMe("MMQT connecting");
// Once connected, publish an announcement...
MMQT_publish();
// ... and resubscribe
MMQT_subscribe();
}
debugMe(client.state( ));
return client.connected();
}
void MMQT_loop()
{
if (get_bool(MQTT_ON) == true)
{
long now = millis();
if (now - lastMsg > (mqtt_cfg.publishSec * 1000 ))
{
lastMsg = now;
if (!client.connected())
{
debugMe("MMQT Not connected");
if (reconnect())
{
lastMsg = 0;
}
} else
{
debugMe("MMQT Connected");
MMQT_publish();
}
}
client.loop();
}
}
void MMQT_setup() {
if (FS_mqtt_read() == false ) FS_mqtt_write();
client.setServer(mqtt_cfg.mqttIP, mqtt_cfg.mqttPort);
client.setCallback(callback);
lastMsg = 0;
if (get_bool(MQTT_ON) == true)
{
reconnect();
}
}