mirror of
https://github.com/esp8266/Arduino.git
synced 2025-08-05 13:16:13 +03:00
WiFi Shield examples: added the firmware version check
This commit is contained in:
@@ -12,7 +12,8 @@
|
|||||||
modified 31 May 2012
|
modified 31 May 2012
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
*/
|
*/
|
||||||
#include <WiFi.h>
|
#include <SPI.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
char ssid[] = "yourNetwork"; // the name of your network
|
char ssid[] = "yourNetwork"; // the name of your network
|
||||||
int status = WL_IDLE_STATUS; // the Wifi radio's status
|
int status = WL_IDLE_STATUS; // the Wifi radio's status
|
||||||
@@ -28,10 +29,14 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// don't continue:
|
||||||
while(true);
|
while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// attempt to connect to Wifi network:
|
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 open SSID: ");
|
Serial.print("Attempting to connect to open SSID: ");
|
||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
@@ -56,7 +61,7 @@ void loop() {
|
|||||||
void printWifiData() {
|
void printWifiData() {
|
||||||
// print your WiFi shield's IP address:
|
// print your WiFi shield's IP address:
|
||||||
IPAddress ip = WiFi.localIP();
|
IPAddress ip = WiFi.localIP();
|
||||||
Serial.print("IP Address: ");
|
Serial.print("IP Address: ");
|
||||||
Serial.println(ip);
|
Serial.println(ip);
|
||||||
Serial.println(ip);
|
Serial.println(ip);
|
||||||
|
|
||||||
@@ -64,17 +69,17 @@ void printWifiData() {
|
|||||||
byte mac[6];
|
byte mac[6];
|
||||||
WiFi.macAddress(mac);
|
WiFi.macAddress(mac);
|
||||||
Serial.print("MAC address: ");
|
Serial.print("MAC address: ");
|
||||||
Serial.print(mac[5],HEX);
|
Serial.print(mac[5], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[4],HEX);
|
Serial.print(mac[4], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[3],HEX);
|
Serial.print(mac[3], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[2],HEX);
|
Serial.print(mac[2], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[1],HEX);
|
Serial.print(mac[1], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.println(mac[0],HEX);
|
Serial.println(mac[0], HEX);
|
||||||
|
|
||||||
// print your subnet mask:
|
// print your subnet mask:
|
||||||
IPAddress subnet = WiFi.subnetMask();
|
IPAddress subnet = WiFi.subnetMask();
|
||||||
@@ -96,17 +101,17 @@ void printCurrentNet() {
|
|||||||
byte bssid[6];
|
byte bssid[6];
|
||||||
WiFi.BSSID(bssid);
|
WiFi.BSSID(bssid);
|
||||||
Serial.print("BSSID: ");
|
Serial.print("BSSID: ");
|
||||||
Serial.print(bssid[5],HEX);
|
Serial.print(bssid[5], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[4],HEX);
|
Serial.print(bssid[4], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[3],HEX);
|
Serial.print(bssid[3], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[2],HEX);
|
Serial.print(bssid[2], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[1],HEX);
|
Serial.print(bssid[1], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.println(bssid[0],HEX);
|
Serial.println(bssid[0], HEX);
|
||||||
|
|
||||||
// print the received signal strength:
|
// print the received signal strength:
|
||||||
long rssi = WiFi.RSSI();
|
long rssi = WiFi.RSSI();
|
||||||
@@ -116,6 +121,6 @@ void printCurrentNet() {
|
|||||||
// print the encryption type:
|
// print the encryption type:
|
||||||
byte encryption = WiFi.encryptionType();
|
byte encryption = WiFi.encryptionType();
|
||||||
Serial.print("Encryption Type:");
|
Serial.print("Encryption Type:");
|
||||||
Serial.println(encryption,HEX);
|
Serial.println(encryption, HEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -22,6 +22,7 @@
|
|||||||
modified 31 May 2012
|
modified 31 May 2012
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
*/
|
*/
|
||||||
|
#include <SPI.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
|
||||||
char ssid[] = "yourNetwork"; // your network SSID (name)
|
char ssid[] = "yourNetwork"; // your network SSID (name)
|
||||||
@@ -40,9 +41,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to WEP network, SSID: ");
|
Serial.print("Attempting to connect to WEP network, SSID: ");
|
||||||
@@ -76,17 +81,17 @@ void printWifiData() {
|
|||||||
byte mac[6];
|
byte mac[6];
|
||||||
WiFi.macAddress(mac);
|
WiFi.macAddress(mac);
|
||||||
Serial.print("MAC address: ");
|
Serial.print("MAC address: ");
|
||||||
Serial.print(mac[5],HEX);
|
Serial.print(mac[5], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[4],HEX);
|
Serial.print(mac[4], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[3],HEX);
|
Serial.print(mac[3], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[2],HEX);
|
Serial.print(mac[2], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[1],HEX);
|
Serial.print(mac[1], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.println(mac[0],HEX);
|
Serial.println(mac[0], HEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
void printCurrentNet() {
|
void printCurrentNet() {
|
||||||
@@ -98,17 +103,17 @@ void printCurrentNet() {
|
|||||||
byte bssid[6];
|
byte bssid[6];
|
||||||
WiFi.BSSID(bssid);
|
WiFi.BSSID(bssid);
|
||||||
Serial.print("BSSID: ");
|
Serial.print("BSSID: ");
|
||||||
Serial.print(bssid[5],HEX);
|
Serial.print(bssid[5], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[4],HEX);
|
Serial.print(bssid[4], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[3],HEX);
|
Serial.print(bssid[3], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[2],HEX);
|
Serial.print(bssid[2], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[1],HEX);
|
Serial.print(bssid[1], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.println(bssid[0],HEX);
|
Serial.println(bssid[0], HEX);
|
||||||
|
|
||||||
// print the received signal strength:
|
// print the received signal strength:
|
||||||
long rssi = WiFi.RSSI();
|
long rssi = WiFi.RSSI();
|
||||||
@@ -118,7 +123,7 @@ void printCurrentNet() {
|
|||||||
// print the encryption type:
|
// print the encryption type:
|
||||||
byte encryption = WiFi.encryptionType();
|
byte encryption = WiFi.encryptionType();
|
||||||
Serial.print("Encryption Type:");
|
Serial.print("Encryption Type:");
|
||||||
Serial.println(encryption,HEX);
|
Serial.println(encryption, HEX);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -12,7 +12,8 @@
|
|||||||
modified 31 May 2012
|
modified 31 May 2012
|
||||||
by Tom Igoe
|
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
|
char pass[] = "secretPassword"; // your network password
|
||||||
@@ -29,10 +30,14 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// don't continue:
|
||||||
while(true);
|
while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// attempt to connect to Wifi network:
|
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 WPA SSID: ");
|
Serial.print("Attempting to connect to WPA SSID: ");
|
||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
@@ -59,7 +64,7 @@ void loop() {
|
|||||||
void printWifiData() {
|
void printWifiData() {
|
||||||
// print your WiFi shield's IP address:
|
// print your WiFi shield's IP address:
|
||||||
IPAddress ip = WiFi.localIP();
|
IPAddress ip = WiFi.localIP();
|
||||||
Serial.print("IP Address: ");
|
Serial.print("IP Address: ");
|
||||||
Serial.println(ip);
|
Serial.println(ip);
|
||||||
Serial.println(ip);
|
Serial.println(ip);
|
||||||
|
|
||||||
@@ -67,17 +72,17 @@ void printWifiData() {
|
|||||||
byte mac[6];
|
byte mac[6];
|
||||||
WiFi.macAddress(mac);
|
WiFi.macAddress(mac);
|
||||||
Serial.print("MAC address: ");
|
Serial.print("MAC address: ");
|
||||||
Serial.print(mac[5],HEX);
|
Serial.print(mac[5], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[4],HEX);
|
Serial.print(mac[4], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[3],HEX);
|
Serial.print(mac[3], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[2],HEX);
|
Serial.print(mac[2], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[1],HEX);
|
Serial.print(mac[1], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.println(mac[0],HEX);
|
Serial.println(mac[0], HEX);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,17 +95,17 @@ void printCurrentNet() {
|
|||||||
byte bssid[6];
|
byte bssid[6];
|
||||||
WiFi.BSSID(bssid);
|
WiFi.BSSID(bssid);
|
||||||
Serial.print("BSSID: ");
|
Serial.print("BSSID: ");
|
||||||
Serial.print(bssid[5],HEX);
|
Serial.print(bssid[5], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[4],HEX);
|
Serial.print(bssid[4], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[3],HEX);
|
Serial.print(bssid[3], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[2],HEX);
|
Serial.print(bssid[2], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(bssid[1],HEX);
|
Serial.print(bssid[1], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.println(bssid[0],HEX);
|
Serial.println(bssid[0], HEX);
|
||||||
|
|
||||||
// print the received signal strength:
|
// print the received signal strength:
|
||||||
long rssi = WiFi.RSSI();
|
long rssi = WiFi.RSSI();
|
||||||
@@ -110,7 +115,7 @@ void printCurrentNet() {
|
|||||||
// print the encryption type:
|
// print the encryption type:
|
||||||
byte encryption = WiFi.encryptionType();
|
byte encryption = WiFi.encryptionType();
|
||||||
Serial.print("Encryption Type:");
|
Serial.print("Encryption Type:");
|
||||||
Serial.println(encryption,HEX);
|
Serial.println(encryption, HEX);
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -29,9 +29,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// Print WiFi MAC address:
|
||||||
printMacAddress();
|
printMacAddress();
|
||||||
|
|
||||||
@@ -54,17 +58,17 @@ void printMacAddress() {
|
|||||||
// print your MAC address:
|
// print your MAC address:
|
||||||
WiFi.macAddress(mac);
|
WiFi.macAddress(mac);
|
||||||
Serial.print("MAC: ");
|
Serial.print("MAC: ");
|
||||||
Serial.print(mac[5],HEX);
|
Serial.print(mac[5], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[4],HEX);
|
Serial.print(mac[4], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[3],HEX);
|
Serial.print(mac[3], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[2],HEX);
|
Serial.print(mac[2], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.print(mac[1],HEX);
|
Serial.print(mac[1], HEX);
|
||||||
Serial.print(":");
|
Serial.print(":");
|
||||||
Serial.println(mac[0],HEX);
|
Serial.println(mac[0], HEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
void listNetworks() {
|
void listNetworks() {
|
||||||
@@ -74,7 +78,7 @@ void listNetworks() {
|
|||||||
if (numSsid == -1)
|
if (numSsid == -1)
|
||||||
{
|
{
|
||||||
Serial.println("Couldn't get a wifi connection");
|
Serial.println("Couldn't get a wifi connection");
|
||||||
while(true);
|
while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// print the list of networks seen:
|
// print the list of networks seen:
|
||||||
@@ -82,7 +86,7 @@ void listNetworks() {
|
|||||||
Serial.println(numSsid);
|
Serial.println(numSsid);
|
||||||
|
|
||||||
// print the network number and name for each network found:
|
// 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(thisNet);
|
||||||
Serial.print(") ");
|
Serial.print(") ");
|
||||||
Serial.print(WiFi.SSID(thisNet));
|
Serial.print(WiFi.SSID(thisNet));
|
||||||
@@ -97,21 +101,21 @@ void listNetworks() {
|
|||||||
void printEncryptionType(int thisType) {
|
void printEncryptionType(int thisType) {
|
||||||
// read the encryption type and print out the name:
|
// read the encryption type and print out the name:
|
||||||
switch (thisType) {
|
switch (thisType) {
|
||||||
case ENC_TYPE_WEP:
|
case ENC_TYPE_WEP:
|
||||||
Serial.println("WEP");
|
Serial.println("WEP");
|
||||||
break;
|
break;
|
||||||
case ENC_TYPE_TKIP:
|
case ENC_TYPE_TKIP:
|
||||||
Serial.println("WPA");
|
Serial.println("WPA");
|
||||||
break;
|
break;
|
||||||
case ENC_TYPE_CCMP:
|
case ENC_TYPE_CCMP:
|
||||||
Serial.println("WPA2");
|
Serial.println("WPA2");
|
||||||
break;
|
break;
|
||||||
case ENC_TYPE_NONE:
|
case ENC_TYPE_NONE:
|
||||||
Serial.println("None");
|
Serial.println("None");
|
||||||
break;
|
break;
|
||||||
case ENC_TYPE_AUTO:
|
case ENC_TYPE_AUTO:
|
||||||
Serial.println("Auto");
|
Serial.println("Auto");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -37,9 +37,13 @@ void setup() {
|
|||||||
// check for the presence of the shield:
|
// check for the presence of the shield:
|
||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
while(true); // don't continue
|
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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to Network named: ");
|
Serial.print("Attempting to connect to Network named: ");
|
||||||
|
@@ -44,9 +44,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to SSID: ");
|
Serial.print("Attempting to connect to SSID: ");
|
||||||
@@ -57,11 +61,12 @@ void setup() {
|
|||||||
// wait 10 seconds for connection:
|
// wait 10 seconds for connection:
|
||||||
delay(10000);
|
delay(10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// start the server:
|
// start the server:
|
||||||
server.begin();
|
server.begin();
|
||||||
// you're connected now, so print out the status:
|
// you're connected now, so print out the status:
|
||||||
printWifiStatus();
|
printWifiStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@@ -48,9 +48,12 @@ void setup()
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
@@ -73,13 +76,13 @@ void setup()
|
|||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
sendNTPpacket(timeServer); // send an NTP packet to a time server
|
sendNTPpacket(timeServer); // send an NTP packet to a time server
|
||||||
// wait to see if a reply is available
|
// wait to see if a reply is available
|
||||||
delay(1000);
|
delay(1000);
|
||||||
Serial.println( Udp.parsePacket() );
|
Serial.println( Udp.parsePacket() );
|
||||||
if ( Udp.parsePacket() ) {
|
if ( Udp.parsePacket() ) {
|
||||||
Serial.println("packet received");
|
Serial.println("packet received");
|
||||||
// We've received a packet, read the data from it
|
// 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,
|
//the timestamp starts at byte 40 of the received packet and is four bytes,
|
||||||
// or two words, long. First, esxtract the two words:
|
// or two words, long. First, esxtract the two words:
|
||||||
@@ -116,7 +119,7 @@ void loop()
|
|||||||
// In the first 10 seconds of each minute, we'll want a leading '0'
|
// In the first 10 seconds of each minute, we'll want a leading '0'
|
||||||
Serial.print('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
|
// wait ten seconds before asking for the time again
|
||||||
delay(10000);
|
delay(10000);
|
||||||
@@ -147,7 +150,7 @@ unsigned long sendNTPpacket(IPAddress& address)
|
|||||||
// 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
|
Udp.beginPacket(address, 123); //NTP requests are to port 123
|
||||||
//Serial.println("4");
|
//Serial.println("4");
|
||||||
Udp.write(packetBuffer,NTP_PACKET_SIZE);
|
Udp.write(packetBuffer, NTP_PACKET_SIZE);
|
||||||
//Serial.println("5");
|
//Serial.println("5");
|
||||||
Udp.endPacket();
|
Udp.endPacket();
|
||||||
//Serial.println("6");
|
//Serial.println("6");
|
||||||
|
@@ -41,9 +41,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to SSID: ");
|
Serial.print("Attempting to connect to SSID: ");
|
||||||
@@ -66,7 +70,7 @@ void loop() {
|
|||||||
|
|
||||||
// if there's data available, read a packet
|
// if there's data available, read a packet
|
||||||
int packetSize = Udp.parsePacket();
|
int packetSize = Udp.parsePacket();
|
||||||
if(packetSize)
|
if (packetSize)
|
||||||
{
|
{
|
||||||
Serial.print("Received packet of size ");
|
Serial.print("Received packet of size ");
|
||||||
Serial.println(packetSize);
|
Serial.println(packetSize);
|
||||||
@@ -77,8 +81,8 @@ void loop() {
|
|||||||
Serial.println(Udp.remotePort());
|
Serial.println(Udp.remotePort());
|
||||||
|
|
||||||
// read the packet into packetBufffer
|
// read the packet into packetBufffer
|
||||||
int len = Udp.read(packetBuffer,255);
|
int len = Udp.read(packetBuffer, 255);
|
||||||
if (len >0) packetBuffer[len]=0;
|
if (len > 0) packetBuffer[len] = 0;
|
||||||
Serial.println("Contents:");
|
Serial.println("Contents:");
|
||||||
Serial.println(packetBuffer);
|
Serial.println(packetBuffer);
|
||||||
|
|
||||||
@@ -86,7 +90,7 @@ void loop() {
|
|||||||
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
|
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
|
||||||
Udp.write(ReplyBuffer);
|
Udp.write(ReplyBuffer);
|
||||||
Udp.endPacket();
|
Udp.endPacket();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -50,9 +50,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while (status != WL_CONNECTED) {
|
while (status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to SSID: ");
|
Serial.print("Attempting to connect to SSID: ");
|
||||||
@@ -93,7 +97,7 @@ void loop() {
|
|||||||
client.stop();
|
client.stop();
|
||||||
|
|
||||||
// do nothing forevermore:
|
// do nothing forevermore:
|
||||||
while(true);
|
while (true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,15 +1,17 @@
|
|||||||
/*
|
/*
|
||||||
Repeating Wifi Web client
|
Repeating Wifi Web Client
|
||||||
|
|
||||||
This sketch connects to a a web server and makes a request
|
This sketch connects to a a web server and makes a request
|
||||||
using an Arduino Wifi shield.
|
using an Arduino Wifi shield.
|
||||||
|
|
||||||
Circuit:
|
Circuit:
|
||||||
* Wifi shield attached to pins 10, 11, 12, 13
|
* WiFi shield attached to pins SPI pins and pin 7
|
||||||
|
|
||||||
created 23 April 2012
|
created 23 April 2012
|
||||||
modifide 31 May 2012
|
modified 31 May 2012
|
||||||
by Tom Igoe
|
by Tom Igoe
|
||||||
|
modified 13 Jan 2014
|
||||||
|
by Federico Vanzati
|
||||||
|
|
||||||
http://arduino.cc/en/Tutorial/WifiWebClientRepeating
|
http://arduino.cc/en/Tutorial/WifiWebClientRepeating
|
||||||
This code is in the public domain.
|
This code is in the public domain.
|
||||||
@@ -31,9 +33,8 @@ WiFiClient client;
|
|||||||
char server[] = "www.arduino.cc";
|
char server[] = "www.arduino.cc";
|
||||||
//IPAddress server(64,131,82,241);
|
//IPAddress server(64,131,82,241);
|
||||||
|
|
||||||
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
|
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 = 10L * 1000L; // delay between updates, in milliseconds
|
||||||
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
//Initialize serial and wait for port to open:
|
//Initialize serial and wait for port to open:
|
||||||
@@ -46,9 +47,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to SSID: ");
|
Serial.print("Attempting to connect to SSID: ");
|
||||||
@@ -72,33 +77,27 @@ void loop() {
|
|||||||
Serial.write(c);
|
Serial.write(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there's no net connection, but there was one last time
|
// if ten seconds have passed since your last connection,
|
||||||
// through the loop, then stop the client:
|
// then connect again and send data:
|
||||||
if (!client.connected() && lastConnected) {
|
if (millis() - lastConnectionTime > postingInterval) {
|
||||||
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)) {
|
|
||||||
httpRequest();
|
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:
|
// this method makes a HTTP connection to the server:
|
||||||
void httpRequest() {
|
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 there's a successful connection:
|
||||||
if (client.connect(server, 80)) {
|
if (client.connect(server, 80)) {
|
||||||
Serial.println("connecting...");
|
Serial.println("connecting...");
|
||||||
// send the HTTP PUT request:
|
// send the HTTP PUT request:
|
||||||
client.println("GET /latest.txt HTTP/1.1");
|
client.println("GET /latest.txt HTTP/1.1");
|
||||||
client.println("Host: www.arduino.cc");
|
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("Connection: close");
|
||||||
client.println();
|
client.println();
|
||||||
|
|
||||||
@@ -108,8 +107,6 @@ void httpRequest() {
|
|||||||
else {
|
else {
|
||||||
// if you couldn't make a connection:
|
// if you couldn't make a connection:
|
||||||
Serial.println("connection failed");
|
Serial.println("connection failed");
|
||||||
Serial.println("disconnecting.");
|
|
||||||
client.stop();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +129,3 @@ void printWifiStatus() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -41,9 +41,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to SSID: ");
|
Serial.print("Attempting to connect to SSID: ");
|
||||||
@@ -93,7 +97,7 @@ void loop() {
|
|||||||
client.println("<br />");
|
client.println("<br />");
|
||||||
}
|
}
|
||||||
client.println("</html>");
|
client.println("</html>");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (c == '\n') {
|
if (c == '\n') {
|
||||||
// you're starting a new line
|
// you're starting a new line
|
||||||
|
@@ -58,9 +58,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to SSID: ");
|
Serial.print("Attempting to connect to SSID: ");
|
||||||
@@ -98,7 +102,7 @@ void loop() {
|
|||||||
|
|
||||||
// if you're not connected, and ten seconds have passed since
|
// if you're not connected, and ten seconds have passed since
|
||||||
// your last connection, then connect again and send data:
|
// your last connection, then connect again and send data:
|
||||||
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
|
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
|
||||||
sendData(sensorReading);
|
sendData(sensorReading);
|
||||||
}
|
}
|
||||||
// store the state of the connection for next time through
|
// store the state of the connection for next time through
|
||||||
@@ -144,7 +148,7 @@ void sendData(int thisData) {
|
|||||||
Serial.println("disconnecting.");
|
Serial.println("disconnecting.");
|
||||||
client.stop();
|
client.stop();
|
||||||
}
|
}
|
||||||
// note the time that the connection was made or attempted:
|
// note the time that the connection was made or attempted:
|
||||||
lastConnectionTime = millis();
|
lastConnectionTime = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,9 +164,9 @@ int getLength(int someValue) {
|
|||||||
// continually divide the value by ten,
|
// continually divide the value by ten,
|
||||||
// adding one to the digit count for each
|
// adding one to the digit count for each
|
||||||
// time you divide, until you're at 0:
|
// time you divide, until you're at 0:
|
||||||
int dividend = someValue /10;
|
int dividend = someValue / 10;
|
||||||
while (dividend > 0) {
|
while (dividend > 0) {
|
||||||
dividend = dividend /10;
|
dividend = dividend / 10;
|
||||||
digits++;
|
digits++;
|
||||||
}
|
}
|
||||||
// return the number of digits:
|
// return the number of digits:
|
||||||
|
@@ -63,9 +63,13 @@ void setup() {
|
|||||||
if (WiFi.status() == WL_NO_SHIELD) {
|
if (WiFi.status() == WL_NO_SHIELD) {
|
||||||
Serial.println("WiFi shield not present");
|
Serial.println("WiFi shield not present");
|
||||||
// don't continue:
|
// 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:
|
// attempt to connect to Wifi network:
|
||||||
while ( status != WL_CONNECTED) {
|
while ( status != WL_CONNECTED) {
|
||||||
Serial.print("Attempting to connect to SSID: ");
|
Serial.print("Attempting to connect to SSID: ");
|
||||||
@@ -112,7 +116,7 @@ void loop() {
|
|||||||
|
|
||||||
// if you're not connected, and ten seconds have passed since
|
// if you're not connected, and ten seconds have passed since
|
||||||
// your last connection, then connect again and send data:
|
// your last connection, then connect again and send data:
|
||||||
if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
|
if (!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
|
||||||
sendData(dataString);
|
sendData(dataString);
|
||||||
}
|
}
|
||||||
// store the state of the connection for next time through
|
// store the state of the connection for next time through
|
||||||
|
Reference in New Issue
Block a user