@@ -81,32 +81,35 @@ http://forum.mysensors.org/topic/303/mqtt-broker-gateway
81
81
82
82
#define TCP_PORT 1883 // Set your MQTT Broker Listening port.
83
83
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!
85
85
86
86
// ////////////////////////////////////////////////////////////////
87
87
88
88
EthernetServer server = EthernetServer(TCP_PORT);
89
+ EthernetClient *currentClient = NULL ;
89
90
MyMQTT gw (RADIO_CE_PIN, RADIO_SPI_SS_PIN);
90
91
91
92
void processEthernetMessages () {
92
93
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 ;
96
97
97
98
EthernetClient client = server.available ();
98
99
if (client) {
99
100
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 ();
101
104
readCnt++;
102
105
103
106
if (inputSize < MQTT_MAX_PACKET_SIZE) {
104
- inputString[inputSize] = (char )inChar ;
107
+ inputString[inputSize] = (char )inByte ;
105
108
inputSize++;
106
109
}
107
110
108
111
if (readCnt == 2 ) {
109
- length = (inChar & 127 ) * 1 ;
112
+ length = (inByte & 127 ) * 1 ;
110
113
}
111
114
if (readCnt == (length+2 )) {
112
115
break ;
@@ -115,19 +118,24 @@ void processEthernetMessages() {
115
118
#ifdef TCPDUMP
116
119
Serial.print (" <<" );
117
120
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 ();
119
122
#endif
120
123
gw.processMQTTMessage (inputString, inputSize);
124
+ currentClient = NULL ;
121
125
}
122
126
}
123
127
124
- void writeEthernet (const char *writeBuffer, uint8_t *writeSize) {
128
+ void writeEthernet (const char *writeBuffer, byte *writeSize) {
125
129
#ifdef TCPDUMP
126
130
Serial.print (" >>" );
127
131
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 ();
129
133
#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);
131
139
}
132
140
133
141
int main (void ) {
0 commit comments