Hi! I’m looking for a recommendation on how to improve the speed of my code.
I have a ceiling fan with an integrated light that is controlled by a 433 mhz remote. I have tried to run the code directly on an esp8266 and the library times out and crashes (seems to be a part of the code that is blocking and crashes the wifi connection). Also, using the esp8266 or esp32 with ESphome + the remote_reciever library is not an option because this remote is not showing any codes on the remote_reciever output. If outputting raw, there is to much information.
right now I’m using an esp32 serving as a Mqtt to serial transciever, decodes the mqtt messages an it assign a numeric code (from 0 to 20). An arduino uno read the code recieved on serial and sends the code over 433. The code works, the only problem is that there is a 1 to 2 seconds delay between mqtt action (ie using the dashboard button, or pressing a switch) and the fan performing the action itself. How can I speed up the code? (So I can improve WAF)
Arduino code: pastebin url
#include <SoftwareSerial.h>
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
const byte rxPin = 2;
const byte txPin = 3;
// Set up a new SoftwareSerial object
void setup() {
// Define pin modes for TX and RX
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// Set the baud rate for the SoftwareSerial object
Serial.begin(500000);
mySwitch.enableTransmit(4);
mySwitch.setProtocol(10);
}
void loop() {
// put your main code here, to run repeatedly:
String str;
// }
if (Serial.available() > 0) {
str = Serial.readString();
}
if(str.length()>0){
// Serial.println(str);
// int strLength = str.length();
// int index = str.lastIndexOf("/");
// Serial.print("index: ");
// Serial.println(index);
// String sub = str.substring(str.lastIndexOf("/")+1);
// Serial.print("sub: ");
// Serial.println(sub);
//
// int value = sub.substring(sub.lastIndexOf("/")+1).toInt();
// Serial.print("value: ");
// Serial.println(value);
// sendcode(value);
sendcode(str.toInt());
// if(str.indexOf("light/intensity")>0){
// light(value);
// }else if(str.indexOf("light/temperature")>0){
// temperature(value);
// }else if(str.indexOf("fan/speed")>0){
// fanSpeed(value);
// }
}
}
int sendcode(int code){
Serial.print("code ");
Serial.println(code);
switch (code){
case 0:
////Serial.println("Apagar luz ");
mySwitch.send("10011010011010011101111110100000");
break;
case 1:
//Serial.print("Encender luz ");
//Serial.println(code);
mySwitch.send("10101010100101011101111110100000");
break;
case 2:
//Serial.print("Encender luz ");
//Serial.println(code);
mySwitch.send("10101001100101011101111110100000");
break;
case 3:
//Serial.print("Encender luz ");
//Serial.println(code);
mySwitch.send("10100110100101011101111110100000");
break;
case 4:
//Serial.print("Encender luz ");
//Serial.println(code);
mySwitch.send("10100101100101011101111110100000");
break;
case 5:
//Serial.print("Encender luz ");
//Serial.println(code);
mySwitch.send("10011010100101011101111110100000");
break;
case 6:
//Serial.print("Encender luz ");
//Serial.println(code);
mySwitch.send("10011001100101011101111110100000");
// break;
case 7:
//Serial.print("Encender luz ");
//Serial.println(code);
mySwitch.send("10010110100101011101111110100000");
break;
case 8:
//Serial.print("Encender luz ");
//Serial.println(code);
mySwitch.send("10010101100101011101111110100000");
// break;
case 9:
//Serial.print("Encender luz ");
//Serial.println(code);
//Serial.println("Codigo no disponible");
//mySwitch.send("10011001100101011101111110100000");
break;
case 11:
//Serial.println("2700K");
mySwitch.send("10011001011010011101111110100000");
break;
case 12:
//Serial.println("4000K");
mySwitch.send("10010110011010011101111110100000");
break;
case 13:
//Serial.println("6500K");
mySwitch.send("10010101011010011101111110100000");
break;
case 14:
//Serial.print("velocidad ventilador");
//Serial.println(code-10-3);
mySwitch.send("10101001100110011101111110100000");
break;
case 15:
//Serial.print("velocidad ventilador");
//Serial.println(code-10-3);
mySwitch.send("10100110100110011101111110100000");
break;
case 16:
//Serial.print("velocidad ventilador");
//Serial.println(code-10-3);
mySwitch.send("10100101100110011101111110100000");
break;
case 17:
//Serial.print("velocidad ventilador");
//Serial.println(code-10-3);
mySwitch.send("10011010100110011101111110100000");
break;
case 18:
//Serial.print("velocidad ventilador");
//Serial.println(code-10-3);
mySwitch.send("10011001100110011101111110100000");
break;
case 19:
//Serial.print("velocidad ventilador");
//Serial.println(code-10-3);
mySwitch.send("10010110100110011101111110100000");
break;
case 20:
//Serial.println("Apagar ventilador");
mySwitch.send("10100101011001011101111110100000");
break;
}
/*if (code==14 or code == 15 or code ==16){
String fanSpeedString;
fanSpeedString = String(fanSpeed);
fanSpeedString.toCharArray(fanSpeedString,fanSpeedString.length()+1);
mqttClient.publish(fanSpeedString,"sensors/fanSpeed");
}*/
}
/*
void light(int value){
switch(value){
case 0:
mySwitch.send("10011010011010011101111110100000");
break;
case 1:
mySwitch.send("10101010100101011101111110100000");
break;
case 2:
mySwitch.send("10101001100101011101111110100000");
break;
case 3:
mySwitch.send("10100110100101011101111110100000");
break;
case 4:
mySwitch.send("10100101100101011101111110100000");
break;
case 5:
mySwitch.send("10011010100101011101111110100000");
break;
case 6:
mySwitch.send("10011001100101011101111110100000");
break;
case 7:
mySwitch.send("10010110100101011101111110100000");
break;
case 8:
mySwitch.send("10010101100101011101111110100000");
break;
}
}
void temperature(int value){
switch(value){
case 2700:
mySwitch.send("10011001011010011101111110100000");
break;
case 4000:
mySwitch.send("10010110011010011101111110100000");
break;
case 2:
mySwitch.send("10010101011010011101111110100000");
break;
}
}
void fanSpeed(int value){
switch(value){
case 0:
mySwitch.send("10100101011001011101111110100000");
break;
case 1:
mySwitch.send("10101001100110011101111110100000");
break;
case 2:
mySwitch.send("10100110100110011101111110100000");
break;
case 3:
mySwitch.send("10100101100110011101111110100000");
break;
case 4:
mySwitch.send("10011010100110011101111110100000");
break;
case 5:
mySwitch.send("10011001100110011101111110100000");
break;
case 6:
mySwitch.send("10010110100110011101111110100000");
break;
}
}*/
ESPHome Code: pastebin url
esphome:
name: esp-to-433
esp32:
board: esp32dev
framework:
type: arduino
# Enable logging
logger:
level: DEBUG
# Enable Home Assistant API
api:
ota:
password: "11253e65714b71241d3516a40bbd63d5"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Esp-To-433 Fallback Hotspot"
password: "urI1L83WcTKk"
captive_portal:
uart:
tx_pin: 17
rx_pin: 16
baud_rate: 500000
mqtt:
broker: 192.168.1.71
username: !secret mqtt_user
password: !secret mqtt_psw
birth_message:
topic: espto433/status
payload: online
will_message:
topic: espto433/status
payload: offline
id: mqtt_client
on_message:
- topic: espto433/light/intensity
then:
uart.write: !lambda |-
std::string str = x;
int y = atoi(str.c_str());
std::string s;
if(y==0){
s="0";
}else if(y==1){
s="";
}else if(y==2){
s="2";
}else if(y==3){
s="3";
}else if(y==4){
s="4";
}else if(y==5){
s="5";
}else if(y==6){
s="6";
}else if(y==7){
s="7";
}else if(y==8){
s="8";
}
std::vector<unsigned char> v(s.begin(), s.end());
return v;
- topic: espto433/light/temperature
then:
uart.write: !lambda |-
std::string str = x;
int y = atoi(str.c_str());
std::string s;
if(y==2700){
s="11";
}else if(y==4000){
s="12";
}else if(y==6500){
s="13";
}
std::vector<unsigned char> v(s.begin(), s.end());
return v;
- topic: espto433/fan/speed
then:
uart.write: !lambda |-
std::string str = x;
int y = atoi(str.c_str());
std::string s;
if(y==0){
s="20";
}else if(y==1){
s="14";
}else if(y==2){
s="15";
}else if(y==3){
s="16";
}else if(y==4){
s="17";
}else if(y==5){
s="18";
}else if(y==6){
s="19";
}
std::vector<unsigned char> v(s.begin(), s.end());
return v;
Thanks in advance!