Arduino - Ethernet Shield - Connection with server

0

I have a server and an Arduino with a temperature sensor, but I'm not able to do a POST of values taken by the sensor on the server page.

Can anyone help me? Follow the code.

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

byte mac []= { 0x58, 0x3B, 0xBB, 0xFD, 0x1B, 0x79 }; //MAC ADDRESS 
IPAddress ip(172,16,27,88);          //Define o endereco IP
IPAddress gateway(172,16,0,234);     //Define o gateway
IPAddress subnet(255, 255, 0, 0); //Define a máscara de rede
EthernetClient client;

#define DHTPIN 2 // pino do sensor
#define DHTTYPE DHT11 // tipo do sensor nosso caso DHT11
DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 1000; // intervalo de leitura(milisegundos)

int t = 0;  // temperatura
int h = 0;  // umidade
String data;

void setup() { 
  Serial.begin(115200);
  Ethernet.begin(mac,ip,gateway,subnet);
  /*if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP"); 
  }*/

  dht.begin(); 
  delay(10000); // certo tempo para o sensor se ambientar no local

  h = (int) dht.readHumidity(); 
  t = (int) dht.readTemperature(); 

  data = "";
}

void loop(){

    h =  dht.readHumidity();
    t =  dht.readTemperature();

  data = '"';
  data.concat("{");
  data.concat('"');
  data.concat("temperatura");
  data.concat('"');
  data.concat(":");
  data.concat('"');
  data.concat(t); // concatenando os valores em uma unica string
  data.concat('"');
  data.concat(", ");
  data.concat('"');
  data.concat("umidade");
  data.concat('"');
  data.concat(":");
  data.concat('"');
  data.concat(h);
  data.concat('"');
  data.concat("}");
  data.concat('"');
  // exibir a string com os daados e a leitura no serial
  Serial.println(data);
  Serial.print("Leitura = ");
  Serial.print(t);
  Serial.print(" (celsius), ");
  Serial.print(h);
  Serial.println(" (% umidade)");
  //if (client.connect("http://172.16.254.11/kit-iot/",8080)) {
    if (client.connect("172.16.254.11",80)) {
      client.println("POST /kit-iot/sensores/1 HTTP/1.1"); 
      client.println("Host: 172.16.254.11");
      client.println("Content-Type: application/x-www-form-urlencoded"); 
      client.print("Content-Length: "); 
      client.println(data.length()); 
      client.println(); 
      client.print(data); 
  } 
  else{
    Serial.println("Erro de conexao...");
  }

  if (client.connected()) { 
    client.stop();  // desconectar
  }

  delay(300000); // esperar 5 minutos
}
    
asked by anonymous 07.04.2016 / 18:56

0 answers