Skip to content

Commit cbd78d6

Browse files
committed
Merge branch 'development' of https://github.com/mysensors/Arduino into development
2 parents f822f1a + 31813b3 commit cbd78d6

File tree

5 files changed

+591
-2
lines changed

5 files changed

+591
-2
lines changed

MQTTGateway/MQTTGateway.ino

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/* MyMQTT Gateway 0.1b
2+
3+
Created by Daniel Wiegert <[email protected]>
4+
Based on Mysensors Ethernet Gateway by Henrik Ekblad <[email protected]>
5+
Requires MySensors lib 1.4b
6+
http://www.mysensors.org
7+
8+
* Don't forget to look at the definitions in MyMQTT.h!
9+
* Don't forget to configure Radio pins, IP and MAC-address!
10+
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.
15+
16+
Example openhab setup: http://www.openhab.org/
17+
18+
* openhab.cfg
19+
---
20+
mqtt:mysensor.url=tcp://192.168.0.234:1883
21+
mqtt:mysensor.clientId=MQTT
22+
---
23+
24+
* items/test.items
25+
---
26+
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)]"}
31+
--- (Note; # = Wildcard character)
32+
33+
* sitemap/test.site
34+
---
35+
sitemap demo label="Menu"
36+
Frame label="Openhab" {
37+
Group item=test label="Test group"
38+
}
39+
---
40+
41+
* transform/1.map
42+
---
43+
1=ON
44+
0=OFF
45+
---
46+
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+
*/
64+
65+
#include <SPI.h>
66+
#include <MyMQTT.h>
67+
#include <Ethernet.h>
68+
#include <MsTimer2.h>
69+
70+
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
78+
79+
//Use this for default configured pro mini / nano etc :
80+
#define RADIO_CE_PIN 5 // radio chip enable
81+
#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
82+
#define RADIO_ERROR_LED_PIN 7 // Error led pin
83+
#define RADIO_RX_LED_PIN 8 // Receive led pin
84+
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED
85+
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.
90+
91+
EthernetServer server = EthernetServer(IP_PORT);
92+
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);
95+
96+
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+
}
108+
}
109+
110+
void writeEthernet(char *writeBuffer, int *writeSize) {
111+
server.write(writeBuffer, *writeSize); // Should this really be *writeSize?
112+
}
113+
114+
void ledTimersInterrupt() {
115+
gw.ledTimersInterrupt();
116+
}
117+
118+
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+
}
133+
}

0 commit comments

Comments
 (0)