0

I'm working on a project where I want to send data from different sensors connected to the RPi 3B+ GPIO pins using python to both collect the data sent from the sensors to the pins and to send a POST request with that information embedded to an external HTTP server. This HTTP server is going to perform as a control station for many other Pis.

The problem I have is that I can't get the HTTP control server to acknowledge what to do with the incoming POST requests sent by the Pi, so it can finally output some sort of text like "measure received".

How I'm doing it:

  1. Installed XAMPP on my PC to simulate the HTTP control server.
  2. Installed Raspbian 9 Lite on the Pi along with Python requests library.
  3. Tested connection from the Pi to the XAMPP server using links2 CLI explorer.
  4. Tested the example code from Python requests to POST data to the HTTP control server (IP: 192.168.0.9).
  5. The server responds with a 200 OK, but I don't really know how to proccess or even read that POST request.

This is the code in the raspberry.

pi@raspberrypi:~ $ more python_requests_example.py
import requests

payload = {'name' : 'test payload' }
p = requests.post('http://192.168.0.9/index.php', payload)

print(p.status_code)

When I run it, the code outputs the following:

pi@raspberrypi:~ $ python python_requests_example.py
200

I captured packets on the HTTP control server, and I see the both the POST and the 200 OK, I also see the payload embedded in the POST package. (https://i.stack.imgur.com/yClF4.jpg)

I opted not to install a whole web server (like LAMP) on the Pi because it won't be really used.

So, for the imputted data provided by the RPi sensors I'm using Python, and the plan is to then send this information to the HTTP control server.

I also would like to proccess this POST request purely with PHP (if possible or if not too hard). I also saw that I can POST from the RPi as json format, I don't have much experience with it, but if it would be better I can dig more about it.

If something is not clear please tell me and I'll try to complement.

Carlos Pérez
  • 47
  • 1
  • 1
  • 9

1 Answers1

0

Just keeping this post updated, I already found a solution while browsing through the posts linked below.

  1. [How to get body of a POST in php?
  2. [How to get request content (body) in PHP?

Just a quick explanation of how I did it:

First, I used the $_SERVER array to sense when a POST request is received on the HTTP control server. Then, I read the content of the POST body with file_get_contents('php://input') and then I saved it to a text file (for now) to prevent the POST body to be lost on a page refresh.

The PHP code ended like this:

<?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        $post_from_Rpi = file_get_contents('php://input');
        $log_file = fopen('test.txt','a') or die("Unable to open the file");
        fwrite($log_file, $post_from_Rpi);
        fclose($log_file);
    }
?>
Carlos Pérez
  • 47
  • 1
  • 1
  • 9