SOLVED: No Need to modify Rpi rc-switch python code to input 16bit length integers

Tags: #<Tag:0x00007f7398bd3cc0>

I have a 315 Mhz Woods RF switch that can be controlled by a 315 Mhz with the following Arduino code.

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();

void setup() {
// Transmitter is connected to Arduino Pin #11  
mySwitch.enableTransmit(11);
mySwitch.setProtocol(2);
mySwitch.setPulseLength(800);
mySwitch.setRepeatTransmit(5);
}

int32_t iOff = 26641;
int32_t iOn = 26657;

void loop() {
mySwitch.send(iOn, 16);
delay(5000);  

mySwitch.send(iOff, 16);
delay(5000);
}

Note that the send command takes the number as a 16bit integer.

When I move the 315 MHz transmitter to the Raspberry Pi hosting my Hassio HomeAssistant, the implementation of RCSwitch doesn’t expose the bit length variable in the configuration. The variable is present in the HA python source just not available in the configuration.

Can I add bitlength variable to switch.py and expose it to the configuration in my install? https://github.com/home-assistant/core/blob/dcba45e67d2920fa531ef3521e8d0fa1ddf3dd97/homeassistant/components/rpi_rf/switch.py

Where do I find switch.py in my Hassio filesystem?

Depends how you installed ha.

I used the Etcher image on an SD card for a Raspberry Pi. My Docker skills are beginner level so I will need a tutorial to modify my HA image. I couldn’t find one on the forum.

The source code for HA core/switch.py looks straightforward. It calls the rpi-rf module so the bitlength variable is accessible.

rpi_rf = importlib.import_module("rpi_rf")

So I can add bitlength to the class definition?


"""Representation of a GPIO RF switch."""

    def __init__(
        self,
        name,
        rfdevice,
        lock,
        protocol,
        pulselength,
        signal_repetitions,
        code_on,
        code_off,
        length,
    ):
        """Initialize the switch."""
        self._name = name
        self._state = False
        self._rfdevice = rfdevice
        self._lock = lock
        self._protocol = protocol
        self._pulselength = pulselength
        self._code_on = code_on
        self._code_off = code_off
        self._rfdevice.tx_repeat = signal_repetitions
        self._length = bitlength

And some more changes to expose the variable to the HA configuration

A different solution solved my problem. The ESPHome remote_transmitter is an implementation of rc-switch for ESP chips that allows different word lengths.

I used an inexpensive Selae Logic Analyzer with the transmitter that came with the wireless outlet and captured the signals for on and off.

I then wrote this in the ESPHome yaml file for the ESP8266 device

  pin: GPIO0
  carrier_duty_percent: 100%
switch:
  - platform: template
    name: 315RF On
    turn_on_action:
      - remote_transmitter.transmit_rc_switch_raw:
          code: '0110100000100001'
          repeat:
            times: 3
            wait_time: 10ms
          protocol:
            pulse_length: 600
            sync: [18,1]
            zero: [1,3]
            one: [3,1]
  - platform: template      
    name: 315RF Off
    turn_on_action:
      - remote_transmitter.transmit_rc_switch_raw:
          code: '0110100000010001'
          repeat:
            times: 3
            wait_time: 10ms          
          protocol:
            pulse_length: 600
            sync: [18,1]
            zero: [1,3]
            one: [3,1]
      
type or paste code here

The nice thing is that I can move the ESP8266 close to the receiver switch and I am not limited to keeping the transmitter next to the Raspberry Pi HA server. For the transmitter I am connecting the Vcc pin to the nodeMCU ESP8266 Vin pin- which has USB 5V rather than the 3.3V from the other pins.

The only issue is that the sync pulses are after the signal- where the keyfob transmitter sends sync followed by signal. The receiver outlet works but I don’t understand why the ESPHome remote_transmitter code doesn’t start the transmission with sync.