Go Down

Topic: Save Arduino Data in MySQL database using PHP (Read 166 times) previous topic - next topic

beyam78

Hello people, I got an arduino UNO and a PM2.5 laser dust sensor. I wanna save the data I get from my sensor in a MySQL database using a PHP script( I don't have an Ethernet Shield). Here is the PHP script I wrote :

Code: [Select]
<?php 
include 
'PhpSerial.php';

// Let's start the class
$serial = new PhpSerial;

// First we must specify the device. This works on both linux and windows (if
// your linux serial device is /dev/ttyS0 for COM1, etc)
$serial->deviceSet("COM10");

// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(9600);

// Then we need to open it
$serial->deviceOpen();

//s'il y'a des donnees a envoyer
if(isset($_GET["data"])){

//connexion à la base de données
$req "mysql:host=localhost;dbname=arduino";
try{
$data $_GET["data"];
//$reponse = explode(" ", $data);
list($ID$PM01$PM25$PM10$date_et_heure)= explode(" "$data);
$bdd = new PDO($req"root""");

//inserer les données

$rep "INSERT INTO esclave(ID, PM01,PM25, PM10, date_et_heure) VALUES(:ID, :PM01, :PM25, :PM10, :date_et_heure)";

$response $bdd->prepare($rep);

$response->execute(array(

":ID" => $ID,
":PM01" => $PM01,
":PM25" => $PM25,
":PM10" => $PM10,
":date_et_heure" => date("Y-m-d H:i:s"),
));
//$req = explode(" ", $data);

}catch(Exception $e){
echo "erreur: " $e->getMessage();
}

//tri des données


}

?>





And here is arduino code I use to get data from the sensor. I just added a variable named "data" where I put all my data.

Code: [Select]
//******************************
 //*Abstract: Read value of PM1,PM2.5 and PM10 of air quality
 //
 //*Version:V3.1
 //*Author:Zuyang @ HUST
 //*Modified by Cain for Arduino Hardware Serial port compatibility
 //*Date:March.25.2016
 //******************************
#include <Arduino.h>
#define LENG 31   //0x42 + 31 bytes equal to 32 bytes
unsigned char buf[LENG];

int PM01Value=0;          //define PM1.0 value of the air detector module
int PM2_5Value=0;         //define PM2.5 value of the air detector module
int PM10Value=0;         //define PM10 value of the air detector module
String data;

void setup()
{
  Serial.begin(9600);   //use serial0
  Serial.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor

}

void loop()
{
  if(Serial.find(0x42)){    //start to read when detect 0x42
    Serial.readBytes(buf,LENG);

    if(buf[0] == 0x4d){
      if(checkValue(buf,LENG)){
        PM01Value=transmitPM01(buf); //count PM1.0 value of the air detector module
        PM2_5Value=transmitPM2_5(buf);//count PM2.5 value of the air detector module
        PM10Value=transmitPM10(buf); //count PM10 value of the air detector module
        data+=PM01Value;
        data+=" ";
        data+=PM2_5Value;
        data+=" ";
        data+=PM10Value;
             
      }
     
    }
  }

  static unsigned long OledTimer=millis(); 
    if (millis() - OledTimer >=1000)
    {
      OledTimer=millis();
     
      Serial.print("PM1.0: "); 
      Serial.print(PM01Value);
      Serial.println("  ug/m3");           
   
      Serial.print("PM2.5: "); 
      Serial.print(PM2_5Value);
      Serial.println("  ug/m3");     
     
      Serial.print("PM1 0: "); 
      Serial.print(PM10Value);
      Serial.println("  ug/m3");   
      Serial.println();
    }
 
}
char checkValue(unsigned char *thebuf, char leng)

  char receiveflag=0;
  int receiveSum=0;

  for(int i=0; i<(leng-2); i++){
  receiveSum=receiveSum+thebuf[i];
  }
  receiveSum=receiveSum + 0x42;
 
  if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the serial data
  {
    receiveSum = 0;
    receiveflag = 1;
  }
  return receiveflag;
}

int transmitPM01(unsigned char *thebuf)
{
  int PM01Val;
  PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
  return PM01Val;
}

//transmit PM Value to PC
int transmitPM2_5(unsigned char *thebuf)
{
  int PM2_5Val;
  PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
  return PM2_5Val;
  }

//transmit PM Value to PC
int transmitPM10(unsigned char *thebuf)
{
  int PM10Val;
  PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module 
  return PM10Val;
}


I installed WAMP and created a database which contains a table to get this values, I've also copy my PHP script in the www directory . But when I upload the sketch there is nothing added to my database

PaulS

Quote
But when I upload the sketch there is nothing added to my database
Of course not. You never read from the serial port. Instead, you expect that the php engine has somehow magically set the $_GET variable for the key named data. That clearly isn't going to happen, because the script is not being launched by the httpd daemon.
The art of getting good answers lies in asking good questions.

Go Up