IOT Device #1/100 (ESP8266 Mailbox notifier) continued

I was having some mechanical problems with the previous version of the mailbox notifier where the pieces of mail would not sit flush against the sensor due to getting caught on the PCB box at the rear.

I solved this by building a false floor in the mailbox out of cardboard with the components glued onto the underside of the flooring. I would have been better to buy another mailbox, but I didn’t want to modify the property or the mailbox.

The current modifications are reversible. Plus its also waterproof.

The code is very simple. IFTTT was to slow – taking up to 1 hour to trigger an Android notification. The new version is near instantaneous by using  MQTT – but the main let down is the Android software with a lack of good MQTT software with reliable persistent connections and notifications.

My MQTT broker runs on my $5 Digital Ocean droplet running FreeBSD. For a WiFi connection I enabled a hidden guest network on my router that does not have access to the rest of my network.

The ESP8266 goes into deepSleep() for 60 seconds and wakes, upon wake it checks the A0 (analog) pin, if there is mail this triggers the WiFi to connect and a message to be sent to my MQTT broker.

Here is the code:

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// WiFi credentials
char ssid[] = "";
char pass[] = "";
// MQTT broker details
const char* server = "";
const char* username = "notabot";
const char* password = "";
// Change as required
const char* topic = "Home/mailbox";
const char* msg = "You Got Mail!";
byte QoS = 2;
boolean retain = false;
WiFiClient espClient;
PubSubClient mqttClient(espClient);
void youGotMail() {
Serial.println("You got mail!");
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqttClient.connect("espbot", username, password, topic, QoS, retain, msg)) {
Serial.println("connected");
// Once connected, publish an announcement...
if ( mqttClient.publish(topic, msg)) {
Serial.println("Successfully published.");
}
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
while (!Serial) {}
Serial.print("Reading sensor: ");
Serial.println(analogRead(A0));
if (analogRead(A0) > 500) {
// Connect to WiFi
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
if (WL_CONNECTED) {
Serial.println("Connected!");
}
pinMode(A0, INPUT);
Serial.print("Reading sensor: ");
Serial.println(analogRead(A0));
mqttClient.setServer(server, 1883);
// Run our notification logic
youGotMail();
mqttClient.disconnect();
// I found that not disconnecting caused issues with my router...
WiFi.disconnect(true);
}
// Sleep for 60 seconds
ESP.deepSleep(60*1000000);
}
// The void loop is never reached when using deepSleep()
// All code must be executed in the setup() function.
void loop() {
}

Parts list:

I only used a scissors, hot glue gun and soldering iron – so there is not a big tool requirement.

Limitations:

  1. Doesn’t count mail
  2. Annoys you by continuously reminding you that you have mail….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s