How to Integrate ENC28J60 with lwIP Using an STM32 Microcontroller
Software and Hardware Requirements
To follow this tutorial, you should have completed this section.
DNS: Name resolve on Linux
To confirm that the DNS server is resolving the correct IP, I also ran it on Linux. The command to resolve a domain name to an IP address is:
nestor@nestor:~$ host test.mosquitto.org
test.mosquitto.org has address 54.36.178.49
test.mosquitto.org has IPv6 address 2001:41d0:303:4831::1
DNS: Name resolve on lwIP using ENC28J60
The below are needed for the server to resolve a domain name to an IP address.
void dns_init(void);called during initialization.void dns_setserver(u8_t numdns, const ip_addr_t *dnsserver);also called during initialization.void dns_tmr(void);called every 1000 milliseconds.-
void dns_gethostbyname(const char *hostname, ip_addr_t *addr, dns_found_callback found, void *callback_arg);used to resolve a domain name.
One function worth noting:
void dns_gethostbyname(const char *hostname, ip_addr_t *addr,
dns_found_callback found, void *callback_arg);
const char *hostname: Domain name to resolve.ip_addr_t *addr: Where the resolved IP will be stored.dns_found_callback found: Callback when resolution succeeds.void *callback_arg: Optional callback argument (NULL in this case).
The below snippets show some of the code I wrote to get DNS client working.
struct mqttBrokerDetails {
const char * name;
union {
ip_addr_t ip;
uint8_t bytes[4];
};
const char * user;
const char * password;
};
dns_init();
// Set the server
ip_addr_t dnsServer;
IP4_ADDR(&dnsServer, 8, 8, 8, 8);
dns_setserver(0, &dnsServer);
ip_addr_t mosquitoIp;
dns_gethostbyname(mqttBroker.name, &mosquitoIp, ipObtained, NULL);
static void ipObtained(const char *name,
const ip_addr_t *ipaddr,
void *callback_arg) {
if(strcmp(mqttBroker.name, name) == 0) {
mqttBroker.ip = *ipaddr;
}
}
Screenshot from STM32CubeIDE
Source Code
The source code for the project can be found here — checkout the dns tag.
NB: The union in Figure 2 was added to expose individual bytes. lwIP stores IP addresses as a uint32_t internally.