@@ -66,31 +66,39 @@ char S_50[] PROGMEM = "Sketch_version"; // Does not exist in mysensors lib!
66
66
67
67
68
68
PROGMEM const char *sType [] =
69
- { S_0, S_1, S_2, S_3, S_4, S_5, S_6, S_7, S_8, S_9, S_10,
69
+ { S_0, S_1, S_2, S_3, S_4, S_5, S_6, S_7, S_8, S_9, S_10,
70
70
S_11, S_12, S_13, S_14, S_15, S_16, S_17, S_18, S_19, S_20,
71
- S_21, S_22, S_23, S_24, S_25, S_26, S_27, S_28, S_29, S_30,
71
+ S_21, S_22, S_23, S_24, S_25, S_26, S_27, S_28, S_29, S_30,
72
72
S_31, S_32, S_33, S_34, S_35, S_36, S_37, S_38, S_39, S_40,
73
73
S_41, S_42, S_43, S_44, S_45, S_46, S_47, S_48, S_49, S_50};
74
74
75
-
76
- MyMQTT::MyMQTT (uint8_t _cepin, uint8_t _cspin) :
75
+
76
+ MyMQTT::MyMQTT (uint8_t _cepin, uint8_t _cspin, uint8_t _rx, uint8_t _tx, uint8_t _er ) :
77
77
MySensor(_cepin, _cspin) {
78
+ if (_rx != NULL ) {
79
+ pinRx = _rx;
80
+ pinMode (pinRx, OUTPUT);
81
+ ledMode = true ;
82
+ }
83
+ if (_tx != NULL ) {
84
+ pinTx = _tx;
85
+ pinMode (pinTx, OUTPUT);
86
+ ledMode = true ;
87
+ }
88
+ if (_er != NULL ) {
89
+ pinEr = _er;
90
+ pinMode (pinEr, OUTPUT);
91
+ ledMode = true ;
92
+ }
78
93
}
79
94
80
95
void MyMQTT::begin (rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate, void (*inDataCallback)(char *, int *)) {
81
96
Serial.begin (BAUD_RATE);
82
97
repeaterMode = true ;
83
98
isGateway = true ;
84
- MQTTClient = false ;
99
+ MQTTClientConnected = false ;
85
100
setupRepeaterMode ();
86
-
87
- if (inDataCallback != NULL ) {
88
- useWriteCallback = true ;
89
- dataCallback = inDataCallback;
90
- }
91
- else {
92
- useWriteCallback = false ;
93
- }
101
+ dataCallback = inDataCallback;
94
102
95
103
nc.nodeId = 0 ;
96
104
nc.distance = 0 ;
@@ -110,6 +118,7 @@ void MyMQTT::processRadioMessage() {
110
118
// A new message was received from one of the sensors
111
119
MyMessage message = getLastMessage ();
112
120
// Pass along the message from sensors to serial line
121
+ rxBlink (3 );
113
122
SendMQTT (message);
114
123
}
115
124
@@ -121,7 +130,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
121
130
buffer[0 ]= 0 ;
122
131
buffsize = 0 ;
123
132
#ifdef TCPDUMP
124
- Serial.print (" <<" );
133
+ Serial.print (" <<" );
125
134
char buf[4 ];
126
135
for (int a=0 ; a<inputPos; a++) { sprintf (buf," %02X " ,(byte)inputString[a]); Serial.print (buf); } Serial.println (" " );
127
136
#endif
@@ -130,7 +139,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
130
139
buffer[buffsize++] = 0x02 ; // Remaining length
131
140
buffer[buffsize++] = 0x00 ; // Connection accepted
132
141
buffer[buffsize++] = 0x00 ; // Reserved
133
- MQTTClient =true ;
142
+ MQTTClientConnected =true ;
134
143
}
135
144
if ((byte)inputString[0 ] >> 4 == MQTTPINGREQ) {
136
145
buffer[buffsize++] = MQTTPINGRESP << 4 ;
@@ -150,18 +159,18 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
150
159
buffer[buffsize++] = (byte)inputString[3 ]; // Message ID LSB
151
160
}
152
161
if ((byte)inputString[0 ] >> 4 == MQTTDISCONNECT) {
153
- MQTTClient =false ;
162
+ MQTTClientConnected =false ;
154
163
}
155
164
if (buffsize > 0 ) {
156
165
#ifdef TCPDUMP
157
- Serial.print (" >>" );
166
+ Serial.print (" >>" );
158
167
char buf[4 ];
159
- for (int a=0 ; a<buffsize; a++) { sprintf (buf," %02X " ,(byte)buffer[a]); Serial.print (buf); } Serial.println (" " );
168
+ for (int a=0 ; a<buffsize; a++) { sprintf (buf," %02X " ,(byte)buffer[a]); Serial.print (buf); } Serial.println (" " );
160
169
#endif
161
170
dataCallback (buffer,&buffsize);
162
171
}
163
172
// We publish everything we get, we dont care if its subscribed or not!
164
- if ((byte)inputString[0 ] >> 4 == MQTTPUBLISH || (MQTT_SEND_SUBSCRIPTION && (byte)inputString[0 ] >> 4 == MQTTSUBSCRIBE)) {
173
+ if ((byte)inputString[0 ] >> 4 == MQTTPUBLISH || (MQTT_SEND_SUBSCRIPTION && (byte)inputString[0 ] >> 4 == MQTTSUBSCRIBE)) {
165
174
buffer[0 ]= 0 ;
166
175
buffsize = 0 ;
167
176
if ((byte)inputString[0 ] >> 4 == MQTTSUBSCRIBE) {
@@ -171,7 +180,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
171
180
}
172
181
msg.sender = GATEWAY_ADDRESS;
173
182
for (str = strtok_r (buffer, " /" , &p); // split using semicolon
174
- str && i < 4 ; str = strtok_r (NULL , " /" , &p) // get subsequent tokens (?)
183
+ str && i < 4 ; str = strtok_r (NULL , " /" , &p) // get subsequent tokens (?)
175
184
) {
176
185
switch (i) {
177
186
case 0 : // Radioid (destination)
@@ -188,11 +197,11 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
188
197
case 3 :
189
198
char match=0 ;
190
199
for (int j=0 ; strcpy_P (convBuf, (char *)pgm_read_word (&(sType [j]))) ; j++) {
191
- if (strcmp (str,convBuf)==0 ) {
192
- match=j;
200
+ if (strcmp (str,convBuf)==0 ) {
201
+ match=j;
193
202
break ;
194
203
}
195
- }
204
+ }
196
205
msg.type = match;
197
206
break ;
198
207
}
@@ -201,27 +210,28 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
201
210
if ((char )inputString[1 ] > (char )(inputString[3 ]+2 ) && !((byte)inputString[0 ] >> 4 == MQTTSUBSCRIBE)) {
202
211
strcpy (convBuf,inputString+(inputString[3 ]+4 ));
203
212
msg.set (convBuf);
204
- } else {
213
+ } else {
205
214
msg.set (" " );
206
215
}
207
216
msg.sender = GATEWAY_ADDRESS;
208
217
mSetCommand (msg,C_SET);
209
218
mSetRequestAck (msg,false );
210
219
mSetAck (msg,false );
211
220
mSetVersion (msg, PROTOCOL_VERSION);
212
- sendRoute (msg);
221
+ txBlink (1 );
222
+ if (!sendRoute (msg)) errBlink (1 );
213
223
}
214
224
}
215
225
216
226
217
227
void MyMQTT::SendMQTT (MyMessage &msg) {
218
228
// serial(PSTR("%d;%d;%d;%d;%s\n"),msg.sender, msg.sensor, mGetCommand(msg), msg.type, msg.getString(convBuf));
219
229
buffsize = 0 ;
220
- if (!MQTTClient ) return ;
230
+ if (!MQTTClientConnected ) return ;
221
231
if (msg.isAck ()) {
222
232
Serial.println (" msg is ack!" );
223
233
if (msg.sender ==255 && mGetCommand (msg)==C_INTERNAL && msg.type ==I_ID_REQUEST) {
224
- // TODO: sending ACK request on id_response fucks node up. doesn't work.
234
+ // TODO: sending ACK request on id_response fucks node up. doesn't work.
225
235
// The idea was to confirm id and save to EEPROM_LATEST_NODE_ADDRESS.
226
236
}
227
237
} else {
@@ -233,11 +243,11 @@ Serial.println("msg is ack!");
233
243
saveState (EEPROM_LATEST_NODE_ADDRESS,newNodeID);
234
244
}
235
245
if (mGetCommand (msg)==C_INTERNAL && msg.type ==I_CONFIG) { // CONFIG
236
- // As for now there is only one 'config' request.
246
+ // As for now there is only one 'config' request.
237
247
// We force SI! Resistance is futile!
238
248
//
239
- // Todo : Support for more config types, Maybe just read from own EEPROM?
240
- // Use internal EEPROM_CONTROLLER_CONFIG_ADDRESS and special MQTT address to write to
249
+ // Todo : Support for more config types, Maybe just read from own EEPROM?
250
+ // Use internal EEPROM_CONTROLLER_CONFIG_ADDRESS and special MQTT address to write to
241
251
// EEPROM_CONTROLLER_CONFIG_ADDRESS & EEPROM_LOCAL_CONFIG_ADDRESS
242
252
msg.destination = msg.sender ; // NodeID
243
253
msg.sender = GATEWAY_ADDRESS;
@@ -248,7 +258,8 @@ Serial.println("msg is ack!");
248
258
mSetAck (msg,false );
249
259
msg.set (" M" );
250
260
mSetVersion (msg, PROTOCOL_VERSION);
251
- sendRoute (msg);
261
+ txBlink (1 );
262
+ if (!sendRoute (msg)) errBlink (1 );
252
263
} else if (mGetCommand (msg)==C_PRESENTATION && (msg.type ==S_ARDUINO_NODE || msg.type ==S_ARDUINO_REPEATER_NODE)) {
253
264
// Doesnt work to check new sensorID here, this message does not always arrive.. See above.
254
265
} else if (msg.sender ==255 && mGetCommand (msg)==C_INTERNAL && msg.type ==I_ID_REQUEST) {
@@ -264,11 +275,12 @@ Serial.println("msg is ack!");
264
275
mSetAck (msg,false );
265
276
msg.set ((uint8_t )newNodeID); // Payload
266
277
mSetVersion (msg, PROTOCOL_VERSION);
267
- sendRoute (msg);
278
+ txBlink (1 );
279
+ if (!sendRoute (msg)) errBlink (1 );
268
280
// if (sendRoute(msg)) saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID); // If send OK save to eeprom. DOES NOT ALWAYS RETURN 'OK'!?
269
281
} else if (mGetCommand (msg)!=0 ) {
270
282
if (mGetCommand (msg)==3 ) msg.type =msg.type +38 ;
271
- buffer[buffsize++] = MQTTPUBLISH << 4 ; // 0:
283
+ buffer[buffsize++] = MQTTPUBLISH << 4 ; // 0:
272
284
buffer[buffsize++] = 0x09 ; // 1: Remaining length with no payload, we'll set this later to correct value, buffsize -2
273
285
buffer[buffsize++] = 0x00 ; // 2: Length MSB (Remaing length can never exceed ff,so MSB must be 0!)
274
286
buffer[buffsize++] = 0x08 ; // 3: Length LSB (ADDR), We'll set this later
@@ -289,17 +301,59 @@ Serial.println("msg is ack!");
289
301
#ifdef TCPDUMP
290
302
Serial.print (" >>" );
291
303
char buf[4 ];
292
- for (int a=0 ; a<buffsize; a++) { sprintf (buf," %02X " ,(byte)buffer[a]); Serial.print (buf); } Serial.println (" " );
304
+ for (int a=0 ; a<buffsize; a++) { sprintf (buf," %02X " ,(byte)buffer[a]); Serial.print (buf); } Serial.println (" " );
293
305
#endif
294
306
dataCallback (buffer,&buffsize);
295
307
}
296
- }
308
+ }
309
+ }
310
+
311
+ boolean MyMQTT::isLedMode () {
312
+ return ledMode;
313
+ }
314
+
315
+ void MyMQTT::ledTimersInterrupt () {
316
+ if (countRx && countRx != 255 ) {
317
+ // switch led on
318
+ digitalWrite (pinRx, LOW);
319
+ } else if (!countRx) {
320
+ // switching off
321
+ digitalWrite (pinRx, HIGH);
322
+ }
323
+ if (countRx != 255 ) { countRx--; }
324
+
325
+ if (countTx && countTx != 255 ) {
326
+ // switch led on
327
+ digitalWrite (pinTx, LOW);
328
+ } else if (!countTx) {
329
+ // switching off
330
+ digitalWrite (pinTx, HIGH);
331
+ }
332
+ if (countTx != 255 ) { countTx--; }
333
+ if (countErr && countErr != 255 ) {
334
+ // switch led on
335
+ digitalWrite (pinEr, LOW);
336
+ } else if (!countErr) {
337
+ // switching off
338
+ digitalWrite (pinEr, HIGH);
339
+ }
340
+ if (countErr != 255 ) { countErr--; }
341
+ }
342
+
343
+ void MyMQTT::rxBlink (uint8_t cnt) {
344
+ if (countRx == 255 ) { countRx = cnt; }
345
+ }
346
+ void MyMQTT::txBlink (uint8_t cnt) {
347
+ if (countTx == 255 ) { countTx = cnt; }
348
+ }
349
+ void MyMQTT::errBlink (uint8_t cnt) {
350
+ if (countErr == 255 ) { countErr = cnt; }
297
351
}
298
352
299
353
char MyMQTT::strncpysType_retL (char *str, char index, char start) {
300
354
char c;
301
355
char l;
302
- char *p = (char *)pgm_read_word (&(sType [index]));
356
+ char *p = (char *)pgm_read_word (&(sType [index]));
303
357
str+=start;
304
358
while ((c = pgm_read_byte (p))) {
305
359
*str=c;
0 commit comments