update to start with millis reporting

This commit is contained in:
2019-11-27 19:39:06 +01:00
parent c367091adf
commit 629e5017ac
4 changed files with 98 additions and 34 deletions

View File

@@ -1,23 +1,13 @@
#ifdef ENABLE_MQTT
// sample schema's to use: https://www.home-assistant.io/integrations/light.mqtt/
/*
* Define a function newcmd in your module to be able to accept mqtt commands
* Syntax and parameters are like this:
*/
void newcmd(String cmd);
void module_message(const String& shorttopic, const String& message);
#include <PubSubClient.h>
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
PubSubClient mqtt(net);
// Setup the MQTT client class by passing in the WiFi client
PubSubClient mqtt(net);
String deviceprefix;
const char* fingerprint = "EA:2A:2B:80:C1:00:57:35:66:26:FF:FC:FA:27:EA:5F:3D:91:5A:F9";
long logtimerMillis = 0;
void mqtt_subscribe(const String& topic) {
@@ -76,24 +66,37 @@ void mqtt_callback(char* topic, byte* payload, unsigned int length) {
}
void mqtt_connect() {
/* bool success= false;
success = net.connect(MQTT_SERVER, MQTT_SERVERPORT);
if (success) {
PRINTLN_SERIAL("Connection complete, valid cert, valid fingerprint.");
} else {
PRINTLN_SERIAL("Connection failed!");
} */
PRINTLN_SERIAL("prepare mqtt connect check net.status: " + String(net.status()));
mqtt.setServer(MQTT_SERVER, MQTT_SERVERPORT);
mqtt.setCallback(mqtt_callback);
if(!mqtt.connected()) {
while (!mqtt.connected()) {
PRINTLN_SERIAL("MQTT not connected, connecting...");
// Attempt to connect
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this,
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
// PubSubClient.h
if (mqtt.connect(settings.name.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) {
PRINTLN_SERIAL("MQTT connected");
} else {
PRINTLN_SERIAL("MQTT connection failed, rc=" + String(mqtt.state()) + "...");
delay(10000); // @@@FIXME@@@ delays are not cool
}
PRINTLN_SERIAL("before mqtt connect check net.status: " + String(net.status()));
while (!mqtt.connected()) {
PRINTLN_SERIAL("MQTT not connected, connecting...");
PRINTLN_SERIAL("while mqtt connecting check net.status: " + String(net.status()));
// Attempt to connect
// Note - the default maximum packet size is 128 bytes. If the
// combined length of clientId, username and password exceed this,
// you will need to increase the value of MQTT_MAX_PACKET_SIZE in
// PubSubClient.h
if (mqtt.connect(settings.name.c_str(), MQTT_USERNAME, MQTT_PASSWORD)) {
PRINTLN_SERIAL("MQTT connected");
PRINTLN_SERIAL("mqtt just connected check net.status: " + String(net.status()));
} else {
PRINTLN_SERIAL("MQTT connection failed, rc=" + String(mqtt.state()) + "...");
PRINTLN_SERIAL("mqtt failed connected check net.status: " + String(net.status()));
delay(10000); // @@@FIXME@@@ delays are not cool
}
// (re)subscribe to the topics we need
@@ -104,6 +107,7 @@ void mqtt_connect() {
mqtt_subscribe(deviceprefix + "#");
mqtt_publish(deviceprefix + "log", "Connected " + settings.name + " running v" + FW_VERSION + " at " + settings.ip);
mqtt_publish(deviceprefix + "log", "Free Heap: " + String(ESP.getFreeHeap()));
PRINTLN_SERIAL("after mqtt connect check net.status: " + String(net.status()));
}
}