Skip to content

Commit 855e553

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

File tree

5 files changed

+40
-128
lines changed

5 files changed

+40
-128
lines changed

include/MqttPublisher.h

-51
This file was deleted.

include/OLEDDisplay.h

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class OLEDDisplay {
3838
void render(RackState_t& rs, const NetworkState_t& ns); // std::map implementation does not support const
3939
void clearDisplay();
4040
void displayOff();
41+
void displayOn();
4142
void setOrientation(OLED_Orientation orient); // rotate
4243

4344
protected:

src/MqttPublisher.cpp

-69
This file was deleted.

src/OLEDDisplay.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ void OLEDDisplay::displayOff() {
4040
_oled.setDisplayOn(false);
4141
}
4242

43+
void OLEDDisplay::displayOn() {
44+
_oled.setDisplayOn(true);
45+
}
46+
4347
void OLEDDisplay::render(RackState_t&rs, const NetworkState_t& ns) {
4448

4549
if (!_usingIRSensor) {

src/main.cpp

+35-8
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#include <ArduinoLog.h>
44
#include "RackTempController.h"
55
#include "OLEDDisplay.h"
6-
#include "MqttPublisher.h"
6+
#include "MqttManager.h"
77
//#include "MAX31790FanControl.h"
88
#include "ArduinoFanControl.h"
99

10+
void onMqttMessage(int messageSize);
11+
1012
/*
1113
All pin usage
1214
2, 4, 7 - OLED
@@ -41,9 +43,11 @@ RackTempController rtc(oneWire, fanControl);
4143
//OLEDDisplay oled(PIN_CS, PIN_DC, PIN_RESET, PIN_IR);
4244
OLEDDisplay oled(PIN_CS, PIN_DC, PIN_RESET);
4345
EthernetClient ethClient;
44-
MqttPublisher mqttPublish(ethClient, CLIENT_ID, MQTT_SERVER_IP, MQTT_PORT);
46+
MqttClient mqttClient(ethClient);
47+
MqttManager mqttManager(&mqttClient, CLIENT_ID, MQTT_SERVER_IP, MQTT_PORT);
4548

4649
bool ethernetPresent = false;
50+
bool displayOnNotOff = true;
4751

4852
RESULT ethernetSetup() {
4953
RESULT res = RES_OK;
@@ -88,8 +92,8 @@ void setup(void)
8892
Serial.begin(9600);
8993

9094
// setup logging
91-
// Log.begin(LOG_LEVEL_WARNING, &mqttPublish); // use &Serial only to avoid MQTT events
92-
Log.begin(LOG_LEVEL_VERBOSE, &mqttPublish); // use &Serial only to avoid log output being published as events
95+
// Log.begin(LOG_LEVEL_WARNING, &mqttManager); // use &Serial only to avoid MQTT events
96+
Log.begin(LOG_LEVEL_VERBOSE, &mqttManager); // use &Serial only to avoid log output being published as events
9397
Log.setPrefix(printTimestamp);
9498
Log.setSuffix(printNewline);
9599

@@ -102,7 +106,7 @@ void setup(void)
102106
oled.render("Ethernet initialised");
103107
Log.notice(F("Ethernet initialised"));
104108

105-
if (mqttPublish.initialise()==0) {
109+
if (mqttManager.initialise()==0) {
106110
oled.render("Mqtt initialised");
107111
Log.notice(F("Mqtt initialised"));
108112
}
@@ -127,18 +131,19 @@ void loop(void) {
127131
ns.mqttServerIP = MQTT_SERVER_IP;
128132
ns.mqttPort = MQTT_PORT;
129133

130-
mqttPublish.poll();
134+
mqttManager.poll();
131135
}
132136

133137
// read temperatures, modify fan speed
134138
rtc.process(rs);
135139

136140
// render rack state, network state
137-
oled.render(rs, ns);
141+
if (displayOnNotOff)
142+
oled.render(rs, ns);
138143

139144
if (ethernetPresent) {
140145
// emit rackstate data, will reconnect if required
141-
mqttPublish.publish(rs);
146+
mqttManager.publish(rs);
142147

143148
// maintain IP via DHCP
144149
int res = Ethernet.maintain();
@@ -147,4 +152,26 @@ void loop(void) {
147152
}
148153
}
149154

155+
void onMqttMessage(int messageSize) {
156+
157+
Serial.println("rx");
158+
char buf[128];
159+
int i=0;
160+
while(mqttClient.available()) {
161+
if (i<128)
162+
buf[i++] = mqttClient.read();
163+
}
164+
165+
Serial.println(buf);
166+
167+
if (buf[0]=='1') {
168+
oled.displayOn();
169+
displayOnNotOff = true;
170+
}
171+
else if(buf[0]=='0') {
172+
oled.displayOff();
173+
displayOnNotOff = false;
174+
}
175+
}
176+
150177
#endif

0 commit comments

Comments
 (0)