Skip to content

Commit c83d8d6

Browse files
committed
Merge pull request arduino#83 from mapnull/hotfix-mqtt_openhab
fix multiple mqtt clients with openhab
2 parents 285f5d7 + 403bd1e commit c83d8d6

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

libraries/MySensors/examples/MQTTGateway/MQTTGateway.ino

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,32 +81,35 @@ http://forum.mysensors.org/topic/303/mqtt-broker-gateway
8181

8282
#define TCP_PORT 1883 // Set your MQTT Broker Listening port.
8383
IPAddress TCP_IP ( 192, 168, 0, 234 ); // Configure your static ip-address here
84-
uint8_t TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - You should change this! see note *2 above!
84+
byte TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - You should change this! see note *2 above!
8585

8686
//////////////////////////////////////////////////////////////////
8787

8888
EthernetServer server = EthernetServer(TCP_PORT);
89+
EthernetClient *currentClient = NULL;
8990
MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
9091

9192
void processEthernetMessages() {
9293
char inputString[MQTT_MAX_PACKET_SIZE] = "";
93-
uint8_t inputSize = 0;
94-
uint8_t readCnt = 0;
95-
uint8_t length = 0;
94+
byte inputSize = 0;
95+
byte readCnt = 0;
96+
byte length = 0;
9697

9798
EthernetClient client = server.available();
9899
if (client) {
99100
while (client.available()) {
100-
uint8_t inChar = client.read();
101+
// Save the current client we are talking with
102+
currentClient = &client;
103+
byte inByte = client.read();
101104
readCnt++;
102105

103106
if (inputSize < MQTT_MAX_PACKET_SIZE) {
104-
inputString[inputSize] = (char)inChar;
107+
inputString[inputSize] = (char)inByte;
105108
inputSize++;
106109
}
107110

108111
if (readCnt == 2) {
109-
length = (inChar & 127) * 1;
112+
length = (inByte & 127) * 1;
110113
}
111114
if (readCnt == (length+2)) {
112115
break;
@@ -115,19 +118,24 @@ void processEthernetMessages() {
115118
#ifdef TCPDUMP
116119
Serial.print("<<");
117120
char buf[4];
118-
for (uint8_t a=0; a<inputSize; a++) { sprintf(buf, "%02X ", (uint8_t)inputString[a]); Serial.print(buf); } Serial.println();
121+
for (byte a=0; a<inputSize; a++) { sprintf(buf, "%02X ", (byte)inputString[a]); Serial.print(buf); } Serial.println();
119122
#endif
120123
gw.processMQTTMessage(inputString, inputSize);
124+
currentClient = NULL;
121125
}
122126
}
123127

124-
void writeEthernet(const char *writeBuffer, uint8_t *writeSize) {
128+
void writeEthernet(const char *writeBuffer, byte *writeSize) {
125129
#ifdef TCPDUMP
126130
Serial.print(">>");
127131
char buf[4];
128-
for (uint8_t a=0; a<*writeSize; a++) { sprintf(buf,"%02X ",(uint8_t)writeBuffer[a]); Serial.print(buf); } Serial.println();
132+
for (byte a=0; a<*writeSize; a++) { sprintf(buf,"%02X ",(byte)writeBuffer[a]); Serial.print(buf); } Serial.println();
129133
#endif
130-
server.write((const uint8_t *)writeBuffer, *writeSize);
134+
// Check whether to respond to a single client or write to all
135+
if (currentClient != NULL)
136+
currentClient->write((const byte *)writeBuffer, *writeSize);
137+
else
138+
server.write((const byte *)writeBuffer, *writeSize);
131139
}
132140

133141
int main(void) {

0 commit comments

Comments
 (0)