An update to my 'rCPU' remote monitoring webserver

I thought that it was about time I had a fiddle with the tool I made to remotely monitor CPU use. I have mainly been using it on my Raspberry Pi 2. I switched the javascript library from Smoothie Charts to Flot, in the hope of getting better browser support. But I was able to take advantage of some features of flot at the same time. So this is how it looks now:

That clip was recorded by remotely monitoring my Raspberry Pi 2 from my MacBook. Hopefully it's a worthwhile improvement. The code is all on GitHub: davidsblog/rCPU.

Remote monitoring *VoCore* CPU use

So I really wrote my CPU monitoring webserver program to be able to monitor my Raspberry Pi, but hoped that the code should run on other Linux machines too. Indeed, I have also been testing it in a virtual box running Debian. The code is runnable from Xcode on my Mac, but unfortunately it's a simulation only for testing purposes, because /proc/stat does not exist on Mac OS. So, for the moment, it definitely won't work on the Mac.

But I did think that it would work on OpenWrt, so decided to try it on my VoCore. In anticipation I had already put the necessary stuff in the makefile, so all I needed to do was go to a box with OpenWrt cross compilation set up and do this:  make vocore.

Then I copied the resulting binary executable to my VoCore and it worked straight away. I even made a recording of the initial run for posterity:

Since all the resources (html and javascript files) are embedded in the executable it is really very simple to do, just copy and run a single file. The version compiled for the VoCore came out at 112 Kb in size. Hopefully that's not too bad. I could compress the resource files if space was really important, but I haven't felt the need to do that.

It's pretty good to see this running on a machine about the size of a postage stamp. Obviously I could make it just show a single graph when it runs on a single core machine. Perhaps I'll do that when I get some time.

Music by numbers - more notes

I blogged before about building a Raspberry Pi music player by numbers thingy and I said that I’d mention some more details about the software used. I did write up the notes, but then forgot to post it on the blog, so here goes, better late than never…

I had already mentioned the USB Message Board linux driver and the MP3 player that I used. The USB Message Board also requires the libusb-1.0-dev package to be installed, like this:

sudo apt-get install libusb-1.0-0-dev

But before I got that far, I needed an OS … and I wanted a version that wouldn’t corrupt the filesystem on the SD card if I just switched the power off at the mains. I have ended up running something called IPE - Industrial Perennial Environment of which I’m using the R1 edition, which is based on Raspbian. It seems to work a treat: I don’t need to worry about clean shutdowns, the filesystem runs in read-only mode. The R2 version of IPE looks even better, but for the time being I’ve stuck with the Raspbian derived version which is more familiar to me.

It goes without saying, that the webserver I used to provide the web-based API was my own dweb lightweight webserver which seems to have done the job very well.

I also wanted the Raspberry Pi to offer its own WiFi hotspot, for which I followed these instructions. After that I also followed this advice and disabled ifplugd for the wireless lan.

So … putting those things together and with a simple C program, I have:

  • a version of Raspbian which can be powered off without a safe shutdown
  • a Raspberry Pi which operates a WiFi hotspot with a DHCP server
  • an MP3 player which shows the track number on an LED display
  • a web based user interface and API

Normally, I just use Safari on my iPhone to call up a song over WiFi, but I added a numeric keypad as a backup, in case of WiFi interference or other problems. I’m sure that I could really go to town and make a much more complicated wireless MP3 player out of this … but what I have just seems to work perfectly, so at the moment I’m leaving it well alone.

Music by numbers

I wanted to use a Raspberry Pi to play some songs (mp3s) through a very simple interface. Something where you can just type in a number and a corresponding mp3 file would play. This is what I've come up with:

The songs play with mpg321, a simple and lightweight mp3 player. I've used a spare "Dream Cheeky" LED display to show which mp3 is being played (by number). Obviously in the photo shown above there is nothing plugged into the audio socket ... but it was easier to take the picture this way.

The LED display is driven by the excellent dcled utility which works perfectly on the Raspberry Pi. A cheap numeric keypad allows input (although I have also made a web-based interface too).

I'll probably describe some of the software in more detail, when I have time.

An update on libpigpio

In the last couple of weeks, a few people have said that my libpigpio GPIO shared library for the Raspberry Pi only works if they recompile it from source, like here for example. (although a chap called Brendan mentioned it first, thanks Brendan).

So I thought that it was worth making people aware of that. Since I compiled the library on the initial Debian Squeeze release, there’s probably some difference with other Debian distributions that have come out since.

However, compiling the library from source on your own Raspberry Pi seems to work, so at some point I’ll update the zip file without the .so file, so that people can just compile their own version. In the meantime, the instructions are as follows:

  1. Download the zip file from here.
  2. Copy the zip file to the Raspberry Pi and extract the files to a folder
  3. Open a terminal window and use the 'cd' command to change directory to where you extracted the files
  4. Now type these commands (press enter after each line):
    gcc -c -fpic libpigpio.c
    gcc -shared -o libpigpio.so libpigpio.o
    sudo cp libpigpio.so /lib
  5. That will have compiled and installed the library into /lib
  6. I have also included a test program which you could try, like this...
  7. In the same terminal window enter these commands
    (note the spelling of -lpigpio is important):
    gcc -Wall -o test main.c -lpigpio
    sudo ./test
  8. Now the GPIO pins 7 to 11 should be turning on an off
    (use CTRL+C when you’re fed up)

I hope that helps...

Raspberry Pi at Work

For the past couple of weeks I've been bringing my Raspberry Pi to work with me each day in my bag. But my desktop PC only has one network socket which is already in use, so I've only been able to connect the Raspberry Pi to my laptop. To make things easier, I bought a USB Ethernet adaptor, so that I can use my Raspberry Pi in the office with ease, in fact I can use it on any PC with a free USB socket. Here it is in action:

That works OK, which is cool. You can see I'm still running the Pi from the battery - which is still on it's original charge. It's given me light use of the Raspberry Pi for over two weeks. But at some point a Raspberry Pi powered laptop will appear I expect...

For my part, I'd be happy if somebody made an integrated Raspberry Pi case-and-battery. Then you could just carry one box around and recharge it like a phone or a kindle. But there may not be a market for crazy people like me who carry their RasPi around with them.

Raspberry Pi gone mobile

I'm writing this blog entry using my new Raspberry Pi, it's the second one I've bought. Not much unusual about that, but I'm doing it in Costa Coffee in Cambridge:

It's good that you can get your hands on a Raspberry Pi much easier now. I ordered this one on Friday night, and it arrived Tuesday morning. So that's less than 2 working days, which is much better. I got this one from Cool Components and I got the red ModMyPi case from the same place too. So because I had a Raspberry Pi in a case, I decided to go out on the road with it, well why not?

To get power to the Pi I'm using an emergency phone charger, mine is a TeckNet iEP380 which I bought from Amazon. It's rated at 5000mAh and seems to power the Raspberry Pi very well (although I've got nothing connected to the USB ports of the Pi). It might struggle if I connected any devices but I'm using the Pi 'headless' with just an ethernet cable to connect to my laptop.

So... now I can sit here in Costa Coffee, sipping my Cortado whilst working on my Raspberry Pi. Excellent, it's a good way to spend an hour or so in the morning.

H2D2: Something to do with Pi

After having success with moving running H2D2 programs between platforms, I wanted to try running H2D2 on the Raspberry Pi. I was pretty confident that it would work, having already compiled H2D2 from source on Debian on another machine. What I wanted to do this time was run some H2D2 code on the Raspberry Pi and then transplant the program to Windows and allow the code to run to completion. So here goes:

...well that seems to work OK. But that was pretty much expected. You can see that I ran the demo code for 500ms this time. But the real trick is to see if the file which has been generated is binary compatible with other platforms. If I move the output file to my Windows machine, will the H2D2 program continue exactly where it left off? Drum roll please.

Yay! After putting all that effort in it's nice to know that H2D2 bytecode is Raspberry Pi compatible, I hope that it goes a long way towards my efforts at crossplatformness. Of course, this also means that I have a means to fork a running program, since I could go back to the file that I copied off the Raspberry Pi and start again from that exact point as many times as I like.

...umm and I also need to write a different example program, all these mandelbrots are starting to get boring again. Trouble is, it is quite a good piece of test code. Anyway, I'm off to implement number arrays in H2D2 now.

Getting GPIO input with libpigpio

I promised that I'd report back when I had gotten round to testing the input capability of libpigpio. Well I now have something to report, here goes:

I've written a small test program in C to check that it works OK (and added the necessary electronics). When I press a button (which is connected to the GPIO) then the LED comes on. This is all done through software. As you can see from the picture above, it works fine. I also have the same program outputting the GPIO input values to the console, like this:

So now that I have tested it in C, I can also try the same thing in C# with mono, but it should work fine.

Using libpigpio in Mono with C#

UPDATE: you'll probably need to recompile the library from source, read here for instructions.

So, here are some instructions for using libpigpio, the shared GPIO library for the Raspberry Pi in a C# program. First, get libpigpio by downloading it from here.

Unzip it and copy the file called libpigpio.so to the /lib folder on your Raspberry Pi. You'll need elevated privileges to do that, so you'll probably type something like:

sudo cp libpigpio.so /lib.

Alternatively, you could compile the library yourself by copying the libpigpio.c file to the Raspberry Pi and using the commands listed inside the file to compile and copy it to /lib.

Once libpigpio.so is in the /lib folder the library is installed and ready to use. We can now begin programming in C#. I'm using MonoDevelop on another machine, I expect you'd probably do something similar. Create your new C# project, and add the RpiGpio class found in RpiGpio.cs or just copy and paste the code from my previous blog entry. This is the wrapper that allows the shared library to be used in C#. Now we can write some C# code to access the GPIO.

This is an example test program:

using System;
using System.Threading;
 
using LibPiGpio;
 
namespace RPiBlinky
{
    class Program
    {
        static void Main(string[] args)
        {
            int inc = 1, t = 7, last = 0;
            RpiGpio.SetOutputPins(new[] { 7, 8, 9, 10, 11 });
 
            while (!Console.KeyAvailable)
            {
                if (last != 0)
                {
                    t = t + inc;
                    if (t >= 11 || t <= 7) inc *= -1;
                    RpiGpio.Pins[last] = false;
                }
                RpiGpio.Pins[t] = true;
                last = t;
                Thread.Sleep(250);
            }
            Console.ReadKey();
        }
    }
}

If you want to test your code, you can uncomment the #define NOPI in RpiGpio.cs, which will allow you to run the code on something other than a Raspberry Pi. For example, here is the test program running like that on my virtual machine:

When you're happy with your code (making sure that you've re-commented the #define in RpiGpio.cs) simply copy the compiled C# executable to your Raspberry Pi and run it like this:

sudo mono GPIOtest.exe.

But replace GPIOtest.exe with the name of your Mono executable. You'll need to use sudo for the program to have enough permission to access the GPIO. That's it! It's quite a simple process really.

Please check everything I've said, you can't hold me responsible if you break your own stuff. Always double check your electronic connections, that will help you not to break stuff too. It's always best to check the source code and compile it yourself before you use it. But have fun, it's good to build stuff.