heartbeat
This commit is contained in:
parent
558711bb2b
commit
8608583a58
@ -31,8 +31,8 @@ Metro secondTick = Metro(1000);
|
|||||||
|
|
||||||
|
|
||||||
MqttClient::MqttClient(RequestSender *meterBusMaster) :
|
MqttClient::MqttClient(RequestSender *meterBusMaster) :
|
||||||
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(MQTT_BROKER, MQTT_PORT, callback, m_client),
|
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(MQTT_BROKER, MQTT_PORT, callback, m_client),
|
||||||
m_disconnectState(3), m_disconnectTime(millis())
|
m_disconnectState(3), m_disconnectTime(millis()), m_uptime(0)
|
||||||
{
|
{
|
||||||
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
||||||
m_mbusDevTuple[i].address = 0;
|
m_mbusDevTuple[i].address = 0;
|
||||||
@ -51,10 +51,14 @@ void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLe
|
|||||||
PString buf = PString(strbuf, sizeof(strbuf));
|
PString buf = PString(strbuf, sizeof(strbuf));
|
||||||
buf << "{ \"metadata\": { \"device\": \"MeterbusHub\" }, " <<
|
buf << "{ \"metadata\": { \"device\": \"MeterbusHub\" }, " <<
|
||||||
"\"data\": {" <<
|
"\"data\": {" <<
|
||||||
|
"\"uptime\": " << m_uptime <<
|
||||||
"}" <<
|
"}" <<
|
||||||
"}" << endl;
|
"}" << endl;
|
||||||
if (m_disconnectState == 0) {
|
if (m_disconnectState == 0) {
|
||||||
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
|
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
|
||||||
|
} else {
|
||||||
|
Serial << "no MQTT connection, message lost: " << endl <<
|
||||||
|
strbuf << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// m_server.write(responseBuffer, responseBufferLength);
|
// m_server.write(responseBuffer, responseBufferLength);
|
||||||
@ -77,19 +81,17 @@ void MqttClient::exec() {
|
|||||||
// everything fine
|
// everything fine
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
Serial.println("discState 1");
|
Serial.println("MQTT Connection lost");
|
||||||
m_mqttClient.disconnect();
|
m_mqttClient.disconnect();
|
||||||
m_disconnectTime = millis();
|
m_disconnectTime = millis();
|
||||||
m_disconnectState = 2;
|
m_disconnectState = 2;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
Serial.println("discState 3");
|
|
||||||
if (m_disconnectTime + 2000 < millis()) {
|
if (m_disconnectTime + 2000 < millis()) {
|
||||||
m_disconnectState = 3;
|
m_disconnectState = 3;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
Serial.println("discState 3");
|
|
||||||
if (m_mqttClient.connect("MeterbusHub")) {
|
if (m_mqttClient.connect("MeterbusHub")) {
|
||||||
Serial << "MQTT connected" << endl;
|
Serial << "MQTT connected" << endl;
|
||||||
m_mqttClient.subscribe("MeterbusHub.Configuration");
|
m_mqttClient.subscribe("MeterbusHub.Configuration");
|
||||||
@ -106,6 +108,16 @@ void MqttClient::exec() {
|
|||||||
|
|
||||||
|
|
||||||
if (secondTick.check() == 1) {
|
if (secondTick.check() == 1) {
|
||||||
|
m_uptime++;
|
||||||
|
|
||||||
|
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
|
||||||
|
if (m_disconnectState == 0) {
|
||||||
|
m_mqttClient.publish("MeterbusHub.Heartbeat", (char*)msg.c_str());
|
||||||
|
} else {
|
||||||
|
Serial << "no MQTT connection, message lost: " << msg << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
||||||
m_mbusDevTuple[i].timer -= 1;
|
m_mbusDevTuple[i].timer -= 1;
|
||||||
if (m_mbusDevTuple[i].timer == 0) {
|
if (m_mbusDevTuple[i].timer == 0) {
|
||||||
@ -117,20 +129,20 @@ void MqttClient::exec() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// m_client = m_server.available();
|
// m_client = m_server.available();
|
||||||
// if (m_client) {
|
// if (m_client) {
|
||||||
// uint16_t sendBufLen = 0;
|
// uint16_t sendBufLen = 0;
|
||||||
// uint8_t *sendBuffer = m_meterBusMaster->getSendBuffer();
|
// uint8_t *sendBuffer = m_meterBusMaster->getSendBuffer();
|
||||||
// if (sendBuffer != 0) {
|
// if (sendBuffer != 0) {
|
||||||
// int chi;
|
// int chi;
|
||||||
// while ((chi = m_client.read()) != -1) {
|
// while ((chi = m_client.read()) != -1) {
|
||||||
// char ch = (char) chi;
|
// char ch = (char) chi;
|
||||||
// *(sendBuffer + sendBufLen) = ch;
|
// *(sendBuffer + sendBufLen) = ch;
|
||||||
// sendBufLen++;
|
// sendBufLen++;
|
||||||
// }
|
// }
|
||||||
// m_meterBusMaster->sendBufferReady(sendBufLen, this);
|
// m_meterBusMaster->sendBufferReady(sendBufLen, this);
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@ private:
|
|||||||
PubSubClient m_mqttClient;
|
PubSubClient m_mqttClient;
|
||||||
uint8_t m_disconnectState;
|
uint8_t m_disconnectState;
|
||||||
uint32_t m_disconnectTime;
|
uint32_t m_disconnectTime;
|
||||||
|
uint32_t m_uptime;
|
||||||
mbusDevTuple_t m_mbusDevTuple[NUM_OF_DEVICES];
|
mbusDevTuple_t m_mbusDevTuple[NUM_OF_DEVICES];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user