Advertisement
Guest User

GalTemp

a guest
Oct 9th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <OneWire.h> //1
  4. #include <DallasTemperature.h>
  5.  
  6. //The data wire for the indoor sensor is plugged into pin d5 on the Arduino
  7. // on pin 5 & 6 (a 4.7K resistor is necessary) - the temperature probe has one built in.
  8. //2
  9. #define TEMPERATURE_INDOOR 5
  10. #define TEMPERATURE_OUTDOOR 6
  11.  
  12. const int ledPin = 7;
  13. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x7D, 0x54 };
  14.  
  15. //3
  16. //setup an instance to communicate with your sensor
  17. OneWire oneWireIndoor(TEMPERATURE_INDOOR);
  18. DallasTemperature sensorIndoor(&oneWireIndoor);
  19.  
  20. //4
  21. OneWire oneWireOutdoor(TEMPERATURE_OUTDOOR);
  22. DallasTemperature sensorOutdoor(&oneWireOutdoor);
  23.  
  24. EthernetServer server(80);
  25. const int ledPin = 7;
  26. byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x7D, 0x54 }; //1
  27.  
  28. EthernetServer server(80); //2
  29.  
  30. void setup(void) {
  31. pinMode(ledPin, OUTPUT); //1
  32. Serial.begin(9600);
  33. Serial.println("LED Pin setup for output on pin ");
  34. digitalWrite(ledPin, HIGH);
  35.  
  36. sensorIndoor.begin();
  37. sensorOutdoor.begin();
  38.  
  39. // start the Ethernet connection and the server:
  40. Serial.println("Trying to get an IP address using DHCP");
  41. digitalWrite(ledPin, HIGH); //2
  42.  
  43. // start the Ethernet connection and the server:
  44. Serial.println("Trying to get an IP address using DHCP");
  45.  
  46. if (Ethernet.begin(mac) == 0) { //3
  47. Serial.println("Failed to configure Ethernet using DHCP");
  48. while (true) {
  49. digitalWrite(ledPin, HIGH);
  50. delay(200);
  51. digitalWrite(ledPin, LOW);
  52. delay(200);
  53. }
  54. }
  55. Serial.println();
  56. // start listening for clients
  57. Serial.print("server is at "); //4
  58. Serial.println(Ethernet.localIP());
  59.  
  60. digitalWrite(ledPin, LOW);
  61.  
  62.  
  63. }
  64. void blinkLED(void)
  65. {
  66. digitalWrite(ledPin, HIGH);
  67. delay(500);
  68. digitalWrite(ledPin, LOW);
  69. delay(500);
  70.  
  71. return;
  72. }
  73. void loop(void) {
  74. client.println();
  75.  
  76. temperatureIndoor = readTemperatureCelsius(sensorIndoor);
  77. temperatureOutdoor = readTemperatureCelsius(sensorOutdoor);
  78.  
  79. client.print("{\"arduino\":[{\"location\":\"indoor\",\"celsius\":\"");
  80. // listen for incoming clients
  81. EthernetClient client = server.available();
  82. if (client) {
  83. Serial.println("new client");
  84. // an http request ends with a blank line
  85. boolean currentLineIsBlank = true;
  86. while (client.connected()) {
  87. if (client.available()) {
  88. char c = client.read();
  89. Serial.write(c);
  90. // if you've gotten to the end of the line (received a newline
  91. // character) and the line is blank, the http request has ended,
  92. // so you can send a reply
  93. if (c == '\n' && currentLineIsBlank) {
  94. // send a standard http response header
  95. client.println("HTTP/1.1 200 OK");
  96. client.println("Content-Type: text/html");
  97. client.println("Connnection: close");
  98. client.println();
  99. client.println("<!DOCTYPE HTML>");
  100. client.println("<html><body>Aquaryoum</body></html>");
  101. break;
  102. }
  103. if (c == '\n') {
  104. // you're starting a new line
  105. currentLineIsBlank = true;
  106. }
  107. else if (c != '\r') {
  108. // you've gotten a character on the current line
  109. currentLineIsBlank = false;
  110. }
  111. }
  112. }
  113. // give the web browser time to receive the data
  114. delay(1);
  115. // close the connection:
  116. client.stop();
  117. Serial.println("client disonnected");
  118. blinkLED();
  119.  
  120. float readTemperatureCelsius(DallasTemperature sensor) {
  121. sensor.requestTemperatures();
  122. float temperature = sensor.getTempCByIndex(0);
  123. Serial.print("Celsius Temperature for device is: ");
  124. Serial.println(temperature); //zero is first sensor if we had multiple on bus
  125. return temperature;
  126. }
  127. }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement