Saturday, June 4, 2016

Configure WIFI on Raspbian before the first boot

Usually when I prepare a Raspberry for a new project or just want set up a new operation system I configured the network with attached screen and keyboard. Sometimes this is not possible and the WIFI needs to be configured before the first boot of the Pi.

The actual Raspbian distribution Jessie released on May 27 216 (NOT the lite version) or newer brings a cool feature to preconfigure the WIFI settings. A keyboard and a screen aren't necessary to bring a Raspberry Pi with Raspbian to life.

Configuration on Windows

  1. download the Raspbian image on RapsberryPi.org and extract it
  2. if you don't have download the Win32DiskImager to copy the image to the SD card
  3. insert the SD card in your card reader (micro SD cards with adapter, start the Win32Diskimager (it will ask to run as Administrator), and select the extracted image file (from 1)
  4. click "Write" to start the transfer of the image to the SD card 
  5. when the process is finished successfully, you should see a part of the SD card in Windows Explorer
  6. right click into the SD card folder and create a new text-file with the name wpa_supplicant.conf
  7. open the file (if possible with Textpad or Notepad++) copy the following text into the file, ensure the bold parts are changed to your WIFI, the quote marks are necessary

    update_config=1
    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    network={
        ssid="testwifi"
        psk="testpassword"   
    }

  8. save the file, move the SD card from your computer to the Raspberry and start it 
During the first boot of Raspbian, the file wpa_supplicant.conf will be moved into the folder /etc/wpa_supplicant/ for future starts the WIFI credentials will be in the right place and the Raspberry should be able to connect to the WIFI. The file will be not longer accessible under Windows. 

Configuration on Linux

  1.  download the Raspbian image on RapsberryPi.org and extract it
  2. before inserting the SD card, check the attached devices with this command:

    df -h

  3. then insert the SD card and repeat the command after a few seconds

    df -h

    in the output you should find a line like

    /dev/mmcblk0p1

    or

    /dev/sdd1

    okay in this example I will use "mmcblk0p1".
  4. With the command:

    mount

    you need to check if the SD card is mounted somewhere. If so, unmount the SD card with

    sudo umount /mnt/sdcrd

  5. Because df shows the partition and we want to copy the image file to the device, the name of the device in the copy command will be /dev/mmcblk0 or /dev/ssd 
  6. when the device name of the SD card is known, the image file can be copied there. the command is:

    dd bs=4M if=2016-05-27-raspbian-jessie.img of=/dev/mmcblk0
  7. after the successful copy to the SD card, the card can be removed and reinserted. There will be a check for the partitions again

    df -h

    in the output you should find again the same /dev/mmcblk0p1device with the command

    mount

    you will see the mountpoint of the SD card (in this example should be /mnt/sdcrd) 
  8. in this folder should be files like

    bcm2708-rpi-b.dtb
    bcm2708-rpi-b-plus.dtb
    bcm2708-rpi-cm.dtb
    bcm2708-rpi-2-b.dtb
    bcm2708-rpi-3-b.dtb
    bootcode.bin
    cmdline.txt
    config.txt
    ...
  9. with the command (change in the command mywifi to your SSID and mypassword to the password of your WIFI)

    sudo wpa_passphrase mywifi mypassword >> mnt/sdcrd/wpa_supplicant.conf

    you will create a file wpa_supplicant.conf in this folder
  10. now the SD card can be unmounted

    sudo umount /mnt/sdcrd

    the card can be removed and inserted in the Raspberry. 
While starting the Raspberry the first time, the file wpa_supplicant.conf will be moved to it's usual location in /etc/wpa_supplicant. The Pi will connect with the configured WIFI during boot and you can use SSH to connect to the Raspberry. The default login is
username: pi
password: raspberry

No comments:

Post a Comment