You thought about it, do IT !

Smart PostBox

Smart PostBox

To avoid opening the postbox everyday after work, had the idea to connect it to my home assistant.

For that we need to setup a reed switch and some electronics on the postbox. It will be connected to the domotic network of the house using wifi and MQTT protocol.

Parts needed :

1 Wemos D1 Pro

1 solid state relay board

1 normally opened reed switch

1 18650 battery and support

1 DC to DC3.3V regulator

Some dupont wires

Electronic sketch :

Wemos D1 mini Pro software :

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

#define MQTT_HOSTNAME "POST_BOX"
#define MQTT_BROKER_IP "your_broker_IP"
#define MQTT_USERNAME "your_broker_USERNAME"
#define MQTT_PASSWORD "your_broker_PASSWORD"
#define OPTOCOUPLER_GPIO D5

//Mesure le temps d'execution
long int execution_time = millis();

//Necessaire pour la lecture de la tension
extern "C" {
  uint16 readvdd33(void);
}

//Variables nécessaires au MQTT
const char* mqtt_server = MQTT_BROKER_IP;
#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];
WiFiClient client;
PubSubClient MQTTclient(client);

// Replace with your SSID and Password
const char* ssid     = "your_WIFI_SSID";
const char* password = "your_WIFI_KEY";

//Mosfet gate sur D1 qui utilise la batterie même quand off
const int GPIO = OPTOCOUPLER_GPIO;

void setup() {

  //Active l'alim parallele
  pinMode(GPIO, OUTPUT);
  digitalWrite(GPIO, HIGH);
  
  Serial.begin(115200); 

  initWifi();
  
  //Initialisation MQTT
  MQTTclient.setServer(mqtt_server, 1883);

}

void loop() {

  //Publication
  MQTT_Publish();

  //Temps d'execution
  Serial.print("TIME - Temps d'execution : ");
  Serial.println(execution_time);
   
  //Deco Wifi
  wifi_station_disconnect();
  wifi_set_opmode_current(NULL_MODE);

  //Coupe son alim parallele comme l'execution est terminee
  digitalWrite(GPIO, LOW);

  //Veille profonde si terminé avant fermeture boite aux lettres
  ESP.deepSleep(0);

}

// Establish a Wi-Fi connection with your router
void initWifi() {
  
  //Connection au Wi-Fi
  Serial.println("WIFI - Initialisation du WIFI de ESP8266");
  Serial.print("WIFI - Connection à ");
  Serial.println(ssid);
  WiFi.hostname(MQTT_HOSTNAME);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(10);
  }
  Serial.print("WIFI - Connection établie !");  
  Serial.print("WIFI - Adresse IP : ");
  Serial.println(WiFi.localIP()); 

}

//Fonction qui publie les données sur le broker MQTT
void MQTT_Publish(){
  
  //Debug
  Serial.println("MQTT - Publication des valeurs");

  //Recupère la tension actuelle d'alimentation pour envoyer l'info en MQTT
  float tension_alim = readvdd33() / 825.00;
  
  //Reconnecte si plus connecté
  if (!MQTTclient.connected()) {
    Serial.println("MQTT - Tentative de connexion...");
    MQTTclient.connect(MQTT_HOSTNAME,MQTT_USERNAME, MQTT_PASSWORD);
  }
  
  //Tente de se connecter
  if (MQTTclient.connected()) {
    Serial.println("MQTT - Connecte");
    
    //Si connecté publie les topic
    MQTTclient.publish("POST_BOX/POWER_voltage",String(tension_alim).c_str(),true);
    delay(50);
    MQTTclient.publish("POST_BOX/BOX_status",String("Courrier reçu").c_str(),true);
    delay(50);

  } else {
    Serial.print("MQTT - Echec, rc=");
    Serial.print(MQTTclient.state());
  }

}

You must make an ip address reservation on your router for the Weemos D1.

Home Assistant part :

in configuration.yaml you must add the following lines

mqtt:
  - sensor:
      name: "Boite aux lettres - Etat"
      state_topic: "POST_BOX/BOX_status"
      icon: "mdi:mailbox"

You have to make an automation to reset POSTBOX status each night :

On the dashboard add the following card :

type: entities
entities:
  - sensor.boite_aux_lettres_etat
  - sensor.boite_aux_lettres_tension_d_alimentation
title: Boite aux lettres

Add another card for manual reset button, with your automation entity id in the target :

show_name: true
show_icon: true
type: button
tap_action:
  action: call-service
  service: automation.trigger
  service_data: {}
  target:
    entity_id: automation.boite_aux_lettres_passe_a_vide_a_minuit
icon: mdi:mailbox-open-outline
name: RAZ Boite aux lettres

Now make the last automation to notify when postbox is full :

Now you will have a Home Assistant notification on your selected mobile device when someone put some mail in the postbox !

do-it.dev Avatar
if you liked it, please contribute !

Leave a Reply

Aurélien Bordinat

I am a french computer engineer, adept of DIY, home automation and new technologies. When i’m not busy trail running, i spend some time prototyping.