mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-17 22:23:10 +03:00
Updated PachubeClientString and PachubeClient examples for Ethernet
This commit is contained in:
@ -16,7 +16,7 @@
|
|||||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||||
|
|
||||||
created 15 March 2010
|
created 15 March 2010
|
||||||
updated 27 Feb 2012
|
updated 16 Mar 2012
|
||||||
by Tom Igoe with input from Usman Haque and Joe Saavedra
|
by Tom Igoe with input from Usman Haque and Joe Saavedra
|
||||||
|
|
||||||
http://arduino.cc/en/Tutorial/PachubeClient
|
http://arduino.cc/en/Tutorial/PachubeClient
|
||||||
@ -27,8 +27,6 @@ http://arduino.cc/en/Tutorial/PachubeClient
|
|||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
|
#define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here
|
||||||
#define FEEDID 00000 // replace your feed ID
|
#define FEEDID 00000 // replace your feed ID
|
||||||
#define USERAGENT "My Project" // user agent is the project name
|
#define USERAGENT "My Project" // user agent is the project name
|
||||||
@ -45,9 +43,14 @@ IPAddress ip(10,0,1,20);
|
|||||||
// initialize the library instance:
|
// initialize the library instance:
|
||||||
EthernetClient client;
|
EthernetClient client;
|
||||||
|
|
||||||
long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
// if you don't want to use DNS (and reduce your sketch size)
|
||||||
|
// use the numeric IP instead of the name for the server:
|
||||||
|
IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
|
||||||
|
//char server[] = "api.pachube.com"; // name address for pachube API
|
||||||
|
|
||||||
|
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
||||||
boolean lastConnected = false; // state of the connection last time through the main loop
|
boolean lastConnected = false; // state of the connection last time through the main loop
|
||||||
const int postingInterval = 10000; //delay between updates to Pachube.com
|
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// start serial port:
|
// start serial port:
|
||||||
@ -93,13 +96,13 @@ void loop() {
|
|||||||
// this method makes a HTTP connection to the server:
|
// this method makes a HTTP connection to the server:
|
||||||
void sendData(int thisData) {
|
void sendData(int thisData) {
|
||||||
// if there's a successful connection:
|
// if there's a successful connection:
|
||||||
if (client.connect("www.pachube.com", 80)) {
|
if (client.connect(server, 80)) {
|
||||||
Serial.println("connecting...");
|
Serial.println("connecting...");
|
||||||
// send the HTTP PUT request:
|
// send the HTTP PUT request:
|
||||||
client.print("PUT /v2/feeds/");
|
client.print("PUT /v2/feeds/");
|
||||||
client.print(FEEDID);
|
client.print(FEEDID);
|
||||||
client.println(".csv HTTP/1.1");
|
client.println(".csv HTTP/1.1");
|
||||||
client.print("Host: api.pachube.com\n");
|
client.println("Host: api.pachube.com");
|
||||||
client.print("X-PachubeApiKey: ");
|
client.print("X-PachubeApiKey: ");
|
||||||
client.println(APIKEY);
|
client.println(APIKEY);
|
||||||
client.print("User-Agent: ");
|
client.print("User-Agent: ");
|
||||||
@ -112,15 +115,14 @@ void sendData(int thisData) {
|
|||||||
client.println(thisLength);
|
client.println(thisLength);
|
||||||
|
|
||||||
// last pieces of the HTTP PUT request:
|
// last pieces of the HTTP PUT request:
|
||||||
client.print("Content-Type: text/csv\n");
|
client.println("Content-Type: text/csv");
|
||||||
client.println("Connection: close\n");
|
client.println("Connection: close");
|
||||||
|
client.println();
|
||||||
|
|
||||||
// here's the actual content of the PUT request:
|
// here's the actual content of the PUT request:
|
||||||
client.print("sensor1,");
|
client.print("sensor1,");
|
||||||
client.println(thisData);
|
client.println(thisData);
|
||||||
|
|
||||||
// note the time that the connection was made:
|
|
||||||
lastConnectionTime = millis();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// if you couldn't make a connection:
|
// if you couldn't make a connection:
|
||||||
@ -128,8 +130,9 @@ void sendData(int thisData) {
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("disconnecting.");
|
Serial.println("disconnecting.");
|
||||||
client.stop();
|
client.stop();
|
||||||
lastConnected = client.connected();
|
|
||||||
}
|
}
|
||||||
|
// note the time that the connection was made or attempted:
|
||||||
|
lastConnectionTime = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||||
|
|
||||||
created 15 March 2010
|
created 15 March 2010
|
||||||
updated 27 Feb 2012
|
updated 16 Mar 2012
|
||||||
by Tom Igoe with input from Usman Haque and Joe Saavedra
|
by Tom Igoe with input from Usman Haque and Joe Saavedra
|
||||||
|
|
||||||
http://arduino.cc/en/Tutorial/PachubeClientString
|
http://arduino.cc/en/Tutorial/PachubeClientString
|
||||||
@ -40,14 +40,19 @@
|
|||||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
||||||
// fill in an available IP address on your network here,
|
// fill in an available IP address on your network here,
|
||||||
// for manual configuration:
|
// for manual configuration:
|
||||||
IPAddress ip(10,0,0,20);
|
IPAddress ip(10,0,1,20);
|
||||||
|
|
||||||
// initialize the library instance:
|
// initialize the library instance:
|
||||||
EthernetClient client;
|
EthernetClient client;
|
||||||
|
|
||||||
|
// if you don't want to use DNS (and reduce your sketch size)
|
||||||
|
// use the numeric IP instead of the name for the server:
|
||||||
|
//IPAddress server(216,52,233,122); // numeric IP for api.pachube.com
|
||||||
|
char server[] = "api.pachube.com"; // name address for pachube API
|
||||||
|
|
||||||
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
||||||
boolean lastConnected = false; // state of the connection last time through the main loop
|
boolean lastConnected = false; // state of the connection last time through the main loop
|
||||||
const unsigned long postingInterval = 10000; //delay between updates to Pachube.com
|
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// start serial port:
|
// start serial port:
|
||||||
@ -105,29 +110,27 @@ void loop() {
|
|||||||
// this method makes a HTTP connection to the server:
|
// this method makes a HTTP connection to the server:
|
||||||
void sendData(String thisData) {
|
void sendData(String thisData) {
|
||||||
// if there's a successful connection:
|
// if there's a successful connection:
|
||||||
if (client.connect("api.pachube.com", 80)) {
|
if (client.connect(server, 80)) {
|
||||||
Serial.println("connecting...");
|
Serial.println("connecting...");
|
||||||
// send the HTTP PUT request:
|
// send the HTTP PUT request:
|
||||||
client.print("PUT /v2/feeds/");
|
client.print("PUT /v2/feeds/");
|
||||||
client.print(FEEDID);
|
client.print(FEEDID);
|
||||||
client.println(".csv HTTP/1.1");
|
client.println(".csv HTTP/1.1");
|
||||||
client.print("Host: api.pachube.com\n");
|
client.println("Host: api.pachube.com");
|
||||||
client.print("X-PachubeApiKey: ");
|
client.print("X-PachubeApiKey: ");
|
||||||
client.println(APIKEY);
|
client.println(APIKEY);
|
||||||
client.print("User-Agent: ");
|
client.print("User-Agent: ");
|
||||||
client.println(USERAGENT);
|
client.println(USERAGENT);
|
||||||
client.print("Content-Length: ");
|
client.print("Content-Length: ");
|
||||||
client.println(thisData.length(), DEC);
|
client.println(thisData.length());
|
||||||
|
|
||||||
// last pieces of the HTTP PUT request:
|
// last pieces of the HTTP PUT request:
|
||||||
client.print("Content-Type: text/csv\n");
|
client.println("Content-Type: text/csv");
|
||||||
client.println("Connection: close\n");
|
client.println("Connection: close");
|
||||||
|
client.println();
|
||||||
|
|
||||||
// here's the actual content of the PUT request:
|
// here's the actual content of the PUT request:
|
||||||
client.println(thisData);
|
client.println(thisData);
|
||||||
|
|
||||||
// note the time that the connection was made:
|
|
||||||
lastConnectionTime = millis();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// if you couldn't make a connection:
|
// if you couldn't make a connection:
|
||||||
@ -135,8 +138,8 @@ void sendData(String thisData) {
|
|||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.println("disconnecting.");
|
Serial.println("disconnecting.");
|
||||||
client.stop();
|
client.stop();
|
||||||
lastConnected = client.connected();
|
|
||||||
}
|
}
|
||||||
|
// note the time that the connection was made or attempted:
|
||||||
|
lastConnectionTime = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user