works better now
This commit is contained in:
parent
79cc72826c
commit
424d9d2a6e
@ -272,7 +272,8 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
|||||||
uint8_t digit;
|
uint8_t digit;
|
||||||
uint8_t pos = 0;
|
uint8_t pos = 0;
|
||||||
uint8_t rc;
|
uint8_t rc;
|
||||||
uint8_t len = length;
|
uint16_t len = length;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
digit = len % 128;
|
digit = len % 128;
|
||||||
len = len / 128;
|
len = len / 128;
|
||||||
@ -294,12 +295,12 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
|||||||
|
|
||||||
const size_t SEND_AT_ONCE = 64;
|
const size_t SEND_AT_ONCE = 64;
|
||||||
size_t remains = length + 1 + llen;
|
size_t remains = length + 1 + llen;
|
||||||
// Serial.print("write len: "); Serial.println(remains);
|
//Serial.print("write len: "); Serial.println(remains);
|
||||||
const uint8_t *writebuf = buf + (4 - llen);
|
const uint8_t *writebuf = buf + (4 - llen);
|
||||||
bool result = true;
|
bool result = true;
|
||||||
while ((remains > 0) && result) {
|
while ((remains > 0) && result) {
|
||||||
size_t actuallySendChars = (remains > SEND_AT_ONCE) ? SEND_AT_ONCE : remains;
|
size_t actuallySendChars = (remains > SEND_AT_ONCE) ? SEND_AT_ONCE : remains;
|
||||||
// Serial.print("tbs: "); Serial.println(actuallySendChars);
|
//Serial.print("tbs: "); Serial.println(actuallySendChars);
|
||||||
size_t sentChars = _client->write(writebuf, actuallySendChars);
|
size_t sentChars = _client->write(writebuf, actuallySendChars);
|
||||||
result = sentChars == actuallySendChars;
|
result = sentChars == actuallySendChars;
|
||||||
remains -= sentChars;
|
remains -= sentChars;
|
||||||
@ -309,6 +310,7 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
|||||||
|
|
||||||
lastOutActivity = millis();
|
lastOutActivity = millis();
|
||||||
// return (rc == 1+llen+length);
|
// return (rc == 1+llen+length);
|
||||||
|
//Serial.print("res: "); Serial.println(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,8 +65,8 @@ void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLe
|
|||||||
buf << "\"}}";
|
buf << "\"}}";
|
||||||
|
|
||||||
if (m_disconnectState == 0) {
|
if (m_disconnectState == 0) {
|
||||||
Serial << "publishing " << strbuf << endl;
|
//Serial << "publishing " << strbuf << endl;
|
||||||
Serial << "length: " << buf.length() << endl;
|
//Serial << "length: " << buf.length() << endl;
|
||||||
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
|
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
|
||||||
} else {
|
} else {
|
||||||
Serial << "no MQTT connection, message lost: " << endl <<
|
Serial << "no MQTT connection, message lost: " << endl <<
|
||||||
@ -78,8 +78,8 @@ void MqttClient::sendError(uint8_t code, uint8_t token) {
|
|||||||
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\", \"error\": ")
|
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\", \"error\": ")
|
||||||
+ code + String(", \"token\": ") + token + String(" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
|
+ code + String(", \"token\": ") + token + String(" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
|
||||||
if (m_disconnectState == 0) {
|
if (m_disconnectState == 0) {
|
||||||
Serial << "publishing " << msg << endl;
|
//Serial << "publishing " << msg << endl;
|
||||||
Serial << "length: " << msg.length() << endl;
|
//Serial << "length: " << msg.length() << endl;
|
||||||
m_mqttClient.publish("MeterbusHub.Measurement", (char*)msg.c_str());
|
m_mqttClient.publish("MeterbusHub.Measurement", (char*)msg.c_str());
|
||||||
} else {
|
} else {
|
||||||
Serial << "no MQTT connection, message lost: " << msg << endl;
|
Serial << "no MQTT connection, message lost: " << msg << endl;
|
||||||
@ -130,7 +130,7 @@ void MqttClient::exec() {
|
|||||||
//Serial << "*** c" << endl;
|
//Serial << "*** c" << endl;
|
||||||
if (secondTick.check() == 1) {
|
if (secondTick.check() == 1) {
|
||||||
m_uptime++;
|
m_uptime++;
|
||||||
Serial << "Tick " << m_uptime << endl;
|
//Serial << "Tick " << m_uptime << endl;
|
||||||
|
|
||||||
// String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
|
// String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
|
||||||
// if (m_disconnectState == 0) {
|
// if (m_disconnectState == 0) {
|
||||||
@ -142,11 +142,11 @@ void MqttClient::exec() {
|
|||||||
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
||||||
if ((m_mbusDevTuple[i].address != 0) && (m_mbusDevTuple[i].timer != 0)) {
|
if ((m_mbusDevTuple[i].address != 0) && (m_mbusDevTuple[i].timer != 0)) {
|
||||||
m_mbusDevTuple[i].timer -= 1;
|
m_mbusDevTuple[i].timer -= 1;
|
||||||
if (m_mbusDevTuple[i].timer == 0) {
|
// if (m_mbusDevTuple[i].timer == 0) {
|
||||||
Serial << "Device " << m_mbusDevTuple[i].token << " ready for request" << endl;
|
// Serial << "Device " << m_mbusDevTuple[i].token << " ready for request" << endl;
|
||||||
} else {
|
// } else {
|
||||||
Serial << "Device " << m_mbusDevTuple[i].token << " not ready for request, timer: " << m_mbusDevTuple[i].timer << endl;
|
// Serial << "Device " << m_mbusDevTuple[i].token << " not ready for request, timer: " << m_mbusDevTuple[i].timer << endl;
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,7 +170,7 @@ void MqttClient::exec() {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
Serial << "Trying " << m_deviceIdx << ", " << m_mbusDevTuple[m_deviceIdx].token << endl;
|
//Serial << "Trying " << m_deviceIdx << ", " << m_mbusDevTuple[m_deviceIdx].token << endl;
|
||||||
m_deviceIdx++;
|
m_deviceIdx++;
|
||||||
if (m_deviceIdx >= NUM_OF_DEVICES) {
|
if (m_deviceIdx >= NUM_OF_DEVICES) {
|
||||||
m_deviceIdx = 0;
|
m_deviceIdx = 0;
|
||||||
|
@ -169,13 +169,13 @@ void MeterBusMaster::sendBufferReady(uint16_t sendBufLen, uint8_t token, Respons
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MeterBusMaster::prepareResponse(bool err, uint8_t in) {
|
void MeterBusMaster::prepareResponse(bool err, uint8_t in) {
|
||||||
Serial << "resp in, err: " << err << endl;
|
//Serial << "resp in, err: " << err << endl;
|
||||||
static int16_t expectedChars = -1;
|
static int16_t expectedChars = -1;
|
||||||
static uint8_t state = 0;
|
static uint8_t state = 0;
|
||||||
|
|
||||||
Serial << "r1" << endl;
|
//Serial << "r1" << endl;
|
||||||
if (err) {
|
if (err) {
|
||||||
Serial << "r1" << endl;
|
//Serial << "r1" << endl;
|
||||||
if (m_responseCallback != 0) {
|
if (m_responseCallback != 0) {
|
||||||
m_responseCallback->sendError(1, m_token);
|
m_responseCallback->sendError(1, m_token);
|
||||||
}
|
}
|
||||||
@ -184,10 +184,10 @@ void MeterBusMaster::prepareResponse(bool err, uint8_t in) {
|
|||||||
m_expectResponse = false;
|
m_expectResponse = false;
|
||||||
m_responseCallback = 0;
|
m_responseCallback = 0;
|
||||||
} else {
|
} else {
|
||||||
Serial << "r2" << endl;
|
//Serial << "r2" << endl;
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case 0:
|
case 0:
|
||||||
Serial << "r3" << endl;
|
//Serial << "r3" << endl;
|
||||||
m_recvBufLen = 0;
|
m_recvBufLen = 0;
|
||||||
if (in == 0xe5) {
|
if (in == 0xe5) {
|
||||||
state = 2;
|
state = 2;
|
||||||
@ -201,29 +201,29 @@ void MeterBusMaster::prepareResponse(bool err, uint8_t in) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
Serial << "r4" << endl;
|
//Serial << "r4" << endl;
|
||||||
expectedChars = (int16_t)in;
|
expectedChars = (int16_t)in;
|
||||||
expectedChars += 4;
|
expectedChars += 4;
|
||||||
state = 2;
|
state = 2;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
Serial << "r5" << endl;
|
//Serial << "r5" << endl;
|
||||||
expectedChars--;
|
expectedChars--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_recvBufLen >= RECEIVE_BUFFER_SIZE) {
|
if (m_recvBufLen >= RECEIVE_BUFFER_SIZE) {
|
||||||
Serial << "r6, " << m_recvBufLen << ", " << RECEIVE_BUFFER_SIZE << endl;
|
//Serial << "r6, " << m_recvBufLen << ", " << RECEIVE_BUFFER_SIZE << endl;
|
||||||
fatal(FATAL_BUFFER_OVERFLOW + 1);
|
fatal(FATAL_BUFFER_OVERFLOW + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial << "r7" << endl;
|
//Serial << "r7" << endl;
|
||||||
m_recvBuffer[m_recvBufLen] = in;
|
m_recvBuffer[m_recvBufLen] = in;
|
||||||
m_recvBufLen++;
|
m_recvBufLen++;
|
||||||
|
|
||||||
Serial << "r8" << endl;
|
//Serial << "r8" << endl;
|
||||||
if (expectedChars == 0) {
|
if (expectedChars == 0) {
|
||||||
Serial << "r9" << endl;
|
//Serial << "r9" << endl;
|
||||||
if (m_responseCallback != 0) {
|
if (m_responseCallback != 0) {
|
||||||
m_responseCallback->sendResponse(m_recvBuffer, m_recvBufLen, m_token);
|
m_responseCallback->sendResponse(m_recvBuffer, m_recvBufLen, m_token);
|
||||||
}
|
}
|
||||||
@ -234,7 +234,7 @@ void MeterBusMaster::prepareResponse(bool err, uint8_t in) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Serial << "resp out" << endl;
|
//Serial << "resp out" << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeterBusMaster::sample() {
|
void MeterBusMaster::sample() {
|
||||||
@ -257,7 +257,7 @@ void MeterBusMaster::exec() {
|
|||||||
|
|
||||||
if (m_cmdReadyToSend) {
|
if (m_cmdReadyToSend) {
|
||||||
sample();
|
sample();
|
||||||
Serial << "MeterBusMaster: sending " << m_sendBufLen << " octets." << endl;
|
//Serial << "MeterBusMaster: sending " << m_sendBufLen << " octets." << endl;
|
||||||
Serial3.write(m_sendBuffer, m_sendBufLen);
|
Serial3.write(m_sendBuffer, m_sendBufLen);
|
||||||
Serial3.flush();
|
Serial3.flush();
|
||||||
hold();
|
hold();
|
||||||
@ -283,7 +283,7 @@ void MeterBusMaster::exec() {
|
|||||||
|
|
||||||
int serialInChar = Serial3.read();
|
int serialInChar = Serial3.read();
|
||||||
if (serialInChar != -1) {
|
if (serialInChar != -1) {
|
||||||
Serial << "Got: " << _HEX(serialInChar) << endl;
|
//Serial << "Got: " << _HEX(serialInChar) << endl;
|
||||||
}
|
}
|
||||||
if ((serialInChar != -1) && m_expectResponse) {
|
if ((serialInChar != -1) && m_expectResponse) {
|
||||||
prepareResponse(false, (uint8_t)serialInChar);
|
prepareResponse(false, (uint8_t)serialInChar);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user