Proposal to keep gateways separated.
This commit is contained in:
@@ -43,11 +43,15 @@
|
||||
* -----------------------------------------------------------------------------------------------------------
|
||||
* Powering: both NRF24l01 radio and Ethernet(ENC28J60) uses 3.3V
|
||||
*/
|
||||
#define NO_PORTB_PINCHANGES
|
||||
|
||||
#include <SPI.h>
|
||||
#include <MySensor.h>
|
||||
#include <MyGateway.h>
|
||||
#include <MySensor.h>
|
||||
#include <stdarg.h>
|
||||
#include <MsTimer2.h>
|
||||
#include <PinChangeInt.h>
|
||||
#include "GatewayUtil.h"
|
||||
|
||||
|
||||
// Use this if you have attached a Ethernet ENC28J60 shields
|
||||
//#include <UIPEthernet.h>
|
||||
@@ -61,10 +65,15 @@
|
||||
|
||||
#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
|
||||
|
||||
|
||||
MySensor gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
|
||||
|
||||
|
||||
#define IP_PORT 5003 // The port you want to open
|
||||
IPAddress myIp (192, 168, 178, 66); // Configure your static ip-address here
|
||||
|
||||
@@ -76,22 +85,37 @@ byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // DEAD BEEF FEED
|
||||
// a R/W server on the port
|
||||
EthernetServer server = EthernetServer(IP_PORT);
|
||||
|
||||
// No blink or button functionality. Use the vanilla constructor.
|
||||
MyGateway gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, INCLUSION_MODE_TIME);
|
||||
|
||||
// Uncomment this constructor if you have leds and include button attached to your gateway
|
||||
//MyGateway gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN, INCLUSION_MODE_TIME, INCLUSION_MODE_PIN, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
|
||||
|
||||
|
||||
char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface
|
||||
int inputPos = 0;
|
||||
|
||||
|
||||
void output(const char *fmt, ... ) {
|
||||
va_list args;
|
||||
va_start (args, fmt );
|
||||
vsnprintf_P(serialBuffer, MAX_SEND_LENGTH, fmt, args);
|
||||
va_end (args);
|
||||
Serial.print(serialBuffer);
|
||||
server.write(serialBuffer);
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize gateway at maximum PA level, channel 70 and callback for write operations
|
||||
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet);
|
||||
gw.begin(incomingMessage, 0, true, 0, RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE);
|
||||
|
||||
Ethernet.begin(mac, myIp);
|
||||
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
|
||||
|
||||
|
||||
// Add led timer interrupt
|
||||
MsTimer2::set(300, ledTimersInterrupt);
|
||||
MsTimer2::start();
|
||||
|
||||
// Add interrupt for inclusion button to pin
|
||||
PCintPort::attachInterrupt(pinInclusion, startInclusionInterrupt, RISING);
|
||||
|
||||
|
||||
// give the Ethernet interface a second to initialize
|
||||
delay(1000);
|
||||
@@ -100,14 +124,15 @@ void setup()
|
||||
server.begin();
|
||||
}
|
||||
|
||||
// This will be called when data should be written to ethernet
|
||||
void writeEthernet(char *writeBuffer) {
|
||||
server.write(writeBuffer);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
gw.process();
|
||||
|
||||
|
||||
checkButtonTriggeredInclusion();
|
||||
checkInclusionFinished();
|
||||
|
||||
// if an incoming client connects, there will be
|
||||
// bytes available to read via the client object
|
||||
EthernetClient client = server.available();
|
||||
@@ -128,7 +153,7 @@ void loop()
|
||||
// echo the string to the serial port
|
||||
Serial.print(inputString);
|
||||
|
||||
gw.parseAndSend(inputString);
|
||||
parseAndSend(gw, inputString);
|
||||
|
||||
// clear the string:
|
||||
inputPos = 0;
|
||||
@@ -143,7 +168,8 @@ void loop()
|
||||
}
|
||||
}
|
||||
}
|
||||
gw.processRadioMessage();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
#ifndef __GATEWAYUTIL_H__
|
||||
#define __GATEWAYUTIL_H__
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
|
||||
uint8_t pinRx = 8; // Rx led pin
|
||||
uint8_t pinTx = 9; // Tx led pin
|
||||
uint8_t pinEr = 7; // Err led pin
|
||||
uint8_t inclusionTime = 1; // Number of minutes inclusion mode is enabled
|
||||
uint8_t pinInclusion = 3; // Input pin that should trigger inclusion mode
|
||||
|
||||
#define MAX_RECEIVE_LENGTH 100 // Max buffersize needed for messages coming from controller
|
||||
#define MAX_SEND_LENGTH 120 // Max buffersize needed for messages destined for controller
|
||||
|
||||
volatile boolean buttonTriggeredInclusion;
|
||||
volatile uint8_t countRx;
|
||||
volatile uint8_t countTx;
|
||||
volatile uint8_t countErr;
|
||||
boolean inclusionMode; // Keeps track on inclusion mode
|
||||
void (*serial)(const char *fmt, ... );
|
||||
|
||||
MyMessage msg;
|
||||
|
||||
void setInclusionMode(boolean newMode);
|
||||
void txBlink(uint8_t cnt);
|
||||
void rxBlink(uint8_t cnt);
|
||||
void errBlink(uint8_t cnt);
|
||||
|
||||
char convBuf[MAX_PAYLOAD*2+1];
|
||||
char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera
|
||||
unsigned long inclusionStartTime;
|
||||
|
||||
void setupGateway(uint8_t _rx, uint8_t _tx, uint8_t _er, uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
|
||||
inclusionMode = 0;
|
||||
buttonTriggeredInclusion = false;
|
||||
serial = _serial;
|
||||
|
||||
pinRx = _rx;
|
||||
pinTx = _tx;
|
||||
pinEr = _er;
|
||||
pinInclusion = _inc;
|
||||
inclusionTime = _incTime;
|
||||
|
||||
countRx = 0;
|
||||
countTx = 0;
|
||||
countErr = 0;
|
||||
|
||||
// Setup led pins
|
||||
pinMode(pinRx, OUTPUT);
|
||||
pinMode(pinTx, OUTPUT);
|
||||
pinMode(pinEr, OUTPUT);
|
||||
digitalWrite(pinRx, LOW);
|
||||
digitalWrite(pinTx, LOW);
|
||||
digitalWrite(pinEr, LOW);
|
||||
|
||||
// Setup digital in that triggers inclusion mode
|
||||
pinMode(pinInclusion, INPUT);
|
||||
digitalWrite(pinInclusion, HIGH);
|
||||
|
||||
// Set initial state of leds
|
||||
digitalWrite(pinRx, HIGH);
|
||||
digitalWrite(pinTx, HIGH);
|
||||
digitalWrite(pinEr, HIGH);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void startInclusionInterrupt() {
|
||||
buttonTriggeredInclusion = true;
|
||||
}
|
||||
|
||||
void incomingMessage(const MyMessage &message) {
|
||||
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
|
||||
rxBlink(3);
|
||||
} else {
|
||||
rxBlink(1);
|
||||
}
|
||||
// Pass along the message from sensors to serial line
|
||||
serial(PSTR("%d;%d;%d;%d;%d;%s\n"),message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type, message.getString(convBuf));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void checkButtonTriggeredInclusion() {
|
||||
if (buttonTriggeredInclusion) {
|
||||
// Ok, someone pressed the inclusion button on the gateway
|
||||
// start inclusion mode for 1 munute.
|
||||
serial(PSTR("0;0;%d;0;%d;Inclusion started by button.\n"), C_INTERNAL, I_LOG_MESSAGE);
|
||||
buttonTriggeredInclusion = false;
|
||||
setInclusionMode(true);
|
||||
}
|
||||
}
|
||||
|
||||
void checkInclusionFinished() {
|
||||
if (inclusionMode && millis()-inclusionStartTime>60000UL*inclusionTime) {
|
||||
// inclusionTimeInMinutes minute(s) has passed.. stop inclusion mode
|
||||
setInclusionMode(false);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t h2i(char c) {
|
||||
uint8_t i = 0;
|
||||
if (c <= '9')
|
||||
i += c - '0';
|
||||
else if (c >= 'a')
|
||||
i += c - 'a' + 10;
|
||||
else
|
||||
i += c - 'A' + 10;
|
||||
return i;
|
||||
}
|
||||
|
||||
void parseAndSend(MySensor gw, char *commandBuffer) {
|
||||
boolean ok = false;
|
||||
char *str, *p, *value=NULL;
|
||||
uint8_t bvalue[MAX_PAYLOAD];
|
||||
uint8_t blen = 0;
|
||||
int i = 0;
|
||||
uint16_t destination = 0;
|
||||
uint8_t sensor = 0;
|
||||
uint8_t command = 0;
|
||||
uint8_t type = 0;
|
||||
uint8_t ack = 0;
|
||||
|
||||
// Extract command data coming on serial line
|
||||
for (str = strtok_r(commandBuffer, ";", &p); // split using semicolon
|
||||
str && i < 6; // loop while str is not null an max 5 times
|
||||
str = strtok_r(NULL, ";", &p) // get subsequent tokens
|
||||
) {
|
||||
switch (i) {
|
||||
case 0: // Radioid (destination)
|
||||
destination = atoi(str);
|
||||
break;
|
||||
case 1: // Childid
|
||||
sensor = atoi(str);
|
||||
break;
|
||||
case 2: // Message type
|
||||
command = atoi(str);
|
||||
break;
|
||||
case 3: // Should we request ack from destination?
|
||||
ack = atoi(str);
|
||||
break;
|
||||
case 4: // Data type
|
||||
type = atoi(str);
|
||||
break;
|
||||
case 5: // Variable value
|
||||
if (command == C_STREAM) {
|
||||
blen = 0;
|
||||
uint8_t val;
|
||||
while (*str) {
|
||||
val = h2i(*str++) << 4;
|
||||
val += h2i(*str++);
|
||||
bvalue[blen] = val;
|
||||
blen++;
|
||||
}
|
||||
} else {
|
||||
value = str;
|
||||
// Remove ending carriage return character (if it exists)
|
||||
uint8_t lastCharacter = strlen(value)-1;
|
||||
if (value[lastCharacter] == '\r')
|
||||
value[lastCharacter] = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (destination==GATEWAY_ADDRESS && command==C_INTERNAL) {
|
||||
// Handle messages directed to gateway
|
||||
if (type == I_VERSION) {
|
||||
// Request for version
|
||||
serial(PSTR("0;0;%d;0;%d;%s\n"), C_INTERNAL, I_VERSION, LIBRARY_VERSION);
|
||||
} else if (type == I_INCLUSION_MODE) {
|
||||
// Request to change inclusion mode
|
||||
setInclusionMode(atoi(value) == 1);
|
||||
}
|
||||
} else {
|
||||
txBlink(1);
|
||||
msg.sender = GATEWAY_ADDRESS;
|
||||
msg.destination = destination;
|
||||
msg.sensor = sensor;
|
||||
msg.type = type;
|
||||
mSetCommand(msg,command);
|
||||
mSetRequestAck(msg,ack?1:0);
|
||||
mSetAck(msg,false);
|
||||
if (command == C_STREAM)
|
||||
msg.set(bvalue, blen);
|
||||
else
|
||||
msg.set(value);
|
||||
ok = gw.sendRoute(msg);
|
||||
if (!ok) {
|
||||
errBlink(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setInclusionMode(boolean newMode) {
|
||||
if (newMode != inclusionMode)
|
||||
inclusionMode = newMode;
|
||||
// Send back mode change on serial line to ack command
|
||||
serial(PSTR("0;0;%d;0;%d;%d\n"), C_INTERNAL, I_INCLUSION_MODE, inclusionMode?1:0);
|
||||
|
||||
if (inclusionMode) {
|
||||
inclusionStartTime = millis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void ledTimersInterrupt() {
|
||||
if(countRx && countRx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinRx, LOW);
|
||||
} else if(!countRx) {
|
||||
// switching off
|
||||
digitalWrite(pinRx, HIGH);
|
||||
}
|
||||
if(countRx != 255) { countRx--; }
|
||||
|
||||
if(countTx && countTx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinTx, LOW);
|
||||
} else if(!countTx) {
|
||||
// switching off
|
||||
digitalWrite(pinTx, HIGH);
|
||||
}
|
||||
if(countTx != 255) { countTx--; }
|
||||
else if(inclusionMode) { countTx = 8; }
|
||||
|
||||
if(countErr && countErr != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinEr, LOW);
|
||||
} else if(!countErr) {
|
||||
// switching off
|
||||
digitalWrite(pinEr, HIGH);
|
||||
}
|
||||
if(countErr != 255) { countErr--; }
|
||||
|
||||
}
|
||||
|
||||
void rxBlink(uint8_t cnt) {
|
||||
if(countRx == 255) { countRx = cnt; }
|
||||
}
|
||||
void txBlink(uint8_t cnt) {
|
||||
if(countTx == 255 && !inclusionMode) { countTx = cnt; }
|
||||
}
|
||||
void errBlink(uint8_t cnt) {
|
||||
if(countErr == 255) { countErr = cnt; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
#error This example is only for use on Arduino.
|
||||
#endif // ARDUINO
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,120 @@
|
||||
#ifndef __GATEWAYUTIL_H__
|
||||
#define __GATEWAYUTIL_H__
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
|
||||
uint8_t pinRx = 8; // Rx led pin
|
||||
uint8_t pinTx = 9; // Tx led pin
|
||||
uint8_t pinEr = 7; // Err led pin
|
||||
|
||||
#define MAX_RECEIVE_LENGTH 100 // Max buffersize needed for messages coming from controller
|
||||
#define MAX_SEND_LENGTH 120 // Max buffersize needed for messages destined for controller
|
||||
|
||||
volatile boolean buttonTriggeredInclusion;
|
||||
volatile uint8_t countRx;
|
||||
volatile uint8_t countTx;
|
||||
volatile uint8_t countErr;
|
||||
|
||||
MyMessage msg;
|
||||
|
||||
void setInclusionMode(boolean newMode);
|
||||
void txBlink(uint8_t cnt);
|
||||
void rxBlink(uint8_t cnt);
|
||||
void errBlink(uint8_t cnt);
|
||||
|
||||
char convBuf[MAX_PAYLOAD*2+1];
|
||||
char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera
|
||||
unsigned long inclusionStartTime;
|
||||
|
||||
void setupGateway(uint8_t _rx, uint8_t _tx, uint8_t _er, uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
|
||||
inclusionMode = 0;
|
||||
buttonTriggeredInclusion = false;
|
||||
serial = _serial;
|
||||
|
||||
pinRx = _rx;
|
||||
pinTx = _tx;
|
||||
pinEr = _er;
|
||||
pinInclusion = _inc;
|
||||
inclusionTime = _incTime;
|
||||
|
||||
countRx = 0;
|
||||
countTx = 0;
|
||||
countErr = 0;
|
||||
|
||||
// Setup led pins
|
||||
pinMode(pinRx, OUTPUT);
|
||||
pinMode(pinTx, OUTPUT);
|
||||
pinMode(pinEr, OUTPUT);
|
||||
digitalWrite(pinRx, LOW);
|
||||
digitalWrite(pinTx, LOW);
|
||||
digitalWrite(pinEr, LOW);
|
||||
|
||||
|
||||
// Set initial state of leds
|
||||
digitalWrite(pinRx, HIGH);
|
||||
digitalWrite(pinTx, HIGH);
|
||||
digitalWrite(pinEr, HIGH);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void startInclusionInterrupt() {
|
||||
buttonTriggeredInclusion = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void ledTimersInterrupt() {
|
||||
if(countRx && countRx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinRx, LOW);
|
||||
} else if(!countRx) {
|
||||
// switching off
|
||||
digitalWrite(pinRx, HIGH);
|
||||
}
|
||||
if(countRx != 255) { countRx--; }
|
||||
|
||||
if(countTx && countTx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinTx, LOW);
|
||||
} else if(!countTx) {
|
||||
// switching off
|
||||
digitalWrite(pinTx, HIGH);
|
||||
}
|
||||
if(countTx != 255) { countTx--; }
|
||||
else if(inclusionMode) { countTx = 8; }
|
||||
|
||||
if(countErr && countErr != 255) {
|
||||
// switch led on
|
||||
digitalWrite(RADIO_ERROR_LED_PIN, LOW);
|
||||
} else if(!countErr) {
|
||||
// switching off
|
||||
digitalWrite(RADIO_ERROR_LED_PIN, HIGH);
|
||||
}
|
||||
if(countErr != 255) { countErr--; }
|
||||
|
||||
}
|
||||
|
||||
void rxBlink(uint8_t cnt) {
|
||||
if(countRx == 255) { countRx = cnt; }
|
||||
}
|
||||
void txBlink(uint8_t cnt) {
|
||||
if(countTx == 255 && !inclusionMode) { countTx = cnt; }
|
||||
}
|
||||
void errBlink(uint8_t cnt) {
|
||||
if(countErr == 255) { countErr = cnt; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
#error This example is only for use on Arduino.
|
||||
#endif // ARDUINO
|
||||
|
||||
#endif
|
||||
@@ -58,10 +58,14 @@ http://forum.mysensors.org/topic/303/mqtt-broker-gateway
|
||||
|
||||
#include <SPI.h>
|
||||
#include <MySensor.h>
|
||||
#include <MyMQTT.h>
|
||||
#include <MsTimer2.h>
|
||||
#include <Ethernet.h>
|
||||
#include "MyMQTT.h"
|
||||
|
||||
|
||||
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
|
||||
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
|
||||
|
||||
// * Use this for IBOARD modded to use standard MISO/MOSI/SCK, see note *1 above!
|
||||
/*
|
||||
#define RADIO_CE_PIN 3 // radio chip enable
|
||||
@@ -72,6 +76,7 @@ http://forum.mysensors.org/topic/303/mqtt-broker-gateway
|
||||
|
||||
// * 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_ERROR_LED_PIN 7 // Error led pin
|
||||
@@ -84,8 +89,28 @@ uint8_t TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - You
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
MySensor gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
|
||||
EthernetServer server = EthernetServer(TCP_PORT);
|
||||
MyMQTT gw(RADIO_CE_PIN, RADIO_SPI_SS_PIN);
|
||||
MyMessage msg;
|
||||
char convBuf[MAX_PAYLOAD*2+1];
|
||||
char broker[] PROGMEM = MQTT_BROKER_PREFIX;
|
||||
bool MQTTClientConnected = false;
|
||||
uint8_t buffsize;
|
||||
char buffer[MQTT_MAX_PACKET_SIZE];
|
||||
volatile uint8_t countRx;
|
||||
volatile uint8_t countTx;
|
||||
volatile uint8_t countErr;
|
||||
|
||||
|
||||
void writeEthernet(const char *writeBuffer, uint8_t *writeSize) {
|
||||
#ifdef TCPDUMP
|
||||
Serial.print(">>");
|
||||
char buf[4];
|
||||
for (uint8_t a=0; a<*writeSize; a++) { sprintf(buf,"%02X ",(uint8_t)writeBuffer[a]); Serial.print(buf); } Serial.println("");
|
||||
#endif
|
||||
server.write((const uint8_t *)writeBuffer, *writeSize);
|
||||
}
|
||||
|
||||
|
||||
void processEthernetMessages() {
|
||||
char inputString[MQTT_MAX_PACKET_SIZE] = "";
|
||||
@@ -102,28 +127,284 @@ void processEthernetMessages() {
|
||||
char buf[4];
|
||||
for (uint8_t a=0; a<inputSize; a++) { sprintf(buf,"%02X ", (uint8_t)inputString[a]); Serial.print(buf); } Serial.println("");
|
||||
#endif
|
||||
gw.processMQTTMessage(inputString, inputSize);
|
||||
processMQTTMessage(inputString, inputSize);
|
||||
}
|
||||
}
|
||||
|
||||
void writeEthernet(const char *writeBuffer, uint8_t *writeSize) {
|
||||
#ifdef TCPDUMP
|
||||
Serial.print(">>");
|
||||
char buf[4];
|
||||
for (uint8_t a=0; a<*writeSize; a++) { sprintf(buf,"%02X ",(uint8_t)writeBuffer[a]); Serial.print(buf); } Serial.println("");
|
||||
|
||||
|
||||
void incomingMessage(const MyMessage &message) {
|
||||
rxBlink(1);
|
||||
sendMQTT(message);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Initialize gateway at maximum PA level, channel 70 and callback for write operations
|
||||
gw.begin(incomingMessage, 0, true, 0, RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE);
|
||||
|
||||
Ethernet.begin(TCP_MAC, TCP_IP);
|
||||
|
||||
|
||||
countRx = 0;
|
||||
countTx = 0;
|
||||
countErr = 0;
|
||||
|
||||
// Setup led pins
|
||||
pinMode(RADIO_RX_LED_PIN, OUTPUT);
|
||||
pinMode(RADIO_TX_LED_PIN, OUTPUT);
|
||||
pinMode(RADIO_ERROR_LED_PIN, OUTPUT);
|
||||
digitalWrite(RADIO_RX_LED_PIN, LOW);
|
||||
digitalWrite(RADIO_TX_LED_PIN, LOW);
|
||||
digitalWrite(RADIO_ERROR_LED_PIN, LOW);
|
||||
|
||||
|
||||
// Set initial state of leds
|
||||
digitalWrite(RADIO_RX_LED_PIN, HIGH);
|
||||
digitalWrite(RADIO_TX_LED_PIN, HIGH);
|
||||
digitalWrite(RADIO_ERROR_LED_PIN, HIGH);
|
||||
|
||||
|
||||
// Add led timer interrupt
|
||||
MsTimer2::set(300, ledTimersInterrupt);
|
||||
MsTimer2::start();
|
||||
|
||||
|
||||
// give the Ethernet interface a second to initialize
|
||||
delay(1000);
|
||||
|
||||
// start listening for clients
|
||||
server.begin();
|
||||
|
||||
Serial.println("Ok!");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
gw.process();
|
||||
|
||||
processEthernetMessages();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
inline MyMessage& build (MyMessage &msg, uint8_t destination, uint8_t sensor, uint8_t command, uint8_t type, bool enableAck) {
|
||||
msg.destination = destination;
|
||||
msg.sender = GATEWAY_ADDRESS;
|
||||
msg.sensor = sensor;
|
||||
msg.type = type;
|
||||
mSetCommand(msg,command);
|
||||
mSetRequestAck(msg,enableAck);
|
||||
mSetAck(msg,false);
|
||||
return msg;
|
||||
}
|
||||
|
||||
char *getType(char *b, const char **index) {
|
||||
char *q = b;
|
||||
char *p = (char *)pgm_read_word(index);
|
||||
while (*q++ = pgm_read_byte(p++));
|
||||
*q=0;
|
||||
return b;
|
||||
}
|
||||
|
||||
void processMQTTMessage(char *inputString, uint8_t inputPos) {
|
||||
char *str, *p;
|
||||
uint8_t i = 0;
|
||||
buffer[0]= 0;
|
||||
buffsize = 0;
|
||||
|
||||
if ((uint8_t)inputString[0] >> 4 == MQTTCONNECT) {
|
||||
buffer[buffsize++] = MQTTCONNACK << 4;
|
||||
buffer[buffsize++] = 0x02; // Remaining length
|
||||
buffer[buffsize++] = 0x00; // Connection accepted
|
||||
buffer[buffsize++] = 0x00; // Reserved
|
||||
MQTTClientConnected=true; // We have connection!
|
||||
}
|
||||
if ((uint8_t)inputString[0] >> 4 == MQTTPINGREQ) {
|
||||
buffer[buffsize++] = MQTTPINGRESP << 4;
|
||||
buffer[buffsize++] = 0x00;
|
||||
}
|
||||
if ((uint8_t)inputString[0] >> 4 == MQTTSUBSCRIBE) {
|
||||
buffer[buffsize++] = MQTTSUBACK << 4; // Just ack everything, we actually dont really care!
|
||||
buffer[buffsize++] = 0x03; // Remaining length
|
||||
buffer[buffsize++] = (uint8_t)inputString[2]; // Message ID MSB
|
||||
buffer[buffsize++] = (uint8_t)inputString[3]; // Message ID LSB
|
||||
buffer[buffsize++] = MQTTQOS0; // QOS level
|
||||
}
|
||||
if ((uint8_t)inputString[0] >> 4 == MQTTUNSUBSCRIBE) {
|
||||
buffer[buffsize++] = MQTTUNSUBACK << 4;
|
||||
buffer[buffsize++] = 0x02; // Remaining length
|
||||
buffer[buffsize++] = (uint8_t)inputString[2]; // Message ID MSB
|
||||
buffer[buffsize++] = (uint8_t)inputString[3]; // Message ID LSB
|
||||
}
|
||||
if ((uint8_t)inputString[0] >> 4 == MQTTDISCONNECT) {
|
||||
MQTTClientConnected=false; // We lost connection!
|
||||
}
|
||||
if (buffsize > 0) {
|
||||
writeEthernet(buffer,&buffsize);
|
||||
}
|
||||
|
||||
// We publish everything we get, we dont care if its subscribed or not!
|
||||
if ((uint8_t)inputString[0] >> 4 == MQTTPUBLISH || (MQTT_SEND_SUBSCRIPTION && (uint8_t)inputString[0] >> 4 == MQTTSUBSCRIBE)) {
|
||||
buffer[0]= 0;
|
||||
buffsize = 0;
|
||||
// Cut out address and payload depending on message type.
|
||||
if ((uint8_t)inputString[0] >> 4 == MQTTSUBSCRIBE) {
|
||||
strncat(buffer,inputString+6,inputString[5]);
|
||||
} else {
|
||||
strncat(buffer,inputString+4,inputString[3]);
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
Serial.println(buffer);
|
||||
#endif
|
||||
server.write((const uint8_t *)writeBuffer, *writeSize);
|
||||
}
|
||||
// TODO: Check if we should send ack or not.
|
||||
for (str = strtok_r(buffer,"/",&p) ; str && i<4 ; str = strtok_r(NULL,"/",&p)) {
|
||||
if (i == 0) {
|
||||
if (strcmp_P(str,broker)!=0) { //look for MQTT_BROKER_PREFIX
|
||||
return; //Message not for us or malformatted!
|
||||
}
|
||||
} else if (i==1) {
|
||||
msg.destination = atoi(str); //NodeID
|
||||
} else if (i==2) {
|
||||
msg.sensor = atoi(str); //SensorID
|
||||
} else if (i==3) {
|
||||
char match=255; //SensorType
|
||||
#ifdef MQTT_TRANSLATE_TYPES
|
||||
|
||||
for (uint8_t j=0; strcpy_P(convBuf, (char*)pgm_read_word(&(vType[j]))) ; j++) {
|
||||
if (strcmp((char*)&str[2],convBuf)==0) { //Strip V_ and compare
|
||||
match=j;
|
||||
break;
|
||||
}
|
||||
if (j >= V_TOTAL) break;
|
||||
}
|
||||
|
||||
#endif
|
||||
if ( atoi(str)!=0 || str=="0" ) {
|
||||
match=atoi(str);
|
||||
}
|
||||
|
||||
if (match==255) {
|
||||
match=V_UNKNOWN;
|
||||
}
|
||||
msg.type = match;
|
||||
}
|
||||
i++;
|
||||
} //Check if packge has payload
|
||||
if ((uint8_t)inputString[1] > (uint8_t)(inputString[3]+2) && !((uint8_t)inputString[0] >> 4 == MQTTSUBSCRIBE)) {
|
||||
strcpy(convBuf,inputString+(inputString[3]+4));
|
||||
msg.set(convBuf); //Payload
|
||||
} else {
|
||||
msg.set(""); //No payload
|
||||
}
|
||||
txBlink(1);
|
||||
if (!gw.sendRoute(build(msg, msg.destination, msg.sensor, C_SET, msg.type, 0))) errBlink(1);
|
||||
|
||||
int main(void) {
|
||||
init();
|
||||
Ethernet.begin(TCP_MAC, TCP_IP);
|
||||
delay(1000); // Wait for Ethernet to get configured.
|
||||
gw.begin(RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE, writeEthernet, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
|
||||
server.begin();
|
||||
while (1) {
|
||||
processEthernetMessages();
|
||||
gw.processRadioMessage();
|
||||
}
|
||||
}
|
||||
|
||||
void sendMQTT(const MyMessage &inMsg) {
|
||||
MyMessage msg = inMsg;
|
||||
buffsize = 0;
|
||||
if (!MQTTClientConnected) return; //We have no connections - return
|
||||
if (msg.isAck()) {
|
||||
// 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.
|
||||
// The idea was to confirm id and save to EEPROM_LATEST_NODE_ADDRESS.
|
||||
// }
|
||||
} else {
|
||||
// we have to check every message if its a newly assigned id or not.
|
||||
// Ack on I_ID_RESPONSE does not work, and checking on C_PRESENTATION isn't reliable.
|
||||
uint8_t newNodeID = gw.loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
|
||||
if (newNodeID <= MQTT_FIRST_SENSORID) newNodeID = MQTT_FIRST_SENSORID;
|
||||
if (msg.sender==newNodeID) {
|
||||
gw.saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID);
|
||||
}
|
||||
if (mGetCommand(msg)==C_INTERNAL) {
|
||||
if (msg.type==I_CONFIG) {
|
||||
txBlink(1);
|
||||
if (!gw.sendRoute(build(msg, msg.sender, 255, C_INTERNAL, I_CONFIG, 0).set(MQTT_UNIT))) errBlink(1);
|
||||
return;
|
||||
} else if (msg.type==I_ID_REQUEST && msg.sender==255) {
|
||||
uint8_t newNodeID = gw.loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
|
||||
if (newNodeID <= MQTT_FIRST_SENSORID) newNodeID = MQTT_FIRST_SENSORID;
|
||||
if (newNodeID >= MQTT_LAST_SENSORID) return; // Sorry no more id's left :(
|
||||
txBlink(1);
|
||||
if (!gw.sendRoute(build(msg, msg.sender, 255, C_INTERNAL, I_ID_RESPONSE, 0).set(newNodeID))) errBlink(1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (mGetCommand(msg)!=C_PRESENTATION) {
|
||||
if (mGetCommand(msg)==C_INTERNAL) msg.type=msg.type+(S_FIRSTCUSTOM-10); //Special message
|
||||
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
|
||||
strcpy_P(buffer+4, broker);
|
||||
buffsize+=strlen_P(broker);
|
||||
#ifdef MQTT_TRANSLATE_TYPES
|
||||
if (msg.type > V_TOTAL) msg.type=V_UNKNOWN;// If type > defined types set to unknown.
|
||||
buffsize+=sprintf(&buffer[buffsize],"/%i/%i/V_%s",msg.sender,msg.sensor,getType(convBuf, &vType[msg.type]));
|
||||
#else
|
||||
buffsize+=sprintf(&buffer[buffsize],"/%i/%i/%i",msg.sender,msg.sensor,msg.type);
|
||||
#endif
|
||||
buffer[3]=buffsize-4; // Set correct address length on byte 4.
|
||||
#ifdef DEBUG
|
||||
Serial.println((char*)&buffer[4]);
|
||||
#endif
|
||||
msg.getString(convBuf);
|
||||
for (uint8_t a=0; a<strlen(convBuf); a++) {// Payload
|
||||
buffer[buffsize++] = convBuf[a];
|
||||
}
|
||||
buffer[1]=buffsize-2; // Set correct Remaining length on byte 2.
|
||||
writeEthernet(buffer,&buffsize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ledTimersInterrupt() {
|
||||
if(countRx && countRx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(RADIO_RX_LED_PIN, LOW);
|
||||
} else if(!countRx) {
|
||||
// switching off
|
||||
digitalWrite(RADIO_RX_LED_PIN, HIGH);
|
||||
}
|
||||
if(countRx != 255) { countRx--; }
|
||||
|
||||
if(countTx && countTx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(RADIO_TX_LED_PIN, LOW);
|
||||
} else if(!countTx) {
|
||||
// switching off
|
||||
digitalWrite(RADIO_TX_LED_PIN, HIGH);
|
||||
}
|
||||
if(countTx != 255) { countTx--; }
|
||||
|
||||
if(countErr && countErr != 255) {
|
||||
// switch led on
|
||||
digitalWrite(RADIO_ERROR_LED_PIN, LOW);
|
||||
} else if(!countErr) {
|
||||
// switching off
|
||||
digitalWrite(RADIO_ERROR_LED_PIN, HIGH);
|
||||
}
|
||||
if(countErr != 255) { countErr--; }
|
||||
|
||||
}
|
||||
|
||||
void rxBlink(uint8_t cnt) {
|
||||
if(countRx == 255) { countRx = cnt; }
|
||||
}
|
||||
void txBlink(uint8_t cnt) {
|
||||
if(countTx == 255) { countTx = cnt; }
|
||||
}
|
||||
void errBlink(uint8_t cnt) {
|
||||
if(countErr == 255) { countErr = cnt; }
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
The MySensors library adds a new layer on top of the RF24 library.
|
||||
It handles radio network routing, relaying and ids.
|
||||
|
||||
Created by Henrik Ekblad <henrik.ekblad@gmail.com>
|
||||
Modified by Daniel Wiegert
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifdef DEBUG
|
||||
#define TCPDUMP // Dump TCP packages
|
||||
#endif
|
||||
|
||||
#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_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.
|
||||
// NOTE above : Beware to check if there is any length on payload in your incommingMessage code:
|
||||
// Example: if (msg.type==V_LIGHT && strlen(msg.getString())>0) otherwise the code might do strange things.
|
||||
#define MQTT_UNIT "M" // Select M for metric or I for imperial.
|
||||
#define MQTT_TRANSLATE_TYPES // V_TYPE in address, Comment if you want all numbers (MyMQTT/01/01/01)
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
#define EEPROM_LATEST_NODE_ADDRESS ((uint8_t)EEPROM_LOCAL_CONFIG_ADDRESS)
|
||||
#define MQTT_MAX_PACKET_SIZE 100
|
||||
|
||||
#define MQTTPROTOCOLVERSION 3
|
||||
#define MQTTCONNECT 1 // Client request to connect to Server
|
||||
#define MQTTCONNACK 2 // Connect Acknowledgment
|
||||
#define MQTTPUBLISH 3 // Publish message
|
||||
#define MQTTPUBACK 4 // Publish Acknowledgment
|
||||
#define MQTTPUBREC 5 // Publish Received (assured delivery part 1)
|
||||
#define MQTTPUBREL 6 // Publish Release (assured delivery part 2)
|
||||
#define MQTTPUBCOMP 7 // Publish Complete (assured delivery part 3)
|
||||
#define MQTTSUBSCRIBE 8 // Client Subscribe request
|
||||
#define MQTTSUBACK 9 // Subscribe Acknowledgment
|
||||
#define MQTTUNSUBSCRIBE 10 // Client Unsubscribe request
|
||||
#define MQTTUNSUBACK 11 // Unsubscribe Acknowledgment
|
||||
#define MQTTPINGREQ 12 // PING Request
|
||||
#define MQTTPINGRESP 13 // PING Response
|
||||
#define MQTTDISCONNECT 14 // Client is Disconnecting
|
||||
#define MQTTReserved 15 // Reserved
|
||||
|
||||
#define MQTTQOS0 (0 << 1)
|
||||
#define MQTTQOS1 (1 << 1)
|
||||
#define MQTTQOS2 (2 << 1)
|
||||
|
||||
#ifdef MQTT_TRANSLATE_TYPES
|
||||
|
||||
char V_0[] PROGMEM = "TEMP"; //V_TEMP
|
||||
char V_1[] PROGMEM = "HUM"; //V_HUM
|
||||
char V_2[] PROGMEM = "LIGHT"; //V_LIGHT
|
||||
char V_3[] PROGMEM = "DIMMER"; //V_DIMMER
|
||||
char V_4[] PROGMEM = "PRESSURE"; //V_PRESSURE
|
||||
char V_5[] PROGMEM = "FORECAST"; //V_FORECAST
|
||||
char V_6[] PROGMEM = "RAIN"; //V_RAIN
|
||||
char V_7[] PROGMEM = "RAINRATE"; //V_RAINRATE
|
||||
char V_8[] PROGMEM = "WIND"; //V_WIND
|
||||
char V_9[] PROGMEM = "GUST"; //V_GUST
|
||||
char V_10[] PROGMEM = "DIRECTON"; //V_DIRECTON
|
||||
char V_11[] PROGMEM = "UV"; //V_UV
|
||||
char V_12[] PROGMEM = "WEIGHT"; //V_WEIGHT
|
||||
char V_13[] PROGMEM = "DISTANCE"; //V_DISTANCE
|
||||
char V_14[] PROGMEM = "IMPEDANCE"; //V_IMPEDANCE
|
||||
char V_15[] PROGMEM = "ARMED"; //V_ARMED
|
||||
char V_16[] PROGMEM = "TRIPPED"; //V_TRIPPED
|
||||
char V_17[] PROGMEM = "WATT"; //V_WATT
|
||||
char V_18[] PROGMEM = "KWH"; //V_KWH
|
||||
char V_19[] PROGMEM = "SCENE_ON"; //V_SCENE_ON
|
||||
char V_20[] PROGMEM = "SCENE_OFF"; //V_SCENE_OFF
|
||||
char V_21[] PROGMEM = "HEATER"; //V_HEATER
|
||||
char V_22[] PROGMEM = "HEATER_SW"; //V_HEATER_SW
|
||||
char V_23[] PROGMEM = "LIGHT_LEVEL"; //V_LIGHT_LEVEL
|
||||
char V_24[] PROGMEM = "VAR1"; //V_VAR1
|
||||
char V_25[] PROGMEM = "VAR2"; //V_VAR2
|
||||
char V_26[] PROGMEM = "VAR3"; //V_VAR3
|
||||
char V_27[] PROGMEM = "VAR4"; //V_VAR4
|
||||
char V_28[] PROGMEM = "VAR5"; //V_VAR5
|
||||
char V_29[] PROGMEM = "UP"; //V_UP
|
||||
char V_30[] PROGMEM = "DOWN"; //V_DOWN
|
||||
char V_31[] PROGMEM = "STOP"; //V_STOP
|
||||
char V_32[] PROGMEM = "IR_SEND"; //V_IR_SEND
|
||||
char V_33[] PROGMEM = "IR_RECEIVE"; //V_IR_RECEIVE
|
||||
char V_34[] PROGMEM = "FLOW"; //V_FLOW
|
||||
char V_35[] PROGMEM = "VOLUME"; //V_VOLUME
|
||||
char V_36[] PROGMEM = "LOCK_STATUS"; //V_LOCK_STATUS
|
||||
char V_37[] PROGMEM = "DUST_LEVEL"; //V_DUST_LEVEL
|
||||
char V_38[] PROGMEM = "VOLTAGE"; //V_VOLTAGE
|
||||
char V_39[] PROGMEM = "CURRENT"; //V_CURRENT
|
||||
char V_40[] PROGMEM = ""; //
|
||||
char V_41[] PROGMEM = ""; //
|
||||
char V_42[] PROGMEM = ""; //
|
||||
char V_43[] PROGMEM = ""; //
|
||||
char V_44[] PROGMEM = ""; //
|
||||
char V_45[] PROGMEM = ""; //
|
||||
char V_46[] PROGMEM = ""; //
|
||||
char V_47[] PROGMEM = ""; //
|
||||
char V_48[] PROGMEM = ""; //
|
||||
char V_49[] PROGMEM = ""; //
|
||||
char V_50[] PROGMEM = ""; //
|
||||
char V_51[] PROGMEM = ""; //
|
||||
char V_52[] PROGMEM = ""; //
|
||||
char V_53[] PROGMEM = ""; //
|
||||
char V_54[] PROGMEM = ""; //
|
||||
char V_55[] PROGMEM = ""; //
|
||||
char V_56[] PROGMEM = ""; //
|
||||
char V_57[] PROGMEM = ""; //
|
||||
char V_58[] PROGMEM = ""; //
|
||||
char V_59[] PROGMEM = ""; //
|
||||
char V_60[] PROGMEM = "DEFAULT"; //Custom for MQTTGateway
|
||||
char V_61[] PROGMEM = "SKETCH_NAME"; //Custom for MQTTGateway
|
||||
char V_62[] PROGMEM = "SKETCH_VERSION"; //Custom for MQTTGateway
|
||||
char V_63[] PROGMEM = "UNKNOWN"; //Custom for MQTTGateway
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
PROGMEM const char *vType[] = {
|
||||
V_0, V_1, V_2, V_3, V_4, V_5, V_6, V_7, V_8, V_9, V_10,
|
||||
V_11, V_12, V_13, V_14, V_15, V_16, V_17, V_18, V_19, V_20,
|
||||
V_21, V_22, V_23, V_24, V_25, V_26, V_27, V_28, V_29, V_30,
|
||||
V_31, V_32, V_33, V_34, V_35, V_36, V_37, V_38, V_39, V_40,
|
||||
V_41, V_42, V_43, V_44, V_45, V_46, V_47, V_48, V_49, V_50,
|
||||
V_51, V_52, V_53, V_54, V_55, V_56, V_57, V_58, V_59, V_60,
|
||||
V_61, V_62, V_63
|
||||
};
|
||||
|
||||
|
||||
|
||||
#define S_FIRSTCUSTOM 60
|
||||
#define V_TOTAL (sizeof(vType)/sizeof(char *))-1
|
||||
|
||||
#endif
|
||||
|
||||
#define S_FIRSTCUSTOM 60
|
||||
#define V_UNKNOWN 63
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
The MySensors library adds a new layer on top of the RF24 library.
|
||||
It handles radio network routing, relaying and ids.
|
||||
|
||||
Created by Henrik Ekblad <henrik.ekblad@gmail.com>
|
||||
Modified by Daniel Wiegert
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License
|
||||
version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef MyMQTT_h
|
||||
#define MyMQTT_h
|
||||
|
||||
#include "MySensor.h"
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifdef DEBUG
|
||||
#define TCPDUMP // Dump TCP packages.
|
||||
#endif
|
||||
|
||||
#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_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.
|
||||
// NOTE above : Beware to check if there is any length on payload in your incommingMessage code:
|
||||
// Example: if (msg.type==V_LIGHT && strlen(msg.getString())>0) otherwise the code might do strange things.
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
|
||||
#define EEPROM_LATEST_NODE_ADDRESS ((uint8_t)EEPROM_LOCAL_CONFIG_ADDRESS)
|
||||
#define MQTT_MAX_PACKET_SIZE 100
|
||||
|
||||
#define MQTTPROTOCOLVERSION 3
|
||||
#define MQTTCONNECT 1 // Client request to connect to Server
|
||||
#define MQTTCONNACK 2 // Connect Acknowledgment
|
||||
#define MQTTPUBLISH 3 // Publish message
|
||||
#define MQTTPUBACK 4 // Publish Acknowledgment
|
||||
#define MQTTPUBREC 5 // Publish Received (assured delivery part 1)
|
||||
#define MQTTPUBREL 6 // Publish Release (assured delivery part 2)
|
||||
#define MQTTPUBCOMP 7 // Publish Complete (assured delivery part 3)
|
||||
#define MQTTSUBSCRIBE 8 // Client Subscribe request
|
||||
#define MQTTSUBACK 9 // Subscribe Acknowledgment
|
||||
#define MQTTUNSUBSCRIBE 10 // Client Unsubscribe request
|
||||
#define MQTTUNSUBACK 11 // Unsubscribe Acknowledgment
|
||||
#define MQTTPINGREQ 12 // PING Request
|
||||
#define MQTTPINGRESP 13 // PING Response
|
||||
#define MQTTDISCONNECT 14 // Client is Disconnecting
|
||||
#define MQTTReserved 15 // Reserved
|
||||
|
||||
#define MQTTQOS0 (0 << 1)
|
||||
#define MQTTQOS1 (1 << 1)
|
||||
#define MQTTQOS2 (2 << 1)
|
||||
|
||||
class MyMQTT :
|
||||
public MySensor {
|
||||
|
||||
public:
|
||||
MyMQTT(uint8_t _cepin=5, uint8_t _cspin=6);
|
||||
void begin(rf24_pa_dbm_e paLevel=RF24_PA_LEVEL_GW, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE, void (*dataCallback)
|
||||
(const char *, uint8_t *)=NULL, uint8_t _rx=6, uint8_t _tx=5, uint8_t _er=4 );
|
||||
void processRadioMessage();
|
||||
void processMQTTMessage(char *inputString, uint8_t inputPos);
|
||||
private:
|
||||
void (*dataCallback)(const char *, uint8_t *);
|
||||
void SendMQTT(MyMessage &msg);
|
||||
void ledTimers();
|
||||
void rxBlink(uint8_t cnt);
|
||||
void txBlink(uint8_t cnt);
|
||||
void errBlink(uint8_t cnt);
|
||||
|
||||
bool MQTTClientConnected;
|
||||
char buffer[MQTT_MAX_PACKET_SIZE];
|
||||
char convBuf[MAX_PAYLOAD*2+1];
|
||||
uint8_t buffsize;
|
||||
char *getType(char *b, const char **index);
|
||||
};
|
||||
|
||||
extern void ledTimersInterrupt();
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,262 @@
|
||||
#ifndef __GATEWAYUTIL_H__
|
||||
#define __GATEWAYUTIL_H__
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
|
||||
uint8_t pinRx = 8; // Rx led pin
|
||||
uint8_t pinTx = 9; // Tx led pin
|
||||
uint8_t pinEr = 7; // Err led pin
|
||||
uint8_t inclusionTime = 1; // Number of minutes inclusion mode is enabled
|
||||
uint8_t pinInclusion = 3; // Input pin that should trigger inclusion mode
|
||||
|
||||
#define MAX_RECEIVE_LENGTH 100 // Max buffersize needed for messages coming from controller
|
||||
#define MAX_SEND_LENGTH 120 // Max buffersize needed for messages destined for controller
|
||||
|
||||
volatile boolean buttonTriggeredInclusion;
|
||||
volatile uint8_t countRx;
|
||||
volatile uint8_t countTx;
|
||||
volatile uint8_t countErr;
|
||||
boolean inclusionMode; // Keeps track on inclusion mode
|
||||
void (*serial)(const char *fmt, ... );
|
||||
|
||||
MyMessage msg;
|
||||
|
||||
void setInclusionMode(boolean newMode);
|
||||
void txBlink(uint8_t cnt);
|
||||
void rxBlink(uint8_t cnt);
|
||||
void errBlink(uint8_t cnt);
|
||||
|
||||
char convBuf[MAX_PAYLOAD*2+1];
|
||||
char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera
|
||||
unsigned long inclusionStartTime;
|
||||
|
||||
void setupGateway(uint8_t _rx, uint8_t _tx, uint8_t _er, uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
|
||||
inclusionMode = 0;
|
||||
buttonTriggeredInclusion = false;
|
||||
serial = _serial;
|
||||
|
||||
pinRx = _rx;
|
||||
pinTx = _tx;
|
||||
pinEr = _er;
|
||||
pinInclusion = _inc;
|
||||
inclusionTime = _incTime;
|
||||
|
||||
countRx = 0;
|
||||
countTx = 0;
|
||||
countErr = 0;
|
||||
|
||||
// Setup led pins
|
||||
pinMode(pinRx, OUTPUT);
|
||||
pinMode(pinTx, OUTPUT);
|
||||
pinMode(pinEr, OUTPUT);
|
||||
digitalWrite(pinRx, LOW);
|
||||
digitalWrite(pinTx, LOW);
|
||||
digitalWrite(pinEr, LOW);
|
||||
|
||||
// Setup digital in that triggers inclusion mode
|
||||
pinMode(pinInclusion, INPUT);
|
||||
digitalWrite(pinInclusion, HIGH);
|
||||
|
||||
// Set initial state of leds
|
||||
digitalWrite(pinRx, HIGH);
|
||||
digitalWrite(pinTx, HIGH);
|
||||
digitalWrite(pinEr, HIGH);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void startInclusionInterrupt() {
|
||||
buttonTriggeredInclusion = true;
|
||||
}
|
||||
|
||||
void incomingMessage(const MyMessage &message) {
|
||||
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
|
||||
rxBlink(3);
|
||||
} else {
|
||||
rxBlink(1);
|
||||
}
|
||||
// Pass along the message from sensors to serial line
|
||||
serial(PSTR("%d;%d;%d;%d;%d;%s\n"),message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type, message.getString(convBuf));
|
||||
}
|
||||
|
||||
|
||||
|
||||
void checkButtonTriggeredInclusion() {
|
||||
if (buttonTriggeredInclusion) {
|
||||
// Ok, someone pressed the inclusion button on the gateway
|
||||
// start inclusion mode for 1 munute.
|
||||
serial(PSTR("0;0;%d;0;%d;Inclusion started by button.\n"), C_INTERNAL, I_LOG_MESSAGE);
|
||||
buttonTriggeredInclusion = false;
|
||||
setInclusionMode(true);
|
||||
}
|
||||
}
|
||||
|
||||
void checkInclusionFinished() {
|
||||
if (inclusionMode && millis()-inclusionStartTime>60000UL*inclusionTime) {
|
||||
// inclusionTimeInMinutes minute(s) has passed.. stop inclusion mode
|
||||
setInclusionMode(false);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t h2i(char c) {
|
||||
uint8_t i = 0;
|
||||
if (c <= '9')
|
||||
i += c - '0';
|
||||
else if (c >= 'a')
|
||||
i += c - 'a' + 10;
|
||||
else
|
||||
i += c - 'A' + 10;
|
||||
return i;
|
||||
}
|
||||
|
||||
void parseAndSend(MySensor gw, char *commandBuffer) {
|
||||
boolean ok = false;
|
||||
char *str, *p, *value=NULL;
|
||||
uint8_t bvalue[MAX_PAYLOAD];
|
||||
uint8_t blen = 0;
|
||||
int i = 0;
|
||||
uint16_t destination = 0;
|
||||
uint8_t sensor = 0;
|
||||
uint8_t command = 0;
|
||||
uint8_t type = 0;
|
||||
uint8_t ack = 0;
|
||||
|
||||
// Extract command data coming on serial line
|
||||
for (str = strtok_r(commandBuffer, ";", &p); // split using semicolon
|
||||
str && i < 6; // loop while str is not null an max 5 times
|
||||
str = strtok_r(NULL, ";", &p) // get subsequent tokens
|
||||
) {
|
||||
switch (i) {
|
||||
case 0: // Radioid (destination)
|
||||
destination = atoi(str);
|
||||
break;
|
||||
case 1: // Childid
|
||||
sensor = atoi(str);
|
||||
break;
|
||||
case 2: // Message type
|
||||
command = atoi(str);
|
||||
break;
|
||||
case 3: // Should we request ack from destination?
|
||||
ack = atoi(str);
|
||||
break;
|
||||
case 4: // Data type
|
||||
type = atoi(str);
|
||||
break;
|
||||
case 5: // Variable value
|
||||
if (command == C_STREAM) {
|
||||
blen = 0;
|
||||
uint8_t val;
|
||||
while (*str) {
|
||||
val = h2i(*str++) << 4;
|
||||
val += h2i(*str++);
|
||||
bvalue[blen] = val;
|
||||
blen++;
|
||||
}
|
||||
} else {
|
||||
value = str;
|
||||
// Remove ending carriage return character (if it exists)
|
||||
uint8_t lastCharacter = strlen(value)-1;
|
||||
if (value[lastCharacter] == '\r')
|
||||
value[lastCharacter] = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (destination==GATEWAY_ADDRESS && command==C_INTERNAL) {
|
||||
// Handle messages directed to gateway
|
||||
if (type == I_VERSION) {
|
||||
// Request for version
|
||||
serial(PSTR("0;0;%d;0;%d;%s\n"), C_INTERNAL, I_VERSION, LIBRARY_VERSION);
|
||||
} else if (type == I_INCLUSION_MODE) {
|
||||
// Request to change inclusion mode
|
||||
setInclusionMode(atoi(value) == 1);
|
||||
}
|
||||
} else {
|
||||
txBlink(1);
|
||||
msg.sender = GATEWAY_ADDRESS;
|
||||
msg.destination = destination;
|
||||
msg.sensor = sensor;
|
||||
msg.type = type;
|
||||
mSetCommand(msg,command);
|
||||
mSetRequestAck(msg,ack?1:0);
|
||||
mSetAck(msg,false);
|
||||
if (command == C_STREAM)
|
||||
msg.set(bvalue, blen);
|
||||
else
|
||||
msg.set(value);
|
||||
ok = gw.sendRoute(msg);
|
||||
if (!ok) {
|
||||
errBlink(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setInclusionMode(boolean newMode) {
|
||||
if (newMode != inclusionMode)
|
||||
inclusionMode = newMode;
|
||||
// Send back mode change on serial line to ack command
|
||||
serial(PSTR("0;0;%d;0;%d;%d\n"), C_INTERNAL, I_INCLUSION_MODE, inclusionMode?1:0);
|
||||
|
||||
if (inclusionMode) {
|
||||
inclusionStartTime = millis();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void ledTimersInterrupt() {
|
||||
if(countRx && countRx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinRx, LOW);
|
||||
} else if(!countRx) {
|
||||
// switching off
|
||||
digitalWrite(pinRx, HIGH);
|
||||
}
|
||||
if(countRx != 255) { countRx--; }
|
||||
|
||||
if(countTx && countTx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinTx, LOW);
|
||||
} else if(!countTx) {
|
||||
// switching off
|
||||
digitalWrite(pinTx, HIGH);
|
||||
}
|
||||
if(countTx != 255) { countTx--; }
|
||||
else if(inclusionMode) { countTx = 8; }
|
||||
|
||||
if(countErr && countErr != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinEr, LOW);
|
||||
} else if(!countErr) {
|
||||
// switching off
|
||||
digitalWrite(pinEr, HIGH);
|
||||
}
|
||||
if(countErr != 255) { countErr--; }
|
||||
|
||||
}
|
||||
|
||||
void rxBlink(uint8_t cnt) {
|
||||
if(countRx == 255) { countRx = cnt; }
|
||||
}
|
||||
void txBlink(uint8_t cnt) {
|
||||
if(countTx == 255 && !inclusionMode) { countTx = cnt; }
|
||||
}
|
||||
void errBlink(uint8_t cnt) {
|
||||
if(countErr == 255) { countErr = cnt; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
#error This example is only for use on Arduino.
|
||||
#endif // ARDUINO
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2013 Henrik Ekblad <henrik.ekblad@gmail.com>
|
||||
* Copyright (C) 2013-2014 Henrik Ekblad <henrik.ekblad@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
@@ -13,41 +13,83 @@
|
||||
*
|
||||
* Wire connections (OPTIONAL):
|
||||
* - Inclusion button should be connected between digital pin 3 and GND
|
||||
* - RX/TX/ERR leds need to be connected between +5V (anode) and digital ping 6/5/4 with resistor 270-330R in a series
|
||||
* - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
|
||||
*
|
||||
* LEDs (OPTIONAL):
|
||||
* - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
|
||||
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
|
||||
* - ERR (red) - fast blink on error during transmission error or recieve crc error
|
||||
*/
|
||||
|
||||
#define NO_PORTB_PINCHANGES
|
||||
|
||||
#include <SPI.h>
|
||||
#include <MySensor.h>
|
||||
#include <MyGateway.h>
|
||||
#include <stdarg.h>
|
||||
#include <MsTimer2.h>
|
||||
#include <PinChangeInt.h>
|
||||
#include "GatewayUtil.h"
|
||||
|
||||
|
||||
#define INCLUSION_MODE_TIME 1 // Number of minutes inclusion mode is enabled
|
||||
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
|
||||
#define INCLUSION_MODE_PIN 3 // Digital pin used for inclusion mode button
|
||||
#define RADIO_ERROR_LED_PIN 4 // Error led pin
|
||||
#define RADIO_RX_LED_PIN 6 // Receive led pin
|
||||
#define RADIO_TX_LED_PIN 5 // the PCB, on board LED
|
||||
|
||||
|
||||
MyGateway gw(DEFAULT_CE_PIN, DEFAULT_CS_PIN, INCLUSION_MODE_TIME, INCLUSION_MODE_PIN, 6, 5, 4);
|
||||
MySensor gw(DEFAULT_CE_PIN, DEFAULT_CS_PIN);
|
||||
|
||||
char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface
|
||||
int inputPos = 0;
|
||||
boolean commandComplete = false; // whether the string is complete
|
||||
|
||||
void parseAndSend(char *commandBuffer);
|
||||
|
||||
void output(const char *fmt, ... ) {
|
||||
va_list args;
|
||||
va_start (args, fmt );
|
||||
vsnprintf_P(serialBuffer, MAX_SEND_LENGTH, fmt, args);
|
||||
va_end (args);
|
||||
Serial.print(serialBuffer);
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
gw.begin();
|
||||
gw.begin(incomingMessage, 0, true, 0, RF24_PA_LEVEL_GW, RF24_CHANNEL, RF24_DATARATE);
|
||||
// Stetup pipes for radio library
|
||||
gw.openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
|
||||
gw.openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
|
||||
gw.startListening();
|
||||
|
||||
Serial.begin(BAUD_RATE);
|
||||
|
||||
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
|
||||
|
||||
// Add led timer interrupt
|
||||
MsTimer2::set(300, ledTimersInterrupt);
|
||||
MsTimer2::start();
|
||||
|
||||
// Add interrupt for inclusion button to pin
|
||||
PCintPort::attachInterrupt(pinInclusion, startInclusionInterrupt, RISING);
|
||||
|
||||
|
||||
// Send startup log message on serial
|
||||
serial(PSTR("0;0;%d;0;%d;Gateway startup complete.\n"), C_INTERNAL, I_GATEWAY_READY);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
gw.processRadioMessage();
|
||||
gw.process();
|
||||
|
||||
checkButtonTriggeredInclusion();
|
||||
checkInclusionFinished();
|
||||
|
||||
if (commandComplete) {
|
||||
// A command wass issued from serial interface
|
||||
// We will now try to send it to the actuator
|
||||
gw.parseAndSend(inputString);
|
||||
parseAndSend(gw, inputString);
|
||||
commandComplete = false;
|
||||
inputPos = 0;
|
||||
}
|
||||
@@ -83,3 +125,4 @@ void serialEvent() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user