Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example testing sketch for various DHT humidity/temperature sensors
- // Written by ladyada, public domain
- #include "DHT.h"
- #include <UIPEthernet.h> // Used for Ethernet
- // **** SENSOR SETTING ****
- #define DHTPIN 2 // what pin we're connected to
- // Uncomment whatever type you're using!
- #define DHTTYPE DHT11 // DHT 11
- //#define DHTTYPE DHT22 // DHT 22 (AM2302)
- //#define DHTTYPE DHT21 // DHT 21 (AM2301)
- // Initialize DHT sensor for normal 16mhz Arduino
- DHT dht(DHTPIN, DHTTYPE);
- // **** ETHERNET SETTING ****
- byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
- IPAddress ip(192, 168, 1, 179);
- EthernetServer server(80);
- void setup() {
- Serial.begin(9600);
- Serial.println("DHTxx test!");
- Serial.println("Ethernet WebServer Example");
- dht.begin();
- Ethernet.begin(mac, ip);
- server.begin();
- Serial.print("IP Address: ");
- Serial.println(Ethernet.localIP());
- }
- void loop() {
- // Wait a few seconds between measurements.
- delay(2000);
- // Reading temperature or humidity takes about 250 milliseconds!
- // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
- float h = dht.readHumidity();
- // Read temperature as Celsius
- float t = dht.readTemperature();
- // Read temperature as Fahrenheit
- float f = dht.readTemperature(true);
- // Check if any reads failed and exit early (to try again).
- if (isnan(h) || isnan(t)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- Serial.print("Humidity: ");
- Serial.print(h);
- Serial.print(" %\t");
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.print(" *C\t");
- Serial.print("\n");
- // listen for incoming clients
- EthernetClient client = server.available();
- if (client)
- {
- Serial.println("-> New Connection");
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- while (client.connected())
- {
- if (client.available())
- {
- char c = client.read();
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- if (c == '\n' && currentLineIsBlank)
- {
- client.println("<html><title>Hello World!</title><body><h3>Hello World!</h3></body>");
- 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(10);
- // close the connection:
- client.stop();
- Serial.println(" Disconnected\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement