friendev EtherDune TCP/IP library
TCPClientDemo_DaytimeClient.ino
Go to the documentation of this file.
1 // EtherDune DNS and TCP Daytime client demo
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Demonstrates how to build a simple TCP client application
4 // and how to resolve a host name to an IP address.
5 //
6 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
7 //
8 // This source is subject to the GPLv2 license.
9 // Please see the License.txt file for more information.
10 // All other rights reserved.
11 //
12 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
13 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
14 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
15 // PARTICULAR PURPOSE.
16 
31 #include <ACross.h>
32 #include <FlowScanner.h>
33 #include <TCPSocket.h>
34 #include <DNS.h>
35 
36 
37 #define AC_LOGLEVEL 5
38 #include <ACLog.h>
39 ACROSS_MODULE("TCPClient Demo");
40 
41 
42 static const uint8_t CS_PIN = 10; //Put here what pin you are using for your ENC28J60's chip select
43 static MACAddress_P mymac = { 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64 };
44 static IPAddress_P gatewayIP = { 192, 168, 1, 1 };
45 static IPAddress_P myIP = { 192, 168, 1, 33 };
46 static IPAddress_P netmask = { 255, 255, 255, 0 };
47 
48 
49 
50 class DaytimeClient : public TCPSocket
51 {
52  uint16_t DNSid;
53 public:
54 
55  void start()
56  {
57  remotePort = 13;
58 
59  DNSid = net::DNS().resolve("ntp-nist.ldsbc.edu");
60  //alternatives here: http://www.jumpjet.info/Offbeat-Internet/Public/Daytime/url.htm
61  }
62 
63  void onConnect()
64  {
65  ACTRACE("on connect");
66  Serial.print(F("Time is:"));
67  }
68 
69  void onClose()
70  {
71  ACTRACE("on close");
72  close(); //properly close the connection.
73  }
74 
75  void onReceive(uint16_t len, const byte* data)
76  {
77  ACTRACE("onReceive: %d bytes", len);
78  Serial.write(data, len);
79  //time info comes in this format
80  //http://www.nist.gov/pml/div688/grp40/its.cfm
81  //this code can be improved by parsing the received string to sync Arduino's own timer, for example
82  }
83 
84 
85  void onDNSResolve(uint8_t status, uint16_t identification, const IPAddress& resolvedIP)
86  {
87 
88  if (identification == DNSid && status==0)
89  {
90  ACINFO("resolved. IP=%d.%d.%d.%d", resolvedIP.b[0], resolvedIP.b[1], resolvedIP.b[2], resolvedIP.b[3]);
91  remoteIP = resolvedIP;
92  connect();
93  }
94  }
95 
96 
97 } sck;
98 
99 
100 void setup()
101 {
102 
103  Serial.begin(115200);
104  ACross::init();
105 
106  Serial.println(F("TCP Client EtherDune example"));
107  Serial.print(F("Free RAM: ")); Serial.println(ACross::getFreeRam());
108  Serial.println(F("Press any key to start..."));
109 
110  while (!Serial.available());
111 
112 
113  net::localIP = myIP;
117  net::dnsIP = IPADDR_P(8, 8, 8, 8);
118 
119  if (!net::begin(CS_PIN))
120  ACERROR("failed to start EtherDune");
121 
122  ACINFO("waiting for link...");
123 
124  while (!net::isLinkUp());
125 
126  ACINFO("link is up");
127 
128 
129 
130  Serial.println(F("connecting..."));
131  sck.start();
132 
133 }
134 
135 
136 
137 void loop()
138 {
139  net::loop();
140 }
141 
142 /// \endcond
#define MACAddress_P
Definition: inet.h:242
#define IPAddress_P
helper macro to store an IP address in PROGMEM
Definition: inet.h:170
uint8_t b[4]
byte-wise access to IP address
Definition: inet.h:183
static DNSClient & DNS()
Obtains access to the DNS service singleton instance.
virtual void onConnect()
Fires when the socket connection is established.
Definition: TCPSocket.cpp:39
static IPAddress_P gatewayIP
virtual void onDNSResolve(uint8_t status, uint16_t identification, const IPAddress &ip)
Called on each network service every time a DNS response is received.
static bool begin(uint8_t cspin)
Initializes EtherDune and the underlying hardware
represents an IP address in memory
Definition: inet.h:181
static IPAddress netmask
Subnet mask.
static IPAddress gatewayIP
IP address of the gateway in this network.
ACROSS_MODULE("ARP")
static const uint8_t CS_PIN
#define IPADDR_P(b0, b1, b2, b3)
defines an IP address as stored in PROGMEM
Definition: inet.h:177
static MACAddress_P mymac
void connect()
Initiates a TCP connection to remoteIP and remotePort.
Definition: TCPSocket.cpp:71
static bool isLinkUp()
Determines whether the network link is ready
Definition: ENC28J60.cpp:306
Implements the TCP protocol.
Definition: TCPSocket.h:45
void setup()
static IPAddress dnsIP
IP address of the DNS server to use.
static MACAddress localMAC
Ethernet MAC address.
uint16_t resolve(const char *name)
Resolves a host name to an IP address
Definition: DNS.cpp:45
static IPAddress_P myIP
static void loop()
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets...
static IPAddress localIP
IP address of this application.
void close()
Attempts to gracefully close a connection.
Definition: TCPSocket.cpp:194
virtual void onReceive(uint16_t len, const byte *data)
Called for each data packet received
Definition: TCPSocket.cpp:50
static IPAddress_P netmask
virtual void onClose()
Fires when the other party closed the incoming end of the connection because it has no more data to s...
Definition: TCPSocket.cpp:31
void loop()