Arduino + PHP + MySQL

1

I do not know about PHP, so I'm having some doubts ... I'm trying to implement a connection of the three systems mentioned in the title. The idea is to capture two sensor values and send them to php and then to mysql. But before that, I'm having trouble sending this data and displaying it on the php page ... I'm using xampp v3.2.2 with php7.0. The idea of the code is to get an ip and printar in the serial, after that, when pressing the '1' key the arduino should send data and request some of the php, as well as, from that, it is possible to visualize the data both in the serial and on the page.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte servidor[] = {192, 168, 1, 67};
#define portaHTTP 80
EthernetClient clienteArduino;

//=============================================================
//  AREA PARA A DECLARACAO DOS SENSORES
float sensor1 = 1;
float sensor2 = 3;
float sensor3 = 5;
//=============================================================

void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac);

  if(Ethernet.begin(mac)== 0){

    Serial.println("Falha ao conectar a rede."); 
    Ethernet.begin(mac); 
  }

  Serial.print("Conectado a rede,no IP: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
//=============================================================
//  AREA PARA A LEITURA DOS SENSORES

//=============================================================


  if(clienteArduino.available()){
    char dadosRecebidos = clienteArduino.read();
    Serial.print(dadosRecebidos);  
  }

  if(!clienteArduino.connected()){
    clienteArduino.stop();  
  }

  char comando = Serial.read();

  if(comando == '1'){

   sensor1++;
   sensor2++;
   sensor3++;

   Serial.println("Conectando ao servidor e enviando dados: ");
   Serial.print("Sensor1: ");
   Serial.println(sensor1);
   Serial.print("Sensor2: ");
   Serial.println(sensor2);
   Serial.print("Sensor3: ");
   Serial.println(sensor3);

   if(clienteArduino.connect(servidor,portaHTTP)){

    //http://192.168.1.67/arduino_v3/teste.php?sensor1=5&sensor2=7&sensor3=9

     //clienteArduino.println("GET /arduino_v3/teste.php HTTP/1.0");

     clienteArduino.print("GET /arduino_v3/teste.php");
     clienteArduino.print("?s1=");
     clienteArduino.println(sensor1);
     clienteArduino.print("&s2=");
     clienteArduino.println(sensor2);
     clienteArduino.print("&s3=");
     clienteArduino.println(sensor3);
     clienteArduino.println(" HTTP/1.0");

     clienteArduino.println("HOST: 192.168.1.67 ");
     clienteArduino.println("Connection: Close");
     clienteArduino.println();


   } else {
      Serial.println("Falha na conexao com o servidor.");

   } 
  }



  //delay(3000);
}

I do not understand the reason for this error. In sensor 2 received and sensor 3 received ...

Idonotunderstandthereasonforthiserror.IfIfillouttherequisitionsdirectlyintheurl,Idonothavethisproblem...whenIexpecttogetthroughthearduino,thishappens.

PHPcode:

<?php$s1=$_GET['s1'];$s2=$_GET['s2'];$s3=$_GET['s3'];echo"Sensor 1 Recebido: ".$s1;
    echo "<br/>";
    echo "Sensor 2 Recebido: ".$s2;
    echo "<br/>";
    echo "Sensor 3 Recebido: ".$s3;
?>
    
asked by anonymous 09.09.2018 / 22:10

1 answer

1

Your problem is to use println() . The correct one is print() in this case.

 clienteArduino.print("GET /arduino_v3/teste.php");
 clienteArduino.print("?s1=");
 clienteArduino.print(sensor1);
 clienteArduino.print("&s2=");
 clienteArduino.print(sensor2);
 clienteArduino.print("&s3=");
 clienteArduino.print(sensor3);
 clienteArduino.println(" HTTP/1.0"); // Aqui sim é pra ter quebra, acabou a linha

 clienteArduino.println("HOST: 192.168.1.67 ");
 clienteArduino.println("Connection: Close");
 clienteArduino.println();

The request line is one line. println() breaks lines. You may notice that s1 had gone right through the Arduino.

In the case of direct testing by browser , it is normal to give error in the 3 variables, since you did not put querystring .     

10.09.2018 / 05:53