PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 line
7.3KB

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