减少arduino,通用代码的代码

时间:2015-04-21 09:27:41

标签: c if-statement arduino generalization

我为我的arduino制作了以下代码。 它与MQTT进行了很好的沟通。

不幸的是,我对arduino的记忆负荷几乎已经满了。

我想减少我的代码,因为我现在按房间检查每个房间。 我认为可以创建更通用的代码。

#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>
#include <NewRemoteReceiver.h>
#include <RemoteReceiver.h>
#include <NewRemoteTransmitter.h>
#include <RemoteTransmitter.h>
#include <IRremote.h>


#define server "m20.cloudmqtt.com"
int port = 13365;

// Update these with values suitable for your network.
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte ip[]     = {  192, 168, 0, 120 };

//define variables
unsigned long time;
int RECV_PIN = 5;
IRrecv irrecv(RECV_PIN);
decode_results results;
String readString = "10";
char message_buff[100];
char tokenList[] = "-";
char *tokenPtr;
int charsRead;
String dim[3];
byte dimvalue;

// Create a transmitter on address 65, using digital pin 6 to transmit, 
// with a period duration of 260ms (default), repeating the transmitted
// code 2^3=8 times.
NewRemoteTransmitter transmitter(65, 7, 200, 4);
KaKuTransmitter kaKuTransmitter(7);

EthernetClient ethClient;
PubSubClient client(server, port, callback, ethClient);

void setup()
{ 
  // init serial link for debugging
  Serial.begin(115200);

  Ethernet.begin(mac);
  Serial.println(F("Querying DHCP"));
  if ( Ethernet.begin( mac ) == 0 ) {
    Serial.println(F("DHCP failed, fallback to static IP"));
    Ethernet.begin( mac, ip ) ;
  }
  printIp();

  if(client.connect("arduino-MQTT","jopo","jopo")){
    client.publish("/arduino/status/","hello world");
    client.subscribe("/arduino/commando/");
    Serial.println("Connected");
  }

  NewRemoteReceiver::init(0, 2, showCode);
  irrecv.enableIRIn();
  Serial.println("IR receiver ready");
}

void loop()
{
  // MQTT client loop processing
  client.loop();

  //IR Sensor
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    switch (results.value) {
      case 1470210214:
        transmitter.sendUnit(2,true);
        break;
      case 851019735:
        transmitter.sendUnit(2,false);
        break;
    }
    irrecv.resume();
  }
}


void callback(char* topic, byte* payload, unsigned int length) {
   int i = 0;
   for(i=0; i<length; i++) {
     message_buff[i] = payload[i];
   }

   message_buff[i] = '\0';
   String msgString = String(message_buff);
   tokenPtr = strtok(message_buff, tokenList);

   if (strcmp(topic, "/arduino/commando/") == 0) {
   Serial.println(msgString);
                                  while (tokenPtr != '\0') {
                                  //Serial.println(tokenPtr);
                                   if (strcmp(message_buff, "A1ON") == 0) {
                                    kaKuTransmitter.sendSignal('A', 1, true);
                                    client.publish("/arduino/status/","Living1 aan");
                                  }
                                  else if (strcmp(message_buff, "A1OF") == 0) {
                                    kaKuTransmitter.sendSignal('A', 1, false);
                                    client.publish("/arduino/status/","Living1 uit");
                                  }
                                  if (strcmp(message_buff, "A2ON") == 0) {
                                    kaKuTransmitter.sendSignal('A', 2, true);
                                    client.publish("/arduino/status/","Living2 aan");
                                  }
                                  else if (strcmp(message_buff, "A2OF") == 0) {
                                    kaKuTransmitter.sendSignal('A', 2, false);
                                    client.publish("/arduino/status/","Living2 uit");
                                  }
                                  if (strcmp(message_buff, "A3") == 0) {
                                    tokenPtr = strtok('\0', tokenList);
                                    String dim(tokenPtr);
                                    dimvalue = dim.toInt();      
                                    transmitter.sendDim(3, dimvalue);
                                    client.publish("/arduino/status/","Waarde A3:");
                                    client.publish("/arduino/status/",tokenPtr);
                                  }
                                  if (strcmp(message_buff, "A3ON") == 0) {
                                    kaKuTransmitter.sendSignal('A', 3, true);
                                    client.publish("/arduino/status/","Living3 aan");
                                  }
                                  else if (strcmp(message_buff, "A3OF") == 0) {
                                    kaKuTransmitter.sendSignal('A', 3, false);
                                    client.publish("/arduino/status/","Living3 uit");
                                  }
                                  if (strcmp(message_buff, "B1ON") == 0) {
                                    kaKuTransmitter.sendSignal('B', 1, true);
                                    client.publish("/arduino/status/","Badkamer1 aan");
                                  }
                                  else if (strcmp(message_buff, "B1OF") == 0) {
                                    kaKuTransmitter.sendSignal('B', 1, false);
                                    client.publish("/arduino/status/","Badkamer1 uit");
                                  }
                                  if (strcmp(message_buff, "B2ON") == 0) {
                                    kaKuTransmitter.sendSignal('B', 2, true);
                                    client.publish("/arduino/status/","Badkamer2 aan");
                                  }
                                  else if (strcmp(message_buff, "B2OF") == 0) {
                                    kaKuTransmitter.sendSignal('B', 2, false);
                                    client.publish("/arduino/status/","Badkamer2 uit");
                                  }
                                  if (strcmp(message_buff, "C1ON") == 0) {
                                    kaKuTransmitter.sendSignal('C', 1, true);
                                    client.publish("/arduino/status/","WC aan");
                                  }
                                  else if (strcmp(message_buff, "C1OF") == 0) {
                                    kaKuTransmitter.sendSignal('C', 1, false);
                                    client.publish("/arduino/status/","WC uit");
                                  }
                                  if (strcmp(message_buff, "I1ON") == 0) {
                                    kaKuTransmitter.sendSignal('I', 1, true);
                                    client.publish("/arduino/status/","Inkom aan");
                                  }
                                  else if (strcmp(message_buff, "I1OF") == 0) {
                                    kaKuTransmitter.sendSignal('I', 1, false);
                                    client.publish("/arduino/status/","Inkom uit");
                                  }
                                  if (strcmp(message_buff, "S1ON") == 0) {
                                    kaKuTransmitter.sendSignal('S', 1, true);
                                    client.publish("/arduino/status/","Slaapkamer1 aan");
                                  }
                                  else if (strcmp(message_buff, "S1OF") == 0) {
                                    kaKuTransmitter.sendSignal('S', 1, false);
                                    client.publish("/arduino/status/","Slaapkamer1 uit");
                                  }
                                  if (strcmp(message_buff, "S2ON") == 0) {
                                    kaKuTransmitter.sendSignal('S', 2, true);
                                    client.publish("/arduino/status/","Slaapkamer2 aan");
                                  }
                                  else if (strcmp(message_buff, "S2OF") == 0) {
                                    kaKuTransmitter.sendSignal('S', 2, false);
                                    client.publish("/arduino/status/","Slaapkamer2 uit");
                                  }
                                  if (strcmp(message_buff, "S3ON") == 0) {
                                    kaKuTransmitter.sendSignal('S', 3, true);
                                    client.publish("/arduino/status/","Tussenkamer aan");
                                  }
                                  else if (strcmp(message_buff, "S3OF") == 0) {
                                    kaKuTransmitter.sendSignal('S', 3, false);
                                    client.publish("/arduino/status/","Tussenkamer uit");
                                  }
                                  if (strcmp(message_buff, "S4ON") == 0) {
                                    kaKuTransmitter.sendSignal('S', 4, true);
                                    client.publish("/arduino/status/","Bureau aan");
                                  }
                                  else if (strcmp(message_buff, "S4OF") == 0) {
                                    kaKuTransmitter.sendSignal('S', 4, false);
                                    client.publish("/arduino/status/","Bureau uit");
                                  }
                                  if (strcmp(message_buff, "K1ON") == 0) {
                                    kaKuTransmitter.sendSignal('K', 1, true);
                                    client.publish("/arduino/status/","Keuken aan");
                                    readString = "";
                                  }
                                  else if (strcmp(message_buff, "K1OF") == 0) {
                                    kaKuTransmitter.sendSignal('K', 1, false);
                                    client.publish("/arduino/status/","Keuken uit");
                                  }
                                  if (strcmp(message_buff, "K2ON") == 0) {
                                    kaKuTransmitter.sendSignal('K', 2, true);
                                    client.publish("/arduino/status/","Berging aan");
                                  }
                                  else if (strcmp(message_buff, "K2OF") == 0) {
                                    kaKuTransmitter.sendSignal('K', 2, false);
                                    client.publish("/arduino/status/","Berging uit");
                                  }
                                  if (strcmp(message_buff, "U1ON") == 0) {
                                    kaKuTransmitter.sendSignal('U', 1, true);
                                    client.publish("/arduino/status/","Buitenspot aan");
                                  }
                                  else if (strcmp(message_buff, "U1OF") == 0) {
                                    kaKuTransmitter.sendSignal('U', 1, false);
                                    client.publish("/arduino/status/","Buitenspot uit");
                                  }
                                  tokenPtr = strtok('\0', tokenList);
                                }
   }else{
    Serial.println("arduino topic not found");
  }  
}



//Print local IP address
void printIp() {
  Serial.print(F("My IP "));
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print('.');
  } 
}

// Callback function is called only when a valid code is received.
void showCode(NewRemoteCode receivedCode) {
  // Note: interrupts are disabled. You can re-enable them if needed.

  // Print the received code.
  Serial.print("Addr ");
  Serial.print(receivedCode.address);

  if (receivedCode.groupBit) {
    Serial.print(" group");
  } else {
    Serial.print(" unit ");
    Serial.print(receivedCode.unit);
  }

  switch (receivedCode.switchType) {
    case 0:
      Serial.print(" off");
      break;
    case 1:
      Serial.print(" on");
      break;
    case 2:
      Serial.print(" dim level");
      Serial.print(receivedCode.dimLevel);
      break;
  }

  Serial.print(", period: ");
  Serial.print(receivedCode.period);
  Serial.println("us.");
}

2 个答案:

答案 0 :(得分:4)

您有超过20次的以下说明:

else if (strcmp(message_buff, "AAAA") == 0) {
                                kaKuTransmitter.sendSignal(B, C, D);
                                client.publish("/arduino/status/","DDDD");
                              }

具有AAAA,B,C,D和DDDD的不同值

制作一个这样的表:

struct
{
  const char *message1,
  char        signal,
  int         value1,
  bool        value2,
  const char *pubmessage
} table[] =
{
  {"A1ON", 'A', 1, true,  "Living1 aan"},
  {"A1OF", 'A', 1, false, "Living1 uit},
  {"A2ON", 'A', 2, true,  "Living2 aan},
  {"A2OF", 'A', 2, false, "Living2 uit},
  ....
} ;

在你的回调函数中,而不是调用20次strcmp(...扫描该表并检索与命令字符串相对应的值(“A1ON”等)。

int index = FindCommandIndex(message_buff);
if (index != -1)
{
  kaKuTransmitter.sendSignal(table[index].signal,
                             table[index].value1,
                             table[index].value2)

  client.publish("/arduino/status/", table[index].pubmessage);
}

int FindCommandIndex(const char *message_buff)
{
   // Return the index in table containing the message_buff command.
   // Left as an exercise to the reader.
}

但这可能不会减少你的代码,因为你使用的库肯定会有很多开销。

答案 1 :(得分:1)

除了优化算法之外,另一个问题是你的字符串文字存储在珍贵的RAM中并且你有很多数字。然后将它们包装到F()宏中,然后移动到闪存中。

来自http://playground.arduino.cc/Learning/Memory的示例:

Serial.println(F("This string will be stored in flash memory"));

在您的情况下,而不是:

client.publish("/arduino/status/", .... );

你可以使用:

client.publish(F("/arduino/status/"), .... );