Merge branch 'development' of https://github.com/mysensors/Arduino into development
This commit is contained in:
+108
-79
@@ -1,133 +1,162 @@
|
|||||||
/* MyMQTT Gateway 0.1b
|
/* MyMQTT Broker Gateway 0.1b
|
||||||
|
|
||||||
Created by Daniel Wiegert <daniel.wiegert@gmail.com>
|
Created by Daniel Wiegert <daniel.wiegert@gmail.com>
|
||||||
Based on Mysensors Ethernet Gateway by Henrik Ekblad <henrik.ekblad@gmail.com>
|
Based on MySensors Ethernet Gateway by Henrik Ekblad <henrik.ekblad@gmail.com>
|
||||||
Requires MySensors lib 1.4b
|
http://www.mysensors.org
|
||||||
http://www.mysensors.org
|
|
||||||
|
|
||||||
* Don't forget to look at the definitions in MyMQTT.h!
|
Requires MySensors lib 1.4b
|
||||||
* Don't forget to configure Radio pins, IP and MAC-address!
|
|
||||||
|
|
||||||
* Address-layout is : [MQTT_BROKER_PREFIX]/[NodeID]/[SensorID]/Light
|
* Change below; TCP_IP, TCP_PORT, TCP_MAC
|
||||||
NodeID and SensorID is number (0-255).
|
This will listen on your selected TCP_IP:TCP_PORT below, Please change TCP_MAC your liking also.
|
||||||
Last segment is translation of the sensor type, look inside MyMQTT.cpp for
|
*1 -> NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Local Ranges.
|
||||||
the definitions. User can change this to their needs.
|
|
||||||
|
|
||||||
Example openhab setup: http://www.openhab.org/
|
*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
|
||||||
|
|
||||||
* openhab.cfg
|
* 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
|
||||||
|
turn on DEBUG in libraries\MySensors\MyConfig.h also.
|
||||||
|
|
||||||
|
MQTT_FIRST_SENSORID is for 'DHCP' server in MyMQTT. You may limit the ID's with FIRST and LAST definition.
|
||||||
|
If you want your manually configured below 20 set MQTT_FIRST_SENSORID to 20.
|
||||||
|
To disable: set MQTT_FIRST_SENSORID to 255.
|
||||||
|
|
||||||
|
MQTT_BROKER_PREFIX is the leading prefix for your nodes. This can be only one char if like.
|
||||||
|
|
||||||
|
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]
|
||||||
|
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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
* Example Openhab configuration:
|
||||||
|
openhab.cfg
|
||||||
---
|
---
|
||||||
mqtt:mysensor.url=tcp://192.168.0.234:1883
|
mqtt:mysensor.url=tcp://192.168.0.234:1883
|
||||||
mqtt:mysensor.clientId=MQTT
|
mqtt:mysensor.clientId=MQTT
|
||||||
---
|
---
|
||||||
|
|
||||||
* items/test.items
|
items/test.items
|
||||||
---
|
---
|
||||||
Group test
|
Group test
|
||||||
Number Temp_test "Temp [%.1f °C]" (test) {mqtt="<[mysensor:MyMQTT/20/#/Temperature:state:default]"}
|
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 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]"}
|
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)]"}
|
Switch sw1 "sw 1" (test) {mqtt="<[mysensor:MyMQTT/21/1/Light:command:MAP(1on0off.map)]"}
|
||||||
|
Switch sw2 "sw2 recieve example" (test) {mqtt=">[mysensor:MyMQTT/21/2/Light:command:ON:1],
|
||||||
|
>[mysensor:MyMQTT/21/2/Light:command:OFF:0]"}
|
||||||
|
Switch sw3 "sw2 send + recieve example" (test) {mqtt=">[mysensor:MyMQTT/21/2/Light:command:ON:1],
|
||||||
|
>[mysensor:MyMQTT/21/2/Light:command:OFF:0],<[mysensor:MyMQTT/21/2/Light:command:MAP(1on0off.map)]"}
|
||||||
|
String sketch20 "Node 20 Sketch name [%s]" (test) {mqtt="<[mysensor:MyMQTT/20/255/Sketch_name:state:default]"}
|
||||||
|
String sketch21 "Node 21 Sketch name [%s]" (test) {mqtt="<[mysensor:MyMQTT/21/255/Sketch_name:state:default]"}
|
||||||
--- (Note; # = Wildcard character)
|
--- (Note; # = Wildcard character)
|
||||||
|
|
||||||
* sitemap/test.site
|
sitemap/test.site
|
||||||
---
|
---
|
||||||
sitemap demo label="Menu"
|
sitemap demo label="Menu"
|
||||||
Frame label="Openhab" {
|
Frame label="Openhab" {
|
||||||
Group item=test label="Test group"
|
Group item=test label="Test group"
|
||||||
}
|
}
|
||||||
---
|
---
|
||||||
|
|
||||||
* transform/1.map
|
transform/1on0off.map
|
||||||
---
|
---
|
||||||
1=ON
|
1=ON
|
||||||
0=OFF
|
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.
|
|
||||||
...
|
|
||||||
|
|
||||||
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 <SPI.h>
|
#include <SPI.h>
|
||||||
#include <MyMQTT.h>
|
#include <MyMQTT.h>
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
#include <MsTimer2.h>
|
#include <MsTimer2.h>
|
||||||
|
|
||||||
|
|
||||||
// Use this for IBOARD modded to use standard MISO/MOSI/SCK More information see;
|
// Use this for IBOARD modded to use standard MISO/MOSI/SCK, see note *1 above!
|
||||||
// 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_CE_PIN 3 // radio chip enable
|
||||||
//#define RADIO_SPI_SS_PIN 8 // radio SPI serial select
|
#define RADIO_SPI_SS_PIN 8 // radio SPI serial select
|
||||||
//#define RADIO_ERROR_LED_PIN A2 // Error led pin
|
#define RADIO_ERROR_LED_PIN A2 // Error led pin
|
||||||
//#define RADIO_RX_LED_PIN A1 // Receive led pin
|
#define RADIO_RX_LED_PIN A1 // Receive led pin
|
||||||
//#define RADIO_TX_LED_PIN A0 // the PCB, on board LED
|
#define RADIO_TX_LED_PIN A0 // the PCB, on board LED*/
|
||||||
|
|
||||||
//Use this for default configured pro mini / nano etc :
|
//Use this for default configured pro mini / nano etc :
|
||||||
|
///*
|
||||||
#define RADIO_CE_PIN 5 // radio chip enable
|
#define RADIO_CE_PIN 5 // radio chip enable
|
||||||
#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
|
#define RADIO_SPI_SS_PIN 6 // radio SPI serial select
|
||||||
#define RADIO_ERROR_LED_PIN 7 // Error led pin
|
#define RADIO_ERROR_LED_PIN 7 // Error led pin
|
||||||
#define RADIO_RX_LED_PIN 8 // Receive led pin
|
#define RADIO_RX_LED_PIN 8 // Receive led pin
|
||||||
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED
|
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED*/
|
||||||
|
|
||||||
#define IP_PORT 1883 // MQTT Listening port
|
#define TCP_PORT 1883 // Set your MQTT Broker Listening port.
|
||||||
IPAddress myIp ( 192, 168, 0, 234 ); // Configure your static ip-address here
|
IPAddress TCP_IP ( 192, 168, 0, 234 ); // Configure your static ip-address here
|
||||||
byte mac[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - Change this!
|
byte TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - You should change this! see note *2 above!
|
||||||
// * NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Locally Administered Address Ranges.
|
|
||||||
|
|
||||||
EthernetServer server = EthernetServer(IP_PORT);
|
EthernetServer server = EthernetServer(TCP_PORT);
|
||||||
MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
|
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);
|
// -- 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() {
|
void processEthernetMessages() {
|
||||||
char inputString[MQTT_MAX_PACKET_SIZE] = "";
|
char inputString[MQTT_MAX_PACKET_SIZE] = "";
|
||||||
int inputSize = 0;
|
int inputSize = 0;
|
||||||
EthernetClient client = server.available();
|
EthernetClient client = server.available();
|
||||||
if (client) {
|
if (client) {
|
||||||
while (client.available()) {
|
while (client.available()) {
|
||||||
char inChar = client.read();
|
char inChar = client.read();
|
||||||
inputString[inputSize] = inChar;
|
inputString[inputSize] = inChar;
|
||||||
inputSize++;
|
inputSize++;
|
||||||
}
|
}
|
||||||
gw.processMQTTMessage(inputString, inputSize);
|
gw.processMQTTMessage(inputString, inputSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeEthernet(char *writeBuffer, int *writeSize) {
|
void writeEthernet(char *writeBuffer, int *writeSize) {
|
||||||
server.write(writeBuffer, *writeSize); // Should this really be *writeSize?
|
server.write(writeBuffer, *writeSize); // Todo: Should this really be *writeSize?
|
||||||
}
|
}
|
||||||
|
|
||||||
void ledTimersInterrupt() {
|
void ledTimersInterrupt() {
|
||||||
gw.ledTimersInterrupt();
|
gw.ledTimersInterrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
init();
|
init();
|
||||||
Ethernet.begin(mac, myIp);
|
Ethernet.begin(TCP_MAC, TCP_IP);
|
||||||
delay(1000); // Wait for Ethernet to get configured.
|
delay(1000); // Wait for Ethernet to get configured.
|
||||||
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
|
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
|
||||||
server.begin();
|
server.begin();
|
||||||
if (gw.isLedMode()) {
|
// Add led timer interrupt
|
||||||
// Add led timer interrupt
|
if (gw.isLedMode()) {
|
||||||
MsTimer2::set(300, ledTimersInterrupt);
|
MsTimer2::set(200, ledTimersInterrupt);
|
||||||
MsTimer2::start();
|
MsTimer2::start();
|
||||||
}
|
}
|
||||||
while (1) {
|
while (1) {
|
||||||
processEthernetMessages();
|
processEthernetMessages();
|
||||||
gw.processRadioMessage();
|
gw.processRadioMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,44 +12,45 @@ version 2 as published by the Free Software Foundation.
|
|||||||
#include "MyMQTT.h"
|
#include "MyMQTT.h"
|
||||||
|
|
||||||
char broker[] PROGMEM = MQTT_BROKER_PREFIX;
|
char broker[] PROGMEM = MQTT_BROKER_PREFIX;
|
||||||
char S_0[] PROGMEM = "Temperature";
|
|
||||||
char S_1[] PROGMEM = "Humidity";
|
char S_0[] PROGMEM = "Temperature"; //V_TEMP
|
||||||
char S_2[] PROGMEM = "Light";
|
char S_1[] PROGMEM = "Humidity"; //V_HUM
|
||||||
char S_3[] PROGMEM = "Dimmer";
|
char S_2[] PROGMEM = "Light"; //V_LIGHT
|
||||||
char S_4[] PROGMEM = "Pressure";
|
char S_3[] PROGMEM = "Dimmer"; //V_DIMMER
|
||||||
char S_5[] PROGMEM = "Forecast";
|
char S_4[] PROGMEM = "Pressure"; //V_PRESSURE
|
||||||
char S_6[] PROGMEM = "Rain";
|
char S_5[] PROGMEM = "Forecast"; //V_FORECAST
|
||||||
char S_7[] PROGMEM = "Rainrate";
|
char S_6[] PROGMEM = "Rain"; //V_RAIN
|
||||||
char S_8[] PROGMEM = "Wind";
|
char S_7[] PROGMEM = "Rainrate"; //V_RAINRATE
|
||||||
char S_9[] PROGMEM = "Gust";
|
char S_8[] PROGMEM = "Wind"; //V_WIND
|
||||||
char S_10[] PROGMEM = "Direction";
|
char S_9[] PROGMEM = "Gust"; //V_GUST
|
||||||
char S_11[] PROGMEM = "UV";
|
char S_10[] PROGMEM = "Direction"; //V_DIRECTION
|
||||||
char S_12[] PROGMEM = "Weight";
|
char S_11[] PROGMEM = "UV"; //V_UV
|
||||||
char S_13[] PROGMEM = "Distance";
|
char S_12[] PROGMEM = "Weight"; //V_WEIGHT
|
||||||
char S_14[] PROGMEM = "Impedance";
|
char S_13[] PROGMEM = "Distance"; //V_DISTANCE
|
||||||
char S_15[] PROGMEM = "Armed";
|
char S_14[] PROGMEM = "Impedance"; //V_IMPEDANCE
|
||||||
char S_16[] PROGMEM = "Tripped";
|
char S_15[] PROGMEM = "Armed"; //V_ARMED
|
||||||
char S_17[] PROGMEM = "Watt";
|
char S_16[] PROGMEM = "Tripped"; //V_TRIPPED
|
||||||
char S_18[] PROGMEM = "KWH";
|
char S_17[] PROGMEM = "Watt"; //V_WATT
|
||||||
char S_19[] PROGMEM = "Scene_ON";
|
char S_18[] PROGMEM = "KWH"; //V_KWH
|
||||||
char S_20[] PROGMEM = "Scene_OFF";
|
char S_19[] PROGMEM = "Scene_ON"; //V_SCENE_ON
|
||||||
char S_21[] PROGMEM = "Heater";
|
char S_20[] PROGMEM = "Scene_OFF"; //V_SCENE_OFF
|
||||||
char S_22[] PROGMEM = "Heater_SW";
|
char S_21[] PROGMEM = "Heater"; //V_HEATER
|
||||||
char S_23[] PROGMEM = "LightLVL";
|
char S_22[] PROGMEM = "Heater_SW"; //V_HEATER_SW
|
||||||
char S_24[] PROGMEM = "Var1";
|
char S_23[] PROGMEM = "LightLevel"; //V_LIGHT_LEVEL
|
||||||
char S_25[] PROGMEM = "Var2";
|
char S_24[] PROGMEM = "Var1"; //V_VAR1
|
||||||
char S_26[] PROGMEM = "Var3";
|
char S_25[] PROGMEM = "Var2"; //V_VAR2
|
||||||
char S_27[] PROGMEM = "Var4";
|
char S_26[] PROGMEM = "Var3"; //V_VAR3
|
||||||
char S_28[] PROGMEM = "Var5,";
|
char S_27[] PROGMEM = "Var4"; //V_VAR4
|
||||||
char S_29[] PROGMEM = "Up";
|
char S_28[] PROGMEM = "Var5,"; //V_VAR5
|
||||||
char S_30[] PROGMEM = "Down";
|
char S_29[] PROGMEM = "Up"; //V_UP
|
||||||
char S_31[] PROGMEM = "Stop";
|
char S_30[] PROGMEM = "Down"; //V_DOWN
|
||||||
char S_32[] PROGMEM = "IR_Send";
|
char S_31[] PROGMEM = "Stop"; //V_STOP
|
||||||
char S_33[] PROGMEM = "IR_Receive";
|
char S_32[] PROGMEM = "IR_Send"; //V_IR_SEND
|
||||||
char S_34[] PROGMEM = "Flow";
|
char S_33[] PROGMEM = "IR_Receive"; //V_IR_RECEIVE
|
||||||
char S_35[] PROGMEM = "Volume";
|
char S_34[] PROGMEM = "Flow"; //V_FLOW
|
||||||
char S_36[] PROGMEM = "Lock_Status";
|
char S_35[] PROGMEM = "Volume"; //V_VOLUME
|
||||||
char S_37[] PROGMEM = "Volt_Batt"; // Does not exist in mysensors lib!
|
char S_36[] PROGMEM = "Lock_Status";//V_LOCK_STATUS
|
||||||
|
char S_37[] PROGMEM = "DustLevel"; //V_DUST_LEVEL
|
||||||
char S_38[] PROGMEM = "";
|
char S_38[] PROGMEM = "";
|
||||||
char S_39[] PROGMEM = "";
|
char S_39[] PROGMEM = "";
|
||||||
char S_40[] PROGMEM = "";
|
char S_40[] PROGMEM = "";
|
||||||
@@ -57,20 +58,21 @@ char S_41[] PROGMEM = "";
|
|||||||
char S_42[] PROGMEM = "";
|
char S_42[] PROGMEM = "";
|
||||||
char S_43[] PROGMEM = "";
|
char S_43[] PROGMEM = "";
|
||||||
char S_44[] PROGMEM = "";
|
char S_44[] PROGMEM = "";
|
||||||
char S_45[] PROGMEM = "";
|
char S_45[] PROGMEM = "Volt_Batt"; // Does not exist in MyMessage.h!
|
||||||
char S_46[] PROGMEM = "";
|
char S_46[] PROGMEM = "";
|
||||||
char S_47[] PROGMEM = "";
|
char S_47[] PROGMEM = "";
|
||||||
char S_48[] PROGMEM = "";
|
char S_48[] PROGMEM = "";
|
||||||
char S_49[] PROGMEM = "Sketch_name"; // Does not exist in mysensors lib!
|
char S_49[] PROGMEM = "Sketch_name"; // Does not exist in MyMessage.h!
|
||||||
char S_50[] PROGMEM = "Sketch_version"; // Does not exist in mysensors lib!
|
char S_50[] PROGMEM = "Sketch_version"; // Does not exist in MyMessage.h!
|
||||||
|
|
||||||
|
|
||||||
PROGMEM const char *sType[] =
|
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_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_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};
|
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) :
|
||||||
@@ -118,7 +120,7 @@ void MyMQTT::processRadioMessage() {
|
|||||||
// A new message was received from one of the sensors
|
// A new message was received from one of the sensors
|
||||||
MyMessage message = getLastMessage();
|
MyMessage message = getLastMessage();
|
||||||
// Pass along the message from sensors to serial line
|
// Pass along the message from sensors to serial line
|
||||||
rxBlink(3);
|
rxBlink(1);
|
||||||
SendMQTT(message);
|
SendMQTT(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -179,22 +181,17 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
|
|||||||
strncat(buffer,inputString+4,inputString[3]);
|
strncat(buffer,inputString+4,inputString[3]);
|
||||||
}
|
}
|
||||||
msg.sender = GATEWAY_ADDRESS;
|
msg.sender = GATEWAY_ADDRESS;
|
||||||
for (str = strtok_r(buffer, "/", &p); // split using semicolon
|
for (str = strtok_r(buffer,"/",&p) ; str && i<4 ; str = strtok_r(NULL,"/",&p)) { //split using semicolon
|
||||||
str && i < 4; str = strtok_r(NULL, "/", &p) //get subsequent tokens (?)
|
// TODO: Check if we should send ack or not.
|
||||||
) {
|
if (i == 0) {
|
||||||
switch (i) {
|
|
||||||
case 0: // Radioid (destination)
|
|
||||||
if (strcmp_P(str,broker)!=0) {
|
if (strcmp_P(str,broker)!=0) {
|
||||||
return; //Message not for us or malformatted!
|
return; //Message not for us or malformatted!
|
||||||
}
|
}
|
||||||
break;
|
} else if (i==1) {
|
||||||
case 1:
|
|
||||||
msg.destination = atoi(str);
|
msg.destination = atoi(str);
|
||||||
break;
|
} else if (i==2) {
|
||||||
case 2:
|
|
||||||
msg.sensor = atoi(str);
|
msg.sensor = atoi(str);
|
||||||
break;
|
} else if (i==3) {
|
||||||
case 3:
|
|
||||||
char match=0;
|
char match=0;
|
||||||
for (int j=0; strcpy_P(convBuf, (char*)pgm_read_word(&(sType[j]))) ; j++) {
|
for (int j=0; strcpy_P(convBuf, (char*)pgm_read_word(&(sType[j]))) ; j++) {
|
||||||
if (strcmp(str,convBuf)==0) {
|
if (strcmp(str,convBuf)==0) {
|
||||||
@@ -203,7 +200,6 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
msg.type = match;
|
msg.type = match;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@@ -225,11 +221,11 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
|
|||||||
|
|
||||||
|
|
||||||
void MyMQTT::SendMQTT(MyMessage &msg) {
|
void MyMQTT::SendMQTT(MyMessage &msg) {
|
||||||
//serial(PSTR("%d;%d;%d;%d;%s\n"),msg.sender, msg.sensor, mGetCommand(msg), msg.type, msg.getString(convBuf));
|
//serial(PSTR("%d;%d;%d;%d;%s\n"),msg.sender, msg.sensor, mGetCommand(msg), msg.type, msg.getString(convBuf));
|
||||||
buffsize = 0;
|
buffsize = 0;
|
||||||
if (!MQTTClientConnected) return;
|
if (!MQTTClientConnected) return;
|
||||||
if (msg.isAck()) {
|
if (msg.isAck()) {
|
||||||
Serial.println("msg is ack!");
|
Serial.println("msg is ack!");
|
||||||
if (msg.sender==255 && mGetCommand(msg)==C_INTERNAL && msg.type==I_ID_REQUEST) {
|
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.
|
// The idea was to confirm id and save to EEPROM_LATEST_NODE_ADDRESS.
|
||||||
@@ -253,7 +249,7 @@ Serial.println("msg is ack!");
|
|||||||
msg.sender = GATEWAY_ADDRESS;
|
msg.sender = GATEWAY_ADDRESS;
|
||||||
msg.sensor = 255; //SensorID
|
msg.sensor = 255; //SensorID
|
||||||
msg.type = I_CONFIG; //msgType
|
msg.type = I_CONFIG; //msgType
|
||||||
mSetCommand(msg,C_INTERNAL); //Subtype
|
mSetCommand(msg,C_INTERNAL);//Subtype
|
||||||
mSetRequestAck(msg,false);
|
mSetRequestAck(msg,false);
|
||||||
mSetAck(msg,false);
|
mSetAck(msg,false);
|
||||||
msg.set("M");
|
msg.set("M");
|
||||||
@@ -277,7 +273,7 @@ Serial.println("msg is ack!");
|
|||||||
mSetVersion(msg, PROTOCOL_VERSION);
|
mSetVersion(msg, PROTOCOL_VERSION);
|
||||||
txBlink(1);
|
txBlink(1);
|
||||||
if (!sendRoute(msg)) errBlink(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) {
|
} else if (mGetCommand(msg)!=0) {
|
||||||
if (mGetCommand(msg)==3) msg.type=msg.type+38;
|
if (mGetCommand(msg)==3) msg.type=msg.type+38;
|
||||||
buffer[buffsize++] = MQTTPUBLISH << 4; // 0:
|
buffer[buffsize++] = MQTTPUBLISH << 4; // 0:
|
||||||
@@ -313,41 +309,42 @@ boolean MyMQTT::isLedMode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MyMQTT::ledTimersInterrupt() {
|
void MyMQTT::ledTimersInterrupt() {
|
||||||
if(countRx && countRx != 255) {
|
if(countRx && countRx != 255) {
|
||||||
// switch led on
|
// switch led on
|
||||||
digitalWrite(pinRx, LOW);
|
digitalWrite(pinRx, LOW);
|
||||||
} else if(!countRx) {
|
} else if(!countRx) {
|
||||||
// switching off
|
// switching off
|
||||||
digitalWrite(pinRx, HIGH);
|
digitalWrite(pinRx, HIGH);
|
||||||
}
|
}
|
||||||
if(countRx != 255) { countRx--; }
|
if(countRx != 255) { countRx--; }
|
||||||
|
|
||||||
if(countTx && countTx != 255) {
|
if(countTx && countTx != 255) {
|
||||||
// switch led on
|
// switch led on
|
||||||
digitalWrite(pinTx, LOW);
|
digitalWrite(pinTx, LOW);
|
||||||
} else if(!countTx) {
|
} else if(!countTx) {
|
||||||
// switching off
|
// switching off
|
||||||
digitalWrite(pinTx, HIGH);
|
digitalWrite(pinTx, HIGH);
|
||||||
}
|
}
|
||||||
if(countTx != 255) { countTx--; }
|
if(countTx != 255) { countTx--; }
|
||||||
if(countErr && countErr != 255) {
|
|
||||||
// switch led on
|
if(countErr && countErr != 255) {
|
||||||
digitalWrite(pinEr, LOW);
|
// switch led on
|
||||||
} else if(!countErr) {
|
digitalWrite(pinEr, LOW);
|
||||||
// switching off
|
} else if(!countErr) {
|
||||||
digitalWrite(pinEr, HIGH);
|
// switching off
|
||||||
}
|
digitalWrite(pinEr, HIGH);
|
||||||
if(countErr != 255) { countErr--; }
|
}
|
||||||
|
if(countErr != 255) { countErr--; }
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyMQTT::rxBlink(uint8_t cnt) {
|
void MyMQTT::rxBlink(uint8_t cnt) {
|
||||||
if(countRx == 255) { countRx = cnt; }
|
if(countRx == 255) { countRx = cnt; }
|
||||||
}
|
}
|
||||||
void MyMQTT::txBlink(uint8_t cnt) {
|
void MyMQTT::txBlink(uint8_t cnt) {
|
||||||
if(countTx == 255) { countTx = cnt; }
|
if(countTx == 255) { countTx = cnt; }
|
||||||
}
|
}
|
||||||
void MyMQTT::errBlink(uint8_t cnt) {
|
void MyMQTT::errBlink(uint8_t cnt) {
|
||||||
if(countErr == 255) { countErr = cnt; }
|
if(countErr == 255) { countErr = cnt; }
|
||||||
}
|
}
|
||||||
|
|
||||||
char MyMQTT::strncpysType_retL(char *str, char index, char start) {
|
char MyMQTT::strncpysType_retL(char *str, char index, char start) {
|
||||||
|
|||||||
@@ -14,13 +14,14 @@ version 2 as published by the Free Software Foundation.
|
|||||||
|
|
||||||
#include "MySensor.h"
|
#include "MySensor.h"
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#define TCPDUMP // Dump TCP packages.
|
#define TCPDUMP // Dump TCP packages.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MQTT_FIRST_SENSORID 20 // If you want manually configured nodes below this value.
|
#define MQTT_FIRST_SENSORID 20 // If you want manually configured nodes below this value. 255 = Disable
|
||||||
#define MQTT_LAST_SENSORID 254 // 254 is max! 255 reserved.
|
#define MQTT_LAST_SENSORID 254 // 254 is max! 255 reserved.
|
||||||
#define MQTT_BROKER_PREFIX "MyMQTT" // First prefix in MQTT tree, keep short!
|
#define MQTT_BROKER_PREFIX "MyMQTT" // First prefix in MQTT tree, keep short!
|
||||||
#define MQTT_SEND_SUBSCRIPTION 1 // Send empty payload (request) to node upon MQTT client subscribe request.
|
#define MQTT_SEND_SUBSCRIPTION 1 // Send empty payload (request) to node upon MQTT client subscribe request.
|
||||||
@@ -53,8 +54,6 @@ version 2 as published by the Free Software Foundation.
|
|||||||
#define MQTTQOS1 (1 << 1)
|
#define MQTTQOS1 (1 << 1)
|
||||||
#define MQTTQOS2 (2 << 1)
|
#define MQTTQOS2 (2 << 1)
|
||||||
|
|
||||||
#define MyTestVar F("Test")
|
|
||||||
|
|
||||||
class MyMQTT :
|
class MyMQTT :
|
||||||
public MySensor {
|
public MySensor {
|
||||||
public:
|
public:
|
||||||
@@ -87,4 +86,6 @@ private:
|
|||||||
void errBlink(uint8_t cnt);
|
void errBlink(uint8_t cnt);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Regular → Executable
+33
-46
@@ -64,28 +64,21 @@ char* MyMessage::getString(char *buffer) const {
|
|||||||
buffer[miGetLength()] = 0;
|
buffer[miGetLength()] = 0;
|
||||||
return buffer;
|
return buffer;
|
||||||
} else if (buffer != NULL) {
|
} else if (buffer != NULL) {
|
||||||
switch(payloadType) {
|
if (payloadType == P_BYTE) {
|
||||||
case P_BYTE:
|
itoa(bValue, buffer, 10);
|
||||||
itoa(bValue, buffer, 10);
|
} else if (payloadType == P_INT16) {
|
||||||
break;
|
itoa(iValue, buffer, 10);
|
||||||
case P_INT16:
|
} else if (payloadType == P_UINT16) {
|
||||||
itoa(iValue, buffer, 10);
|
utoa(uiValue, buffer, 10);
|
||||||
break;
|
} else if (payloadType == P_LONG32) {
|
||||||
case P_UINT16:
|
ltoa(lValue, buffer, 10);
|
||||||
utoa(uiValue, buffer, 10);
|
} else if (payloadType == P_ULONG32) {
|
||||||
break;
|
|
||||||
case P_LONG32:
|
ultoa(ulValue, buffer, 10);
|
||||||
ltoa(lValue, buffer, 10);
|
} else if (payloadType == P_FLOAT32) {
|
||||||
break;
|
dtostrf(fValue,2,fPrecision,buffer);
|
||||||
case P_ULONG32:
|
} else if (payloadType == P_CUSTOM) {
|
||||||
ultoa(ulValue, buffer, 10);
|
return getStream(buffer);
|
||||||
break;
|
|
||||||
case P_FLOAT32:
|
|
||||||
dtostrf(fValue,2,fPrecision,buffer);
|
|
||||||
break;
|
|
||||||
case P_CUSTOM:
|
|
||||||
return getStream(buffer);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
return buffer;
|
return buffer;
|
||||||
} else {
|
} else {
|
||||||
@@ -94,12 +87,11 @@ char* MyMessage::getString(char *buffer) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint8_t MyMessage::getByte() const {
|
uint8_t MyMessage::getByte() const {
|
||||||
switch(miGetPayloadType()) {
|
if (miGetPayloadType() == P_BYTE) {
|
||||||
case P_BYTE:
|
|
||||||
return data[0];
|
return data[0];
|
||||||
case P_STRING:
|
} else if (miGetPayloadType() == P_STRING) {
|
||||||
return atoi(data);
|
return atoi(data);
|
||||||
default:
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,56 +101,51 @@ bool MyMessage::getBool() const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float MyMessage::getFloat() const {
|
float MyMessage::getFloat() const {
|
||||||
switch(miGetPayloadType()) {
|
if (miGetPayloadType() == P_FLOAT32) {
|
||||||
case P_FLOAT32:
|
|
||||||
return fValue;
|
return fValue;
|
||||||
case P_STRING:
|
} else if (miGetPayloadType() == P_STRING) {
|
||||||
return atof(data);
|
return atof(data);
|
||||||
default:
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long MyMessage::getLong() const {
|
long MyMessage::getLong() const {
|
||||||
switch(miGetPayloadType()) {
|
if (miGetPayloadType() == P_LONG32) {
|
||||||
case P_LONG32:
|
|
||||||
return lValue;
|
return lValue;
|
||||||
case P_STRING:
|
} else if (miGetPayloadType() == P_STRING) {
|
||||||
return atol(data);
|
return atol(data);
|
||||||
default:
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long MyMessage::getULong() const {
|
unsigned long MyMessage::getULong() const {
|
||||||
switch(miGetPayloadType()) {
|
if (miGetPayloadType() == P_ULONG32) {
|
||||||
case P_ULONG32:
|
|
||||||
return ulValue;
|
return ulValue;
|
||||||
case P_STRING:
|
} else if (miGetPayloadType() == P_STRING) {
|
||||||
return atol(data);
|
return atol(data);
|
||||||
default:
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int MyMessage::getInt() const {
|
int MyMessage::getInt() const {
|
||||||
switch(miGetPayloadType()) {
|
if (miGetPayloadType() == P_INT16) {
|
||||||
case P_INT16:
|
|
||||||
return iValue;
|
return iValue;
|
||||||
case P_STRING:
|
} else if (miGetPayloadType() == P_STRING) {
|
||||||
return atoi(data);
|
return atoi(data);
|
||||||
default:
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int MyMessage::getUInt() const {
|
unsigned int MyMessage::getUInt() const {
|
||||||
switch(miGetPayloadType()) {
|
if (miGetPayloadType() == P_UINT16) {
|
||||||
case P_UINT16:
|
|
||||||
return uiValue;
|
return uiValue;
|
||||||
case P_STRING:
|
} else if (miGetPayloadType() == P_STRING) {
|
||||||
return atoi(data);
|
return atoi(data);
|
||||||
default:
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Regular → Executable
+5
-10
@@ -327,12 +327,10 @@ boolean MySensor::process() {
|
|||||||
} else if (sender == GATEWAY_ADDRESS) {
|
} else if (sender == GATEWAY_ADDRESS) {
|
||||||
bool isMetric;
|
bool isMetric;
|
||||||
|
|
||||||
switch (type) {
|
if (type == I_REBOOT) {
|
||||||
case I_REBOOT:
|
|
||||||
wdt_enable(WDTO_15MS);
|
wdt_enable(WDTO_15MS);
|
||||||
for (;;);
|
for (;;);
|
||||||
break;
|
} else if (type == I_ID_RESPONSE) {
|
||||||
case I_ID_RESPONSE:
|
|
||||||
if (nc.nodeId == AUTO) {
|
if (nc.nodeId == AUTO) {
|
||||||
nc.nodeId = msg.getByte();
|
nc.nodeId = msg.getByte();
|
||||||
// Write id to EEPROM
|
// Write id to EEPROM
|
||||||
@@ -346,8 +344,7 @@ boolean MySensor::process() {
|
|||||||
}
|
}
|
||||||
debug(PSTR("id=%d\n"), nc.nodeId);
|
debug(PSTR("id=%d\n"), nc.nodeId);
|
||||||
}
|
}
|
||||||
break;
|
} else if (type == I_CONFIG) {
|
||||||
case I_CONFIG:
|
|
||||||
// Pick up configuration from controller (currently only metric/imperial)
|
// Pick up configuration from controller (currently only metric/imperial)
|
||||||
// and store it in eeprom if changed
|
// and store it in eeprom if changed
|
||||||
isMetric = msg.getByte() == 'M' ;
|
isMetric = msg.getByte() == 'M' ;
|
||||||
@@ -356,8 +353,7 @@ boolean MySensor::process() {
|
|||||||
eeprom_write_byte((uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric);
|
eeprom_write_byte((uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric);
|
||||||
//eeprom_write_block((const void*)&cc, (uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, sizeof(ControllerConfig));
|
//eeprom_write_block((const void*)&cc, (uint8_t*)EEPROM_CONTROLLER_CONFIG_ADDRESS, sizeof(ControllerConfig));
|
||||||
}
|
}
|
||||||
break;
|
} else if (type == I_CHILDREN) {
|
||||||
case I_CHILDREN:
|
|
||||||
if (repeaterMode && msg.getByte() == 'C') {
|
if (repeaterMode && msg.getByte() == 'C') {
|
||||||
// Clears child relay data for this node
|
// Clears child relay data for this node
|
||||||
debug(PSTR("rd=clear\n"));
|
debug(PSTR("rd=clear\n"));
|
||||||
@@ -366,8 +362,7 @@ boolean MySensor::process() {
|
|||||||
}
|
}
|
||||||
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN,false).set(""));
|
sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN,false).set(""));
|
||||||
}
|
}
|
||||||
break;
|
} else if (type == I_TIME) {
|
||||||
case I_TIME:
|
|
||||||
if (timeCallback != NULL) {
|
if (timeCallback != NULL) {
|
||||||
// Deliver time to callback
|
// Deliver time to callback
|
||||||
timeCallback(msg.getULong());
|
timeCallback(msg.getULong());
|
||||||
|
|||||||
Reference in New Issue
Block a user