friendev EtherDune TCP/IP library
DNSDemo.ino
Go to the documentation of this file.
1 // EtherDune DNS Demo
2 // Author: Javier Peletier <jm@friendev.com>
3 // Summary: Configures EtherDune to resolve a hostname and print its IP address.
4 //
5 // Copyright (c) 2015 All Rights Reserved, http://friendev.com
6 //
7 // This source is subject to the GPLv2 license.
8 // Please see the License.txt file for more information.
9 // All other rights reserved.
10 //
11 // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
12 // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13 // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
14 // PARTICULAR PURPOSE.
15 
20 
21 
22 #include <ACross.h>
23 #include <FlowScanner.h>
24 #include <DNS.h>
25 
26 
27 #define AC_LOGLEVEL 6
28 #include <ACLog.h>
29 ACROSS_MODULE("DNSDemo");
30 
31 
32 static const uint8_t CS_PIN = 10; //Put here what pin you are using for your ENC28J60's chip select
33 static MACAddress_P mymac = { 0x66, 0x72, 0x69, 0x65, 0x6e, 0x64 };
34 static IPAddress_P gatewayIP = { 192, 168, 1, 1 };
35 static IPAddress_P myIP = { 192, 168, 1, 33 };
36 static IPAddress_P netmask = { 255, 255, 255, 0 };
37 static IPAddress_P dnsIP = { 8, 8, 8, 8 };
38 
39 class DNSResolver : public NetworkService
40 {
41  uint16_t id;
42  bool onPacketReceived(){ return false; };
43  void onDNSResolve(uint8_t status, uint16_t identification, const IPAddress& ip)
44  {
45  if (identification == id)
46  {
47  if (status == 0)
48  {
49  id = 0;
50  Serial.print(F("Resolved hostname to: "));
51  Serial.println(ip.toString());
52 
53  }
54  else
55  {
56  Serial.println("Could not resolve hostname");
57  }
58  }
59  }
60 
61 public:
62  void resolve(const char* hostName)
63  {
64  id = DNS().resolve(hostName);
65  Serial.print(F("Resolving "));
66  Serial.print(hostName);
67  Serial.println(F(" ..."));
68 
69  }
70 
71 } resolver;
72 
73 
74 
75 void setup()
76 {
77 
78  Serial.begin(115200);
79  ACross::init();
80 
81  Serial.println(F("EtherDune DNS sample"));
82  Serial.print(F("Free RAM: ")); Serial.println(ACross::getFreeRam());
83  Serial.println(F("Press any key to start..."));
84 
85  while (!Serial.available());
86 
91  net::dnsIP = dnsIP;
92 
93  if (!net::begin(CS_PIN))
94  ACERROR("failed to start EtherDune");
95 
96  ACINFO("waiting for link...");
97 
98  while (!net::isLinkUp());
99 
100  ACINFO("link is up");
101 
102 
103  resolver.resolve("www.friendev.com");
104 
105 }
106 
107 
108 void loop()
109 {
110  net::loop();
111 
112 }
113 
115 
116 
#define MACAddress_P
Definition: inet.h:242
#define IPAddress_P
helper macro to store an IP address in PROGMEM
Definition: inet.h:170
static DNSClient & DNS()
Obtains access to the DNS service singleton instance.
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
static MACAddress_P mymac
static bool isLinkUp()
Determines whether the network link is ready
Definition: ENC28J60.cpp:306
virtual bool onPacketReceived()=0
Called every time a packet is received.
void setup()
char * toString() const
Converts the IP address to a string for display.
Definition: inet.h:227
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.
Abstract base class for any network service running in EtherDune.
static IPAddress_P netmask
void loop()