Okay, so as the title says, I'm an embedded C/C++ developer.

Right now, I'm working on a project that involves a client/server C application. The server side of the application is running on multiple SoC nodes that are deployed in random locations (they run off a battery and use a cellular modem for communication).

Right now, I would like to poll information about these nodes. So I have a central computer that I call my server, but that is running the client-side application of the project (bear with the nomenclature here). I run my program from the command line with an IP address, and other parameters as an input. It establishes a TCP connection with a node, sends it a request packet, and the node answers with a response packet. I then write this data to a CSV file on the server computer (the one who initiated the request). The socket is closed, and that's it.

One of the modes it runs in is "live mode", in which the client instantiates a TCP connection to one of the nodes (running the server portion of the application), and returns data once a second. When data is returned, a CSV file is appended to. Once the program receives an interrupt signal, it gracefully shuts down.

Now I'd like to do a little more. I would like a user to be able to control this application via a web browser.

The hacky solution I came up with is to have a button on a page. When this button is pressed, I do a shell_exec(/srv/bin/myApp_cli <parameters> &). Then through AJAX Javascript, the page starts reading from its output file once a second. It looks at the last line of the CSV file and posts it on the screen. When I want to stop this application, I do shell_exec(killall -2 myApp_cli).

It works, but my god is that a hacky way of doing something that should be easy. These two parts of the larger project: the web portion and the C client server portion; are extremely independent of one another.

My question is.. how can I interface my simple little C program with PHP, or any web language easily (I chose PHP because that's the only real back-end language I know). What would be the best approach to achieve what I would like to do? Especially in the case in which I run a program indefinitely until I want to send it a signal to stop?

  • 2
    Have you thought about having the C program write to a lightweight queue and then have your web app be a client that queries the queue? – GlenH7 Feb 11 '15 at 1:36
  • websockets? – Steven Burnap Feb 11 '15 at 6:08
  • cgi scripts can run any C code in the web server, and then its just matter of passing the data to correct place. – tp1 Feb 26 '15 at 18:56
  • "Passing the data to the correct place" is the hard part I'm trying to figure out. I can run the C code with parameters, etc, just fine. I can even write a script to run the C binary. It's passing the data between the C code and web service that I'm trying to get nailed down. – justynnuff Mar 3 '15 at 19:40

The best back end webserver language for an embedded C program is... C!

There are plenty of embedded C webservers that are trivially easy to use, I added civetweb to my service a while ago and it was too easy. Civetweb is a non-GPL fork of Mongoose, but there are others such as NxWeb.

Civetweb has some examples, including ones that include Lua support and websockets if you need push responses to the web browser, all in a handful of lines of code.

  • I'm looking through the civetweb stuff. It looks really interesting. The problem, and hence my posting on here, is that I just don't know what exists out there. This is a massive jump start, though. Thank you. – justynnuff Feb 11 '15 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.