diff --git a/MQTTGateway/MQTTGateway.ino b/MQTTGateway/MQTTGateway.ino old mode 100644 new mode 100755 index 816c2b0..868cc54 --- a/MQTTGateway/MQTTGateway.ino +++ b/MQTTGateway/MQTTGateway.ino @@ -1,77 +1,56 @@ -/* MyMQTT Gateway 0.1b - - Created by Daniel Wiegert - Based on Mysensors Ethernet Gateway by Henrik Ekblad - Requires MySensors lib 1.4b - http://www.mysensors.org - - * Don't forget to look at the definitions in MyMQTT.h! - * Don't forget to configure Radio pins, IP and MAC-address! - - * Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/Light - NodeID and SensorID is number (0-255). - Last segment is translation of the sensor type, look inside MyMQTT.cpp for - the definitions. User can change this to their needs. - -Example openhab setup: http://www.openhab.org/ - -* openhab.cfg ---- -mqtt:mysensor.url=tcp://192.168.0.234:1883 -mqtt:mysensor.clientId=MQTT ---- - -* items/test.items ---- -Group test -Number Temp_test "Temp [%.1f °C]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Temperature:state:default]"} -Number Hum_test "Hum [%.1f %%]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Humidity:state:default]"} -Number test_test "test [%s]" (test) {mqtt="<[mysensor:MyMQTT/20/3/#:state:default]"} -Switch sw1 "sw 1" (test) {<[mysensor:MyMQTT/21/1/Light:command:MAP(1.map)]"} ---- (Note; # = Wildcard character) - -* sitemap/test.site ---- -sitemap demo label="Menu" -Frame label="Openhab" { - Group item=test label="Test group" -} ---- - -* transform/1.map ---- -1=ON -0=OFF ---- - -* Features: - - Supports automatic nodeID delegation - - Recieve sketchname and version Example: (openhab item) -String sketch20 "Node 20 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:MyMQTT/20/255/Sketch_name:state:default]"} -String sketch21 "Node 21 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:MyMQTT/21/255/Sketch_name:state:default]"} -[...] - -* Todo: - - DOCUMENTATION... - - Special commands - Read and set EEPROM Values - Send Reboot, And reboot gateway itself. -... +// MyMQTT Gateway 0.1b +// +// Created by Daniel Wiegert +// Based on Mysensors Ethernet Gateway by Henrik Ekblad +// http://www.mysensors.org +// +// Requires MySensors lib 1.4b +// +// Don't forget to look at the definitions in MyMQTT.h! +// Don't forget to configure Radio pins, IP and MAC-address! +// +// Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/Light +// NodeID and SensorID is Decimal number. +// Last segment is translation of the sensor type, look inside MyMQTT.cpp for +// the definitions. User can change this to their needs. +// +// Features: +// Supports automatic nodeID delegation +// Recieve sketchname and version Example: (openhab item) +//String sketch20 "Node 20 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:MyMQTT/20/255/Sketch_name:state:default]"} +//String sketch21 "Node 21 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:MyMQTT/21/255/Sketch_name:state:default]"} +// ... +// +// Todo: +// DOCUMENTATION... +// Special commands : clear or set EEPROM Values +// Special commands : Reboot +// ... +/* +Sketch uses 23,666 bytes (77%) of program storage space. Maximum is 30,720 bytes. +Global variables use 766 bytes (37%) of dynamic memory, leaving 1,282 bytes for local variables. Maximum is 2,048 bytes. */ #include #include #include +#include // Use this for IBOARD modded to use standard MISO/MOSI/SCK More information see; // http://forum.mysensors.org/topic/224/iboard-cheap-single-board-ethernet-arduino-with-radio/5 -#define RADIO_CE_PIN 3 // radio chip enable -#define RADIO_SPI_SS_PIN 8 // radio SPI serial select +//#define RADIO_CE_PIN 3 // radio chip enable +//#define RADIO_SPI_SS_PIN 8 // radio SPI serial select +//#define RADIO_ERROR_LED_PIN A2 // Error led pin +//#define RADIO_RX_LED_PIN A1 // Receive led pin +//#define RADIO_TX_LED_PIN A0 // the PCB, on board LED //Use this for default configured pro mini / nano etc : -//#define RADIO_CE_PIN 5 // radio chip enable -//#define RADIO_SPI_SS_PIN 6 // radio SPI serial select +#define RADIO_CE_PIN 5 // radio chip enable +#define RADIO_SPI_SS_PIN 6 // radio SPI serial select +#define RADIO_ERROR_LED_PIN 7 // Error led pin +#define RADIO_RX_LED_PIN 8 // Receive led pin +#define RADIO_TX_LED_PIN 9 // the PCB, on board LED #define IP_PORT 1883 // MQTT Listening port IPAddress myIp ( 192, 168, 0, 234 ); // Configure your static ip-address here @@ -80,6 +59,8 @@ byte mac[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - Change th EthernetServer server = EthernetServer(IP_PORT); MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN); +// Uncomment this constructor if you have leds and include button attached to your gateway +// MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN); void processEthernetMessages() { char inputString[MQTT_MAX_PACKET_SIZE] = ""; @@ -99,16 +80,23 @@ void writeEthernet(char *writeBuffer, int *writeSize) { server.write(writeBuffer, *writeSize); // Should this really be *writeSize? } +void ledTimersInterrupt() { + gw.ledTimersInterrupt(); +} + int main(void) { init(); Ethernet.begin(mac, myIp); delay(1000); // Wait for Ethernet to get configured. gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet); server.begin(); + if (gw.isLedMode()) { + // Add led timer interrupt + MsTimer2::set(300, ledTimersInterrupt); + MsTimer2::start(); + } while (1) { processEthernetMessages(); gw.processRadioMessage(); } } - - diff --git a/libraries/MySensors/MyMQTT.cpp b/libraries/MySensors/MyMQTT.cpp old mode 100644 new mode 100755 index 71d49a5..7d8c9a3 --- a/libraries/MySensors/MyMQTT.cpp +++ b/libraries/MySensors/MyMQTT.cpp @@ -66,31 +66,39 @@ char S_50[] PROGMEM = "Sketch_version"; // Does not exist in mysensors lib! 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) : + +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; + pinMode(pinRx, OUTPUT); + ledMode = true; + } + if (_tx != NULL) { + pinTx = _tx; + pinMode(pinTx, OUTPUT); + ledMode = true; + } + if (_er != NULL) { + pinEr = _er; + pinMode(pinEr, OUTPUT); + ledMode = true; + } } void MyMQTT::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate, void (*inDataCallback)(char *, int *)) { Serial.begin(BAUD_RATE); repeaterMode = true; isGateway = true; - MQTTClient = false; + MQTTClientConnected = false; setupRepeaterMode(); - - if (inDataCallback != NULL) { - useWriteCallback = true; - dataCallback = inDataCallback; - } - else { - useWriteCallback = false; - } + dataCallback = inDataCallback; nc.nodeId = 0; nc.distance = 0; @@ -110,6 +118,7 @@ void MyMQTT::processRadioMessage() { // A new message was received from one of the sensors MyMessage message = getLastMessage(); // Pass along the message from sensors to serial line + rxBlink(3); SendMQTT(message); } @@ -121,7 +130,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> 4 == MQTTPINGREQ) { buffer[buffsize++] = MQTTPINGRESP << 4; @@ -150,18 +159,18 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) { buffer[buffsize++] = (byte)inputString[3]; // Message ID LSB } if ((byte)inputString[0] >> 4 == MQTTDISCONNECT) { - MQTTClient=false; + MQTTClientConnected=false; } if (buffsize > 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) { @@ -171,7 +180,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) { } msg.sender = GATEWAY_ADDRESS; for (str = strtok_r(buffer, "/", &p); // split using semicolon - str && i < 4; str = strtok_r(NULL, "/", &p) //get subsequent tokens (?) + str && i < 4; str = strtok_r(NULL, "/", &p) //get subsequent tokens (?) ) { switch (i) { case 0: // Radioid (destination) @@ -188,11 +197,11 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) { case 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; break; } @@ -201,7 +210,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; @@ -209,7 +218,8 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) { mSetRequestAck(msg,false); mSetAck(msg,false); mSetVersion(msg, PROTOCOL_VERSION); - sendRoute(msg); + txBlink(1); + if (!sendRoute(msg)) errBlink(1); } } @@ -217,11 +227,11 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) { void MyMQTT::SendMQTT(MyMessage &msg) { //serial(PSTR("%d;%d;%d;%d;%s\n"),msg.sender, msg.sensor, mGetCommand(msg), msg.type, msg.getString(convBuf)); buffsize = 0; - if (!MQTTClient) return; + if (!MQTTClientConnected) return; 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 { @@ -233,11 +243,11 @@ Serial.println("msg is ack!"); 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; @@ -248,7 +258,8 @@ Serial.println("msg is ack!"); mSetAck(msg,false); msg.set("M"); mSetVersion(msg, PROTOCOL_VERSION); - sendRoute(msg); + txBlink(1); + if (!sendRoute(msg)) errBlink(1); } else if (mGetCommand(msg)==C_PRESENTATION && (msg.type==S_ARDUINO_NODE || msg.type==S_ARDUINO_REPEATER_NODE)) { //Doesnt work to check new sensorID here, this message does not always arrive.. See above. } else if (msg.sender==255 && mGetCommand(msg)==C_INTERNAL && msg.type==I_ID_REQUEST) { @@ -264,11 +275,12 @@ Serial.println("msg is ack!"); mSetAck(msg,false); msg.set((uint8_t)newNodeID); //Payload mSetVersion(msg, PROTOCOL_VERSION); - sendRoute(msg); + 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'!? } 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 @@ -289,17 +301,59 @@ Serial.println("msg is ack!"); #ifdef TCPDUMP Serial.print(">>"); char buf[4]; - for (int a=0; a