Category: Linux

Information about Linux

  • Knoppix 7.4 – How to create USB bootable disk

    Knoppix 7.4 has a desktop icon to create a flash disk.

    This in how do create the usb drive without creating a CD.

    1. Create a new virtual computer using the downloaded Knoppix .iso file

    2. Redirect a USB flash drive to the virtual computer and start the virtual computer.

    3. Click on the icon ‘Install KNOPPIX to flash disk’
    (as of Nov14) This only works if you select ‘Overlay partition’. You can reduce the size of the overlay partition to leave space on the first partition usable for general Flash Disk purposes. (If the partitioning on the USB disk is non-standard, you may need to clear it and then delete entries from /etc/fstab and then restart the virtual computer).

  • Fix a non-booting Linux disk

    If you boot from a Linux CD, you can switch to your non-booting disk as follows:

    1. Find the device that is your non-booting disk.
    This could be /dev/sda, /dev/sdb etc. (Disks presented as SCSI)
    This could be /dev/hda, /dev/hdb etc. (Disks presented old-style HDs)
    Or even /dev/vda etc. (Disks in virtual systems)
    2. Mount it, and then remound /dev, /proc, and /sys onto the non-booting disk

    mount /dev/sda2 /mnt
    mount --rbind /dev /mnt/dev
    mount --rbind /sys /mnt/sys
    mount --rbind /proc /mnt/proc

    3. Set your non-booting disk to root

    chroot /mnt

    Now you can run your normal commands to fix your boot

    grub2-install /dev/sda # restore grub
    # Note: you may need to update the Disk UUID in /boot/grub2/grub2.cfg
    # to the value returned by blkid /dev/sda2
  • Raspberry Pi WiFi Automatic Connection

    By default, RPi doesn’t connect to the WiFi automatically, which is annoying, especially when you don’t have keyboard/mouse/screen connected.

    You can fix this by:

    First connect to the WiFi using the WiFi app. This will include the following lines in /etc/network/interfaces:

    allow-hotplug wlan0
    iface wlan0 inet manual
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    iface default inet dhcp

    Then edit /etc/wpa_supplicant/wpa_supplicant.conf

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    network={
            ssid="SSID"
            psk="pre-shared-key"
            proto=RSN
            key_mgmt=WPA-PSK
            pairwise=TKIP
            auth_alg=OPEN
    } 

    You can repeat the network block for other networks you’d like to automatically connect to.

  • Raspberry Pi – easier access

    For the Debian builds (Raspbian), the default username/password is pi/raspberry.

    Once logged in, you can create new users by:

    sudo adduser yourname

    You can enable the root user by (the same procedure will reset any user’s password:

    sudo passwd root

    If you’re using ssh, then setting up keys saves a lot of time.

  • RGPIO and PiBoard

    How to use RGPIO with PiBoard.
    This guide uses a PiBoard with the following components:
    • EA DOG-M LCD (on pins 17,0,11,22,23,24,25) in 3.3V mode)
    • LCD Backlight (on pin 18)
    • Buttons connecting pins 7,8,9,10 to ground when pressed
    • PCF8563 I2C Real time clock (with super-capacitor power backup)
    • DS1621 I2C Temperature device
    • DS18B20 1-Wire Temperature device
    To use RGPIO from python, we open the /dev/rgpio device as an unbuffered file in r/w mode:
    rgpio=open('/dev/rgpio','r+',0)
    # r+ means read/write; 0 means unbuffered
    we can then write to and read from this file to control the PiBoard.

    LCD – EA-DOG-M

    RGPIO does the basic initialization from the C command to get the LCD running in 4-bit mode with the appropriate pins, but the EADOG-M screens need additional initialization according to whether you run in 3.3V or 5V mode.
    \C29 – Set 4-bit mode and instruction set 1
    \C15 – Set bias for 3-line LCD
    \C55 – Booster (for 3.3V) on, Contrast in follower mode
    \C6E – Follower ccontrol
    \C72 – Set Contrast
    \C28 – Set 4-bit more and instruction set 0
    rgpio.write('L17,0,11,22,23,24,25C')
    rgpio.write('LD\C29\C15\C55\C6E\C72\C28')

    LED Backlight (EA LED55x31-G)

    The EA LEDs are coordinated with the LCDs but use completely separate pins. The PiBoard lets you choose between a 3.3V or a 5V supply using the appropriate current-limiting resistor.
    The LED is controlled through a NPN transisitor driven by pin 18. The Raspberry Pi has hardware PWM enabled on pin 18 and this can be used to control the LED brightness. RGPIO supports both software and hardware (pin 18 only) PWM, but the hardware version is much more efficient.
    #Set LED to 50%
    rgpio.write('W50,18C')

    Buttons (Tyco 6x6mm)

    When the buttons are pressed, the GPIO port is shorted to GND. For the buttons to work, they need a pull-up resistor. The PiBoard has space for a separate pull-up resistor, or we can use the CPU’s internal pull-up capability using P7U8U9U10U.
    To get the pin settings we use RGPIO’s pin change interrupt to tell us when the pin is grounded.
    rgpio.write('P7U1,7L')  # Initialize pull
    rgpio.write('P8U1,7L')  # up and get repeated notifications
    rgpio.write('P9U1,7L')  # when pin Lo
    rgpio.write('P10U1,7L')
    To see what is going on, we have two options: read in a polling loop, knowing we may get a blank line because there is nothing waiting:
    while True:
        inLine=rgpio.readline()
        # Now process the line
    Or we can get RGPIO to send us a signal to tell us something is waiting.
    import signal,os
    def IOSignal(signum,frame):
        inLine=rgpio.readline()
        # Now process the line
    
    signal.signal(signal.SIGIO,IOSignal) # call IOSignal on SIGIO
    pid=os.getpid()                      # Get PID of this process
    rgpio.write('%dS'%pid)               # tell RGPIO to send SIGIO here
    while True:
        time.sleep(1)                    # just loop asleep waiting...

    I2C Clock

    To give the Raspberry Pi a clock, you can either be connected to the internet (in which case, the very accurate internet time is available), or you need to provide an external clock. We do this with an I2C Real Time Clock chip.
    The I2C bus is a very convenient mechanism to communicate between components. It needs 4 connections: VDD, GND, SCL, SLA. Many CPUs (including the Raspberry Pi’s) have I2C communications built in. On the Raspberry Pi the I2C components are connected to I2C channel 1. Each I2C component has an Id: the DS1307 and DS1337 are on 0x68, the PCF8563 is on 0x51.
    There are device drivers for many of the available RTC chips (and other I2C chips), and you can use these if you want to, but they are not always as flexible as you might want. RGPIO provides another interface.
    To set the clock on PCF8563 from the system time, use:

    t=datetime.datetime.today()
    sec=toBCD(t.second)
    min=toBCD(t.minute)
    hour=toBCD(t.hour)
    date=toBCD(t.day)
    month=toBCD(t.month)
    year=toBCD(t.year % 100)
    day=t.weekday()
    rgpio.write("I1N0x51D2,%d,%d,%d,%d,%d,%d,%dW"%(sec,min,hour,date,day,month,year))
    To get the clock from PCF8563 and set the system time, use:
    import re, os
    rgpio.write("I1N0x51D2,7R")
    reI2C=re.compile('I2C *([^@]*)@([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*)')
    inLine=rgpio.readline()    # If using signal handling, this code goes there
    event=reI2C.search(inLine) # Use Regular expressions to unpack the input
    sec=int(g(3))&0x7f         # The PC8563 doesn't leave the unused bits zero
    min=int(g(4))&0x7f         # so we have to clear them ourselves
    hour=int(g(5))&0x1f
    date=int(g(7))&0x3f
    month=int(g(8))&0x1f
    year=int(g(9))+2000
    newDate=datetime.datetime(year,month,date,hour,min,sec)
    os.popen("date -s %s"%newDate)
    The DS13x7 series are similar but some detail differences. There are other I2C Real-time-clocks that can be used.

    I2C Thermometer (DS1621)

    The DS1621 needs initializing so it starts measuring the temperature. Thereafter the temperature can be read whenever required.
    rgpio.write('I1N0x48D0xEEW') # Command 0xEE starts conversion
    rgpio.write('I1N0x48D0xAA,2R') # Read 2 bytes to get the temperature
    inLine=rgpio.readline()
    event=reI2C.search(inLine) # Use Regular expressions to unpack the input
    temp=(event.group(3)+256*event.group(4))/256

    This temperature is accurate to 0.5C, and you can greater accuracy (about 1/16C) by reading from locations A8 and A9 and using a formula in the specification.
    Note: PiBoard has the DS1621 located next to the Timekeeping Crystal. Time Crystals typically have a fixed frequency that varies according to the temperature. This thermometer can be used to improve the accuracy of the clock.

    1-Wire Temperator sensor (DS18B20)

    The 1-Wire bus uses VDD, GND and DQ (the signal wire). (And some devices will work without the VDD connection.) Linux has 1-Wire modules available and you can use these, however, there are some problems with usability:

    1. The w1-therm module requires reading a file /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1-slave. The problem is that reading this file takes a long time (about 1 second) – this is because it has to initiate a temperature conversion, and must when way for the conversion to complete – which is between 94ms and 750ms, depending on the conversion accuracy. 1 second is much too long to wait for a system with a user interface.
    2. the w1-gpio module can also be used but writing to and reading from /sys/bus/w1/devices/28-xxxxxxxxxxxx/rw. The problem here is that if the sensor goes off line, file operations to the rw file hang.
    3. The built-in support for 1-wire is only available on pin 4. You can have multiple devices on that one pin, but a single pin is still a little limiting.

    RGPIO provides an alternative interface to 1-Wire devices.

    rgpio.write('P4U')      # Set pin 4's pullup - essential for 1-wire operation
    rgpio.write('O4P')      # Set pin 4 as 1-Wire pin
    rgpio.write('O0D')      # Tell RGPIO there is only one 1-Wire device
    rgpio.write('O0x44C')   # Start temperature convertion
    rgpio.write('O0xBEC2W') # Read the temperature
    reW1=re.compile('W1(:[0-9a-fA-F\-\.]*)* *(=*) *([^ ]*) *([^ ]*) *([^ ]*)')
    inLine=rgpio.readline() # Get the result
    event=reW1.search(inLine)
    temp=(int(event.group(3),16)+256*int(event.group(4),16))/16.0

    If you have more than one device on the bus, you can do the following:

    rgpio.write('P4U')      # Set pin 4's pullup - essential for 1-wire operation
    rgpio.write('O4P')      # Set pin 4 as 1-Wire pin
    rgpio.write('OL')       # List available devices
    inLine=rgpio.readline() # Get the first found device (blank if none)
    event=reW1.search(inLine)
    w1Device=event.group(1)[1:]  # Get the device name
                                 # format is ff-nnnnnnnnnnnn.cc
                                 # ff is family, n..n is number, cc is checksum
    w1Family=w1Device[0:2]
    w1NC=w1Device[3:7]
    w1NB=w1Device[7:11]
    w1NC=w2Device[11:15]
    w1Id="%x,0x%x,0x%x,0x%xD"
    rgpio.write("O%s"%w1Id)      # Set this device as the one to use
    rgpio.write('O0x44C')        # proceed as above
    ...
  • RGPIO – LCD Screens

    Common Character LCD Screens can be controlled with between 6 and 11 GPIO pins. RGPIO L-Mode contains simple support for a 6-pin or 7-pin solution. The 6 or 7 pins are D4,D5,D6,D7,EN,RS with an optional RW (for most uses RW can be hard-wired to GND).

    Initialize RGPIO with the ports:

    L<RS>,<RW>,<EN>,<D4>,<D5>,<D6>,<D7>D

    For the PiBoard, the initialization is (RW is hardwired to GND, and we use 0 for that pin:

    L17,0,11,22,23,24,25C

    The initialization will reset the the screen and clear it.
    Some screens need additional initialization which need to be sent to the screen separately.
    To write characters to the LCD:

    LDHello world

    To send special codes, use \Cxx or \Xxx where xx is a hexadecimal pair. \C for a command (RS=1) and \X for a character (RS=0). For example, to write to the screen’s top-left, prefix with \C80. (the beginning of row 2 is often \CA0 on a 2-line screen).
    Special positioning commands include \C01 (clear screen); \C02 (go to top left)
    When sending \ using a command shell or programming language that uses \ as an escape character, you will need to double it:

    ./p LD\\C80

    These screens usually have a built-in font with most of the characters you need, and with the ability to add your own characters at code positions 00-07 or 00-0F.
    To add an elipsis at character position 00 (6 blank rows, one row of alternate bits,1 blank row):

    LD\C40\x00\x00\x00\x00\x00\x00\x15\x00\xC80

    To print a short line of elipsis characters

    LD\x00\x00\x00\x00\xC80
  • RGPIO Port handling

    RGPIO commands for simple ports (P-mode) are:

    Command Comment Example
    P<port>+ Set port Hi (turn it on) P4+
    P<port>- Set port Lo (turn it off) P11-
    P<port>R Set port input and read value P0x12R
    P[n,]<port>L Report when port goes Lo
    Cache n events – default=0, report once only.
    P8L
    P[n,]<port>H Report when port goes Hi
    Cache n events – default=0, report once only.
    P2,8H
    P[n,]<port>L Report when port changes
    Cache n events – default=1; if n=0, once only.
    P0,8C
    P<port>l Set port Hi and time how long before it goes Lo P8l
    P<port>h Set port Lo and time how long before it goes Hi P8l
    P<port>U Turn on the internal pullup on the port P8U
    P<port>u Turn off the internal pullup on the port P8u
    Command Response
    P4R P4:on
    This reports the current state of the port.
    P8L E8:off 2013/03/15 14:21:35 (812355433)
    After an L command is sent, RGPIO watches for the port to change (using a hardware interrupt). The output will be available after the event has occured. The output can be pollled for, or a signal can be set. The output indicates the port, and state, the time and an incrementing timestamp for the event.
    P7h E7:on t=56000
    After an h command is sent, RGPIO sets the port Lo and then times how long (in nanoseconds) it takes for the port to go Hi. The output can be pollled for, or a signal can be set. The output indicates the port, and state, the elapsed time.
  • Raspberry Pi – RGPIO

    RGPIO is a Raspberry Pi installable module that provides a device driver interface GPIO (General Purpose Input/Output).

    Installing RGPIO

    Put rgpio.ko in /lib/modules/3.6.11+/kernel/drivers/rgpio
    Add rgpio to the list of modules to load at startup in /etc/modules
    Run depmod to update modprobe’s list of module dependencies.

    What RGPIO looks like

    RGPIO creates a device: /dev/rgpio

    This device is a character mode device that you can write text strings to and read text strings from. These strings tell RGPIO what to do, and tell you what RGPIO has found.

    For testing, it is useful to set up a small script – I call it p containing:

    [ -c /dev/rgpio ] && echo $@ > /dev/rgpio && cat - < /dev/rgpio

    ([ -c /dev/rgpio ] stops anything being written to a non-existent device.)

    Then you can use rgpio by entering commands as follows, and the output will be shown immediately:

    user@rpi:/home/me# ./p P14R
    RGPIO 14:on

    Help is built in:

    user@rpi:/home/me# ./p H
    {P|O|I|W|L}     set mode: PIN,One-wire,I2C,PWM,LCD
    ?               show Help
    pidS            Sending SIGIO to process when new output is added (pid=0 to cancel)
    {P|O|I|W|L}?    show Help for each mode
                    #,% can be decimal, 0x Hex or 0b Binary

    RGPIO Modes

    P mode: control the individual GPIO pins. This includes reading from and writing (Lo or Hi) to the pin, watching for changes in the read-state, and setting the internal pull-ups for each pin.

    W mode: PWM control – hardware (on Raspberry Pi, this is only on pin 18) or software modes.

    L mode: control a parallel character LCD device

    I mode: seek, query and control I2C devices

    O mode: seek, query and control 1-Wire devices

    Here are some Python examples using RGPIO on the PiBoard

  • SSH with keys (no passwords)

    Abstract: Instructions on setting up a Linux computer (server) so you can log in (from your client) with ssh without having to enter a password (using keys).

    Without a password, authentication is done with a key. To achieve password-less access, you need a key, and the computer you are logging in to needs to know about that key.
    A Key has two parts – private and public. You keep the private part, you give the public part to anyone who needs to know you are who you say you are.

    Generate a key on the client computer using:
    ssh-keygen -t rsa
    This will create two files:

    File Description
    ~/.ssh/id_rsa Your private key
    ~/.ssh/id_rsa.pub Your public key

    You need to add the contents of ~/.ssh/id_rsa.pub to the file ~/.ssh/authorized_keys on the server, logged in as the user you will be using:
    cat ~/.ssh/id_rsa.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
    This is the last time you will need to enter the password.
    From now on, you can just connect using:
    ssh user@server
    Or, if you are logged on to the client as the same username, just
    ssh-keygen -R ip-address