Skip to content

Commit 66943b8

Browse files
adding dns resolution with lwip example
1 parent c3ba277 commit 66943b8

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_PASS ""
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include <Arduino_DebugUtils.h>
2+
#include <EthernetC33.h>
3+
#include <WiFiC3.h>
4+
#include "arduino_secrets.h"
5+
6+
static char const SSID[] = SECRET_SSID; /* your network SSID (name) */
7+
static char const PASS[] = SECRET_PASS; /* your network password (use for WPA, or use as key for WEP) */
8+
9+
void application();
10+
11+
#define BLOCKING_DNS_RESOLUTION
12+
13+
void setup() {
14+
Serial.begin(115200);
15+
while(!Serial);
16+
17+
DEBUG_INFO("Setting up netif");
18+
19+
Ethernet.begin();
20+
21+
int res = 0;
22+
DEBUG_INFO("Connecting to AP");
23+
while((res=WiFi.begin(SSID, SECRET_PASS)) != ESP_CONTROL_OK) {
24+
DEBUG_INFO("Connection failed retry: %d", res);
25+
delay(1000);
26+
}
27+
DEBUG_INFO("Connected to AP");
28+
DEBUG_INFO("Beginning");
29+
}
30+
31+
void loop() {
32+
#ifndef LWIP_USE_TIMER
33+
CLwipIf::getInstance().task();
34+
#endif
35+
36+
application();
37+
}
38+
39+
// application stuff
40+
volatile uint8_t state = 0;
41+
uint32_t counter = 0;
42+
43+
char* domains[] = {
44+
"google.it"
45+
, "www.google.com"
46+
, "arduino.cc"
47+
, "oniudra.cc"
48+
, "youtube.it"
49+
, "youtube.com"
50+
, "github.com"
51+
, "drive.google.com"
52+
};
53+
54+
#ifndef BLOCKING_DNS_RESOLUTION
55+
void dns_cbk(const IPAddress& ip) {
56+
DEBUG_INFO("%u DNS response for %s: %s ",
57+
counter,
58+
domains[counter % (sizeof(domains)/sizeof(char*))],
59+
ip.toString().c_str());
60+
state = 1;
61+
counter++;
62+
}
63+
#endif // BLOCKING_DNS_RESOLUTION
64+
65+
void application() {
66+
67+
switch(state) {
68+
case 0:
69+
if(WiFiStation.isDhcpAcquired() && Ethernet.isDhcpAcquired()) {
70+
DEBUG_INFO("dhcp acquired");
71+
72+
state = 1;
73+
}
74+
break;
75+
case 1: {
76+
DEBUG_INFO("changing default Interface: \"%s\"", counter%2==0 ? "Ethernet": "WiFiStation");
77+
78+
CLwipIf::getInstance().setDefaultIface(counter%2==0? (CNetIf*)&Ethernet: (CNetIf*)&WiFiStation);
79+
80+
DEBUG_INFO("%u Performing DNS request for %s",
81+
counter,
82+
domains[counter % (sizeof(domains)/sizeof(char*))]);
83+
#ifdef BLOCKING_DNS_RESOLUTION
84+
IPAddress ip;
85+
86+
auto res = CLwipIf::getInstance().getHostByName(
87+
domains[counter % (sizeof(domains)/sizeof(char*))],
88+
ip,
89+
#ifndef LWIP_USE_TIMER
90+
true);
91+
#else
92+
false);
93+
#endif
94+
95+
counter++;
96+
DEBUG_INFO("%u DNS response for %s: %u %s ",
97+
counter,
98+
domains[counter % (sizeof(domains)/sizeof(char*))],
99+
res,
100+
ip.toString().c_str());
101+
#else // BLOCKING_DNS_RESOLUTION
102+
state = 2;
103+
auto res = CLwipIf::getInstance().getHostByName(
104+
domains[counter % (sizeof(domains)/sizeof(char*))],
105+
dns_cbk);
106+
107+
if(res != 1) {
108+
counter++;
109+
}
110+
#endif // BLOCKING_DNS_RESOLUTION
111+
break;
112+
}
113+
case 2:
114+
// do nothing, request made, wait for request to complete
115+
break;
116+
}
117+
118+
}
119+

0 commit comments

Comments
 (0)