Saturday, April 11, 2015

Installing and using OpenWRT SDK on Ubuntu 64bit VM

What are we compiling for?

I have a couple of very hackable routers that I have flashed with latest version of OpenWRT -Barrier Breaker 14.07:

  1. TP-LINK TL-MR3020
    (Pocket Router with USB Port | Install Instructions | Image: openwrt-ar71xx-generic-tl-mr3020-v1-squashfs-factory.bin | I use this device to build my Weather Station)
  2. TP-LINK TL-MR3040
    (Just like MR3020 but has a building battery | Install Instructions | Image: openwrt-ar71xx-generic-tl-mr3040-v1-squashfs-factory.bin)
  3. Dragino MS14
    (OpenWRT wiki | Product Page | Firmware installation and instruction | Change LogFirmware Image)
All these routers have the AR9331 SoC in them which is MIPS and not MIPSel. MIPSel refers to a MIPS architecture using a little endian byte order. Since almost all MIPS microprocessors have the capability of operating with either little endian or big endian byte order, the term is used only for processors where little endian byte order has been pre-determined. So we want the mips toolchain and not mipsel toolchain. AR9331 is also used on Arduino Yún.

OpenWRT SDK is a collection of tools for cross compiling software for MIPS on an x86 PC

Installing it.

Here are the steps I executed to run OpenWRT on Ubuntu 64 bit. The latest version of OpenWRT SDK (corresponding to Barrier Breaker) needs a 64bit Linux to run on. I used a Ubuntu 64 bit virtual machine running on Windows to get this done

  1. Install VirtualBox
  2. Download Ubuntu 14.10 64-bit .vdi file from http://www.osboxes.org/ubuntu/
  3. Create a new 64 bit Ubuntu machine and while doing so, attach the downloaded and existing .VDI file to the Virtual Machine
  4. Get latest Ubuntu software lists:
    sudo apt-get update
  5. Installed guest editions, setup shared folders between Windows and Linux and set user group permissions (here is how)
  6. Install dependencies:
    sudo apt-get install build-essential subversion libncurses5-dev zlib1g-dev gawk gcc-multilib flex git-core gettext libssl-dev
    Otherwise you will get the following messages while compiling:
    Build dependency: Please install ncurses. (Missing libncurses.so or ncurses.h)
    Build dependency: Please install zlib. (Missing libz.so or zlib.h)
    Build dependency: Please install GNU awk.
    Build dependency: Please install git (git-core) v1.6.5 or later.
    Build dependency: Please install the subversion client.
  7. Download latest Barrier Breaker SDK which is a 64-bit SDK (i.e. MIPS cross compiler that runs on 64bit version of x86 PCs):
    wget http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2
  8. Extract 64-bit SDK:
    bzcat OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 | tar -xvf -
  9. Edit Config-build.in in ~/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2 :

Change:
config CCACHE
     bool
     default y
to
config CCACHE
     bool
     default ""

Hello World:

Create the source file and Makefile for the helloworld program. Create the appropriate folders and the following files in it (beware, there are two makefiles):

~/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/package/helloworld/src/helloworld.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
/****************
* Helloworld.c
* The most simplistic C program ever written.
* An epileptic monkey on crack could write this code.
*****************/

#include <stdio.h>

int main(void)
{
 printf("Hell! O' world, why won't my code compile?\n\n");
 return 0;
}

------------    ------------    ------------
~/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/package/helloworld/src/Makefile

1
2
3
4
5
6
7
8
9
# build helloworld executable when user executes "make"
helloworld: helloworld.o
 $(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
 $(CC) $(CFLAGS) -c helloworld.c

# remove object files and executable when user executes "make clean"
clean:
 rm *.o helloworld

------------    ------------    ------------

~/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/package/helloworld/Makefile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################

include $(TOPDIR)/rules.mk

# Name and release number of this package
PKG_NAME:=helloworld
PKG_VERSION:=1.0.0
PKG_RELEASE:=1

# This specifies the directory where we're going to build the program.
# The root build directory, $(BUILD_DIR), is by default the build_mipsel
# directory in your OpenWrt SDK directory
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)

include $(INCLUDE_DIR)/package.mk

# Specify package information for this program.
# The variables defined here should be self explanatory.
define Package/helloworld
 SECTION:=utils
 CATEGORY:=Utilities
 TITLE:=Helloworld -- prints a snarky message
endef

define Package/helloworld/description
 If you can't figure out what this program does,
 you're probably brain-dead and need immediate
 medical attention.
endef

# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
 mkdir -p $(PKG_BUILD_DIR)
 $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Build/Configure
  $(call Build/Configure/Default,--with-linux-headers=$(LINUX_DIR))
endef

# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one

# Specify where and how to install the program. Since we only have one file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
 $(INSTALL_DIR) $(1)/bin
 $(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef

# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

Compiling it, Installing it, Running it on your OpenWRT device:

  1. While in ~/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2 run:
    make V=99
  2. .ipkwill be placed in ~/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2/bin/ar71xx/packages/base
  3. Copy the ipk to Windows using shared folders
  4. Use WinSCP to copy helloworld_1.0.0-1_ar71xx.ipk to your OpenWRT router. (To know how to use WinSCP, go through the "Copying the executable to the router" section here)
  5. Connect to OpenWRT device using PuTTY and install the package:
    opkg install helloworld_1.0.0-1_ar71xx.ipk
  6. Execute:
    helloworld
Running helloworld

References:

  1. OpenWRT: Using the SDK http://wiki.openwrt.org/doc/howto/obtain.firmware.sdk 
  2. https://giovanni.wordpress.com/2011/01/23/how-to-cross-compile-a-c-program-for-openwrt/ 
  3. http://postwarrior.com/openwrt-please-fix-packagehelloworldmakefile-see-logspackagehelloworlddump-txt-for-details-solution/ 
  4. http://wiki.openwrt.org/doc/devel/packages 
  5. http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/OpenWrt-SDK-ar71xx-for-linux-x86_64-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 Will not work on 32bit Linux and needs 64 bit linux because the binaries in there are built to run on 64 bit. You will get an error like: find: syntax error: "(" unexpected

In case of 32-bit Ubuntu, use the older Attitude Adjustment SDK:

  • Download SDK:
    wget http://downloads.openwrt.org/attitude_adjustment/12.09/ar71xx/generic/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2
  • Extract SDK:
    bzcat OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2.tar.bz2   | tar -xvf -

Wednesday, October 29, 2014

Raspberry Pi + PiFace Digital : automatically detects loss of connectivity and reboots Gateway Router (cron job)

The problem:
I have a buggy 4G LTE router (a ZTE MF29 used with Airtel 4G here in Pune, India) which hangs around thrice a week causing loss of connectivity until it is rebooted manually by toggling the power button on its back. I cannot get the 4G LTE router exchanged - I am stuck with it. And there hasn't been any firmware updates, neither does it have any option for rebooting using its webpage based management console. So I have to climb up to the small loft where I have kept my network equipment and manually toggle the power switch on the back side of the router.

The problem is compounded by the fact that I have setup a wired LAN between 2 more apartments in my building complex. Three of us friends bought flats in this building together and we had the foresight to ask the builder to run lengths of CAT5e cables through the concealed wiring pipes between our houses. This allowed us to create our own LAN and have a common internet connection.
Now when the Gateway router threw a fit, and I wasn't at home, my friends (cum neighbor) had to use spare keys to get into my house, use a step ladder to access the loft and manually toggle the power on the Gateway router.

The Setting:
Here is the network diagram:

The network diagram of equipment and CAT5e wiring installed in our flats.

Photo of the network equipment installed at my house (A703).
This is the loft which houses the 4G Gateway router and the NAS
along with the two UPSes. The yellow box has the Rapsberry Pi that
is used to reboot the gateway router.

Photo of the network equipment installed at my house (A703).
This is the WiFi Router and the Network Switch. Cat5e cables from the
network switch go to the other two apartments (A602 and A202) through the
concealed wiring PVC pipes in the walls.


The Solution:
I automated the process of rebooting the gateway router using a pair of Raspberry Pi and PiFace Digital. All we need is a combination of a C program and a cron job which checks for internet connectivity (Ping google's Public DNS servers) every 5 minutes and power cycles the gateway router (ZTE MF29). The relay on the PiFace Digital is used to power off and power on the gateway router. I have a Seagate BlackArmor 110 NAS with a spare USB port stationed next to the gateway. I power the Raspberry Pi from that USB port - why add another wall wart if you can steal power from another device nearby.

Steps:
  1. Get a Raspberry Pi and an SD card. Flash the latest Raspbian image onto it and insert the card into the Raspberry Pi.
  2. Insert the PiFace Digital over your Raspberry Pi
  3. My PiFace had issues with the relays not getting toggled with their corresponding jumpers JP6 and JP5 mounted (they are mounted by default). So I had to remove those jumpers on the PiFace and route cables from pins 7 and 6 of the output screw terminals to the proper pins of JP5 and JP6 to switch the relays (see photo below)
    The default settings of JP5 and JP6 - both of them are mounted.
    The image is taken from Sparkfun which also sells PiFace
  4. The modification: unmounted JP5 and JP6 and routed wires to them from terminals 6 and 7
    Install a CAT5e cable between the Rasberry Pi and one of the local LAN ports of the gateway router.
  5. From your laptop, access the Raspberry Pi over SSH using PuTTY (login: pi, password: raspberry)
  6. Now we need to compile the C program for toggling the relay: execute the following commands (while you are in your home directory) to install required packages and get the code for the basic library for piface from Thomas MacPherson's Github repository. the following steps were taken from here.
    1. Install the C compiler:
      sudo apt-get install gcc automake libtool
    2. The PiFace uses the Pi's SPI interface with its own on-board I/O port extender. This means it uses 5 of the Pi's GPIO pins plus power, specifically 1 (+3.3V), 2 (+5V), 6 (0V), 19 (MOSI), 21 (MIS0), 23 (SCLK), 24 (CE0N) and 26 (CE1N). The SPI interface driver is included in the later Raspbian distributions but is not enabled by default so you will need to ensure that it loads each time your start your Pi. You do this by commenting out the blacklist spi-bcm2708 line in your /etc/modprobe.d/raspi-blacklist.conf file by inserting a # in column 1. Edit the file using the command below:
      sudo nano /etc/modprobe.d/raspi-blacklist.conf
    3. Download the source files - this will create a directory called piface:
      git clone https://github.com/thomasmacpherson/piface.git
    4. To install the C pfio library, move into the C directory, then call the setup scripts and then (as root) run the install command. ./autogen.sh takes a while to process with no console output, so don't think it's hung:
      cd piface/c/

      ./autogen.sh

      ./configure

      make

      sudo make install
    5. Runtime libraries have been installed and you need to refresh the library cache:
      sudo ldconfig
  7. Create a C file in your home directory (in ~ and not ~/piface) called toggle_relays.c and edit it using nano. Copy the following code into it:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    #include <stdio.h>
    #include <time.h>
    #include <libpiface-1.0/pfio.h>
    
    int main(void)
    {
     time_t time_elapsed_since_epoch;
     struct timespec required_time, remaining_time;
     pfio_init();
    
     printf("\nTurning ON Relays...");
    
            pfio_digital_write(7, 1);
            pfio_digital_write(6, 1);
    
     //Total delay: 5.5 seconds pfio_digital_write(6, 1);
     required_time.tv_sec = 5; //5.0 seconds pfio_deinit();
     required_time.tv_nsec = 500 * 1000 * 1000L; //0.5 seconds}
    
     printf("Done!");
     fflush(stdout);
    
     if (nanosleep(&required_time, &remaining_time) < 0) {
      printf("Nano sleep system call failed \n");
      return -1;
     }
    
     printf("\nTurning OFF Relays...");
            pfio_digital_write(7, 0);
            pfio_digital_write(6, 0);
     printf("Done!");
    
     return 0;
    }
    
  8. Compile the C file.
    gcc -L/usr/local/lib/ -lpiface-1.0 -o toggle_relays toggle_relays.c
  9. Execute it like so and check to heard the sound of the relays being toggled.
    ./toggle_relays
  10. Route the mains power to the gateway router's wall wart through one of the two relays on the PiFace Digital (see diagram below)
    Wiring the AC mains for powering the gateway router through one of the relays of PiFace
  11. Create a file that contains the script to ping and check if the internet connection on the gateway is working. Name this file as monitor_gateway.sh and copy the following code into it:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # Send two Ping request packets and 4 seconds timeout
    return_value=$(ping -c 2 -W 4 www.google.com)
    return_value=$?
    # Return code for ping are taken from
    # ./OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/include/sysexits.h
     
        case $return_value in
            0)  #Ping reply received
                logger "Ping result: ONLINE"
                ;;
            1)  #Ping sent but reply was not received
                logger "Ping result: OFFLINE (Ping reply not received), Rebooting Airtel LTE Gateway..."
                /home/pi/toggle_relays
                ;;
            *)  #Error
                logger "Ping result: OFFLINE (ping return code: $return_value), Rebooting Airtel LTE Gateway..."
                /home/pi/toggle_relays
                ;;
        esac
    
  12. Mark it as executable
    sudo chmod +x monitor_gateway.sh
  13. To add the script to the crontab to be executed every 5 minutes, execute:
    crontab -e
  14. Add the following line to the end of the file and save it (To save, press Ctrl+O followed by Ctrl+X):
    */5 * * * * /home/pi/monitor_gateway.sh
  15. After 30 mins, check the syslog using the following command. If the router hangs, it will be rebooted and you will see appropriate messages logged there.
    cat /var/log/messages

syslog (cat /var/log/messages) shows the cron job working beautifully
Whenever there is an interruption in service, the executable for toggling the relays is called.
PS. I recommend that you also install RaspControl to allow your Raspberry Pi to be monitored using a webpage based console. 

Sunday, October 5, 2014

Contest Buzzer System (by re-purposing Sparkfun's Arduino based 7 Segment Serial Display)

Quiz contest's are one of the most fun ways to learn things in a group. Throw in a buzzer system and you have sense of urgency, excitement and teamwork surging through the various teams.

Couple of years ago (in 2006) - before Arduino or microcontrollers - me and Ranjit assembled a parallel port based buzzer system. We had 5 buzzers for upto 5 teams - we used the 5 Status pins (nACK, Busy, Paper Out, Select and nError) of the parallel port to read the status of the switches. The project lasted us a long time - till the time they stopped equipping laptops with parallel ports!
Some of the features of this system were:

  • Use of bell switches as buzzers. the spring action, brings the switches back to off position.
  • Use of microphone coaxial cables - these sturdy yet flexible cables are designed to be stepped upon and still last long!
  • Use of RCA connector to attach and detach buzzers - essential because we were using coaxial cables
  • Pull up resistors to make sure the status pins idled at logic high when the switch weren't pressed. Pressing the switch grounded the corresponding status pin.
  • The "Always on top" Visual Basic application which changed color and showed which team pressed the buzzer first - This went well with our quiz sessions hosted on a digital projecter using Power Point slide - the audience could see on the screen who pressed the buzzer first!


The hardware - made of microphone coax cables and bell switches.

The Visual Basic application that read the status pins.

The buzzer application went well with our powerpoint slide deck - it always stayed on top.


Fast forward to 2014 - We now have Arduino and portable power banks! but I am lazier than ever before, so instead of assembling my own circuit, I picked up Sparkfun's 7 Segment Serial Display Widget which is simply an Arduino enabled AVR microcontroller (ATmega328) with a 4 digit 7 segment display. The microcontroller's firmware allows the display to be controlled via its SPI/UART/I2C interface.

And so connected bell switches to the SPI/I2C pins of the 7 segment serial display using them in GPIO mode and replaced the firmware with an Arduino sketch of my own and I had a standalone buzzer system ready. Oh and I added a hacked portable charger (a.k.a, power bank) to make the system really stand alone. The 7 segment display shows the time sequence in which the buzzers were pressed. The system can be reset using the reset bell switch which is connected to the reset pin of the Arduino microcontroller present on the 7 Segment Serial Display widget.

Sparkfun's 7-segment serial display widget

Here are the features of the system:
  • Sturdy Hardware: Bell switches, microphone coax cables and RCA connectors - all the goodness carried over from the previous parallel port based system. Used cable ties to act as cable stress relief around the bell switches.
  • Avoiding Confusion: Used printed numbered labels to mark the RCA sockets and switches. Used spray paint on the beginning and end of coax cables to help identify the RCA plugs corresponding to various bell switches in case the cables got entangled.
  • Pin Map: 4 + 1 = 5 bell switches for 4 teams and 1 for the quiz master to reset the system before every question. The pin connection
    Buzzer 1 - SDI/MOSI/PB3/PCINT3 - Digital I/O 11
    Buzzer 2 - SCK/PB5/PCINT5 - Digital I/O 13
    Buzzer 3 - SS#/PB2/PCINT2 - Digital I/O 10
    Buzzer 4 - SDO/MISO/PB4/PCINT4 - Digital I/O 12
  • Pullup resistors: 8.2 kilo ohms pull up resistors on 4 GPIOs and the RESET pin to prevent false triggering due to electrical noise. Pressing the bell switches grounds the corresponding GPIO pin which is registered by the microcontroller as a falling edge pin change interrupt. One might think that such long lengths of microphone coax cables would cause false triggering
  • Arduino Libraries: 3 arduino libraries are used:
    • TimerOne - Library to use the Timer1 in interrupt mode - used to refresh the 7-segment display.
    • SevSeg - Library used to drive the 7-segment display.
    • PinChangeInt - Use interrupts to detect when switches are pressed
  • Interrupt Driven: The software does everything in interrupts: The 7 segment display is refreshed every 0.01 seconds using Timer1 and the buzzer inputs are registered using Pin Change interrupts. The main loop lies idle.
  • No debounce required for the switches: Since the pin change interrupts are used.
  • Non standard Arduino pins used: If you refer to the schematic of 7 Segment Serial Display, you will note that pins PB6 and PB7 are used. You will need to modify C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard\pins_arduino.h to access these pins from withing Arduino IDE. These pins are used to light up segment E and the decimal points. Refer the source code comments on how to make modifications to pins_arduino.h.
  • Power Source: A hacked power bank is used to power the system anywhere. The system draws just a few milliamperes of current which is about a hundred times less than what mobile phones used while being charged from the power bank. So the power bank had to be hacked lest it thinks that it is not being used for charging and automatically turns off. Here is how to hack a power bank to make sure it doesn't turn off while powering a low power DI?y project - it involves replacing the current sense resistor. The power bank supplies 5 volts to the 7-Segment Serial
  • Housing: A small easily procurable lunch box.
  • Program downloading: The 7-Segment Serial Display can be re-programmed using Sparkfun's FTDI Basic Breakout
Mounting the 7-Segment Serial Display widget and the 6 way RCA block
in the lid of the lunch box.

The hacked iBall power bank supplies 5 volts to the Arduino widget.

Wiring it all up - note the JST connector, the 8.2Kohms pullup resistors and the
blu-tack used to hold the wires in place

Nylon Cable ties used as stress relief near the ends of the bell switches.


The hacked power bank supplying power to the arduino widget.


The complete system.


The power bank lies at the bottom inside the lunch box.

Using FTDI basic breakout to download a new sketch into 7 Segment Serial Display

And finally, here is the Arduino sketch that goes into the 7-Segment Serial Display widget:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// This code uses the following libraries each of which need to be downloaded to C:\Program Files (x86)\Arduino\libraries
// and placed in a folder of its own.
// 1. TimerOne https://code.google.com/p/arduino-timerone/downloads/list
// 2. SevSeg https://github.com/sparkfun/SevSeg
// 3. PinChangeInt https://code.google.com/p/arduino-pinchangeint/downloads/list

// Add non standard arduino pins: 
// PB6 as digital pin 20 to pins_arduino.h
// PB7 as digital pin 21 to pins_arduino.h
// To do that
// 1. Open C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard\pins_arduino.h with admin rights
// 2. Append PB, PB to digital_pin_to_port_PGM[]
// 3. Append _BV(6), _BV(7) to digital_pin_to_bit_mask_PGM[]


//My buzzer Pinout
//RCA Socket 1 - Red - SDI or MOSI or PB3 or PCINT3 or D 11
//RCA Socket 2 - Yellow - SCK or PB5 or PCINT5 or D 13
//RCA Socket 3 - Brown - SS# or PB2 or PCINT2 or D 10
//RCA Socket 4 - Green - SDO - MISO or PB4 or PCINT4 or D 12

#include "TimerOne.h"
#include "SevSeg.h"
#include "PinChangeInt.h"

SevSeg myDisplay; //Create an instance of the object

#define RCASOCK01 11
#define RCASOCK02 13
#define RCASOCK03 10
#define RCASOCK04 12

// Struct for 4-digit, 7-segment display 
// Stores display value (digits),  decimal status (decimals) for each digit, and cursor for overall display
struct display
{
  char digits[4];
  unsigned char decimals;
  unsigned char cursor;
} 
display;  // displays be displays

void Timer1ISR() //use Timer Interrupt to refresh the display
{
  myDisplay.DisplayString(display.digits, display.decimals);
}

void buzzpressed()
{
  uint8_t latest_interrupted_pin;
  latest_interrupted_pin=PCintPort::arduinoPin;
  if(display.cursor < 4)
  {
    switch(latest_interrupted_pin) {
      case RCASOCK01: display.digits[display.cursor] = '1'; break;
      case RCASOCK02: display.digits[display.cursor] = '2'; break;
      case RCASOCK03: display.digits[display.cursor] = '3'; break;
      case RCASOCK04: display.digits[display.cursor] = '4'; break;
      default: break;
    }
    PCintPort::detachInterrupt(latest_interrupted_pin);
    display.cursor++;
  }
}


void setup()
{  
  myDisplay.SetBrightness(100); //Set the display to 100% bright

  // Set the initial state of displays and decimals 'x' =  off
  display.digits[0] = '-';
  display.digits[1] = '-';
  display.digits[2] = '-';
  display.digits[3] = '-';
  display.decimals = 0x00;  // Turn all decimals off
  display.cursor = 0;  // Set cursor to first (left-most) digit

  int digit1 = 16; // DIG1 = A2/16 (PC2)
  int digit2 = 17; // DIG2 = A3/17 (PC3)
  int digit3 = 3;  // DIG3 = D3 (PD3)
  int digit4 = 4;  // DIG4 = D4 (PD4)

  //Declare what pins are connected to the segments
  int segA = 8;  // A = D8 (PB0)
  int segB = 14; // B = A0 (PC0)
  int segC = 6;  // C = D6 (PD6), shares a pin with colon cathode
  int segD = A1; // D = A1 (PC1)
  int segE = 21; // E = PB7 (not a standard Arduino pin)
  int segF = 7;  // F = D7 (PD6), shares a pin with apostrophe cathode
  int segG = 5;  // G = D5 (PD5)
  int segDP= 20; //DP = PB6 (not a standard Arduino pin)

  int digitColon = 2; // COL-A = D2 (PD2) (anode of colon)
  int segmentColon = 6; // COL-C = D6 (PD6) (cathode of colon), shares a pin with C
  int digitApostrophe = 9; // APOS-A = D9 (PB1) (anode of apostrophe)
  int segmentApostrophe = 7; // APOS-C = D7 (PD7) (cathode of apostrophe), shares a pin with F

  int numberOfDigits = 4; //Do you have a 2 or 4 digit display?

  int displayType = COMMON_ANODE; //SparkFun 10mm height displays are common anode

  //Initialize the SevSeg library with all the pins needed for this type of display
  myDisplay.Begin(displayType, numberOfDigits, 
  digit1, digit2, digit3, digit4, 
  digitColon, digitApostrophe, 
  segA, segB, segC, segD, segE, segF, segG, 
  segDP,
  segmentColon, segmentApostrophe);
  
  Timer1.initialize(10000); // set a timer of length 10000 microseconds (or 0.01 sec)
  Timer1.attachInterrupt( Timer1ISR ); // attach the service routine here
  
  pinMode(RCASOCK01, INPUT);
  digitalWrite(RCASOCK01, HIGH);
  PCintPort::attachInterrupt(RCASOCK01, &buzzpressed, FALLING);  // add more attachInterrupt code as required

  pinMode(RCASOCK02, INPUT);
  digitalWrite(RCASOCK02, HIGH);
  PCintPort::attachInterrupt(RCASOCK02, &buzzpressed, FALLING);  // add more attachInterrupt code as required

  pinMode(RCASOCK03, INPUT);
  digitalWrite(RCASOCK03, HIGH);
  PCintPort::attachInterrupt(RCASOCK03, &buzzpressed, FALLING);  // add more attachInterrupt code as required

  pinMode(RCASOCK04, INPUT);
  digitalWrite(RCASOCK04, HIGH);
  PCintPort::attachInterrupt(RCASOCK04, &buzzpressed, FALLING);  // add more attachInterrupt code as required
  
  interrupts();  // Turn interrupts on, and les' go
}

void loop()
{
}

And the buzzer in action in my classroom! Excitement!



Saturday, October 4, 2014

Replacing the current sense resistor in Portable Chargers/Power Banks for powering low power DIY projects.

Mobile Phone/Tablet charging Power Banks can be used to power so many DIY projects. But if your project is low power and draws very little current, the power bank will auto turn off after a few seconds thinking that it isn't being used to charge gadget (because the current draw is very low). I need to figure out a way to hack one and locate and replace the current sense resistor with one of a higher value. Here is how I did it.

  1. This told me that the resistor would be located on the return path (ground path) of the output USB connector. I promptly located the R100 SMD resistor in my iBall portable charger. The fact that this resistor was fatter (higher power dissipation rating) and that it was 0.1 ohms in value confirmed that this had to be the current sense resistor
    Original 0.1 ohms current sense resistor
  2. I measured the voltage across it when nothing was connected. It was zero volts and the portable charger turned off in a few seconds when nothing was being charged.
  3. I then turned on the charger back again and this time measured the voltage across it while charging my galaxy tab. The voltage across the resistor was 0.07 volts
  4. I now disconnected the galaxy tab and tried powering my arduino circuit which I knew for sure was drawing very very less current because the charger used to keep turning off after a few seconds. The low current drawn by Arduino didn't cause appreciable voltage drop across the current sense resistor and so the charger decided to turn off. When I measured the voltage across the 0.1 ohm current sense resistor while powering the arduino circuit, the multimeter didnt even register a reading - even on the lowest range.
  5. On a hunch I replaced the 0.1 ohm SMD resistor with a 1/4 watt 10 ohm thru hole resistor. I figured, that USB ports provide upto 500 mA of current and so the phone must be drawing that much current when charging while my arduino circuit would only be drawing a few mA of current (maybe 5 mA coz it had a 7 segment display on it). So i increased the value of the current sense 100 times. Also 10 ohms was the least value I had in my resistor collection.
    Replaced the 0.1 ohm resistor with a 10 ohm one
  6. After replacing the resistor, I measured the voltage across it while powering the Arduino. It was now 0.07 volts and now the power bank wouldn't turn off!!!
  7. Awesomeness!


    OMG it's alive! and doesn't auto turn off now!
Update 2014-10-26
The latest portable chargers like the ones in the picture below now longer have any kind of switch on them. They just have a two USB connectors on them - one to charge the cell phones (output port) and other one to charge the charger themselves (input port). The chargers automatically detect if something is plugged into their output connector and automatically turn on. These chargers don't care about how much current you are drawing - any amount is okay - as long as there is a cable connected from their output port to something that can draw some current from them.

Left: Minix S5 10,000 mAh (INR 1999 at flipkart)
Right Portronics Pico II (INR 704 at flipkart)

Monday, July 7, 2014

Using Raspberry Pi with a Digital Projector in a Teach For India classroom

I am a TeachForIndia fellow now and have been teaching at a low income school in Pune. The school is named Kilbil Public School (kilbil is Marathi for "chattering") and I teach Maths and Geography to 3 classes of 7th Graders. We are a team of 3 TFI Fellows, the other two teach English, Science and History/Civics. We have a facebook page here. The first thing I did is to ask all of my close friend to contribute money to help me buy a digital projector. The idea was that if we are  to use Inquiry-based learning for Geography, History/Civics and Science, we would need to hook the students attention by showing them images and youtube videos. So here's what I got for my setup:

Hardware:

  1. BenQ MS521p DLP Projector with HDMI Input and Analog Stereo output. This cost Rs. 30,000 from Flipkart. My friends contributed hard cash (and Flipkart vouchers - which cannot be bought using international credit cards) for this. Recently I have ordered another projector, so now the two new BenQ projectors together with an existing old one will allow us to deliver awesome instructions simultaneously all three section of 7th grade. The projector decodes the digital audio stream from HDMI and makes it available as analog audio for feeding it into speakers.
  2. Raspberry Pi with case - I had this one lying around. I was scared that I would end up breaking my laptop if I carried it to school everyday. So I thought of using a Raspberry Pi in the classroom instead to play MP4 videos downloaded from YouTube and to teach my kids programming using Scratch
  3. SanDisk SD Card (16Gb 45 MB/s) - I had been using this with my DSLR, I figured that I could put it to better use with my RPi since I was anyway using my Nexus 5 to take photos of my kids instead of the DSLR.
  4. Logitech MK220 Wireless Keyboard and Mouse Combo - I had bought this long ago for general use with my laptop or RPi. Available from Flipkart.
  5. Netgear Wireless USB Micro WNA1000M - USB WiFi Dongle for use with RPi. Buy from Flipkart. This was so that I could access internet using my cellphone as a WiFi hotspot.
  6. HP Stereo Speakers S3000 bought from Croma for Rs. 899, they are available for less here on Flipkart. These can be powered from USB
  7. Nextech Dual USB Charger from Croma for powering the RPi and Speakers simultaneously
  8. A really thin and short HDMI Cable to connect the RPi to the projector
  9. A Micro USB Cable for powering the RPi
  10. A powerstrip to act as an extension board with its short cable replaced with a longer one.
  11. A Square Bucket and Mug to carry everything in - the convenience of carrying things in a bucket cannot be stressed enough!


The dual port charger. This charger has a Europlug.
I needed to create an extension wire which was equal in length to
the power cable of the projector.
See image below.
I used cable ties to tie the charger to the projector power cable.
The extension wire made for the dual port charger made it convenient
to twist the two wires around each other.
Now days you get powerstrip with USB ports builtin for
charging personal communication devices.
If you can get your hands on one of these then perhaps you may
not need a separate dual port USB charger for powering the speakers and the RPi.
Just make sure that the built in charger of the powerstrip is able
to provide enough current to the Speakers and RPi.


The backside of BenQ MS521p projector.
The wires going into it are: power cable for powering the projector,
3.5mm stereo lead from speakers and the thin HDMI cable from the Raspberry Pi.

The speakers

Raspberry Pi with the Logitech wireless mouse next to it.
The wires attached to the RPi are the mircoUSB cable for powering it
and the HDMI cable which goes to the projector.

RPi with the SD card containing Raspbian OS

RPi with the wireless mouse and keyboard


The final setup. Note how I have twisted the cables around each other
so that only three bunches of cable are coming out of the projector.
And this is how I carry the hardware to school everyday.
In a square bucket.
Its makes it easy to carry all the stuff to school and back.
And it takes under a minute to setup or store (no zips to open like in a bag).


Software:
  1. Raspbian as OS for RPi - Download the SD card image from here and write the image to the SD Card
  2. Youtube Downloader HD for Windows - Help you download MP4 versions of videos from youtube. Get from here.
  3. Paragon ExtFS for Windows - When you prepare an SD card for RPi by writing the Raspbian image to it, the card cannot be read by Windows because the card uses the ext3/ext4 files system. This software will allow you to read files from and copy files to the Raspbian SD card from within Windows. Make sure to copy your MP4 files to /home/pi folder. This software is essential in two scenarios - 1) when you do not have any USB port left because the two that RPi has have been used up and so you cannot connect a FAT32 formatted USB flash drive containing the Youtube videos. 2) RPi might not be able to provide enough power via the USB port and it might hang when you do connect a USB flash drive. So its just better to copy the videos to the SD card instead of playing them from a FAT32 formatted USB flash drive. (FAT32 flash drive can be read to and written from Windows as well as Linux). You can play MP4 and videos in other format using omxplayer. Issue the command at the terminal: "omxplayer /home/pi/abc.mp4" 

Issues:
  1. HDMI settings - To make the Raspberry Pi's audio behave properly with BenQ MS521p projector, you will need to make a minor change in \boot\config.txt. You need to uncomment (remove the #) the line hdmi_drive=2. More details here. The BenQ projector will decode the audio from HDMI and make the analog stream available via the 3.5mm stereo jack marked "Audio Out" on its back side. Edit the config.txt file on Raspberry Pi itself using the nano editor. Issue the command 
  2. USB Extension for Logitech Wireless Receiver - Raspberry Pi has two USB port. If you stick a WiFi USB dongle in one and the keyboard receiver dongle in another (these are very close to each other), the WiFi dongle's signals will interfere with the keyboard receiver and the keyboard and mouse will no longer work. To prevent this, just use a USB extension cable with the keyboard receiver to create some distance between the two dongles.
What doesn't work: inserting the Keyboard receiver dongle next to the
WiFi dongle will prevent it from receiving signals

What works: use a short extension cable to move the keyboard
receiver dongle away from the WiFi Dongle




How the setup looks like in the class.
The image is being projected onto a blackboard.
You actually do not need a white screen.

So I realized that I could optimize the setup time even further if
mounted everything on a piece of plywood and carry it as it is to school.
This would also prevent breakage because now all the cables are tucked
away safely and there is no way any kid would trip on it.
And the kids would be more than happy to carry the whole thing
between their class and my car - helps build ownership towards their own classroom.