gpt4 book ai didi

c - 减少arduino的代码,通用代码

转载 作者:太空宇宙 更新时间:2023-11-04 05:54:57 26 4
gpt4 key购买 nike

我为我的 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.");
}

最佳答案

您有超过 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.
}

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

关于c - 减少arduino的代码,通用代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29768115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com