mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-27 18:02:17 +03:00
examples: format all .ino files
This formats all the example source files using Arduino style rules.
This commit is contained in:
committed by
Ivan Grokhotkov
parent
e226251b27
commit
61cd8d8385
@ -1,24 +1,24 @@
|
||||
/*
|
||||
Advanced Chat Server
|
||||
Advanced Chat Server
|
||||
|
||||
A more advanced server that distributes any incoming messages
|
||||
to all connected clients but the client the message comes from.
|
||||
To use telnet to your device's IP address and type.
|
||||
You can see the client's input in the serial monitor as well.
|
||||
Using an Arduino Wiznet Ethernet shield.
|
||||
A more advanced server that distributes any incoming messages
|
||||
to all connected clients but the client the message comes from.
|
||||
To use telnet to your device's IP address and type.
|
||||
You can see the client's input in the serial monitor as well.
|
||||
Using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
* Analog inputs attached to pins A0 through A5 (optional)
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
Analog inputs attached to pins A0 through A5 (optional)
|
||||
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
redesigned to make use of operator== 25 Nov 2013
|
||||
by Norbert Truchsess
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
redesigned to make use of operator== 25 Nov 2013
|
||||
by Norbert Truchsess
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
@ -26,10 +26,11 @@
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
// The IP address will be dependent on your local network.
|
||||
// gateway and subnet are optional:
|
||||
byte mac[] = {
|
||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
||||
IPAddress ip(192,168,1, 177);
|
||||
IPAddress gateway(192,168,1, 1);
|
||||
byte mac[] = {
|
||||
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
|
||||
};
|
||||
IPAddress ip(192, 168, 1, 177);
|
||||
IPAddress gateway(192, 168, 1, 1);
|
||||
IPAddress subnet(255, 255, 0, 0);
|
||||
|
||||
|
||||
@ -43,9 +44,9 @@ void setup() {
|
||||
Ethernet.begin(mac, ip, gateway, subnet);
|
||||
// start listening for clients
|
||||
server.begin();
|
||||
// Open serial communications and wait for port to open:
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for Leonardo only
|
||||
}
|
||||
|
||||
@ -62,9 +63,9 @@ void loop() {
|
||||
if (client) {
|
||||
|
||||
boolean newClient = true;
|
||||
for (byte i=0;i<4;i++) {
|
||||
for (byte i = 0; i < 4; i++) {
|
||||
//check whether this client refers to the same socket as one of the existing instances:
|
||||
if (clients[i]==client) {
|
||||
if (clients[i] == client) {
|
||||
newClient = false;
|
||||
break;
|
||||
}
|
||||
@ -72,8 +73,8 @@ void loop() {
|
||||
|
||||
if (newClient) {
|
||||
//check which of the existing clients can be overridden:
|
||||
for (byte i=0;i<4;i++) {
|
||||
if (!clients[i] && clients[i]!=client) {
|
||||
for (byte i = 0; i < 4; i++) {
|
||||
if (!clients[i] && clients[i] != client) {
|
||||
clients[i] = client;
|
||||
// clead out the input buffer:
|
||||
client.flush();
|
||||
@ -90,8 +91,8 @@ void loop() {
|
||||
// read the bytes incoming from the client:
|
||||
char thisChar = client.read();
|
||||
// echo the bytes back to all other connected clients:
|
||||
for (byte i=0;i<4;i++) {
|
||||
if (clients[i] && (clients[i]!=client)) {
|
||||
for (byte i = 0; i < 4; i++) {
|
||||
if (clients[i] && (clients[i] != client)) {
|
||||
clients[i].write(thisChar);
|
||||
}
|
||||
}
|
||||
@ -99,7 +100,7 @@ void loop() {
|
||||
Serial.write(thisChar);
|
||||
}
|
||||
}
|
||||
for (byte i=0;i<4;i++) {
|
||||
for (byte i = 0; i < 4; i++) {
|
||||
if (!(clients[i].connected())) {
|
||||
// client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false;
|
||||
clients[i].stop();
|
||||
|
@ -1,25 +1,25 @@
|
||||
/*
|
||||
SCP1000 Barometric Pressure Sensor Display
|
||||
|
||||
Serves the output of a Barometric Pressure Sensor as a web page.
|
||||
Uses the SPI library. For details on the sensor, see:
|
||||
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
|
||||
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
|
||||
Serves the output of a Barometric Pressure Sensor as a web page.
|
||||
Uses the SPI library. For details on the sensor, see:
|
||||
http://www.sparkfun.com/commerce/product_info.php?products_id=8161
|
||||
http://www.vti.fi/en/support/obsolete_products/pressure_sensors/
|
||||
|
||||
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
|
||||
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
|
||||
This sketch adapted from Nathan Seidle's SCP1000 example for PIC:
|
||||
http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip
|
||||
|
||||
Circuit:
|
||||
SCP1000 sensor attached to pins 6,7, and 11 - 13:
|
||||
DRDY: pin 6
|
||||
CSB: pin 7
|
||||
MOSI: pin 11
|
||||
MISO: pin 12
|
||||
SCK: pin 13
|
||||
Circuit:
|
||||
SCP1000 sensor attached to pins 6,7, and 11 - 13:
|
||||
DRDY: pin 6
|
||||
CSB: pin 7
|
||||
MOSI: pin 11
|
||||
MISO: pin 12
|
||||
SCK: pin 13
|
||||
|
||||
created 31 July 2010
|
||||
by Tom Igoe
|
||||
*/
|
||||
created 31 July 2010
|
||||
by Tom Igoe
|
||||
*/
|
||||
|
||||
#include <Ethernet.h>
|
||||
// the sensor communicates using SPI, so include the library:
|
||||
@ -156,8 +156,7 @@ void listenForEthernetClients() {
|
||||
if (c == '\n') {
|
||||
// you're starting a new line
|
||||
currentLineIsBlank = true;
|
||||
}
|
||||
else if (c != '\r') {
|
||||
} else if (c != '\r') {
|
||||
// you've gotten a character on the current line
|
||||
currentLineIsBlank = false;
|
||||
}
|
||||
@ -219,5 +218,5 @@ unsigned int readRegister(byte registerName, int numBytes) {
|
||||
// take the chip select high to de-select:
|
||||
digitalWrite(chipSelectPin, HIGH);
|
||||
// return the result:
|
||||
return(result);
|
||||
return (result);
|
||||
}
|
||||
|
@ -1,21 +1,21 @@
|
||||
/*
|
||||
Chat Server
|
||||
Chat Server
|
||||
|
||||
A simple server that distributes any incoming messages to all
|
||||
connected clients. To use telnet to your device's IP address and type.
|
||||
You can see the client's input in the serial monitor as well.
|
||||
Using an Arduino Wiznet Ethernet shield.
|
||||
A simple server that distributes any incoming messages to all
|
||||
connected clients. To use telnet to your device's IP address and type.
|
||||
You can see the client's input in the serial monitor as well.
|
||||
Using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
* Analog inputs attached to pins A0 through A5 (optional)
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
Analog inputs attached to pins A0 through A5 (optional)
|
||||
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
@ -1,18 +1,18 @@
|
||||
/*
|
||||
DHCP-based IP printer
|
||||
|
||||
This sketch uses the DHCP extensions to the Ethernet library
|
||||
to get an IP address via DHCP and print the address obtained.
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
This sketch uses the DHCP extensions to the Ethernet library
|
||||
to get an IP address via DHCP and print the address obtained.
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 12 April 2011
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
created 12 April 2011
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
DHCP Chat Server
|
||||
DHCP Chat Server
|
||||
|
||||
A simple server that distributes any incoming messages to all
|
||||
connected clients. To use telnet to your device's IP address and type.
|
||||
You can see the client's input in the serial monitor as well.
|
||||
Using an Arduino Wiznet Ethernet shield.
|
||||
A simple server that distributes any incoming messages to all
|
||||
connected clients. To use telnet to your device's IP address and type.
|
||||
You can see the client's input in the serial monitor as well.
|
||||
Using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
THis version attempts to get an IP address using DHCP
|
||||
THis version attempts to get an IP address using DHCP
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 21 May 2011
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
Based on ChatServer example by David A. Mellis
|
||||
created 21 May 2011
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
Based on ChatServer example by David A. Mellis
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
Telnet client
|
||||
|
||||
This sketch connects to a a telnet server (http://www.google.com)
|
||||
using an Arduino Wiznet Ethernet shield. You'll need a telnet server
|
||||
to test this with.
|
||||
Processing's ChatServer example (part of the network library) works well,
|
||||
running on port 10002. It can be found as part of the examples
|
||||
in the Processing application, available at
|
||||
http://processing.org/
|
||||
This sketch connects to a a telnet server (http://www.google.com)
|
||||
using an Arduino Wiznet Ethernet shield. You'll need a telnet server
|
||||
to test this with.
|
||||
Processing's ChatServer example (part of the network library) works well,
|
||||
running on port 10002. It can be found as part of the examples
|
||||
in the Processing application, available at
|
||||
http://processing.org/
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 14 Sep 2010
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
created 14 Sep 2010
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
@ -54,15 +54,13 @@ void setup() {
|
||||
// if you get a connection, report back via serial:
|
||||
if (client.connect(server, 10002)) {
|
||||
Serial.println("connected");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// if you didn't get a connection to the server:
|
||||
Serial.println("connection failed");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
// if there are incoming bytes available
|
||||
// from the server, read them and print them:
|
||||
if (client.available()) {
|
||||
|
@ -1,16 +1,16 @@
|
||||
/*
|
||||
UDPSendReceive.pde:
|
||||
This sketch receives UDP message strings, prints them to the serial port
|
||||
and sends an "acknowledge" string back to the sender
|
||||
This sketch receives UDP message strings, prints them to the serial port
|
||||
and sends an "acknowledge" string back to the sender
|
||||
|
||||
A Processing sketch is included at the end of file that can be used to send
|
||||
and received messages for testing with a computer.
|
||||
A Processing sketch is included at the end of file that can be used to send
|
||||
and received messages for testing with a computer.
|
||||
|
||||
created 21 Aug 2010
|
||||
by Michael Margolis
|
||||
created 21 Aug 2010
|
||||
by Michael Margolis
|
||||
|
||||
This code is in the public domain.
|
||||
*/
|
||||
This code is in the public domain.
|
||||
*/
|
||||
|
||||
|
||||
#include <SPI.h> // needed for Arduino versions later than 0018
|
||||
@ -45,17 +45,14 @@ void setup() {
|
||||
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 ");
|
||||
IPAddress remote = Udp.remoteIP();
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Serial.print(remote[i], DEC);
|
||||
if (i < 3)
|
||||
{
|
||||
if (i < 3) {
|
||||
Serial.print(".");
|
||||
}
|
||||
}
|
||||
@ -78,42 +75,42 @@ void loop() {
|
||||
|
||||
/*
|
||||
Processing sketch to run with this example
|
||||
=====================================================
|
||||
=====================================================
|
||||
|
||||
// Processing UDP example to send and receive string data from Arduino
|
||||
// press any key to send the "Hello Arduino" message
|
||||
// Processing UDP example to send and receive string data from Arduino
|
||||
// press any key to send the "Hello Arduino" message
|
||||
|
||||
|
||||
import hypermedia.net.*;
|
||||
import hypermedia.net.*;
|
||||
|
||||
UDP udp; // define the UDP object
|
||||
UDP udp; // define the UDP object
|
||||
|
||||
|
||||
void setup() {
|
||||
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
|
||||
//udp.log( true ); // <-- printout the connection activity
|
||||
udp.listen( true ); // and wait for incoming message
|
||||
}
|
||||
void setup() {
|
||||
udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000
|
||||
//udp.log( true ); // <-- printout the connection activity
|
||||
udp.listen( true ); // and wait for incoming message
|
||||
}
|
||||
|
||||
void draw()
|
||||
{
|
||||
}
|
||||
void draw()
|
||||
{
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
String ip = "192.168.1.177"; // the remote IP address
|
||||
int port = 8888; // the destination port
|
||||
void keyPressed() {
|
||||
String ip = "192.168.1.177"; // the remote IP address
|
||||
int port = 8888; // the destination port
|
||||
|
||||
udp.send("Hello World", ip, port ); // the message to send
|
||||
udp.send("Hello World", ip, port ); // the message to send
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void receive( byte[] data ) { // <-- default handler
|
||||
//void receive( byte[] data, String ip, int port ) { // <-- extended handler
|
||||
void receive( byte[] data ) { // <-- default handler
|
||||
//void receive( byte[] data, String ip, int port ) { // <-- extended handler
|
||||
|
||||
for(int i=0; i < data.length; i++)
|
||||
print(char(data[i]));
|
||||
println();
|
||||
}
|
||||
*/
|
||||
for(int i=0; i < data.length; i++)
|
||||
print(char(data[i]));
|
||||
println();
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
@ -1,20 +1,20 @@
|
||||
/*
|
||||
|
||||
Udp NTP Client
|
||||
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,
|
||||
see http://en.wikipedia.org/wiki/Network_Time_Protocol
|
||||
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,
|
||||
see http://en.wikipedia.org/wiki/Network_Time_Protocol
|
||||
|
||||
created 4 Sep 2010
|
||||
by Michael Margolis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
created 4 Sep 2010
|
||||
by Michael Margolis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
This code is in the public domain.
|
||||
This code is in the public domain.
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
@ -37,8 +37,7 @@ byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing pack
|
||||
// A UDP instance to let us send and receive packets over UDP
|
||||
EthernetUDP Udp;
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
@ -56,13 +55,12 @@ void setup()
|
||||
Udp.begin(localPort);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
sendNTPpacket(timeServer); // send an NTP packet to a time server
|
||||
|
||||
// wait to see if a reply is available
|
||||
delay(1000);
|
||||
if ( Udp.parsePacket() ) {
|
||||
if (Udp.parsePacket()) {
|
||||
// We've received a packet, read the data from it
|
||||
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
|
||||
|
||||
@ -74,7 +72,7 @@ void loop()
|
||||
// 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;
|
||||
Serial.print("Seconds since Jan 1 1900 = " );
|
||||
Serial.print("Seconds since Jan 1 1900 = ");
|
||||
Serial.println(secsSince1900);
|
||||
|
||||
// now convert NTP time into everyday time:
|
||||
@ -91,13 +89,13 @@ void loop()
|
||||
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(':');
|
||||
if ( ((epoch % 3600) / 60) < 10 ) {
|
||||
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(':');
|
||||
if ( (epoch % 60) < 10 ) {
|
||||
if ((epoch % 60) < 10) {
|
||||
// In the first 10 seconds of each minute, we'll want a leading '0'
|
||||
Serial.print('0');
|
||||
}
|
||||
@ -108,8 +106,7 @@ void loop()
|
||||
}
|
||||
|
||||
// send an NTP request to the time server at the given address
|
||||
void sendNTPpacket(char* address)
|
||||
{
|
||||
void sendNTPpacket(char* address) {
|
||||
// set all bytes in the buffer to 0
|
||||
memset(packetBuffer, 0, NTP_PACKET_SIZE);
|
||||
// Initialize values needed to form NTP request
|
||||
|
@ -1,18 +1,18 @@
|
||||
/*
|
||||
Web client
|
||||
|
||||
This sketch connects to a website (http://www.google.com)
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
This sketch connects to a website (http://www.google.com)
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe, based on work by Adrian McEwen
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe, based on work by Adrian McEwen
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
@ -59,15 +59,13 @@ void setup() {
|
||||
client.println("Host: www.google.com");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// kf you didn't get a connection to the server:
|
||||
Serial.println("connection failed");
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
// if there are incoming bytes available
|
||||
// from the server, read them and print them:
|
||||
if (client.available()) {
|
||||
|
@ -1,26 +1,26 @@
|
||||
/*
|
||||
Repeating Web client
|
||||
|
||||
This sketch connects to a a web server and makes a request
|
||||
using a Wiznet Ethernet shield. 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 sketch connects to a a web server and makes a request
|
||||
using a Wiznet Ethernet shield. 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 DNS, by assigning the Ethernet client with a MAC address,
|
||||
IP address, and DNS address.
|
||||
This example uses DNS, by assigning the Ethernet client with a MAC address,
|
||||
IP address, and DNS address.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
|
||||
created 19 Apr 2012
|
||||
by Tom Igoe
|
||||
modified 21 Jan 2014
|
||||
by Federico Vanzati
|
||||
created 19 Apr 2012
|
||||
by Tom Igoe
|
||||
modified 21 Jan 2014
|
||||
by Federico Vanzati
|
||||
|
||||
http://www.arduino.cc/en/Tutorial/WebClientRepeating
|
||||
This code is in the public domain.
|
||||
http://www.arduino.cc/en/Tutorial/WebClientRepeating
|
||||
This code is in the public domain.
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
@ -98,8 +98,7 @@ void httpRequest() {
|
||||
|
||||
// note the time that the connection was made:
|
||||
lastConnectionTime = millis();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// if you couldn't make a connection:
|
||||
Serial.println("connection failed");
|
||||
}
|
||||
|
@ -1,19 +1,19 @@
|
||||
/*
|
||||
Web Server
|
||||
|
||||
A simple web server that shows the value of the analog input pins.
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
A simple web server that shows the value of the analog input pins.
|
||||
using an Arduino Wiznet Ethernet shield.
|
||||
|
||||
Circuit:
|
||||
* Ethernet shield attached to pins 10, 11, 12, 13
|
||||
* Analog inputs attached to pins A0 through A5 (optional)
|
||||
Circuit:
|
||||
Ethernet shield attached to pins 10, 11, 12, 13
|
||||
Analog inputs attached to pins A0 through A5 (optional)
|
||||
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
*/
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
@ -84,8 +84,7 @@ void loop() {
|
||||
if (c == '\n') {
|
||||
// you're starting a new line
|
||||
currentLineIsBlank = true;
|
||||
}
|
||||
else if (c != '\r') {
|
||||
} else if (c != '\r') {
|
||||
// you've gotten a character on the current line
|
||||
currentLineIsBlank = false;
|
||||
}
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
|
||||
Udp NTP Client
|
||||
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,
|
||||
see http://en.wikipedia.org/wiki/Network_Time_Protocol
|
||||
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,
|
||||
see http://en.wikipedia.org/wiki/Network_Time_Protocol
|
||||
|
||||
created 4 Sep 2010
|
||||
by Michael Margolis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
created 4 Sep 2010
|
||||
by Michael Margolis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe
|
||||
|
||||
This code is in the public domain.
|
||||
This code is in the public domain.
|
||||
|
||||
Modified by David Henry to show where all the 'magic numbers' come from.
|
||||
You need to read the RFC-1305 spec to understand https://tools.ietf.org/html/rfc1305
|
||||
mgadriver@gmail.com
|
||||
Modified by David Henry to show where all the 'magic numbers' come from.
|
||||
You need to read the RFC-1305 spec to understand https://tools.ietf.org/html/rfc1305
|
||||
mgadriver@gmail.com
|
||||
|
||||
*/
|
||||
|
||||
@ -42,8 +42,7 @@ struct sRFC1305 packetBuffer; //buffer to hold incoming and outgoing packets
|
||||
// A UDP instance to let us send and receive packets over UDP
|
||||
EthernetUDP Udp;
|
||||
|
||||
void setup()
|
||||
{
|
||||
void setup() {
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(9600);
|
||||
while (!Serial) {
|
||||
@ -62,34 +61,33 @@ void setup()
|
||||
Udp.begin(localPort);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
void loop() {
|
||||
sendNTPpacket(timeServer); // send an NTP packet to a time server
|
||||
|
||||
// wait to see if a reply is available
|
||||
delay(1000);
|
||||
if ( Udp.parsePacket() ) {
|
||||
if (Udp.parsePacket()) {
|
||||
// We've received a packet, read the data from it
|
||||
Udp.read((byte *)&packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
|
||||
#if 0 // just for debugging
|
||||
Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_main),HEX);
|
||||
Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_fraction),HEX);
|
||||
Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdispersion_main),HEX);
|
||||
Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdispersion_fraction),HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.referencetimestamp_main),HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.referencetimestamp_fraction),HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.origintimestamp_main),HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.origintimestamp_fraction),HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.receivetimestamp_main),HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.receivetimestamp_fraction),HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_main),HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_fraction),HEX);
|
||||
#endif
|
||||
#if 0 // just for debugging
|
||||
Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_main), HEX);
|
||||
Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_fraction), HEX);
|
||||
Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdispersion_main), HEX);
|
||||
Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdispersion_fraction), HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.referencetimestamp_main), HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.referencetimestamp_fraction), HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.origintimestamp_main), HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.origintimestamp_fraction), HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.receivetimestamp_main), HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.receivetimestamp_fraction), HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_main), HEX);
|
||||
Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_fraction), HEX);
|
||||
#endif
|
||||
Serial.print("Delay ");
|
||||
Serial.print(ENDIAN_SWAP_16(packetBuffer.rootdelay_main));Serial.print(".");Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_fraction));
|
||||
Serial.print("Seconds since Jan 1 1900 = " );
|
||||
Serial.print(ENDIAN_SWAP_16(packetBuffer.rootdelay_main)); Serial.print("."); Serial.println(ENDIAN_SWAP_16(packetBuffer.rootdelay_fraction));
|
||||
Serial.print("Seconds since Jan 1 1900 = ");
|
||||
unsigned long secsSince1900 = ENDIAN_SWAP_32(packetBuffer.transmittimestamp_main);
|
||||
Serial.print(secsSince1900);Serial.print(".");Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_fraction));
|
||||
Serial.print(secsSince1900); Serial.print("."); Serial.println(ENDIAN_SWAP_32(packetBuffer.transmittimestamp_fraction));
|
||||
|
||||
// now convert NTP time into everyday time:
|
||||
Serial.print("Unix time = ");
|
||||
@ -99,34 +97,33 @@ void loop()
|
||||
unsigned long epoch = secsSince1900 - seventyYears;
|
||||
// print Unix time:
|
||||
Serial.println(epoch);
|
||||
|
||||
|
||||
#define SECS_PER_MINUTE 60
|
||||
#define SECS_PER_HOUR 3600
|
||||
#define SECS_PER_DAY 86400L
|
||||
|
||||
// print the hour, minute and second:
|
||||
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
|
||||
Serial.print((epoch % SECS_PER_DAY) / SECS_PER_HOUR);
|
||||
Serial.print((epoch % SECS_PER_DAY) / SECS_PER_HOUR);
|
||||
Serial.print(':');
|
||||
if ( ((epoch % SECS_PER_HOUR) / SECS_PER_MINUTE) < 10 ) {
|
||||
if (((epoch % SECS_PER_HOUR) / SECS_PER_MINUTE) < 10) {
|
||||
// In the first 10 minutes of each hour, we'll want a leading '0'
|
||||
Serial.print('0');
|
||||
}
|
||||
Serial.print((epoch % SECS_PER_HOUR) / SECS_PER_MINUTE);
|
||||
Serial.print((epoch % SECS_PER_HOUR) / SECS_PER_MINUTE);
|
||||
Serial.print(':');
|
||||
if ( (epoch % SECS_PER_MINUTE) < 10 ) {
|
||||
if ((epoch % SECS_PER_MINUTE) < 10) {
|
||||
// In the first 10 seconds of each minute, we'll want a leading '0'
|
||||
Serial.print('0');
|
||||
}
|
||||
Serial.println(epoch % SECS_PER_MINUTE); // print the second
|
||||
}
|
||||
}
|
||||
// wait ten seconds before asking for the time again
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
// send an NTP request to the time server at the given address
|
||||
unsigned long sendNTPpacket(char* address)
|
||||
{
|
||||
unsigned long sendNTPpacket(char* address) {
|
||||
// set all bytes in the buffer to 0
|
||||
memset((char *)&packetBuffer, 0, NTP_PACKET_SIZE);
|
||||
// Initialize values needed to form NTP request
|
||||
@ -141,7 +138,7 @@ unsigned long sendNTPpacket(char* address)
|
||||
packetBuffer.identifier[1] = 'N';
|
||||
packetBuffer.identifier[2] = '1';
|
||||
packetBuffer.identifier[3] = '4';
|
||||
// Serial.println(*(uint8_t *)&packetBuffer,HEX);
|
||||
// Serial.println(*(uint8_t *)&packetBuffer,HEX);
|
||||
// all NTP fields have been given values, now
|
||||
// you can send a packet requesting a timestamp:
|
||||
Udp.beginPacket(address, 123); //NTP requests are to port 123
|
||||
@ -157,4 +154,4 @@ unsigned long sendNTPpacket(char* address)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user