Bugfixes and cleanup

This commit is contained in:
Henrik Ekblad
2014-09-09 22:33:49 +02:00
parent 7fa54a6e5a
commit 5bb8c9b40c
8 changed files with 24 additions and 218 deletions
+1 -1
View File
@@ -18,6 +18,6 @@
/***
* Enable/Disable debug logging
*/
#define DEBUG
//#define DEBUG
#endif
+3 -1
View File
@@ -81,7 +81,9 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
requestNodeId();
}
debug(PSTR("%s started, id %d\n"), repeaterMode?"repeater":"sensor", nc.nodeId);
if (!isGateway) {
debug(PSTR("%s started, id %d\n"), repeaterMode?"repeater":"sensor", nc.nodeId);
}
// Open reading pipe for messages directed to this node (set write pipe to same)
RF24::openReadingPipe(WRITE_PIPE, TO_ADDR(nc.nodeId));
@@ -104,11 +104,14 @@ 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);
// Setup pipes for radio library
gw.openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
gw.openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
gw.startListening();
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();
@@ -116,12 +119,14 @@ void setup()
// Add interrupt for inclusion button to pin
PCintPort::attachInterrupt(pinInclusion, startInclusionInterrupt, RISING);
// give the Ethernet interface a second to initialize
delay(1000);
// start listening for clients
server.begin();
//output(PSTR("0;0;%d;0;%d;Gateway startup complete.\n"), C_INTERNAL, I_GATEWAY_READY);
}
@@ -132,20 +137,21 @@ void loop()
checkButtonTriggeredInclusion();
checkInclusionFinished();
// 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 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') {
Serial.println("Finished");
// a command was issued by the client
// we will now try to send it to the actuator
inputString[inputPos] = 0;
@@ -167,7 +173,7 @@ void loop()
inputPos = 0;
}
}
}
}
}
@@ -1,7 +1,6 @@
#ifndef __GATEWAYUTIL_H__
#define __GATEWAYUTIL_H__
#include <stdarg.h>
#ifdef ARDUINO
@@ -48,6 +47,7 @@ void setupGateway(uint8_t _rx, uint8_t _tx, uint8_t _er, uint8_t _inc, uint8_t _
countTx = 0;
countErr = 0;
// Setup led pins
pinMode(pinRx, OUTPUT);
pinMode(pinTx, OUTPUT);
@@ -240,7 +240,6 @@ void ledTimersInterrupt() {
digitalWrite(pinEr, HIGH);
}
if(countErr != 255) { countErr--; }
}
void rxBlink(uint8_t cnt) {
@@ -1,120 +0,0 @@
#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
@@ -144,7 +144,11 @@ 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);
// Setup pipes for radio library
gw.openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
gw.openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
gw.startListening();
Ethernet.begin(TCP_MAC, TCP_IP);
@@ -1,83 +0,0 @@
/*
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
@@ -63,8 +63,6 @@ void setup()
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