ESP8266 Bed Occupancy Sensor - martikainen87/Home-Automation GitHub Wiki

Inspiration: https://149walnut.com/2018-05-making-my-dumb-bed-smart-home-assistant-bed-occupancy-sensor/

DIY Sensor for monitoring if you are in bed or not

I was having a hard time getting a accurate reading if we are in our bed or not. I wanted to use the information to turn off any lights outside our bedroom if it was late at night, and arm the alarm for the house, check that all doors/windows are closed, turn off any sound notifications and alot of other fun stuff!

Came across this guide and it looked like it was exactly what I needed.

I did some small modifications to the whole package.

Components

I recommend testing everything on a breadboard before soldering the parts. If you dont feel like soldering you can use jumper cables but since this device will be close to your bed it's a risk that the cables might come lose.

A good way to test it is opening the "serial monitor" in Arduino IDE Tools -> Serial monitor, and you can also check your mqtt broker if it's populating it with info.

Schematics: schematics

Code to use in arduino IDE, (guide to flashing nodemcu in arduino )

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
int LEDbrightness;
WiFiClient espClient;
const char * ssid = "SSID";
const char * password = "wifipassword";
PubSubClient client(espClient);
const char * mqtt_topic_bed = "sensor/bed/left";
const char * mqtt_server = "broker.com";
const char * mqtt_username = "username"; // if you dont have your mqtt publised, just delete these rows
const char * mqtt_password = "password"; // if you dont have your mqtt publised, just delete these rows
void loop()
{
  // put your main code here, to run repeatedly:
  fsrReading = analogRead(fsrAnalogPin);
  Serial.print("Analog reading = ");
  Serial.println(fsrReading);
  if (!client.connected())
  {
    reconnect();
  }
  
  char buffer[10];
  dtostrf(fsrReading,0, 0, buffer);

  Serial.println(buffer);
  client.publish(mqtt_topic_bed, buffer);
  delay(1000);
}

void setup()
{
  Serial.begin(9600); // We'll send debugging information via the Serial monitor
  // setup WiFi
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  reconnect();
}

void reconnect()
{
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client", mqtt_username, mqtt_password))
    {
      Serial.println("connected");
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup_wifi()
{
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
}

Then you need to add the device in homeassistant,

mattias_status.yaml (Package file) You will need to edit the value_template with a correct value, 20 is good for testing if it works, place the sensor underneath the thin mattress (not the big wooden framework) and place yourself in the bed and watch the sensor value change, try different positions to make sure you dont set the value to high or to low.

homeassistant:
  customize:
    sensor.mattias_bed_sensor:
      friendly_name: Mattias Säng
      custom_ui_state_card: state-card-custom-ui
      hide_control: false
      show_last_changed: true

sensor:
  - platform: mqtt
    state_topic: 'sensor/bed/left'
    name: 'bed_left_value'

  - platform: template
    sensors:
      mattias_bed_sensor:
        value_template: "{% if states('sensor.bed_left_value')| float > 20 %}Upptagen{% else %}tom{% endif %}"
        icon_template: "{% if states('sensor.bed_left_value')| float > 20 %}mdi:hotel{% else %}mdi:bed-empty{% endif %}"