Sram optimization

This commit is contained in:
1technophile committed 2017-04-20 23:11:09 +02:00
1 parent ec90b44360
commit 8c174255c4
4 files changed
+55 -41

No files matched your search

+8 -6
View File
@@ -35,6 +35,7 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT
*/ */
#include "User_config.h" #include "User_config.h"
#include <PubSubClient.h> #include <PubSubClient.h>
#include <avr/pgmspace.h>
// array to store previous received RFs, IRs codes and their timestamps // array to store previous received RFs, IRs codes and their timestamps
unsigned long ReceivedSignal[10][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}; unsigned long ReceivedSignal[10][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
@@ -55,7 +56,7 @@ void callback(char*topic, byte* payload,unsigned int length);
PubSubClient client(mqtt_server, mqtt_port, callback, eClient); PubSubClient client(mqtt_server, mqtt_port, callback, eClient);
//MQTT last attemps reconnection date //MQTT last attemps reconnection date
int lastReconnectAttempt = 0; unsigned int lastReconnectAttempt = 0;
boolean reconnect() { boolean reconnect() {
// Loop until we're reconnected // Loop until we're reconnected
@@ -175,6 +176,7 @@ void loop()
} else { //connected } else { //connected
// MQTT loop // MQTT loop
client.loop(); client.loop();
#ifdef ZsensorDHT #ifdef ZsensorDHT
MeasureTempAndHum(); //Addon to measure the temperature with a DHT MeasureTempAndHum(); //Addon to measure the temperature with a DHT
#endif #endif
@@ -189,6 +191,7 @@ void loop()
if(resultIR) if(resultIR)
trc(F("IR successfully sent by MQTT")); trc(F("IR successfully sent by MQTT"));
#endif #endif
} }
delay(100); delay(100);
} }
@@ -240,24 +243,23 @@ return false;
} }
void receivingMQTT(char * topicOri, char * datacallback) { void receivingMQTT(char * topicOri, char * datacallback) {
String topic = topicOri;
trc(F("Receiving data by MQTT")); trc(F("Receiving data by MQTT"));
trc(topic); trc(String(topicOri));
trc(F("Callback value")); trc(F("Callback value"));
trc(String(datacallback)); trc(String(datacallback));
unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295 unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
trc(F("Converted value to unsigned long")); trc(F("Converted value to unsigned long"));
trc(String(data)); trc(String(data));
// Storing data received if (strstr(topicOri, subjectMultiGTWKey) != NULL)
int pos0 = topic.lastIndexOf(subjectMultiGTWKey); {
if (pos0 != -1){
trc(F("Storing signal")); trc(F("Storing signal"));
storeValue(data); storeValue(data);
trc(F("Data stored")); trc(F("Data stored"));
} }
#ifdef ZgatewayRF #ifdef ZgatewayRF
MQTTtoRF(topicOri, datacallback); MQTTtoRF(topicOri, datacallback);
#endif #endif
+14 -10
View File
@@ -28,7 +28,7 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT
/*-------------DEFINE YOUR MQTT & NETWORK PARAMETERS BELOW----------------*/ /*-------------DEFINE YOUR MQTT & NETWORK PARAMETERS BELOW----------------*/
//MQTT Parameters definition //MQTT Parameters definition
#define mqtt_server "192.168.1.17" #define mqtt_server "192.168.1.17"
#define mqtt_user "your_username" // not compulsory only if your broker needs authentication //#define mqtt_user "your_username" // not compulsory only if your broker needs authentication
#define mqtt_password "your_password" // not compulsory only if your broker needs authentication #define mqtt_password "your_password" // not compulsory only if your broker needs authentication
#define mqtt_port 1883 #define mqtt_port 1883
#define Gateway_Name "OpenMQTTGateway" #define Gateway_Name "OpenMQTTGateway"
@@ -43,14 +43,14 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT
#define wifi_ssid "wifi ssid" #define wifi_ssid "wifi ssid"
#define wifi_password "wifi password" #define wifi_password "wifi password"
#else // for arduino + W5100 #else // for arduino + W5100
const byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95 }; //W5100 ethernet shield mac adress const byte mac[] PROGMEM = { 0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95 }; //W5100 ethernet shield mac adress
#endif #endif
const byte ip[] = { 192, 168, 1, 99 }; //ip adress const byte ip[] PROGMEM = { 192, 168, 1, 99 }; //ip adress
// Advanced network config (optional) if you want to use these parameters uncomment line 158, 172 and comment line 171 of OpenMQTTGateway.ino // Advanced network config (optional) if you want to use these parameters uncomment line 158, 172 and comment line 171 of OpenMQTTGateway.ino
const byte gateway[] = { 192, 168, 1, 1 }; //ip adress const byte gateway[] PROGMEM = { 192, 168, 1, 1 }; //ip adress
const byte Dns[] = { 192, 168, 1, 1 }; //ip adress const byte Dns[] PROGMEM = { 192, 168, 1, 1 }; //ip adress
const byte subnet[] = { 255, 255, 255, 0 }; //ip adress const byte subnet[] PROGMEM = { 255, 255, 255, 0 }; //ip adress
//Addons and module management, comment the line if you don't use //Addons and module management, comment the line if you don't use
//#define ZsensorDHT //#define ZsensorDHT
@@ -70,17 +70,20 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
#define subjectMultiGTWKey "toMQTT" #define subjectMultiGTWKey "toMQTT"
//433Mhz MQTT Subjects and keys //433Mhz MQTT Subjects and keys
#define subjectMQTTto433 "home/commands/MQTTto433" #define subjectMQTTtoRF "home/commands/MQTTto433"
#define subject433toMQTT "home/433toMQTT" #define subjectRFtoMQTT "home/433toMQTT"
#define subjectGTWRFtoMQTT "home/433toMQTT" #define subjectGTWRFtoMQTT "home/433toMQTT"
#define subject433toMQTTAdvanced "home/433toMQTTAdvanced" #define subjectRFtoMQTTport "home/433toMQTT/port"
#define subjectRFtoMQTTbits "home/433toMQTT/bits"
#define subjectRFtoMQTTlength "home/433toMQTT/length"
#define RFprotocolKey "433_" // protocol will be defined if a subject contains RFprotocolKey followed by a value of 1 digit #define RFprotocolKey "433_" // protocol will be defined if a subject contains RFprotocolKey followed by a value of 1 digit
#define RFpulselengthKey "PLSL_" // pulselength will be defined if a subject contains RFprotocolKey followed by a value of 3 digits #define RFpulselengthKey "PLSL_" // pulselength will be defined if a subject contains RFprotocolKey followed by a value of 3 digits
//IR MQTT Subjects //IR MQTT Subjects
#define subjectGTWIRtoMQTT "home/IRtoMQTT" #define subjectGTWIRtoMQTT "home/IRtoMQTT"
#define subjectIRtoMQTT "home/IRtoMQTT" #define subjectIRtoMQTT "home/IRtoMQTT"
#define subjectMQTTtoIR "home/commands/MQTTtoIR" #define subjectMQTTtoIR "home/commands/MQTTtoIR"
#define subjectIRtoMQTTAdvanced "home/IRtoMQTTAdvanced" #define subjectIRtoMQTTport "home/IRtoMQTT/port"
#define subjectIRtoMQTTbits "home/IRtoMQTT/bits"
/* /*
RF supported protocols RF supported protocols
433_1 433_1
@@ -118,3 +121,4 @@ IR_Whynter
//Do we want to see trace for debugging purposes //Do we want to see trace for debugging purposes
#define TRACE 1 // 0= trace off 1 = trace on #define TRACE 1 // 0= trace off 1 = trace on
+20 -19
View File
@@ -45,8 +45,6 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT
IRsend irsend; //connect IR emitter pin to D9 on arduino, you need to comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library IRremote.h so as to free pin D3 for RF RECEIVER PIN IRsend irsend; //connect IR emitter pin to D9 on arduino, you need to comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library IRremote.h so as to free pin D3 for RF RECEIVER PIN
#endif #endif
decode_results results;
void setupIR() void setupIR()
{ {
//IR init parameters //IR init parameters
@@ -58,16 +56,21 @@ void setupIR()
} }
boolean IRtoMQTT(){ boolean IRtoMQTT(){
unsigned long MQTTvalue = 0; decode_results results;
String valueAdvanced;
if (irrecv.decode(&results)){ if (irrecv.decode(&results)){
trc(F("Receiving IR signal")); trc(F("Receiving IR signal"));
MQTTvalue=results.value; unsigned long MQTTvalue = 0;
valueAdvanced = "Value " + String(MQTTvalue)+" Bit " + String(results.bits) + " Protocol " + String(results.decode_type); int MQTTport = 0;
int MQTTbits = 0;
MQTTvalue = results.value;
MQTTport = results.decode_type;
MQTTbits = results.bits;
irrecv.resume(); // Receive the next value irrecv.resume(); // Receive the next value
if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
trc(F("Sending advanced signal to MQTT")); trc(F("Sending advanced data to MQTT"));
client.publish(subjectIRtoMQTTAdvanced,(char *)valueAdvanced.c_str()); client.publish(subjectIRtoMQTTport,(char *)MQTTport);
client.publish(subjectIRtoMQTTbits,(char *)MQTTbits);
trc(F("Sending IR to MQTT")); trc(F("Sending IR to MQTT"));
String value = String(MQTTvalue); String value = String(MQTTvalue);
trc(value); trc(value);
@@ -79,10 +82,8 @@ boolean IRtoMQTT(){
} }
void MQTTtoIR(char * topicOri, char * datacallback) { void MQTTtoIR(char * topicOri, char * datacallback) {
String topic = topicOri;
trc(F("Receiving data by MQTT")); trc(F("Receiving data by MQTT"));
trc(topic);
trc(F("Callback value")); trc(F("Callback value"));
trc(String(datacallback)); trc(String(datacallback));
unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295 unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
@@ -93,40 +94,40 @@ void MQTTtoIR(char * topicOri, char * datacallback) {
//send received MQTT value by IR signal (example of signal sent data = 1086296175) //send received MQTT value by IR signal (example of signal sent data = 1086296175)
boolean signalSent = false; boolean signalSent = false;
#ifdef ESP8266 // send coolix not available for arduino IRRemote library #ifdef ESP8266 // send coolix not available for arduino IRRemote library
if (topic.lastIndexOf("IR_COOLIX") != -1 ){ if (strstr(topicOri, "IR_COOLIX") != NULL){
irsend.sendCOOLIX(data, 24); irsend.sendCOOLIX(data, 24);
signalSent = true; signalSent = true;
} }
#endif #endif
if (topic.lastIndexOf("IR_NEC")!= -1 || topic == subjectMQTTtoIR){ if (strstr(topicOri, "IR_NEC") != NULL || strstr(topicOri, subjectMQTTtoIR) != NULL ){
irsend.sendNEC(data, 32); irsend.sendNEC(data, 32);
signalSent = true; signalSent = true;
} }
if (topic.lastIndexOf("IR_Whynter")!=-1){ if (strstr(topicOri, "IR_Whynter") != NULL){
irsend.sendWhynter(data, 32); irsend.sendWhynter(data, 32);
signalSent = true; signalSent = true;
} }
if (topic.lastIndexOf("IR_LG")!=-1){ if (strstr(topicOri, "IR_LG") != NULL){
irsend.sendLG(data, 28); irsend.sendLG(data, 28);
signalSent = true; signalSent = true;
} }
if (topic.lastIndexOf("IR_Sony")!=-1){ if (strstr(topicOri, "IR_Sony") != NULL){
irsend.sendSony(data, 12); irsend.sendSony(data, 12);
signalSent = true; signalSent = true;
} }
if (topic.lastIndexOf("IR_DISH")!=-1){ if (strstr(topicOri, "IR_DISH") != NULL){
irsend.sendDISH(data, 16); irsend.sendDISH(data, 16);
signalSent = true; signalSent = true;
} }
if (topic.lastIndexOf("IR_RC5")!=-1){ if (strstr(topicOri, "IR_RC5") != NULL){
irsend.sendRC5(data, 12); irsend.sendRC5(data, 12);
signalSent = true; signalSent = true;
} }
if (topic.lastIndexOf("IR_Sharp")!=-1){ if (strstr(topicOri, "IR_Sharp") != NULL){
irsend.sendSharpRaw(data, 15); irsend.sendSharpRaw(data, 15);
signalSent = true; signalSent = true;
} }
if (topic.lastIndexOf("IR_SAMSUNG")!=-1){ if (strstr(topicOri, "IR_SAMSUNG") != NULL){
irsend.sendSAMSUNG(data, 32); irsend.sendSAMSUNG(data, 32);
signalSent = true; signalSent = true;
} }
+13 -6
View File
@@ -50,20 +50,27 @@ void setupRF()
} }
boolean RFtoMQTT(){ boolean RFtoMQTT(){
unsigned long MQTTvalue = 0;
String valueAdvanced;
if (mySwitch.available()){ if (mySwitch.available()){
trc(F("Receiving 433Mhz signal")); trc(F("Receiving 433Mhz signal"));
unsigned long MQTTvalue = 0;
int MQTTport = 0;
int MQTTbits = 0;
int MQTTlength = 0;
MQTTvalue = mySwitch.getReceivedValue(); MQTTvalue = mySwitch.getReceivedValue();
valueAdvanced = "Value " + String(MQTTvalue)+" Bit " + String(mySwitch.getReceivedBitlength()) + " Delay " + String(mySwitch.getReceivedDelay()) + " Protocol " + String(mySwitch.getReceivedProtocol()); MQTTport = mySwitch.getReceivedProtocol();
MQTTbits = mySwitch.getReceivedBitlength();
MQTTlength = mySwitch.getReceivedDelay();
mySwitch.resetAvailable(); mySwitch.resetAvailable();
if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
trc(F("Sending advanced signal to MQTT")); trc(F("Sending advanced signal to MQTT"));
client.publish(subject433toMQTTAdvanced,(char *)valueAdvanced.c_str()); client.publish(subjectRFtoMQTTport,(char *)MQTTport);
client.publish(subjectRFtoMQTTbits,(char *)MQTTbits);
client.publish(subjectRFtoMQTTlength,(char *)MQTTlength);
trc(F("Sending RF to MQTT")); trc(F("Sending RF to MQTT"));
String value = String(MQTTvalue); String value = String(MQTTvalue);
trc(value); trc(value);
boolean result = client.publish(subject433toMQTT,(char *)value.c_str()); boolean result = client.publish(subjectRFtoMQTT,(char *)value.c_str());
return result; return result;
} }
} }
@@ -109,7 +116,7 @@ void MQTTtoRF(char * topicOri, char * datacallback) {
trc(String(valuePLSL)); trc(String(valuePLSL));
} }
if ((topic == subjectMQTTto433) && (valuePRT == 0) && (valuePLSL == 0)){ if ((topic == subjectMQTTtoRF) && (valuePRT == 0) && (valuePLSL == 0)){
trc(F("Sending data by RF, default parameters")); trc(F("Sending data by RF, default parameters"));
mySwitch.setProtocol(1,350); mySwitch.setProtocol(1,350);
mySwitch.send(data, 24); mySwitch.send(data, 24);