Upload Arduino serial data to web storage file

Here shows a method to upload and save the serial data from the Arduino to a web server. This can be used to send values from an Arduino to a web server via a PC without a wifi module or ethernet. But here the data should be able to read by a serial terminal.

The below Arduino code is used to generate serial data for testing; the code generates numbers from 1 to 10.

Arduino Code

int number = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  while (number < 10) {
    number += 1;
    Serial.println(number);
    delay(1000);
  }
}

Refer: Save Arduino serial data as a text file

The processing tool reads the serial data and sends the data to a URL address as a GET request.

To send data as an HTTP request the processing needs an additional  HTTP Requests for Processing library is required.

Processing Code

import processing.serial.*;
import http.requests.*;

Serial COMPort;  // Create object from Serial class

public void setup()
{
  frameRate(5);
  String portName = Serial.list()[0];
  COMPort = new Serial(this, portName, 9600);
}
void draw() {
  if (COMPort.available() > 0) {  // If data is available,
    String read = trim(COMPort.readString());  // read and store it to string read
    println(read);
    //Replace below address with your file location in host location.
    GetRequest get = new GetRequest("http://example/savejson.php?value=" + read);
    get.send();
    println("Reponse :" + get.getContent());
  }
  delay(100);
}

Save data to a file in server

The processing sends the values that read from the serial port to the URL address as GET request (eg:- when value is 10: http://example/savejson.php?value=10). Here given a server script using PHP to read the get request values and to save it to a JSON file. Any server language can be used to receive the GET request and save the value. Also, any file format like CSV, XML, or SQL database can be used to store the data.

Create and save the files savejosn.php and data.json at the server location http://example/, ie http://example/savejson.php http://example/data.json respectively; briefed below.

1. Create a file, data.json

Save it with open and close square brackets.

data.json
[]

The data.json file stores the data that are received.

2. Create a PHP file with the file name “savejson.php” and save the below code.

PHP code – savejson.php

 <?php
if (isset($_GET['value'])) {
    $data = json_decode(file_get_contents('data.json'), true);
    $new = array(
            'Value' => $_GET['value'],
            'Date_Time' => date("Y-m-d, h:i:s, A"), );
    array_push($data, $new);
    $json = json_encode($data);
    file_put_contents('data.json', $json);
    print_r(array_values($new));
}
?>

The file savejson.php has the PHP script to collect the variable passed as GET request and save it to the data.json file. The PHP code appends each new array to the JSON file whenever it receives a get request for the parameter “value”. Each array element will have a value and corresponding date&time assigned for keys “Value” & “date_Time” respectively.

Resultant Json file; for value 10 entered on date 2021-01-01 & time 1:00:00 AM.
[{“Value”:”10″,”Date_Time”:”2021-01-01, 01:00:00, AM”}]

See also : Show live serial data on webpage

Leave a Reply

Your email address will not be published. Required fields are marked *