Skip to content

Commit 88ebc82

Browse files
committed
Merge branch 'new-extension' of github.com:arduino/Arduino into new-extension
2 parents 87a04fc + 4805fea commit 88ebc82

File tree

5 files changed

+393
-0
lines changed

5 files changed

+393
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Serial Event example
3+
4+
When new serial data arrives, this sketch adds it to a String.
5+
When a newline is received, the loop prints the string and
6+
clears it.
7+
8+
A good test for this is to try it with a GPS receiver
9+
that sends out NMEA 0183 sentences.
10+
11+
Created 9 May 2011
12+
by Tom Igoe
13+
14+
This example code is in the public domain.
15+
16+
http://www.arduino.cc/en/Tutorial/SerialEvent
17+
18+
*/
19+
20+
String inputString = ""; // a string to hold incoming data
21+
boolean stringComplete = false; // whether the string is complete
22+
23+
void setup() {
24+
// initialize serial:
25+
Serial.begin(9600);
26+
// reserve 200 bytes for the inputString:
27+
inputString.reserve(200);
28+
}
29+
30+
void loop() {
31+
// print the string when a newline arrives:
32+
if (stringComplete) {
33+
Serial.println(inputString);
34+
// clear the string:
35+
inputString = "";
36+
stringComplete = false;
37+
}
38+
}
39+
40+
/*
41+
SerialEvent occurs whenever a new byte comes in the
42+
hardware serial RX. Don't do complex things here, as thge
43+
processor halts the regular program to run this routine:
44+
*/
45+
void serialEvent() {
46+
// get the new byte:
47+
char inChar = (char)Serial.read();
48+
// add it to the inputString:
49+
inputString += inChar;
50+
// if the incoming character is a newline, set a flag
51+
// so the main loop can do something about it:
52+
if (inChar == '\n') {
53+
stringComplete = true;
54+
}
55+
}
56+
57+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
DHCP-based IP printer
3+
4+
This sketch uses the DHCP extensions to the Ethernet library
5+
to get an IP address via DHCP and print the address obtained.
6+
using an Arduino Wiznet Ethernet shield.
7+
8+
Circuit:
9+
* Ethernet shield attached to pins 10, 11, 12, 13
10+
11+
created 12 April 2011
12+
by Tom Igoe
13+
14+
*/
15+
16+
#include <SPI.h>
17+
#include <Ethernet.h>
18+
19+
// Enter a MAC address for your controller below.
20+
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
21+
byte mac[] = {
22+
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
23+
24+
// Initialize the Ethernet client library
25+
// with the IP address and port of the server
26+
// that you want to connect to (port 80 is default for HTTP):
27+
Client client;
28+
29+
void setup() {
30+
// start the serial library:
31+
Serial.begin(9600);
32+
// start the Ethernet connection:
33+
Serial.println("Trying to get an IP address using DHCP");z
34+
if (Ethernet.begin(mac) == 0) {
35+
Serial.println("Failed to configure Ethernet using DHCP");
36+
// no point in carrying on, so do nothing forevermore:
37+
while(true);
38+
}
39+
// print your local IP address:
40+
Serial.print("My IP address: ");
41+
IPAddress myIPAddress = Ethernet.localIP();
42+
for (byte thisByte = 0; thisByte < 4; thisByte++) {
43+
// print the value of each byte of the IP address:
44+
Serial.print(myIPAddress[thisByte], DEC);
45+
Serial.print(".");
46+
}
47+
Serial.println();
48+
}
49+
50+
void loop() {
51+
52+
}
53+
54+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
DHCP Chat Server
3+
4+
A simple server that distributes any incoming messages to all
5+
connected clients. To use telnet to your device's IP address and type.
6+
You can see the client's input in the serial monitor as well.
7+
Using an Arduino Wiznet Ethernet shield.
8+
9+
THis version attempts to get an IP address using DHCP
10+
11+
Circuit:
12+
* Ethernet shield attached to pins 10, 11, 12, 13
13+
14+
created 21 May 2011
15+
by Tom Igoe
16+
Based on ChatServer example by David A. Mellis
17+
18+
*/
19+
20+
#include <SPI.h>
21+
#include <Ethernet.h>
22+
23+
// Enter a MAC address and IP address for your controller below.
24+
// The IP address will be dependent on your local network.
25+
// gateway and subnet are optional:
26+
byte mac[] = {
27+
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
28+
IPAddress ip(192,168,1, 177);
29+
IPAddress gateway(192,168,1, 1);
30+
IPAddress subnet(255, 255, 0, 0);
31+
32+
// telnet defaults to port 23
33+
Server server(23);
34+
boolean gotAMessage = false; // whether or not you got a message from the client yet
35+
36+
void setup() {
37+
// open the serial port
38+
Serial.begin(9600);
39+
// start the Ethernet connection:
40+
Serial.println("Trying to get an IP address using DHCP");
41+
if (Ethernet.begin(mac) == 0) {
42+
Serial.println("Failed to configure Ethernet using DHCP");
43+
// initialize the ethernet device not using DHCP:
44+
Ethernet.begin(mac, ip, gateway, subnet);
45+
}
46+
// print your local IP address:
47+
Serial.print("My IP address: ");
48+
ip = Ethernet.localIP();
49+
for (byte thisByte = 0; thisByte < 4; thisByte++) {
50+
// print the value of each byte of the IP address:
51+
Serial.print(ip[thisByte], DEC);
52+
Serial.print(".");
53+
}
54+
Serial.println();
55+
// start listening for clients
56+
server.begin();
57+
58+
}
59+
60+
void loop() {
61+
// wait for a new client:
62+
Client client = server.available();
63+
64+
// when the client sends the first byte, say hello:
65+
if (client) {
66+
if (!gotAMessage) {
67+
Serial.println("We have a new client");
68+
client.println("Hello, client!");
69+
gotAMessage = true;
70+
}
71+
72+
// read the bytes incoming from the client:
73+
char thisChar = client.read();
74+
// echo the bytes back to the client:
75+
server.write(thisChar);
76+
// echo the bytes to the server as well:
77+
Serial.print(thisChar);
78+
}
79+
}
80+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Twitter Client with Strings
3+
4+
This sketch connects to Twitter using an Ethernet shield. It parses the XML
5+
returned, and looks for <text>this is a tweet</text>
6+
7+
You can use the Arduino Ethernet shield, or the Adafruit Ethernet shield,
8+
either one will work, as long as it's got a Wiznet Ethernet module on board.
9+
10+
This example uses the DHCP routines in the Ethernet library which is part of the
11+
Arduino core from version 1.0 beta 1
12+
13+
This example uses the String library, which is part of the Arduino core from
14+
version 0019.
15+
16+
Circuit:
17+
* Ethernet shield attached to pins 10, 11, 12, 13
18+
19+
created 21 May 2011
20+
by Tom Igoe
21+
22+
This code is in the public domain.
23+
24+
*/
25+
#include <SPI.h>
26+
#include <Ethernet.h>
27+
28+
29+
// Enter a MAC address and IP address for your controller below.
30+
// The IP address will be dependent on your local network:
31+
byte mac[] = {
32+
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
33+
IPAddress ip(192,168,1,20);
34+
35+
// initialize the library instance:
36+
Client client;
37+
38+
const int requestInterval = 60000; // delay between requests
39+
40+
char serverName[] = "api.twitter.com"; // twitter URL
41+
42+
boolean requested; // whether you've made a request since connecting
43+
long lastAttemptTime = 0; // last time you connected to the server, in milliseconds
44+
45+
String currentLine = ""; // string to hold the text from server
46+
String tweet = ""; // string to hold the tweet
47+
boolean readingTweet = false; // if you're currently reading the tweet
48+
49+
void setup() {
50+
// reserve space for the strings:
51+
currentLine.reserve(256);
52+
tweet.reserve(150);
53+
54+
// initialize serial:
55+
Serial.begin(9600);
56+
// attempt a DHCP connection:
57+
if (!Ethernet.begin(mac)) {
58+
// if DHCP fails, start with a hard-coded address:
59+
Ethernet.begin(mac, ip);
60+
}
61+
// connect to Twitter:
62+
connectToServer();
63+
}
64+
65+
66+
67+
void loop()
68+
{
69+
if (client.connected()) {
70+
if (client.available()) {
71+
// read incoming bytes:
72+
char inChar = client.read();
73+
74+
// add incoming byte to end of line:
75+
currentLine += inChar;
76+
77+
// if you get a newline, clear the line:
78+
if (inChar == '\n') {
79+
currentLine = "";
80+
}
81+
// if the current line ends with <text>, it will
82+
// be followed by the tweet:
83+
if ( currentLine.endsWith("<text>")) {
84+
// tweet is beginning. Clear the tweet string:
85+
readingTweet = true;
86+
tweet = "";
87+
}
88+
// if you're currently reading the bytes of a tweet,
89+
// add them to the tweet String:
90+
if (readingTweet) {
91+
if (inChar != '<') {
92+
tweet += inChar;
93+
}
94+
else {
95+
// if you got a "<" character,
96+
// you've reached the end of the tweet:
97+
readingTweet = false;
98+
Serial.println(tweet);
99+
// close the connection to the server:
100+
client.stop();
101+
}
102+
}
103+
}
104+
}
105+
else if (millis() - lastAttemptTime > requestInterval) {
106+
// if you're not connected, and two minutes have passed since
107+
// your last connection, then attempt to connect again:
108+
connectToServer();
109+
}
110+
}
111+
112+
void connectToServer() {
113+
// attempt to connect, and wait a millisecond:
114+
Serial.println("connecting to server...");
115+
if (client.connect(serverName, 80)) {
116+
Serial.println("making HTTP request...");
117+
// make HTTP GET request to twitter:
118+
client.println("GET /1/statuses/user_timeline.xml?screen_name=arduino&count=1 HTTP/1.1");
119+
client.println("HOST: api.twitter.com");
120+
client.println();
121+
}
122+
// note the time of this connect attempt:
123+
lastAttemptTime = millis();
124+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Software serial multple serial test
3+
4+
Receives from the two software serial ports,
5+
sends to the hardware serial port.
6+
7+
In order to listen on a software port, you call port.listen().
8+
When using two software serial ports, you have to switch ports
9+
by listen()ing on each one in turn. Pick a logical time to switch
10+
ports, like the end of an expected transmission, or when the
11+
buffer is empty. This example switches ports when there is nothing
12+
more to read from a port
13+
14+
The circuit:
15+
Two devices which communicate serially are needed.
16+
* First serial device's TX attached to digital pin 2, RX to pin 3
17+
* Second serial device's TX attached to digital pin 4, RX to pin 5
18+
19+
created 18 Apr. 2011
20+
by Tom Igoe
21+
based on Mikal Hart's twoPortRXExample
22+
23+
This example code is in the public domain.
24+
25+
*/
26+
27+
#include <SoftwareSerial.h>
28+
// software serial #1: TX = digital pin 2, RX = digital pin 3
29+
SoftwareSerial portOne(2, 3);
30+
31+
// software serial #2: TX = digital pin 4, RX = digital pin 5
32+
SoftwareSerial portTwo(4, 5);
33+
34+
void setup()
35+
{
36+
// Start the hardware serial port
37+
Serial.begin(9600);
38+
39+
// Start each software serial port
40+
portOne.begin(9600);
41+
portTwo.begin(9600);
42+
}
43+
44+
void loop()
45+
{
46+
// By default, the last intialized port is listening.
47+
// when you want to listen on a port, explicitly select it:
48+
portOne.listen();
49+
Serial.println("Data from port one:");
50+
// while there is data coming in, read it
51+
// and send to the hardware serial port:
52+
while (portOne.available() > 0) {
53+
char inByte = portOne.read();
54+
Serial.write(inByte);
55+
}
56+
57+
// blank line to separate data from the two ports:
58+
Serial.println();
59+
60+
// Now listen on the second port
61+
portTwo.listen();
62+
// while there is data coming in, read it
63+
// and send to the hardware serial port:
64+
Serial.println("Data from port two:");
65+
while (portTwo.available() > 0) {
66+
char inByte = portTwo.read();
67+
Serial.write(inByte);
68+
}
69+
70+
// blank line to separate data from the two ports:
71+
Serial.println();
72+
}
73+
74+
75+
76+
77+
78+

0 commit comments

Comments
 (0)