add MQTT stuff
This commit is contained in:
99
MqttClient.cpp
Normal file
99
MqttClient.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* MqttClient.cpp
|
||||
*
|
||||
* Created on: 08.05.2015
|
||||
* Author: wn
|
||||
*/
|
||||
|
||||
#include "MqttClient.h"
|
||||
|
||||
#include <PubSubClient.h>
|
||||
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
// handle message arrived
|
||||
}
|
||||
|
||||
|
||||
MqttClient::MqttClient(RequestSender *meterBusMaster) :
|
||||
m_client(255), m_meterBusMaster(meterBusMaster), m_mqttClient(MQTT_BROKER, MQTT_PORT, callback, m_client),
|
||||
m_disconnectState(3), m_disconnectTime(millis())
|
||||
{
|
||||
}
|
||||
|
||||
void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLength) {
|
||||
char strbuf[256];
|
||||
memset(strbuf, sizeof(strbuf), 0);
|
||||
PString buf = PString(strbuf, sizeof(strbuf));
|
||||
buf << "{ \"metadata\": { \"device\": \"MeterbusHub\" }, " <<
|
||||
"\"data\": {" <<
|
||||
"}" <<
|
||||
"}" << endl;
|
||||
if (m_disconnectState == 0) {
|
||||
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
|
||||
}
|
||||
|
||||
// m_server.write(responseBuffer, responseBufferLength);
|
||||
}
|
||||
|
||||
void MqttClient::sendError(uint8_t code) {
|
||||
}
|
||||
|
||||
void MqttClient::begin() {
|
||||
}
|
||||
|
||||
void MqttClient::exec() {
|
||||
if ((m_disconnectState == 0) && (! m_mqttClient.loop())) {
|
||||
m_disconnectState = 1;
|
||||
}
|
||||
|
||||
switch (m_disconnectState) {
|
||||
case 0:
|
||||
// Serial.println("discState 0");
|
||||
// everything fine
|
||||
break;
|
||||
case 1:
|
||||
Serial.println("discState 1");
|
||||
m_mqttClient.disconnect();
|
||||
m_disconnectTime = millis();
|
||||
m_disconnectState = 2;
|
||||
break;
|
||||
case 2:
|
||||
Serial.println("discState 3");
|
||||
if (m_disconnectTime + 2000 < millis()) {
|
||||
m_disconnectState = 3;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
Serial.println("discState 3");
|
||||
if (m_mqttClient.connect("MeterbusHub")) {
|
||||
m_disconnectTime = millis();
|
||||
m_disconnectState = 0;
|
||||
} else {
|
||||
m_disconnectState = 1;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
m_disconnectState = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// m_client = m_server.available();
|
||||
// if (m_client) {
|
||||
// uint16_t sendBufLen = 0;
|
||||
// uint8_t *sendBuffer = m_meterBusMaster->getSendBuffer();
|
||||
// if (sendBuffer != 0) {
|
||||
// int chi;
|
||||
// while ((chi = m_client.read()) != -1) {
|
||||
// char ch = (char) chi;
|
||||
// *(sendBuffer + sendBufLen) = ch;
|
||||
// sendBufLen++;
|
||||
// }
|
||||
// m_meterBusMaster->sendBufferReady(sendBufLen, this);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user