Merge pull request #21 from Damme/patch-1

Minor changes + Added back LED-blinking code.
This commit is contained in:
Henrik Ekblad
2014-08-15 15:32:41 +02:00
3 changed files with 140 additions and 57 deletions
Regular → Executable
+25 -6
View File
@@ -57,21 +57,31 @@ String sketch21 "Node 21 Sketch name [%s]" (sketch,name,a) {mqtt="<[mysensor:M
Read and set EEPROM Values Read and set EEPROM Values
Send Reboot, And reboot gateway itself. 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>
// 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 More information see;
// http://forum.mysensors.org/topic/224/iboard-cheap-single-board-ethernet-arduino-with-radio/5 // 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_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 : //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_RX_LED_PIN 8 // Receive led pin
#define RADIO_TX_LED_PIN 9 // the PCB, on board LED
#define IP_PORT 1883 // MQTT Listening port #define IP_PORT 1883 // MQTT Listening port
IPAddress myIp ( 192, 168, 0, 234 ); // Configure your static ip-address here IPAddress myIp ( 192, 168, 0, 234 ); // Configure your static ip-address here
@@ -80,6 +90,8 @@ byte mac[] = { 0x02, 0xDE, 0xAD, 0x00, 0x00, 0x42 }; // Mac-address - Change th
EthernetServer server = EthernetServer(IP_PORT); EthernetServer server = EthernetServer(IP_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);
void processEthernetMessages() { void processEthernetMessages() {
char inputString[MQTT_MAX_PACKET_SIZE] = ""; char inputString[MQTT_MAX_PACKET_SIZE] = "";
@@ -99,16 +111,23 @@ void writeEthernet(char *writeBuffer, int *writeSize) {
server.write(writeBuffer, *writeSize); // Should this really be *writeSize? server.write(writeBuffer, *writeSize); // Should this really be *writeSize?
} }
void ledTimersInterrupt() {
gw.ledTimersInterrupt();
}
int main(void) { int main(void) {
init(); init();
Ethernet.begin(mac, myIp); Ethernet.begin(mac, myIp);
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
MsTimer2::set(300, ledTimersInterrupt);
MsTimer2::start();
}
while (1) { while (1) {
processEthernetMessages(); processEthernetMessages();
gw.processRadioMessage(); gw.processRadioMessage();
} }
} }
Regular → Executable
+69 -15
View File
@@ -73,24 +73,32 @@ PROGMEM const char *sType[] =
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) : MyMQTT::MyMQTT(uint8_t _cepin, uint8_t _cspin, uint8_t _rx, uint8_t _tx, uint8_t _er) :
MySensor(_cepin, _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)(char *, int *)) {
Serial.begin(BAUD_RATE); Serial.begin(BAUD_RATE);
repeaterMode = true; repeaterMode = true;
isGateway = true; isGateway = true;
MQTTClient = false; MQTTClientConnected = false;
setupRepeaterMode(); setupRepeaterMode();
if (inDataCallback != NULL) {
useWriteCallback = true;
dataCallback = inDataCallback; dataCallback = inDataCallback;
}
else {
useWriteCallback = false;
}
nc.nodeId = 0; nc.nodeId = 0;
nc.distance = 0; nc.distance = 0;
@@ -110,6 +118,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);
SendMQTT(message); SendMQTT(message);
} }
@@ -130,7 +139,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
buffer[buffsize++] = 0x02; // Remaining length buffer[buffsize++] = 0x02; // Remaining length
buffer[buffsize++] = 0x00; // Connection accepted buffer[buffsize++] = 0x00; // Connection accepted
buffer[buffsize++] = 0x00; // Reserved buffer[buffsize++] = 0x00; // Reserved
MQTTClient=true; MQTTClientConnected=true;
} }
if ((byte)inputString[0] >> 4 == MQTTPINGREQ) { if ((byte)inputString[0] >> 4 == MQTTPINGREQ) {
buffer[buffsize++] = MQTTPINGRESP << 4; buffer[buffsize++] = MQTTPINGRESP << 4;
@@ -150,7 +159,7 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
buffer[buffsize++] = (byte)inputString[3]; // Message ID LSB buffer[buffsize++] = (byte)inputString[3]; // Message ID LSB
} }
if ((byte)inputString[0] >> 4 == MQTTDISCONNECT) { if ((byte)inputString[0] >> 4 == MQTTDISCONNECT) {
MQTTClient=false; MQTTClientConnected=false;
} }
if (buffsize > 0) { if (buffsize > 0) {
#ifdef TCPDUMP #ifdef TCPDUMP
@@ -209,7 +218,8 @@ void MyMQTT::processMQTTMessage(char *inputString, int inputPos) {
mSetRequestAck(msg,false); mSetRequestAck(msg,false);
mSetAck(msg,false); mSetAck(msg,false);
mSetVersion(msg, PROTOCOL_VERSION); mSetVersion(msg, PROTOCOL_VERSION);
sendRoute(msg); txBlink(1);
if (!sendRoute(msg)) errBlink(1);
} }
} }
@@ -217,7 +227,7 @@ 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 (!MQTTClient) 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) {
@@ -248,7 +258,8 @@ Serial.println("msg is ack!");
mSetAck(msg,false); mSetAck(msg,false);
msg.set("M"); msg.set("M");
mSetVersion(msg, PROTOCOL_VERSION); mSetVersion(msg, PROTOCOL_VERSION);
sendRoute(msg); txBlink(1);
if (!sendRoute(msg)) errBlink(1);
} else if (mGetCommand(msg)==C_PRESENTATION && (msg.type==S_ARDUINO_NODE || msg.type==S_ARDUINO_REPEATER_NODE)) { } 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. //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) { } else if (msg.sender==255 && mGetCommand(msg)==C_INTERNAL && msg.type==I_ID_REQUEST) {
@@ -264,7 +275,8 @@ Serial.println("msg is ack!");
mSetAck(msg,false); mSetAck(msg,false);
msg.set((uint8_t)newNodeID); //Payload msg.set((uint8_t)newNodeID); //Payload
mSetVersion(msg, PROTOCOL_VERSION); mSetVersion(msg, PROTOCOL_VERSION);
sendRoute(msg); txBlink(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;
@@ -296,6 +308,48 @@ Serial.println("msg is ack!");
} }
} }
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; }
}
void MyMQTT::txBlink(uint8_t cnt) {
if(countTx == 255) { countTx = cnt; }
}
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, char index, char start) {
char c; char c;
char l; char l;
Regular → Executable
+18 -8
View File
@@ -14,7 +14,6 @@ version 2 as published by the Free Software Foundation.
#include "MySensor.h" #include "MySensor.h"
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
#ifdef DEBUG #ifdef DEBUG
@@ -54,17 +53,19 @@ 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:
MyMQTT(uint8_t _cepin=9, uint8_t _cspin=10); 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); 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);
void processRadioMessage(); void processRadioMessage();
void processMQTTMessage(char *inputString, int inputPos); void processMQTTMessage(char *inputString, int inputPos);
boolean isLedMode();
void ledTimersInterrupt();
private: private:
bool MQTTClient; bool MQTTClientConnected;
char buffer[MQTT_MAX_PACKET_SIZE]; char buffer[MQTT_MAX_PACKET_SIZE];
int buffsize; int buffsize;
char convBuf[MAX_PAYLOAD*2+1]; char convBuf[MAX_PAYLOAD*2+1];
@@ -72,9 +73,18 @@ private:
void (*dataCallback)(char *, int *); void (*dataCallback)(char *, int *);
void SendMQTT(MyMessage &msg); void SendMQTT(MyMessage &msg);
char strncpysType_retL(char *str, char index, char start); char strncpysType_retL(char *str, 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);
}; };
#endif #endif