Hello World ;)
Here is my latest project, near completion: smart doorbell.
Old raspberry 1B connected to the regular RaspiCam V1 and a push button.
As soon as pushbutton is pressed :
- RPi sends an MQTT message to network
- Cam takes a picture and stores it to my local NAS
- Gateway sends a notification all my portable devices connected to openHAB
- RPi sends and email with picture attached
This post has 2 goals :
- share my findings. My house was until now still relying on openhab2 and mysensors binding. All working very happily. But there was no direct answer to get a raspberry to behave as a MySensor device and talk directly to the gateway. It was also time to move to openhab3. I was then advised to have a go at MQTT. So be it !!!
- get some help linux-wise for completion of this project.
I had to learn new tricks along the way... like mounting a network drive to the RPi. It's kind of linking/sharing a folder between my NAS and my RPi. If a file is dropped into that "mounted folder" it then appears on each device. Magic :+1:
To do so edit your fstab file
$ sudo nano /etc/fstab
and add something like :
//192.168.1.77/home/DoorBell /home/pi/doorbell/ cifs vers=2.0,username=username, password=passwordthatgoeswithit,credentials=/home/pi/doorbell .smbcred,noauto,x-systemd.automount 0 0
I have tested this setup both at work and at home. I had to remove "credentials" and everything after at home as i could never get it to work (got some errors). I also had to change from "vers=3.0" to "vers=2.0". Googling never got me a real explanation...
This is my python script, working as it should :
import os
import subprocess
from datetime import datetime
from gpiozero import Button
button = Button(17)
while True:
button.wait_for_press()
os.system('mosquitto_pub -h openhabian -t doorbell/50/1/0/0/1 -m "1"')
time = datetime.now()
filename = "capture-%04d%02d%02d-%02d%02d%02d.jpg" % (time.year, time.month, time.day, time.hour, time.minute, time.second)
subprocess.call("raspistill -t 500 -o %s" % filename, shell=True)
subprocess.call("echo '' | mutt -s 'Someone ringing !' -i messageBody.txt myemailaddress@someprovider.com -a %s" % filename, shell=True)
os.system('sudo /home/pi/movefile.sh')
os.system('mosquitto_pub -h openhabian -t doorbell/50/1/0/0/1 -m "0"')
The picture is stored with a convenient name (date and time) like so : capture-20210910-224322.jpg
mutt is some simple, cool and efficient mailing program.
messageBody.txt is a plain text file containing the body of the generic email.
movefile.sh is a script that moves the jpg file from RPi's SD card up to NAS shared folder once the email has been sent.
As i said previously it's all working as intended with
$ python doorbell.py
Next step is to get that python script to be launched at startup. A job for cron:
$ sudo nano launcher.sh
#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home
cd /
cd home/pi
sudo python doorbell.py
cd /
$ chmod 755 launcher.sh
$ sh launcher.sh
$ mkdir logs
$ sudo crontab -e
add this to file :
@reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1
$ sudo reboot
Quick check at logfile to make sure it all went well:
$ cat logs/cronlog
From then the email part of my script doesn't work anymore.
Trouble with rights ?
ps aux
returns that launcher.sh belongs to root but python script belongs to pi :
root 475 0.0 0.0 0 0 ? S 16:51 0:00 [cifsd]
root 500 0.0 0.6 7932 2300 ? Ss 16:51 0:00 /usr/sbin/cron -f
root 502 0.0 0.6 9452 2384 ? S 16:51 0:00 /usr/sbin/CRON -f
root 506 0.0 0.3 1924 1148 ? Ss 16:51 0:00 /bin/sh -c sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1
root 511 0.0 0.2 1924 1108 ? S 16:51 0:00 sh /home/pi/launcher.sh
root 513 1.5 2.5 34348 9728 ? Sl 16:51 4:25 python doorbell02.py
root 514 0.0 0.3 27640 1364 ? SLsl 16:51 0:00 /usr/sbin/rngd -r /dev/hwrng
root 525 0.0 0.4 4292 1588 tty1 Ss+ 16:51 0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root 527 0.0 0.4 6600 1852 ? Ss+ 16:51 0:00 /sbin/agetty -o -p -- \u --keep-baud 115200,38400,9600 ttyAMA0 vt220
root 757 0.0 1.6 12224 6228 ? Ss 16:53 0:00 sshd: pi [priv]
pi 760 0.0 1.8 14584 7072 ? Ss 16:53 0:00 /lib/systemd/systemd --user
Does it ring a bell to anyone ?
Thanks a lot for your help guys!