Beruflich Dokumente
Kultur Dokumente
#include
#include
#include
"DHT.h"
<Ethernet.h>
<SPI.h>
<Wire.h>
#define DHTPIN 3
// Declara constante y pin de entrada de DHT11
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Se introducen la IP y la MAC
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 206);
// Inicializa la libreria ethernet, el puerto 80 esta por defecto para HTTP
EthernetServer server(80);
void setup() {
dht.begin();
// Abrir comunicacion serial y esperar que el puerto responda
Serial.begin(9600);
while (!Serial) {
; // Esperar a que el puerto serial se conecte (solo para Arduino Leonardo)
}
// Inicializar la conexion Ethernet y el servidor
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
//Termina setup
}
void loop() {
// Lee variables del sensor
int h = dht.readHumidity();
int t = dht.readTemperature();
// Escucha clientes de entrada
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// Una solicitud http cuya linea esta en blanco
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// Si la linea en la solicitud http esta en blanco y el cliente esta dis
ponible se puede enviar una respuesta
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// agregar una etiqueta meta que recargue la pagina cada seg
undo
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
// La salida evalua los valores de temperatura y humedad de DHT11
client.print("JHG SERVICIOS AMBIENTALES");
client.println("<br />");
client.println("<br />");
client.print("Sensor DHT11");
client.println("<br />");
client.print("Temperatura=");
client.print(t);
client.print("<sup>0</sup>");
client.print("C");
client.println("<br />");
client.print("Humedad=");
client.print(h);
client.print("%");
client.println("<br />");
client.println("</html>");
break;
}
if (c == '\n') {
// linea en blanco
currentLineIsBlank = true;
}
else if (c != '\r') {
// si hay un caracter en la linea en blanco
currentLineIsBlank = false;
}
}
}
// Dar al navegador web el tiempo para recibir los datos
delay(1);
// Se cierra la conexion
client.stop();
Serial.println("client disonnected");
}
}
/*
Repeating Web client
This sketch connects to a a web server and makes a request
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
This example uses DNS, by assigning the Ethernet client with a MAC address,
IP address, and DNS address.
Circuit:
loop() {
if there's incoming data from the net connection.
send it out the serial port. This is for debugging
purposes only:
(client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}