Fix a few warnings in MQTTGateway.
Move all gateway examples to "examples" to work better with codebender and Arduino IDE. Ledmode is always enabled for gateway. Still optional to connect the wires though ;) Move MsTimer to local utility folder (no external dependencies).
This commit is contained in:
@@ -10,6 +10,9 @@ version 2 as published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include "MyMQTT.h"
|
||||
#include "utility/MsTimer2.h"
|
||||
|
||||
|
||||
|
||||
char broker[] PROGMEM = MQTT_BROKER_PREFIX;
|
||||
|
||||
@@ -75,26 +78,19 @@ PROGMEM const char *sType[] = {
|
||||
};
|
||||
|
||||
|
||||
MyMQTT::MyMQTT(uint8_t _cepin, uint8_t _cspin, uint8_t _rx, uint8_t _tx, uint8_t _er) :
|
||||
extern volatile uint8_t countRx;
|
||||
extern volatile uint8_t countTx;
|
||||
extern volatile uint8_t countErr;
|
||||
extern uint8_t pinRx;
|
||||
extern uint8_t pinTx;
|
||||
extern uint8_t pinEr;
|
||||
|
||||
MyMQTT::MyMQTT(uint8_t _cepin, uint8_t _cspin) :
|
||||
MySensor(_cepin, _cspin) {
|
||||
if (_rx != NULL) {
|
||||
pinRx = _rx;
|
||||
pinMode(pinRx, OUTPUT);
|
||||
ledMode = true;
|
||||
}
|
||||
if (_tx != NULL) {
|
||||
pinTx = _tx;
|
||||
pinMode(pinTx, OUTPUT);
|
||||
ledMode = true;
|
||||
}
|
||||
if (_er != NULL) {
|
||||
pinEr = _er;
|
||||
pinMode(pinEr, OUTPUT);
|
||||
ledMode = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MyMQTT::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate, void (*inDataCallback)(char *, int *)) {
|
||||
void MyMQTT::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataRate, void (*inDataCallback)(const char *, int *), uint8_t _rx, uint8_t _tx, uint8_t _er) {
|
||||
Serial.begin(BAUD_RATE);
|
||||
repeaterMode = true;
|
||||
isGateway = true;
|
||||
@@ -110,6 +106,18 @@ void MyMQTT::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e dataR
|
||||
RF24::openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
|
||||
RF24::openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
|
||||
RF24::startListening();
|
||||
|
||||
pinRx = _rx;
|
||||
pinMode(pinRx, OUTPUT);
|
||||
pinTx = _tx;
|
||||
pinMode(pinTx, OUTPUT);
|
||||
pinEr = _er;
|
||||
pinMode(pinEr, OUTPUT);
|
||||
|
||||
MsTimer2::set(200, ledTimersInterrupt);
|
||||
MsTimer2::start();
|
||||
|
||||
|
||||
// Send startup log message on serial
|
||||
//Serial.print(PSTR("Started\n"));//TODO: progmem gives error..? error: sType causes a section type conflict with __c
|
||||
Serial.print("Started\n");//TODO: fix this...
|
||||
@@ -127,7 +135,7 @@ void MyMQTT::processRadioMessage() {
|
||||
}
|
||||
|
||||
void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
|
||||
char *str, *p, *value=NULL;
|
||||
char *str, *p;
|
||||
char i = 0;
|
||||
buffer[0]= 0;
|
||||
buffsize = 0;
|
||||
@@ -233,7 +241,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
|
||||
} 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.
|
||||
char newNodeID = loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
|
||||
uint8_t newNodeID = loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
|
||||
if (newNodeID <= MQTT_FIRST_SENSORID) newNodeID = MQTT_FIRST_SENSORID;
|
||||
if (msg.sender==newNodeID) {
|
||||
saveState(EEPROM_LATEST_NODE_ADDRESS,newNodeID);
|
||||
@@ -259,7 +267,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
|
||||
} else if (mGetCommand(msg)==C_PRESENTATION && (msg.type==S_ARDUINO_NODE || msg.type==S_ARDUINO_REPEATER_NODE)) {
|
||||
//Doesnt work to check new sensorID here, this message does not always arrive.. See above.
|
||||
} else if (msg.sender==255 && mGetCommand(msg)==C_INTERNAL && msg.type==I_ID_REQUEST) {
|
||||
char newNodeID = loadState(EEPROM_LATEST_NODE_ADDRESS)+1;
|
||||
unsigned char newNodeID = 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 :(
|
||||
msg.destination = msg.sender; //NodeID
|
||||
@@ -289,7 +297,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
|
||||
Serial.println((char*)&buffer[4]);
|
||||
#endif
|
||||
msg.getString(convBuf);
|
||||
for (int a=0; a<strlen(convBuf); a++) { // Payload
|
||||
for (unsigned int a=0; a<strlen(convBuf); a++) { // Payload
|
||||
buffer[buffsize++] = convBuf[a];
|
||||
}
|
||||
buffer[1]=buffsize-2; // Set correct Remaining length on byte 2.
|
||||
@@ -304,38 +312,7 @@ void MyMQTT::SendMQTT(MyMessage &msg) {
|
||||
}
|
||||
}
|
||||
|
||||
boolean MyMQTT::isLedMode() {
|
||||
return ledMode;
|
||||
}
|
||||
|
||||
void MyMQTT::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--; }
|
||||
|
||||
if(countErr && countErr != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinEr, LOW);
|
||||
} else if(!countErr) {
|
||||
// switching off
|
||||
digitalWrite(pinEr, HIGH);
|
||||
}
|
||||
if(countErr != 255) { countErr--; }
|
||||
}
|
||||
|
||||
void MyMQTT::rxBlink(uint8_t cnt) {
|
||||
if(countRx == 255) { countRx = cnt; }
|
||||
@@ -347,7 +324,7 @@ void MyMQTT::errBlink(uint8_t cnt) {
|
||||
if(countErr == 255) { countErr = cnt; }
|
||||
}
|
||||
|
||||
char MyMQTT::strncpysType_retL(char *str, char index, char start) {
|
||||
char MyMQTT::strncpysType_retL(char *str, unsigned char index, char start) {
|
||||
char c;
|
||||
char l;
|
||||
char *p = (char *)pgm_read_word(&(sType[index]));
|
||||
|
||||
@@ -30,7 +30,7 @@ version 2 as published by the Free Software Foundation.
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#define EEPROM_LATEST_NODE_ADDRESS EEPROM_LOCAL_CONFIG_ADDRESS+0
|
||||
#define EEPROM_LATEST_NODE_ADDRESS ((uint8_t)EEPROM_LOCAL_CONFIG_ADDRESS)
|
||||
#define MQTT_MAX_PACKET_SIZE 100
|
||||
|
||||
#define MQTTPROTOCOLVERSION 3
|
||||
@@ -57,35 +57,28 @@ version 2 as published by the Free Software Foundation.
|
||||
class MyMQTT :
|
||||
public MySensor {
|
||||
public:
|
||||
MyMQTT(uint8_t _cepin=5, uint8_t _cspin=6, uint8_t _rx=NULL, uint8_t _tx=NULL, uint8_t _er=NULL);
|
||||
void begin(rf24_pa_dbm_e paLevel=RF24_PA_LEVEL_GW, uint8_t channel=RF24_CHANNEL, rf24_datarate_e dataRate=RF24_DATARATE, void (*dataCallback)(char *, int *)=NULL);
|
||||
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 *, int *)=NULL, uint8_t _rx=6, uint8_t _tx=5, uint8_t _er=4 );
|
||||
void processRadioMessage();
|
||||
void processMQTTMessage(char *inputString, int inputPos);
|
||||
boolean isLedMode();
|
||||
void ledTimersInterrupt();
|
||||
private:
|
||||
bool MQTTClientConnected;
|
||||
char buffer[MQTT_MAX_PACKET_SIZE];
|
||||
int buffsize;
|
||||
char convBuf[MAX_PAYLOAD*2+1];
|
||||
boolean useWriteCallback;
|
||||
void (*dataCallback)(char *, int *);
|
||||
void (*dataCallback)(const char *, int *);
|
||||
void SendMQTT(MyMessage &msg);
|
||||
char strncpysType_retL(char *str, char index, char start);
|
||||
char strncpysType_retL(char *str, unsigned char index, char start);
|
||||
|
||||
|
||||
volatile uint8_t countRx;
|
||||
volatile uint8_t countTx;
|
||||
volatile uint8_t countErr;
|
||||
uint8_t pinRx;
|
||||
uint8_t pinTx;
|
||||
uint8_t pinEr;
|
||||
boolean ledMode;
|
||||
void ledTimers();
|
||||
void rxBlink(uint8_t cnt);
|
||||
void txBlink(uint8_t cnt);
|
||||
void errBlink(uint8_t cnt);
|
||||
};
|
||||
|
||||
extern void ledTimersInterrupt();
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 2013 Henrik Ekblad <henrik.ekblad@gmail.com>
|
||||
*
|
||||
* Contribution by a-lurker
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* DESCRIPTION
|
||||
* The EthernetGateway sends data received from sensors to the ethernet link.
|
||||
* The gateway also accepts input on ethernet interface, which is then sent out to the radio network.
|
||||
*
|
||||
* The GW code is designed for Arduino 328p / 16MHz. ATmega168 does not have enough memory to run this program.
|
||||
*
|
||||
* COMPILING
|
||||
* You must make sure to disable DEBUG in Sensor.h before compiling this sketch. Othervise the sketch won't fit in program space when downloading.
|
||||
* For UIPEthernet(ENC28J60) usage: Note that I had to disable UDP and DHCP support in uipethernet-conf.h to reduce space. (which meas you ave to choose a static IP)
|
||||
* For WizNET usage: Do *not* install the provided UIPEthernet-library. Remove UIPEthernet-include below and uncomment the Ethernet.h.
|
||||
*
|
||||
* VERA CONFIGURATION:
|
||||
* Enter "ip-number:port" in the ip-field of the Arduino GW device. This will temporarily override any serial configuration for the Vera plugin.
|
||||
* E.g. If you want to use the defualt values in this sketch enter: 192.168.178.66:5003
|
||||
*
|
||||
* LED purposes:
|
||||
* - 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
|
||||
*
|
||||
* ----------- Connection guide ---------------------------------------------------------------------------
|
||||
* 13 Radio & Ethernet SPI SCK
|
||||
* 12 Radio & Ethernet SPI MISO (SO)
|
||||
* 11 Radio & Ethernet SPI MOSI (SI)
|
||||
* 10 Ethernet SPI Slave Select (CS) Pin 10, the SS pin, must be an o/p to put SPI in master mode
|
||||
* 9 Radio TX LED using on board LED (optional) +5V -> LED -> 270-330 Ohm resistor -> pin 9.
|
||||
* 8 Radio RX LED (optional) +5V -> LED -> 270-330 Ohm resistor -> pin 8.
|
||||
* 7 Radio error LED (optional) +5V -> LED -> 270-330 Ohm resistor -> pin 7.
|
||||
* 6 Radio SPI Slave Select
|
||||
* 5 Radio Chip Enable
|
||||
* 3 Inclusion mode button (optional), 10K pull down to GND, button to VCC)
|
||||
* 2 Radio IRQ pin (optional), W5100 Int, if linked to pin 2)
|
||||
* -----------------------------------------------------------------------------------------------------------
|
||||
* Powering: both NRF24l01 radio and Ethernet(ENC28J60) uses 3.3V
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <MyGateway.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
// Use this if you have attached a Ethernet ENC28J60 shields
|
||||
#include <UIPEthernet.h>
|
||||
|
||||
// Use this fo WizNET module and Arduino Ethernet Shield
|
||||
//#include <Ethernet.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 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
|
||||
|
||||
#define IP_PORT 5003 // The port you want to open
|
||||
IPAddress myIp (192, 168, 178, 66); // Configure your static ip-address here
|
||||
|
||||
// The MAC address can be anything you want but should be unique on your network.
|
||||
// Newer boards have a MAC address printed on the underside of the PCB, which you can (optionally) use.
|
||||
// Note that most of the Ardunio examples use "DEAD BEEF FEED" for the MAC address.
|
||||
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 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);
|
||||
|
||||
Ethernet.begin(mac, myIp);
|
||||
|
||||
// give the Ethernet interface a second to initialize
|
||||
delay(1000);
|
||||
|
||||
// start listening for clients
|
||||
server.begin();
|
||||
}
|
||||
|
||||
// This will be called when data should be written to ethernet
|
||||
void writeEthernet(char *writeBuffer) {
|
||||
server.write(writeBuffer);
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
// if an incoming client connects, there will be
|
||||
// bytes available to read via the client object
|
||||
EthernetClient client = server.available();
|
||||
|
||||
if (client) {
|
||||
// if got 1 or more bytes
|
||||
if (client.available()) {
|
||||
// read the bytes incoming from the client
|
||||
char inChar = client.read();
|
||||
|
||||
if (inputPos<MAX_RECEIVE_LENGTH-1) {
|
||||
// if newline then command is complete
|
||||
if (inChar == '\n') {
|
||||
// a command was issued by the client
|
||||
// we will now try to send it to the actuator
|
||||
inputString[inputPos] = 0;
|
||||
|
||||
// echo the string to the serial port
|
||||
Serial.print(inputString);
|
||||
|
||||
gw.parseAndSend(inputString);
|
||||
|
||||
// clear the string:
|
||||
inputPos = 0;
|
||||
} else {
|
||||
// add it to the inputString:
|
||||
inputString[inputPos] = inChar;
|
||||
inputPos++;
|
||||
}
|
||||
} else {
|
||||
// Incoming message too long. Throw away
|
||||
inputPos = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
gw.processRadioMessage();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
/* MyMQTT Broker Gateway 0.1b
|
||||
|
||||
Created by Daniel Wiegert <daniel.wiegert@gmail.com>
|
||||
Based on MySensors Ethernet Gateway by Henrik Ekblad <henrik.ekblad@gmail.com>
|
||||
http://www.mysensors.org
|
||||
|
||||
Requires MySensors lib 1.4b
|
||||
|
||||
* Change below; TCP_IP, TCP_PORT, TCP_MAC
|
||||
This will listen on your selected TCP_IP:TCP_PORT below, Please change TCP_MAC your liking also.
|
||||
*1 -> NOTE: Keep first byte at x2, x6, xA or xE (replace x with any hex value) for using Local Ranges.
|
||||
|
||||
*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
|
||||
|
||||
* 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.clientId=MQTT
|
||||
---
|
||||
|
||||
items/test.items
|
||||
---
|
||||
Group test
|
||||
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 test_test "test [%s]" (test) {mqtt="<[mysensor:MyMQTT/20/3/#:state:default]"}
|
||||
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)
|
||||
|
||||
sitemap/test.site
|
||||
---
|
||||
sitemap demo label="Menu"
|
||||
Frame label="Openhab" {
|
||||
Group item=test label="Test group"
|
||||
}
|
||||
---
|
||||
|
||||
transform/1on0off.map
|
||||
---
|
||||
1=ON
|
||||
0=OFF
|
||||
---
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#include <SPI.h>
|
||||
#include <MyMQTT.h>
|
||||
#include <Ethernet.h>
|
||||
|
||||
|
||||
// Use this for IBOARD modded to use standard MISO/MOSI/SCK, see note *1 above!
|
||||
/*
|
||||
#define RADIO_CE_PIN 3 // radio chip enable
|
||||
#define RADIO_SPI_SS_PIN 8 // radio SPI serial select
|
||||
#define RADIO_ERROR_LED_PIN A2 // Error led pin
|
||||
#define RADIO_RX_LED_PIN A1 // Receive led pin
|
||||
#define RADIO_TX_LED_PIN A0 // the PCB, on board LED*/
|
||||
|
||||
//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
|
||||
#define RADIO_RX_LED_PIN 8 // Receive led pin
|
||||
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED*/
|
||||
|
||||
#define TCP_PORT 1883 // Set your MQTT Broker Listening port.
|
||||
IPAddress TCP_IP ( 192, 168, 0, 234 ); // Configure your static ip-address here
|
||||
byte TCP_MAC[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - You should change this! see note *2 above!
|
||||
|
||||
EthernetServer server = EthernetServer(TCP_PORT);
|
||||
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);
|
||||
|
||||
void processEthernetMessages() {
|
||||
char inputString[MQTT_MAX_PACKET_SIZE] = "";
|
||||
int inputSize = 0;
|
||||
EthernetClient client = server.available();
|
||||
if (client) {
|
||||
while (client.available()) {
|
||||
char inChar = client.read();
|
||||
inputString[inputSize] = inChar;
|
||||
inputSize++;
|
||||
}
|
||||
gw.processMQTTMessage(inputString, inputSize);
|
||||
}
|
||||
}
|
||||
|
||||
void writeEthernet(const char *writeBuffer, int *writeSize) {
|
||||
server.write((const uint8_t *)writeBuffer, *writeSize); // Todo: Should this really be *writeSize?
|
||||
}
|
||||
|
||||
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);
|
||||
server.begin();
|
||||
while (1) {
|
||||
processEthernetMessages();
|
||||
gw.processRadioMessage();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,7 @@
|
||||
const uint8_t maxKeyLength = 7;
|
||||
uint8_t validKeys[][maxKeyLength] = {
|
||||
{ 0xB3, 0xC6, 0xD9, 0x80, 0x00, 0x00, 0x00 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0 },
|
||||
{ 0, 0, 0, 0, 0, 0, 0 }, // ADD YOUR KEYS HERE!
|
||||
{ 0, 0, 0, 0, 0, 0, 0 }};
|
||||
int keyCount = sizeof validKeys / maxKeyLength;
|
||||
|
||||
@@ -84,11 +82,12 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
gw.process(); // Process incomming messages
|
||||
|
||||
boolean success;
|
||||
uint8_t key[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
|
||||
uint8_t currentKeyLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
|
||||
|
||||
gw.process(); // Process incomming messages
|
||||
|
||||
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
|
||||
// 'uid' will be populated with the UID, and uidLength will indicate
|
||||
@@ -159,3 +158,4 @@ void incomingMessage(const MyMessage &message) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2013 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
|
||||
* version 2 as published by the Free Software Foundation.
|
||||
*
|
||||
* DESCRIPTION
|
||||
* The ArduinoGateway prints data received from sensors on the serial link.
|
||||
* The gateway accepts input on seral which will be sent out on radio network.
|
||||
*
|
||||
* The GW code is designed for Arduino Nano 328p / 16MHz
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <MyGateway.h>
|
||||
#include <stdarg.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
|
||||
|
||||
|
||||
MyGateway gw(DEFAULT_CE_PIN, DEFAULT_CS_PIN, INCLUSION_MODE_TIME, INCLUSION_MODE_PIN, 6, 5, 4);
|
||||
|
||||
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 setup()
|
||||
{
|
||||
gw.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
gw.processRadioMessage();
|
||||
checkSerialInput();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void checkSerialInput() {
|
||||
if (commandComplete) {
|
||||
// A command wass issued from serial interface
|
||||
// We will now try to send it to the actuator
|
||||
gw.parseAndSend(inputString);
|
||||
commandComplete = false;
|
||||
inputPos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
SerialEvent occurs whenever a new data comes in the
|
||||
hardware serial RX. This routine is run between each
|
||||
time loop() runs, so using delay inside loop can delay
|
||||
response. Multiple bytes of data may be available.
|
||||
*/
|
||||
void serialEvent() {
|
||||
while (Serial.available()) {
|
||||
// get the new byte:
|
||||
char inChar = (char)Serial.read();
|
||||
// if the incoming character is a newline, set a flag
|
||||
// so the main loop can do something about it:
|
||||
if (inputPos<MAX_RECEIVE_LENGTH-1 && !commandComplete) {
|
||||
if (inChar == '\n') {
|
||||
inputString[inputPos] = 0;
|
||||
commandComplete = true;
|
||||
} else {
|
||||
// add it to the inputString:
|
||||
inputString[inputPos] = inChar;
|
||||
inputPos++;
|
||||
}
|
||||
} else {
|
||||
// Incoming message too long. Throw away
|
||||
inputPos = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user