Arduino send GET to PHP page

5

I have the following code in Arduino:

#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 through the IP address and view the data, however, I have the following line:     client.print ("GET link ");

And with this line I would like the arduino to send a get to the specified PHP page. However, he is not accomplishing this. What can I have done wrong?

    
asked by anonymous 23.03.2016 / 14:14

1 answer

1

You can replace your line:

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

By the following:

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

Or by:

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

And see if you can get a response this way.

    
11.06.2016 / 19:46