Tweeting coffee machine using Raspbery Pi

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
  • 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:

Tweeting coffee machine using Raspbery Pi

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:Schematic of Tweeting coffee machine using Raspbery Pi

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:LDR Tweeting coffee machine using Raspbery Pi

The coated LDR is hot-glued on top the status LED:Coated LDR Tweeting coffee machine using Raspbery Pi

Circuir Tweeting coffee machine using Raspbery Pi

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

while True:
    if (not GPIO.input(buttonPin)):
        #this is the script that will be called (as root)
        os.system("python /home/pi/")

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.

python /home/pi/

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 = "**********"
PASSWORD = "**********"
MAILTO  = "***********"

msg = MIMEText('Martin is getting caffinated..again!')
msg['Subject'] = 'Coffee alert!'
msg['From'] = USERNAME
msg['To'] = MAILTO

server = smtplib.SMTP('')
server.sendmail(USERNAME, MAILTO, msg.as_string())

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:

Source: Tweeting coffee machine using Pi

0/5 (0 Reviews)

About The Author

Hassan Zaka

I am an expert in accounting and possess diverse experience in technical writing. I have written for various industries on topics such as finance, business, and technology. My writing style is clear and simple, and I utilize infographics and diagrams to make my writing more engaging. I can be a valuable asset to any organization in need of technical writing services.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top