-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathH4P_MQTT_CustomLWT.ino
32 lines (26 loc) · 1019 Bytes
/
H4P_MQTT_CustomLWT.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
#include<H4Plugins.h>
H4_USE_PLUGINS(115200,H4_Q_CAPACITY,false) // Serial baud rate, Q size, SerialCmd autostop
struct H4P_LWT myLWT={"myLWT","byebye",1,false}; // topic,pload,QoS,retain
H4P_WiFi h4wifi("XXXXXXXX","XXXXXXXX","testbed");
H4P_AsyncMQTT h4mqtt("http://192.168.1.4:1883","","",myLWT); // Custom lwt
uint32_t myCallback(std::vector<std::string> vs){
Serial.printf("USER: Msg received with payload=%s\n",H4PAYLOAD.c_str()); // convert payload to C-style string
if(H4PAYLOAD=="good") {
h4mqtt.publishDevice("goodpayload","Thank You!");
return H4_CMD_OK;
}
else {
h4mqtt.publishDevice("badpayload",H4PAYLOAD);
return H4_CMD_PAYLOAD_FORMAT;
}
}
void onMqttConnect(){ h4mqtt.subscribeDevice("mytopic",myCallback); }
void onMqttDisconnect(){}
void h4pGlobalEventHandler(const std::string& svc,H4PE_TYPE t,const std::string& msg){
switch(t){
H4P_DEFAULT_SYSTEM_HANDLER
case H4PE_SERVICE:
H4P_SERVICE_ADAPTER(Mqtt);
break;
}
}