Skip to content

Commit 82159aa

Browse files
Ethernet lib: add default examples
1 parent 2c8a6ab commit 82159aa

File tree

18 files changed

+1723
-353
lines changed

18 files changed

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

0 commit comments

Comments
 (0)