A pretty neat idea

I've bought myself a new watch, one of these Casio MP-PCGM1-6 ones:

I've decided that I'm one of those people who really does think that the digital watch is a pretty neat idea.

But you need to be careful. The first one I ordered was from one of the Amazon shops and when it arrived I was pretty sure it was a fake. It was very poor quality, the plastic was rubbish and the printing on the dial didn't look right. It just arrived in a jiffy bag with no other packaging. But I was able to get a refund. This time, I ordered from a jeweller, and there's no doubt this is the real deal. It also came in a proper Casio box. Don't settle for a poor quality digital watch...

Running dweb on OpenWrt

To take OpenWrt for a test drive, I have been messing about with one of these TP-Link WR740N routers. It gives me a small low-power Linux box, which I’m sure I will find many uses for. When you consider that this £30 router comes in a consumer style plastic case with a power supply and even an ethernet cable, you realise that it’s a pretty good deal. It can’t do all the things that a Raspberry Pi can do, but it still has potential. And I also found this link which shows I’m not the only one.

So after getting the cross compiler set up (which I did using a Debian machine running inside VirtualBox) and getting some simple C programs running (flashing the LEDs), I decided to try something a little more interesting.

Despite the fact that OpenWrt seems well catered for in the web server department (it can run uHTTPd which supports SSL and even embedded Lua scripts), I still wanted to try my own web server code, out of curiosity, and just because I can.

So I decided to cross compile my own web server, called dweb for OpenWrt and try it out. I’m pleased to say that it worked without any modification. The only thing that I needed to do was install the POSIX thread library onto the router, using this command:

opkg install libpthread

…and after doing that I could run dweb. My simple web based API example which uses jQuery ajax to post values worked perfectly.

So now I can write code in C and expose it as a web-based API from my router. The problem is, I don’t really know why I’d want to do that. But in any case I can. At the moment it’s a solution looking for a problem. But it’s been fun.

What I wanted to achieve with dweb was to write a small webserver in portable C code and without needing external dependencies. I suppose that using the POSIX thread library would be considered a dependency, but the dweb source code allows you to turn that off.

So hopefully, this means that dweb will run on one of these VoCore one inch Linux machines which would be pretty cool. I am thinking of what I could do with a tiny webserver running an API which exposes the GPIO pins over http. Perhaps I’ll build that home sensor network I’ve been wanting ;-)

OpenWrt Linux on a TP-Link WR740N router

Recently, I have been watching this crowdfunded coin-sized Linux machine with some interest. I also noticed that it runs OpenWrt... which is a version of Linux that's designed to run on routers. So I thought that it would be worth some further investigation. I tried to find the easiest way of running OpenWrt at home, and ended up paying £30 for one of these TP-Link TL-WR740N machines (I bought mine at my local branch of Maplin).

It’s not bad actually, for the money you get the power adaptor and even an ethernet cable included – so you're ready to go. This router will accept the OpenWrt firmware straight from its web interface, so you just fetch the correct bin file from here and upload it to the router in the normal way. Personally I’m using this firmware and my device is a v4.27.

After the firmware has flashed and the router has rebooted you can telnet in and access OpenWrt for the first time. It was very simple indeed. For detailed information look here. Basically I’ve followed the "OEM easy installation". It doesn’t get much easier.

Of course, I want to write C programs for it now, so I’ll be setting up a cross-compiler. I think I’m going to use this page as a guide because it looks like one of the best sets of instructions out there.

So maybe I’ll end up fiddling with one of those VoCore coin sized computers eventually... It looks very tempting.

dweb: a lightweight portable webserver in C

I've been continuing to experiment with the source code to 'nweb' (a minimal web server written in C). In fact, I've probably experimented so much that I've changed most of the code beyond recognition by now. But I'm still calling my version 'dweb' in honour of the original.

Since the C# webserver I wrote ages ago has proved quite popular, I thought that I'd try something similar in C. I realised that I should be able to produce a lightweight little web server, which could also be a starting point for some other things I have in mind. Since I'm not using any external libraries, there are no dependencies to worry about. Well, OK I have used the POSIX thread library, but it can be taken out by changing a #define.

I'm attempting to make something which can easily be adapted, where you simply need to write a function which assembles your html response and everything else is done for you. So I'm making use of C function pointers for that. All you need to do is write your custom "responder" function (using the correct signature) and then call my webserver function, passing in a pointer to your own responder function.

That way, by adding a single C file to a project and writing a few lines of code you can have an embedded web server - albeit a very simple one of course.

Obviously I'm not attempting to write a fully featured web server platform, but this is the type of hobbyist project you could run on a Raspberry Pi to create a very simple web interface for something, whilst keeping it very lightweight and low on dependencies. It might work on different types of embedded Linux too (I'd be quite interested to know if it does).

But for my purposes, it will be an easy way to provide a simple web based API, where I can send commands with an http POST or just retrieve data by sending a GET.

But my main aim was to make a custom webserver possible in C with just a few lines of additional code. Being portable, it should run on Mac OS X, Linux (including Android) and even Windows if Cygwin is installed. It certainly worked OK on the Raspberry Pi and I've also used it on my Android tablet.

Since I've included support for HTTP POST (which nweb didn't have) it means that simple form posting scenarios will work, and Ajaxy type stuff should even work. The example code I've written allows HTML form values to be submitted. It also includes a simple Ajax call where a parameter is sent to the server and a return value comes back in the response.

So how does the code look? Well, here is the simplest (trivial) implementation:

void test_response(struct hitArgs *args,
   char *path, char *request_body, http_verb type)
{
    ok_200(args,
        "<html><head><title>Test Page</title></head>"
        "<body><h1>Testing...</h1>This is a test response.</body>"
        "</html>", path);
}

int main(int argc, char **argv)
{
    if (argc != 2 || !strcmp(argv[1], "-?"))
    {
        printf("hint: dweb [port number]\n");
        exit(0);
    }
    dwebserver(atoi(argv[1]), &test_response, NULL);
}

Not too bad - but it just gives the same response to every incoming request. The full source code includes more advanced examples showing an HTML form posting back some values. It also shows an Ajax call using jQuery.

Like 'nweb', it started out as a multi-process server, where each request spawned a new process using fork(). But I am now including support for single-threaded and multi-threaded modes as well, the default being multi-threaded.

Anyway, the source code is on GitHub, if you're interested (released under the MIT license). Meanwhile, I'll keep tinkering with it...