Sei sulla pagina 1di 5

OpenHAB Version Update: Letter

Box Alert System


Programming with ESP8266
This lecture includes steps to program the PIR sensor with the ESP-01
Wi-Fi module to create a DIY letter box alert system. It also elaborates
on interfacing the hardware to openHAB v2. You need not go through
the upcoming videos in this section because they pertain to an older
version of openHAB (v1).

In this lecture, a PIR sensor is used to detect the inflow of letters in


a letterbox and update the status of the letterbox The motion sensor
output along with the count of letters is hosted in a webserver. This is
then accessed by the openHAB server.

Programming with ESP-01

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#define MOTION 2
#define ssid "makerdemy1"
#define password "*******"
int SenSor, LaststateofSenSor = 0, letter_count = 0;
ESP8266WebServer server(80);

void setup(){
Serial.begin(9600);
delay(10);
WiFi.begin(ssid, password);
IPAddress ip(192, 168, 1, 9); //enter the ip address of ESP-01
IPAddress gateway(192, 168, 0, 1);// use ipconfig in command prompt to
get these values
IPAddress subnet(255, 255, 255, 0);
WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to wifi with IP ");
Serial.println(WiFi.localIP());
server.on("/", handle_root);
server.on("/letterbox_status", [](){
ReadSensor();
server.send(200, "text/plain", String(SenSor));
});
server.on("/letter_count", [](){
ReadSensor();
server.send(200, "text/plain", String(letter_count));
});
server.begin();
Serial.println("HTTP server started");
}
void handle_root(){
server.send(200, "text/plain", "ESP8266 is serving data
@http://192.168.1.9/letterbox_status or http://192.168.1.9/letter_count");
delay(100);
}
void loop(){
server.handleClient();
}
void ReadSensor() {
pinMode(MOTION, INPUT);
SenSor = digitalRead(MOTION);
if (SenSor != LaststateofSenSor)
{
if (SenSor == 1)
{
letter_count = letter_count + 1;
}
}
LaststateofSenSor = SenSor;
delay(10);
}
● Upload the program in ESP-01. Open serial monitor and press
reset. You’ll get the following output.
● Go to your web browser and type the local host IP address, in
this case it’s 192.168.1.9. Once your host server is started, the
values of status of the Motion sensor and the Count of letters
will be stored in separate web pages like
192.168.1.9/letterbox_status or 192.168.1.9/letter_count.
● Once movement is detected, the status will go to high value (1)
and the count will increase accordingly. The status value will
come back to low (0) if there is no activity.
● The output from the web pages will look like,

● Once the sensor detects a letter, the status goes HIGH (1) as
shown in the picture above.
● The values from the web page are sent to the openhab client
server. Before those values can be read, an interface with the
sensor items, http binding and rules to get the values from the
http server must be created.

Interfacing with openHAB


Configuration and Interfacing in openHAB

● Open Visual Studio Code and add the network drive created
(openhab-conf) into the programming section.
● Create a new file in the items folder, say pir.items and add the
following code.
Group gGroundFloor (All)
Group GF_Living "Living Room" <video> (gGroundFloor)
Number Motion "Motion Sensor Output [%d]"(GF_Living)
Number LetterCount "No. of Letters [%d]"(GF_Living)
● Create a new file in the sitemaps folder, say pir.sitemap and
add the following code.
sitemap default label="Home Automation"
{
Frame label="Letter Box"
{
Text item=Motion
Text item=LetterCount
}
}
● Create a new file in rules folder, say pir.rules and add the
following code.
rule "Store Motion Sensor Output and No.of Letters"
when
System started or
Time cron "* * * * * ?"
then
var result4 = sendHttpGetRequest("http://192.168.1.9/letterbox_status", 5000)
var result5 = sendHttpGetRequest("http://192.168.1.9/letter_count", 5000)
Motion.postUpdate(result3)
LetterCount.postUpdate(result4)
end
● The url given in the bracket will be same as the web
servercreated by the ESP-01. Adding a delay will make sure
there is no traffic or packet loss during the http get request.
● With the mandatory files configured in VS Code, the readings
from the sensor can be accessed in the openhab basic UI. To
do this, open openhabianpi:8080 in your web browser.
● Initially, to check if the required bindings are added, the paper
UI must be configured.
● Open Paper UI, click on add-ons, then bindings. Check if the
HTTP Binding is installed, if not install it.
● Next, to view the sitemap that you created, the demo
sitemapthat opens by default must be disabled. Click on
Configurations and select services. Go to the UI tab and click
on configure in the Basic UI option. Type the name of your
sitemap, in this case “pir” is the default sitemap option.
● Now type openhabianpi:8080 and select basic UI. It will preview
the sitemap created with the values acquired from the webserver.
● Pressing the reset button on the ESP-01 will erase the values in
the webserver and a new entry of letters can be monitored.

The final home automation system with the temperature humidity feed
and the letter box system will look like,

You've successfully completed creating a home automation system


using ESP8266, openHAB2 and Raspberry-Pi 3. You don't have to look
through the next videos in this section as they pertain to openHAB v1.

Potrebbero piacerti anche