Azuria

Arduino - Måling af akkumulationstank med netvisning

Oct 23rd, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.89 KB | None | 0 0
  1. /*
  2.   Web  Server
  3.  
  4.  A simple web server that shows the value of the analog input pins.
  5.  using an Arduino Wiznet Ethernet shield.
  6.  
  7.  Circuit:
  8.  * Ethernet shield attached to pins 10, 11, 12, 13
  9.  * Analog inputs attached to pins A0 through A5 (optional)
  10.  
  11.  created 18 Dec 2009
  12.  by David A. Mellis
  13.  modified 4 Sep 2010
  14.  by Tom Igoe
  15.  
  16.  */
  17.  
  18. #include <SPI.h>
  19. #include <Ethernet.h>
  20. #include <OneWire.h>
  21. #include <DallasTemperature.h>
  22.  
  23. // Data wire is plugged into pin 3 on the Arduino
  24. #define ONE_WIRE_BUS 3
  25.  
  26. // Setup a oneWire instance to communicate with any OneWire devices
  27. OneWire oneWire(ONE_WIRE_BUS);
  28.  
  29. // Pass our oneWire reference to Dallas Temperature.
  30. DallasTemperature sensors(&oneWire);
  31.  
  32. // Assign the addresses of your 1-Wire temp sensors.
  33. // See the tutorial on how to obtain these addresses:
  34. // http://www.hacktronics.com/Tutorials/arduino-1-wire-address-finder.html
  35.  
  36. DeviceAddress tanktopThermometer = { 0x28, 0xD5, 0x83, 0x34, 0x03, 0x00, 0x00, 0x16 };
  37. DeviceAddress tankmidtThermometer = { 0x28, 0x94, 0x50, 0x32, 0x03, 0x00, 0x00, 0x4D };
  38. DeviceAddress tankbundThermometer = { 0x28, 0xEE, 0x80, 0x32, 0x03, 0x00, 0x00, 0x44 };
  39. DeviceAddress arduinoThermometer = { 0x28, 0xD3, 0x4D, 0x32, 0x03, 0x00, 0x00, 0x2D };
  40.  
  41.  
  42. // Enter a MAC address and IP address for your controller below.
  43. // The IP address will be dependent on your local network:
  44. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  45. byte ip[] = { 172,16,1, 177 };
  46.  
  47. // Initialize the Ethernet server library
  48. // with the IP address and port you want to use
  49. // (port 80 is default for HTTP):
  50. Server server(80);
  51.  
  52. void setup()
  53. {
  54.   // start the Ethernet connection and the server:
  55.   Ethernet.begin(mac, ip);
  56.   server.begin();
  57.  
  58.   sensors.begin();
  59.   // set the resolution to 10 bit (good enough?)
  60.   sensors.setResolution(tanktopThermometer, 10);
  61.   sensors.setResolution(tankmidtThermometer, 10);
  62.   sensors.setResolution(tankbundThermometer, 10);
  63.   sensors.setResolution(arduinoThermometer, 10);
  64. }
  65.  
  66. void printTemperature(DeviceAddress deviceAddress)
  67. {
  68.   float tempC = sensors.getTempC(deviceAddress);
  69.   if (tempC == -127.00) {
  70.     Serial.println("Error getting temperature");
  71.   } else {
  72.     Serial.println("C: ");
  73.     Serial.println(tempC);
  74.     //Serial.print(" F: ");
  75.     //Serial.print(DallasTemperature::toFahrenheit(tempC));
  76.   }
  77. }
  78.  
  79. void loop()
  80. {
  81.  
  82.           //Serial.print("Getting temperatures...\n\r");
  83.           sensors.requestTemperatures();
  84.  
  85.   // listen for incoming clients
  86.   Client client = server.available();
  87.   if (client) {
  88.     // an http request ends with a blank line
  89.     boolean currentLineIsBlank = true;
  90.     while (client.connected()) {
  91.       if (client.available()) {
  92.         char c = client.read();
  93.         // if you've gotten to the end of the line (received a newline
  94.         // character) and the line is blank, the http request has ended,
  95.         // so you can send a reply
  96.         if (c == '\n' && currentLineIsBlank) {
  97.           // send a standard http response header
  98.           client.println("HTTP/1.1 200 OK");
  99.           client.println("Content-Type: text/html");
  100.           client.println();
  101.           client.println(sensors.getTempC(arduinoThermometer));
  102.           client.println(", ");
  103.           client.println(sensors.getTempC(tanktopThermometer));
  104.           client.println(", ");
  105.           client.println(sensors.getTempC(tankmidtThermometer));
  106.           client.println(", ");
  107.           client.println(sensors.getTempC(tankbundThermometer));
  108.           break;
  109.         }
  110.         if (c == '\n') {
  111.           // you're starting a new line
  112.           currentLineIsBlank = true;
  113.         }
  114.         else if (c != '\r') {
  115.           // you've gotten a character on the current line
  116.           currentLineIsBlank = false;
  117.         }
  118.       }
  119.     }
  120.     // give the web browser time to receive the data
  121.     delay(1);
  122.     // close the connection:
  123.  
  124.  
  125.     client.stop();
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment