Hi,
For testing the remote and getting what each button used i think i used this, (which is the RC-Switch library basic example, i think i also used the Advanced version which gives more data):
/*
Simple example for receiving
https://github.com/sui77/rc-switch/
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
Serial.println("Init complete ");
}
void loop() {
//Serial.println("Test");
if (mySwitch.available()) {
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );
mySwitch.resetAvailable();
}
}
Advanced is here: rc-switch/examples/ReceiveDemo_Advanced at master · sui77/rc-switch · GitHub
This printed out in the Serial Monitor the strings in needed.
I then use a project like this to transmit using a WeMos D1 mini board, note i haven’t tested this as my project has more sensors attached to the board, this is a cut down version):
//For 8266
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <RCSwitch.h>
//433MHz Transmitter pinout
const int TRANSMITPIN = 0;
RCSwitch fanTransmit = RCSwitch();
const char* ssid = "****"; // Enter the SSID of your WiFi Network.
const char* password = "****";// Enter the Password of your WiFi Network.
//MQTT stuff
#define mqtt_server "****"
#define mqtt_port 1883
#define mqtt_user "****"
#define mqtt_password "****"
#define bedroomFan_command_topic "home/Bedroom/Fan"
#define bedroomFan_state_topic "home/Bedroom/Fan/State"
#define bedroomFan_mode_topic "home/Bedroom/Fan/Preset_Mode"
#define bedroomFan_mode_state_topic "home/Bedroom/Fan/Preset_Mode/State"
WiFiClient espClient;
PubSubClient client;
const int interval = 60000;
unsigned long previousMillis=0;
char fanOffRawData[] = "001011111101"; //
char fanLowRawData[] = "001011110111"; //
char fanMedRawData[] = "001011101111"; //
char fanHiRawData[] = "001011011111"; //
void setup()
{
Serial.begin(9600,SERIAL_8N1,SERIAL_TX_ONLY);
//setup RC
fanTransmit.enableTransmit(TRANSMITPIN);
fanTransmit.setPulseLength(322);
fanTransmit.setProtocol(11);
fanTransmit.setRepeatTransmit(5);
delay(10);
Serial.println("");
Serial.println("");
Serial.print("Connecting To: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi Connected.");
client.setClient(espClient);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(mqtt_callback);
}
void loop()
{
//MQTT subscribe
//send code depending on MQTT topic received
if (!client.connected()) {
reconnect();
}
client.loop();
unsigned long currentMillis = millis();
}
void sendCode(int button) {
switch(button){
case 1:
//fan off
fanTransmit.send(fanOffRawData);
break;
case 2:
//fan low
fanTransmit.send(fanLowRawData);
break;
case 3:
//fan med
fanTransmit.send(fanMedRawData);
break;
case 4:
//fan high
fanTransmit.send(fanHiRawData);
break;
}
Serial.println("Transmitted Binary code");
//send mqtt status
}
//reconnect to mqtt topic
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Bedroom_New", mqtt_user, mqtt_password)) {
Serial.println("connected");
//now subscribe to topic
client.subscribe(bedroomFan_command_topic);
client.subscribe(bedroomFan_mode_topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//MQTT function
void mqtt_callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
//for (int i = 0; i < length; i++) {
//char receivedChar = (char)payload[i];
payload[length] = '\0';
String strPayload = String((char*)payload);
Serial.println(strPayload);
if(strcmp(topic,bedroomFan_mode_topic) == 0)
{
if(strPayload == "Low")
{
sendCode(2);
client.publish(bedroomFan_mode_state_topic, String("Low").c_str(), true);
client.publish(bedroomFan_state_topic, String("On").c_str(), true);
}
else if(strPayload == "Med")
{
sendCode(3);
client.publish(bedroomFan_mode_state_topic, String("Med").c_str(), true);
client.publish(bedroomFan_state_topic, String("On").c_str(), true);
}
else if(strPayload == "Hi")
{
sendCode(4);
client.publish(bedroomFan_mode_state_topic, String("Hi").c_str(), true);
client.publish(bedroomFan_state_topic, String("On").c_str(), true);
}
}
else if(strcmp(topic,bedroomFan_command_topic) == 0)
{
if(strPayload == "Off")
{
sendCode(1);
client.publish(bedroomFan_state_topic, String("Off").c_str(), true);
}
else if(strPayload == "On")
{
sendCode(2);
client.publish(bedroomFan_mode_state_topic, String("Low").c_str(), true);
client.publish(bedroomFan_state_topic, String("On").c_str(), true);
}
}
Serial.println();
}
This is very crude but should be a starting point if you are familiar with Arduino, it connects to WiFi subscribes to the MQTT broker and then waits in an incoming MQTT topic, it reads the topic and then sends a 433MHz signal. If this is a bit too much then ESPHome should be much easier with something like this: Remote Transmitter — ESPHome
I used the transmitter/receiver pairs mentioned above that i bought on eBay.