Arduino mandar GET to PHP page

Asked

Viewed 1,721 times

5

I have the following code on the Arduin:

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

#define DHTPIN 2 
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

IPAddress ip(192, 168, 5, 177);
IPAddress subnet( 255, 255, 192, 0);
IPAddress gateway( 192, 168, 63, 254);

EthernetServer server(80);

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  float hi = dht.computeHeatIndex(f, h);

  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n' && currentLineIsBlank) {
            client.println("HTTP/1.1 200 OK");
            client.print("GET http://meudominio.com.br/teste_arduino.php?nivel=elevado");
            client.println("Content-Type: text/html");
            client.println("Connection: close");  
            client.println("Refresh: 5");  
            client.println();
            client.println("<!DOCTYPE HTML>");
            client.println("<html>");
              client.print("Humidity: "); 
              client.print(h);
              client.print(" %\t");
              client.print("Temperature: "); 
              client.print(t);
              client.print(" *C ");
              client.print(f);
              client.print(" *F\t");
              client.print("Heat index: ");
              client.print(hi);
              client.println(" *F <br>");

       for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
        int sensorReading = analogRead(analogChannel);
        client.print("analog input ");
        client.print(analogChannel);
        client.print(" is ");
        client.print(sensorReading);
        client.println("<br />");
      }
      client.println("</html>");
      break;
    }
    if (c == '\n') {
      // you're starting a new line
      currentLineIsBlank = true;
    } else if (c != '\r') {
      // you've gotten a character on the current line
      currentLineIsBlank = false;
    }
  }
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
Ethernet.maintain();
  }
}

The code works correctly, I can access it by the IP address and view the data, however, I have the following line: client.print("GET http://meudominio.com.br/teste_arduino.php?nivel=elevado");

And with this line I would like Arduino to send a get to the specified PHP page. However, it is not doing this. What could I have done wrong?

1 answer

1

You can replace your line:

client.print("GET http://meudominio.com.br/teste_arduino.php?nivel=elevado");

For the following:

client.println("GET http://meudominio.com.br/teste_arduino.php?nivel=elevado");

Or else by:

client.println("GET http://meudominio.com.br/teste_arduino.php?nivel=elevado HTTP/1.1");

And watch if this way you can get an answer.

Browser other questions tagged

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