I want to send sensor data from raspberrypi to php by POST.

Asked 1 years ago, Updated 1 years ago, 98 views

I'm thinking of saving the sensor data sent from Raspy on my PC in CSV format, but data.csv is created but no data is written.
By the way, the data I'm passing is comma-separated data (like 1, 2, 3.1, 4.2, 5.3)

I don't even know if there's a problem on the raspy side or php side.
I know it's a messy question, but if anyone knows anything about it, please let me know.

Data Transmitter (raspberrypi)

#!/usr/bin/env python
# -*-coding:utf-8-*-

import requests
import serial
import time

if__name__=='__main__':
    gpio_seri=serial.Serial('/dev/ttyACM0',9600,timeout=10)
    print(gpio_seri.portstr)
    time.sleep(3)
    while1:
        gpio_seri.write('get')
        time.sleep(1)
        data=gpio_seri.readline()
        print(data)
        url="http://192.168.0.103:8080/test1.php/"
        s=requests.session()
        r=s.post(url,data)

Data Receiver (php)

<?php

    $data1 = $_POST ['data'];

    $filename = "data.csv";
    $fp = fopen('data.csv', 'a');

    $line=implode($data1);

    fwrite($fp,$line."\n");

    fclose($fp);

?>

python php apache raspberry-pi xampp

2022-09-30 17:12

1 Answers

Resolved
I changed it like the code below, and it was successful.
Thank you so much for your comments.

I changed the sender by referring to the comments.

In addition, I changed the write part of the receiver to current without using fwite

sender

#!/usr/bin/env python
# -*-coding:utf-8-*-

import requests
import serial
import time

if__name__=='__main__':
    gpio_seri=serial.Serial('/dev/ttyACM0',9600,timeout=10)
    print(gpio_seri.portstr)
    time.sleep(3)
    while1:
        gpio_seri.write('get')
        time.sleep(1)
        bio_data=gpio_seri.readline()
        print(bio_data)
        url="http://192.168.0.103:8080/test1.php/"
        response=requests.post(url,data={'bio_csv':bio_data})

Recipient

<?php
    $data1 = $_POST ['bio_csv'];
    echo$data1;
  $file='test.csv';
  $current=file_get_contents($file);
  $current.=$data1;
  file_put_contents($file,$current);
?>


2022-09-30 17:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.