Python Forum

Full Version: sending data from my raspberry pi to my website
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my question here

I am an embedded systems engineer with just basic understanding of python.
I need to send temperature readings that have been stored in my raspberry pi as an xls file, to my website. after that on the server side, the other team is resposible to parse that data using PHP or whatever.
my responsibility is to make a python script that will send this data to the server.
I know that I have to use urllib in some way, most of the tutorials talk about grabbing and parsing data from a website, but none show how to send data.
if someone could help me with what I am supposed to do, that would be a great help

my code here
(Sep-05-2017, 04:15 PM)mohitsangavikar Wrote: [ -> ]I need to send temperature readings that have been stored in my raspberry pi as an xls file
xls is a really bad choice when it comes to web commutation.
If the server is not specialty setup to handle xls,then have to upload file as binary and server side has to deal with the whole file.
JSON is the preferred format when it come sending stuff to and from a server.
(Sep-05-2017, 04:15 PM)mohitsangavikar Wrote: [ -> ]I know that I have to use urllib in some way, most of the tutorials talk about grabbing and parsing data from a website, but none show how to send data.
No Requests is the one to use,not urllib.
Here a demo with Flask as server,and i will be sending data from outside into the server with Requests.
app.py:
from flask import Flask, request, jsonify

app = Flask(__name__)
@app.route('/pi', methods=['POST'])
def pi():
    pi_data = request.json
    print(f'Vaule on server {pi_data}') #--> Value on server {'temp': 100, 'temp_1': 150}
    return jsonify(pi_data)

if __name__ == '__main__':
    app.run(debug=True)
Start server with python app.py will be running on http://127.0.0.1:5000/.

pi_data.py:
import requests

data_from_pi = {'temp_1': 100, 'temp_2': 150}
response = requests.post('http://127.0.0.1:5000/pi', json=data_from_pi)
if response.ok:
    print(response.json()) #--> {'temp_1': 100, 'temp_2': 150}
While I agree that JSON is the way to go for more complicated data, for just sending a couple of values you might also consider using GET statements for simplicity.

For example:

import urllib

url = "http://yourserver.com/webpage.php?temperature=55"
urllib.urlopen(url)
And webpage.php would look something like:

<?php

$temperature = $_GET["temperature"];
print $temperature;

?>