Python with Ubuntu on Windows

Now that bash is on Windows, I wanted to try and make all the other guides I’d writen for Python on Windows irrelevant.

So here’s how to setup an effective environment for Python on Ubuntu on Windows.

1. Install Bash on Windows
2. Check for updates
3. Check out the REPL
4. Install Pip
5. Install VirtualEnv
6. Install VirtualEnvWrapper
7. Create your first virtualenv
8. Configure bashrc to keep it working
9. Install some packages
10. Test Flask

1. Install Bash on Windows:
Here’s the announcement blog for context.
How to Geek has a good breakdown of making it happen.

Make sure you remember your password.

Now that its installed try opening a command prompt and typing bash.
The prompt should change like this:
bashtomnt

Notice that path?
/mnt/c/Users/TimReilly

That’s your user directory for windows where your OneDrive, Documents, Desktop, etc. exist.

You can go in there now and run python scripts that might already exist, but your probably won’t have all the necessary packages installed.

Before we move forward we want to make sure Ubuntu is up to date.

2. Check for updates
From another command prompt:
lxrun /update

And inside bash
sudo apt-get update

Thanks reddit for the tips!

3. Check out the REPL

Now run python!
$ python

Should look like this:
pythonrepl

4. Install Pip

Now we’ll install Pip:
sudo apt-get install python-pip

If you have permission issues try starting an elevated prompt:
$ sudo -i
$ apt-get install python-pip
$ exit

Use exit to return to the regular prompt.
Should look like this:
sudoi
Yay we’ve got pip!
Try pip list to see what comes standard.

5. Install VirtualEnv
Now we’re basically following along with the guide presented at hitchhikersguidetopython.com

Again, you might need to start an elevated prompt to install virtualenv.

$ sudo -i
$ pip install virtualenv
$ exit
$ cd my_project_folder
$ virtualenv venv

Then to use the VirtualEnvironment

$ source venv/bin/activate

You should now see a little (venv) before your prompt.
Like this:
virtualenvfolder

Now you’ve created a virtualenv inside of your my_project_folder directory. Which is cool, but can be confusing with git, sharing code, and testing package versions.
So we use VirtualEnvWrapper to keep our virtualenvs in the same place.

Before we move on make sure you deactivate your env
deactivate

6. Install VirtualEnvWrapper

http://virtualenvwrapper.readthedocs.io/en/latest/index.html


$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh

$ export WORKON_HOME=~/
Can be customized to whichever directory you’d like to place your virtualenvs

7. Create virtualenv using virtualenvwrapper

$ mkvirtualenv venv
$ workon venv
$ deactivate

Here’s an example of what it looks like to remove our venv directory and instead use venvv which will be stored in the directory underlined in red.

venvv

8. Configure bashrc to keep it working

This might not happen to you, but when I opened a new bash terminal I had to re-source my virtualenvwrapper.sh and WORKON_HOME.

So instead I added those lines to my bashrc script.

$ sudo nano ~/.bashrc
-- type in password --

This is what it looks like in nano for me.
Ctrl+X to exit and y-enter to save.
bashrc

Then either:

Source ~/.bashrc

Or start a new command prompt->bash and try “workon” or “lsvirtualenv”

See the next image for a simple workflow.

9. Install some packages

Now lets install ‘requests’ into our newly created virtualenv:
pipinstallrequests

Isn’t that nice!

10. Test Flask
Finally we’re going to test this with flask.
First we install the required files using pip into our activated ‘venv’
Then runserver -> navigate to the designated address -> and see our site.

Here’s what it looks like:
flaskrunning

Have fun building with Python, on Ubuntu, on Windows!

Bike with pizza tied to the back
Sometimes you gotta tie a pizza to your bike.

W10 Core IoT Workshop

IoT and Windows are Better than ever with the new IoT Core for Raspberry Pi.

In this workshop we’ll have deploy Universal Windows Platform Code to a Raspberry Pi and begin communicating with the cloud and cloud services in the form of an Azure IoT Hub.

The steps include:
1. Gathering the materials
2. Preparing the Raspberry Pi
3. Installing our software
4. Connecting our LED and PushButton
5. Connecting to our raspberry pi
6. Deploying our push button app
7. Connecting our app to IoT Hub
8. Connecting our app to weather data

1. Gathering the materials

For this workshop you’ll need about $55 worth of hardware, but all the software is free.

Hardware:
1 Raspberry Pi 3 (It has built in Wifi!)
1 Micro USB to USB Cable
1 Breadboard
4 Female/Male Jumper Wires
1 Push Button Switch
1 led
1 200 ohm resistor for said led
1 Monitor with HDMI out

Software:
Windows 10 Machine version 10.0.10240 or better.
Visual Studio 2015 Update 2
The Windows 10 IoT Core Dashboard
Git

Here's my little setup
Here’s my little setup

2. Preparing Our Pi

The windows developer site has very clear instructions for setting up your device with the proper operating system so lets hop over there.

developer.microsoft.com

If you follow these instructions all the way to Step 4 of 4 you’re ready for step 5 on our workshop. So skip ahead if you followed along there.

I’m working with a raspberry pi three so my selection screen looks like this:

selection

3. Install the dashboard and Flash the OS
Next we’ll download the dashboard as prompted by the site

Then we’ll download the ISO of the raspberry pi image we want to use, then click through installer.

Once installed we can use the IoT Dashboard to flash the image onto our SD card in the form of a .ffu

Then we’ll connect the device to the network by selecting configure device.

Really though the instructions are super clear RIGHT HERE

4. Install/Update Visual Studio

We need Visual studio Update 2 to build our IoT Application, so make sure you’re up to date!

Or go here to install it: https://www.visualstudio.com/vs-2015-product-editions

Seriously though, they did an awesome job with the documentation.
If you followed along to their step 4 you’re ready for our step 5.

5. Connecting our LED and PushButton

Now that we have our pi configured and Visual Studio up to date, let’s plug in our neat hardware!

Here’s an overview of the pin layout for the raspberry pi 3:
gpiopins

And the wiring diagram for the led + push button looks like this:

wiringdiagram

Sweet, lets light it up!

6. Connecting to our raspberry pi

We need our raspberry pi’s IP address before we try to deploy our code.
Power it up with a micro USB cord and connect it to a monitor with HDMI and see the landing page for your machine.

With the Windows IOT Core Come a super helpful web portal that allows you to configure your machine through a browser.

Navigate to that IP:
http://you.have.an.ip:8080/
https://192.168.1.145:8080/ is the IP address of my machine when I tried this

You’ll be prompted with a login panel.
The default user name is:
Administrator
The default password is:
p@ssw0rd

Here’s what you should see in the portal:
coreportal

From here you can configure the name/password, get network info like the MAC address under Networking, and even test out some samples.

Sweet! This is one of my favorite Window IoT Features.

7. Deploying our push button app

The pi is setup, our computer is updated with the newest build tools, now its time to deploy some code!

Go here and clone this repo to a dev directory:
https://github.com/timmyreilly/RaspberryPiWorkshop

And setup visual studio debugging by right clicking on PushButton project like this:
properties

Then under debug set the remote machine ip to the ip you collect from the IoT Dashboard or from the raspberry pi itself by plugging in a monitor.
findip
You will then need to rebuild your solution, and restore nuget packages.

Poke around for a minute, put a break point in the buttonPin_ValueChanged method and step around to see what’s going on with the push button.

8. Connecting our app to IoT Hub

Next we’re going to turn on the IoT Hub Connection.
Go into the azure portal and create a new IoT Hub like this:
iothubone
Then create a new device called MyDevice in the IoT Hub.

More information about the IoT Hub can be found RIGHT HERE.

Then we’ll want to add our connecting strings to the app at the very bottom of MainPage.xaml.cs


static string iotHubUri = "YOURHUB.azure-devices.net";
static string deviceKey = "TOKENasQOPUesD1BmSOMETHINGCOMPLICATED";
static string deviceId = "MyDevice"; // your device name

Next we’ll uncomment this line of code on line 78:

SendDeviceToCloudMessagesAsync();

To see what’s happening in the event hub there is a small console application that we’ll run alongside to see what’s making into the cloud.

You can find that code here:
https://github.com/timmyreilly/IoTHubConsoleReader
Open that in visual studio and replace the connection string with the one provided in the Azure Portal.


string connectionString = "HostName=YOURHUBNAME.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=YOURSECRETOKENqf"

Sweet, now run both those apps at the same time, push the button, and see the glory that is Internet of things.

9. Connecting our app to weather data

Now that we’ve got it talking to the cloud, lets listen to what the clouds have to say!

We’re going to use forecast.io to provide weather data to our app, then use Speech. Synthesis to read it out over a the audio jack.

Navigate to forecast.io and signup register an account, then copy the complicated token highlighted here:

forecast

And replace what you see at line 168:

private const string FORECAST_URL = "https://api.forecast.io/forecast/YOURSECRETOKEN/";

Then if you want to be more specific about where you’re getting weather data replace the string on line 76.

var words = await GetWeatherString("37.8267,-122.423")

Make sure that the code in lines 72 through 81 are uncommented.

Deploy the app, press the button, and listen to the weather like never before!

It’s all very simple code and ready to be spun into lots of fun projects.
Here’s a list of all the ones that me and my friends could thing of:
Tap into the band app cloud
Morse code
Dance party
Send a random act of kindness
Tap into yammer
Time to finish breast feeding start and stop button
Random stats button
Gives you weather, or distance walked
Morse code each other
Send bro to someone random – twilio
Launch a middle
Listener recognition and ignore
Competitive button clicking
Obscure metadata
Best reflexes tester

Hope you had fun getting started with Windows IoT!

This workshop wouldn’t be possible without the help provided by the community. Here are some of the helpful posts that help me learn:
Windows 10 IoT Core Speech Recognition
Windows 10 IoT Core Speech Synthesis
All The ms-iot samples

Feel free to ask me questions in the comments or on twitter @timmyreilly

TOMATOES IN PROGRESS
TOMATOES IN PROGRESS