HELP with arduino, RF 433 and Home Assistant

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

If the 8266 module you have bought has enough io pins, you can just program it with this code and use either mqtt or http requests to talk to HA.

If not, you will have to use the 8266 as a communications processor. I think the ESPWifi arduino library handles this.

Either way, I think this would be easier than the 433MHz route, since you have the 8266s.

Here are the esp8266 i bought :

tinxi® ESP8266 ESP-12

SunFounder ESP8266

But i will still have to use the 433Mhz module for the rf power outlet. Do you know which platform i should use to communicate between the arduino and the 8266 ? Mysensor ?

I think the ESP12 has 9 IO ports available, and you need 8 for the switches. Presumably you also need 1 for the 433Mhz radio, so I think that will be ok to run on the esp8266 directly.

I would replace the arduino with the esp8266, run your code on the esp8266, but use the arduino PubSubClient library to publish mqtt messages.

I believe the arduino ESP8266Wifi library can communicate with the ESP 8266 through an IO pin, but I have only ever run the code directly on the esp8266 when I needed WiFi. MySensors is a protocol for talking between two RF nodes, so isn’t appropriate here.

Edit:
I think I just realized what you first question was trying to say about 433MHz and the Pi. You could run a 433MHz receiver on the Pi and interpret the signals there for passing on to HA. The would have 2 spare 8266s to play with for something else.

Thanks i’ll try that tomorrow.

Several solutions in term if architecture are available, you could take a look first at the esp8266 gateway:


You can place it either on your control box directly by wiring the gpio as stated above, the gateway has mqtt and bidirectional rf.
Or you can let your control box like that and add the gateway withou connecting it to the box. Indeed the gateway will trigger the hass switch by receiving the rf sognal from the box and send it to mqtt

Hi

Thanks for the advice, i m gonna choose the first solution cause i want my box to be updated and it doesn’t have a RF receiver; only a transmitter.

You’d better send a mqtt message each time a button is pressed.
Don’t try so save the state of each lamp in the arduino, Home-assistant should take care of that.

Try to use your board with an esp8266 simply as mqtt switch. And you can send the rf message from home-assistant (using a second esp, running the code from 1technophile to send rf code).

You can find example of code for your esp:
Use nodemcu to send push-button signal to remote mqtt-client

Thanks a lot

Does my esp8266 esp12 work exactly like the nodeMCU ? what i mean is can i use my esp8266 esp12 instead of the nodeMCU in the link you provide ?

Thanks again

The nodeMCU is based on the ESP-12 that you have bought. It has a regulator so you can power it with a usb power supply and renames the IO pins into different numbers, which are mapped in the arduino IDE if you select the NodeMCU board type.

So I think you can use that code, but you will need to use the generic Esp8266 board type in arduino to get to the IO pins to match what is labelled on the ESP-12 module you have. With the 12-E you have, you will need a USB-Serial port to program them, but I assume you knew that.

On the other hand, you have code that already works and you understand. Not storing the state locally is good advice, but I think you should tackle that after you have something up and running with something you understand.

Yes NodeMCU are basically esp board with a little addition (embedded serial, voltage regulator, …)
and I would recommend you to order the very inexpensive wemos d1 mini from their aliexpress shop (see www.wemos.cc).
Those thinxy board are not really user friendly when it comes to programming/powering them and you’ll get a much easier start with the wemos d1 mini.

D1 mini 3.8€

I use ESP1, 201 and wemos with ESP Easy software has a KaKu option in the playground, haven’t been able to bind them into HA, cause of not enough brainpower, and many strange errors in HA, but it should be able to bind via MQTT or command line, all other inputs, switches and sensors work flawless on my esp’s with ESp easy, real fun.

O by the way there is an option to set the rx and tx pins on the esp8266 too which gives you an extra 2 pins, gives 12 io pins, off which one is ADC, on a wemos or 201.

Works also without extra filter like my old pilight configuration, and with cheap chinese rf and tx modules I get a great reception of codes, readable in json or log file on the esp, and transmit power is good enough for a room and just outside, crank up voltage to 5V, think about the level converter 5 <-> 3v and give them a good antenna and you get further.