long message does not work

This commit is contained in:
hg
2015-05-09 23:15:45 +02:00
parent f9df2439a8
commit 79cc72826c
5 changed files with 67 additions and 49 deletions

View File

@ -18,12 +18,6 @@ const uint16_t MQTT_PORT = 1883;
void callback(char* topic, byte* payload, unsigned int length) {
char strbuf[256];
memset(strbuf, sizeof(strbuf), 0);
memcpy(strbuf, payload, length);
Serial << "MQTT Message received: " << topic << ", " << strbuf << endl;
// handle message arrived
}
@ -38,18 +32,20 @@ MqttClient::MqttClient(RequestSender *meterBusMaster) :
m_mbusDevTuple[i] = { 0, 0, 0, 0 };
}
m_mbusDevTuple[0] = { 1, 0x53, 60, 0 }; // light meter
m_mbusDevTuple[0] = { 1, 0x53, 10, 0 }; // light meter
m_mbusDevTuple[1] = { 2, 32, 10, 0 }; // electrity
}
void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLength, uint8_t token) {
char strbuf[256];
char strbuf[1024];
memset(strbuf, sizeof(strbuf), 0);
PString buf = PString(strbuf, sizeof(strbuf));
buf << "{ \"metadata\": { \"device\": \"MeterbusHub\" }, " <<
"\"data\": {" <<
buf << "{ \"metadata\": { \"device\": \"MeterbusHub\", " <<
"\"token\": " << token << ", " <<
"}, " <<
"\"data\": {" <<
"\"uptime\": " << m_uptime << ", " <<
"\"telegram\": \"";
@ -69,26 +65,37 @@ void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLe
buf << "\"}}";
if (m_disconnectState == 0) {
Serial << "publishing " << strbuf << endl;
Serial << "length: " << buf.length() << endl;
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
} else {
Serial << "no MQTT connection, message lost: " << endl <<
strbuf << endl;
}
// m_server.write(responseBuffer, responseBufferLength);
}
void MqttClient::sendError(uint8_t code, uint8_t token) {
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;
}
}
void MqttClient::begin() {
}
void MqttClient::exec() {
//Serial << "*** a" << endl;
if ((m_disconnectState == 0) && (! m_mqttClient.loop())) {
m_disconnectState = 1;
}
//Serial << "*** b, " << m_disconnectState << endl;
switch (m_disconnectState) {
case 0:
// Serial.println("discState 0");
@ -106,9 +113,9 @@ void MqttClient::exec() {
}
break;
case 3:
Serial << "Trying to re-connect" << endl;
if (m_mqttClient.connect("MeterbusHub")) {
Serial << "MQTT connected" << endl;
m_mqttClient.subscribe("MeterbusHub.Configuration");
m_disconnectTime = millis();
m_disconnectState = 0;
} else {
@ -120,16 +127,17 @@ void MqttClient::exec() {
break;
}
//Serial << "*** c" << endl;
if (secondTick.check() == 1) {
m_uptime++;
Serial << "Tick " << m_uptime << endl;
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;
}
// 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++) {
if ((m_mbusDevTuple[i].address != 0) && (m_mbusDevTuple[i].timer != 0)) {
@ -142,13 +150,14 @@ void MqttClient::exec() {
}
}
while (m_deviceIdx < NUM_OF_DEVICES) {
//Serial << "while in" << endl;
while (true) {
if ((m_mbusDevTuple[m_deviceIdx].address != 0) && (m_mbusDevTuple[m_deviceIdx].timer == 0)) {
m_mbusDevTuple[m_deviceIdx].timer = m_mbusDevTuple[m_deviceIdx].queryPeriod;
Serial << "Issue request for device " << m_mbusDevTuple[m_deviceIdx].token << endl;
uint8_t *sendBuffer = m_meterBusMaster->getSendBuffer();
if (sendBuffer != 0) {
Serial << "send buffer ready" << endl;
m_mbusDevTuple[m_deviceIdx].timer = m_mbusDevTuple[m_deviceIdx].queryPeriod;
sendBuffer[0] = 0x10;
sendBuffer[1] = 0x5b;
@ -156,34 +165,24 @@ void MqttClient::exec() {
sendBuffer[3] = (uint8_t)(sendBuffer[1] + sendBuffer[2]);
sendBuffer[4] = 0x16;
m_meterBusMaster->sendBufferReady(5, m_mbusDevTuple[m_deviceIdx].token, this);
break;
} else {
Serial << "no send buffer ready" << endl;
}
break;
} else {
Serial << "Trying " << m_deviceIdx << ", " << m_mbusDevTuple[m_deviceIdx].token << endl;
m_deviceIdx++;
if (m_deviceIdx >= NUM_OF_DEVICES) {
m_deviceIdx = 0;
}
break;
}
m_deviceIdx++;
}
if (m_deviceIdx >= NUM_OF_DEVICES) {
m_deviceIdx = 0;
}
//Serial << "while out" << endl;
}
//Serial << "*** d" << endl;
// 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);
// }
// }
}