Skip to content

Commit d5dc704

Browse files
committed
Added Barometric pressure Web Server example for Ethernet library
1 parent 0f2355a commit d5dc704

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
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+
byte ip[] = {
35+
192,169,1,20 };
36+
byte gateway[] = {
37+
192,168,1,1};
38+
byte subnet[] = {
39+
255, 255, 255, 0 };
40+
41+
42+
// Initialize the Ethernet server library
43+
// with the IP address and port you want to use
44+
// (port 80 is default for HTTP):
45+
Server server(80);
46+
47+
48+
//Sensor's memory register addresses:
49+
const int PRESSURE = 0x1F; //3 most significant bits of pressure
50+
const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure
51+
const int TEMPERATURE = 0x21; //16 bit temperature reading
52+
53+
// pins used for the connection with the sensor
54+
// the others you need are controlled by the SPI library):
55+
const int dataReadyPin = 6;
56+
const int chipSelectPin = 7;
57+
58+
float temperature = 0.0;
59+
long pressure = 0;
60+
long lastReadingTime = 0;
61+
62+
void setup() {
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+
// start the SPI library:
72+
SPI.begin();
73+
Serial.begin(9600);
74+
75+
//Configure SCP1000 for low noise configuration:
76+
writeRegister(0x02, 0x2D);
77+
writeRegister(0x01, 0x03);
78+
writeRegister(0x03, 0x02);
79+
80+
// give the sensor and Ethernet shield time to set up:
81+
delay(1000);
82+
83+
//Set the sensor to high resolution mode tp start readings:
84+
writeRegister(0x03, 0x0A);
85+
86+
}
87+
88+
void loop() {
89+
// check for a reading no more than once a second.
90+
if (millis() - lastReadingTime > 1000){
91+
// if there's a reading ready, read it:
92+
// don't do anything until the data ready pin is high:
93+
if (digitalRead(dataReadyPin) == HIGH) {
94+
getData();
95+
// timestamp the last time you got a reading:
96+
lastReadingTime = millis();
97+
}
98+
}
99+
100+
// listen for incoming Ethernet connections:
101+
listenForClients();
102+
}
103+
104+
105+
void getData() {
106+
Serial.println("Getting reading");
107+
//Read the temperature data
108+
int tempData = readRegister(0x21, 2);
109+
110+
// convert the temperature to celsius and display it:
111+
temperature = (float)tempData / 20.0;
112+
113+
//Read the pressure data highest 3 bits:
114+
byte pressureDataHigh = readRegister(0x1F, 1);
115+
pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0
116+
117+
//Read the pressure data lower 16 bits:
118+
unsigned int pressureDataLow = readRegister(0x20, 2);
119+
//combine the two parts into one 19-bit number:
120+
pressure = ((pressureDataHigh << 16) | pressureDataLow)/4;
121+
122+
Serial.print("Temperature: ");
123+
Serial.print(temperature);
124+
Serial.println(" degrees C");
125+
Serial.print("Pressure: " + String(pressure));
126+
Serial.println(" Pa");
127+
}
128+
129+
void listenForClients() {
130+
// listen for incoming clients
131+
Client client = server.available();
132+
if (client) {
133+
Serial.println("Got a client");
134+
// an http request ends with a blank line
135+
boolean currentLineIsBlank = true;
136+
while (client.connected()) {
137+
if (client.available()) {
138+
char c = client.read();
139+
// if you've gotten to the end of the line (received a newline
140+
// character) and the line is blank, the http request has ended,
141+
// so you can send a reply
142+
if (c == '\n' && currentLineIsBlank) {
143+
// send a standard http response header
144+
client.println("HTTP/1.1 200 OK");
145+
client.println("Content-Type: text/html");
146+
client.println();
147+
// print the current readings, in HTML format:
148+
client.print("Temperature: ");
149+
client.print(temperature);
150+
client.print(" degrees C");
151+
client.println("<br />");
152+
client.print("Pressure: " + String(pressure));
153+
client.print(" Pa");
154+
client.println("<br />");
155+
break;
156+
}
157+
if (c == '\n') {
158+
// you're starting a new line
159+
currentLineIsBlank = true;
160+
}
161+
else if (c != '\r') {
162+
// you've gotten a character on the current line
163+
currentLineIsBlank = false;
164+
}
165+
}
166+
}
167+
// give the web browser time to receive the data
168+
delay(1);
169+
// close the connection:
170+
client.stop();
171+
}
172+
}
173+
174+
175+
//Send a write command to SCP1000
176+
void writeRegister(byte registerName, byte registerValue) {
177+
// SCP1000 expects the register name in the upper 6 bits
178+
// of the byte:
179+
registerName <<= 2;
180+
// command (read or write) goes in the lower two bits:
181+
registerName |= 0b00000010; //Write command
182+
183+
// take the chip select low to select the device:
184+
digitalWrite(chipSelectPin, LOW);
185+
186+
SPI.send(registerName); //Send register location
187+
SPI.send(registerValue); //Send value to record into register
188+
189+
// take the chip select high to de-select:
190+
digitalWrite(chipSelectPin, HIGH);
191+
}
192+
193+
194+
//Read register from the SCP1000:
195+
unsigned int readRegister(byte registerName, int numBytes) {
196+
byte inByte = 0; // incoming from the SPI read
197+
unsigned int result = 0; // result to return
198+
199+
// SCP1000 expects the register name in the upper 6 bits
200+
// of the byte:
201+
registerName <<= 2;
202+
// command (read or write) goes in the lower two bits:
203+
registerName &= 0b11111100; //Read command
204+
205+
// take the chip select low to select the device:
206+
digitalWrite(chipSelectPin, LOW);
207+
// send the device the register you want to read:
208+
int command = SPI.send(registerName);
209+
// send a value of 0 to read the first byte returned:
210+
inByte = SPI.send(0x00);
211+
212+
result = inByte;
213+
// if there's more than one byte returned,
214+
// shift the first byte then get the second byte:
215+
if (numBytes > 1){
216+
result = inByte << 8;
217+
inByte = SPI.send(0x00);
218+
result = result |inByte;
219+
}
220+
// take the chip select high to de-select:
221+
digitalWrite(chipSelectPin, HIGH);
222+
// return the result:
223+
return(result);
224+
}

0 commit comments

Comments
 (0)