So as I went into day 2 of #Picademy I had decided upon a “motion activated camera which tweets the photo taken along with a random poem, giving you a LED countdown indicator” for my project.
Some of this I had previously coded (the random poem part) which you can see running in this Trinket app below:
My thoughts were that I could make use of this to create the text of my tweet adding the #Picademy hashtag to the end too.
I simplified this code down, and with the suggestion of Sarah, changed the “random.randint” part to select each verb etc., to a “random.choice”. This avoided the need to count up how many words were in each list.
So the tweet text part done, I moved onto the physical parts of the code. I wanted to have three LEDs (red, yellow and green) which would all light up when the program ran, and after motion was detected they would switch off in order with 1 second between each. I spent some time wiring up my LEDs to my Pi and my breadboard and then wrote a simple Python script to do the countdown sequence.
After debugging my code (I forgot to add my parenthesis to the end of the LED.on command!), I had this part done…now on to the camera!
We had written some Python code on day 1 which made use of the camera and gave us a on screen preview and then took a picture, saving it to the desktop of the Pi. I made use of this code with one minor adjustment. I imported the time function from the time library, which allowed me to add a date-stamp to my image file name so that I could keep them all rather than the script overwrite the image each time. I added this into my code and moved onto the next section; the motion sensor!
Now I had never used one of these devices before, so it required using a couple of new commands from the GPIOZero library; “MotionSensor” and “motion_detected”. I soon realised however that these motion sensors were very; well, sensitive! I had a quick chat with Dave who confirmed that this is always an issue with these devices, and helped me turn the sensitivity right down on the device and suggested that I construct a collar which would shield the device from all movement, except what was directly in-front of it. Having a working concept of my project (except the minor part of getting it to tweet!), I decided that I should get a bit “Blue Peter” and build something to hold it all together! A mix of drinking straws, masking tape, pipe cleaners and a silver takeaway box carton lid seemed to fit the bill! Here is what it looked like in all it’s glory!
So now to try and get this to send a tweet! Thankfully the Raspberry Pi Foundation have an amazing set of resources available on-line, including one about how to make a Tweeting Babbage. This resource stepped me through what was required to get it all working. There were two difficulties I ran into whilst try to get the tweeting section working:
- I had to manually install the Twython library for Python, as this is not available on the standard Jessie image. A quick:
sudo apt-get install python-setuptools sudo easy_install pip sudo pip install twython
resolved this though!
- I wanted to set up a new Twitter account to send these tweets on to, so as not to annoy those following my main account. However it transpired that you have to have a phone verified to your account before you can create a Twitter App; and Twitter just did not want to let me add my phone to my new account! So a quick tweet to let people know to expect some fairly weird tweets from me and we were good to go!
So a couple of tweaks and battles with the WiFi at the venue (I resorted to using my phone as a mobile hotspot, as thankfully I had a great 4G signal!) and I had some working tests:
At this point I walked away from the computer and drank some well deserved coffee!
As everyone finished off their projects, James introduced how we were going to present. I happened to be the only person standing at this point so had the honour(!) of being first to present. Thankfully it all went as planned and you can see my automated tweet below:
My code (which could be refined no doubt!) is here in this trinket… Don’t try to run it though; you will need it on a Pi with the LEDs and PIR on the same pins and also to set-up your Twitter app before this code does anything!
In conclusion; I would easily place the #Picademy training as the best CPD I have ever been on! If you ever get the chance; go! I hope to bring even more physical computing into my classroom; having now seen the power that this work can bring!