简体   繁体   中英

Raspberry Pi with c++ running thread, publish data on web

I would like to show some values handled in my c++ program on a web page, this should be opened from many clients and the web page should be not mandatory for my application.

I described these requirements because I getting crazy to solve this issue: I found many samples about cgi and it is clear, more or less, how it works and it's not enough for my scope because it's a process called from web page but it has no connection with my application. My main can't by a cgi program, it should run even the web page is never opened (should be optional).

So, how I can share data between a cgi program and my main c++ program?

Saving on a file or database is really the only way?

At the end what is necessary to me is intercept "get" and "post" info, or call my functions from web server (server code script).. but how?

Thanks Andrea

If it's a one-way system (eg data flows only from the C++ program to the user on the website, not from the website to the C++ program), then the easiest way would be writing to a file or preferably a database.

You could write to the DB from your C++ code using something like the MySQL connector , and a basic website to display the data from the DB could be written very quickly in PHP on an Apache server.

Another solution might be to write the output to a HTML file directly from the C++ program whenever new data is created. Users requesting the data could just access your server and get the most recent data from the HTML file.

The way I would do this is to have a small program that measures things and outputs the data to a file, using mv or rename to ensure the file is replaced atomically.

Then use a web-server with programming capabilities (I know PHP, but you could use Java, Java Script, Python, C, C++, or probably a dozen other languages).

Keep things simple - unless you absolutely have a criteria to make it different, simple is always better than complicated when it comes to getting things done - once it's working, you can fiddle with it, or completely rebuild it and make it better, but at least you have something that works first.

Requests for things can either be stored in a configuration file that your application reads - being sent as POST or URL arguments (If you want to use "function 3", then you use: http://example.com/mypage.php?function=3 , etc).

This is not the ONLY way, there are MANY solutions. But this is the SIMPLE way, that will have it solved and working reliably this weekend or so, rather than a solution that takes you several weeks to solve the problem with, and not working reliably "ever".

I suggested that "you can use shared memory" in a comment. It is not my suggestion that you do that. It is quite hard to use, and you need to be absolutely sure your data is updated atomically, or you get problems with "part of the data belongs to one update, the other to an older update", and thus very confusing. You don't want to spend days or weeks trying to get shared memory synchronisation working. If you really need to communicate between the webserver and your application, I'd use some form two (named?) pipes and send request to the app, and send back the response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM