Skip to content

Commit 31813b3

Browse files
committed
Merge pull request arduino#21 from Damme/patch-1
Minor changes + Added back LED-blinking code.
2 parents 32a47d6 + 519d652 commit 31813b3

File tree

3 files changed

+140
-57
lines changed

3 files changed

+140
-57
lines changed

MQTTGateway/MQTTGateway.ino

100644100755
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
Created by Daniel Wiegert <[email protected]>
44
Based on Mysensors Ethernet Gateway by Henrik Ekblad <[email protected]>
55
Requires MySensors lib 1.4b
6-
http://www.mysensors.org
7-
8-
* Don't forget to look at the definitions in MyMQTT.h!
6+
http://www.mysensors.org
7+
8+
* Don't forget to look at the definitions in MyMQTT.h!
99
* Don't forget to configure Radio pins, IP and MAC-address!
10-
10+
1111
* Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/Light
1212
NodeID and SensorID is number (0-255).
13-
Last segment is translation of the sensor type, look inside MyMQTT.cpp for
13+
Last segment is translation of the sensor type, look inside MyMQTT.cpp for
1414
the definitions. User can change this to their needs.
1515
1616
Example openhab setup: http://www.openhab.org/
@@ -57,21 +57,31 @@ String sketch21 "Node 21 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:M
5757
Read and set EEPROM Values
5858
Send Reboot, And reboot gateway itself.
5959
...
60+
61+
Sketch uses 23,666 bytes (77%) of program storage space. Maximum is 30,720 bytes.
62+
Global variables use 766 bytes (37%) of dynamic memory, leaving 1,282 bytes for local variables. Maximum is 2,048 bytes.
6063
*/
6164

6265
#include <SPI.h>
6366
#include <MyMQTT.h>
6467
#include <Ethernet.h>
68+
#include <MsTimer2.h>
6569

6670

6771
// Use this for IBOARD modded to use standard MISO/MOSI/SCK More information see;
6872
// http://forum.mysensors.org/topic/224/iboard-cheap-single-board-ethernet-arduino-with-radio/5
69-
#define RADIO_CE_PIN 3 // radio chip enable
70-
#define RADIO_SPI_SS_PIN 8 // radio SPI serial select
73+
//#define RADIO_CE_PIN 3 // radio chip enable
74+
//#define RADIO_SPI_SS_PIN 8 // radio SPI serial select
75+
//#define RADIO_ERROR_LED_PIN A2 // Error led pin
76+
//#define RADIO_RX_LED_PIN A1 // Receive led pin
77+
//#define RADIO_TX_LED_PIN A0 // the PCB, on board LED
7178

7279
//Use this for default configured pro mini / nano etc :
73-
//#define RADIO_CE_PIN 5 // radio chip enable
74-
//#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
80+
#define RADIO_CE_PIN 5 // radio chip enable
81+
#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
82+
#define RADIO_ERROR_LED_PIN 7 // Error led pin
83+
#define RADIO_RX_LED_PIN 8 // Receive led pin
84+
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED
7585

7686
#define IP_PORT 1883 // MQTT Listening port
7787
IPAddress myIp ( 192, 168, 0, 234 ); // Configure your static ip-address here
@@ -80,6 +90,8 @@ byte mac[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - Change th
8090

8191
EthernetServer server = EthernetServer(IP_PORT);
8292
MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
93+
// Uncomment this constructor if you have leds and include button attached to your gateway
94+
// MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
8395

8496
void processEthernetMessages() {
8597
char inputString[MQTT_MAX_PACKET_SIZE] = "";
@@ -99,16 +111,23 @@ void writeEthernet(char *writeBuffer, int *writeSize) {
99111
server.write(writeBuffer, *writeSize); // Should this really be *writeSize?
100112
}
101113

114+
void ledTimersInterrupt() {
115+
gw.ledTimersInterrupt();
116+
}
117+
102118
int main(void) {
103119
init();
104120
Ethernet.begin(mac, myIp);
105121
delay(1000); // Wait for Ethernet to get configured.
106122
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
107123
server.begin();
124+
if (gw.isLedMode()) {
125+
// Add led timer interrupt
126+
MsTimer2::set(300, ledTimersInterrupt);
127+
MsTimer2::start();
128+
}
108129
while (1) {
109130
processEthernetMessages();
110131
gw.processRadioMessage();
111132
}
112133
}
113-
114-

libraries/MySensors/MyMQTT.cpp

100644100755
Lines changed: 90 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -66,31 +66,39 @@ char S_50[] PROGMEM = "Sketch_version"; // Does not exist in mysensors lib!
6666

6767

6868
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,
7070
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,
7272
S_31, S_32, S_33, S_34, S_35, S_36, S_37, S_38, S_39, S_40,
7373
S_41, S_42, S_43, S_44, S_45, S_46, S_47, S_48, S_49, S_50};
7474

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) :
7777
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+
}
7893
}
7994

8095
void MyMQTT::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate, void (*inDataCallback)(char *, int *)) {
8196
Serial.begin(BAUD_RATE);
8297
repeaterMode = true;
8398
isGateway = true;
84-
MQTTClient = false;
99+
MQTTClientConnected = false;
85100
setupRepeaterMode();
86-
87-
if (inDataCallback != NULL) {
88-
useWriteCallback = true;
89-
dataCallback = inDataCallback;
90-
}
91-
else {
92-
useWriteCallback = false;
93-
}
101+
dataCallback = inDataCallback;
94102

95103
nc.nodeId = 0;
96104
nc.distance = 0;
@@ -110,6 +118,7 @@ void MyMQTT::processRadioMessage() {
110118
// A new message was received from one of the sensors
111119
MyMessage message = getLastMessage();
112120
// Pass along the message from sensors to serial line
121+
rxBlink(3);
113122
SendMQTT(message);
114123
}
115124

@@ -121,7 +130,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
121130
buffer[0]= 0;
122131
buffsize = 0;
123132
#ifdef TCPDUMP
124-
Serial.print("<<");
133+
Serial.print("<<");
125134
char buf[4];
126135
for (int a=0; a<inputPos; a++) { sprintf(buf,"%02X ",(byte)inputString[a]); Serial.print(buf); } Serial.println("");
127136
#endif
@@ -130,7 +139,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
130139
buffer[buffsize++] = 0x02; // Remaining length
131140
buffer[buffsize++] = 0x00; // Connection accepted
132141
buffer[buffsize++] = 0x00; // Reserved
133-
MQTTClient=true;
142+
MQTTClientConnected=true;
134143
}
135144
if ((byte)inputString[0] >> 4 == MQTTPINGREQ) {
136145
buffer[buffsize++] = MQTTPINGRESP << 4;
@@ -150,18 +159,18 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
150159
buffer[buffsize++] = (byte)inputString[3]; // Message ID LSB
151160
}
152161
if ((byte)inputString[0] >> 4 == MQTTDISCONNECT) {
153-
MQTTClient=false;
162+
MQTTClientConnected=false;
154163
}
155164
if (buffsize > 0) {
156165
#ifdef TCPDUMP
157-
Serial.print(">>");
166+
Serial.print(">>");
158167
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("");
160169
#endif
161170
dataCallback(buffer,&buffsize);
162171
}
163172
// 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)) {
165174
buffer[0]= 0;
166175
buffsize = 0;
167176
if ((byte)inputString[0] >> 4 == MQTTSUBSCRIBE) {
@@ -171,7 +180,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
171180
}
172181
msg.sender = GATEWAY_ADDRESS;
173182
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 (?)
175184
) {
176185
switch (i) {
177186
case 0: // Radioid (destination)
@@ -188,11 +197,11 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
188197
case 3:
189198
char match=0;
190199
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;
193202
break;
194203
}
195-
}
204+
}
196205
msg.type = match;
197206
break;
198207
}
@@ -201,27 +210,28 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
201210
if ((char)inputString[1] > (char)(inputString[3]+2) && !((byte)inputString[0] >> 4 == MQTTSUBSCRIBE)) {
202211
strcpy(convBuf,inputString+(inputString[3]+4));
203212
msg.set(convBuf);
204-
} else {
213+
} else {
205214
msg.set("");
206215
}
207216
msg.sender = GATEWAY_ADDRESS;
208217
mSetCommand(msg,C_SET);
209218
mSetRequestAck(msg,false);
210219
mSetAck(msg,false);
211220
mSetVersion(msg, PROTOCOL_VERSION);
212-
sendRoute(msg);
221+
txBlink(1);
222+
if (!sendRoute(msg)) errBlink(1);
213223
}
214224
}
215225

216226

217227
void MyMQTT::SendMQTT(MyMessage &msg) {
218228
//serial(PSTR("%d;%d;%d;%d;%s\n"),msg.sender, msg.sensor, mGetCommand(msg), msg.type, msg.getString(convBuf));
219229
buffsize = 0;
220-
if (!MQTTClient) return;
230+
if (!MQTTClientConnected) return;
221231
if (msg.isAck()) {
222232
Serial.println("msg is ack!");
223233
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.
225235
// The idea was to confirm id and save to EEPROM_LATEST_NODE_ADDRESS.
226236
}
227237
} else {
@@ -233,11 +243,11 @@ Serial.println("msg is ack!");
233243
saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID);
234244
}
235245
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.
237247
// We force SI! Resistance is futile!
238248
//
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
241251
// EEPROM_CONTROLLER_CONFIG_ADDRESS & EEPROM_LOCAL_CONFIG_ADDRESS
242252
msg.destination = msg.sender; //NodeID
243253
msg.sender = GATEWAY_ADDRESS;
@@ -248,7 +258,8 @@ Serial.println("msg is ack!");
248258
mSetAck(msg,false);
249259
msg.set("M");
250260
mSetVersion(msg, PROTOCOL_VERSION);
251-
sendRoute(msg);
261+
txBlink(1);
262+
if (!sendRoute(msg)) errBlink(1);
252263
} else if (mGetCommand(msg)==C_PRESENTATION && (msg.type==S_ARDUINO_NODE || msg.type==S_ARDUINO_REPEATER_NODE)) {
253264
//Doesnt work to check new sensorID here, this message does not always arrive.. See above.
254265
} else if (msg.sender==255 && mGetCommand(msg)==C_INTERNAL && msg.type==I_ID_REQUEST) {
@@ -264,11 +275,12 @@ Serial.println("msg is ack!");
264275
mSetAck(msg,false);
265276
msg.set((uint8_t)newNodeID); //Payload
266277
mSetVersion(msg, PROTOCOL_VERSION);
267-
sendRoute(msg);
278+
txBlink(1);
279+
if (!sendRoute(msg)) errBlink(1);
268280
// if (sendRoute(msg)) saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID); // If send OK save to eeprom. DOES NOT ALWAYS RETURN 'OK'!?
269281
} else if (mGetCommand(msg)!=0) {
270282
if (mGetCommand(msg)==3) msg.type=msg.type+38;
271-
buffer[buffsize++] = MQTTPUBLISH << 4; // 0:
283+
buffer[buffsize++] = MQTTPUBLISH << 4; // 0:
272284
buffer[buffsize++] = 0x09; // 1: Remaining length with no payload, we'll set this later to correct value, buffsize -2
273285
buffer[buffsize++] = 0x00; // 2: Length MSB (Remaing length can never exceed ff,so MSB must be 0!)
274286
buffer[buffsize++] = 0x08; // 3: Length LSB (ADDR), We'll set this later
@@ -289,17 +301,59 @@ Serial.println("msg is ack!");
289301
#ifdef TCPDUMP
290302
Serial.print(">>");
291303
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("");
293305
#endif
294306
dataCallback(buffer,&buffsize);
295307
}
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; }
297351
}
298352

299353
char MyMQTT::strncpysType_retL(char *str, char index, char start) {
300354
char c;
301355
char l;
302-
char *p = (char *)pgm_read_word(&(sType[index]));
356+
char *p = (char *)pgm_read_word(&(sType[index]));
303357
str+=start;
304358
while ((c = pgm_read_byte(p))) {
305359
*str=c;

0 commit comments

Comments
 (0)