-1

For my Project I'm using a raspberry pi which, after processing values from the sensor with the function conv_aqi(), is supposed to send them on a database hosted on 000webhost so that everyone can see them. The code does not produce errors, but values are missing on the db. I'm trying to pass those values are json. This is the python script:

import requests
def sendDatatoServer():
url = 'https://*******.000webhostapp.com/api/insert.php'

data = {
            'aqi_2_5' : 'conv_aqi(pmt_2_5)',
            'aqi_10' : 'conv_aqi(pmt_10)',
        }

print(data)
res = requests.post(url, data)
print(res.text)

sendDatatoServer()

where the insert.php on the 000webhost is the following:

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
//Creating Array for JSON response
$response = array();

// Check if we got the field from the user
if (isset($_GET['AQI_2_5']) && isset($_GET['AQI_10'])) {

    $AQI_2_5 = $_GET['AQI_2_5'];
    $AQI_10 = $_GET['AQI_10'];

    // Include data base connect class
    $filepath = realpath (dirname(__FILE__));
    require_once($filepath."/db_connect.php");

    // Connecting to database 
    $db = new DB_CONNECT();

    // Fire SQL query to insert data in sensorData
    $result = mysql_query("INSERT INTO sensorData(AQI_2_5,AQI_10) VALUES('$AQI_2_5','$AQI_10')");

    // Check for succesfull execution of query
    if ($result) {
        // successfully inserted 
        $response["success"] = 1;
        $response["message"] = "Data from sensor successfully created.";

        // Show JSON response
        echo json_encode($response);
    } else {
        // Failed to insert data in database
        $response["success"] = 0;
        $response["message"] = "Something went wrong";

        // Show JSON response
        echo json_encode($response);
    }
} else {
    // If required parameter is missing
    $response["success"] = 0;
    $response["message"] = "Parameter(s) are missing. Please check the request";

    // Show JSON response
    echo json_encode($response);
}
?>

And what I get is "Parameters are missing. Please check the request". Can I please have a hand on understanding where I'm mistaken ? Thanks

Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100

1 Answers1

1

Your Python code is using HTTP POST to send data in the body but your insert.php has been written to expect query string parameters from a GET request.

The simplest thing to do is to change your Python code to pass query string parameters by using GET and Request's params attribute:

res = requests.get(url, params=data)
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
  • Thanks a millions! it works now in that data are sent to the db. However I expected to send the results of the conv_aqi() function. While what I find on the db is the exact string "conv_aqi(pmt_2_5)" and "conv_aqi(pmt_10)" , so it sees the values as strings, while I would like to have there the numbers resulting from the conv_aqi() – purple berries Apr 21 '20 at 11:34
  • @purpleberries I think you've almost answered your own question - "exact **string**". You need to have a look at some programming principals as PHP and Python both have the same concept of quoted text and functional calls. – Alastair McCormack Apr 21 '20 at 16:13
  • Yes in the end I was able to pass the values and it works, I was not familiar at all with json either. Thanks for putting me on the right path! – purple berries Apr 22 '20 at 10:42