mirror of
https://github.com/arduino/Arduino.git
synced 2025-03-03 00:29:21 +01:00
Merge branch 'master' of github.com:arduino/wifishield
This commit is contained in:
commit
5082b35a17
99
WiFi/examples/WifiChatServer/WifiChatServer.ino
Normal file
99
WiFi/examples/WifiChatServer/WifiChatServer.ino
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
/*
|
||||||
|
Wifi 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.
|
||||||
|
|
||||||
|
This example obtains an IP address from the Wifi router and
|
||||||
|
reports it.
|
||||||
|
|
||||||
|
Circuit:
|
||||||
|
* WiFi shield attached
|
||||||
|
|
||||||
|
created 21 May 2011
|
||||||
|
by Tom Igoe
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <SPI.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <IPAddress.h>
|
||||||
|
|
||||||
|
char ssid[] = "yourWifiNetwork";
|
||||||
|
char pass[] = "seekrit-password";
|
||||||
|
int status = WL_IDLE_STATUS;
|
||||||
|
// telnet defaults to port 23
|
||||||
|
Server server(23);
|
||||||
|
boolean gotAMessage = false; // whether or not you got a message from the client yet
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
Serial.println("Attempting to connect to WPA network...");
|
||||||
|
Serial.print("SSID: ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
status = WiFi.begin(ssid, pass);
|
||||||
|
if ( status == WL_CONNECTED) {
|
||||||
|
// start listening for clients
|
||||||
|
server.begin();
|
||||||
|
Serial.println("Wifi shield is idle");
|
||||||
|
// report the IP address:
|
||||||
|
IPAddress myIPAddress = WiFi.localIp();
|
||||||
|
Serial.print("My IP Address: ");
|
||||||
|
Serial.print(WiFi.localIp()[3]);
|
||||||
|
Serial.print(".");
|
||||||
|
Serial.print(WiFi.localIp()[2]);
|
||||||
|
Serial.print(".");
|
||||||
|
Serial.print(WiFi.localIp()[1]);
|
||||||
|
Serial.print(".");
|
||||||
|
Serial.println(WiFi.localIp()[0]);
|
||||||
|
} else {
|
||||||
|
// if you didn't get a wifi connection, report what happened:
|
||||||
|
switch (status) {
|
||||||
|
case WL_IDLE_STATUS:
|
||||||
|
Serial.println("Wifi connection succeeded");
|
||||||
|
break;
|
||||||
|
case WL_NO_SSID_AVAIL:
|
||||||
|
Serial.println("No Wifi network available");
|
||||||
|
break;
|
||||||
|
case WL_CONNECT_FAILED:
|
||||||
|
Serial.println("Wifi connection failed");
|
||||||
|
break;
|
||||||
|
case WL_CONNECTION_LOST:
|
||||||
|
Serial.println("Wifi connection lost");
|
||||||
|
break;
|
||||||
|
case WL_DISCONNECTED:
|
||||||
|
Serial.println("Wifi disconnected");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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("I 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
129
WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino
Normal file
129
WiFi/examples/WifiTwitterClient/WifiTwitterClient.ino
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
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 <WiFi.h>
|
||||||
|
#include <IPAddress.h>
|
||||||
|
|
||||||
|
char ssid[] = "yourWifiNetwork";
|
||||||
|
char pass[] = "seekrit-password";
|
||||||
|
int status = WL_IDLE_STATUS;
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
Serial.println("Attempting to connect to WPA network...");
|
||||||
|
Serial.print("SSID: ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
status = WiFi.begin(ssid, pass);
|
||||||
|
if ( status != WL_CONNECTED) {
|
||||||
|
Serial.println("Couldn't get a wifi connection");
|
||||||
|
while(true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Connected to wifi");
|
||||||
|
connectToServer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
if (client.connected()) {
|
||||||
|
if (client.available()) {
|
||||||
|
// read incoming bytes:
|
||||||
|
char inChar = client.read();
|
||||||
|
Serial.write(inChar);
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
|
78
WiFi/examples/WifiWebClient/WifiWebClient.ino
Normal file
78
WiFi/examples/WifiWebClient/WifiWebClient.ino
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#include <SPI.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
Web client
|
||||||
|
|
||||||
|
This sketch connects to a website (http://www.google.com)
|
||||||
|
using a WiFi shield.
|
||||||
|
|
||||||
|
Circuit:
|
||||||
|
* WiFi shield attached
|
||||||
|
* Analog inputs attached to pins A0 through A5 (optional)
|
||||||
|
|
||||||
|
created 13 July 2010
|
||||||
|
by Domenico La Fauci
|
||||||
|
modified 21 May 2011
|
||||||
|
by Tom Igoe
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include <SPI.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <IPAddress.h>
|
||||||
|
|
||||||
|
char ssid[] = "yourWifiNetwork";
|
||||||
|
char pass[] = "seekrit-password";
|
||||||
|
int status = WL_IDLE_STATUS;
|
||||||
|
IPAddress server(74,125,115,105); // Google
|
||||||
|
|
||||||
|
// 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() {
|
||||||
|
Serial.begin(9600);
|
||||||
|
Serial.println("Attempting to connect to WPA network...");
|
||||||
|
Serial.print("SSID: ");
|
||||||
|
Serial.println(ssid);
|
||||||
|
|
||||||
|
status = WiFi.begin(ssid, pass);
|
||||||
|
if ( status != WL_CONNECTED) {
|
||||||
|
Serial.println("Couldn't get a wifi connection");
|
||||||
|
while(true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Serial.println("Connected to wifi");
|
||||||
|
Serial.println("\nStarting connection...");
|
||||||
|
// if you get a connection, report back via serial:
|
||||||
|
if (client.connect(server, 80)) {
|
||||||
|
Serial.println("connected");
|
||||||
|
// Make a HTTP request:
|
||||||
|
client.println("GET /search?q=arduino HTTP/1.0");
|
||||||
|
client.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// if there are incoming bytes available
|
||||||
|
// from the server, read them and print them:
|
||||||
|
if (client.available()) {
|
||||||
|
char c = client.read();
|
||||||
|
Serial.print(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the server's disconnected, stop the client:
|
||||||
|
if (!client.connected()) {
|
||||||
|
Serial.println();
|
||||||
|
Serial.println("disconnecting.");
|
||||||
|
client.stop();
|
||||||
|
|
||||||
|
// do nothing forevermore:
|
||||||
|
for(;;)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user