-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrz_mqtt.h
244 lines (190 loc) · 5.04 KB
/
rz_mqtt.h
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
239
240
241
242
243
244
/*
TITLE:
rz_mqtt.h
BRIEF:
header only library
DESC:
Arduino library for ESP32, with some MQTT client functionalities (publish, subscribe)
DEPENDENCIES:
- PubSubClient
Topic begins allways with ESP32/ESP32-<Chip-ID> where "Chip-ID" = Mac-Address
publishing:
- ESP32/ESP32-<Chip-ID>/status // NTP time during boot/setup
- ESP32/ESP32-<Chip-ID>/temperature
- ESP32/ESP32-<Chip-ID>/humidity
subscribing:
- ESP32/ESP32-<Chip-ID>/get_temperature
- ESP32/ESP32-<Chip-ID>/get_humidity
SOURCE:
https://github.com/Zheng-Bote/ESP32_libs
SYNTAX:
#include "rz_mqtt.h"
int rz_mqttclient_start(std::string hostId)
bool rz_mqtt_sendMsg(std::string mqtt_topic, char mqtt_msg[])
HISTORY:
Version | Date | Developer | Comments
------- | ---------- | ---------- | ---------------------------------------------------------------
0.1.0 | 2019-10-27 | RZheng | created
0.4.0 | 2022-03-26 | RZheng | baseMac removed
1.0.0 | 2022-02-27 | RZheng | some small corrections and code updates; finalized
*/
#ifndef rz_mqtt
#define rz_mqtt
#include "Arduino.h"
#include <WiFiClient.h>
#include <PubSubClient.h>
WiFiClient wifiClient;
PubSubClient mqtt_client(wifiClient);
int rz_mqttclient_start(std::string hostId)
{
int counter = 0;
int cLimit = 3;
mqtt_client.setServer(mqttServer, mqttPort);
while (!mqtt_client.connected())
{
//Serial.print("Connecting to MQTT...");
if (mqtt_client.connect(hostId.c_str(), mqttUser, mqttPwd ))
{
Serial.println("-- connected");
return 0;
}
else
{
counter++;
Serial.print("failed with state ");
Serial.println(mqtt_client.state());
delay(2000);
}
if(counter >= cLimit)
{
Serial.println("-- FAILED: MQTT connect impossible");
return -1;
}
}
}
bool rz_mqtt_sendMsg(std::string mqtt_topic, char mqtt_msg[])
{
if (! mqtt_client.connect("nodeid", mqttUser, mqttPwd)) {
rz_mqttclient_start("nodeid");
}
//char mqtt_topicStr[50];
char mqtt_topicStr[100] = "ESP32/";
//strcpy(mqtt_topicStr, mqtt_topic.c_str());
strcat(mqtt_topicStr, mqtt_topic.c_str());
//Serial.print("MQTT Message:\nTopic: ");
//Serial.println(mqtt_topicStr);
//Serial.print("Message: ");
//Serial.println(mqtt_msg);
if (mqtt_client.publish(mqtt_topicStr, mqtt_msg) == true)
{
//Serial.println("MQTT successfully sent message");
return true;
}
else
{
//Serial.println("MQTT error sending message");
return false;
}
}
void rz_mqtt_action(char* topic, char* msg)
{
if(strcmp(topic, "C4D9/Alarm") == 0)
{
Serial.println("Topic identical");
}
else
{
Serial.println("Topic not identical");
}
char delimiter[] = "/";
char *ptr;
char buf[50] = {0};
ptr = strtok(topic, delimiter);
while(ptr != NULL)
{
strcpy(buf, ptr);
printf("Abschnitt gefunden: %s\n", ptr);
// naechsten Abschnitt erstellen
ptr = strtok(NULL, delimiter);
}
Serial.print("Buffer: ");
Serial.println(buf);
typedef struct
{
char type[50];
char msgtxt[255];
char action[100];
int number;
} Actions;
Actions Action[2];
strcpy(Action[0].type, "C4D9/Pushover");
strcpy(Action[0].action, "Pushover");
Action[0].number = 0;
strcpy(Action[1].type, "C4D9/Alarm");
strcpy(Action[1].action, "Alarm");
Action[1].number = 1;
Serial.print("Struct type: ");
Serial.println(Action[1].type);
int nr;
for(int i = 0; i < 2; i++)
{
if(strcmp(Action[i].action, buf) == 0)
{
nr = Action[i].number;
}
}
switch(nr)
{
case 0: Serial.print("case 0: "); Serial.println(Action[nr].type); break;
case 1: Serial.print("case 1: "); Serial.println(Action[nr].type); break;
default: Serial.println("nr ist irgendwas\n"); break;
}
}
void mqtt_callback(char* topic, byte* payload, unsigned int length)
{
char* buf = new char[length];
Serial.print("\n");
Serial.print("MQTT Message arrived in topic: ");
Serial.println(topic);
Serial.print("MQTT Message (length: ");
Serial.print(length);
Serial.println("):");
for (int i = 0; i < length; i++)
{
Serial.print("-");
}
Serial.print("\n");
strncat(buf, (char*)payload,length);
Serial.println(buf);
// Action
//rz_action(topic, buf);
//rz_action_msg_rcv(rz_baseMac(), topic, "MQTT", buf);
}
void rz_mqtt_subscribe(char mqtt_topic[])
{
mqtt_client.setCallback(mqtt_callback);
mqtt_client.subscribe(mqtt_topic);
}
#endif
/********************** README *********************
### ESP32 Main =>
#include "rz_wifi.h"
#include "rz_snippets.h"
#include "rz_mqtt.h"
void setup()
{
Serial.begin(115200);
delay(10);
rz_baseMac();
rz_wifi_connect();
rz_mqttclient_start();
rz_mqtt_sendMsg("ESP32/Test", "Hello World"); // will be topic: <baseMac>/ESP32/Test
rz_mqtt_subscribe("C4D9/Test");
rz_mqtt_subscribe("C4D9/Alarm");
}
void loop()
{
mqtt_client.loop();
}
### MQTT Client <=
*/