Skip to content

Commit 70dfdda

Browse files
committed
Enabled MQTT subscribe callback to switch OLED on/off
1 parent 855e553 commit 70dfdda

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

include/MqttManager.h

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include "RackTempController.h"
2+
#include <Ethernet.h>
3+
#include <ArduinoMqttClient.h>
4+
#include <Print.h>
5+
6+
/**
7+
* Wrapper class for MQTT and Logging framework
8+
*/
9+
class MqttManager : public Print {
10+
11+
public:
12+
MqttManager(MqttClient* p_mqttClient, const char* clientID, const char* serverIP, const uint16_t port) :
13+
_p_mqttClient(p_mqttClient),
14+
_clientID(clientID),
15+
_serverIP(serverIP),
16+
_port(port) {};
17+
18+
int initialise();
19+
20+
void publish(RackState_t& rs);
21+
22+
void poll();
23+
24+
/**
25+
* From Print.h
26+
*/
27+
size_t write(uint8_t c);
28+
29+
private:
30+
void sendMessage(const String& topic, const String& msg);
31+
32+
MqttClient* _p_mqttClient;
33+
34+
const String _clientID; // client identifier
35+
const String _serverIP; // mqtt server IP endpoint
36+
const uint16_t _port; // mqtt server port
37+
String _buf; // log buffer
38+
39+
const String topicTempRackTop = "/device/temp/rack/top";
40+
const String topicTempRackBase = "/device/temp/rack/base";
41+
const String topicTempRackAve = "/device/temp/rack/average";
42+
43+
const String topicFanTopLeft = "/device/rack/fan/topleft";
44+
const String topicFanTopRight = "/device/rack/fan/topright";
45+
const String topicFanBaseLeft = "/device/rack/fan/baseleft";
46+
const String topicFanBaseRight = "/device/rack/fan/baseright";
47+
48+
const String topicConfig = "/device/rack/config"; //display on/off subscriber topic
49+
50+
const String topicRackLog = "/device/rack/log";
51+
const String subtopicFanError = "/error";
52+
const String subtopicFanRPM = "/rpm";
53+
};

src/MqttManager.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#include "MqttManager.h"
2+
#include <ArduinoLog.h>
3+
4+
extern void onMqttMessage(int messageSize);
5+
6+
int MqttManager::initialise()
7+
{
8+
if (!_p_mqttClient->connect(_serverIP.c_str(), _port)) {
9+
Log.error(F("Mqtt connection failed - error code %d"), _p_mqttClient->connectError());
10+
return _p_mqttClient->connectError();
11+
}
12+
_p_mqttClient->setId(_clientID);
13+
14+
// set message handler for callback
15+
_p_mqttClient->onMessage(::onMqttMessage);
16+
17+
// subscribe to config topic
18+
_p_mqttClient->subscribe(topicConfig);
19+
20+
return 0;
21+
}
22+
23+
void MqttManager::publish(RackState_t& rs) {
24+
25+
if (!_p_mqttClient->connected()) {
26+
Log.error(F("Mqtt connection lost ... attempting reconnection"));
27+
initialise();
28+
}
29+
30+
Log.notice(F("Publishing temperature events"));
31+
sendMessage(topicTempRackTop, String(rs.thermos["topRack"].tempCelsuis));
32+
sendMessage(topicTempRackBase, String(rs.thermos["baseRack"].tempCelsuis));
33+
sendMessage(topicTempRackAve, String(rs.aveTempCelsius));
34+
35+
//Log.notice(F("Publishing fan events"));
36+
/*
37+
sendMessage(topicFanBaseLeft + subtopicFanRPM, String(rs.fanTopLeft.rpm));
38+
sendMessage(topicFanTopRight + subtopicFanRPM, String(rs.fanTopRight.rpm));
39+
sendMessage(topicFanBaseLeft + subtopicFanRPM, String(rs.fanBaseLeft.rpm));
40+
sendMessage(topicFanTopRight + subtopicFanRPM, String(rs.fanBaseRight.rpm));
41+
*/
42+
43+
// if (rs.hasFanError()) {
44+
// for each fan in error
45+
// sendMessage(topicRackFanError, rs.fanid)
46+
//}
47+
48+
}
49+
50+
void MqttManager::poll() {
51+
_p_mqttClient->poll();
52+
}
53+
54+
void MqttManager::sendMessage(const String& topic, const String& msg)
55+
{
56+
_p_mqttClient->beginMessage(topic);
57+
_p_mqttClient->print(msg);
58+
if (!_p_mqttClient->endMessage())
59+
Log.error(F("Failed to send message to topic %s"), topic.c_str());
60+
}
61+
62+
/**
63+
* From Print.h
64+
* Enables writing all log events to the topicRackLog
65+
*/
66+
size_t MqttManager::write(uint8_t c) {
67+
Serial.print(char(c));
68+
_buf += (char)c;
69+
if (c == '\n') {
70+
if (_p_mqttClient->connected()) {
71+
_p_mqttClient->beginMessage(topicRackLog);
72+
_p_mqttClient->print(_buf);
73+
_p_mqttClient->endMessage();
74+
}
75+
_buf = "";
76+
}
77+
return 1;
78+
};

0 commit comments

Comments
 (0)