3
3
#include < ArduinoLog.h>
4
4
#include " RackTempController.h"
5
5
#include " OLEDDisplay.h"
6
- #include " MqttPublisher .h"
6
+ #include " MqttManager .h"
7
7
// #include "MAX31790FanControl.h"
8
8
#include " ArduinoFanControl.h"
9
9
10
+ void onMqttMessage (int messageSize);
11
+
10
12
/*
11
13
All pin usage
12
14
2, 4, 7 - OLED
@@ -41,9 +43,11 @@ RackTempController rtc(oneWire, fanControl);
41
43
// OLEDDisplay oled(PIN_CS, PIN_DC, PIN_RESET, PIN_IR);
42
44
OLEDDisplay oled (PIN_CS, PIN_DC, PIN_RESET);
43
45
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);
45
48
46
49
bool ethernetPresent = false ;
50
+ bool displayOnNotOff = true ;
47
51
48
52
RESULT ethernetSetup () {
49
53
RESULT res = RES_OK;
@@ -88,8 +92,8 @@ void setup(void)
88
92
Serial.begin (9600 );
89
93
90
94
// 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
93
97
Log.setPrefix (printTimestamp);
94
98
Log.setSuffix (printNewline);
95
99
@@ -102,7 +106,7 @@ void setup(void)
102
106
oled.render (" Ethernet initialised" );
103
107
Log.notice (F (" Ethernet initialised" ));
104
108
105
- if (mqttPublish .initialise ()==0 ) {
109
+ if (mqttManager .initialise ()==0 ) {
106
110
oled.render (" Mqtt initialised" );
107
111
Log.notice (F (" Mqtt initialised" ));
108
112
}
@@ -127,18 +131,19 @@ void loop(void) {
127
131
ns.mqttServerIP = MQTT_SERVER_IP;
128
132
ns.mqttPort = MQTT_PORT;
129
133
130
- mqttPublish .poll ();
134
+ mqttManager .poll ();
131
135
}
132
136
133
137
// read temperatures, modify fan speed
134
138
rtc.process (rs);
135
139
136
140
// render rack state, network state
137
- oled.render (rs, ns);
141
+ if (displayOnNotOff)
142
+ oled.render (rs, ns);
138
143
139
144
if (ethernetPresent) {
140
145
// emit rackstate data, will reconnect if required
141
- mqttPublish .publish (rs);
146
+ mqttManager .publish (rs);
142
147
143
148
// maintain IP via DHCP
144
149
int res = Ethernet.maintain ();
@@ -147,4 +152,26 @@ void loop(void) {
147
152
}
148
153
}
149
154
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
+
150
177
#endif
0 commit comments