Blog

Speed Radar in the Hallway - Home Assistant Integration

2/1/2024 3 min read

How fast do you walk through your hallway every day, and does it vary throughout the day? Or who parks faster in the home garage? This post shows you how to record and analyze these speeds using a speed radar integrated with Home Assistant.

Speed Radar in the Hallway - Home Assistant Integration

How fast do you walk through your hallway every day—and does it perhaps vary over the course of the day?

Or who parks faster in the home garage?

It would be practical to record and analyze these speeds—and that’s exactly what this post is about.


Background

In the past, we’ve already output the speed of people and objects directly in the console.

Today, we take care of sending the data via MQTT to Home Assistant.

What you do with it afterwards is, of course, up to you.


Programming the Microcontroller

The code should run on all common microcontrollers—as long as they have Wi-Fi capability.

It doesn’t matter whether Arduino, ESP32, or other alternatives.

Prerequisite:
MQTT is already set up in Home Assistant.

The original script was extended with Wi-Fi and MQTT functionality.

You need to adjust the variables in the code to fit your network.

If you don’t have a particularly complex Home Assistant installation, the MQTT server is usually your Home Assistant server.

For this, you need a user in Home Assistant—it’s worth creating a dedicated MQTT user.


Example Code

#include 
#include 

// Wi-Fi configuration
const char* ssid = "YOUR_WIFI_SSID";          // Wi-Fi SSID
const char* password = "YOUR_WIFI_PASSWORD"; // Wi-Fi password

// MQTT server configuration
const char* mqttServer = "YOUR_MQTT_SERVER";
const int mqttPort = 1883;
const char* mqttUser = "YOUR_MQTT_USER";
const char* mqttPassword = "YOUR_MQTT_PASSWORD";
const char* mqttTopic = "homeassistant/sensor/speed_sensor/state";

WiFiClient espClient;
PubSubClient client(espClient);

const int sensorPin = 26;
const float frequency = 10.525e9;
const float c = 3e8;
const int measureDuration = 100;
const int numMeasurements = 10;

volatile unsigned long pulseCount = 0;

void IRAM_ATTR handlePulse() {
  pulseCount++;
}

void setup() {
  Serial.begin(115200);
  pinMode(sensorPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(sensorPin), handlePulse, RISING);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.println("Connecting to Wi-Fi...");
  }
  Serial.println("Connected to Wi-Fi");

  // Connect to MQTT
  client.setServer(mqttServer, mqttPort);
  while (!client.connected()) {
    Serial.println("Connecting to MQTT server...");
    if (client.connect("ESP32Client", mqttUser, mqttPassword)) {
      Serial.println("Connected to MQTT server");
    } else {
      Serial.print("Failed to connect to MQTT: ");
      Serial.println(client.state());
      delay(2000);
    }
  }

  // Home Assistant Discovery
  String configTopic = "homeassistant/sensor/speed_sensor/config";
  String configPayload = "{\"name\": \"Speed Sensor\", \"state_topic\": \"" 
    + String(mqttTopic) + "\", \"unit_of_measurement\": \"km/h\", \"value_template\": \"{{ value }}\"}";
  client.publish(configTopic.c_str(), configPayload.c_str(), true);
}

void loop() {
  float totalSpeed = 0.0;
  unsigned long startTime = millis();

  for (int i = 0; i < numMeasurements; i++) {
    unsigned long measurementStartTime = millis();
    pulseCount = 0;

    // Measurement (100 ms)
    delay(measureDuration);

    unsigned long duration = millis() - measurementStartTime;
    unsigned long pulses = pulseCount;

    float pulseFrequency = (float)pulses / duration * 1000;
    float speed_m_s = pulseFrequency * c / (2 * frequency);
    float speed_km_h = speed_m_s * 3.6;

    totalSpeed += speed_km_h;
  }

  float averageSpeed = totalSpeed / numMeasurements;

  Serial.print("Average speed: ");
  Serial.print(averageSpeed);
  Serial.println(" km/h");

  if (client.connected()) {
    String payload = String(averageSpeed);
    client.publish(mqttTopic, payload.c_str());
  }

  client.loop();

  unsigned long endTime = millis();
  unsigned long loopDuration = endTime - startTime;
  if (loopDuration < 1000) {
    delay(1000 - loopDuration);
  }
}

Debugging

If you want to check if everything works, you can look at the microcontroller’s console.

There, Wi-Fi and MQTT errors should be visible.

For even more detail, use an MQTT Explorer:

This lets you track exactly which data the ESP/Arduino sends to the MQTT server.


Home Assistant Integration

If you did not change the MQTT topic in the script, Home Assistant will automatically recognize the entity via MQTT Discovery.

After a few minutes, the entity "Speed Sensor" should appear in Home Assistant.


Fine Tuning

If the measurements are inaccurate or the sensor does not respond reliably:

  • Check the orientation
  • Adjust the sensitivity using the dial on the sensor

In my tests, I obtained usable results at distances of 3–5 meters.

I cannot guarantee absolute precision, but for my purposes, it is more than sufficient.