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