1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-10-12 12:44:53 +03:00

Updates to a number of Examples

removed pachube examples, added xively examples. changes to the Servo
examples
This commit is contained in:
Scott Fitzgerald
2013-11-08 18:46:27 +04:00
parent 89d6841ac0
commit cb9686dc33
11 changed files with 78 additions and 530 deletions

View File

@@ -1,136 +0,0 @@
/*
Twitter Client with Strings
This sketch connects to Twitter using an Ethernet shield. It parses the XML
returned, and looks for <text>this is a tweet</text>
You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,
either one will work, as long as it's got a Wiznet Ethernet module on board.
This example uses the DHCP routines in the Ethernet library which is part of the
Arduino core from version 1.0 beta 1
This example uses the String library, which is part of the Arduino core from
version 0019.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011
modified 9 Apr 2012
by Tom Igoe
This code is in the public domain.
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192,168,1,20);
// initialize the library instance:
EthernetClient client;
const unsigned long requestInterval = 60000; // delay between requests
char serverName[] = "api.twitter.com"; // twitter URL
boolean requested; // whether you've made a request since connecting
unsigned long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
String currentLine = ""; // string to hold the text from server
String tweet = ""; // string to hold the tweet
boolean readingTweet = false; // if you're currently reading the tweet
void setup() {
// reserve space for the strings:
currentLine.reserve(256);
tweet.reserve(150);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// attempt a DHCP connection:
Serial.println("Attempting to get an IP address using DHCP:");
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Serial.println("failed to get an IP address using DHCP, trying manually");
Ethernet.begin(mac, ip);
}
Serial.print("My address:");
Serial.println(Ethernet.localIP());
// connect to Twitter:
connectToServer();
}
void loop()
{
if (client.connected()) {
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
}
// if the current line ends with <text>, it will
// be followed by the tweet:
if ( currentLine.endsWith("<text>")) {
// tweet is beginning. Clear the tweet string:
readingTweet = true;
tweet = "";
}
// if you're currently reading the bytes of a tweet,
// add them to the tweet String:
if (readingTweet) {
if (inChar != '<') {
tweet += inChar;
}
else {
// if you got a "<" character,
// you've reached the end of the tweet:
readingTweet = false;
Serial.println(tweet);
// close the connection to the server:
client.stop();
}
}
}
}
else if (millis() - lastAttemptTime > requestInterval) {
// if you're not connected, and two minutes have passed since
// your last connection, then attempt to connect again:
connectToServer();
}
}
void connectToServer() {
// attempt to connect, and wait a millisecond:
Serial.println("connecting to server...");
if (client.connect(serverName, 80)) {
Serial.println("making HTTP request...");
// make HTTP GET request to twitter:
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1");
client.println("HOST: api.twitter.com");
client.println("Connection: close");
client.println();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}

View File

@@ -1,12 +1,12 @@
/*
Pachube sensor client
Xively sensor client
This sketch connects an analog sensor to Pachube (http://www.pachube.com)
This sketch connects an analog sensor to Xively (http://www.xively.com)
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
This example has been updated to use version 2.0 of the Pachube.com API.
This example has been updated to use version 2.0 of the Xively.com API.
To make it work, create a feed with a datastream, and give it the ID
sensor1. Or change the code below to match your feed.
@@ -19,7 +19,7 @@
modified 9 Apr 2012
by Tom Igoe with input from Usman Haque and Joe Saavedra
http://arduino.cc/en/Tutorial/PachubeClient
http://arduino.cc/en/Tutorial/XivelyClient
This code is in the public domain.
*/
@@ -27,7 +27,7 @@ http://arduino.cc/en/Tutorial/PachubeClient
#include <SPI.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 xively api key here
#define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Project" // user agent is the project name
@@ -45,12 +45,12 @@ 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
IPAddress server(216,52,233,122); // numeric IP for api.xively.com
//char server[] = "api.xively.com"; // name address for xively 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
const unsigned long postingInterval = 10*1000; //delay between updates to Pachube.com
const unsigned long postingInterval = 10*1000; //delay between updates to Xively.com
void setup() {
// Open serial communications and wait for port to open:
@@ -107,8 +107,8 @@ void sendData(int thisData) {
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.pachube.com");
client.print("X-PachubeApiKey: ");
client.println("Host: api.xively.com");
client.print("X-XivelyApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);

View File

@@ -1,12 +1,12 @@
/*
Pachube sensor client with Strings
Xively sensor client with Strings
This sketch connects an analog sensor to Pachube (http://www.pachube.com)
This sketch connects an analog sensor to Xively (http://www.xively.com)
using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or
the Adafruit Ethernet shield, either one will work, as long as it's got
a Wiznet Ethernet module on board.
This example has been updated to use version 2.0 of the pachube.com API.
This example has been updated to use version 2.0 of the xively.com API.
To make it work, create a feed with two datastreams, and give them the IDs
sensor1 and sensor2. Or change the code below to match your feed.
@@ -23,7 +23,7 @@
modified 8 September 2012
by Scott Fitzgerald
http://arduino.cc/en/Tutorial/PachubeClientString
http://arduino.cc/en/Tutorial/XivelyClientString
This code is in the public domain.
*/
@@ -32,7 +32,7 @@
#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 Xively api key here
#define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Project" // user agent is the project name
@@ -51,12 +51,12 @@ 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,121); // numeric IP for api.pachube.com
//char server[] = "api.pachube.com"; // name address for pachube API
IPAddress server(216,52,233,121); // numeric IP for api.xively.com
//char server[] = "api.xively.com"; // name address for xively 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
const unsigned long postingInterval = 10*1000; //delay between updates to pachube.com
const unsigned long postingInterval = 10*1000; //delay between updates to xively.com
void setup() {
// Open serial communications and wait for port to open:
@@ -85,7 +85,7 @@ void loop() {
dataString += sensorReading;
// you can append multiple readings to this String if your
// pachube feed is set up to handle multiple values:
// xively feed is set up to handle multiple values:
int otherSensorReading = analogRead(A1);
dataString += "\nsensor2,";
dataString += otherSensorReading;
@@ -125,8 +125,8 @@ void sendData(String thisData) {
client.print("PUT /v2/feeds/");
client.print(FEEDID);
client.println(".csv HTTP/1.1");
client.println("Host: api.pachube.com");
client.print("X-pachubeApiKey: ");
client.println("Host: api.xively.com");
client.print("X-xivelyApiKey: ");
client.println(APIKEY);
client.print("User-Agent: ");
client.println(USERAGENT);