Here is a small project for social network connected home appliance, done for the sake of fun, but provides a good learning experience. I will be connecting my coffee machine to Twitter since I am one of the 6 people without a Facebook registration left on the planet. Connecting to Facebook is equally easy, just follow the instructions below.
Here is a summary of what we want to achieve:
- Identify if someone is using the coffee machine
- Tweet about it
What we need
- A Internet connected Raspberry Pi
- A 150K ohm resistor
- A LDR
- GMail account
For this to be a safe and non-invasive project, I opted to sense the coffee machine’s state by monitoring its status LED. Mine has a LED that reads “OK”, it lights up whenever it ready to make or in the process of making coffee:
Observing that LED will give us knowledge if it is being used or not. I am using a Light Dependent Resistor (LDR) as eyes for the Raspberry Pi. The LDRs change their resistance as per the intensity of light falling upon them. Our goal is to register the LED being lit by driving a GPIO pin on the Raspberry Pi LOW. Below is the schematic of my setup:
It is a voltage divider, with a weak pull up provided by a 150K resistor. The LDR has huge resistance when dark, so the GPIO pin will remain in HIGH state when the LED is dark. Upon lighting it up, the LDR’s resistance falls and the voltage divider tend to go LOW. We watch for that even in a python script on the Raspberry Pi and take action.
For the above schematic to work, we need to ensure that the LDR is in really shielded from ambient light. I cut a piece of pen and placed my LDR inside it, then painted the tube black to prevent outside light from falling on the LDR. The LDR was hot-glued in position, the leads go out in the back:
Now to the software side, we need a python script that will watch the state of GPIO pin 17 and take some action when the coffee machine brings it to ‘LOW’
and it should look like this:
import RPi.GPIO as GPIO import time import os #adjust for where your switch is connected buttonPin = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(buttonPin,GPIO.IN) while True: if (not GPIO.input(buttonPin)): #this is the script that will be called (as root) os.system("python /home/pi/sendnotify.py") #debounce time.sleep(720)
To have this run upon booting, we need to edit /etc/rc.local (as root since this is the owner).
sudo nano /etc/rc.local
At the bottom, just above
exit 0 we’ll add a call to our script.
The notification script that will be run is actually sending us an email:
with this content, replace the email account details:
#!/usr/bin/env python import smtplib from email.mime.text import MIMEText USERNAME = "**********@gmail.com" PASSWORD = "**********" MAILTO = "***********@gmail.com" msg = MIMEText('Martin is getting caffinated..again!') msg['Subject'] = 'Coffee alert!' msg['From'] = USERNAME msg['To'] = MAILTO server = smtplib.SMTP('smtp.gmail.com:587') server.ehlo_or_helo_if_needed() server.starttls() server.ehlo_or_helo_if_needed() server.login(USERNAME,PASSWORD) server.sendmail(USERNAME, MAILTO, msg.as_string()) server.quit()
I created a Gmail filter that would label incoming emails with subject ‘Coffee alert!’ with ‘coffee’ tag, needed for the next step.
To tweet about this notable event, I registered with the IFTTT wonderful service. I created a simple “recipe” that would watch my Gmail inbox for emails tagged ‘coffee’ and send a tweet:
With equal ease, you could make a Facebook post instead, to have your friends all jealous about you having coffee.
Page views: 5939