Moving recent commits to new-extension branch

This commit is contained in:
Tom Igoe 2011-05-21 13:22:43 -04:00
parent 2711c9910b
commit 4805fea3b2
5 changed files with 393 additions and 0 deletions

View File

@ -0,0 +1,57 @@
/*
Serial Event example
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(200);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
Serial.println(inputString);
// clear the string:
inputString = "";
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new byte comes in the
hardware serial RX. Don't do complex things here, as thge
processor halts the regular program to run this routine:
*/
void serialEvent() {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}

View File

@ -0,0 +1,54 @@
/*
DHCP-based IP printer
This sketch uses the DHCP extensions to the Ethernet library
to get an IP address via DHCP and print the address obtained.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 12 April 2011
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
Client client;
void setup() {
// start the serial library:
Serial.begin(9600);
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");z
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// print your local IP address:
Serial.print("My IP address: ");
IPAddress myIPAddress = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(myIPAddress[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
void loop() {
}

View File

@ -0,0 +1,80 @@
/*
DHCP Chat Server
A simple server that distributes any incoming messages to all
connected clients. To use telnet to your device's IP address and type.
You can see the client's input in the serial monitor as well.
Using an Arduino Wiznet Ethernet shield.
THis version attempts to get an IP address using DHCP
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
created 21 May 2011
by Tom Igoe
Based on ChatServer example by David A. Mellis
*/
#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.
// gateway and subnet are optional:
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress ip(192,168,1, 177);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
// telnet defaults to port 23
Server server(23);
boolean gotAMessage = false; // whether or not you got a message from the client yet
void setup() {
// open the serial port
Serial.begin(9600);
// start the Ethernet connection:
Serial.println("Trying to get an IP address using DHCP");
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// initialize the ethernet device not using DHCP:
Ethernet.begin(mac, ip, gateway, subnet);
}
// print your local IP address:
Serial.print("My IP address: ");
ip = Ethernet.localIP();
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(ip[thisByte], DEC);
Serial.print(".");
}
Serial.println();
// start listening for clients
server.begin();
}
void loop() {
// wait for a new client:
Client client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!gotAMessage) {
Serial.println("We have a new client");
client.println("Hello, client!");
gotAMessage = true;
}
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.print(thisChar);
}
}

View File

@ -0,0 +1,124 @@
/*
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
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:
Client client;
const int requestInterval = 60000; // delay between requests
char serverName[] = "api.twitter.com"; // twitter URL
boolean requested; // whether you've made a request since connecting
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);
// initialize serial:
Serial.begin(9600);
// attempt a DHCP connection:
if (!Ethernet.begin(mac)) {
// if DHCP fails, start with a hard-coded address:
Ethernet.begin(mac, ip);
}
// 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();
}
// note the time of this connect attempt:
lastAttemptTime = millis();
}

View File

@ -0,0 +1,78 @@
/*
Software serial multple serial test
Receives from the two software serial ports,
sends to the hardware serial port.
In order to listen on a software port, you call port.listen().
When using two software serial ports, you have to switch ports
by listen()ing on each one in turn. Pick a logical time to switch
ports, like the end of an expected transmission, or when the
buffer is empty. This example switches ports when there is nothing
more to read from a port
The circuit:
Two devices which communicate serially are needed.
* First serial device's TX attached to digital pin 2, RX to pin 3
* Second serial device's TX attached to digital pin 4, RX to pin 5
created 18 Apr. 2011
by Tom Igoe
based on Mikal Hart's twoPortRXExample
This example code is in the public domain.
*/
#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 2, RX = digital pin 3
SoftwareSerial portOne(2, 3);
// software serial #2: TX = digital pin 4, RX = digital pin 5
SoftwareSerial portTwo(4, 5);
void setup()
{
// Start the hardware serial port
Serial.begin(9600);
// Start each software serial port
portOne.begin(9600);
portTwo.begin(9600);
}
void loop()
{
// By default, the last intialized port is listening.
// when you want to listen on a port, explicitly select it:
portOne.listen();
Serial.println("Data from port one:");
// while there is data coming in, read it
// and send to the hardware serial port:
while (portOne.available() > 0) {
char inByte = portOne.read();
Serial.write(inByte);
}
// blank line to separate data from the two ports:
Serial.println();
// Now listen on the second port
portTwo.listen();
// while there is data coming in, read it
// and send to the hardware serial port:
Serial.println("Data from port two:");
while (portTwo.available() > 0) {
char inByte = portTwo.read();
Serial.write(inByte);
}
// blank line to separate data from the two ports:
Serial.println();
}