NetMeterbusMaster/MqttClient.cpp

190 lines
5.3 KiB
C++
Raw Normal View History

2015-05-08 23:40:36 +02:00
/*
* MqttClient.cpp
*
* Created on: 08.05.2015
* Author: wn
*/
#include "MqttClient.h"
#include <PubSubClient.h>
2015-05-09 00:08:54 +02:00
#include <PString.h>
#include <Streaming.h>
2015-05-09 16:31:49 +02:00
#include <Metro.h>
2015-05-09 00:08:54 +02:00
byte MQTT_BROKER[] = { 192, 168, 75, 1 };
const uint16_t MQTT_PORT = 1883;
2015-05-08 23:40:36 +02:00
void callback(char* topic, byte* payload, unsigned int length) {
}
2015-05-09 16:31:49 +02:00
Metro secondTick = Metro(1000);
2015-05-08 23:40:36 +02:00
MqttClient::MqttClient(RequestSender *meterBusMaster) :
2015-05-09 20:26:31 +02:00
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(MQTT_BROKER, MQTT_PORT, callback, m_client),
m_disconnectState(3), m_disconnectTime(millis()), m_uptime(0), m_deviceIdx(0)
2015-05-08 23:40:36 +02:00
{
2015-05-09 16:31:49 +02:00
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
2015-05-09 20:26:31 +02:00
m_mbusDevTuple[i] = { 0, 0, 0, 0 };
2015-05-09 16:31:49 +02:00
}
2015-05-09 23:15:45 +02:00
m_mbusDevTuple[0] = { 1, 0x53, 10, 0 }; // light meter
m_mbusDevTuple[1] = { 2, 32, 10, 0 }; // electrity
2015-05-08 23:40:36 +02:00
}
2015-05-09 20:26:31 +02:00
void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLength, uint8_t token) {
2015-05-09 23:15:45 +02:00
char strbuf[1024];
2015-05-08 23:40:36 +02:00
memset(strbuf, sizeof(strbuf), 0);
PString buf = PString(strbuf, sizeof(strbuf));
2015-05-09 18:55:15 +02:00
2015-05-09 23:15:45 +02:00
buf << "{ \"metadata\": { \"device\": \"MeterbusHub\", " <<
2015-05-09 20:26:31 +02:00
"\"token\": " << token << ", " <<
2015-05-09 23:15:45 +02:00
"}, " <<
"\"data\": {" <<
2015-05-09 20:26:31 +02:00
"\"uptime\": " << m_uptime << ", " <<
2015-05-09 18:55:15 +02:00
"\"telegram\": \"";
2015-05-09 20:26:31 +02:00
uint16_t i = 0;
while (true) {
if (responseBuffer[i] <= 0x0f) {
buf.print("0");
}
buf.print(responseBuffer[i], HEX);
buf.print(" ");
i++;
if (i == responseBufferLength) {
break;
}
}
2015-05-09 18:55:15 +02:00
buf << "\"}}";
2015-05-08 23:40:36 +02:00
if (m_disconnectState == 0) {
2015-05-09 23:15:45 +02:00
Serial << "publishing " << strbuf << endl;
Serial << "length: " << buf.length() << endl;
2015-05-08 23:40:36 +02:00
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
2015-05-09 17:45:17 +02:00
} else {
Serial << "no MQTT connection, message lost: " << endl <<
strbuf << endl;
2015-05-08 23:40:36 +02:00
}
}
2015-05-09 20:26:31 +02:00
void MqttClient::sendError(uint8_t code, uint8_t token) {
2015-05-09 23:15:45 +02:00
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\", \"error\": ")
+ code + String(", \"token\": ") + token + String(" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
if (m_disconnectState == 0) {
Serial << "publishing " << msg << endl;
Serial << "length: " << msg.length() << endl;
m_mqttClient.publish("MeterbusHub.Measurement", (char*)msg.c_str());
} else {
Serial << "no MQTT connection, message lost: " << msg << endl;
}
2015-05-08 23:40:36 +02:00
}
void MqttClient::begin() {
}
void MqttClient::exec() {
2015-05-09 23:15:45 +02:00
//Serial << "*** a" << endl;
2015-05-08 23:40:36 +02:00
if ((m_disconnectState == 0) && (! m_mqttClient.loop())) {
m_disconnectState = 1;
}
2015-05-09 23:15:45 +02:00
//Serial << "*** b, " << m_disconnectState << endl;
2015-05-08 23:40:36 +02:00
switch (m_disconnectState) {
case 0:
// Serial.println("discState 0");
// everything fine
break;
case 1:
2015-05-09 17:45:17 +02:00
Serial.println("MQTT Connection lost");
2015-05-08 23:40:36 +02:00
m_mqttClient.disconnect();
m_disconnectTime = millis();
m_disconnectState = 2;
break;
case 2:
if (m_disconnectTime + 2000 < millis()) {
m_disconnectState = 3;
}
break;
case 3:
2015-05-09 23:15:45 +02:00
Serial << "Trying to re-connect" << endl;
2015-05-08 23:40:36 +02:00
if (m_mqttClient.connect("MeterbusHub")) {
2015-05-09 00:08:54 +02:00
Serial << "MQTT connected" << endl;
2015-05-08 23:40:36 +02:00
m_disconnectTime = millis();
m_disconnectState = 0;
} else {
m_disconnectState = 1;
}
break;
default:
m_disconnectState = 0;
break;
}
2015-05-09 23:15:45 +02:00
//Serial << "*** c" << endl;
2015-05-09 16:31:49 +02:00
if (secondTick.check() == 1) {
2015-05-09 17:45:17 +02:00
m_uptime++;
2015-05-09 23:15:45 +02:00
Serial << "Tick " << m_uptime << endl;
2015-05-09 17:45:17 +02:00
2015-05-09 23:15:45 +02:00
// 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;
// }
2015-05-09 17:45:17 +02:00
2015-05-09 16:31:49 +02:00
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
2015-05-09 20:26:31 +02:00
if ((m_mbusDevTuple[i].address != 0) && (m_mbusDevTuple[i].timer != 0)) {
m_mbusDevTuple[i].timer -= 1;
if (m_mbusDevTuple[i].timer == 0) {
Serial << "Device " << m_mbusDevTuple[i].token << " ready for request" << endl;
} else {
Serial << "Device " << m_mbusDevTuple[i].token << " not ready for request, timer: " << m_mbusDevTuple[i].timer << endl;
}
}
}
2015-05-09 18:55:15 +02:00
2015-05-09 23:15:45 +02:00
//Serial << "while in" << endl;
while (true) {
2015-05-09 20:26:31 +02:00
if ((m_mbusDevTuple[m_deviceIdx].address != 0) && (m_mbusDevTuple[m_deviceIdx].timer == 0)) {
Serial << "Issue request for device " << m_mbusDevTuple[m_deviceIdx].token << endl;
2015-05-09 18:55:15 +02:00
uint8_t *sendBuffer = m_meterBusMaster->getSendBuffer();
2015-05-09 20:26:31 +02:00
if (sendBuffer != 0) {
Serial << "send buffer ready" << endl;
2015-05-09 23:15:45 +02:00
m_mbusDevTuple[m_deviceIdx].timer = m_mbusDevTuple[m_deviceIdx].queryPeriod;
2015-05-09 20:26:31 +02:00
sendBuffer[0] = 0x10;
sendBuffer[1] = 0x5b;
sendBuffer[2] = m_mbusDevTuple[m_deviceIdx].address;
sendBuffer[3] = (uint8_t)(sendBuffer[1] + sendBuffer[2]);
sendBuffer[4] = 0x16;
m_meterBusMaster->sendBufferReady(5, m_mbusDevTuple[m_deviceIdx].token, this);
} else {
Serial << "no send buffer ready" << endl;
}
2015-05-09 23:15:45 +02:00
break;
} else {
Serial << "Trying " << m_deviceIdx << ", " << m_mbusDevTuple[m_deviceIdx].token << endl;
m_deviceIdx++;
if (m_deviceIdx >= NUM_OF_DEVICES) {
m_deviceIdx = 0;
}
break;
2015-05-09 16:31:49 +02:00
}
}
2015-05-09 23:15:45 +02:00
//Serial << "while out" << endl;
2015-05-09 16:31:49 +02:00
}
2015-05-09 23:15:45 +02:00
//Serial << "*** d" << endl;
2015-05-09 16:31:49 +02:00
2015-05-08 23:40:36 +02:00
}