Understanding Raspberrypi-php POST Communications

Asked 2 years ago, Updated 2 years ago, 89 views

Send data obtained by Arduino to raspberrypi via serial communication and
Launch the server on the PC with Apache, send data from raspberrypi to the PC, and
Finally, save it as a csv file.
I'm going to save the data of the two sensors, but the contents of the csv file are

26.6

28.2

26.5

28.2

Data 1, line feed, data 2, line feed, data 1, ...
It is saved in the order shown in .
I think raspberrypi sends data at the same time, but it doesn't work

raspberrypi code (python)

import requests
import serial
import time

if__name__=='__main__':
    gpio_seri=serial.Serial('/dev/ttyACM0',9600,timeout=10)
    gpio_seri2=serial.Serial('/dev/ttyACM1', 115200, timeout=10)
    while1:
        gpio_seri.write('get')
        gpio_seri2.write('get')
        bio_data=gpio_seri.readline()
        bio_data2=gpio_seri2.readline()
        print(bio_data)
        print(bio_data2)
        url="http://10.0.221.101:8080/send.php"
        response=requests.post(url,data={'bio_csv':bio_data})
        response2=requests.post(url,data={'bio2_csv':bio_data2}

php code

<?php
  $data1 = $_POST ['bio_csv'];
  $data2 = $_POST ['bio2_csv'];

  $datas=array(
    array($data1,$data2)
  );

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

    $line =implode(',',',$data);
    fwrite($fp,$line."\n");
  }
  fclose($fp);
?>

I don't know the cause at all
If you are familiar with Python or php, please let me know

python php raspberry-pi arduino

2022-09-30 11:50

1 Answers

I sent it twice on requests.post, so it's a separate row of data.
I have never written python, but I think I should send the POST data at once as follows.

response=requests.post(url,data={'bio_csv':bio_data,'bio2_csv':bio_data2})


2022-09-30 11:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.