Skip to content

Commit 469c026

Browse files
committed
Merge pull request #972 from Links2004/w5100
add Ethernet library for W5100 (#962)
2 parents 2f3f1f5 + 6e968aa commit 469c026

31 files changed

+4404
-0
lines changed

libraries/Ethernet/README.adoc

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
= Ethernet Library for Arduino =
2+
3+
With the Arduino Ethernet Shield, this library allows an Arduino board to connect to the internet.
4+
5+
For more information about this library please visit us at
6+
http://www.arduino.cc/en/Reference/Ethernet
7+
8+
modified to run on the ESP8266
9+
10+
== License ==
11+
12+
Copyright (c) 2010 Arduino LLC. All right reserved.
13+
14+
This library is free software; you can redistribute it and/or
15+
modify it under the terms of the GNU Lesser General Public
16+
License as published by the Free Software Foundation; either
17+
version 2.1 of the License, or (at your option) any later version.
18+
19+
This library is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22+
Lesser General Public License for more details.
23+
24+
You should have received a copy of the GNU Lesser General Public
25+
License along with this library; if not, write to the Free Software
26+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
* Analog inputs attached to pins A0 through A5 (optional)
13+
14+
created 18 Dec 2009
15+
by David A. Mellis
16+
modified 9 Apr 2012
17+
by Tom Igoe
18+
redesigned to make use of operator== 25 Nov 2013
19+
by Norbert Truchsess
20+
21+
*/
22+
23+
#include <SPI.h>
24+
#include <Ethernet.h>
25+
26+
// Enter a MAC address and IP address for your controller below.
27+
// The IP address will be dependent on your local network.
28+
// gateway and subnet are optional:
29+
byte mac[] = {
30+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
31+
IPAddress ip(192,168,1, 177);
32+
IPAddress gateway(192,168,1, 1);
33+
IPAddress subnet(255, 255, 0, 0);
34+
35+
36+
// telnet defaults to port 23
37+
EthernetServer server(23);
38+
39+
EthernetClient clients[4];
40+
41+
void setup() {
42+
// initialize the ethernet device
43+
Ethernet.begin(mac, ip, gateway, subnet);
44+
// start listening for clients
45+
server.begin();
46+
// Open serial communications and wait for port to open:
47+
Serial.begin(9600);
48+
while (!Serial) {
49+
; // wait for serial port to connect. Needed for Leonardo only
50+
}
51+
52+
53+
Serial.print("Chat server address:");
54+
Serial.println(Ethernet.localIP());
55+
}
56+
57+
void loop() {
58+
// wait for a new client:
59+
EthernetClient client = server.available();
60+
61+
// when the client sends the first byte, say hello:
62+
if (client) {
63+
64+
boolean newClient = true;
65+
for (byte i=0;i<4;i++) {
66+
//check whether this client refers to the same socket as one of the existing instances:
67+
if (clients[i]==client) {
68+
newClient = false;
69+
break;
70+
}
71+
}
72+
73+
if (newClient) {
74+
//check which of the existing clients can be overridden:
75+
for (byte i=0;i<4;i++) {
76+
if (!clients[i] && clients[i]!=client) {
77+
clients[i] = client;
78+
// clead out the input buffer:
79+
client.flush();
80+
Serial.println("We have a new client");
81+
client.print("Hello, client number: ");
82+
client.print(i);
83+
client.println();
84+
break;
85+
}
86+
}
87+
}
88+
89+
if (client.available() > 0) {
90+
// read the bytes incoming from the client:
91+
char thisChar = client.read();
92+
// echo the bytes back to all other connected clients:
93+
for (byte i=0;i<4;i++) {
94+
if (clients[i] && (clients[i]!=client)) {
95+
clients[i].write(thisChar);
96+
}
97+
}
98+
// echo the bytes to the server as well:
99+
Serial.write(thisChar);
100+
}
101+
}
102+
for (byte i=0;i<4;i++) {
103+
if (!(clients[i].connected())) {
104+
// client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
105+
clients[i].stop();
106+
}
107+
}
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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

Comments
 (0)