1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-26 20:54:22 +01:00

WiFi Shield examples: added the firmware version check

This commit is contained in:
Fede85 2014-02-06 18:40:48 +01:00
parent b415903b69
commit 5dfafe7847
13 changed files with 406 additions and 362 deletions

View File

@ -1,38 +1,43 @@
/*
This example connects to an unencrypted Wifi network.
This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details.
Circuit:
* WiFi shield attached
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <WiFi.h>
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // the name of your network
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to open SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid);
@ -40,7 +45,7 @@ void setup() {
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
@ -56,26 +61,26 @@ void loop() {
void printWifiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[5],HEX);
Serial.print(mac[5], HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
Serial.println(mac[0], HEX);
// print your subnet mask:
IPAddress subnet = WiFi.subnetMask();
Serial.print("NetMask: ");
@ -94,19 +99,19 @@ void printCurrentNet() {
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5],HEX);
Serial.print(bssid[5], HEX);
Serial.print(":");
Serial.print(bssid[4],HEX);
Serial.print(bssid[4], HEX);
Serial.print(":");
Serial.print(bssid[3],HEX);
Serial.print(bssid[3], HEX);
Serial.print(":");
Serial.print(bssid[2],HEX);
Serial.print(bssid[2], HEX);
Serial.print(":");
Serial.print(bssid[1],HEX);
Serial.print(bssid[1], HEX);
Serial.print(":");
Serial.println(bssid[0],HEX);
Serial.println(bssid[0], HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
@ -116,6 +121,6 @@ void printCurrentNet() {
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption,HEX);
Serial.println(encryption, HEX);
}

View File

@ -1,50 +1,55 @@
/*
This example connects to a WEP-encrypted Wifi network.
This example connects to a WEP-encrypted Wifi network.
Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details.
If you use 40-bit WEP, you need a key that is 10 characters long,
and the characters must be hexadecimal (0-9 or A-F).
e.g. for 40-bit, ABBADEAF01 will work, but ABBADEAF won't work
(too short) and ABBAISDEAF won't work (I and S are not
hexadecimal characters).
For 128-bit, you need a string that is 26 characters long.
D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters,
If you use 40-bit WEP, you need a key that is 10 characters long,
and the characters must be hexadecimal (0-9 or A-F).
e.g. for 40-bit, ABBADEAF01 will work, but ABBADEAF won't work
(too short) and ABBAISDEAF won't work (I and S are not
hexadecimal characters).
For 128-bit, you need a string that is 26 characters long.
D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters,
all in the 0-9, A-F range.
Circuit:
* WiFi shield attached
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char key[] = "D0D0DEADF00DABBADEAFBEADED"; // your network key
int keyIndex = 0; // your network key Index number
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WEP network, SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, keyIndex, key);
@ -73,20 +78,20 @@ void printWifiData() {
Serial.println(ip);
// print your MAC address:
byte mac[6];
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[5],HEX);
Serial.print(mac[5], HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
Serial.println(mac[0], HEX);
}
void printCurrentNet() {
@ -96,19 +101,19 @@ void printCurrentNet() {
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5],HEX);
Serial.print(bssid[5], HEX);
Serial.print(":");
Serial.print(bssid[4],HEX);
Serial.print(bssid[4], HEX);
Serial.print(":");
Serial.print(bssid[3],HEX);
Serial.print(bssid[3], HEX);
Serial.print(":");
Serial.print(bssid[2],HEX);
Serial.print(bssid[2], HEX);
Serial.print(":");
Serial.print(bssid[1],HEX);
Serial.print(bssid[1], HEX);
Serial.print(":");
Serial.println(bssid[0],HEX);
Serial.println(bssid[0], HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
@ -118,7 +123,7 @@ void printCurrentNet() {
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption,HEX);
Serial.println(encryption, HEX);
Serial.println();
}

View File

@ -1,48 +1,53 @@
/*
This example connects to an unencrypted Wifi network.
This example connects to an unencrypted Wifi network.
Then it prints the MAC address of the Wifi shield,
the IP address obtained, and other network details.
Circuit:
* WiFi shield attached
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <WiFi.h>
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network:
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
@ -59,26 +64,26 @@ void loop() {
void printWifiData() {
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.print("IP Address: ");
Serial.println(ip);
Serial.println(ip);
// print your MAC address:
byte mac[6];
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print(mac[5],HEX);
Serial.print(mac[5], HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
Serial.println(mac[0], HEX);
}
void printCurrentNet() {
@ -88,19 +93,19 @@ void printCurrentNet() {
// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print(bssid[5],HEX);
Serial.print(bssid[5], HEX);
Serial.print(":");
Serial.print(bssid[4],HEX);
Serial.print(bssid[4], HEX);
Serial.print(":");
Serial.print(bssid[3],HEX);
Serial.print(bssid[3], HEX);
Serial.print(":");
Serial.print(bssid[2],HEX);
Serial.print(bssid[2], HEX);
Serial.print(":");
Serial.print(bssid[1],HEX);
Serial.print(bssid[1], HEX);
Serial.print(":");
Serial.println(bssid[0],HEX);
Serial.println(bssid[0], HEX);
// print the received signal strength:
long rssi = WiFi.RSSI();
@ -110,7 +115,7 @@ void printCurrentNet() {
// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.println(encryption,HEX);
Serial.println(encryption, HEX);
Serial.println();
}

View File

@ -1,13 +1,13 @@
/*
This example prints the Wifi shield's MAC address, and
scans for available Wifi networks using the Wifi shield.
Every ten seconds, it scans again. It doesn't actually
Every ten seconds, it scans again. It doesn't actually
connect to any network, so no encryption scheme is specified.
Circuit:
* WiFi shield attached
created 13 July 2010
by dlf (Metodo2 srl)
modified 21 Junn 2012
@ -20,17 +20,21 @@
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// Print WiFi MAC address:
printMacAddress();
@ -49,22 +53,22 @@ void loop() {
void printMacAddress() {
// the MAC address of your Wifi shield
byte mac[6];
byte mac[6];
// print your MAC address:
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[5],HEX);
Serial.print(mac[5], HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(mac[4], HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(mac[3], HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(mac[2], HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(mac[1], HEX);
Serial.print(":");
Serial.println(mac[0],HEX);
Serial.println(mac[0], HEX);
}
void listNetworks() {
@ -72,17 +76,17 @@ void listNetworks() {
Serial.println("** Scan Networks **");
int numSsid = WiFi.scanNetworks();
if (numSsid == -1)
{
{
Serial.println("Couldn't get a wifi connection");
while(true);
}
while (true);
}
// print the list of networks seen:
Serial.print("number of available networks:");
Serial.println(numSsid);
// print the network number and name for each network found:
for (int thisNet = 0; thisNet<numSsid; thisNet++) {
for (int thisNet = 0; thisNet < numSsid; thisNet++) {
Serial.print(thisNet);
Serial.print(") ");
Serial.print(WiFi.SSID(thisNet));
@ -97,22 +101,22 @@ void listNetworks() {
void printEncryptionType(int thisType) {
// read the encryption type and print out the name:
switch (thisType) {
case ENC_TYPE_WEP:
Serial.println("WEP");
break;
case ENC_TYPE_TKIP:
Serial.println("WPA");
break;
case ENC_TYPE_CCMP:
Serial.println("WPA2");
break;
case ENC_TYPE_NONE:
Serial.println("None");
break;
case ENC_TYPE_AUTO:
Serial.println("Auto");
break;
}
case ENC_TYPE_WEP:
Serial.println("WEP");
break;
case ENC_TYPE_TKIP:
Serial.println("WPA");
break;
case ENC_TYPE_CCMP:
Serial.println("WPA2");
break;
case ENC_TYPE_NONE:
Serial.println("None");
break;
case ENC_TYPE_AUTO:
Serial.println("Auto");
break;
}
}

View File

@ -1,29 +1,29 @@
/*
WiFi Web Server LED Blink
A simple web server that lets you blink an LED via the web.
This sketch will print the IP address of your WiFi Shield (once connected)
to the Serial monitor. From there, you can open that address in a web browser
to turn on and off the LED on pin 9.
If the IP address of your shield is yourAddress:
http://yourAddress/H turns the LED on
http://yourAddress/L turns it off
This example is written for a network using WPA encryption. For
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
* LED attached to pin 9
created 25 Nov 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
@ -36,20 +36,24 @@ void setup() {
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while(true); // don't continue
}
Serial.println("WiFi shield not present");
while (true); // don't continue
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
}
server.begin(); // start the web server on port 80
printWifiStatus(); // you're connected now, so print out the status
}
@ -69,9 +73,9 @@ void loop() {
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
@ -83,12 +87,12 @@ void loop() {
// The HTTP response ends with another blank line:
client.println();
// break out of the while loop:
break;
}
break;
}
else { // if you got a newline, then clear currentLine:
currentLine = "";
}
}
}
else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}

View File

@ -1,28 +1,28 @@
/*
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 is written for a network using WPA encryption. For
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
created 18 Dec 2009
by David A. Mellis
modified 31 May 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
@ -35,33 +35,38 @@ boolean alreadyConnected = false; // whether or not the client was connected pre
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
}
// start the server:
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
}
}
void loop() {
@ -73,11 +78,11 @@ void loop() {
if (client) {
if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
client.println("Hello, client!");
alreadyConnected = true;
}
}
if (client.available() > 0) {
// read the bytes incoming from the client:

View File

@ -1,22 +1,22 @@
/*
Udp NTP Client
Get the time from a Network Time Protocol (NTP) time server
Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them,
Demonstrates use of UDP sendPacket and ReceivePacket
For more on NTP time servers and the messages needed to communicate with them,
see http://en.wikipedia.org/wiki/Network_Time_Protocol
created 4 Sep 2010
created 4 Sep 2010
by Michael Margolis
modified 9 Apr 2012
by Tom Igoe
This code is in the public domain.
*/
#include <SPI.h>
#include <SPI.h>
#include <WiFi.h>
#include <WiFiUdp.h>
@ -31,12 +31,12 @@ IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
void setup()
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
@ -46,17 +46,20 @@ void setup()
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
@ -73,61 +76,61 @@ void setup()
void loop()
{
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
// wait to see if a reply is available
delay(1000);
Serial.println( Udp.parsePacket() );
if ( Udp.parsePacket() ) {
Serial.println("packet received");
if ( Udp.parsePacket() ) {
Serial.println("packet received");
// We've received a packet, read the data from it
Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900);
Serial.println(secsSince1900);
// now convert NTP time into everyday time:
Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);
Serial.println(epoch);
// print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
Serial.print(':');
if ( ((epoch % 3600) / 60) < 10 ) {
// In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
Serial.print(':');
if ( (epoch % 60) < 10 ) {
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
Serial.println(epoch %60); // print the second
Serial.println(epoch % 60); // print the second
}
// wait ten seconds before asking for the time again
delay(10000);
delay(10000);
}
// send an NTP request to the time server at the given address
// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
//Serial.println("1");
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
//Serial.println("2");
@ -136,20 +139,20 @@ unsigned long sendNTPpacket(IPAddress& address)
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;
//Serial.println("3");
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
//Serial.println("4");
Udp.write(packetBuffer,NTP_PACKET_SIZE);
Udp.write(packetBuffer, NTP_PACKET_SIZE);
//Serial.println("5");
Udp.endPacket();
Udp.endPacket();
//Serial.println("6");
}

View File

@ -1,13 +1,13 @@
/*
WiFi UDP Send and Receive String
This sketch wait an UDP packet on localPort using a WiFi shield.
When a packet is received an Acknowledge packet is sent to the client on port remotePort
Circuit:
* WiFi shield attached
created 30 December 2012
by dlf (Metodo2 srl)
@ -19,7 +19,7 @@
#include <WiFiUdp.h>
int status = WL_IDLE_STATUS;
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
@ -32,42 +32,46 @@ WiFiUDP Udp;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid);
// wait 10 seconds for connection:
delay(10000);
}
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
Udp.begin(localPort);
Udp.begin(localPort);
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
if (packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
@ -77,16 +81,16 @@ void loop() {
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
int len = Udp.read(packetBuffer,255);
if (len >0) packetBuffer[len]=0;
int len = Udp.read(packetBuffer, 255);
if (len > 0) packetBuffer[len] = 0;
Serial.println("Contents:");
Serial.println(packetBuffer);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}
}

View File

@ -1,19 +1,19 @@
/*
Web client
This sketch connects to a website (http://www.google.com)
using a WiFi shield.
This example is written for a network using WPA encryption. For
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
This example is written for a network using WPA encryption. For
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
@ -24,7 +24,7 @@
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
@ -35,37 +35,41 @@ int status = WL_IDLE_STATUS;
char server[] = "www.google.com"; // name address for Google (using DNS)
// Initialize the Ethernet client library
// with the IP address and port of the server
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
@ -79,7 +83,7 @@ void setup() {
}
void loop() {
// if there are incoming bytes available
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
@ -93,7 +97,7 @@ void loop() {
client.stop();
// do nothing forevermore:
while(true);
while (true);
}
}

View File

@ -1,24 +1,26 @@
/*
Repeating Wifi Web client
Repeating Wifi Web Client
This sketch connects to a a web server and makes a request
using an Arduino Wifi shield.
Circuit:
* Wifi shield attached to pins 10, 11, 12, 13
* WiFi shield attached to pins SPI pins and pin 7
created 23 April 2012
modifide 31 May 2012
modified 31 May 2012
by Tom Igoe
modified 13 Jan 2014
by Federico Vanzati
http://arduino.cc/en/Tutorial/WifiWebClientRepeating
This code is in the public domain.
*/
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
@ -31,34 +33,37 @@ WiFiClient client;
char server[] = "www.arduino.cc";
//IPAddress server(64,131,82,241);
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, in milliseconds
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
}
// you're connected now, so print out the status:
printWifiStatus();
}
@ -72,44 +77,36 @@ void loop() {
Serial.write(c);
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
// store the state of the connection for next time through
// the loop:
lastConnected = client.connected();
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP PUT request:
client.println("GET /latest.txt HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: arduino-ethernet");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
}
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
Serial.println("disconnecting.");
client.stop();
}
}
@ -132,7 +129,3 @@ void printWifiStatus() {
}

View File

@ -1,16 +1,16 @@
/*
WiFi Web Server
A simple web server that shows the value of the analog input pins.
using a WiFi shield.
This example is written for a network using WPA encryption. For
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* WiFi shield attached
* Analog inputs attached to pins A0 through A5 (optional)
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
@ -22,7 +22,7 @@
#include <WiFi.h>
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
@ -32,28 +32,32 @@ WiFiServer server(80);
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
}
server.begin();
// you're connected now, so print out the status:
printWifiStatus();
@ -90,15 +94,15 @@ void loop() {
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("<br />");
client.println("<br />");
}
client.println("</html>");
break;
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
@ -107,7 +111,7 @@ void loop() {
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disonnected");

View File

@ -3,26 +3,26 @@
This sketch connects an analog sensor to Xively (http://www.xively.com)
using an Arduino Wifi shield.
This example is written for a network using WPA encryption. For
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
This example has been updated to use version 2.0 of the Xively 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.
Circuit:
* Analog sensor attached to analog in 0
* Wifi shield attached to pins 10, 11, 12, 13
created 13 Mar 2012
modified 31 May 2012
by Tom Igoe
modified 8 Nov 2013
by Scott Fitzgerald
This code is in the public domain.
*/
#include <SPI.h>
#include <WiFi.h>
@ -31,7 +31,7 @@
#define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Arduino Project" // user agent is the project name
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS;
@ -49,28 +49,32 @@ const unsigned long postingInterval = 10*1000; //delay between updates to xively
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
}
// you're connected now, so print out the status:
printWifiStatus();
}
@ -78,7 +82,7 @@ void setup() {
void loop() {
// read the analog sensor:
int sensorReading = analogRead(A0);
int sensorReading = analogRead(A0);
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
@ -98,7 +102,7 @@ void loop() {
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(sensorReading);
}
// store the state of the connection for next time through
@ -135,8 +139,8 @@ void sendData(int thisData) {
// here's the actual content of the PUT request:
client.print("sensor1,");
client.println(thisData);
}
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");
@ -144,7 +148,7 @@ void sendData(int thisData) {
Serial.println("disconnecting.");
client.stop();
}
// note the time that the connection was made or attempted:
// note the time that the connection was made or attempted:
lastConnectionTime = millis();
}
@ -157,12 +161,12 @@ void sendData(int thisData) {
int getLength(int someValue) {
// there's at least one byte:
int digits = 1;
// continually divide the value by ten,
// continually divide the value by ten,
// adding one to the digit count for each
// time you divide, until you're at 0:
int dividend = someValue /10;
int dividend = someValue / 10;
while (dividend > 0) {
dividend = dividend /10;
dividend = dividend / 10;
digits++;
}
// return the number of digits:

View File

@ -3,29 +3,29 @@
This sketch connects an analog sensor to Xively (http://www.xively.com)
using a Arduino Wifi shield.
This example is written for a network using WPA encryption. For
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
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.
This example uses the String library, which is part of the Arduino core from
version 0019.
version 0019.
Circuit:
* Analog sensor attached to analog in 0
* Wifi shield attached to pins 10, 11, 12, 13
created 16 Mar 2012
modified 31 May 2012
by Tom Igoe
modified 8 Sept 2012
by Scott Fitzgerald
This code is in the public domain.
*/
#include <SPI.h>
@ -35,7 +35,7 @@
#define FEEDID 00000 // replace your feed ID
#define USERAGENT "My Arduino Project" // user agent is the project name
char ssid[] = "yourNetwork"; // your network SSID (name)
char ssid[] = "yourNetwork"; // your network SSID (name)
char pass[] = "secretPassword"; // your network password
int status = WL_IDLE_STATUS;
@ -54,35 +54,39 @@ const unsigned long postingInterval = 10*1000; //delay between updates to xivel
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// check for the presence of the shield:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
Serial.println("WiFi shield not present");
// don't continue:
while(true);
}
while (true);
}
String fv = WiFi.firmwareVersion();
if ( fv != "1.1.0" )
Serial.println("Please upgrade the firmware");
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
}
// you're connected now, so print out the status:
printWifiStatus();
}
void loop() {
// read the analog sensor:
int sensorReading = analogRead(A0);
int sensorReading = analogRead(A0);
// convert the data to a String to send it:
String dataString = "sensor1,";
@ -111,8 +115,8 @@ void loop() {
}
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
// your last connection, then connect again and send data:
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
sendData(dataString);
}
// store the state of the connection for next time through
@ -144,7 +148,7 @@ void sendData(String thisData) {
// here's the actual content of the PUT request:
client.println(thisData);
}
}
else {
// if you couldn't make a connection:
Serial.println("connection failed");