Stap 4: Een Bonus
In het geval u niet wilt gebruiken Thingspeak maar wil gewoon uw eigen webserver: dit programma gebruiken:
/* * DHT11 Sensor connected to Pin 2 <a href="http://arduino-info.wikispaces.com/ethernet-temp-humidity" rel="nofollow"> http://arduino-info.wikispaces.com/ethernet-temp-...> Based on code by David A. Mellis & Tom Igoe Adapted by diy_bloke * bmp180sensor on a4/a5 */ /*-----( Import needed libraries )-----*/ #include <SPI.h> #include <Ethernet.h> #include <dht11.h> #include <Wire.h> //#include <Adafruit_Sensor.h> //#include <Adafruit_BMP085_U.h> #include <Adafruit_BMP085.h> /*-----( Declare Constants and Pin Numbers )-----*/ #define DHT11PIN 2 // The Temperature/Humidity sensor // Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; /*-----( Declare objects )-----*/ IPAddress ip(192,168,1, 177); // Initialize the Ethernet server library // with the IP address and port you want to use // (port 80 is default for HTTP): EthernetServer server(80); //Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085); Adafruit_BMP085 bmp; dht11 DHT11; //The Sensor Object /*-----( Declare Variables )-----*/ void setup() /****** SETUP: RUNS ONCE ******/ { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection and the server: Ethernet.begin(mac, ip); server.begin(); Serial.print(F("server is at ")); Serial.println(Ethernet.localIP()); }//--(end setup )--- void loop() /*----( LOOP: RUNS OVER AND OVER AGAIN )----*/ { // listen for incoming clients EthernetClient client = server.available(); if (client) { Serial.println(F("new client")); // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); // 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) { // send a standard http response header 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>"); // add a meta refresh tag, so the browser pulls again every 5 seconds: client.println("<meta http-equiv=\"refresh\" content=\"5\">"); client.println("<head>"); client.println("<title>"); client.println(F("My Home Environment")); client.println("</title>"); client.println("</head>"); client.println("<body>"); client.print(F("<h3>My Home Environment</h3>")); //client.println("<br />"); /*----(Get sensor reading, calculate and print results )-----------------*/ int chk = DHT11.read(DHT11PIN); Serial.print(F("Read sensor: ")); switch (chk) { case 0: Serial.println(F("OK")); break; case -1: Serial.println(F("Checksum error")); break; case -2: Serial.println(F("Time out error")); break; default: Serial.println(F("Unknown error")); break; } /* Get a new sensor (BMP085) event */ // sensors_event_t event; // bmp.getEvent(&event); //-------------- client.print(F("<table style='border:1px solid black; background-color: white;'>")); client.print(F("<tr><td>")); client.print("<font color='red'>Temperature</font> (°C): "); client.print(F("</td><td align='right'>")); client.println((float)DHT11.temperature, 1); client.println(F("<br />")); client.print(F("</td></tr>")); client.print(F("<tr><td>")); client.print("<b>Temperature</b> (°F): "); client.print(F("</td><td align='right'>")); client.println(Fahrenheit(DHT11.temperature), 1); client.println("<br />"); client.print(F("</td></tr>")); client.print(F("<tr><td>")); client.print("<font color='blue'>Humidity</font> (%): "); client.print(F("</td><td align='right'>")); client.println((float)DHT11.humidity, 0); client.println("<br />"); client.print(F("</td></tr>")); client.print(F("<tr><td>")); /* client.print("<b>Temperature</b> (°K): "); client.println(Kelvin(DHT11.temperature), 1); client.println("<br />"); */ client.print("<i>Dew Point</i> (°C): "); client.print(F("</td><td align='right'>")); client.println(dewPoint(DHT11.temperature, DHT11.humidity)); client.println("<br />"); client.print(F("</td></tr>")); client.print(F("<tr><td>")); client.print("<i>Dew PointFast</i> (°C): "); client.print(F("</td><td align='right'>")); client.println(dewPointFast(DHT11.temperature, DHT11.humidity)); client.println("<br />"); client.print(F("</td></tr>")); client.print(F("<tr><td>")); if (!bmp.begin()) { client.print("geen sensor"); } else { client.print("Pressure (hPa): "); client.print(F("</td><td align='right'>")); client.println(bmp.readPressure()/100.0); client.print(F("</td></tr>")); client.print(F("<tr><td>")); client.print("Pressure (mmHg): "); client.print(F("</td><td align='right'>")); client.println(bmp.readPressure()/133.3); client.print(F("</td></tr>")); client.print(F("<tr><td>")); client.println("Temperature (°C): "); client.print(F("</td><td align='right'>")); client.print(bmp.readTemperature()); client.println("<br />"); } client.print(F("</td></tr>")); client.print(F("</table>")); /* Display the results (barometric pressure is measure in hPa) */ /* Display atmospheric pressure in hPa */ /* client.print("Pressure: "); client.println(event.pressure); client.println(" hPa"); client.println("<br />"); */ /*--------( End Sensor Read )--------------------------------*/ client.println("</body>"); 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(F("client disonnected")); } } // END Loop /*-----( Declare User-written Functions )-----*/ // //Celsius to Fahrenheit conversion double Fahrenheit(double celsius) { return 1.8 * celsius + 32; } //Celsius to Kelvin conversion double Kelvin(double celsius) { return celsius + 273.15; } // dewPoint function NOAA // reference: <a href="http://wahiduddin.net/calc/density_algorithms.htm" rel="nofollow"> http://arduino-info.wikispaces.com/ethernet-temp-...> double dewPoint(double celsius, double humidity) { double A0= 373.15/(273.15 + celsius); double SUM = -7.90298 * (A0-1); SUM += 5.02808 * log10(A0); SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ; SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ; SUM += log10(1013.246); double VP = pow(10, SUM-3) * humidity; double T = log(VP/0.61078); // temp var return (241.88 * T) / (17.558-T); } // delta max = 0.6544 wrt dewPoint() // 5x faster than dewPoint() // reference: <a href="http://en.wikipedia.org/wiki/Dew_point" rel="nofollow"> http://arduino-info.wikispaces.com/ethernet-temp-...> double dewPointFast(double celsius, double humidity) { double a = 17.271; double b = 237.7; double temp = (a * celsius) / (b + celsius) + log(humidity/100); double Td = (b * temp) / (a - temp); return Td; } /* ( THE END ) */