-
Notifications
You must be signed in to change notification settings - Fork 1
/
cMQTT.h
175 lines (147 loc) · 5.47 KB
/
cMQTT.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
#include <PubSubClient.h> //mqtt
#include <sps30.h> //struct sps_values
//#pragma once
class cMQTT {
private:
WiFiClient *client;
PubSubClient mqtt;
String firmwareVersion;
unsigned long lastReport;//time of last mqtt connection status report
unsigned long lastConnect; //mqtt connection try
// MQTT TOPICs //
String device_update_global = "pyramidak/update/command";
String sensor_global = "pyramidak/sensor/command";
String device_restart = "/restart/command";
String device_update_command= "/update/command";
String device_update_state = "/update/state";
String device_ip_state = "/ip/state";
String mqtt_status = "/status";
String text_command = "/text/command";
String brightness_command = "/brightness/command";
String ambient_light = "/light";
String temperature_state = "/temperature";
String humidity_state = "/humidity";
bool connected() {
return mqtt.connected();
}
void connect() {
// Attempt to connect
if (mqtt.connect(deviceName.c_str(), user.c_str(), password.c_str())) {
mqtt.publish((deviceName + mqtt_status).c_str(), "offline");
report("MQTT was offline.");
mqtt.subscribe((deviceName + device_restart).c_str());
report("restart subscribed");
mqtt.subscribe((deviceName + device_update_command).c_str());
mqtt.subscribe(device_update_global.c_str());
update(firmwareVersion);
report("update subscribed");
reportSensors = true;
mqtt.subscribe(sensor_global.c_str());
report("sensors subscribed");
mqtt.subscribe((deviceName + brightness_command).c_str());
report("brightness subscribed");
mqtt.subscribe((deviceName + text_command).c_str());
report("text subscribed");
mqtt.publish((deviceName + mqtt_status).c_str(), "online");
report("MQTT connected to " + server, true);
} else {
report("MQTT connn.failed: " + String(mqtt.state()));
}
}
void report(String msg, bool offset = false) {
if(offset) {Serial.println("");}
Serial.println(msg);
}
public:
// MQTT settings //
String server = "192.168.0.181";
String user = "";//mqtt_pc";
String password = "";//photosmart";
String deviceName;
bool callUpdate;
bool reconnectNeeded; // force reconnect
bool reportSensors;
int updateStart; //web server update start: 0 - manual only, 1 - mqtt command
int diodBrightChange = -1;
String textChanged;
cMQTT() {}
void begin(String deviceName_, String firmwareVersion_) {
deviceName = deviceName_;
firmwareVersion = firmwareVersion_;
client = new WiFiClient();
//mqtt = new PubSubClient();
mqtt.setClient(*client);
mqtt.setServer(server.c_str(), 1883);
mqtt.setCallback([this] (char* topic, byte* payload, unsigned int length) { this->callback(topic, payload, length); });
}
void callback(char* topic, byte* payload, unsigned int length) {
//convert topic to string to make it easier to work with
String topicStr = topic;
String payloadStr = "";
for (int i = 0; i < length; i++) { payloadStr = payloadStr + (char)payload[i]; }
report("Message arrived:", true);
report("topic: " + topicStr);
report("payload: " + payloadStr);
if(topicStr == (deviceName + device_restart)) {
disconnect();
ESP.restart();
}
if(topicStr == (deviceName + device_update_command) or topicStr == device_update_global) {
if (updateStart == 1) callUpdate = true;
}
if(topicStr == sensor_global) {
update(firmwareVersion);
reportSensors = true;
}
if(topicStr == (deviceName + text_command)) textChanged = payloadStr;
if(topicStr == (deviceName + brightness_command)) diodBrightChange = payloadStr.toInt();
}
void disconnect() {
reconnectNeeded = false;
lastReport = 0;
lastConnect = 0;
mqtt.publish((deviceName + mqtt_status).c_str(), "offline");
report("MQTT status: offline");
mqtt.disconnect();
mqtt.setServer(server.c_str(), 1883);
}
void loop() {
if (reconnectNeeded == true) disconnect();
if (mqtt.connected() == true) {
if (millis() - lastReport >= 60*1000UL or lastReport == 0) {
lastReport = millis();
mqtt.publish((deviceName + mqtt_status).c_str(), "online");
report("MQTT status: online");
}
mqtt.loop();
} else {
if (millis() - lastConnect >= 10*1000UL or lastConnect == 0) {
lastConnect = millis();
connect();
}
}
}
void update(String version) {
mqtt.publish((deviceName + device_ip_state).c_str(), WiFi.localIP().toString().c_str());
mqtt.publish((deviceName + device_update_state).c_str(), version.c_str());
report("update state and IP address published: " + version);
}
void light(int value) {
if (mqtt.connected() == true and value > 0) {
mqtt.publish((deviceName + ambient_light).c_str(), String(value).c_str());
report("ambient light published: " + String(value));
}
}
void temp(float temperature, int humidity) {
if (mqtt.connected() == true) {
if (temperature != 0) {
mqtt.publish((deviceName + temperature_state).c_str(), String(temperature).c_str());
report("temperature published: " + String(temperature));
}
if (humidity != 0) {
mqtt.publish((deviceName + humidity_state).c_str(), String(humidity).c_str());
report("humidity published: " + String(humidity));
}
}
}
};