Monday 27 August 2012

LCD, BBC and JSON

Building on the previous LCD example, I tried to connect to the BBC's JSON API for schedule information, and display some of that data.


The code can be found here - https://github.com/gitgrimbo/raspi-fun/tree/master/c/lcd-bbc-json.

I used Jansson for the JSON parsing, and libcurl for the downloading.

Brain dump of trying to install Jansson [JSON library for c]

https://github.com/akheron/jansson

Instructions are:

$ ./configure
$ make
$ make install

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

This did not work, so I tried the other suggestion, to run "autoreconf -i":

pi@raspberrypi /mnt/nas-backup/apps/raspi/jansson $ sudo autoreconf -i
configure.ac:11: error: possibly undefined macro: AC_PROG_LIBTOOL
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1

Error.

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

No joy, Googling found this:
    http://forum.xbmc.org/showthread.php?tid=63780

"hi try apt-get install libtool an run ./bootstrap ./configure again."

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

So try installing libtool:

pi@raspberrypi /mnt/nas-backup/apps/raspi/jansson $ sudo apt-get install libtool
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libltdl-dev libltdl7
Suggested packages:
  libtool-doc gfortran fortran95-compiler gcj
The following NEW packages will be installed:
  libltdl-dev libltdl7 libtool
0 upgraded, 3 newly installed, 0 to remove and 17 not upgraded.
Need to get 1,170 kB of archives.
After this operation, 2,756 kB of additional disk space will be used.
Do you want to continue [Y/n]? Y
Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libltdl7 armhf 2.4.2-1.1 [349 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libltdl-dev armhf 2.4.2-1.1 [203 kB]
Get:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libtool armhf 2.4.2-1.1 [618 kB]
Fetched 1,170 kB in 6s (187 kB/s)
Selecting previously unselected package libltdl7:armhf.
(Reading database ... 57093 files and directories currently installed.)
Unpacking libltdl7:armhf (from .../libltdl7_2.4.2-1.1_armhf.deb) ...
Selecting previously unselected package libltdl-dev:armhf.
Unpacking libltdl-dev:armhf (from .../libltdl-dev_2.4.2-1.1_armhf.deb) ...
Selecting previously unselected package libtool.
Unpacking libtool (from .../libtool_2.4.2-1.1_armhf.deb) ...
Processing triggers for man-db ...
Setting up libltdl7:armhf (2.4.2-1.1) ...
Setting up libltdl-dev:armhf (2.4.2-1.1) ...
Setting up libtool (2.4.2-1.1) ...

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

And try "autoreconf -i" again:

pi@raspberrypi /mnt/nas-backup/apps/raspi/jansson $ sudo autoreconf -i
libtoolize: putting auxiliary files in `.'.
libtoolize: copying file `./ltmain.sh'
libtoolize: Consider adding `AC_CONFIG_MACRO_DIR([m4])' to configure.ac and
libtoolize: rerunning libtoolize, to keep the correct libtool macros in-tree.
libtoolize: Consider adding `-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
configure.ac:11: installing `./config.guess'
configure.ac:11: installing `./config.sub'
configure.ac:4: installing `./install-sh'
configure.ac:4: installing `./missing'
src/Makefile.am: installing `./depcomp'

pi@raspberrypi /mnt/nas-backup/apps/raspi/jansson $ ll
total 17408
drwxr-xr-x 1 root root      0 Aug 26 20:45 .
drwxr-xr-x 1 root root      0 Aug 26 20:23 ..
-rwxr-xr-x 1 root root 343810 Aug 26 20:43 aclocal.m4
drwxr-xr-x 1 root root      0 Aug 26 20:44 autom4te.cache
-rwxr-xr-x 1 root root  11735 Aug 11 10:45 CHANGES
-rwxr-xr-x 1 root root  44826 Aug 26 20:45 config.guess
-rwxr-xr-x 1 root root   2397 Aug 26 20:45 config.h.in
-rwxr-xr-x 1 root root  35543 Aug 26 20:45 config.sub
-rwxr-xr-x 1 root root 415106 Aug 26 20:45 configure
-rwxr-xr-x 1 root root   1246 Aug 11 10:45 configure.ac
-rwxr-xr-x 1 root root  20899 Aug 26 20:45 depcomp
drwxr-xr-x 1 root root      0 Aug 26 20:45 doc
-rwxr-xr-x 1 root root    253 Aug 11 10:45 .gitignore
-rwxr-xr-x 1 root root  13998 Aug 26 20:45 install-sh
-rwxr-xr-x 1 root root    244 Aug 11 10:45 jansson.pc.in
-rwxr-xr-x 1 root root   1081 Aug 11 10:45 LICENSE
-rwxr-xr-x 1 root root 283670 Aug 26 20:44 ltmain.sh
-rwxr-xr-x 1 root root    273 Aug 11 10:45 Makefile.am
-rwxr-xr-x 1 root root  26456 Aug 26 20:45 Makefile.in
-rwxr-xr-x 1 root root  10346 Aug 26 20:45 missing
-rwxr-xr-x 1 root root   1322 Aug 11 10:45 README.rst
drwxr-xr-x 1 root root      0 Aug 26 20:45 src
drwxr-xr-x 1 root root      0 Aug 26 20:45 test
drwxr-xr-x 1 root root      0 Aug 26 20:22 win32

Worked!

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

Compiling the Jansson example:
    http://www.digip.org/jansson/doc/2.3/tutorial.html
    http://www.digip.org/jansson/doc/2.3/_downloads/github_commits.c

Requires curl/curl.h!

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

So try and install curl dev tools.

pi@raspberrypi ~ $ sudo apt-get install libcurl4-gnutls-dev
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  comerr-dev krb5-multidev libcomerr2 libcurl3-gnutls libgcrypt11-dev libgnutls-dev libgnutls-openssl27 libgnutlsxx27 libgpg-error-dev libgssapi-krb5-2 libgssrpc4
  libidn11-dev libk5crypto3 libkadm5clnt-mit8 libkadm5srv-mit8 libkdb5-6 libkrb5-3 libkrb5-dev libkrb5support0 libldap-2.4-2 libldap2-dev libp11-kit-dev librtmp-dev
  libssh2-1 libssh2-1-dev libtasn1-3-dev
Suggested packages:
  doc-base krb5-doc libgcrypt11-doc gnutls-doc krb5-user
The following NEW packages will be installed:
  comerr-dev krb5-multidev libcurl3-gnutls libcurl4-gnutls-dev libgcrypt11-dev libgnutls-dev libgnutls-openssl27 libgnutlsxx27 libgpg-error-dev libgssrpc4 libidn11-dev
  libkadm5clnt-mit8 libkadm5srv-mit8 libkdb5-6 libkrb5-dev libldap2-dev libp11-kit-dev librtmp-dev libssh2-1-dev libtasn1-3-dev
The following packages will be upgraded:
  libcomerr2 libgssapi-krb5-2 libk5crypto3 libkrb5-3 libkrb5support0 libldap-2.4-2 libssh2-1
7 upgraded, 20 newly installed, 0 to remove and 100 not upgraded.
Need to get 2,375 kB/6,657 kB of archives.
After this operation, 11.8 MB of additional disk space will be used.
Do you want to continue [Y/n]? Y

[installed]

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

Proof:

pi@raspberrypi ~ $ sudo find / -name curl.h
/usr/include/curl/curl.h

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

No idea where the headers and libraries live. This command was the only way I could get it to work.
Specifying the full path to the jansson.h (-I) and libjansson.so (-L) files.

I found this site quite useful:
    http://www.rapidtables.com/code/linux/gcc.htm

pi@raspberrypi /mnt/nas-backup/apps/raspi/jansson $ sudo gcc -o github_commits github_commits.c -lcurl -I/mnt/nas-backup/apps/raspi/jansson/src -L/mnt/nas-backup/apps/raspi/jansson/src/.libs -ljansson

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

Again, running was a pain as I was getting this:

pi@raspberrypi /mnt/nas-backup/apps/raspi/jansson $ ./github_commits https://github.com/akheron/jansson.git
./github_commits: error while loading shared libraries: libjansson.so.4: cannot open shared object file: No such file or directory

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

So I had to revert to a hack documented here:

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

pi@raspberrypi /mnt/nas-backup/apps/raspi/jansson $ LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH:/mnt/nas-backup/apps/raspi/jansson/src/.libs/ ./github_commits akheron jansson
0dac319b CHANGES entry for #88
0b871a11 Merge branch '2.3'
8176527f fix check-exports test on ppc64
ec7bb71d Add an initial CHANGES entry for v2.4
b6a1d8cf Add json_boolean() macro
52924288 Merge branch '2.3'
7892ecce Fix a small error in tutorial
a501a396 Document the JSON_ESCAPE_SLASH encoding flag
37bc3bbf Tweak slash escaping
2d46ea06 Merge branch 'fix-slash' of git://github.com/jrbasso/jansson
b217cd66 Created flag to dump escaping slash
a0c262d0 Escaping the slash when dump
6ce273e2 Merge branch '2.3'
f62b1f5d Bugfix: must cast void* to do pointer math
2b87fdcb Bugfix: Changed all use of ssize_t to size_t.
c0139681 Update the documentation to mention Visual Studio 2010 support
9d6f9511 Merge pull request #74 from rogerz/contrib
a79f64e1 Fix build warnings under MinGW
7ca783c3 Add vs2010 solution and project
ff0c05b8 Support building on Windows
4601bf71 Merge branch '2.3'
c7f86abf fix slight typo in json_pack apiref example
f309e303 Merge branch '2.3'
233574e8 fix slight typo in json_pack apiref example
4a6939ef Merge branch '2.3'
42bc7a3c Check for missing args and envvars in run-tests.sh
abdb8d99 Merge branch '2.3'
ff6e6ee2 jansson 2.3.1
8b2bfd55 Merge branch '2.3'
e46b912f Fix tutorial to use Github API v3

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

Then I actually did what the instructions said!

http://www.digip.org/jansson/doc/dev/gettingstarted.html

That is, I ran the following commands. All as sudo so that my Pi can write to my NAS share.

./configure
make
make check
make install

Did a check for the library, and found it:

pi@raspberrypi ~ $ sudo find / -name libjansson.so
/mnt/nas-backup/apps/raspi/jansson/src/.libs/libjansson.so
/usr/local/lib/libjansson.so

But still didn't work. Apparently I need to do more.

This SO link mentions "/etc/ld.so.conf", so let's have a look:

http://stackoverflow.com/questions/4743233/is-usr-local-lib-searched-for-shared-libraries

pi@raspberrypi ~ $ cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

Ok, one include. Let's follow that up:

pi@raspberrypi ~ $ ll /etc/ld.so.conf.d
total 20
drwxr-xr-x  2 root root 4096 Jul 15 20:06 .
drwxr-xr-x 88 root root 4096 Aug 27 17:58 ..
-rw-r--r--  1 root root   12 Jul 15 18:12 00-vmcs.conf
-rw-r--r--  1 root root   74 Jun 10 06:07 arm-linux-gnueabihf.conf
-rw-r--r--  1 root root   44 Jun 10 01:36 libc.conf

And again:

pi@raspberrypi ~ $ cat /etc/ld.so.conf.d/libc.conf
# libc default configuration
/usr/local/lib

Ok, so looks like "libjansson.so" has been copied to the right place. Why isn't it working?

More Googling, and now let's try reloading (?) the libraries:

pi@raspberrypi ~ $ ldconfig
ldconfig: Can't create temporary cache file /etc/ld.so.cache~: Permission denied

Nope, try sudo:

pi@raspberrypi ~ $ sudo ldconfig

And...

pi@raspberrypi /mnt/nas-backup/apps/raspi/raspi-fun/c/lcd-bbc-json/grimbo $ sudo ./bbctests
grimbo_url_url_to_buffer(http://www.bbc.co.uk/tv/programmes/genres/factual/scienceandnature/schedules/upcoming.json)
Using temp file /tmp/grimbo_url_R6v1wk
File descriptor 3
grimbo_url_url_to_file_descriptor(http://www.bbc.co.uk/tv/programmes/genres/factual/scienceandnature/schedules/upcoming.json, 7704584)
grimbo_url_wget(http://www.bbc.co.uk/tv/programmes/genres/factual/scienceandnature/schedules/upcoming.json)
cmd=wget -qO- http://www.bbc.co.uk/tv/programmes/genres/factual/scienceandnature/schedules/upcoming.json
grimbo_url_copy(7704952, 7704584)
read 10239 bytes
read 10239 bytes
read 10239 bytes
read 8768 bytes
written 39485 bytes
written=39485
status=0
Child exit code: 0
buffer=7704952
page=1.000000
total=66.000000
offset=0.000000

Works!

Saturday 25 August 2012

Hacking a GPIO ribbon cable so it fits in an Adafruit case

Short story - I bought one of these cases, but the GPIO ribbon cable I had was too tall to fit. So I had to break off the top piece by hitting it with a hammer and a screwdriver.



Similar cable, plus the piece that comes off when hit with a hammer.


Cable now fits!


Cable working!

Tuesday 21 August 2012

PC1602F LCD driven by a Raspberry Pi

Pinching all the ideas from Raspberry Pi plus Hitachi HD44780-compatible LCD using GPIO, here is my Pi driving an LCD display.


I did have to make some changes. My LCD Pin layout was different, see datasheet.pdf and schematic from alldatasheet. My Pins 15 and 16 actually come before Pin 1, otherwise it's exactly the same order.


But I still used LCD Pins DB4..DB7 to send data to the LCD in 4-bit mode.

Pin no. Symbol Function
1 Vss Power supply(GND)
2 Vdd Power supply(+)
3 Vo Contrast Adjust
4 RS Register select signal
5 R/W Data read / write
6 E Enable signal
7 DB0 Data bus line
8 DB1 Data bus line
9 DB2 Data bus line
10 DB3 Data bus line
11 DB4 Data bus line
12 DB5 Data bus line
13 DB6 Data bus line
14 DB7 Data bus line
15 A Power supply for LED B/L (+)
16 K Power supply for LED B/L ( )




So to build and run:

pi@raspberrypi ~/dev/lcd $ make
gcc -O2 -o lcdinfo main.c lcd.c rpi.c
pi@raspberrypi ~/dev/lcd $ sudo ./lcdinfo


And here's a vid.


The only modification I did to the source code (main.c) was to alter the "ifname" passed to the get_addr() function. I changed it from "eth0" to "wlan0" as I was using WIFI, not ethernet.

Electronics - My Components

LCD

PC1602F - OUTLINE DIMENSION & BLOCK DIAGRAM - Powertip Technology

Brief Overview about Hitachi HD44780

Arduino – Getting my LCD Screen to work - I’ve gotten hold of what I assumed was a compatible 16×2 LCD screen with my Arduino board – it’s a Powertip PC1602F and uses a compatible display driver for the Hitachi HD44780 display chip ...

PIC

PIC16F690 - Comes with the PikKit 2.

Parameter NameValue
Program Memory TypeFlash
Program Memory (KB)7
CPU Speed (MIPS)5
RAM Bytes256
Data EEPROM (bytes)256
Digital Communication Peripherals1-A/E/USART, 1-SSP(SPI/I2C)
Capture/Compare/PWM Peripherals1 ECCP
Timers2 x 8-bit, 1 x 16-bit
ADC12 ch, 10-bit
Comparators2
Temperature Range (C)-40 to 125
Operating Voltage Range (V)2 to 5.5
Pin Count20
Cap Touch Channels12

Low Pin Count Demo Board with PIC16F690 (pdf user guide) - Small prototyping board that comes with the PicKit 2.

PIC16F628A (x2)

Parameter NameValue
Program Memory TypeFlash
Program Memory (KB)3.5
CPU Speed (MIPS)5
RAM Bytes224
Data EEPROM (bytes)128
Digital Communication Peripherals1-A/E/USART,
Capture/Compare/PWM Peripherals1 CCP
Timers2 x 8-bit, 1 x 16-bit
Comparators2
Temperature Range (C)-40 to 125
Operating Voltage Range (V)2 to 5.5
Pin Count18

PIC16F84A (x3) - First PIC. Very old now.

Parameter NameValue
Program Memory TypeFlash
Program Memory (KB)1.75
CPU Speed (MIPS)5
RAM Bytes68
Data EEPROM (bytes)64
Timers1 x 8-bit
Temperature Range (C)-40 to 85
Operating Voltage Range (V)2 to 6
Pin Count18

PCF8574 I2C Port Expander (pdf)

AT89C2051 (x1) - Wikipedia Atmel AT89 series - Datasheet (pdf).

No idea why I have this. Not sure I have a programmer for it.

Sunday 19 August 2012

Breadboard Power Supply 5V/3.3V

Breadboard Power Supply 5V/3.3V - here's the link - https://www.sparkfun.com/products/114

And here's the spiel:

Breadboard Power Supply 5V/3.3V
PRT-00114 RoHS Compliant

Description: Here is a very simple breadboard power supply kit that takes power from a DC wall wart and outputs a selectable 5V or 3.3V regulated voltage. The .1" headers are mounted on the bottom of the PCB for simple insertion into a breadboard. Pins labeled VCC and GND plug directly into the power lines. The lone pair of pins have no electrical connection but help support the PCB.
There are two pins available within the barrel jack footprint. Any stripped +/- DC supply can be connected instead of the barrel connector. Board has both an On/Off switch and a voltage select switch (3.3V/5V).
Comes as a bag of parts kit and is easily assembled if you can follow the silkscreen indicators and have beginning experience with a soldering iron. You will need to read the resistor bands or use a multimeter to determine the resistor sizes.
Dimensions: 1.25x1.25"
Kit Includes:
  • DC Barrel Connector (2.1mm center positive)
  • TO-220 Voltage Regulator (LM317 1.5A max current)
  • 1N4004 Reverse Protection Diode
  • 100uF 25V Capacitor
  • 10uF 25V Capacitor
  • 0.1uF 50V Capacitor
  • Red Power LED - High Brightness
  • 2 x SPDT Slide Switch
  • 4 x 0.1" Header Pins
  • 2 x 330 Resistor 1/6W
  • 390 Resistor 1/6W
  • 240 Resistor 1/6W
  • Bare PCB with Silkscreen Indicators
  • PTC resettable fuse



The soldering was pretty straightforward. I used my wire cutters to split the 4 header pins into two. As I didn't have a vice to hold the board, I used a kind of manual solder and reflow to get a component as close/straight as I needed it by soldering one pin, then soldered the other pins. This was most necessary for the two switches and the header pins (because I couldn't actually bend those pins under the board to hold them in place - like I could with the resistors etc).

Soldering everything left me with a little problem. Er, my breadboard's not compatible with the stabalising (NC) pins!


Back to the 'soldering' board, remove the NC pins ...

 

Bingo! Provides 3.3V or 5V via a switch, making the LED brighter or dimmer.

The circuit uses an LM317 voltage regulator, and there is a good video tutorial about this component online [LM317 Adjustable Voltage Regulator Tutorial].

Raspberry Pi - BI-COLOR RIGHT ANGLE LED - 2xGPIO

Moving on from "My first GPIO LED", I decided to take a look at a different LED I had in my possession, an L-59BL/1.

This LED has 3 pins and I had no idea what they were for, so I Googled it and found this parts list, and this data sheet.

The interesting bits are in this diagram:


So this LED has two anodes, one for Red and one for Green. This would let me try and use two GPIO Pins.

I decided to leave the +5V LED connected, and so used another Female-to-Female jumper to connect Pin-12 (GPIO-18). This would be in addition to the already connected Pin-11 (GPIO-17).


Original image found here - http://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/.

To connect one anode to the circuit, I decided to use the 3x10k-Ohm in parallel I already had to limit the current for Pin-11. But with the +5V LED taking up another resistor, that left me with no more resistors!

After some digging around, I found a 1k-Ohm SIL (similar to this) that I had removed from a previous PCB. I would use this for the second anode.

I stripped 3 Female-to-Female jumper wires off my ribbon cable to house the 3-Pin LED.

The finished circuit looks like this (the picture had to be taken in the dark as the LED was quite dim in my circuit).


To control GPIO-18, I had to follow the setup steps from my previous post.
root@raspberrypi:~# echo "18" > /sys/class/gpio/export
root@raspberrypi:~# echo "out" > /sys/class/gpio/gpio18/direction

Then I could control both lights.
root@raspberrypi:~# echo "1" > /sys/class/gpio/gpio17/value
root@raspberrypi:~# echo "0" > /sys/class/gpio/gpio17/value
root@raspberrypi:~# echo "1" > /sys/class/gpio/gpio18/value
root@raspberrypi:~# echo "1" > /sys/class/gpio/gpio17/value
root@raspberrypi:~# echo "0" > /sys/class/gpio/gpio17/value
root@raspberrypi:~# echo "0" > /sys/class/gpio/gpio18/value
root@raspberrypi:~# echo "1" > /sys/class/gpio/gpio17/value
root@raspberrypi:~# echo "0" > /sys/class/gpio/gpio17/value
root@raspberrypi:~# echo "1" > /sys/class/gpio/gpio18/value

I didn't actually get any Red light at all! I think this is because the part I have is actually a Yellow/Green two-colour part.

Saturday 18 August 2012

Raspberry Pi - My first GPIO LED

Following the directions on these pages:

[1] http://elinux.org/RPi_Tutorial_EGHS:LED_output (Circuit 1 - Basic LED Driving Circuit)
[2] http://elinux.org/RPi_Low-level_peripherals#GPIO_Driving_Example_.28Shell_script.29

See this Simple Guide to the RPi GPIO Header and Pins page for how the Pi's Pin numbers map to the GPIO Pins. The guide's Pin diagram is shown below:


I will use Pin-11 on the Pi, which translates to GPIO-17. Therefore, I use GPIO-17 in the shell.
pi@raspberrypi ~ $ sudo -i
root@raspberrypi:~# echo "17" > /sys/class/gpio/export
root@raspberrypi:~# echo "out" > /sys/class/gpio/gpio17/direction
root@raspberrypi:~# echo "1" > /sys/class/gpio/gpio17/value
root@raspberrypi:~# echo "0" > /sys/class/gpio/gpio17/value

The following picture shows the setup.


The top Red-Rail is Pin-11 (GPIO-17) output. The bottom Red-Rail is Pin-2 (+5V). Both Blue-Rails are Pin-6 (GND).

There's an LED connected between Pin-2 (+5V) and Pin-6 (GND), in series with a 10k-Ohm resistor. This will always be On, and just proves that the Pi is supplying power.

There's an LED connected between Pin-11 (GPIO-17) and Pin-6 (GND), in series with a 10k-Ohm resistor.

The shell commands as specified above, will turn the LED On and Off.

In practice, the LED connected to 5V is brighter than the LED connected to GPIO-17. I assume this is because GPIO voltage levels are 3.3 V.

The GPIO-connected LED wasn't very bright. I think this is a combination of the lower voltage level (3.3V) and using a resistor value 37 times bigger than the 270-Ohm resistor recommended in [1].


To increase the brightness of the GPIO LED, I used 3x10k-Ohm resistors in parallel to decrease resistance to 3.3k-Ohm.

Sunday 19 February 2012

Hello

This is where news of grimbodroid's applications will appear!