Arduino + PHP + Mysql

Asked

Viewed 841 times

2

Guys, I know little of PHP I’m having some doubts... I’m wanting 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. Only before that, I’m having trouble sending this data and showing on the php page... I’m using xampp v3.2.2 with php7.0 . The idea of the code is to take an ip and print it in the serial, after that, when pressing the '1' key the Arduino should send data and request something from php, as well as, from there, be possible to view 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. Sensor 2 received and sensor 3 received... Não entendo o motivo deste erro. No sensor 2 recebido e sensor 3 recebido...

I don’t understand the reason for this mistake. If I fill out the requests right at the url, I don’t have this problem... when I expect to get through the Rduino, it happens this way. Não entendo o motivo deste erro. Se eu preencho as requisições direto na url, não tenho esse problema... quando eu espero obter pelo arduino, acontece isso.

PHP code:

<?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;
?>
  • This is the full php code you are using?

  • In the print you submitted, you are trying to access the url without passing parameters S1, s2 and S3. I don’t know about Adian, but apparently, he’s functional

  • @Evertonneri on the Arduino he is breaking line in improper place, so only the S1 goes, and the s2 and S3 fail. See the explanation in the posted answer. Watch out for the print sent by Arduino: https://i.stack.Imgur.com/UVKJP.png - S1 went well, gave no error, because it was before the println of the value. The following items were not considered as being part of the following lines

  • @Evertonneri was what Bacco explained. It worked partially... but when accessing the url nothing happens. It seems that I’m not concatenating php in the right way or the information does not reach php... I don’t know how to debug this question.

  • Solved, I traveled here one hour. But you opened my eyes, it was already bitolado. Thank you guys. Thanks !!!

2 answers

3


Your problem is using println(). The correct is print() in that 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 only one. O println() lines breaking. You may notice that the s1 had gone right by 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 the querystring.

-3

Your code is not interpreting get method completely, you can for example see literally everything (for testing), in php file:

You can literally know everything that is going by querystring :).

This I mentioned was to READ ALL GET METHOD, but in this case maybe your Arduíno is not sending the parameters...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.