Hi,
Let me explain my problem :
I built a multi switch button box with an arduino and RF433 module, i can turn on all the lamps of my living room which are connected to generic outlets
Here the arduino code :
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
const int pinButton = 2; //the pin where we connect the button
const int pinButton2 = 3;
const int pinButton3 = 4;
const int pinButton4 = 5;
const int pinButton5 = 6;
const int pinButton6 = 7;
const int pinButton7 = 8;
const int pinButton8 = 9;
int stateButton = 0;
int stateButton2 = 0;
int stateButton3 = 0;
int stateButton4 = 0;
int stateButton5 = 0;
int stateButton6 = 0;
int stateButton7 = 0;
int stateButton8 = 0;
bool lampState = false;
bool lampState2 = false;
bool lampState3 = false;
bool lampState4 = false;
bool lampState5 = false;
bool lampState6 = false;
bool lampState7 = false;
bool lampState8 = false;
void lamp1_on(){
mySwitch.send(1381719, 24);
delay (5);
Serial.print("lamp1on");
}
void lamp1_off(){
mySwitch.send(1381716, 24);
delay (5);
Serial.print("lamp1off");
}
void lamp2_on(){
mySwitch.send(1394007, 24);
delay (5);
Serial.print("lamp2on");
}
void lamp2_off(){
mySwitch.send(1394004, 24);
delay (5);
Serial.print("lamp2off");
}
void lamp3_on(){
mySwitch.send(1397079, 24);
delay (5);
Serial.print("lamp3on");
}
void lamp3_off(){
mySwitch.send(1397076, 24);
delay (5);
Serial.print("lamp3off");
}
void lamp4_on(){
mySwitch.send(4527447, 24);
delay (5);
Serial.print("lamp4on");
}
void lamp4_off(){
mySwitch.send(4527444, 24);
delay (5);
Serial.print("lamp4off");
}
void lamp5_on(){
mySwitch.send(4539735, 24);
delay (5);
Serial.print("lamp5on");
}
void lamp5_off(){
mySwitch.send(4539732, 24);
delay (5);
Serial.print("lamp5off");
}
void lamp6_on(){
mySwitch.send(4542807, 24);
delay (5);
Serial.print("lamp6on");
}
void lamp6_off(){
mySwitch.send(4542720, 24);
delay (5);
Serial.print("lamp6off");
}
void All_lamp_on(){
mySwitch.send(1381719, 24);
delayMicroseconds (100);
mySwitch.send(1394007, 24);
delayMicroseconds (100);
mySwitch.send(1397079, 24);
delayMicroseconds (100);
mySwitch.send(4527447, 24);
delayMicroseconds (100);
mySwitch.send(4539735, 24);
delayMicroseconds (100);
mySwitch.send(4542807, 24);
delayMicroseconds (100);
Serial.print("All lamp on");
}
void All_lamp_off(){
mySwitch.send(1381716, 24);
delayMicroseconds (100);
mySwitch.send(1394004, 24);
delayMicroseconds (100);
mySwitch.send(1397076, 24);
delayMicroseconds (100);
mySwitch.send(4527444, 24);
delayMicroseconds (100);
mySwitch.send(4539732, 24);
delayMicroseconds (100);
mySwitch.send(4542720, 24);
delayMicroseconds (100);
Serial.print("All lamp off");
}
void setup() {
pinMode(pinButton, INPUT); //set the button pin as INPUT
Serial.begin(9600);
mySwitch.enableTransmit(10);
}
void loop() {
int stateButton = digitalRead(pinButton);
int stateButton2 = digitalRead(pinButton2);
int stateButton3 = digitalRead(pinButton3);
int stateButton4 = digitalRead(pinButton4);
int stateButton5 = digitalRead(pinButton5);
int stateButton6 = digitalRead(pinButton6);
int stateButton7 = digitalRead(pinButton7);
int stateButton8 = digitalRead(pinButton8);
if (stateButton == 1 && lampState == false){
lamp1_on();
delayMicroseconds (100);
lampState = true;
}
else if (stateButton == 1 && lampState == true){
lamp1_off();
delayMicroseconds (100);
lampState = false;
}
if (stateButton2 == 1 && lampState2 == false){
lamp2_on();
delayMicroseconds (100);
lampState2 = true;
}
else if (stateButton2 == 1 && lampState2 == true){
lamp2_off();
delayMicroseconds (100);
lampState2 = false;
}
if (stateButton3 == 1 && lampState3 == false){
lamp3_on();
delayMicroseconds (100);
lampState3 = true;
}
else if (stateButton3 == 1 && lampState3 == true){
lamp3_off();
delayMicroseconds (100);
lampState3 = false;
}
if (stateButton4 == 1 && lampState4 == false){
lamp4_on();
delayMicroseconds (100);
lampState4 = true;
}
else if (stateButton4 == 1 && lampState4 == true){
lamp4_off();
delayMicroseconds (100);
lampState4 = false;
}
if (stateButton5 == 1 && lampState5 == false){
lamp5_on();
delayMicroseconds (100);
lampState5 = true;
}
else if (stateButton5 == 1 && lampState5 == true){
lamp5_off();
delayMicroseconds (100);
lampState5 = false;
}
if (stateButton6 == 1 && lampState6 == false){
lamp6_on();
delayMicroseconds (100);
lampState6 = true;
}
else if (stateButton6 == 1 && lampState6 == true){
lamp6_off();
delayMicroseconds (100);
lampState6 = false;
}
if (stateButton7 == 1){
All_lamp_on();
delayMicroseconds (100);
lampState = true;
lampState2 = true;
lampState3 = true;
lampState4 = true;
lampState5 = true;
lampState6 = true;
}
else if (stateButton8 == 1){
All_lamp_off();
delayMicroseconds (100);
lampState = false;
lampState2 = false;
lampState3= false;
lampState4= false;
lampState5= false;
lampState6= false;
}
}
If you think my code is crap, let me know, i’m learning.
I would like to be able to change the “stateButton” value from Home Assistant. And when i turn on or off a lamp the arduino tell Home assistant the state of the lamp for the switch.
I don’t know how to communicate via serial between arduino and Home Assistant
Due to my code i can’t use the firmata solution.
Should i try mqtt ? i bought 2 ESP8266 this afternoon, should be delivered tomorow. Should i change my arduino code ? Should i let the rf 433 part to the Raspberry, and use the arduino just for switches.
Hope you cand find the time and kidness to help me.
Thanks anyway