|
1 | | -/* MyMQTT Gateway 0.1b |
| 1 | +/* MyMQTT Broker Gateway 0.1b |
2 | 2 |
|
3 | | - Created by Daniel Wiegert <daniel.wiegert@gmail.com> |
4 | | - Based on Mysensors Ethernet Gateway by Henrik Ekblad <henrik.ekblad@gmail.com> |
5 | | - Requires MySensors lib 1.4b |
6 | | - http://www.mysensors.org |
| 3 | +Created by Daniel Wiegert <daniel.wiegert@gmail.com> |
| 4 | +Based on MySensors Ethernet Gateway by Henrik Ekblad <henrik.ekblad@gmail.com> |
| 5 | +http://www.mysensors.org |
7 | 6 |
|
8 | | - * Don't forget to look at the definitions in MyMQTT.h! |
9 | | - * Don't forget to configure Radio pins, IP and MAC-address! |
| 7 | +Requires MySensors lib 1.4b |
10 | 8 |
|
11 | | - * Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/Light |
12 | | - NodeID and SensorID is number (0-255). |
13 | | - Last segment is translation of the sensor type, look inside MyMQTT.cpp for |
14 | | - the definitions. User can change this to their needs. |
| 9 | +* Change below; TCP_IP, TCP_PORT, TCP_MAC |
| 10 | +This will listen on your selected TCP_IP:TCP_PORT below, Please change TCP_MAC your liking also. |
| 11 | +*1 -> NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Local Ranges. |
15 | 12 |
|
16 | | -Example openhab setup: http://www.openhab.org/ |
| 13 | +*2 You can use standard pin set-up as MySensors recommends or if you own a IBOARD you may change |
| 14 | + the radio-pins below if you hardware mod your iBoard. see [URL BELOW] for more details. |
| 15 | + http://forum.mysensors.org/topic/224/iboard-cheap-single-board-ethernet-arduino-with-radio/5 |
17 | 16 |
|
18 | | -* openhab.cfg |
| 17 | +* Don't forget to look at the definitions in libraries\MySensors\MyMQTT.h! |
| 18 | +
|
| 19 | + define TCPDUMP and connect serial interface if you have problems, please write on |
| 20 | + http://forum.mysensors.org/ and explain your problem, include serial output. Don't forget to |
| 21 | + turn on DEBUG in libraries\MySensors\MyConfig.h also. |
| 22 | +
|
| 23 | + MQTT_FIRST_SENSORID is for 'DHCP' server in MyMQTT. You may limit the ID's with FIRST and LAST definition. |
| 24 | + If you want your manually configured below 20 set MQTT_FIRST_SENSORID to 20. |
| 25 | + To disable: set MQTT_FIRST_SENSORID to 255. |
| 26 | +
|
| 27 | + MQTT_BROKER_PREFIX is the leading prefix for your nodes. This can be only one char if like. |
| 28 | +
|
| 29 | + MQTT_SEND_SUBSCRIPTION is if you want the MyMQTT to send a empty payload message to your nodes. |
| 30 | + This can be useful if you want to send latest state back to the MQTT client. Just check if incoming |
| 31 | + message has any length or not. |
| 32 | + Example: if (msg.type==V_LIGHT && strlen(msg.getString())>0) otherwise the code might do strange things. |
| 33 | +
|
| 34 | +* Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/[SensorType] |
| 35 | + NodeID and SensorID is uint8 (0-255) number. |
| 36 | + Last segment is translation of the sensor type, look inside MyMQTT.cpp for the definitions. |
| 37 | + User can change this to their needs. We have also left some space for custom types. |
| 38 | +
|
| 39 | +Special: (sensor 255 reserved for special commands) |
| 40 | +You can receive a node sketch name with MyMQTT/20/255/Sketch_name (or version with _version) |
| 41 | +
|
| 42 | +To-do: |
| 43 | +Special commands : clear or set EEPROM Values, Send REBOOT and Receive reboot for MyMQTT itself. |
| 44 | +Be able to send ACK so client returns the data being sent. |
| 45 | +... Please come with ideas! |
| 46 | +
|
| 47 | +Test in more MQTT clients, So far tested in openhab and MyMQTT for Android (Not my creation) |
| 48 | +- http://www.openhab.org/ |
| 49 | +- https://play.google.com/store/apps/details?id=at.tripwire.mqtt.client&hl=en |
| 50 | +... Please notify me if you use this broker with other software. |
| 51 | +
|
| 52 | +
|
| 53 | +* Example Openhab configuration: |
| 54 | +openhab.cfg |
19 | 55 | --- |
20 | 56 | mqtt:mysensor.url=tcp://192.168.0.234:1883 |
21 | 57 | mqtt:mysensor.clientId=MQTT |
22 | 58 | --- |
23 | 59 |
|
24 | | -* items/test.items |
| 60 | +items/test.items |
25 | 61 | --- |
26 | 62 | Group test |
27 | | -Number Temp_test "Temp [%.1f °C]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Temperature:state:default]"} |
28 | | -Number Hum_test "Hum [%.1f %%]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Humidity:state:default]"} |
29 | | -Number test_test "test [%s]" (test) {mqtt="<[mysensor:MyMQTT/20/3/#:state:default]"} |
30 | | -Switch sw1 "sw 1" (test) {<[mysensor:MyMQTT/21/1/Light:command:MAP(1.map)]"} |
| 63 | +Number Temp_test "Temp [%.1f °C]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Temperature:state:default]"} |
| 64 | +Number Hum_test "Hum [%.1f %%]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Humidity:state:default]"} |
| 65 | +Number test_test "test [%s]" (test) {mqtt="<[mysensor:MyMQTT/20/3/#:state:default]"} |
| 66 | +Switch sw1 "sw 1" (test) {mqtt="<[mysensor:MyMQTT/21/1/Light:command:MAP(1on0off.map)]"} |
| 67 | +Switch sw2 "sw2 recieve example" (test) {mqtt=">[mysensor:MyMQTT/21/2/Light:command:ON:1], |
| 68 | + >[mysensor:MyMQTT/21/2/Light:command:OFF:0]"} |
| 69 | +Switch sw3 "sw2 send + recieve example" (test) {mqtt=">[mysensor:MyMQTT/21/2/Light:command:ON:1], |
| 70 | + >[mysensor:MyMQTT/21/2/Light:command:OFF:0],<[mysensor:MyMQTT/21/2/Light:command:MAP(1on0off.map)]"} |
| 71 | +String sketch20 "Node 20 Sketch name [%s]" (test) {mqtt="<[mysensor:MyMQTT/20/255/Sketch_name:state:default]"} |
| 72 | +String sketch21 "Node 21 Sketch name [%s]" (test) {mqtt="<[mysensor:MyMQTT/21/255/Sketch_name:state:default]"} |
31 | 73 | --- (Note; # = Wildcard character) |
32 | 74 |
|
33 | | -* sitemap/test.site |
| 75 | +sitemap/test.site |
34 | 76 | --- |
35 | 77 | sitemap demo label="Menu" |
36 | 78 | Frame label="Openhab" { |
37 | | - Group item=test label="Test group" |
| 79 | + Group item=test label="Test group" |
38 | 80 | } |
39 | 81 | --- |
40 | 82 |
|
41 | | -* transform/1.map |
| 83 | +transform/1on0off.map |
42 | 84 | --- |
43 | 85 | 1=ON |
44 | 86 | 0=OFF |
45 | 87 | --- |
46 | 88 |
|
47 | | -* Features: |
48 | | - - Supports automatic nodeID delegation |
49 | | - - Recieve sketchname and version Example: (openhab item) |
50 | | -String sketch20 "Node 20 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:MyMQTT/20/255/Sketch_name:state:default]"} |
51 | | -String sketch21 "Node 21 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:MyMQTT/21/255/Sketch_name:state:default]"} |
52 | | -[...] |
53 | | -
|
54 | | -* Todo: |
55 | | - - DOCUMENTATION... |
56 | | - - Special commands |
57 | | - Read and set EEPROM Values |
58 | | - Send Reboot, And reboot gateway itself. |
59 | | -... |
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. |
63 | 89 | */ |
64 | 90 |
|
| 91 | + |
65 | 92 | #include <SPI.h> |
66 | 93 | #include <MyMQTT.h> |
67 | 94 | #include <Ethernet.h> |
68 | 95 | #include <MsTimer2.h> |
69 | 96 |
|
70 | 97 |
|
71 | | -// Use this for IBOARD modded to use standard MISO/MOSI/SCK More information see; |
72 | | -// http://forum.mysensors.org/topic/224/iboard-cheap-single-board-ethernet-arduino-with-radio/5 |
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 |
| 98 | +// Use this for IBOARD modded to use standard MISO/MOSI/SCK, see note *1 above! |
| 99 | +/* |
| 100 | +#define RADIO_CE_PIN 3 // radio chip enable |
| 101 | +#define RADIO_SPI_SS_PIN 8 // radio SPI serial select |
| 102 | +#define RADIO_ERROR_LED_PIN A2 // Error led pin |
| 103 | +#define RADIO_RX_LED_PIN A1 // Receive led pin |
| 104 | +#define RADIO_TX_LED_PIN A0 // the PCB, on board LED*/ |
78 | 105 |
|
79 | 106 | //Use this for default configured pro mini / nano etc : |
| 107 | +///* |
80 | 108 | #define RADIO_CE_PIN 5 // radio chip enable |
81 | 109 | #define RADIO_SPI_SS_PIN 6 // radio SPI serial select |
82 | 110 | #define RADIO_ERROR_LED_PIN 7 // Error led pin |
83 | 111 | #define RADIO_RX_LED_PIN 8 // Receive led pin |
84 | | -#define RADIO_TX_LED_PIN 9 // the PCB, on board LED |
| 112 | +#define RADIO_TX_LED_PIN 9 // the PCB, on board LED*/ |
85 | 113 |
|
86 | | -#define IP_PORT 1883 // MQTT Listening port |
87 | | -IPAddress myIp ( 192, 168, 0, 234 ); // Configure your static ip-address here |
88 | | -byte mac[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - Change this! |
89 | | -// * NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Locally Administered Address Ranges. |
| 114 | +#define TCP_PORT 1883 // Set your MQTT Broker Listening port. |
| 115 | +IPAddress TCP_IP ( 192, 168, 0, 234 ); // Configure your static ip-address here |
| 116 | +byte TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - You should change this! see note *2 above! |
90 | 117 |
|
91 | | -EthernetServer server = EthernetServer(IP_PORT); |
| 118 | +EthernetServer server = EthernetServer(TCP_PORT); |
92 | 119 | 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); |
| 120 | + |
| 121 | +// -- Uncomment this constructor if you have leds and include button attached to your gateway |
| 122 | +//MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN); |
95 | 123 |
|
96 | 124 | void processEthernetMessages() { |
97 | | - char inputString[MQTT_MAX_PACKET_SIZE] = ""; |
98 | | - int inputSize = 0; |
99 | | - EthernetClient client = server.available(); |
100 | | - if (client) { |
101 | | - while (client.available()) { |
102 | | - char inChar = client.read(); |
103 | | - inputString[inputSize] = inChar; |
104 | | - inputSize++; |
105 | | - } |
106 | | - gw.processMQTTMessage(inputString, inputSize); |
107 | | - } |
| 125 | + char inputString[MQTT_MAX_PACKET_SIZE] = ""; |
| 126 | + int inputSize = 0; |
| 127 | + EthernetClient client = server.available(); |
| 128 | + if (client) { |
| 129 | + while (client.available()) { |
| 130 | + char inChar = client.read(); |
| 131 | + inputString[inputSize] = inChar; |
| 132 | + inputSize++; |
| 133 | + } |
| 134 | + gw.processMQTTMessage(inputString, inputSize); |
| 135 | + } |
108 | 136 | } |
109 | 137 |
|
110 | 138 | void writeEthernet(char *writeBuffer, int *writeSize) { |
111 | | - server.write(writeBuffer, *writeSize); // Should this really be *writeSize? |
| 139 | + server.write(writeBuffer, *writeSize); // Todo: Should this really be *writeSize? |
112 | 140 | } |
113 | 141 |
|
114 | 142 | void ledTimersInterrupt() { |
115 | | - gw.ledTimersInterrupt(); |
| 143 | + gw.ledTimersInterrupt(); |
116 | 144 | } |
117 | 145 |
|
118 | 146 | int main(void) { |
119 | | - init(); |
120 | | - Ethernet.begin(mac, myIp); |
121 | | - delay(1000); // Wait for Ethernet to get configured. |
122 | | - gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet); |
123 | | - server.begin(); |
124 | | - if (gw.isLedMode()) { |
125 | | - // Add led timer interrupt |
126 | | - MsTimer2::set(300, ledTimersInterrupt); |
127 | | - MsTimer2::start(); |
128 | | - } |
129 | | - while (1) { |
130 | | - processEthernetMessages(); |
131 | | - gw.processRadioMessage(); |
132 | | - } |
| 147 | + init(); |
| 148 | + Ethernet.begin(TCP_MAC, TCP_IP); |
| 149 | + delay(1000); // Wait for Ethernet to get configured. |
| 150 | + gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet); |
| 151 | + server.begin(); |
| 152 | + // Add led timer interrupt |
| 153 | + if (gw.isLedMode()) { |
| 154 | + MsTimer2::set(200, ledTimersInterrupt); |
| 155 | + MsTimer2::start(); |
| 156 | + } |
| 157 | + while (1) { |
| 158 | + processEthernetMessages(); |
| 159 | + gw.processRadioMessage(); |
| 160 | + } |
133 | 161 | } |
| 162 | + |
0 commit comments