#PiCam/#ChewCam Automation!

Overview: It’s awesome, buuuut…..

Note: This doesn’t work unless you’ve read and setup your Pi like described in the previous guides linked below.

So you’ve gone thru the first three posts (1, 2, 3) about the #PiCam/#ChewCam and got it going and think it’s awesome as hell, right?

Now think of the awesome setups you can stream with your new camera. You’re out and about and you want to stream your party/gig/event/whatever. You’ve got an awesome cellphone plan or a wifi connection at the event, and want to do as little as possible to the pi to get it running.

For me, I wanted to use the #PiCam to stream to Chew.tv and share my awesome parties/events/gigs that I’m at. I already have to setup gear usually, so I wanted it to be as simplistic as possible for me to turn the pi on, connect it to whatever internet connection I’m using, and let it do it’s magic. In a perfect world for me, all I would need to do is turn on the Pi and be done with it (which I suppose I could do), but I suppose a little bit of interaction is probably safest.

Step 1: Configuring your Network

So here’s my situation and setup. I plan on using my ChewCam at my regular weekly event. We have a (usually)steady wifi there, then I have my cellphone’s hotspot feature, then my home wifi for when I’m at home. So what we do is add these all in the proper priority in the right file. To do this, we’ll edit the wpa_supplicant file. This is for wifi connections only, cause honestly, if you’re connecting by ethernet this is all moot. You won’t have this file if you have no wifi built-in or via dongle (older Pi devices).
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
This pulls up the file in the editor. I’ll post my example and you should be able to figure out what to do to match your situations.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev



Ok, so what does the above do? It is ordered in a specific manner so that the Raspberry Pi connects to the wifi ssid’s automatically. So at home, when I turn my Pi on, it connects to the “supercoolhomewifi” using the password assigned. The neat part though is, let’s say I am at home and turn on my phone’s hotspot. Since it is higher in the priority list, the Pi will move wifi connection to it. Extremely handy. Same thing if the Pi was connected to my “workunsecuredwifisucks” ssid. I turn on my hotspot on my phone (or whatever device), and the pi switches over to it and uses it’s connection.

After you’ve made changes to your wpa_supplicant file, you’ll need to reboot to put it into effect.

Step 2: Semi-automagic connection

So here is where things get fun. Chew.TV has an API service, and thru the API, you can create/modify/delete shows and grab show/chat information. So with a little wizardry, we can get the #ChewCam to create our show and automatically start streaming to the show.

First, head on over to Chew.TV and get your API Key here. Once you have your API key, we’re gonna do a few things on the Pi to get it ready for what we need.

The magic is done via PHP & cURL, by using your API key, we’ll call the api, create your show, then pass the stream url/key to the gstreamer string (from the previous guide steps). So we’ll install PHP, php-curl, and php-cli a command-line interface version of PHP.
sudo apt-get install php5 php5-curl php5-cli
Now that’s complete, we’ll create our PHP file that does the magic. Create a file in your favorite editor.
nano mychewbot.php

$apikey = "yourapikeyhere";
$data = array("name" => "Whatever You Want To Name Your Show", "description" => "Show Description");
$data_string = json_encode($data);
$ch = curl_init('https://api.chew.tv/v1/shows?key='.$apikey);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
$result = curl_exec($ch);
$decoded = json_decode($result);
$streamurl = $decoded->show->stream_url .'/'. $decoded->show->stream_key;
exec("gst-launch-1.0 rpicamsrc bitrate=2000000 do-timestamp=true ! video/x-h264,width=1280,height=720,framerate=30/1,profile=high ! h264parse ! queue ! flvmux name=mux streamable=true alsasrc device=plughw:1,0 ! audio/x-raw,rate=44100,channels=2 ! queue! voaacenc ! queue ! aacparse ! queue ! mux. mux. ! rtmpsink location=$streamurl");


So what does it do? Well first, we create an array with required data needed to create a show. You can expand this array by adding more info from the options listed here. The next part encodes the array into a json string.

We then connect to the api interface and send all the proper header and REST-type information. Then we retrieve the results, decode the json string into an object, then pull our URL and Key information from the object that we created.

The final line, the exec()┬áline should look familiar to you. Inside the exec is our gstreamer string from our previous steps. Now depending on your connection you may want to modify the width/height/FPS. Save the file and we’ll move on.

Now, if you’ve done all the steps we’ve talked about previously, and didn’t encounter any errors, we should be good to go for some magic! We’re going to call that new php file with the php command line interface. In my example below, my php file is named chewshow.php:


But wait? Why is it blank? Well that’s a GOOD thing. The way we have it setup, it’ll only display something if there is an error. What’s happened is the ChewCam file is now streaming in the background. Go ahead, go look at your chew shows!


So there we have it. When you’re done streaming, you can CTRL+C in your ChewCam via SSH/RDP/VNC and the stream wraps up and you’re done. Or if you turn off the camera without doing that, Chew.TV will auto-end your stream after 20 minutes.

Now you could of course take this to full-on automation, but then what about those times where you simple wanted to turn on your Pi and not stream. I figured a simple in-between step would be good, and it’s a lot less bothersome now to setup and use out in the field than it used to be.