Skip to content

Commit 3af3014

Browse files
committed
New comments and minor fixes
1 parent b0917af commit 3af3014

File tree

3 files changed

+216
-186
lines changed

3 files changed

+216
-186
lines changed

MQTTGateway/MQTTGateway.ino

+105-76
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,162 @@
1-
/* MyMQTT Gateway 0.1b
1+
/* MyMQTT Broker Gateway 0.1b
22
33
Created by Daniel Wiegert <[email protected]>
4-
Based on Mysensors Ethernet Gateway by Henrik Ekblad <[email protected]>
4+
Based on MySensors Ethernet Gateway by Henrik Ekblad <[email protected]>
5+
http://www.mysensors.org
6+
57
Requires MySensors lib 1.4b
6-
http://www.mysensors.org
78
8-
* Don't forget to look at the definitions in MyMQTT.h!
9-
* Don't forget to configure Radio pins, IP and MAC-address!
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.
12+
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
16+
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.
1032
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.
33+
* Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/[SensorType]
34+
NodeID and SensorID is uint8 (0-255) number.
35+
Last segment is translation of the sensor type, look inside MyMQTT.cpp for the definitions.
36+
User can change this to their needs. We have also left some space for custom types.
1537
16-
Example openhab setup: http://www.openhab.org/
38+
Special: (sensor 255 reserved for special commands)
39+
You can receive a node sketch name with MyMQTT/20/255/Sketch_name (or version with _version)
1740
18-
* openhab.cfg
41+
To-do:
42+
Special commands : clear or set EEPROM Values, Send REBOOT and Receive reboot for MyMQTT itself.
43+
Be able to send ACK so client returns the data being sent.
44+
... Please come with ideas!
45+
46+
Test in more MQTT clients, So far tested in openhab and MyMQTT for Android (Not my creation)
47+
- http://www.openhab.org/
48+
- https://play.google.com/store/apps/details?id=at.tripwire.mqtt.client&hl=en
49+
... Please notify me if you use this broker with other software.
50+
51+
52+
* Example Openhab configuration:
53+
openhab.cfg
1954
---
2055
mqtt:mysensor.url=tcp://192.168.0.234:1883
2156
mqtt:mysensor.clientId=MQTT
2257
---
2358
24-
* items/test.items
59+
items/test.items
2560
---
2661
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)]"}
62+
Number Temp_test "Temp [%.1f °C]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Temperature:state:default]"}
63+
Number Hum_test "Hum [%.1f %%]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Humidity:state:default]"}
64+
Number test_test "test [%s]" (test) {mqtt="<[mysensor:MyMQTT/20/3/#:state:default]"}
65+
Switch sw1 "sw 1" (test) {mqtt="<[mysensor:MyMQTT/21/1/Light:command:MAP(1on0off.map)]"}
66+
Switch sw2 "sw2 recieve example" (test) {mqtt=">[mysensor:MyMQTT/21/2/Light:command:ON:1],
67+
>[mysensor:MyMQTT/21/2/Light:command:OFF:0]"}
68+
Switch sw3 "sw2 send + recieve example" (test) {mqtt=">[mysensor:MyMQTT/21/2/Light:command:ON:1],
69+
>[mysensor:MyMQTT/21/2/Light:command:OFF:0],<[mysensor:MyMQTT/21/2/Light:command:MAP(1on0off.map)]"}
70+
String sketch20 "Node 20 Sketch name [%s]" (test) {mqtt="<[mysensor:MyMQTT/20/255/Sketch_name:state:default]"}
71+
String sketch21 "Node 21 Sketch name [%s]" (test) {mqtt="<[mysensor:MyMQTT/21/255/Sketch_name:state:default]"}
3172
--- (Note; # = Wildcard character)
3273
33-
* sitemap/test.site
74+
sitemap/test.site
3475
---
3576
sitemap demo label="Menu"
3677
Frame label="Openhab" {
3778
Group item=test label="Test group"
3879
}
3980
---
4081
41-
* transform/1.map
82+
transform/1on0off.map
4283
---
4384
1=ON
4485
0=OFF
4586
---
4687
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.
6388
*/
6489

90+
6591
#include <SPI.h>
6692
#include <MyMQTT.h>
6793
#include <Ethernet.h>
6894
#include <MsTimer2.h>
6995

7096

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
97+
// Use this for IBOARD modded to use standard MISO/MOSI/SCK, see note *1 above!
98+
/*
99+
#define RADIO_CE_PIN 3 // radio chip enable
100+
#define RADIO_SPI_SS_PIN 8 // radio SPI serial select
101+
#define RADIO_ERROR_LED_PIN A2 // Error led pin
102+
#define RADIO_RX_LED_PIN A1 // Receive led pin
103+
#define RADIO_TX_LED_PIN A0 // the PCB, on board LED*/
78104

79105
//Use this for default configured pro mini / nano etc :
106+
///*
80107
#define RADIO_CE_PIN 5 // radio chip enable
81108
#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
82109
#define RADIO_ERROR_LED_PIN 7 // Error led pin
83110
#define RADIO_RX_LED_PIN 8 // Receive led pin
84-
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED
111+
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED*/
85112

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.
113+
#define TCP_PORT 1883 // Set your MQTT Broker Listening port.
114+
IPAddress TCP_IP ( 192, 168, 0, 234 ); // Configure your static ip-address here
115+
byte TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - You should change this! see note *2 above!
90116

91-
EthernetServer server = EthernetServer(IP_PORT);
117+
EthernetServer server = EthernetServer(TCP_PORT);
92118
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);
119+
120+
// -- Uncomment this constructor if you have leds and include button attached to your gateway
121+
//MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
95122

96123
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-
}
124+
char inputString[MQTT_MAX_PACKET_SIZE] = "";
125+
int inputSize = 0;
126+
EthernetClient client = server.available();
127+
if (client) {
128+
while (client.available()) {
129+
char inChar = client.read();
130+
inputString[inputSize] = inChar;
131+
inputSize++;
132+
}
133+
gw.processMQTTMessage(inputString, inputSize);
134+
}
108135
}
109136

110137
void writeEthernet(char *writeBuffer, int *writeSize) {
111-
server.write(writeBuffer, *writeSize); // Should this really be *writeSize?
138+
server.write(writeBuffer, *writeSize); // Todo: Should this really be *writeSize?
112139
}
113140

114141
void ledTimersInterrupt() {
115-
gw.ledTimersInterrupt();
142+
gw.ledTimersInterrupt();
116143
}
117144

118145
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-
}
146+
init();
147+
Ethernet.begin(TCP_MAC, TCP_IP);
148+
delay(1000); // Wait for Ethernet to get configured.
149+
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
150+
server.begin();
151+
// Add led timer interrupt
152+
if (gw.isLedMode()) {
153+
MsTimer2::set(200, ledTimersInterrupt);
154+
MsTimer2::start();
155+
}
156+
while (1) {
157+
processEthernetMessages();
158+
gw.processRadioMessage();
159+
}
133160
}
161+
162+

0 commit comments

Comments
 (0)