my pi reads data from sensors and I want it to send the data to my pc, and whenever the pi reads a new value, it must send it to the pc automatically and save it in a text file and appends it to it. Can anyone suggest a good way to do that ? Note : I'm using C++ to read the data. And my pc's operating system is ubuntu Thank you all
-
Are the PC and Pi networked?– joanCommented Mar 4, 2017 at 22:12
-
The best way would be networking, which means some kind of IP based client server application a la DriverUpdate's answer -- although personally I would skip the use of HTTP unless using a web app adds some further advantage to you.– goldilocks ♦Commented Mar 5, 2017 at 8:43
2 Answers
How often does this need to be updated, is it more than twice a minute or so, or is it long term logging(Once an hour or day, maybe)? If the latter, one way to do it would be to create a PHP-script running on your Ubuntu machine, taking http requests(GET or POST), formatting and saving the received data. the request could look something like this:
"http://address.of.Ubuntu.machine/?Sensor1=Data1&Sensor2=Data2 HTTP/1.1"
The php script would take that information, easily read and put however you like and neatly format it and save it. I haven't bothered to write the formatting, sorry, but this php running on apache or something should do the trick(Not tested) $req_dump = print_r( $_REQUEST, true ); $fp = file_put_contents( 'request.log', $req_dump ); In PHP, the $_REQUEST variable is basically a dump of the http request coming into the server... You should probably make the filename into a timestamp.
The C++ on the RasPi could look something like the selected best answer here: https://stackoverflow.com/questions/26761058/how-to-send-http-request-and-retrieve-a-json-response-c-boost, especially the part
#include <boost/asio.hpp>
#include <iostream>
int main() {
boost::system::error_code ec;
using namespace boost::asio;
// what we need
io_service svc;
ip::tcp::socket sock(svc);
sock.connect({ {}, 8087 }); // http://localhost:8087 for testing
// send request
std::string request("GET /newGame?name=david HTTP/1.1\r\n\r\n");
sock.send(buffer(request));
, where the "name=david" is replaced with all of your sensor information, separated with &s.
The only disadvantage I can tell with this method is: -A web PHP-compatible web-server has to be running on the Ubuntu PC
Feel free to comment suggestions if anyone finds something terribly wrong with my thinking or code...(I am Norwegian, 100% english is NOT guaranteed)
-
"The only disadvantage I can tell with this method is: -A web PHP-compatible web-server has to be running on the Ubuntu PC" -> It's not hard to take this a step further and just create a custom client and server application for both ends.– goldilocks ♦Commented Mar 5, 2017 at 8:41
What about saving the data to a file on the Raspberry Pi, then using rsync to update the file on the PC at whatever frequency you wish.