I did this a while back, but I thought I would share. I initially bought a 433Mhz driveway alarm from Harbor Freight with the intention of using the rpi-rf module (I use it already for all sorts of 433 Mhz sensors)
https://www.harborfreight.com/wireless-security-alert-system-62447.html
The rf-sniffer could not decipher any codes coming from this device so I decided to hack an esp8266 into the receiver unit and when the alarm bell goes off (something in the driveway) it sends a high signal to an input on the ESP8266 which then publishes a message to my MQTT broker and then HA can have automations against it. It works quite well. Here is the Arduino code (which I cobbled together using code from various other people–so credit for a lot of the code is not mine). And I used this code on some door sensors too (that is why the odd variable names)
#include <ESP8266WiFi.h> //ESP library from http://github.com/esp8266/Arduino
#include <ESP8266mDNS.h>
#include <PubSubClient.h> // MQTT library from http://github.com/Imroy/pubsubclient
#include <ArduinoOTA.h> //upload code over the network!
#include <WiFiUdp.h>
#define mqtt_server "192.168.1.185"
/**************************** FOR OTA **************************************************/
#define SENSORNAME "driveway" //change this to whatever you want to call your device
#define OTApassword "secret" //the password you will need to enter to upload remotely via the ArduinoIDE
//int OTAport = 8266;
const char *ssid = "wifissid"; // cannot be longer than 32 characters!
const char *pass = "password"; //
//long previousMillis = 0; // Timer loop from http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
int previousdoor_reading = HIGH;
//IPAddress server( 192, 168, 1, 185 ); // Update these with values suitable for your network.
IPAddress server(192, 168, 1, 185);
WiFiClient espClient;
PubSubClient mqttClient(espClient);
void setup(){
// Setup console
Serial.begin(115200);
mqttClient.setServer(server, 1883);
// mqttClient.setCallback(callback);
pinMode(5, INPUT_PULLUP);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
//int retries = 0;
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("Connection Failed! Rebooting...");
delay(5000);
ESP.restart();
}
//OTA SETUP
// ArduinoOTA.setPort(OTAport);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("driveway");
// No authentication by default
//ArduinoOTA.setPassword((const char *)OTApassword);
//mqttClient.connect("garage");
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("Ready");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// If you do not want to use a username and password, change next line to
// if (client.connect("ESP8266Client")) {
if (mqttClient.connect("driveway")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop(){
ArduinoOTA.handle();
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop();
int door_reading = digitalRead(5);
// Serial.println(reading);
if (previousdoor_reading == HIGH && door_reading == LOW ) {
// counter++;
mqttClient.publish("driveway/motion", "detected");
}
if (previousdoor_reading == HIGH && door_reading == LOW ) {
// counter++;
mqttClient.publish("driveway/motion", "none");
}
delay(1000);
previousdoor_reading = door_reading;
}