Simple web interface with BBB

This is actually a very basic question, but I’m kind of lost right now.
I was working on a school project with the Beagle bone black. It controls a bunch of motors and sensors, etc. We essentially wrote everything in C++ and made them into libraries of functions. They work when a main program calls the libraries and the function needed.

Recently we have been asked to demo our progress so far, that too in just a few days. The main program is far from over, so we were thinking of making a very basic web interface that is just going to call the functions. I personally never did this yet, so have no idea what should I be looking at. Node.js and Javascript (‘bonescript’) seems to be a popular tutorial around, but can they be used to call C++ libraries? Is it possible to host a webpage on the board so that we can open up the page in another PC over LAN?

I know this is a very basic question, but I don’t want to spend too much time looking up the wrong methods. Any points towards the right direction would be appreciated.

This is actually a very basic question, but I’m kind of lost right now.
I was working on a school project with the Beagle bone black. It controls a bunch of motors and sensors, etc. We essentially wrote everything in C++ and made them into libraries of functions. They work when a main program calls the libraries and the function needed.

Recently we have been asked to demo our progress so far, that too in just a few days. The main program is far from over, so we were thinking of making a very basic web interface that is just going to call the functions. I personally never did this yet, so have no idea what should I be looking at. Node.js and Javascript (‘bonescript’) seems to be a popular tutorial around, but can they be used to call C++ libraries? Is it possible to host a webpage on the board so that we can open up the page in another PC over LAN?

I know this is a very basic question, but I don’t want to spend too much time looking up the wrong methods. Any points towards the right direction would be appreciated.

Nodejs is your best solution. I suggest you do all your development in Cloud9, which is a web based IDE. There is documentation on the Nodejs website on interfacing to C and C++ code. You might want to look at DojoToolkit for web based widgets such as button, dials, graphs, etc. DojoToolkit plays nicely with Nodejs. There is also www.marqetta.org, which is another web based IDE that supports DojoToolkit widgets.

Regards,
John

Hello,

I agree with John in that nodejs would be your best bet. You can use nodejs to call your executable directly, if that is what you wish.

However I would say that cloud9 is not necessary, and in my own case is not even wanted. If you have a favorite IDE / code editor that handles syntax highlighting for javascript, that would work fine too. Personally, I use sublime text 2, which handles code complete / syntax highlighting great.

Hello,

I agree with John in that nodejs would be your best bet. You can use nodejs to call your executable directly, if that is what you wish.

However I would say that cloud9 is not necessary, and in my own case is not even wanted. If you have a favorite IDE / code editor that handles syntax highlighting for javascript, that would work fine too. Personally, I use sublime text 2, which handles code complete / syntax highlighting great.

Yep, I use Sublime Text as well, and with Live Reload, it is really productive.

Regards,
John

Hello,

I agree with John in that nodejs would be your best bet. You can use nodejs to call your executable directly, if that is what you wish.

However I would say that cloud9 is not necessary, and in my own case is not even wanted. If you have a favorite IDE / code editor that handles syntax highlighting for javascript, that would work fine too. Personally, I use sublime text 2, which handles code complete / syntax highlighting great.

Hi William,

I thought I would expand a little on choice of editors. While I use Sublime Text for Javascript and Web development, I do not use it for Linux Kernel code development. I know most developers use Vim or Emacs, but I have a hard time learning all the key combinations. For Linux Kernel development, I use Eclipse because I can index the code that is smart enough to understand #define in the config file and also do macro expansion. To setup Eclipse, I use the following instructions:

http://wiki.eclipse.org/HowTo_use_the_CDT_to_navigate_Linux_kernel_source

Now I can use ctrl left click to jump to the code implementation. I no longer get a hundred choices for a function definition when each arch has it’s own implementation.

Regards,
John

Others have suggested nodejs, but I think interfacing to your C++ directly from Javascript is going to be non-trivial; alternately you would have to build some kind of RPC interface to C++ that nodejs can call (e.g. REST or 0MQ).

You should consider Google’s Go (http://golang.org/), it has the ability to interface to C directly via cgo (you might need to wrap any C++ APIs in a C wrapper), and using open source projects such as Martini (https://github.com/codegangsta/martini) you can easily build high performance web interfaces with Go. The language is C/Java like, pretty easy to learn and very productive.

Best of all, go will produce arm/linux binaries with no dependencies that you can run directly on the BBB. In fact you can cross compile them from your workstation with Go (https://coderwall.com/p/pnfwxg).

Hope this helps,

-W.

This is actually a very basic question, but I’m kind of lost right now.
I was working on a school project with the Beagle bone black. It controls a bunch of motors and sensors, etc. We essentially wrote everything in C++ and made them into libraries of functions. They work when a main program calls the libraries and the function needed.

Recently we have been asked to demo our progress so far, that too in just a few days. The main program is far from over, so we were thinking of making a very basic web interface that is just going to call the functions. I personally never did this yet, so have no idea what should I be looking at. Node.js and Javascript (‘bonescript’) seems to be a popular tutorial around, but can they be used to call C++ libraries? Is it possible to host a webpage on the board so that we can open up the page in another PC over LAN?

I know this is a very basic question, but I don’t want to spend too much time looking up the wrong methods. Any points towards the right direction would be appreciated.

Others have suggested nodejs, but I think interfacing to your C++ directly from Javascript is going to be non-trivial; alternately you would have to build some kind of RPC interface to C++ that nodejs can call (e.g. REST or 0MQ).

It isn’t that difficult.
http://nodejs.org/api/addons.html

Nodejs has a huge repository of application and this is what separates it from almost everything out there.
https://npmjs.org/

Regards,
John

Hi John,

In “rebuttal” and off topic to the OP, I do use sublime text for C/C++ too. I am not exactly a GCC guru, but I am learning as much as I can a bit at a time, and often prefer to use the tools from the command line. I also use code::blocks, but only because it is easier for me to use quickly ( for base projects ) since I do not know make well.

I used to like using Visual Studio for almost all projects, and even set it up to work with mspgcc ( MSP430 version of gcc ) because I used to love the code completion and debugging / definition information tools. Now days though, it is far too bloated for my own tastes.

If you are skilled C/C++ programmers, why don't you use C CGI-programs
as Web interface?

Below a simple program for toggling the user led 0. Works perfectly on
my BBW with debian and apache. The main problem are the unix
user-rights, because Apache runs as user www-data and therefore the
CGI-binaries have no rights to change something on the system.
So, before running such a program via Web-server, you have to adapt
the user rights, in my case I did as root (after every reboot):
root@arm:~# echo none > /sys/class/leds/beaglebone\:green\:usr0/trigger
to stop the heart beat
root@arm:~# chmod 666 /sys/class/leds/beaglebone\:green\:usr0/brightness
to change permissions.

Simple options to call C binaries are PHP, perl, bash, ....
A simple php script for callling "ls" is below as well

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
char c[10]=" ";
printf ("Content-type: text/html\n\n"); /*do not delete this line*/
printf ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\
<html>\n <body bgcolor='#E6E6FA'>\n");
FILE *fp;
fp=fopen("/sys/class/leds/beaglebone:green:usr0/brightness","r");
if (!fp)
{
fprintf(stderr, "Unable to open File");
return(1);
}
c[0] = getc(fp); /*read the first char*/
fclose(fp);
fp=fopen("/sys/class/leds/beaglebone:green:usr0/brightness","w");
if (strcmp(c, "0") == 0) fprintf(fp,"255");
else fprintf(fp,"0");
fclose(fp);
printf ("<a href='led'>toggle the user led 0</a><br></html>\n");
printf ("</body></html>\n");
       exit(0);
}

And a php example:
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html> <body bgcolor='#E6E6FA'>
<?php
$output = shell_exec('ls -lart');
echo "<pre>".$output."</pre>";
?>
</body></html>