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 pos = 0;
|
||||
uint8_t rc;
|
||||
uint8_t len = length;
|
||||
uint16_t len = length;
|
||||
|
||||
do {
|
||||
digit = 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;
|
||||
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);
|
||||
bool result = true;
|
||||
while ((remains > 0) && result) {
|
||||
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);
|
||||
result = sentChars == actuallySendChars;
|
||||
remains -= sentChars;
|
||||
@ -309,6 +310,7 @@ boolean PubSubClient::write(uint8_t header, uint8_t* buf, uint16_t length) {
|
||||
|
||||
lastOutActivity = millis();
|
||||
// return (rc == 1+llen+length);
|
||||
//Serial.print("res: "); Serial.println(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -65,8 +65,8 @@ void MqttClient::sendResponse(uint8_t *responseBuffer, uint16_t responseBufferLe
|
||||
buf << "\"}}";
|
||||
|
||||
if (m_disconnectState == 0) {
|
||||
Serial << "publishing " << strbuf << endl;
|
||||
Serial << "length: " << buf.length() << endl;
|
||||
//Serial << "publishing " << strbuf << endl;
|
||||
//Serial << "length: " << buf.length() << endl;
|
||||
m_mqttClient.publish("MeterbusHub.Measurement", strbuf);
|
||||
} else {
|
||||
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\": ")
|
||||
+ code + String(", \"token\": ") + token + String(" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
|
||||
if (m_disconnectState == 0) {
|
||||
Serial << "publishing " << msg << endl;
|
||||
Serial << "length: " << msg.length() << endl;
|
||||
//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;
|
||||
@ -130,7 +130,7 @@ void MqttClient::exec() {
|
||||
//Serial << "*** c" << endl;
|
||||
if (secondTick.check() == 1) {
|
||||
m_uptime++;
|
||||
Serial << "Tick " << m_uptime << endl;
|
||||
//Serial << "Tick " << m_uptime << endl;
|
||||
|
||||
// String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String("}}");
|
||||
// if (m_disconnectState == 0) {
|
||||
@ -142,11 +142,11 @@ void MqttClient::exec() {
|
||||
for (uint8_t i = 0; i < NUM_OF_DEVICES; i++) {
|
||||
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;
|
||||
}
|
||||
// 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;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@ -170,7 +170,7 @@ void MqttClient::exec() {
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
Serial << "Trying " << m_deviceIdx << ", " << m_mbusDevTuple[m_deviceIdx].token << endl;
|
||||
//Serial << "Trying " << m_deviceIdx << ", " << m_mbusDevTuple[m_deviceIdx].token << endl;
|
||||
m_deviceIdx++;
|
||||
if (m_deviceIdx >= NUM_OF_DEVICES) {
|
||||
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) {
|
||||
Serial << "resp in, err: " << err << endl;
|
||||
//Serial << "resp in, err: " << err << endl;
|
||||
static int16_t expectedChars = -1;
|
||||
static uint8_t state = 0;
|
||||
|
||||
Serial << "r1" << endl;
|
||||
//Serial << "r1" << endl;
|
||||
if (err) {
|
||||
Serial << "r1" << endl;
|
||||
//Serial << "r1" << endl;
|
||||
if (m_responseCallback != 0) {
|
||||
m_responseCallback->sendError(1, m_token);
|
||||
}
|
||||
@ -184,10 +184,10 @@ void MeterBusMaster::prepareResponse(bool err, uint8_t in) {
|
||||
m_expectResponse = false;
|
||||
m_responseCallback = 0;
|
||||
} else {
|
||||
Serial << "r2" << endl;
|
||||
//Serial << "r2" << endl;
|
||||
switch (state) {
|
||||
case 0:
|
||||
Serial << "r3" << endl;
|
||||
//Serial << "r3" << endl;
|
||||
m_recvBufLen = 0;
|
||||
if (in == 0xe5) {
|
||||
state = 2;
|
||||
@ -201,29 +201,29 @@ void MeterBusMaster::prepareResponse(bool err, uint8_t in) {
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
Serial << "r4" << endl;
|
||||
//Serial << "r4" << endl;
|
||||
expectedChars = (int16_t)in;
|
||||
expectedChars += 4;
|
||||
state = 2;
|
||||
break;
|
||||
case 2:
|
||||
Serial << "r5" << endl;
|
||||
//Serial << "r5" << endl;
|
||||
expectedChars--;
|
||||
break;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
Serial << "r7" << endl;
|
||||
//Serial << "r7" << endl;
|
||||
m_recvBuffer[m_recvBufLen] = in;
|
||||
m_recvBufLen++;
|
||||
|
||||
Serial << "r8" << endl;
|
||||
//Serial << "r8" << endl;
|
||||
if (expectedChars == 0) {
|
||||
Serial << "r9" << endl;
|
||||
//Serial << "r9" << endl;
|
||||
if (m_responseCallback != 0) {
|
||||
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() {
|
||||
@ -257,7 +257,7 @@ void MeterBusMaster::exec() {
|
||||
|
||||
if (m_cmdReadyToSend) {
|
||||
sample();
|
||||
Serial << "MeterBusMaster: sending " << m_sendBufLen << " octets." << endl;
|
||||
//Serial << "MeterBusMaster: sending " << m_sendBufLen << " octets." << endl;
|
||||
Serial3.write(m_sendBuffer, m_sendBufLen);
|
||||
Serial3.flush();
|
||||
hold();
|
||||
@ -283,7 +283,7 @@ void MeterBusMaster::exec() {
|
||||
|
||||
int serialInChar = Serial3.read();
|
||||
if (serialInChar != -1) {
|
||||
Serial << "Got: " << _HEX(serialInChar) << endl;
|
||||
//Serial << "Got: " << _HEX(serialInChar) << endl;
|
||||
}
|
||||
if ((serialInChar != -1) && m_expectResponse) {
|
||||
prepareResponse(false, (uint8_t)serialInChar);
|
||||
|
Loading…
x
Reference in New Issue
Block a user