mqtt cmd + uptime
This commit is contained in:
parent
b4705d5662
commit
c2de0695c0
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
delay(500);
|
delay(500);
|
||||||
MySerial.printf("\n\n\nStartup\n");
|
Log.printf("\n\n\nStartup\n");
|
||||||
wifiConnect();
|
wifiConnect();
|
||||||
sensorSetup();
|
sensorSetup();
|
||||||
}
|
}
|
||||||
|
|||||||
41
src/mqtt.cpp
41
src/mqtt.cpp
@ -1,4 +1,7 @@
|
|||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
|
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
@ -8,23 +11,49 @@ WiFiClient client;
|
|||||||
|
|
||||||
PubSubClient mqtt(client);
|
PubSubClient mqtt(client);
|
||||||
|
|
||||||
MySerialClass MySerial(mqtt);
|
LogClass Log(mqtt);
|
||||||
|
|
||||||
unsigned long mqttFailureMillis = 0;
|
unsigned long mqttFailureMillis = 0;
|
||||||
|
|
||||||
// ReSharper disable once CppUseAuto
|
// ReSharper disable once CppUseAuto
|
||||||
const char* LOG_TOPIC = (String("log/") + HOSTNAME).c_str();
|
const String logTopic = String("log/") + HOSTNAME;
|
||||||
|
|
||||||
|
// ReSharper disable once CppUseAuto
|
||||||
|
const String cmdTopic = String("cmd/") + HOSTNAME;
|
||||||
|
|
||||||
|
void mqttCallback(char *topic, const uint8_t *payload, unsigned int length) {
|
||||||
|
char message[500];
|
||||||
|
length = min(sizeof message - 1, length);
|
||||||
|
memcpy(message, payload, length);
|
||||||
|
*(message + length) = 0;
|
||||||
|
Log.printf("MQTT received: topic=\"%s\", message=\"%s\"\n", topic, message);
|
||||||
|
if (strcmp(message, "reboot") == 0) {
|
||||||
|
Log.printf("rebooting...\n");
|
||||||
|
delay(500);
|
||||||
|
mqtt.disconnect();
|
||||||
|
delay(500);
|
||||||
|
EspClass::restart();
|
||||||
|
} else if (strcmp(message, "info") == 0) {
|
||||||
|
Log.printf("INFO\n");
|
||||||
|
Log.printf(" %-10s %s\n", "SSID:", WiFi.SSID().c_str());
|
||||||
|
Log.printf(" %-10s %s\n", "IP:", WiFi.localIP().toString().c_str());
|
||||||
|
Log.printf(" %-10s %d\n", "RSSI:", WiFi.RSSI());
|
||||||
|
Log.printf(" %-10s %s\n", "uptime:", uptimeString().c_str());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void mqttLoop() {
|
void mqttLoop() {
|
||||||
if (!mqtt.loop() && isWifiConnected() && (mqttFailureMillis == 0 || millis() - mqttFailureMillis >= 3000)) {
|
if (!mqtt.loop() && isWifiConnected() && (mqttFailureMillis == 0 || millis() - mqttFailureMillis >= 3000)) {
|
||||||
mqtt.setServer("10.0.0.50", 1883);
|
mqtt.setServer("10.0.0.50", 1883);
|
||||||
if (mqtt.connect(HOSTNAME, LOG_TOPIC, 0, false, "disconnected\n")) {
|
if (mqtt.connect(HOSTNAME, logTopic.c_str(), 0, false, "disconnected\n")) {
|
||||||
yield();
|
yield();
|
||||||
mqttPublish(LOG_TOPIC, "connected\n");
|
mqttPublish(logTopic.c_str(), "connected\n");
|
||||||
MySerial.printf("MQTT connected as \"%s\"\n", HOSTNAME);
|
mqtt.setCallback(mqttCallback);
|
||||||
|
mqtt.subscribe(cmdTopic.c_str());
|
||||||
|
Log.printf("MQTT connected as \"%s\"\n", HOSTNAME);
|
||||||
mqttFailureMillis = 0;
|
mqttFailureMillis = 0;
|
||||||
} else {
|
} else {
|
||||||
MySerial.printf("Failed to connect MQTT.\n");
|
Log.printf("Failed to connect MQTT.\n");
|
||||||
mqttFailureMillis = max(1UL, millis());
|
mqttFailureMillis = max(1UL, millis());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/mqtt.h
14
src/mqtt.h
@ -5,7 +5,9 @@
|
|||||||
#include "PubSubClient.h"
|
#include "PubSubClient.h"
|
||||||
#include "wifi.h"
|
#include "wifi.h"
|
||||||
|
|
||||||
extern const char* LOG_TOPIC;
|
extern const String logTopic;
|
||||||
|
|
||||||
|
extern const String cmdTopic;
|
||||||
|
|
||||||
void mqttLoop();
|
void mqttLoop();
|
||||||
|
|
||||||
@ -17,7 +19,7 @@ void mqttPublish(const char *topic, float value, const char *unit);
|
|||||||
|
|
||||||
void mqttPublish(const char *topic, const char *payload);
|
void mqttPublish(const char *topic, const char *payload);
|
||||||
|
|
||||||
class MySerialClass final : public Stream {
|
class LogClass final : public Stream {
|
||||||
|
|
||||||
PubSubClient& mqtt;
|
PubSubClient& mqtt;
|
||||||
|
|
||||||
@ -33,7 +35,7 @@ class MySerialClass final : public Stream {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit MySerialClass(PubSubClient& mqttClient) : mqtt(mqttClient) {
|
explicit LogClass(PubSubClient& mqttClient) : mqtt(mqttClient) {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,10 +51,10 @@ public:
|
|||||||
due = true;
|
due = true;
|
||||||
if (mqtt.connected()) {
|
if (mqtt.connected()) {
|
||||||
if (overflow > 0) {
|
if (overflow > 0) {
|
||||||
mqtt.publish(LOG_TOPIC, "\n### LOG BUFFER OVERFLOW BY %d BYTES ###\n");
|
mqtt.publish(logTopic.c_str(), "\n### LOG BUFFER OVERFLOW BY %d BYTES ###\n");
|
||||||
overflow = 0;
|
overflow = 0;
|
||||||
}
|
}
|
||||||
mqtt.publish(LOG_TOPIC, reinterpret_cast<const char *>(buffer));
|
mqtt.publish(logTopic.c_str(), reinterpret_cast<const char *>(buffer));
|
||||||
bufferWrite = buffer;
|
bufferWrite = buffer;
|
||||||
*bufferWrite = 0;
|
*bufferWrite = 0;
|
||||||
due = false;
|
due = false;
|
||||||
@ -74,6 +76,6 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
extern MySerialClass MySerial;
|
extern LogClass Log;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -11,9 +11,9 @@ auto tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT);
|
|||||||
|
|
||||||
void sensorSetup() {
|
void sensorSetup() {
|
||||||
if (tsl.begin()) {
|
if (tsl.begin()) {
|
||||||
MySerial.printf("TSL2561: Initialized.\n");
|
Log.printf("TSL2561: Initialized.\n");
|
||||||
} else {
|
} else {
|
||||||
MySerial.printf("TSL2561: Failed to initialize.\n");
|
Log.printf("TSL2561: Failed to initialize.\n");
|
||||||
}
|
}
|
||||||
tsl.enableAutoRange(true);
|
tsl.enableAutoRange(true);
|
||||||
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
|
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
|
||||||
@ -25,7 +25,7 @@ void sensorRead() {
|
|||||||
tsl.getLuminosity(&broadband, &ir);
|
tsl.getLuminosity(&broadband, &ir);
|
||||||
const auto illuminance = tsl.calculateLux(broadband, ir);
|
const auto illuminance = tsl.calculateLux(broadband, ir);
|
||||||
if (illuminance == 65536) {
|
if (illuminance == 65536) {
|
||||||
MySerial.printf("TSL2561: Failed to read.\n");
|
Log.printf("TSL2561: Failed to read.\n");
|
||||||
sensorSetup();
|
sensorSetup();
|
||||||
} else {
|
} else {
|
||||||
mqttPublish("garden/illuminance", illuminance, "lux");
|
mqttPublish("garden/illuminance", illuminance, "lux");
|
||||||
|
|||||||
45
src/wifi.cpp
45
src/wifi.cpp
@ -23,6 +23,40 @@ void wifiConnect() {
|
|||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t uptimeOffset = 0;
|
||||||
|
|
||||||
|
void uptimeLoop() {
|
||||||
|
const auto now = millis();
|
||||||
|
static auto uptimeLast = 0UL;
|
||||||
|
if (now < uptimeLast) {
|
||||||
|
uptimeOffset += static_cast<uint64_t>(-1);
|
||||||
|
}
|
||||||
|
uptimeLast = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t uptimeMillis() {
|
||||||
|
return uptimeOffset + millis();
|
||||||
|
}
|
||||||
|
|
||||||
|
String uptimeString() {
|
||||||
|
const auto ms = uptimeMillis();
|
||||||
|
const auto days = static_cast<int>(ms / (24 * 60 * 60 * 1000));
|
||||||
|
const auto hours = static_cast<int>(ms / (60 * 60 * 1000) % 24);
|
||||||
|
const auto minutes = static_cast<int>(ms / (60 * 1000) % 60);
|
||||||
|
const auto seconds = static_cast<int>(ms / 1000 % 60);
|
||||||
|
char buffer[32];
|
||||||
|
if (days > 0) {
|
||||||
|
snprintf(buffer, sizeof buffer, "%d day%s, %d hour%s", days, days == 1 ? "" : "s", hours, hours == 1 ? "" : "s");
|
||||||
|
} else if (hours > 0) {
|
||||||
|
snprintf(buffer, sizeof buffer, "%d hour%s, %d minute%s", hours, hours == 1 ? "" : "s", minutes, minutes == 1 ? "" : "s");
|
||||||
|
} else if (minutes > 0) {
|
||||||
|
snprintf(buffer, sizeof buffer, "%d minute%s, %d second%s", minutes, minutes == 1 ? "" : "s", seconds, seconds == 1 ? "" : "s");
|
||||||
|
} else {
|
||||||
|
snprintf(buffer, sizeof buffer, "%d second%s", seconds, seconds == 1 ? "" : "s");
|
||||||
|
}
|
||||||
|
return {buffer};
|
||||||
|
}
|
||||||
|
|
||||||
void timeLoop() {
|
void timeLoop() {
|
||||||
const auto now = time(nullptr);
|
const auto now = time(nullptr);
|
||||||
const auto nowHour = now / 3600;
|
const auto nowHour = now / 3600;
|
||||||
@ -31,12 +65,12 @@ void timeLoop() {
|
|||||||
if (now > 1700000000) {
|
if (now > 1700000000) {
|
||||||
timeSet = true;
|
timeSet = true;
|
||||||
lastHour = nowHour;
|
lastHour = nowHour;
|
||||||
MySerial.printf("Got time: %s\n", getTimeString().c_str());
|
Log.printf("Got time: %s\n", getTimeString().c_str());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (lastHour != nowHour) {
|
if (lastHour != nowHour) {
|
||||||
lastHour = nowHour;
|
lastHour = nowHour;
|
||||||
MySerial.printf("%s\n", getTimeString().c_str());
|
Log.printf("%s\n", getTimeString().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -46,7 +80,7 @@ void wifiLoop() {
|
|||||||
if (WiFi.localIP() == 0UL) {
|
if (WiFi.localIP() == 0UL) {
|
||||||
if (wifiConnected) {
|
if (wifiConnected) {
|
||||||
wifiConnected = false;
|
wifiConnected = false;
|
||||||
MySerial.printf("WiFi disconnected.\n");
|
Log.printf("WiFi disconnected.\n");
|
||||||
wifiConnect();
|
wifiConnect();
|
||||||
} else if (wifiConnectBeginMillis == 0 || millis() - wifiConnectBeginMillis >= WIFI_TIMEOUT_MS) {
|
} else if (wifiConnectBeginMillis == 0 || millis() - wifiConnectBeginMillis >= WIFI_TIMEOUT_MS) {
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
@ -57,10 +91,11 @@ void wifiLoop() {
|
|||||||
if (!wifiConnected) {
|
if (!wifiConnected) {
|
||||||
wifiConnected = true;
|
wifiConnected = true;
|
||||||
wifiConnectBeginMillis = 0;
|
wifiConnectBeginMillis = 0;
|
||||||
MySerial.printf("WiFi connected as \"%s\" (%s)\n", HOSTNAME, WiFi.localIP().toString().c_str());
|
Log.printf("WiFi connected as \"%s\" (%s)\n", HOSTNAME, WiFi.localIP().toString().c_str());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
uptimeLoop();
|
||||||
timeLoop();
|
timeLoop();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String getTimeString() {
|
String getTimeString() {
|
||||||
|
|||||||
@ -13,4 +13,8 @@ bool isWifiConnected();
|
|||||||
|
|
||||||
bool isTimeSet();
|
bool isTimeSet();
|
||||||
|
|
||||||
|
uint64_t uptimeMillis();
|
||||||
|
|
||||||
|
String uptimeString();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user