Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / Ethernet / examples / BarometricPressureWebServer / BarometricPressureWebServer.ino @ 58d82c77

History | View | Annotate | Download (6.44 KB)

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
// assign an IP address for the controller:
34
IPAddress ip(192,168,1,20);
35
IPAddress gateway(192,168,1,1);	
36
IPAddress subnet(255, 255, 255, 0);
37

    
38

    
39
// Initialize the Ethernet server library
40
// with the IP address and port you want to use 
41
// (port 80 is default for HTTP):
42
EthernetServer server(80);
43

    
44

    
45
//Sensor's memory register addresses:
46
const int PRESSURE = 0x1F;      //3 most significant bits of pressure
47
const int PRESSURE_LSB = 0x20;  //16 least significant bits of pressure
48
const int TEMPERATURE = 0x21;   //16 bit temperature reading
49

    
50
// pins used for the connection with the sensor
51
// the others you need are controlled by the SPI library):
52
const int dataReadyPin = 6; 
53
const int chipSelectPin = 7;
54

    
55
float temperature = 0.0;
56
long pressure = 0;
57
long lastReadingTime = 0;
58

    
59
void setup() {
60
  // start the SPI library:
61
  SPI.begin();
62

    
63
  // start the Ethernet connection and the server:
64
  Ethernet.begin(mac, ip);
65
  server.begin();
66

    
67
  // initalize the  data ready and chip select pins:
68
  pinMode(dataReadyPin, INPUT);
69
  pinMode(chipSelectPin, OUTPUT);
70

    
71
  Serial.begin(9600);
72

    
73
  //Configure SCP1000 for low noise configuration:
74
  writeRegister(0x02, 0x2D);
75
  writeRegister(0x01, 0x03);
76
  writeRegister(0x03, 0x02);
77

    
78
  // give the sensor and Ethernet shield time to set up:
79
  delay(1000);
80

    
81
  //Set the sensor to high resolution mode tp start readings:
82
  writeRegister(0x03, 0x0A);
83

    
84
}
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

    
98
  // listen for incoming Ethernet connections:
99
  listenForEthernetClients();
100
}
101

    
102

    
103
void getData() {
104
  Serial.println("Getting reading");
105
  //Read the temperature data
106
  int tempData = readRegister(0x21, 2);
107

    
108
  // convert the temperature to celsius and display it:
109
  temperature = (float)tempData / 20.0;
110

    
111
  //Read the pressure data highest 3 bits:
112
  byte  pressureDataHigh = readRegister(0x1F, 1);   
113
  pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0
114

    
115
  //Read the pressure data lower 16 bits:
116
  unsigned int pressureDataLow = readRegister(0x20, 2);    
117
  //combine the two parts into one 19-bit number:
118
  pressure = ((pressureDataHigh << 16) | pressureDataLow)/4;
119

    
120
  Serial.print("Temperature: ");
121
  Serial.print(temperature);
122
  Serial.println(" degrees C");
123
  Serial.print("Pressure: " + String(pressure));
124
  Serial.println(" Pa");
125
}
126

    
127
void listenForEthernetClients() {
128
  // listen for incoming clients
129
  EthernetClient client = server.available();
130
  if (client) {
131
    Serial.println("Got a client");
132
    // an http request ends with a blank line
133
    boolean currentLineIsBlank = true;
134
    while (client.connected()) {
135
      if (client.available()) {
136
        char c = client.read();
137
        // if you've gotten to the end of the line (received a newline
138
        // character) and the line is blank, the http request has ended,
139
        // so you can send a reply
140
        if (c == '\n' && currentLineIsBlank) {
141
          // send a standard http response header
142
          client.println("HTTP/1.1 200 OK");
143
          client.println("Content-Type: text/html");
144
          client.println();
145
          // print the current readings, in HTML format:
146
          client.print("Temperature: ");
147
          client.print(temperature);
148
          client.print(" degrees C");
149
          client.println("<br />");
150
          client.print("Pressure: " + String(pressure));
151
          client.print(" Pa");
152
          client.println("<br />");  
153
          break;
154
        }
155
        if (c == '\n') {
156
          // you're starting a new line
157
          currentLineIsBlank = true;
158
        } 
159
        else if (c != '\r') {
160
          // you've gotten a character on the current line
161
          currentLineIsBlank = false;
162
        }
163
      }
164
    }
165
    // give the web browser time to receive the data
166
    delay(1);
167
    // close the connection:
168
    client.stop();
169
  }
170
} 
171

    
172

    
173
//Send a write command to SCP1000
174
void writeRegister(byte registerName, byte registerValue) {
175
  // SCP1000 expects the register name in the upper 6 bits
176
  // of the byte:
177
  registerName <<= 2;
178
  // command (read or write) goes in the lower two bits:
179
  registerName |= 0b00000010; //Write command
180

    
181
  // take the chip select low to select the device:
182
  digitalWrite(chipSelectPin, LOW); 
183

    
184
  SPI.transfer(registerName); //Send register location
185
  SPI.transfer(registerValue); //Send value to record into register
186

    
187
  // take the chip select high to de-select:
188
  digitalWrite(chipSelectPin, HIGH); 
189
}
190

    
191

    
192
//Read register from the SCP1000:
193
unsigned int readRegister(byte registerName, int numBytes) {
194
  byte inByte = 0;           // incoming from  the SPI read
195
  unsigned int result = 0;   // result to return 
196

    
197
  // SCP1000 expects the register name in the upper 6 bits
198
  // of the byte:
199
  registerName <<=  2;
200
  // command (read or write) goes in the lower two bits:
201
  registerName &= 0b11111100; //Read command
202

    
203
  // take the chip select low to select the device:
204
  digitalWrite(chipSelectPin, LOW); 
205
  // send the device the register you want to read:
206
  int command = SPI.transfer(registerName); 
207
  // send a value of 0 to read the first byte returned:
208
  inByte = SPI.transfer(0x00); 
209
  
210
  result = inByte;
211
  // if there's more than one byte returned, 
212
  // shift the first byte then get the second byte:
213
  if (numBytes > 1){
214
    result = inByte << 8;
215
    inByte = SPI.transfer(0x00); 
216
    result = result |inByte;
217
  }
218
  // take the chip select high to de-select:
219
  digitalWrite(chipSelectPin, HIGH); 
220
  // return the result:
221
  return(result);
222
}