moved comment and minor ops
This commit is contained in:
+25
-25
@@ -1,20 +1,20 @@
|
|||||||
/* MyMQTT Broker Gateway 0.1b
|
/* MyMQTT Broker Gateway 0.1b
|
||||||
|
|
||||||
Created by Daniel Wiegert <daniel.wiegert@gmail.com>
|
Created by Daniel Wiegert <daniel.wiegert@gmail.com>
|
||||||
Based on MySensors Ethernet Gateway by Henrik Ekblad <henrik.ekblad@gmail.com>
|
Based on MySensors Ethernet Gateway by Henrik Ekblad <henrik.ekblad@gmail.com>
|
||||||
http://www.mysensors.org
|
http://www.mysensors.org
|
||||||
|
|
||||||
Requires MySensors lib 1.4b
|
Requires MySensors lib 1.4b
|
||||||
|
|
||||||
* Change below; TCP_IP, TCP_PORT, TCP_MAC
|
* Change below; TCP_IP, TCP_PORT, TCP_MAC
|
||||||
This will listen on your selected TCP_IP:TCP_PORT below, Please change TCP_MAC your liking also.
|
This will listen on your selected TCP_IP:TCP_PORT below, Please change TCP_MAC your liking also.
|
||||||
*1 -> NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Local Ranges.
|
*1 -> NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Local Ranges.
|
||||||
|
|
||||||
*2 You can use standard pin set-up as MySensors recommends or if you own a IBOARD you may change
|
*2 You can use standard pin set-up as MySensors recommends or if you own a IBOARD you may change
|
||||||
the radio-pins below if you hardware mod your iBoard. see [URL BELOW] for more details.
|
the radio-pins below if you hardware mod your iBoard. see [URL BELOW] for more details.
|
||||||
http://forum.mysensors.org/topic/224/iboard-cheap-single-board-ethernet-arduino-with-radio/5
|
http://forum.mysensors.org/topic/224/iboard-cheap-single-board-ethernet-arduino-with-radio/5
|
||||||
|
|
||||||
* Don't forget to look at the definitions in libraries\MySensors\MyMQTT.h!
|
* Don't forget to look at the definitions in libraries\MySensors\MyMQTT.h!
|
||||||
|
|
||||||
define TCPDUMP and connect serial interface if you have problems, please write on
|
define TCPDUMP and connect serial interface if you have problems, please write on
|
||||||
http://forum.mysensors.org/ and explain your problem, include serial output. Don't forget to
|
http://forum.mysensors.org/ and explain your problem, include serial output. Don't forget to
|
||||||
@@ -29,8 +29,9 @@
|
|||||||
MQTT_SEND_SUBSCRIPTION is if you want the MyMQTT to send a empty payload message to your nodes.
|
MQTT_SEND_SUBSCRIPTION is if you want the MyMQTT to send a empty payload message to your nodes.
|
||||||
This can be useful if you want to send latest state back to the MQTT client. Just check if incoming
|
This can be useful if you want to send latest state back to the MQTT client. Just check if incoming
|
||||||
message has any length or not.
|
message has any length or not.
|
||||||
|
Example: if (msg.type==V_LIGHT && strlen(msg.getString())>0) otherwise the code might do strange things.
|
||||||
|
|
||||||
* Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/[SensorType]
|
* Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/[SensorType]
|
||||||
NodeID and SensorID is uint8 (0-255) number.
|
NodeID and SensorID is uint8 (0-255) number.
|
||||||
Last segment is translation of the sensor type, look inside MyMQTT.cpp for the definitions.
|
Last segment is translation of the sensor type, look inside MyMQTT.cpp for the definitions.
|
||||||
User can change this to their needs. We have also left some space for custom types.
|
User can change this to their needs. We have also left some space for custom types.
|
||||||
@@ -39,18 +40,18 @@ Special: (sensor 255 reserved for special commands)
|
|||||||
You can receive a node sketch name with MyMQTT/20/255/Sketch_name (or version with _version)
|
You can receive a node sketch name with MyMQTT/20/255/Sketch_name (or version with _version)
|
||||||
|
|
||||||
To-do:
|
To-do:
|
||||||
Special commands : clear or set EEPROM Values, Send REBOOT and Receive reboot for MyMQTT itself.
|
Special commands : clear or set EEPROM Values, Send REBOOT and Receive reboot for MyMQTT itself.
|
||||||
Be able to send ACK so client returns the data being sent.
|
Be able to send ACK so client returns the data being sent.
|
||||||
... Please come with ideas!
|
... Please come with ideas!
|
||||||
|
|
||||||
Test in more MQTT clients, So far tested in openhab and MyMQTT for Android (Not my creation)
|
Test in more MQTT clients, So far tested in openhab and MyMQTT for Android (Not my creation)
|
||||||
- http://www.openhab.org/
|
- http://www.openhab.org/
|
||||||
- https://play.google.com/store/apps/details?id=at.tripwire.mqtt.client&hl=en
|
- https://play.google.com/store/apps/details?id=at.tripwire.mqtt.client&hl=en
|
||||||
... Please notify me if you use this broker with other software.
|
... Please notify me if you use this broker with other software.
|
||||||
|
|
||||||
|
|
||||||
* Example Openhab configuration:
|
* Example Openhab configuration:
|
||||||
openhab.cfg
|
openhab.cfg
|
||||||
---
|
---
|
||||||
mqtt:mysensor.url=tcp://192.168.0.234:1883
|
mqtt:mysensor.url=tcp://192.168.0.234:1883
|
||||||
mqtt:mysensor.clientId=MQTT
|
mqtt:mysensor.clientId=MQTT
|
||||||
@@ -75,7 +76,7 @@ sitemap/test.site
|
|||||||
---
|
---
|
||||||
sitemap demo label="Menu"
|
sitemap demo label="Menu"
|
||||||
Frame label="Openhab" {
|
Frame label="Openhab" {
|
||||||
Group item=test label="Test group"
|
Group item=test label="Test group"
|
||||||
}
|
}
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -148,7 +149,7 @@ int main(void) {
|
|||||||
delay(1000); // Wait for Ethernet to get configured.
|
delay(1000); // Wait for Ethernet to get configured.
|
||||||
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
|
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
|
||||||
server.begin();
|
server.begin();
|
||||||
// Add led timer interrupt
|
// Add led timer interrupt
|
||||||
if (gw.isLedMode()) {
|
if (gw.isLedMode()) {
|
||||||
MsTimer2::set(200, ledTimersInterrupt);
|
MsTimer2::set(200, ledTimersInterrupt);
|
||||||
MsTimer2::start();
|
MsTimer2::start();
|
||||||
@@ -159,4 +160,3 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -67,15 +67,15 @@ char S_50[] PROGMEM = "Sketch_version"; // Does not exist in MyMessage.h!
|
|||||||
|
|
||||||
|
|
||||||
PROGMEM const char *sType[] = {
|
PROGMEM const char *sType[] = {
|
||||||
S_0, S_1, S_2, S_3, S_4, S_5, S_6, S_7, S_8, S_9, S_10,
|
S_0, S_1, S_2, S_3, S_4, S_5, S_6, S_7, S_8, S_9, S_10,
|
||||||
S_11, S_12, S_13, S_14, S_15, S_16, S_17, S_18, S_19, S_20,
|
S_11, S_12, S_13, S_14, S_15, S_16, S_17, S_18, S_19, S_20,
|
||||||
S_21, S_22, S_23, S_24, S_25, S_26, S_27, S_28, S_29, S_30,
|
S_21, S_22, S_23, S_24, S_25, S_26, S_27, S_28, S_29, S_30,
|
||||||
S_31, S_32, S_33, S_34, S_35, S_36, S_37, S_38, S_39, S_40,
|
S_31, S_32, S_33, S_34, S_35, S_36, S_37, S_38, S_39, S_40,
|
||||||
S_41, S_42, S_43, S_44, S_45, S_46, S_47, S_48, S_49, S_50
|
S_41, S_42, S_43, S_44, S_45, S_46, S_47, S_48, S_49, S_50
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
MyMQTT::MyMQTT(uint8_t _cepin, uint8_t _cspin, uint8_t _rx, uint8_t _tx, uint8_t _er) :
|
MyMQTT::MyMQTT(uint8_t _cepin, uint8_t _cspin, uint8_t _rx, uint8_t _tx, uint8_t _er) :
|
||||||
MySensor(_cepin, _cspin) {
|
MySensor(_cepin, _cspin) {
|
||||||
if (_rx != NULL) {
|
if (_rx != NULL) {
|
||||||
pinRx = _rx;
|
pinRx = _rx;
|
||||||
@@ -85,7 +85,7 @@ MySensor(_cepin, _cspin) {
|
|||||||
if (_tx != NULL) {
|
if (_tx != NULL) {
|
||||||
pinTx = _tx;
|
pinTx = _tx;
|
||||||
pinMode(pinTx, OUTPUT);
|
pinMode(pinTx, OUTPUT);
|
||||||
ledMode = true;
|
ledMode = true;
|
||||||
}
|
}
|
||||||
if (_er != NULL) {
|
if (_er != NULL) {
|
||||||
pinEr = _er;
|
pinEr = _er;
|
||||||
@@ -132,7 +132,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
|
|||||||
buffer[0]= 0;
|
buffer[0]= 0;
|
||||||
buffsize = 0;
|
buffsize = 0;
|
||||||
#ifdef TCPDUMP
|
#ifdef TCPDUMP
|
||||||
Serial.print("<<");
|
Serial.print("<<");
|
||||||
char buf[4];
|
char buf[4];
|
||||||
for (int a=0; a<inputPos; a++) { sprintf(buf,"%02X ",(byte)inputString[a]); Serial.print(buf); } Serial.println("");
|
for (int a=0; a<inputPos; a++) { sprintf(buf,"%02X ",(byte)inputString[a]); Serial.print(buf); } Serial.println("");
|
||||||
#endif
|
#endif
|
||||||
@@ -165,14 +165,14 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
|
|||||||
}
|
}
|
||||||
if (buffsize > 0) {
|
if (buffsize > 0) {
|
||||||
#ifdef TCPDUMP
|
#ifdef TCPDUMP
|
||||||
Serial.print(">>");
|
Serial.print(">>");
|
||||||
char buf[4];
|
char buf[4];
|
||||||
for (int a=0; a<buffsize; a++) { sprintf(buf,"%02X ",(byte)buffer[a]); Serial.print(buf); } Serial.println("");
|
for (int a=0; a<buffsize; a++) { sprintf(buf,"%02X ",(byte)buffer[a]); Serial.print(buf); } Serial.println("");
|
||||||
#endif
|
#endif
|
||||||
dataCallback(buffer,&buffsize);
|
dataCallback(buffer,&buffsize);
|
||||||
}
|
}
|
||||||
// We publish everything we get, we dont care if its subscribed or not!
|
// We publish everything we get, we dont care if its subscribed or not!
|
||||||
if ((byte)inputString[0] >> 4 == MQTTPUBLISH || (MQTT_SEND_SUBSCRIPTION && (byte)inputString[0] >> 4 == MQTTSUBSCRIBE)) {
|
if ((byte)inputString[0] >> 4 == MQTTPUBLISH || (MQTT_SEND_SUBSCRIPTION && (byte)inputString[0] >> 4 == MQTTSUBSCRIBE)) {
|
||||||
buffer[0]= 0;
|
buffer[0]= 0;
|
||||||
buffsize = 0;
|
buffsize = 0;
|
||||||
if ((byte)inputString[0] >> 4 == MQTTSUBSCRIBE) {
|
if ((byte)inputString[0] >> 4 == MQTTSUBSCRIBE) {
|
||||||
@@ -194,11 +194,11 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
|
|||||||
} else if (i==3) {
|
} else if (i==3) {
|
||||||
char match=0;
|
char match=0;
|
||||||
for (int j=0; strcpy_P(convBuf, (char*)pgm_read_word(&(sType[j]))) ; j++) {
|
for (int j=0; strcpy_P(convBuf, (char*)pgm_read_word(&(sType[j]))) ; j++) {
|
||||||
if (strcmp(str,convBuf)==0) {
|
if (strcmp(str,convBuf)==0) {
|
||||||
match=j;
|
match=j;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
msg.type = match;
|
msg.type = match;
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
@@ -206,7 +206,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
|
|||||||
if ((char)inputString[1] > (char)(inputString[3]+2) && !((byte)inputString[0] >> 4 == MQTTSUBSCRIBE)) {
|
if ((char)inputString[1] > (char)(inputString[3]+2) && !((byte)inputString[0] >> 4 == MQTTSUBSCRIBE)) {
|
||||||
strcpy(convBuf,inputString+(inputString[3]+4));
|
strcpy(convBuf,inputString+(inputString[3]+4));
|
||||||
msg.set(convBuf);
|
msg.set(convBuf);
|
||||||
} else {
|
} else {
|
||||||
msg.set("");
|
msg.set("");
|
||||||
}
|
}
|
||||||
msg.sender = GATEWAY_ADDRESS;
|
msg.sender = GATEWAY_ADDRESS;
|
||||||
@@ -227,7 +227,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
|
|||||||
if (msg.isAck()) {
|
if (msg.isAck()) {
|
||||||
Serial.println("msg is ack!");
|
Serial.println("msg is ack!");
|
||||||
if (msg.sender==255 && mGetCommand(msg)==C_INTERNAL && msg.type==I_ID_REQUEST) {
|
if (msg.sender==255 && mGetCommand(msg)==C_INTERNAL && msg.type==I_ID_REQUEST) {
|
||||||
// TODO: sending ACK request on id_response fucks node up. doesn't work.
|
// TODO: sending ACK request on id_response fucks node up. doesn't work.
|
||||||
// The idea was to confirm id and save to EEPROM_LATEST_NODE_ADDRESS.
|
// The idea was to confirm id and save to EEPROM_LATEST_NODE_ADDRESS.
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -239,11 +239,11 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
|
|||||||
saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID);
|
saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID);
|
||||||
}
|
}
|
||||||
if (mGetCommand(msg)==C_INTERNAL && msg.type==I_CONFIG) { // CONFIG
|
if (mGetCommand(msg)==C_INTERNAL && msg.type==I_CONFIG) { // CONFIG
|
||||||
// As for now there is only one 'config' request.
|
// As for now there is only one 'config' request.
|
||||||
// We force SI! Resistance is futile!
|
// We force SI! Resistance is futile!
|
||||||
//
|
//
|
||||||
// Todo : Support for more config types, Maybe just read from own EEPROM?
|
// Todo : Support for more config types, Maybe just read from own EEPROM?
|
||||||
// Use internal EEPROM_CONTROLLER_CONFIG_ADDRESS and special MQTT address to write to
|
// Use internal EEPROM_CONTROLLER_CONFIG_ADDRESS and special MQTT address to write to
|
||||||
// EEPROM_CONTROLLER_CONFIG_ADDRESS & EEPROM_LOCAL_CONFIG_ADDRESS
|
// EEPROM_CONTROLLER_CONFIG_ADDRESS & EEPROM_LOCAL_CONFIG_ADDRESS
|
||||||
msg.destination = msg.sender; //NodeID
|
msg.destination = msg.sender; //NodeID
|
||||||
msg.sender = GATEWAY_ADDRESS;
|
msg.sender = GATEWAY_ADDRESS;
|
||||||
@@ -273,10 +273,10 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
|
|||||||
mSetVersion(msg, PROTOCOL_VERSION);
|
mSetVersion(msg, PROTOCOL_VERSION);
|
||||||
txBlink(1);
|
txBlink(1);
|
||||||
if (!sendRoute(msg)) errBlink(1);
|
if (!sendRoute(msg)) errBlink(1);
|
||||||
// if (sendRoute(msg)) saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID); // If send OK save to eeprom. DOES NOT ALWAYS RETURN 'OK'!?
|
// if (sendRoute(msg)) saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID); // If send OK save to eeprom. DOES NOT ALWAYS RETURN 'OK'!?
|
||||||
} else if (mGetCommand(msg)!=0) {
|
} else if (mGetCommand(msg)!=0) {
|
||||||
if (mGetCommand(msg)==3) msg.type=msg.type+38;
|
if (mGetCommand(msg)==3) msg.type=msg.type+38;
|
||||||
buffer[buffsize++] = MQTTPUBLISH << 4; // 0:
|
buffer[buffsize++] = MQTTPUBLISH << 4; // 0:
|
||||||
buffer[buffsize++] = 0x09; // 1: Remaining length with no payload, we'll set this later to correct value, buffsize -2
|
buffer[buffsize++] = 0x09; // 1: Remaining length with no payload, we'll set this later to correct value, buffsize -2
|
||||||
buffer[buffsize++] = 0x00; // 2: Length MSB (Remaing length can never exceed ff,so MSB must be 0!)
|
buffer[buffsize++] = 0x00; // 2: Length MSB (Remaing length can never exceed ff,so MSB must be 0!)
|
||||||
buffer[buffsize++] = 0x08; // 3: Length LSB (ADDR), We'll set this later
|
buffer[buffsize++] = 0x08; // 3: Length LSB (ADDR), We'll set this later
|
||||||
@@ -297,11 +297,11 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
|
|||||||
#ifdef TCPDUMP
|
#ifdef TCPDUMP
|
||||||
Serial.print(">>");
|
Serial.print(">>");
|
||||||
char buf[4];
|
char buf[4];
|
||||||
for (int a=0; a<buffsize; a++) { sprintf(buf,"%02X ",(byte)buffer[a]); Serial.print(buf); } Serial.println("");
|
for (int a=0; a<buffsize; a++) { sprintf(buf,"%02X ",(byte)buffer[a]); Serial.print(buf); } Serial.println("");
|
||||||
#endif
|
#endif
|
||||||
dataCallback(buffer,&buffsize);
|
dataCallback(buffer,&buffsize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean MyMQTT::isLedMode() {
|
boolean MyMQTT::isLedMode() {
|
||||||
@@ -350,7 +350,7 @@ void MyMQTT::errBlink(uint8_t cnt) {
|
|||||||
char MyMQTT::strncpysType_retL(char *str, char index, char start) {
|
char MyMQTT::strncpysType_retL(char *str, char index, char start) {
|
||||||
char c;
|
char c;
|
||||||
char l;
|
char l;
|
||||||
char *p = (char *)pgm_read_word(&(sType[index]));
|
char *p = (char *)pgm_read_word(&(sType[index]));
|
||||||
str+=start;
|
str+=start;
|
||||||
while ((c = pgm_read_byte(p))) {
|
while ((c = pgm_read_byte(p))) {
|
||||||
*str=c;
|
*str=c;
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ version 2 as published by the Free Software Foundation.
|
|||||||
///////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
//#define TCPDUMP // Dump TCP packages.
|
#define TCPDUMP // Dump TCP packages.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MQTT_FIRST_SENSORID 20 // If you want manually configured nodes below this value. 255 = Disable
|
#define MQTT_FIRST_SENSORID 20 // If you want manually configured nodes below this value. 255 = Disable
|
||||||
@@ -54,9 +54,7 @@ version 2 as published by the Free Software Foundation.
|
|||||||
#define MQTTQOS1 (1 << 1)
|
#define MQTTQOS1 (1 << 1)
|
||||||
#define MQTTQOS2 (2 << 1)
|
#define MQTTQOS2 (2 << 1)
|
||||||
|
|
||||||
#define MyTestVar F("Test")
|
class MyMQTT :
|
||||||
|
|
||||||
class MyMQTT :
|
|
||||||
public MySensor {
|
public MySensor {
|
||||||
public:
|
public:
|
||||||
MyMQTT(uint8_t _cepin=5, uint8_t _cspin=6, uint8_t _rx=NULL, uint8_t _tx=NULL, uint8_t _er=NULL);
|
MyMQTT(uint8_t _cepin=5, uint8_t _cspin=6, uint8_t _rx=NULL, uint8_t _tx=NULL, uint8_t _er=NULL);
|
||||||
@@ -74,7 +72,7 @@ private:
|
|||||||
void (*dataCallback)(char *, int *);
|
void (*dataCallback)(char *, int *);
|
||||||
void SendMQTT(MyMessage &msg);
|
void SendMQTT(MyMessage &msg);
|
||||||
char strncpysType_retL(char *str, char index, char start);
|
char strncpysType_retL(char *str, char index, char start);
|
||||||
|
|
||||||
volatile uint8_t countRx;
|
volatile uint8_t countRx;
|
||||||
volatile uint8_t countTx;
|
volatile uint8_t countTx;
|
||||||
volatile uint8_t countErr;
|
volatile uint8_t countErr;
|
||||||
@@ -91,4 +89,3 @@ private:
|
|||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user