Skip to content

Commit c60b4a6

Browse files
committed
Merge branch 'master' of github.com:arduino/Arduino into LUFA_bootloader
2 parents dbfd9ed + 36e5a84 commit c60b4a6

File tree

4 files changed

+319
-12
lines changed

4 files changed

+319
-12
lines changed

EthernetClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ int EthernetClient::connect(IPAddress ip, uint16_t port) {
4141

4242
for (int i = 0; i < MAX_SOCK_NUM; i++) {
4343
uint8_t s = W5100.readSnSR(i);
44-
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
44+
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
4545
_sock = i;
4646
break;
4747
}

examples/CosmClient/CosmClient.ino

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
Cosm sensor client
3+
4+
This sketch connects an analog sensor to Cosm (http://www.cosm.com)
5+
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
6+
the Adafruit Ethernet shield, either one will work, as long as it's got
7+
a Wiznet Ethernet module on board.
8+
9+
This example has been updated to use version 2.0 of the cosm.com API.
10+
To make it work, create a feed with a datastream, and give it the ID
11+
sensor1. Or change the code below to match your feed.
12+
13+
14+
Circuit:
15+
* Analog sensor attached to analog in 0
16+
* Ethernet shield attached to pins 10, 11, 12, 13
17+
18+
created 15 March 2010
19+
updated 14 May 2012
20+
by Tom Igoe with input from Usman Haque and Joe Saavedra
21+
22+
http://arduino.cc/en/Tutorial/CosmClient
23+
This code is in the public domain.
24+
25+
*/
26+
27+
#include <SPI.h>
28+
#include <Ethernet.h>
29+
30+
#define APIKEY "YOUR API KEY GOES HERE" // replace your Cosm api key here
31+
#define FEEDID 00000 // replace your feed ID
32+
#define USERAGENT "My Project" // user agent is the project name
33+
34+
// assign a MAC address for the ethernet controller.
35+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
36+
// fill in your address here:
37+
byte mac[] = {
38+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
39+
40+
// fill in an available IP address on your network here,
41+
// for manual configuration:
42+
IPAddress ip(10,0,1,20);
43+
44+
// initialize the library instance:
45+
EthernetClient client;
46+
47+
// if you don't want to use DNS (and reduce your sketch size)
48+
// use the numeric IP instead of the name for the server:
49+
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
50+
char server[] = "api.cosm.com"; // name address for cosm API
51+
52+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
53+
boolean lastConnected = false; // state of the connection last time through the main loop
54+
const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com
55+
56+
void setup() {
57+
// start serial port:
58+
Serial.begin(9600);
59+
// start the Ethernet connection:
60+
if (Ethernet.begin(mac) == 0) {
61+
Serial.println("Failed to configure Ethernet using DHCP");
62+
// DHCP failed, so use a fixed IP address:
63+
Ethernet.begin(mac, ip);
64+
}
65+
}
66+
67+
void loop() {
68+
// read the analog sensor:
69+
int sensorReading = analogRead(A0);
70+
71+
// if there's incoming data from the net connection.
72+
// send it out the serial port. This is for debugging
73+
// purposes only:
74+
if (client.available()) {
75+
char c = client.read();
76+
Serial.print(c);
77+
}
78+
79+
// if there's no net connection, but there was one last time
80+
// through the loop, then stop the client:
81+
if (!client.connected() && lastConnected) {
82+
Serial.println();
83+
Serial.println("disconnecting.");
84+
client.stop();
85+
}
86+
87+
// if you're not connected, and ten seconds have passed since
88+
// your last connection, then connect again and send data:
89+
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
90+
sendData(sensorReading);
91+
}
92+
// store the state of the connection for next time through
93+
// the loop:
94+
lastConnected = client.connected();
95+
}
96+
97+
// this method makes a HTTP connection to the server:
98+
void sendData(int thisData) {
99+
// if there's a successful connection:
100+
if (client.connect(server, 80)) {
101+
Serial.println("connecting...");
102+
// send the HTTP PUT request:
103+
client.print("PUT /v2/feeds/");
104+
client.print(FEEDID);
105+
client.println(".csv HTTP/1.1");
106+
client.println("Host: api.cosm.com");
107+
client.print("X-ApiKey: ");
108+
client.println(APIKEY);
109+
client.print("User-Agent: ");
110+
client.println(USERAGENT);
111+
client.print("Content-Length: ");
112+
113+
// calculate the length of the sensor reading in bytes:
114+
// 8 bytes for "sensor1," + number of digits of the data:
115+
int thisLength = 8 + getLength(thisData);
116+
client.println(thisLength);
117+
118+
// last pieces of the HTTP PUT request:
119+
client.println("Content-Type: text/csv");
120+
client.println("Connection: close");
121+
client.println();
122+
123+
// here's the actual content of the PUT request:
124+
client.print("sensor1,");
125+
client.println(thisData);
126+
127+
}
128+
else {
129+
// if you couldn't make a connection:
130+
Serial.println("connection failed");
131+
Serial.println();
132+
Serial.println("disconnecting.");
133+
client.stop();
134+
}
135+
// note the time that the connection was made or attempted:
136+
lastConnectionTime = millis();
137+
}
138+
139+
140+
// This method calculates the number of digits in the
141+
// sensor reading. Since each digit of the ASCII decimal
142+
// representation is a byte, the number of digits equals
143+
// the number of bytes:
144+
145+
int getLength(int someValue) {
146+
// there's at least one byte:
147+
int digits = 1;
148+
// continually divide the value by ten,
149+
// adding one to the digit count for each
150+
// time you divide, until you're at 0:
151+
int dividend = someValue /10;
152+
while (dividend > 0) {
153+
dividend = dividend /10;
154+
digits++;
155+
}
156+
// return the number of digits:
157+
return digits;
158+
}
159+
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
Cosm sensor client with Strings
3+
4+
This sketch connects an analog sensor to Cosm (http://www.cosm.com)
5+
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
6+
the Adafruit Ethernet shield, either one will work, as long as it's got
7+
a Wiznet Ethernet module on board.
8+
9+
This example has been updated to use version 2.0 of the Cosm.com API.
10+
To make it work, create a feed with two datastreams, and give them the IDs
11+
sensor1 and sensor2. Or change the code below to match your feed.
12+
13+
This example uses the String library, which is part of the Arduino core from
14+
version 0019.
15+
16+
Circuit:
17+
* Analog sensor attached to analog in 0
18+
* Ethernet shield attached to pins 10, 11, 12, 13
19+
20+
created 15 March 2010
21+
updated 14 May 2012
22+
by Tom Igoe with input from Usman Haque and Joe Saavedra
23+
24+
http://arduino.cc/en/Tutorial/CosmClientString
25+
This code is in the public domain.
26+
27+
*/
28+
29+
#include <SPI.h>
30+
#include <Ethernet.h>
31+
32+
33+
#define APIKEY "YOUR API KEY GOES HERE" // replace your Cosm api key here
34+
#define FEEDID 00000 // replace your feed ID
35+
#define USERAGENT "My Project" // user agent is the project name
36+
37+
// assign a MAC address for the ethernet controller.
38+
// fill in your address here:
39+
byte mac[] = {
40+
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
41+
42+
// fill in an available IP address on your network here,
43+
// for manual configuration:
44+
IPAddress ip(10,0,1,20);
45+
46+
// initialize the library instance:
47+
EthernetClient client;
48+
49+
// if you don't want to use DNS (and reduce your sketch size)
50+
// use the numeric IP instead of the name for the server:
51+
//IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
52+
char server[] = "api.cosm.com"; // name address for Cosm API
53+
54+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
55+
boolean lastConnected = false; // state of the connection last time through the main loop
56+
const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com
57+
58+
void setup() {
59+
// start serial port:
60+
Serial.begin(9600);
61+
// give the ethernet module time to boot up:
62+
delay(1000);
63+
// start the Ethernet connection:
64+
if (Ethernet.begin(mac) == 0) {
65+
Serial.println("Failed to configure Ethernet using DHCP");
66+
// DHCP failed, so use a fixed IP address:
67+
Ethernet.begin(mac, ip);
68+
}
69+
}
70+
71+
void loop() {
72+
// read the analog sensor:
73+
int sensorReading = analogRead(A0);
74+
// convert the data to a String to send it:
75+
76+
String dataString = "sensor1,";
77+
dataString += sensorReading;
78+
79+
// you can append multiple readings to this String if your
80+
// Cosm feed is set up to handle multiple values:
81+
int otherSensorReading = analogRead(A1);
82+
dataString += "\nsensor2,";
83+
dataString += otherSensorReading;
84+
85+
// if there's incoming data from the net connection.
86+
// send it out the serial port. This is for debugging
87+
// purposes only:
88+
if (client.available()) {
89+
char c = client.read();
90+
Serial.print(c);
91+
}
92+
93+
// if there's no net connection, but there was one last time
94+
// through the loop, then stop the client:
95+
if (!client.connected() && lastConnected) {
96+
Serial.println();
97+
Serial.println("disconnecting.");
98+
client.stop();
99+
}
100+
101+
// if you're not connected, and ten seconds have passed since
102+
// your last connection, then connect again and send data:
103+
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
104+
sendData(dataString);
105+
}
106+
// store the state of the connection for next time through
107+
// the loop:
108+
lastConnected = client.connected();
109+
}
110+
111+
// this method makes a HTTP connection to the server:
112+
void sendData(String thisData) {
113+
// if there's a successful connection:
114+
if (client.connect(server, 80)) {
115+
Serial.println("connecting...");
116+
// send the HTTP PUT request:
117+
client.print("PUT /v2/feeds/");
118+
client.print(FEEDID);
119+
client.println(".csv HTTP/1.1");
120+
client.println("Host: api.cosm.com");
121+
client.print("X-ApiKey: ");
122+
client.println(APIKEY);
123+
client.print("User-Agent: ");
124+
client.println(USERAGENT);
125+
client.print("Content-Length: ");
126+
client.println(thisData.length());
127+
128+
// last pieces of the HTTP PUT request:
129+
client.println("Content-Type: text/csv");
130+
client.println("Connection: close");
131+
client.println();
132+
133+
// here's the actual content of the PUT request:
134+
client.println(thisData);
135+
}
136+
else {
137+
// if you couldn't make a connection:
138+
Serial.println("connection failed");
139+
Serial.println();
140+
Serial.println("disconnecting.");
141+
client.stop();
142+
}
143+
// note the time that the connection was made or attempted:
144+
lastConnectionTime = millis();
145+
}
146+

examples/PachubeClientString/PachubeClientString.ino

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
Pachube sensor client with Strings
2+
Cosm sensor client with Strings
33
4-
This sketch connects an analog sensor to Pachube (http://www.pachube.com)
4+
This sketch connects an analog sensor to Cosm (http://www.cosm.com)
55
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
66
the Adafruit Ethernet shield, either one will work, as long as it's got
77
a Wiznet Ethernet module on board.
88
9-
This example has been updated to use version 2.0 of the Pachube.com API.
9+
This example has been updated to use version 2.0 of the Cosm.com API.
1010
To make it work, create a feed with two datastreams, and give them the IDs
1111
sensor1 and sensor2. Or change the code below to match your feed.
1212
@@ -21,7 +21,7 @@
2121
modified 9 Apr 2012
2222
by Tom Igoe with input from Usman Haque and Joe Saavedra
2323
24-
http://arduino.cc/en/Tutorial/PachubeClientString
24+
http://arduino.cc/en/Tutorial/CosmClientString
2525
This code is in the public domain.
2626
2727
*/
@@ -30,14 +30,16 @@
3030
#include <Ethernet.h>
3131

3232

33-
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
33+
/#define APIKEY "YOUR API KEY GOES HERE" // replace your Cosm api key here
3434
#define FEEDID 00000 // replace your feed ID
3535
#define USERAGENT "My Project" // user agent is the project name
3636

37+
3738
// assign a MAC address for the ethernet controller.
3839
// fill in your address here:
3940
byte mac[] = {
4041
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
42+
4143
// fill in an available IP address on your network here,
4244
// for manual configuration:
4345
IPAddress ip(10,0,1,20);
@@ -47,12 +49,12 @@ EthernetClient client;
4749

4850
// if you don't want to use DNS (and reduce your sketch size)
4951
// use the numeric IP instead of the name for the server:
50-
//IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
51-
char server[] = "api.pachube.com"; // name address for pachube API
52+
IPAddress server(216,52,233,121); // numeric IP for api.cosm.com
53+
//char server[] = "api.cosm.com"; // name address for Cosm API
5254

5355
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
5456
boolean lastConnected = false; // state of the connection last time through the main loop
55-
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
57+
const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com
5658

5759
void setup() {
5860
// Open serial communications and wait for port to open:
@@ -81,7 +83,7 @@ void loop() {
8183
dataString += sensorReading;
8284

8385
// you can append multiple readings to this String if your
84-
// pachube feed is set up to handle multiple values:
86+
// Cosm feed is set up to handle multiple values:
8587
int otherSensorReading = analogRead(A1);
8688
dataString += "\nsensor2,";
8789
dataString += otherSensorReading;
@@ -121,8 +123,8 @@ void sendData(String thisData) {
121123
client.print("PUT /v2/feeds/");
122124
client.print(FEEDID);
123125
client.println(".csv HTTP/1.1");
124-
client.println("Host: api.pachube.com");
125-
client.print("X-PachubeApiKey: ");
126+
client.println("Host: api.cosm.com");
127+
client.print("X-CosmApiKey: ");
126128
client.println(APIKEY);
127129
client.print("User-Agent: ");
128130
client.println(USERAGENT);

0 commit comments

Comments
 (0)