Sunday 13 December 2015

Christmas project: part 2

Before building the circuit I thought it would be best to write the program to source the tweets and send them to the photon first. This way we can see exactly how much data to expect and what form it will take.

I wrote a little java program to do this. You can download it from github here along with the cpp photon program. The program searches twitter for Christmasy tweets within the London M25 (that's where I live, so obviously you can change this to an area that interests you). It polls every minute for tweets in 5 different locations, north, south, east, west and central London. 

The twitter search string is:

    ""christmas OR xmas OR "joyeux noel" OR weihnachten OR navidad OR "boze narodzenie"

Hopefully this will cover the majority of Christmas related tweets, including English, French, German, Spanish and Polish languages. Amusingly I originally only searched for 'noel' rather than 'joyeux noel' but there appeared to be a Noel Gallagher concert on recently so I got a lot of false positives. Goes to show you should inspect your results!


The photon needs to be running for the java program to run successfully as you get a HTTP 400 Bad Request from the particle server when your cloud function isn't available. But once the photon is running and the java program begins, you will see the tweet locations being logged to the serial monitor.


Now we have data, we can start making a circuit to visualise who is the most Christmasy.
Given that we have 5 possible sources of tweets, I imagine having 5 sets of LEDs each flashing at a rate/pattern that indicates the total count/frequency of tweets being received.

More on that soon...

Sunday 6 December 2015

Christmas project time!

In the run up to Christmas I fancy making something Christmassy!

My first thought was visualising Christmas related tweets through IFTTT. After linking my photon to IFTTT and playing around with a few recipes it was clear that the triggers were just not reliable enough and the correct tweets were not coming through.

So let's create something from scratch.

First we need a mechanism for a notification to be sent to the photon to trigger the visualisation. This is done by exporting a function to the particle cloud that will give us a REST API endpoint to poke.

Create xmas.cpp

  
   #include "application.h"  
   
   #define ledPin D7  
   
   int handleTweet(String message);  
   
   void setup()  
   {  
    Particle.function("tweet", handleTweet);  
    pinMode(ledPin, OUTPUT);  
   }  
   
   void loop()  
   {  
    delay(1000);  
    digitalWrite(ledPin, LOW);  
    delay(1000);  
   }  
   
   int handleTweet(String message)  
   {  
    digitalWrite(ledPin, HIGH);  
    Serial.print("Received tweet: ");  
    Serial.print(message);  
    Serial.println();  
   
    return 0;  
   }  

This program will export a function called 'tweet' which takes in a String (max length 63 characters) and pull the D7 pin high when it is called. This is the pin with the blue LED next to it on the board so we can easily see it is working without constructing a circuit. The function also writes a log line to the serial output so we can see what the message is.

   $ particle compile p  
   $ sudo particle flash --usb photon_firmware_xxx.bin  
   $ sudo particle serial monitor  

Now we are connected to the photon and listening for log messages.

When you export a cloud function a REST endpoint is created in the format:

   https://api.particle.io/v1/devices/<device ID>/<function name>

So to poke the function exported for my particular photon you can use curl:

   $ curl https://api.particle.io/v1/devices/12341234/tweet -d access_token=12341234 -d "args=hello world"  

You can find your device ID and access tokens with the following commands:

 $ particle list  
 $ particle token list  

The 'user' access token is the one you want to use here. You can also find this token on the particle cloud IDE under 'Settings'.

Once you submit the curl command you instantly see "Received tweet: hello world" written to the serial log. Pretty cool!



Next post we'll hook up a circuit to visualise the data received by the photon.