Skip to content

Commit e01dc2e

Browse files
committed
Fix a few warnings in MQTTGateway.
Move all gateway examples to "examples" to work better with codebender and Arduino IDE. Ledmode is always enabled for gateway. Still optional to connect the wires though ;) Move MsTimer to local utility folder (no external dependencies).
1 parent 85d6daf commit e01dc2e

File tree

6 files changed

+151
-135
lines changed

6 files changed

+151
-135
lines changed

libraries/MySensors/MyMQTT.cpp

Lines changed: 30 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ version 2 as published by the Free Software Foundation.
1010
*/
1111

1212
#include "MyMQTT.h"
13+
#include "utility/MsTimer2.h"
14+
15+
1316

1417
char broker[] PROGMEM = MQTT_BROKER_PREFIX;
1518

@@ -75,26 +78,19 @@ PROGMEM const char *sType[] = {
7578
};
7679

7780

78-
MyMQTT::MyMQTT(uint8_t _cepin, uint8_t _cspin, uint8_t _rx, uint8_t _tx, uint8_t _er) :
81+
extern volatile uint8_t countRx;
82+
extern volatile uint8_t countTx;
83+
extern volatile uint8_t countErr;
84+
extern uint8_t pinRx;
85+
extern uint8_t pinTx;
86+
extern uint8_t pinEr;
87+
88+
MyMQTT::MyMQTT(uint8_t _cepin, uint8_t _cspin) :
7989
MySensor(_cepin, _cspin) {
80-
if (_rx != NULL) {
81-
pinRx = _rx;
82-
pinMode(pinRx, OUTPUT);
83-
ledMode = true;
84-
}
85-
if (_tx != NULL) {
86-
pinTx = _tx;
87-
pinMode(pinTx, OUTPUT);
88-
ledMode = true;
89-
}
90-
if (_er != NULL) {
91-
pinEr = _er;
92-
pinMode(pinEr, OUTPUT);
93-
ledMode = true;
94-
}
90+
9591
}
9692

97-
void MyMQTT::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate, void (*inDataCallback)(char *, int *)) {
93+
void MyMQTT::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate, void (*inDataCallback)(const char *, int *), uint8_t _rx, uint8_t _tx, uint8_t _er) {
9894
Serial.begin(BAUD_RATE);
9995
repeaterMode = true;
10096
isGateway = true;
@@ -110,6 +106,18 @@ void MyMQTT::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataR
110106
RF24::openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
111107
RF24::openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
112108
RF24::startListening();
109+
110+
pinRx = _rx;
111+
pinMode(pinRx, OUTPUT);
112+
pinTx = _tx;
113+
pinMode(pinTx, OUTPUT);
114+
pinEr = _er;
115+
pinMode(pinEr, OUTPUT);
116+
117+
MsTimer2::set(200, ledTimersInterrupt);
118+
MsTimer2::start();
119+
120+
113121
// Send startup log message on serial
114122
//Serial.print(PSTR("Started\n"));//TODO: progmem gives error..? error: sType causes a section type conflict with __c
115123
Serial.print("Started\n");//TODO: fix this...
@@ -127,7 +135,7 @@ void MyMQTT::processRadioMessage() {
127135
}
128136

129137
void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
130-
char *str, *p, *value=NULL;
138+
char *str, *p;
131139
char i = 0;
132140
buffer[0]= 0;
133141
buffsize = 0;
@@ -233,7 +241,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
233241
} else {
234242
// we have to check every message if its a newly assigned id or not.
235243
// Ack on I_ID_RESPONSE does not work, and checking on C_PRESENTATION isn't reliable.
236-
char newNodeID = loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
244+
uint8_t newNodeID = loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
237245
if (newNodeID <= MQTT_FIRST_SENSORID) newNodeID = MQTT_FIRST_SENSORID;
238246
if (msg.sender==newNodeID) {
239247
saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID);
@@ -259,7 +267,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
259267
} else if (mGetCommand(msg)==C_PRESENTATION && (msg.type==S_ARDUINO_NODE || msg.type==S_ARDUINO_REPEATER_NODE)) {
260268
//Doesnt work to check new sensorID here, this message does not always arrive.. See above.
261269
} else if (msg.sender==255 && mGetCommand(msg)==C_INTERNAL && msg.type==I_ID_REQUEST) {
262-
char newNodeID = loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
270+
unsigned char newNodeID = loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
263271
if (newNodeID <= MQTT_FIRST_SENSORID) newNodeID = MQTT_FIRST_SENSORID;
264272
if (newNodeID >= MQTT_LAST_SENSORID) return; // Sorry no more id's left :(
265273
msg.destination = msg.sender; //NodeID
@@ -289,7 +297,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
289297
Serial.println((char*)&buffer[4]);
290298
#endif
291299
msg.getString(convBuf);
292-
for (int a=0; a<strlen(convBuf); a++) { // Payload
300+
for (unsigned int a=0; a<strlen(convBuf); a++) { // Payload
293301
buffer[buffsize++] = convBuf[a];
294302
}
295303
buffer[1]=buffsize-2; // Set correct Remaining length on byte 2.
@@ -304,38 +312,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
304312
}
305313
}
306314

307-
boolean MyMQTT::isLedMode() {
308-
return ledMode;
309-
}
310-
311-
void MyMQTT::ledTimersInterrupt() {
312-
if(countRx && countRx != 255) {
313-
// switch led on
314-
digitalWrite(pinRx, LOW);
315-
} else if(!countRx) {
316-
// switching off
317-
digitalWrite(pinRx, HIGH);
318-
}
319-
if(countRx != 255) { countRx--; }
320315

321-
if(countTx && countTx != 255) {
322-
// switch led on
323-
digitalWrite(pinTx, LOW);
324-
} else if(!countTx) {
325-
// switching off
326-
digitalWrite(pinTx, HIGH);
327-
}
328-
if(countTx != 255) { countTx--; }
329-
330-
if(countErr && countErr != 255) {
331-
// switch led on
332-
digitalWrite(pinEr, LOW);
333-
} else if(!countErr) {
334-
// switching off
335-
digitalWrite(pinEr, HIGH);
336-
}
337-
if(countErr != 255) { countErr--; }
338-
}
339316

340317
void MyMQTT::rxBlink(uint8_t cnt) {
341318
if(countRx == 255) { countRx = cnt; }
@@ -347,7 +324,7 @@ void MyMQTT::errBlink(uint8_t cnt) {
347324
if(countErr == 255) { countErr = cnt; }
348325
}
349326

350-
char MyMQTT::strncpysType_retL(char *str, char index, char start) {
327+
char MyMQTT::strncpysType_retL(char *str, unsigned char index, char start) {
351328
char c;
352329
char l;
353330
char *p = (char *)pgm_read_word(&(sType[index]));

libraries/MySensors/MyMQTT.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ version 2 as published by the Free Software Foundation.
3030

3131
///////////////////////////////////////////////////////////////////////////////////
3232

33-
#define EEPROM_LATEST_NODE_ADDRESS EEPROM_LOCAL_CONFIG_ADDRESS+0
33+
#define EEPROM_LATEST_NODE_ADDRESS ((uint8_t)EEPROM_LOCAL_CONFIG_ADDRESS)
3434
#define MQTT_MAX_PACKET_SIZE 100
3535

3636
#define MQTTPROTOCOLVERSION 3
@@ -57,35 +57,28 @@ version 2 as published by the Free Software Foundation.
5757
class MyMQTT :
5858
public MySensor {
5959
public:
60-
MyMQTT(uint8_t _cepin=5, uint8_t _cspin=6, uint8_t _rx=NULL, uint8_t _tx=NULL, uint8_t _er=NULL);
61-
void begin(rf24_pa_dbm_e paLevel=RF24_PA_LEVEL_GW, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE, void (*dataCallback)(char *, int *)=NULL);
60+
MyMQTT(uint8_t _cepin=5, uint8_t _cspin=6);
61+
void begin(rf24_pa_dbm_e paLevel=RF24_PA_LEVEL_GW, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE, void (*dataCallback)(const char *, int *)=NULL, uint8_t _rx=6, uint8_t _tx=5, uint8_t _er=4 );
6262
void processRadioMessage();
6363
void processMQTTMessage(char *inputString, int inputPos);
64-
boolean isLedMode();
65-
void ledTimersInterrupt();
6664
private:
6765
bool MQTTClientConnected;
6866
char buffer[MQTT_MAX_PACKET_SIZE];
6967
int buffsize;
7068
char convBuf[MAX_PAYLOAD*2+1];
7169
boolean useWriteCallback;
72-
void (*dataCallback)(char *, int *);
70+
void (*dataCallback)(const char *, int *);
7371
void SendMQTT(MyMessage &msg);
74-
char strncpysType_retL(char *str, char index, char start);
75-
76-
volatile uint8_t countRx;
77-
volatile uint8_t countTx;
78-
volatile uint8_t countErr;
79-
uint8_t pinRx;
80-
uint8_t pinTx;
81-
uint8_t pinEr;
82-
boolean ledMode;
72+
char strncpysType_retL(char *str, unsigned char index, char start);
73+
74+
8375
void ledTimers();
8476
void rxBlink(uint8_t cnt);
8577
void txBlink(uint8_t cnt);
8678
void errBlink(uint8_t cnt);
8779
};
8880

81+
extern void ledTimersInterrupt();
8982

9083

9184
#endif

EthernetGateway/EthernetGateway.ino renamed to libraries/MySensors/examples/EthernetGateway/EthernetGateway.ino

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,15 @@
4444
* Powering: both NRF24l01 radio and Ethernet(ENC28J60) uses 3.3V
4545
*/
4646

47-
48-
49-
5047
#include <SPI.h>
51-
#include <MsTimer2.h>
52-
#include <PinChangeInt.h>
5348
#include <MyGateway.h>
5449
#include <stdarg.h>
5550

56-
#include <UIPEthernet.h> // Use this if you have attached a Ethernet ENC28J60 shields
57-
//#include <Ethernet.h> // Use this fo WizNET module and Arduino Ethernet Shield
51+
// Use this if you have attached a Ethernet ENC28J60 shields
52+
#include <UIPEthernet.h>
53+
54+
// Use this fo WizNET module and Arduino Ethernet Shield
55+
//#include <Ethernet.h>
5856

5957

6058
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
@@ -99,17 +97,6 @@ void setup()
9997

10098
// start listening for clients
10199
server.begin();
102-
103-
// C++ classes and interrupts really sucks. Need to attach interrupt
104-
// outside thw Gateway class due to language shortcomings! Gah!
105-
106-
if (gw.isLedMode()) {
107-
// Add led timer interrupt
108-
MsTimer2::set(300, ledTimersInterrupt);
109-
MsTimer2::start();
110-
// Add interrupt for inclustion button to pin
111-
PCintPort::attachInterrupt(INCLUSION_MODE_PIN, startInclusionInterrupt, RISING);
112-
}
113100
}
114101

115102
// This will be called when data should be written to ethernet
@@ -118,24 +105,22 @@ void writeEthernet(char *writeBuffer) {
118105
}
119106

120107

121-
void processEthernetMessages()
108+
void loop()
122109
{
123-
// if an incoming client connects, there will be
124-
// bytes available to read via the client object
125-
EthernetClient client = server.available();
110+
// if an incoming client connects, there will be
111+
// bytes available to read via the client object
112+
EthernetClient client = server.available();
126113

127-
if (client)
128-
{
114+
if (client) {
129115
// if got 1 or more bytes
130-
if (client.available())
131-
{
116+
if (client.available()) {
132117
// read the bytes incoming from the client
133118
char inChar = client.read();
134119

135120
if (inputPos<MAX_RECEIVE_LENGTH-1) {
136121
// if newline then command is complete
137-
if (inChar == '\n')
138-
{ // a command was issued by the client
122+
if (inChar == '\n') {
123+
// a command was issued by the client
139124
// we will now try to send it to the actuator
140125
inputString[inputPos] = 0;
141126

@@ -146,36 +131,18 @@ void processEthernetMessages()
146131

147132
// clear the string:
148133
inputPos = 0;
149-
}
150-
else
151-
{ // add it to the inputString:
152-
inputString[inputPos] = inChar;
153-
inputPos++;
134+
} else {
135+
// add it to the inputString:
136+
inputString[inputPos] = inChar;
137+
inputPos++;
154138
}
155139
} else {
156140
// Incoming message too long. Throw away
157141
inputPos = 0;
158142
}
159143
}
160144
}
161-
}
162-
163-
164-
165-
void loop()
166-
{
167-
// check for incoming commands from clients
168-
processEthernetMessages();
169-
170-
gw.processRadioMessage();
171-
}
172-
173-
void startInclusionInterrupt() {
174-
gw.startInclusionInterrupt();
175-
}
176-
177-
void ledTimersInterrupt() {
178-
gw.ledTimersInterrupt();
145+
gw.processRadioMessage();
179146
}
180147

181148

MQTTGateway/MQTTGateway.ino renamed to libraries/MySensors/examples/MQTTGateway/MQTTGateway.ino

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ transform/1on0off.map
9292
#include <SPI.h>
9393
#include <MyMQTT.h>
9494
#include <Ethernet.h>
95-
#include <MsTimer2.h>
9695

9796

9897
// Use this for IBOARD modded to use standard MISO/MOSI/SCK, see note *1 above!
@@ -135,12 +134,8 @@ void processEthernetMessages() {
135134
}
136135
}
137136

138-
void writeEthernet(char *writeBuffer, int *writeSize) {
139-
server.write(writeBuffer, *writeSize); // Todo: Should this really be *writeSize?
140-
}
141-
142-
void ledTimersInterrupt() {
143-
gw.ledTimersInterrupt();
137+
void writeEthernet(const char *writeBuffer, int *writeSize) {
138+
server.write((const uint8_t *)writeBuffer, *writeSize); // Todo: Should this really be *writeSize?
144139
}
145140

146141
int main(void) {
@@ -149,11 +144,6 @@ int main(void) {
149144
delay(1000); // Wait for Ethernet to get configured.
150145
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
151146
server.begin();
152-
// Add led timer interrupt
153-
if (gw.isLedMode()) {
154-
MsTimer2::set(200, ledTimersInterrupt);
155-
MsTimer2::start();
156-
}
157147
while (1) {
158148
processEthernetMessages();
159149
gw.processRadioMessage();

libraries/MySensors/examples/RFIDLockSensor/RFIDLockSensor.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434
const uint8_t maxKeyLength = 7;
3535
uint8_t validKeys[][maxKeyLength] = {
3636
{ 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
37-
{ 0, 0, 0, 0, 0, 0, 0 },
38-
{ 0, 0, 0, 0, 0, 0, 0 },
39-
{ 0, 0, 0, 0, 0, 0, 0 },
37+
{ 0, 0, 0, 0, 0, 0, 0 }, // ADD YOUR KEYS HERE!
4038
{ 0, 0, 0, 0, 0, 0, 0 }};
4139
int keyCount = sizeof validKeys / maxKeyLength;
4240

@@ -84,11 +82,12 @@ void setup() {
8482
}
8583

8684
void loop() {
85+
gw.process(); // Process incomming messages
86+
8787
boolean success;
8888
uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
8989
uint8_t currentKeyLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
9090

91-
gw.process(); // Process incomming messages
9291

9392
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
9493
// 'uid' will be populated with the UID, and uidLength will indicate
@@ -159,3 +158,4 @@ void incomingMessage(const MyMessage &message) {
159158
}
160159
}
161160

161+

0 commit comments

Comments
 (0)