From 2ff7c74e990ea16b337f9828c15b1908d0fb2990 Mon Sep 17 00:00:00 2001 From: Daniel Wiegert Date: Sun, 17 Aug 2014 22:04:42 +0200 Subject: [PATCH] moved comment and minor ops --- MQTTGateway/MQTTGateway.ino | 50 +++++++++++++++++----------------- libraries/MySensors/MyMQTT.cpp | 42 ++++++++++++++-------------- libraries/MySensors/MyMQTT.h | 9 ++---- 3 files changed, 49 insertions(+), 52 deletions(-) diff --git a/MQTTGateway/MQTTGateway.ino b/MQTTGateway/MQTTGateway.ino index 467ac44..54da02d 100755 --- a/MQTTGateway/MQTTGateway.ino +++ b/MQTTGateway/MQTTGateway.ino @@ -1,20 +1,20 @@ /* MyMQTT Broker Gateway 0.1b - Created by Daniel Wiegert - Based on MySensors Ethernet Gateway by Henrik Ekblad - http://www.mysensors.org +Created by Daniel Wiegert +Based on MySensors Ethernet Gateway by Henrik Ekblad +http://www.mysensors.org - Requires MySensors lib 1.4b +Requires MySensors lib 1.4b - * 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. - *1 -> NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Local Ranges. +* 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. +*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 - 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 +*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. + 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 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. 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. + 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. 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. @@ -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) To-do: - 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. - ... Please come with ideas! - - Test in more MQTT clients, So far tested in openhab and MyMQTT for Android (Not my creation) - - http://www.openhab.org/ - - 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. +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. +... Please come with ideas! + +Test in more MQTT clients, So far tested in openhab and MyMQTT for Android (Not my creation) +- http://www.openhab.org/ +- 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. - * Example Openhab configuration: -openhab.cfg +* Example Openhab configuration: +openhab.cfg --- mqtt:mysensor.url=tcp://192.168.0.234:1883 mqtt:mysensor.clientId=MQTT @@ -75,7 +76,7 @@ sitemap/test.site --- sitemap demo label="Menu" 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. gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet); server.begin(); -// Add led timer interrupt + // Add led timer interrupt if (gw.isLedMode()) { MsTimer2::set(200, ledTimersInterrupt); MsTimer2::start(); @@ -159,4 +160,3 @@ int main(void) { } } - diff --git a/libraries/MySensors/MyMQTT.cpp b/libraries/MySensors/MyMQTT.cpp index 2e1d49e..d2a0352 100755 --- a/libraries/MySensors/MyMQTT.cpp +++ b/libraries/MySensors/MyMQTT.cpp @@ -67,15 +67,15 @@ char S_50[] PROGMEM = "Sketch_version"; // Does not exist in MyMessage.h! 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_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_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) { if (_rx != NULL) { pinRx = _rx; @@ -85,7 +85,7 @@ MySensor(_cepin, _cspin) { if (_tx != NULL) { pinTx = _tx; pinMode(pinTx, OUTPUT); - ledMode = true; + ledMode = true; } if (_er != NULL) { pinEr = _er; @@ -132,7 +132,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) { buffer[0]= 0; buffsize = 0; #ifdef TCPDUMP - Serial.print("<<"); + Serial.print("<<"); char buf[4]; for (int a=0; a 0) { #ifdef TCPDUMP - Serial.print(">>"); + Serial.print(">>"); char buf[4]; - for (int a=0; a> 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; buffsize = 0; if ((byte)inputString[0] >> 4 == MQTTSUBSCRIBE) { @@ -194,11 +194,11 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) { } else if (i==3) { char match=0; for (int j=0; strcpy_P(convBuf, (char*)pgm_read_word(&(sType[j]))) ; j++) { - if (strcmp(str,convBuf)==0) { - match=j; + if (strcmp(str,convBuf)==0) { + match=j; break; } - } + } msg.type = match; } 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)) { strcpy(convBuf,inputString+(inputString[3]+4)); msg.set(convBuf); - } else { + } else { msg.set(""); } msg.sender = GATEWAY_ADDRESS; @@ -227,7 +227,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) { if (msg.isAck()) { Serial.println("msg is ack!"); 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. } } else { @@ -239,11 +239,11 @@ void MyMQTT::SendMQTT(MyMessage &msg) { saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID); } 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! // - // 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 + // 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 // EEPROM_CONTROLLER_CONFIG_ADDRESS & EEPROM_LOCAL_CONFIG_ADDRESS msg.destination = msg.sender; //NodeID msg.sender = GATEWAY_ADDRESS; @@ -273,10 +273,10 @@ void MyMQTT::SendMQTT(MyMessage &msg) { mSetVersion(msg, PROTOCOL_VERSION); txBlink(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) { 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++] = 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 @@ -297,11 +297,11 @@ void MyMQTT::SendMQTT(MyMessage &msg) { #ifdef TCPDUMP Serial.print(">>"); char buf[4]; - for (int a=0; a