Tuesday, September 27, 2016

Adafruit Circuit Playground Second Hour

If you have followed last week's post on Adafruit's Circuit Playground, you now have a working communication between your computer and the board. In this example, I would like to establish the communication between my Raspberry Pi over the serial communication to the board. This way I can leverage everything the RasPi has while utilizing the sensors and outputs from the board. 

The package I need is PySerial, which can be installed via 'sudo pip install pyserial'. It is used as the 'serial' package in Python code. 

Here is the objective: 
  1. The Arduino code will print out serial output when either the right or left button is pressed.
  2. When the B key is pressed, the LED light will light up one at a time. 
  3. In the Python code we will send the letter B 9 times then wait for any serial input

Here is the Python code. Python Code. A few notes: 

- Raspberry Pi the serial port is /dev/ttyACM0 and /dev/cu.usbmodem142 on Mac, therefore I comment out the line depending on if I am on the Mac or Raspberry Pi. 
- The character is sent as Bytecode, so chr(0x42) is used for letter 'B'. Here is an ASCII table. 

- Timeout of 1 second is used to gradually light up the LED. 

import serial
import time

#ser = serial.Serial('/dev/ttyACM0', 9600)
ser = serial.Serial('/dev/cu.usbmodem1421', 9600)

# ASCII character 'B'
sendChr = chr(0x42)

# Send character B which turns on light one at a time
for i in range(9):

# Read the input from board
while True:
    message = ser.readline()

Here is the Arduino Code: 

- I am using the Demo code provided by Adafruit:

- Use the library manager to install the Adafruit Circuit Playground Libraries

// Demo program for testing library and board - flip the switch to turn on/off buzzer

#include <Adafruit_CircuitPlayground.h>
#include <Wire.h>
#include <SPI.h>

// we light one pixel at a time, this is our counter
uint8_t pixeln = 0;

byte byteRead;

void setup() {
  //while (!Serial);
  Serial.println("Circuit Playground test!");


void loop() {

  /************* TEST BOTH BUTTONS */
  if (CircuitPlayground.leftButton()) {
    Serial.println("Left button pressed!");
  if (CircuitPlayground.rightButton()) {
    Serial.println("Right button pressed!");

  /************** Read Serial In, 65 DEC ASCII is letter B */
  byteRead = Serial.read();
  if(byteRead == 66) {
    Serial.println("Letter B input, turn on light");
    CircuitPlayground.setPixelColor(pixeln++, CircuitPlayground.colorWheel(25 * pixeln));
    if (pixeln == 11) {
    pixeln = 0;

Another screenshot for the IDE: 

You can also use the serial monitor included with the Arduino IDE to test: 

This is really fun, I cant wait to see all the possibilities when combining the two! 

Tuesday, September 20, 2016

Adafruit Circuit Playground First Hour

The Adafruit Circuit Playground arrive a few days ago. I remember seeing it for the first time at PyCon 2016 when Tony D. had a demo board and I was really impressed by it. Imagine a board not much bigger than a regular US quarter PACKED with sensors.

Here are two quick demos with playing some sound on the board:

Source: https://www.adafruit.com/

  • 10 x mini NeoPixels, each one can display any rainbow color
  • 1 x Motion sensor (LIS3DH triple-axis accelerometer with tap detection, free-fall detection)
  • 1 x Temperature sensor (thermistor)
  • 1 x Light sensor (phototransistor)
  • 1 x Sound sensor (MEMS microphone)
  • 1 x Mini speaker (magnetic buzzer)
  • 2 x Push buttons, left and right
  • 1 x Slide switch
  • 8 x alligator-clip friendly input/output pins
    Includes I2C, UART, and 4 pins that can do analog inputs/PWM output
  • All 8 pads can act as capacitive touch inputs
  • Green "ON" LED so you know its powered
  • Red "#13" LED for basic blinking
  • Reset button

No soldering required, just plug it in, setup and off you go with your prototype!

I just had some time to play with it enough to set up the environment and get a few program going. Below are some useful links and screenshots. I was able to go thru the two sound tutorials:

1. Get the latest Arduino software.
2. Here is the Adafruit Getting Started with Circuit Playground instructions.
3. Circuit Playground Intro
4. Make sure you can see the serial port:

MacBook-Air:~ echou$ ls /dev/cu*
/dev/cu.Bluetooth-Incoming-Port    /dev/cu.usbmodem1411

MacBook-Air:~ echou$ 

5. Setup screenshots:

6. I was able to get to these three tutorials:

- Playground Speaker

- Circuit Playground Library:

- The sound of music:

The only thing that I found for the sound of music tutorial is that extra library is needed besides pitches.h: 

I am really looking forward to tinker with it more, the idea really is to integrate this with Raspberry Pi and Python to make some interesting project! 

Happy Coding! 

Tuesday, September 13, 2016

AWS Internet of Things Button

I received my AWS IoT Button a few days ago. This is a programmable button based on the Amazon Dash Button, you know, those little buttons that allows you to order stuff from Amazon by just pressing on it. What is different about this button is the programmability and getting you introduced to the AWS IoT platform with sample code and wizard workflow.

The 'Getting Started' on this page is a good introduction, since the purpose of the button is to allow you to experience the end user experience when they received your future IoT device, I dont repeat or rob you of the experience here. What I want to show would be the sample output of each of the step for me so you can compare with your own if you run into something different.

The whole process took about 1 hour for me, it would have been quicker if I had not deviate from the wizard of trying to see if there were Python Lambda code or if if I had not change the IoT permission in the Wizard.

Here are some picture of the steps:

1. Picture of the IoT button when it arrived:

2. Overall workflow of what we will go thru

3. Choosing the IoT button from the AWS IoT portal

4. Configure the IoT trigger to go to Lambda

5. The page will automatically generate the cert and private key, download them and set up the button by following the instruction on the page

6. Lambda function is now complete

7. You can now press the button, which will blink blue while operating and green when it is done. 

8. Here are some screenshots of the email sent, subscription confirmation page, and following emails. Notice in the subject line there is also the type of button push, single or double click.

This was really fun. I am thinking of other use. The only thing is the sample code is in Node.js, so I need to spend some time looking it over before I can start think of other use cases. At some point I should convert that to Python. :)

Happy coding!

Tuesday, September 6, 2016

Make a Twitter Bot in Python and AWS Lambda

I followed this excellent tutorial from Joel Grus on how to make a Twitter Bot. If you have followed some of my post before, Project Mining Social Web 2nd Edition or Python with JSon, you know I have had a little bit of experience using the Twitter API.

Here is a screenshot of the Twitter Bot:

What is different this time, is that I wish to use AWS Lambda as the backend. I want to know what is required and what the workflow was. I wont repeat the steps Joel listed out in his tutorial, just some of the things I found out:

1. Seems that AWS Lambda wants the main file to be lambda_function.py and the main function to be lambda_handler(event, context).

2. The scheduling trigger is now "CloudWatch Events - Schedule" instead of just schedule.

3. I learned this from a friend that to always immediately put the credential file in .gitignore to avoid accidentally checking it in on GitHub.

echou$ cat .gitignore

I am now the proud owner of a Twitter Bot that searches for Cisco layoff news and retweet them. On this date of Aug. 19, 2016 that was the big news of the week.



Here is what my code ended up as:

from __future__ import print_function
from twython import Twython
from twython.exceptions import TwythonError
import re
import json, pprint
with open('credentials.json') as f:
  credentials = json.loads(f.read())
client = Twython(credentials["consumer_key"],
query = "cisco layoffs -filters:retweets"
rgx = r"cisco"
def lambda_handler(event, context):
    results = client.search(q=query)
    for tweet in results["statuses"]:
        text = tweet["text"]
        # re.search matches anywhere in the string; re.I means case-insensitive
        if re.search(rgx, text, re.I):
            # client.retweet will raise an error if we try to retweet a tweet
            # that we've already retweeted. to avoid having to keep track, we
            # just use a try/except block
            except TwythonError as e:

results = client.search(q=query)

Happy Coding!

Tuesday, August 23, 2016

Curlify Your Python Requests Objects

I love things that are simple and useful. Egor's Curlify Python module is one of these gems I saw this morning.

I already have a Python script that demonstrate A10 Network's AXAPI using the Requests package:


With this package, I changed exactly one line of code wherever I wanted to give an curl example of the same operation.

For example, after importing the curlify file, instead of printing out r.content in line 23, I just print out curlify.to_curl(r.reqeust) and boom, I have a curl command I can pass to people! How cool is that?

7 import requests, json, curlify
 17 # Acquire athorization token
 18 auth_headers = {'content-type': 'application/json'}
 19 auth_payload = {"credentials": {"username": "admin", "password": "a10"}}
 20 auth_endpoint = '/axapi/v3/auth'
 21 url = base_url + auth_endpoint
 22 r = requests.post(url, data=json.dumps(auth_payload), headers=auth_headers, verify=Fa    lse)
 23 print(curlify.to_curl(r.request))

Of course, you get the most out of it if you use Requests already (who isnt?) and you can just write it in Requests. When somebody else wants a simpler operation, say using just curl, that you can just easily dish it out to them in less than 5 minutes.

This is my final output from the same Python file above:

The only different from the plain output is I added a '-k' to the curl operation because I have a self-signed certificate on the A10 Thunder TPS.

Nice tool, nice output, no sweat. My type of thing. :)

Monday, August 15, 2016

How-To Enable Bash on Windows 10 in 5 Steps

I don't know about you, but it really feels like a new day in technology when I see a bash shell running naively on Windows!

Windows 10 Anniversary Update has been out for almost 2 weeks now (update 1607), and by now it is relatively stable. Any major flaw should have been discovered by now, note that it is still update-at-your-own-risk kind of a thing, but I have decided to go for it since I just want to see Bash shell running on Windows natively.

Here are the steps I took (from PCWorld article):

1. Check for Windows Update. If the updater does not automatically prompt for update 1607, click on 'Learn More' on the same page and follow the link to get the executable for the update. That is what I had to do:

2. Enable Developer Mode in Settings:

4. In control panel, enable Windows Bash Shell feature:

5. Reboot, search for Bash and it will prompt you for the rest of the installation. Of course the first hing I do is to test out Python2 and Python3.

Leave me a comment or question below, let me know what you think! Happy Coding!

Wednesday, August 10, 2016

How to Mine Twitter in Under One Hour

There are a number of books discussing using Python to mine the social web. After trying out Facebook, LinkedIn, and Twitter API's, it is my believe that Twitter provides the most friendly workflow for anybody to get started. For the past two days I have been working on a project where mining the Twitter feed was a part of the overall process. Here I want to briefly share some of the steps I took using Python. I believe the whole process shouldn't take you more than 1 hour.

1. Determine the Twitter module to use.

There were a number of Python Twitter modules, a simple search turns up two popular modules: Python-Twitter and Tweepy. Since I had the chance to listen to Elizabeth Uselton's PyCon 2016 Talk a few weeks back at a local Python meetup, I decided to use the same library, which was python-twitter.

[Update 9/5/2016] Make sure you use 'python-twitter' package, i.e. pip install python-twitter. Not pip install twitter.

2. Register a developer account at https://dev.twitter.com/ for documentation and create an app via https://apps.twitter.com/ to get all the keys for your program.

3. Follow the instruction here to pip install the module and give it a spin. Below is an example using the Python REPL (off my Raspberry Pi 3, mind you :)).

pi@raspberrypi:~/Alexa $ python
Python 2.7.9 (default, Mar  8 2015, 00:52:26)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import twitter, pprint, json
>>> api = twitter.Api(consumer_key="<you get this from the twitter app>",
...                   consumer_secret="<you get this from the twitter app>",
...                   access_token_key="<you get this from the twitter app>",
...                   access_token_secret="<you get this from the twitter app>")
>>> print(api.VerifyCredentials())
{"created_at": "Thu Dec 18 17:35:54 +0000 2008", "description": "Network Automation Nerds", "favourites_count": 16, "followers_count": 58, "friends_count": 225, "name": "ericchou", "profile_background_color": <skip>}

4. Cool, let's do some search. Since the Olympics is going on, I bet that is a hot topic:

>>> searchResult = api.GetSearch(term="Olympics", lang='en', result_type='recent', count=5, max_id='')
>>> pprint.pprint(searchResult)
[Status(ID=763393850215960577, ScreenName=Brezshun, Created=Wed Aug 10 15:17:31 +0000 2016, Text=u'RT @DragonflyJonez: Even Hitler sent Jesse Owens a nondescript, mass mailing thank you card for participating in the Olympics. FDR never ev\u2026'),
 Status(ID=763393849964294144, ScreenName=JeremyMcDoniell, Created=Wed Aug 10 15:17:31 +0000 2016, Text=u'RT @br_uk: Michael Phelps has won more men\u2019s swimming #Gold medals than all but two countries in the history of the #Olympics \U0001f64c https://t.c\u2026'),
 Status(ID=763393849930584065, ScreenName=alyajaafar, Created=Wed Aug 10 15:17:31 +0000 2016, Text=u'RT @BBAnimaIVids: Kitten Summer Olympics \U0001f63a https://t.co/EGUWnOrGsX'),
 Status(ID=763393849536372736, ScreenName=ilhamfachrul, Created=Wed Aug 10 15:17:31 +0000 2016, Text=u'RT @BBCSport: Watch live @BBCOne (UK only) and the @BBCSport website.\n\nhttps://t.co/b87rfBg0qt #RioOlympics2016  https://t.co/767BAdAmza'),
 Status(ID=763393848747888640, ScreenName=JustLandlords, Created=Wed Aug 10 15:17:31 +0000 2016, Text=u'RT @NewsLandlords: Have the London Olympics had a long-lasting effect on the property market? https://t.co/QolbtKou6a #London #property htt\u2026')]

5. There you have it, quick and simple. Here is the official API Doc from Twitter.

Happy Coding!