Skip to content

Commit 357dba7

Browse files
committed
Add examples
1 parent 27d36d8 commit 357dba7

File tree

12 files changed

+1503
-0
lines changed

12 files changed

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

0 commit comments

Comments
 (0)