Let's Get Some Things Connected! (for any questions, get in touch with Bryan Morgan at bryan_morgan@ischool.berkeley.edu) First Things First, What You'll Need: - Raspberry Pi - Mini-USB Cable - Computer - Resistor - Breadboard - Jumper cables - LEDs - Ethernet Cable - PubNub Account Things to Take Care of before class: - Sign up for a PubNub Developer account: https://admin.pubnub.com/#/register - Review the docs on using the PubNub Python SDK: https://www.pubnub.com/docs/python/pubnub-python-sdk-v4 - Please pay special attention to the publish/subscribe section: https://www.pubnub.com/docs/python/data-streams-publish-and-subscribe-sdk-v4 Setting Things Up! You should have python installed on your computer already if you are using a Mac, if you are using Windows and don't have Python installed please visit the following download page (https://www.python.org/downloads/windows/). Now that everyone is on the same page, lets get your environment set up! Setting Up The Environment! First thing is you'll probably want pip installed if you don't already have it. You can get the python script for installing here: https://pip.pypa.io/en/stable/installing/ Once you have that script, open up your terminal and navigate to where you save it. (If you saved it to your downloads folder it should be something like: ~/Users//Downloads on a Mac). Once there, run the pip install script by typing python get-pip.py. You should see it install pip with a little progress bar. One note, if you get an OSError along the lines of "Permission denied", rerun the install script with the sudo command infront (sudo python get-pip.py). Next you'll need to install virtual environment. This super handy tool allows you to set up unique environments for each project, which helps you manage different library dependencies. Once pip is installed, you should be able to simply type pip install virtualenv. Virtual environment will begin installing globally! Next order of buisness! Setting Up Your Working Area. In your terminal, navigate to a directory where you plan on working for this class. I would suggest something along the lines of "~/Users//development/iot_class". Once you have made the directory, go in there and create your first virtual environment. To do this, simply type virtualenv venv. This will go through the process of installing a new virtual environment in the directory "venv". Once that is done, you can type . venv/bin/activate which will activate your virtual environment. You know you have things working properly if you see "(venv)" at the start of your terminal line. Almost there for your comp, Phew! Now that you have your virtual environment installed, you'll need to install the PubNub SDK. With virtualenv active, just type pip install pubnub. This will go and get all the packages and install the python SDK for PubNub. Congrats! You should now be all set up on your computer! Now the bad news...you're going to have to do this again on your Raspberry Pi...but at least the process is consistent! Make sure you have: - A moniter - A mouse - A keyboard - An ethernet cable - A raspberry pi - A power supply Plug everyone together, with the power for the raspberry pi last. You should see a terminal come up with a whole but of lines coming across the screen. Once it is done, type `startx` which will launch a GUI that you should be a little more familiar with. Once the GUI has loaded, make sure you are connected to the internet via the ethernet cable. Once you have that in place, open a terminal window and go through the same process of installing pip, virtual environment, creating a working directory, setting up the virtual environment in that directory, activating it and then installing PubNub. There's one little twist though for the raspberry pi, you will also need to install raspberry pi-GPIO. This allows you to control the GPIO of the raspberry pi with python. In order to install raspberry pi-GPIO you will need to type three commands into the terminal, the first is sudo apt-get update, and the second is sudo apt-get install python-dev and the last one is sudo apt-get install python-raspberry pi.gpio. Phew! I know that was a lot of set up but it'll be worth it. Now that you have everything set up, it's almost time to get things talking! Setting Up PubNub: Since you were a good student, you should already have your PubNub account set up. Log into your developer account on PubNub and set up your first app. Once you create the app, you should see the new tile with the name of the app you just created. Use this app for the rest of this assignment. Once you click on the app, you will be taken to your dashboard for that app. Two important things to note in here, the keys and the debug console. Now that you have your app created in PubNub, try getting your raspberry pi to subscribe to a channel you are using in your debug console. Try and get your raspberry pi to print out the message from your debug console. (hint: look at the PubNub Docs) Great Work! Next we need to get the GPIO working. Setting up GPIO: Plug your LED so that the longer pin is below the shorter one in the center of the breadboard. Connect a jumper cable to the same row as the LED pins, with the jumper cable in the row of the longer LED pin connected to the 3.3v pin on the GPIO and the jumper cable of the shorter LED pin connected to pin 11 of the GPIO. Blinking your LED: Once you have everything wired up, try running the following script: import raspberry pi.GPIO as GPIO import time LedPin = 11 # pin11 def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(LedPin, GPIO.OUT) # Set LedPin's mode is output GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led def loop(): while True: print '...led on' GPIO.output(LedPin, GPIO.LOW) # led on time.sleep(0.5) print 'led off...' GPIO.output(LedPin, GPIO.HIGH) # led off time.sleep(0.5) def destroy(): GPIO.output(LedPin, GPIO.HIGH) # led off GPIO.cleanup() # Release resource if __name__ == '__main__': # Program start from here setup() try: loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy() Once you run the above script, your LED should blink. Try modifying this script to see how things change (this is what you'll turn in, the modified script). If you have any issues please review the following tutorial: https://www.sunfounder.com/learn/Super_Kit_V2_for_RaspberryPi/lesson-1-blinking-led-super-kit-for-raspberrypi.html Consuming a Message and Reacting To It: Now that you have an LED blinking, try and set up more LEDs following the same process. Once you have multiple LEDs working, try to set up an animation (changing when the lights turn on). Awesome! Now that you have a series of LEDs blinking, lets tie everyting together. Set up a script that will consume a message from your PubNub console, and do your animation based if, and only if, a specific message is sent (as opposed to any message published on the channel). Congrats, you now officially have a device that consumes data from the internet and does something physical based upon it. This is a basic but important building block for IoT. Deliverables: Turn in a zip with the following: - The script you used to have your Raspberry Pi subscribe to your debug channel - The script you used to blink a light on your GPIO - The script you used to blink a series of lights - The script you used to consume data and blink lights from my channel