POTW–Setting up Pi Zero OTG

POTW–Setting up Pi Zero OTG

This weeks ‘Project of the Week’ is Setting up Pi Zero OTG.
This is a really neat software only project that effectively allows you to set up a Pi Zero in headless mode without ever having to plug in monitors, keyboards etc.

In essence it exploits the fact that the latest version of Raspbian (Jesse) comes with the necessary kernel options and modules to use ‘On The Go’ (OTG) mode for USB.
Adding some command line options to configure the module gets us running, and these command line options can be defined in the /boot partition, which is accessible from Windows when you plug your SD card into a windows machine.

It’s great to see these kind of advancements – updates to the kernel, modules included and people digging in to the details to enable simpler and more functional use.
I configured both my own Pi Zeros in this manner and it is so easy, no need for wobbly Wi-Fi dongles hanging precariously off a tiny OTG adapter, just a single USB cable to my PC and I’m off to the races.
Hats off to Andrew Mulholland for getting all these details together and publishing them.

There’s a lot more to play with in USB ‘OTG’ support – it allows your Pi Zero to mimic a mouse, keyboard, headphones, mic, Ethernet port and more.  I suspect there will be a number of projects around this to come.

Raspberry Pi WiFi Configuration

This article is about how to configure Wi-Fi on your Raspberry Pi without using the desktop GUI

It only takes a minute or so and is basically just entering your Wi-Fi settings into the configuration file.

Let’s get started…

We’ll need our Pi up and running and you’ll want to make sure we have all the latest updates, so open a terminal (or SSH connection to it) and make sure you’ve run the following commands:

sudo apt-get update
sudo apt-get upgrade

Now plug in the USB Wi-Fi plug. To check that it has been recognised you use the command:

lsusb

You should see your Wi-Fi adapter listed. An easy way to check is to run the lsusb command with the adapter unplugged and again with it plugged in (you should see an extra device listed).
Next we want to configure the device so that it connects to our Wi-Fi network. To do that we’ll need to edit the /etc/wpa_supplicant/wpa_supplicant.conf file, so type:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

You’ll see there are only 2 lines in the file (assuming you haven’t already entered any Wi-Fi configuration. So now we add our Wi-Fi details – namely the SSID (network name) and the PSK (password). The format is to add those details as follows:

network={
SSID=“myssid”
PSK=”mypassword”
}

Obviously replace myssid and mypassword with your actual values. Below is one I create with sample values.

wpa_supplicant

Now close and save the file (CNTL+X, Y, ENTER), and reboot and your Pi should connect to Wi-Fi.

 

Hope this has been useful. Leave a comment below and let me know…

Raspberry Pi FTP Server

In this project we are going to set up a FTP server using vsftpd. This is one of the fastest and most secure FTP server for Linux.

This will take us around 10 minutes to complete and is solely consists of typing commands to install and configure software.
By the end of this you will have a FTP server running on your Pi that you (and authorized others) will be able to connect to and upload/download files.

Lets get started…

We’ll need our Pi up and running and you’ll want to make sure we have all the latest updates, so open a terminal (or SSH connection to it) and make sure you’ve run the following commands:

sudo apt-get update
sudo apt-get upgrade

Now we’ll want to install the vsftpd package:

sudo apt-get install vsftpd

When this is complete (it takes a minute or two) we will have the software installed and need to move on to configuring it. To configure it we need to edit the vsftpd.conf  file in the /etc/ folder. As this file is in the /etc/ folder we’ll need to be sudo to do that, so enter the following:

sudo nano /etc/vsftpd.conf

All the options are set to their defaults initially, but there are a few we want to change:

listen=YES

Setting this to ‘YES’ allows us to run the software as a daemon

#listen_ipv6=YES

This setting needs commenting out, or the service will fail to start. Simply add a # character at the beginning of the line

anonymous_enable=YES

Setting this to ‘YES’ allow other users to log in without have to enter a username / password first, but they will only have ‘read’ access (because of the following settings). Obviously, set this to ‘NO’ if you don’t want unauthenticated users being able to access your server / files.

write_enable=YES

Setting this to ‘YES’ (by removing the # character at the start of line) allows authenticated (not anonymous) users to upload files to our server.

anon_upload_enable=NO

Setting this to ‘NO’ (or omitting it from the configuration by adding a # character at the start of the line it is on) prevents anonymous users from uploading files – we don’t want any old user uploading files to our server (and maybe maliciously using up all the storage, or worse)

ftpd_banner=Welcome to the Pi Starter FTP server

You can set this to any text value and that text will be displayed to users when they successfully login. Make sure you remove the # character from the start of the line.

Excellent, we should now be fully configured and ready to start the ftp server, so we do that by entering the following command:

sudo service vsftp start

We should now have a fully working FTP server. You can test this out by running something like CuteFTP, or FileZilla on a remote machine and connecting to your Pi. Here’s an example of my FileZilla client connected to the Pi, and a Windows (PowerShell) command line connection to it

filezillaps_ftp

Hope this has been useful. Drop me a comment if you got it working.

Time-lapse Video with a Webcam

This project will show you how to use a standard webcam to create a time-lapse video recorder – great for watching the whole day go by in a few minutes.

For this we’ll need a Raspberry Pi, a USB webcam (and a portable power source if you want to position it somewhere outside)timelapse_stuff
I’ll be using a Pi B+ running the latest version of Raspbian Jesse, a USB Wi-Fi dongle, a Microsoft Lifecam HD and an Anker portable battery pack.

First up you’ll want to get powered up and logged in, with the USB webcam connected. To check the webcam is recognised just type lsusb

lsusb

You can see that my webcam is Device 005 on Bus 001 and is from Microsoft Corp.  So great we’re in business, now we need some software to control the webcam. For that we’ll be using fswebcam – this is a great package with tons of features, but we’ll only be using the basics. To install it just type sudo apt-get install fswebcam

fswebcam

You can try it out by typing fswebcam –r 1280×720 –no-banner image.jpg (the –r 1280×720 tells it what resolution to use for the capture, the -–no-banner tells it not to display the time banner and image.jpg is the name of the file to save the image in.

image1

You can view this image now on the Pi if you have a monitor attached or by grabbing a copy of the file (FTP, SFTP etc) onto another machine.
Now we have this working we’ll also need some software to convert all the image captures we take into a movie, for this we’ll use avconv which is part of the libav-tools package, so let’s install that by typing sudo apt-get install libav-tools

libav-tools

That’s all the software we need, it is just a matter of orchestrating things. We’ll need to:

  1. Capture a webcam snapshot to a file (with a sequential number as the filename)
  2. Sleep for X seconds
  3. If we don’t have enough snapshots yet go back to step 1
  4. Merge all the snapshots together into a time-lapse movie file
  5. Delete all the snapshot files

To write the script that does all this type nano timelapse.sh and enter the following:

#!/bin/bash
DIR=/home/pi/timelapse
COUNT=0
MAXCOUNT=30
INTERVAL=10
DATE=$(date +”%Y-%m-%d_%H%M”)

while [ $COUNT -le $MAXCOUNT ]; do

FILENAME=$(printf “%05d.jpg” “$COUNT”)
let COUNT=COUNT+10

# take the snapshot
fswebcam -r 1280×720 –no-banner $DIR/$FILENAME

# pause for a bit
sleep $INTERVAL;

done;

# now make the movie
avconv -r 5 -I $DIR/%05d.jpg -vcodec libx264 -r 30 timelapse_$DATE.mp4

# and tidy up the files
rm -f $DIR/*.jpg

Final bits required are to create the timelapse folder – type mkdir timelapse and make the script we wrote executable – type chmod +x timelapse.sh

We now have a script that will take a snapshot every 10 seconds (INTERVAL) until we have 30 of them (MAXCOUNT) and then combine them all into a movie for us. Be warned that this will take some time (30 time 10 seconds to get all the shots and then around 10 minutes of processing to combine them) but the result will be a cool time-lapse movie.
Feel free to adjust the INTERVAL and MAXCOUNT to record for longer period and with different periods between shots.

Getting Started with a Pi Zero

Managed to get hold of a Pi Zero ? Here’s how to get started with it…

The Pi Zero only has a single micro USB port for connecting peripherals. That can make it a little tricking when trying to get things set up. There’s a few bits that you will need to go along with your Pi Zero in order to set it up and get it going :

First up you’ll want to load the latest version of Raspbian on to your micro SD card (instructions here), then you’ll want to insert the micro SD card into your Pi Zero, making sure it’s pushed all the way in.

IMG_20160629_132625IMG_20160629_132649

Next, connect up the micro USB OTG (On The Go) connector to the wireless keyboard/mouse dongle and insert that into the middle of the three connectors.

IMG_20160629_132807IMG_20160629_132820IMG_20160629_132849

Lastly, for this bit, connect up the mini HDMI cable to the monitor and then connect the micro USB power cable.

IMG_20160629_132931IMG_20160629_132954
The Pi Zero should now boot and you’ll see the text boot sequence log flashing by on the monitor. Depending on which version of Raspbian you are using you will either get a shell (text based console) or the screen will blank and then start up the desktop. If it boots to the desktop, then open xTerminal, if it boot to the shell then your good to go.

What we need to do now is to configure the Wi-Fi settings. To do this enter the following :

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

This will open the existing file which will likely only have 2 lines in it. Underneath those lines you will add your Wi-Fi details. It looks like this:

network={
ssid=”MYSSID”
psk=”mypassword”
id_str=”Home”
}

Replace MYSSID and mypassword with the actual values for your Wi-Fi network.

You can multiple of these ‘network’ sections if you have multiple Wi-Fi access points that your Pi Zero will access. Just make sure that each of them has a unique id_str.
When you have added your details hit CNTL+X, then Y and then ENTER to quit and save the file. Now you can shutdown the Pi Zero and we’ll do a little ‘plug and play’

When it has shutdown pull out the wireless keyboard/mouse dongle (it may leave the OTG adapter in there, no problem). Now plug in the USB Wi-Fi adapter to the OTG and make sure it’s plugged into the Pi Zero. Reapply power (unplug and plug in again) and it should boot up as usual, but will now automatically connect to Wi-Fi.

IMG_20160629_133031

You can now access it from a remote machine via SSH. Open your SSH client and connect to raspberrypi.local (note: you need to have iTunes or other Apple software installed on the remote machine for it find the Pi using .local) Assuming all that has worked you’ll have a SSH connection to your Pi Zero and can tinker as needed. You can also, at this stage, remove the mini HDMI and run ‘headless’.

Great, we’re done, one Pi Zero set up with Wi-Fi, running headless, ready for some project.

Raspberry Pi P6 Reset Hack

I’ve been trying to get some code running on my Pi 2. The code has been ‘problematic’ to get running, and I’m running into frequent hangs on the Pi where I cannot initiate a new SSH or console session. It’s also complicated because the Pi is outside in the garage (I have a GPS hooked up so it needs some visibility of sky). That means that every time it hangs I have to head out to the garage, pull the micro USB cable, reinsert it and then start the whole ‘Edit, Compile, Run, Head out to the garage to Reset’ cycle again.

Anyway, in an effort to simplify things I was looking around for a way of remotely resetting the Pi without having a connection to it. I found a ton of sites talking about the P6/RUN header on the newer boards, but this was mostly just about adding pins to short out with a jumper or adding a momentary switch to do the same. What I wanted was something more like connecting an Arduino pin, or a pin from another Pi to the P6/RUN on the target Pi and forcing a reset that way. What I wanted was a “Raspberry Pi P6 Reset Hack” – unfortunately AdaFruit don’t sell a kit for that.PiP6PullUpResistor_thumb

I was initially concerned about the current that the watchdog Pi would have to sink on the GPIO0 pin, but looking at schematics it seems the RUN pin is pulled up to 3.3V by a 10K resistor – a little bit of Ohms law says this will mean 3.3v/10K = 0.33mA will flow, well within the recommended limits of the GPIO pins 2mA to 16mA

I have an ESP8266-12 laying around just waiting for this kind of thing, but for the sake of speed I opted for another Pi I had (already hooked up, working and configured). So this essentially came down to :

  • Solder header pins on the target Pi (the one to be reset)
  • Connect the grounds of the target and ‘watchdog’ Pis together
  • Connect a GPIO pin from the ‘watchdog’ Pi (GPIO0) to the RUN pin on the target Pi
  • Putting together a small app to force the GPIO0 pin low for a few milliseconds and then high again.

PiP6WithHeaderPins_thumb Pi2P6WithGndAndGpioConnected_thumb

For the app to be run on the ‘watchdog’ Pi, I’m making use of the excellent wiringPi library. Instructions for downloading and building it can be found at http://wiringpi.com/
The code for ‘reset-app’ is simply (nano reset-app.c):

 

#define <wiringPi.h> 
int main (void) 
{
      wiringPiSetup();
      pinMode(0, OUTPUT);
      digitalWrite(0, HIGH);
      delay(500);
      digitalWrite(0, LOW);
      delay(500);
      digitalWrite(0, HIGH);
      return 0; 
}

… then to build it just execute :

sudo gcc –Wall –o reset-app reset-app.c –lwiringPi

Now when my target Pi hangs I can just ssh into the watchdog Pi, and run sudo  ./reset-app  and the target Pi reboots.
Also, when I’m done with this testing/hanging stuff I may simply connect a switch to the header for ‘’future ‘just-in-case’ things…

DISCLAIMERS

This is a QuickAndNasty™ solution. I am not responsible for any damage to your Pi, your electrics, your health or anything else – anything you do as a result of reading this is at your own risk.
Using this to reset you Pi can result in SDcard corruption !!   All calculations are off the top of my head, and could be wrong.

There are a ton of things to improve it also:

  • It relies on the GPIO pin on the watchdog ‘floating high’ while it is an input – it really should be set as an output (HIGH) at start-up.
  • I’ve not tested what happens when the watchdog Pi reboots – there’s a chance (likelihood?) that it will reboot the target Pi.
  • The ‘reset-app’ can be improved (drastically!!)

EDIT: I have tested what happens when the watchdog Pi reboots and, for me, it does not reboot the target Pi – however there is a chance (likelihood ?) that it will reboot the target Pi.
Anyway – enjoy, and let me know if you found it useful….

Starting Pi Starter

Okay, first post…

It’s been a manic week at Pi Starter HQ, getting everything set up (domain names, hosting, websites, themes, etc) and planning out what things should look like (menus, sidebars, footers etc) but most of that is done, or planned now.
I’ve also been putting a lot of thought into what kind of articles are needed (suggestions in the comments if you want something specific), and I have a bunch of idea – more ideas than time available at the moment…

Loosely, I’m thinking of having :

  • Projects for Beginners, Intermediate and Advanced level folks, hardware and software
  • Useful snippets
  • News and Announcements
  • A ‘Project of the Week’ (Fridays)

That’s the ‘starting’ point, we’ll see how things pan out and what people want to see, and adjust accordingly. I think there might be scope to cover Arduino and micro:bit projects too, but that’s on hold at the moment – plenty to do already.

So, time to get on with some writing…

Gradle Generate Release Notes from Git

I thought my ‘Gradle Generate Release Notes’ script might be useful for others, so here you go:

If you are using the built in Gradle tool for Android Studio and you want to automagically generate release notes (or a list of commit messages) from Git then this little script might help (whilst it is pretty specific for Android Studio, it could easily be modified for any Gradle

AndroidStudio

I got the inspiration from this post over at coders kitchen – but it seemed a little to complex for my needs, so I cut it right back to just text based, and just dates and commit messages between the tags. More than sufficient for my needs.

Raspberry Pi P6 Reset Hack

I’ve been trying to get some code running on my Pi 2. The code has been ‘problematic’ to get working, and I’m running into frequent hangs on the Pi where I cannot initiate a new SSH or console session. It’s also complicated because the Pi is outside in the garage (I have a GPS hooked up so it needs some visibility of sky). That means that every time it hangs I have to head out to the garage, pull the micro USB cable, reinsert it and then start the whole ‘Edit, Compile, Run, Head out to the garage to Reset’ cycle again.

Anyway, in an effort to simplify things I was looking around for a way of remotely resetting the Pi without having a connection to it. I found a ton of sites talking about the P6/RUN header on the newer boards, but this was mostly just about adding pins to short out with a jumper or adding a momentary switch to do the same. What I wanted was something more like connecting an Arduino pin, or a pin from another Pi to the P6/RUN on the target Pi and forcing a reset that way. What I wanted was a “Raspberry Pi P6 Reset Hack” – unfortunately AdaFruit don’t sell a kit for that.PiP6PullUpResistor

I was initially concerned about the current that the watchdog Pi would have to sink on the GPIO0 pin, but looking at schematics it seems the RUN pin is pulled up to 3.3V by a 10K resistor – a little bit of Ohms law says this will mean 3.3v/10K = 0.33mA will flow, well within the recommended limits of the GPIO pins 2mA to 16mA

I have an ESP8266-12 laying around just waiting for this kind of thing, but for the sake of speed I opted for another Pi I had (already hooked up, working and configured). So this essentially came down to :

  • Solder header pins on the target Pi (the one to be reset)
  • Connect the grounds of the target and ‘watchdog’ Pis together
  • Connect a GPIO pin from the ‘watchdog’ Pi (GPIO0) to the RUN pin on the target Pi
  • Putting together a small app to force the GPIO0 pin low for a few milliseconds and then high again.

PiP6WithHeaderPinsPi2P6WithGndAndGpioConnected

For the app to be run on the ‘watchdog’ Pi, I’m making use of the excellent wiringPi library. Instructions for downloading and building it can be found at http://wiringpi.com/
The code for ‘reset-app’ is simply (nano reset-app.c):

 

#define <wiringPi.h>
int main (void)
{
    wiringPiSetup();
    pinMode(0, OUTPUT);
    digitalWrite(0, HIGH);
    delay(500);
    digitalWrite(0, LOW);
    delay(500);
    digitalWrite(0, HIGH);
    return 0;
}

… then to build it just execute :

sudo gcc –Wall –o reset-app reset-app.c –lwiringPi

Now when my target Pi hangs I can just ssh into the watchdog one, and run sudo ./reset-app and the target Pi reboots.
Also, when I’m done with this testing/hanging stuff I may simply connect a switch to the header for ‘’future ‘just-in-case’ things…

DISCLAIMERS

This is a QuickAndNasty™ solution. I am not responsible for any damage to your Pi, your electrics, your health or anything else – anything you do as a result of reading this is at your own risk.
Using this to reset you Pi can result in SDcard corruption !!   All calculations are off the top of my head, and could be wrong.

There are a ton of things to improve it also:

  • It relies on the GPIO pin on the watchdog ‘floating high’ while it is an input – it really should be set as an output (HIGH) at startup.
  • I’ve not tested what happens when the watchdog Pi reboots – there’s a chance (likelihood?) that it will reboot the target Pi.
  • The ‘reset-app’ can be improved (drastically!!)

EDIT: I have tested what happens when the watchdog Pi reboots and, for me, it does not reboot the target Pi – however there is a chance (likelihood ?) that it will reboot the target Pi.
Anyway – enjoy, and let me know if you found it useful….

Quadcopter V1

So for the past few weeks, on and off, I have been focusing on hardware – building a magnificent flying machine – a quadcopter.

I bought a build it yourself quadcopter kit from ebay, just for quickness – it was around £120 and came with all the required bits to get started:

  • 4 x generic 2212 motors with mounts
  • 4 x generic 30A Electronic Speed Controllers (ESCs)
  • A power distribution board
  • 600mm frame (X layout)
  • A flight controller module (KK 2.1.5)
  • Propellers (2 x CW, 2 x CCW)

The only bits missing were batteries (I bought 2 x 3s LiPo and a charger) and the remote controller (I bought a FlySky TH9x and receiver). A few sundries were also needed – a little buzzer, some cable ties, tape, soldering etc.

After taking delivery of the various bits, I put the quadcopter kit together in a couple of hours, and I’ve been noodling around for the past couple of weeks with getting the right settings and configuration sorted out on both the Flight Controller (FC) and the Transmitter.

It seems that not many folks have the FC and FlySky combination that I had, but with enough googling around I found that I should have had the Transmitter set to ACRO mode rather than HELI mode. HELI mode only allowed me to get the sticks registering +-60 rather than the +-100 that I needed, also the trims couldn’t get to 0

The final setup for the transmitter was:

  • Mode Type = ACRO
  • Throttle = Reversed
  • Elevation = Reversed

Then I headed to the ‘Receiver Tests’ menu in the FC and using the stick trims to make sure all settings were trimmed to 0, and when I moved the sticks the correct values were displayed on the FC.
IMG_20150514_210851IMG_20150514_210924IMG_20150514_210929IMG_20150514_210937IMG_20150514_210940IMG_20150514_210947

Make sure you have programmed your ESCs – you can do all 4 at once by removing power from the ESC, making sure your throttle is at maximum and all ESCs are plugged into the right connector then while holding down buttons 1 and 4 reapply the power. You should see the display saying ‘Throttle passthrough’, wait for the beeps and move the throttle to minimum then let go of the buttons, you should hear more beeps and your ESCs should be calibrated.

 

The litmus test for me is arming the unit, giving it a little throttle so the props spin and then holding it in my hand. Dip each of the motors and make sure it speeds up and the quad tries to level itself, then hold it level it again and make sure the motor slows again. I find this test, as well as giving it a bit more throttle and making sure it tries to lift as a great indicator that everything is set up correctly.

When that’s all working correctly, you know your quadcopter is ready for flight.
Outside, open space, little or no wind, time for the first flight – give it throttle, have the quadcopter lift then reduce throttle and let it land. Now the basics are out the way time to test out the maneuverability and do the PID tuning etc to get smoother flying.

Next up is attaching a camera – maybe a Raspberry Pi with a USB camera, or an unused smartphone.