Post topics, source code that relate to the Arduino Platform

User avatar
By LizardOnTheRoad
#74950 Hi everyone,

I am currently building a module that is able to send data to a webpage. I am using an MKR ZERO Arduino. Basically, I am able so far to send a simple HTML GET request which is treated by a php file. Then, it sends everything to a mySQL data base. The GET request is sent from the Arduino to the webpage. Then, the php file does the rest of the job. First, this is what my arduino code looks like :
Code: Select allvoid sendSQL(){
  Serial1.println("AT+CIPSTART=1,\"TCP\",\"" + server + "\",80");
  delay(200);
  if( Serial1.find("OK")){
    Serial.println("TCP connection ready");
   
    delay(200);

    String postRequest = "GET " + uri + "?location=" + "\'" + location + "\'" +
      "&sun_influence=" + "\'" + sunInfluence + "\'" + "&module_name=" + "\'" + moduleName + "\'" + "
      HTTP/1.1\r\nHost: " + server + "\r\n\r\n";
 
    Serial1.println("AT+CIPSEND=1," + String(postRequest.length())); 
    Serial.println("AT+CIPSEND=1," + String(postRequest.length())); 
    delay(200);
    if(Serial1.available()){
      if(Serial1.find(">")) {
        Serial.println("Sending..");
        Serial.print(postRequest);
        Serial1.print(postRequest);
        delay(200);
        if(Serial1.find("SEND OK")){
          Serial.println("Packet sent");
          Serial1.println("AT+CIPCLOSE=1");
          delay(200);           
        }
        else
          Serial.println("Packet cannot be sent");
      }
      else
        Serial.println("ERROR");
    }
    else
      Serial.println("Nothing received");
  }
  else
    Serial.println("TCP connection failed");   
}


The php file looks like this :
Code: Select all<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "gestion_lux";

// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$val1 = $_GET['location'];
$val2 = $_GET['sun_influence'];
$val3 = $_GET['module_name'];

$sql = "INSERT INTO room(location,sun_influence,module_name) VALUES ($val1,$val2,$val3);";

if ($conn->query($sql) === TRUE) {
    echo "Room parameters Saved Successfully!";
} else {
    echo "Error:" . $sql . "<br>" . $conn->error;
}

$conn->close();

?>


All of that part works pretty well! Unfortunately, I won't be able to use this solution in my case. Indeed, that would mean sending data at each end of a test (approximately 1 minute), which is a waste of energy. And in my situation, my module is supplied by a 3.3V battery. My idea is to gather a maximum of data in the SD card of my MKR ZERO, and to send everything at once every 30 minutes, for example. Then, I would waste less energy.

The thing is I'm not that good in HTTP/Php treatment, that's why I'm asking for some help. My idea is to create a text file in my SD CARD (I already know how to do that), and to send it after 30 minutes to the webpage. Is it possible to do that through a HTML request? Then, how am I suppose to treat the data in the php file? The php should be able to send each test to the data base line per line. Maybe my solution is not smart, so I'm open about hearing any kind of suggestion!

Thank you very much!

Maxime.