blog

  • Home
  • blog
  • Home-Made Twitter and Gmail Notifications with PHP and Arduino

Home-Made Twitter and Gmail Notifications with PHP and Arduino

I am a little obsessed with Twitter. It’s how I communicate with most of the developers I’m not fortunate to live in the same city as. I’m a little less obsessed with IoT projects, but then it’s harder to find the time to work on them than it is to check Twitter.

Unless I can do both at the same time.

Ever since the IoT week, I’ve been meaning to work on a project that will let me know when someone wants to speak to me. Something that looks cool, and is at the same time non-invasive. This is what I’ve come up with…

Arduino logo

Most of this code can be found on Github. I’ve tested it using PHP 7.1.

In this post I talk a lot about pins. I use this word to mean the pins coming out of components, as well as the Arduino sockets these pins go into. They’re often called terminals or ports. When I talk about pins, I just mean a place you can connect components or wires to. I’m quite specific each time though, so you’ll be fine!

The Project

The project we’re going to work on is a simple notification system, connecting a single RGB LED, a proximity sensor, and a PHP script to gather notifications. As notifications are received, the RGB LED will cycle through them, fading the LED to the appropriate color for each notification type.

Say someone has mentioned us on Twitter. The LED should periodically cycle to the Twitter blue. Then, if someone sends us an email, the LED should cycle between the Twitter blue and the GMail red. We could extend this to the Fitbit green, the Facebook blue and so on.

If we’ve seen the Twitter notification, we should be able to wave our hand in front of the proximity sensor, and Twitter blue should be removed from the rotation, leaving GMail red etc.

The Hardware

We can use just about any RGB LED. As long as we can control the color it fades to, we can simulate the social network notifications we’re after. Really, just about any common anode RGB LED. If you get one that doesn’t include resistors, be sure to add those into your circuit. I’ll touch on that later…

The more IoT work you do, the more likely you are to run into the terms anode and cathode. A common anode RGB LED is one that connects one pin to the positive pin on your micro-controller or battery, and three pins to ground, depending on the combination of colors you’re after.

Connecting the red pin to ground will close the circuit so that current flows through the red portion of the LED. Connecting the red pin to a Pulse Width Modulation (or PWM) port on our Arduino, and grounding the port by half will reduce the amount of current flowing through the red portion of the LED.

Grounding the various pins by varying amounts will lead to many different colors – as many as you can define as CSS colors.

Next, we need some kind of proximity sensor. The easiest (in my opinion) is an infrared transceiver. You could also use an ultrasonic transceiver, but they’re better at mid-range (as they have a minimum sensing distance, so you’ll have to be further away from the sensor).

Finally, we need an Arduino. You can use another micro controller, but this post will refer specifically to Arduino, because I have three within arms-reach, and nothing else to test on.

Connecting Things Together

Connecting the circuit

This is a relatively simple circuit, as far as they go. I’m by no means an expert, but I did manage to find the data sheets for the infrared sensor and RGB LED I bought; so connecting them up wasn’t too troubling.

The important bits are that our LED pins are connected to PWN ports, so that we can gradually adjust the amounts of red, green, and blue, and that the sensor pin is connected to an analog port (in this case A0).

You may be wondering why we don’t connect the LEG pins to analog ports, or why we don’t connect the sensor to a PWM port. PWM is designed to simulate degrees of on/off, but the way that is done is by turning something on for a fraction of a second.

With the right frequency of on/off cycles, LEDs only appear to be partially bright. That’s a side-effect of our eyesight, in much the same way as thirty static images played in quick succession can appear to represent motion video.

We need the sensor connected to the analog port because we really do need a gradual measurement, and A0 will give us that.

My RGB LED is fine with 3.3v and 200mA of current (before the resistors). So I can connect that to the 3.3v pin, and leave the 5v pin for the sensor’s power supply.

My sensor also has a pin to enable/disable the sensor readings. I’ll code for this, but keep in mind that your sensor might also have this. If it does, connect it to any unused output pin (like 08 or 1213) and make sure you set that pin to high.

We also need to connect the Arduino USB port to an open USB port on development machine.

The Software

Now let’s look at the software we’ll use to control things. Before we start telling the Arduino what to do, we should define some services:

namespace Notifier;

interface Service
{
    /**
     * Queries the service to trigger notification
     * alerts. Returns true if there are new
     * notifications to show.
     *
     * @return bool
     */
    public function query();

    /**
     * Marks the most recent notifications as seen.
     */
    public function dismiss();

    /**
     * Returns the name of this service.
     *
     * @return string
     */
    public function name();

    /**
     * Returns an array of pin color values,
     * for a common-anode RGB LED.
     *
     * @return int[]
     */
    public function colors();
}

This is from src/Service.php

Each service we connect to needs to have a way for us to query for new notifications. Once we dismiss a notification type, we’ll also need to let the related service know.

Continue reading %Home-Made Twitter and Gmail Notifications with PHP and Arduino%

LEAVE A REPLY