BarometricPressureWebServer.ino 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. SCP1000 Barometric Pressure Sensor Display
  3. Serves the output of a Barometric Pressure Sensor as a web page.
  4. Uses the SPI library. For details on the sensor, see:
  5. http://www.sparkfun.com/commerce/product_info.php?products_id=8161
  6. http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
  7. This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
  8. http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
  9. Circuit:
  10. SCP1000 sensor attached to pins 6,7, and 11 - 13:
  11. DRDY: pin 6
  12. CSB: pin 7
  13. MOSI: pin 11
  14. MISO: pin 12
  15. SCK: pin 13
  16. created 31 July 2010
  17. by Tom Igoe
  18. */
  19. #include <Ethernet2.h>
  20. // the sensor communicates using SPI, so include the library:
  21. #include <SPI.h>
  22. // assign a MAC address for the ethernet controller.
  23. // fill in your address here:
  24. byte mac[] = {
  25. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  26. };
  27. // assign an IP address for the controller:
  28. IPAddress ip(192, 168, 1, 20);
  29. IPAddress gateway(192, 168, 1, 1);
  30. IPAddress subnet(255, 255, 255, 0);
  31. // Initialize the Ethernet server library
  32. // with the IP address and port you want to use
  33. // (port 80 is default for HTTP):
  34. EthernetServer server(80);
  35. //Sensor's memory register addresses:
  36. const int PRESSURE = 0x1F; //3 most significant bits of pressure
  37. const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
  38. const int TEMPERATURE = 0x21; //16 bit temperature reading
  39. // pins used for the connection with the sensor
  40. // the others you need are controlled by the SPI library):
  41. const int dataReadyPin = 6;
  42. const int chipSelectPin = 7;
  43. float temperature = 0.0;
  44. long pressure = 0;
  45. long lastReadingTime = 0;
  46. void setup() {
  47. // start the SPI library:
  48. SPI.begin();
  49. // start the Ethernet connection and the server:
  50. Ethernet.begin(mac, ip);
  51. server.begin();
  52. // initalize the data ready and chip select pins:
  53. pinMode(dataReadyPin, INPUT);
  54. pinMode(chipSelectPin, OUTPUT);
  55. Serial.begin(9600);
  56. //Configure SCP1000 for low noise configuration:
  57. writeRegister(0x02, 0x2D);
  58. writeRegister(0x01, 0x03);
  59. writeRegister(0x03, 0x02);
  60. // give the sensor and Ethernet shield time to set up:
  61. delay(1000);
  62. //Set the sensor to high resolution mode tp start readings:
  63. writeRegister(0x03, 0x0A);
  64. }
  65. void loop() {
  66. // check for a reading no more than once a second.
  67. if (millis() - lastReadingTime > 1000) {
  68. // if there's a reading ready, read it:
  69. // don't do anything until the data ready pin is high:
  70. if (digitalRead(dataReadyPin) == HIGH) {
  71. getData();
  72. // timestamp the last time you got a reading:
  73. lastReadingTime = millis();
  74. }
  75. }
  76. // listen for incoming Ethernet connections:
  77. listenForEthernetClients();
  78. }
  79. void getData() {
  80. Serial.println("Getting reading");
  81. //Read the temperature data
  82. int tempData = readRegister(0x21, 2);
  83. // convert the temperature to celsius and display it:
  84. temperature = (float)tempData / 20.0;
  85. //Read the pressure data highest 3 bits:
  86. byte pressureDataHigh = readRegister(0x1F, 1);
  87. pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0
  88. //Read the pressure data lower 16 bits:
  89. unsigned int pressureDataLow = readRegister(0x20, 2);
  90. //combine the two parts into one 19-bit number:
  91. pressure = ((pressureDataHigh << 16) | pressureDataLow) / 4;
  92. Serial.print("Temperature: ");
  93. Serial.print(temperature);
  94. Serial.println(" degrees C");
  95. Serial.print("Pressure: " + String(pressure));
  96. Serial.println(" Pa");
  97. }
  98. void listenForEthernetClients() {
  99. // listen for incoming clients
  100. EthernetClient client = server.available();
  101. if (client) {
  102. Serial.println("Got a client");
  103. // an http request ends with a blank line
  104. boolean currentLineIsBlank = true;
  105. while (client.connected()) {
  106. if (client.available()) {
  107. char c = client.read();
  108. // if you've gotten to the end of the line (received a newline
  109. // character) and the line is blank, the http request has ended,
  110. // so you can send a reply
  111. if (c == '\n' && currentLineIsBlank) {
  112. // send a standard http response header
  113. client.println("HTTP/1.1 200 OK");
  114. client.println("Content-Type: text/html");
  115. client.println();
  116. // print the current readings, in HTML format:
  117. client.print("Temperature: ");
  118. client.print(temperature);
  119. client.print(" degrees C");
  120. client.println("<br />");
  121. client.print("Pressure: " + String(pressure));
  122. client.print(" Pa");
  123. client.println("<br />");
  124. break;
  125. }
  126. if (c == '\n') {
  127. // you're starting a new line
  128. currentLineIsBlank = true;
  129. }
  130. else if (c != '\r') {
  131. // you've gotten a character on the current line
  132. currentLineIsBlank = false;
  133. }
  134. }
  135. }
  136. // give the web browser time to receive the data
  137. delay(1);
  138. // close the connection:
  139. client.stop();
  140. }
  141. }
  142. //Send a write command to SCP1000
  143. void writeRegister(byte registerName, byte registerValue) {
  144. // SCP1000 expects the register name in the upper 6 bits
  145. // of the byte:
  146. registerName <<= 2;
  147. // command (read or write) goes in the lower two bits:
  148. registerName |= 0b00000010; //Write command
  149. // take the chip select low to select the device:
  150. digitalWrite(chipSelectPin, LOW);
  151. SPI.transfer(registerName); //Send register location
  152. SPI.transfer(registerValue); //Send value to record into register
  153. // take the chip select high to de-select:
  154. digitalWrite(chipSelectPin, HIGH);
  155. }
  156. //Read register from the SCP1000:
  157. unsigned int readRegister(byte registerName, int numBytes) {
  158. byte inByte = 0; // incoming from the SPI read
  159. unsigned int result = 0; // result to return
  160. // SCP1000 expects the register name in the upper 6 bits
  161. // of the byte:
  162. registerName <<= 2;
  163. // command (read or write) goes in the lower two bits:
  164. registerName &= 0b11111100; //Read command
  165. // take the chip select low to select the device:
  166. digitalWrite(chipSelectPin, LOW);
  167. // send the device the register you want to read:
  168. int command = SPI.transfer(registerName);
  169. // send a value of 0 to read the first byte returned:
  170. inByte = SPI.transfer(0x00);
  171. result = inByte;
  172. // if there's more than one byte returned,
  173. // shift the first byte then get the second byte:
  174. if (numBytes > 1) {
  175. result = inByte << 8;
  176. inByte = SPI.transfer(0x00);
  177. result = result | inByte;
  178. }
  179. // take the chip select high to de-select:
  180. digitalWrite(chipSelectPin, HIGH);
  181. // return the result:
  182. return(result);
  183. }