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.

Sunday, 30 August 2015

Particle Photon: Output data over the serial connection

Another useful piece of functionality that the CLI provides is the ability to listen and output data from the serial connection on the photon. This essentially allows you to do printlns.

To output some logging your program you need to initialise the serial connection in the setup:

void setup()
{
    Serial.begin(9600);
}
The 'Serial' object is the connection over USB to your computer. There are other Serial objects you can use to communicate over different channels.

then you are ready to output some logging:

...
Serial.println("Hello World!");
...

 In order to listen to the serial connection, use the following CLI command:
> sudo particle serial monitor
You should start seeing your messages output on the console.

Converting .ino file into .cpp

I found myself wanting to have a program folder made up solely of C++ files and the existence of the .ino file irked me for some reason (slightly OCD?)

After a bit of googling it was apparent that the .ino file only differed from regular C++ files in that a preprocessor would run on it before compilation to insert a header include.

So lo and behold a file that looks like this:
myApp.ino
-------
void setup()
{
   //do some setup
}
void loop()
{
   //do cool stuff here
}
-------
can be converted into:
main.cpp
-------
#include "application.h"
void setup()
{
   //do some setup
}
void loop()
{
   //do cool stuff here
}
-------

by simply renaming the file and adding the application.h include.

It is a little strange not to have a main method in this file but I assume the set up is similar to arduino where the main method is hidden away and you are only given the hooks into it by implementing setup() and loop()

Using the particle CLI on Ubuntu 14.04

I spent a bit of time playing with the cloud IDE and flashing some simple programs to the photon. I can't believe how easy it is to get up and running. The fact that you can flash your program onto the photon through the IDE without installing a million drivers and fighting with obscure errors is fantastic.

However, I'm not sure if it was my internet being lame or their servers being a bit laggy, but I sometimes found it would take over 10 seconds to compile my programs and would also time out when trying to flash my photon. I found this behaviour a little frustrating so invested a little time in getting the command line interface working so I could compile and flash programs without needing the cloud IDE. I believe the commands do connect to the particle servers so you still need internet for them to work.

So here we go:

1. Install the CLI


I followed the instructions here but found the spark-cli was deprecated and so at the end I installed the particle-cli instead.

swap

> sudo npm -g install spark-cli
for

> sudo npm -g install particle-cli


2. Compile your program


There are several ways you can compile your program into a binary file that can be flashed onto the photon. I chose the method of putting all the required files in a directory and running the following:

> cd <dir-with-program-files>
> particle compile p .

Note: the 'p' indicates you are compiling the program to run on a photon.

As an example, this was what my folder looked like:

- photon-app/
  |
  - myApp.ino
  - lib1.h
  - lib1.cpp

If your compilation was successful then a binary file will get created inside your directory. (It is named something like 'photon_firmware_1440934375581.bin'.

3. Put the photon in DFU mode

The photon needs to be put in Device Firmware Upgrade mode in order to accept a new program.
Perform the following steps:

  1. Ensure the photon is powered and the LED is pulsing cyan to indicate normal operation
  2. Press and hold the 'setup' and 'reset' buttons simultaneously on the photon 
  3. The LED should now be switched off
  4. Release just the 'reset' button and continue to hold the 'setup' button
  5. The LED will start flashing magenta and quickly change to yellow
  6. Once the LED is flashing yellow, release the 'setup' button
The photon is now in DFU mode and we are ready to flash our program onto it.


4. Flash your binary onto the photon


The command to flash a binary onto the photon is

> sudo particle flash --usb <binary-file>

e.g.
> sudo particle flash --usb photon_firmware_1440933735205.bin


Note: this needs to be done with sudo otherwise you get an error that looks like

> Error writing firmware...dfu-util: Cannot open DFU device 2b04:d006

If the flash was successful, the LED on the photon will return to its normal pulsing cyan colour.

I had to install a few bits and pieces to get this stage to work on my Ubuntu 14.04 laptop.

When I first ran the flash command it complained that dfu-util wasn't installed. After installing it with the standard 'sudo apt-get install dfu-util' I got an error along the lines of:

> 'invalid dfuse address: 0x08005000:leave'.

This led me to a helpful forum post which indicated I needed to install a later version of dfu-util as the official ubuntu repo only had version 0.5 and we need 0.7+.

So I downloaded version 0.8 from here (dfu-util_0.8.orig.tar.gz) and followed the instructions on the forum post to build it. In order to successfully run ./autogen.sh I needed to install 'autoconf'. And then in order to successfully run ./configure.sh I needed to install 'libusb-1.0.0-dev'.

After finishing up the dfu-util instructions I was able to successfully flash my program onto the photon. The only slightly annoying thing is that I get an error out of dfu-util despite the successful flash. From what I can tell from other people on the internet this is not unexpected. 

Here is an example output:

jenny@jenny-940X3G:~/code/sht71-photon$ sudo particle flash --usb photon_firmware_1440934375581.bin [sudo] password for jenny: Found DFU device 2b04:d006checking file  photon_firmware_1440934375581.binspawning dfu-util -d 2b04:d006 -a 0 -i 0 -s 0x080A0000:leave -D photon_firmware_1440934375581.bindfu-util 0.8
Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.Copyright 2010-2014 Tormod Volden and Stefan SchmidtThis program is Free Software and has ABSOLUTELY NO WARRANTYPlease report bugs to dfu-util@lists.gnumonks.org
dfu-util: Invalid DFU suffix signaturedfu-util: A valid DFU suffix will be required in a future dfu-util release!!!Opening DFU capable USB device...ID 2b04:d006Run-time device DFU version 011aClaiming USB DFU Interface...Setting Alternate Setting #0 ...Determining device status: state = dfuIDLE, status = 0dfuIDLE, continuingDFU mode device DFU version 011aDevice returned transfer size 4096DfuSe interface name: "Internal Flash   "Downloading to address = 0x080a0000, size = 7756Download [=========================] 100%         7756 bytesDownload done.File downloaded successfullydfu-util: Error during download get_status
Error writing firmware...dfu-util: Invalid DFU suffix signaturedfu-util: A valid DFU suffix will be required in a future dfu-util release!!!
dfu-util: Error during download get_status


Tuesday, 25 August 2015

Phat Photon Pins

So the pins on the photon are slightly too fat to fit in either of my standard sized breadboards. You can jam it in and the pins will stick but it will bounce out soon after if you touch it. I also found that it did not create a solid connection with the breadboard connectors so the components were not getting powered unless you wiggled the photon.

While developing the software and working out the circuits I really want to have the photon on a breadboard so I thought about how to modify the pin size. I have just had great success using shield extender headers. The photon with its chunky legs fit inside the header strips and they have slightly thinner legs which fit snugly in the breadboard and seems to provide a solid reliable connection.


You need a header strip with 12 pins. Unfortunately I couldn't find any at short notice so have improvised with smaller ones. You can pick them up on ebay for a couple of pounds.

Sunday, 23 August 2015

Particle photon battery pack

In order to allow my photon to be stand-alone and not require a power cable I bought the accompanying power shield. This is a lithium battery you can connect to your photon to give it it's own power source.

Here's a little guide to using  it:


In the pack you get a shield to attach your photon to, a battery and 2 header strips.


Plug the battery into the shield. It will only fit one way round.


Plug a powered USB cable into the shield. The red LED shows it is receiving power. The LED next to the battery connector will be blue if the battery is currently charging and will change to red when fully charged.


The photon is then placed over the power shield with the USB connector on the opposite end to the battery connector

 

If you plug the photon directly into the power shield then the photon board does not sit evenly and could potentially damage the components below. This is why we have the header strips.


Plug the header strips into the photon pins and then plug it into the power shield. You can now see the boards are cleanly separated.


However I found that the holes in the power shield are too large to provide connections with the header pins unless you push the photon to one side to make the legs slant into the holes. This means no power is provided to the photon. For this reason I will leave out the header strips while developing my project and solder the header strips to the power shield once I am happy with everything.

Move over Arduino Uno, Particle Photon is here!

I recently found myself wanting to create something to measure the temperature, humidity and light levels around a rather needy orchid I was growing to help me not kill it so quickly. This immediately made me think of my arduino uno. However it requires a separate shield to use wifi and is pretty large for what I wanted to do so I looked for alternatives. It was then I found the Particle photon.


This tiny component is usb powered, has an onboard wifi chip and a really cool ecosystem of software to help you interact with it (cloud IDE, mobile app, cloud storage etc.).

Any look how tiny!



Here is a guide on how to initialise your photon, connect it to your wifi network and make an LED flash using the mobile phone app.




First plug a powered USB cable into the power socket of the photon. Then open the particle app on your phone.

    

You won't have any devices listed yet so press the '+' to start setting up the photon. It may take a while for your device to appear in the list. Your board should be flashing blue to indicate it is in setup mode. If it is not then hold the 'setup' button for 3 seconds which should switch it to setup mode.

Once your photon has been discovered it will pop up in the list with a random name.

  

Click the photon and follow the wizard to connect it to your wifi network of choice. If all goes well you will end on the the setup complete screen.


Your photon will now be listed under your devices. If you click on the menu icon you can rename it to something more sensible. The first time you pair your photon with your account it may take a while for the status of your device to change to 'online' with a green circle. When I did mine the LED on the photon was flashing and changing colours for a few minutes before it settled down on a solid cyan light indicating it was finished initialising. I assume it was downloading updates. Any subsequent time you open the app (assuming your photon is powered and connected to the wifi) the status should appear as online almost immediately.

You can now enter 'Tinker' mode on the app where you are presented with the pin layout below. You can click on the pins to set a hi/low value to the output.  There is an LED on the board attached to pin D7, so set D7 to hi and you should see the LED light up on your board. Pretty cool!