0% found this document useful (0 votes)
61 views2 pages

Arduino Code Using DHT Sensor Plus Ethernet

This code is reading temperature and humidity values from a DHT11 sensor connected to an Arduino board. It includes the necessary libraries and defines the sensor pin and type. The code initializes Ethernet and the sensor, then reads the sensor values in a loop every 250 seconds (4 minutes). It builds a string of the temperature and humidity values and sends an HTTP POST request to a server to upload the sensor data.

Uploaded by

Boboy Cortezo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views2 pages

Arduino Code Using DHT Sensor Plus Ethernet

This code is reading temperature and humidity values from a DHT11 sensor connected to an Arduino board. It includes the necessary libraries and defines the sensor pin and type. The code initializes Ethernet and the sensor, then reads the sensor values in a loop every 250 seconds (4 minutes). It builds a string of the temperature and humidity values and sends an HTTP POST request to a server to upload the sensor data.

Uploaded by

Boboy Cortezo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <DHT.

h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;
#define DHTPIN 6 // SENSOR PIN
#define DHTTYPE DHT11 // SENSOR TYPE � THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE
MODELS
DHT dht(DHTPIN, DHTTYPE);
long previousMillis = 0;
unsigned long currentMillis = 0;
long interval = 250000; // READING INTERVAL
int t = 0; // TEMPERATURE VAR
int h = 0; // HUMIDITY VAR
String data;
void setup() {
Serial.begin(115200);
pinMode(7, OUTPUT); // sets the digital pin as output
pinMode(5, OUTPUT); // sets the digital pin as output
digitalWrite(7, HIGH);
digitalWrite(5, LOW);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
}
dht.begin();
delay(10000); // GIVE THE SENSOR SOME TIME TO START
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
data = "";
Serial.println("connecting...");
}
void loop(){
currentMillis = millis();
if(currentMillis - previousMillis > interval) { // READ ONLY ONCE PER INTERVAL
previousMillis = currentMillis;
h = (int) dht.readHumidity();
t = (int) dht.readTemperature();
}
data = "temp1=";
data.concat(t);
data.concat("&hum1=");
data.concat(h);
if (client.connect("www.arduinosi.x10host.com",80)) { // REPLACE WITH YOUR SERVER
ADDRESS
client.println("POST /new/add.php HTTP/1.1");
client.println("Host: arduinosi.x10host.com"); // SERVER ADDRESS HERE TOO
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);

Serial.print("Temperature= ");
Serial.println(t);
Serial.print("Humidity= ");
Serial.println(h);

}
if (client.connected()) {
client.stop(); // DISCONNECT FROM THE SERVER
}
delay(60000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
}

You might also like