📜 ⬆️ ⬇️

Arduino: configuration using DHCP

For a small project, I got Arduino Ethernet and started writing programs for it. The project involves connecting dozens or even several hundred identical devices to an IP network and periodically resetting information to a Web server.

The project is designed for long-term use. Devices can be installed in hard-to-reach places or in vandal-resistant enclosures. During the operation of the system, I want to be able to replace or add new devices to the system without using a programmer.

Considering all this, I decided to take up the question of transferring the configuration from the central source (server) when the board started working, rather than writing the configuration to EEPROM or Flash.

BOOTP / DHCP , which is defined in RFC 951, 1531, and 2131, and is designed just to make the initial configuration of network devices. One of the protocol capabilities is the definition of native variable types and their processing on the client side. I just want to use this opportunity.
')
The implementation of the DHCP Arduino Ethernet library is very limited and does not imply an extension. Using DHCP, it is possible (in version 1.0.3) to set only the IP address, network mask, DNS server and default route.

I modified the corresponding code in the library and now it became possible to implement this way of setting up:
//  -    void dhcpOptionParser(const uint8_t optionType, EthernetUDP *dhcpUdpSocket) { uint8_t opt_len = dhcpUdpSocket->read(); if (optionType == 15) { // domain name //       dhcpUdpSocket->read() } else { while (opt_len--) { dhcpUdpSocket->read(); } } } //  -    void dhcpOptionProvider(const uint8_t messageType, EthernetUDP *dhcpUdpSocket) { uint8_t buffer[] = { dhcpClassIdentifier, 0x08, 'M','S','F','T',' ','5','.','0' }; dhcpUdpSocket->write(buffer, 10); } //... //   Ethernet    void setup() { // ... Ethernet.begin(mac, (DhcpOptionParser *) &dhcpOptionParser, (DhcpOptionProvider *) &dhcpOptionProvider); // ... } 

Thus, in order to get the parameters from the server and configure your application, you must implement the dhcpOptionParser function and read the data received from the DHCP server in it.

This method can be implemented on any DHCP server where you can define additional options. For example, for ISC-DHCP, it will look like this (define a new xTargetWebServerAddress option number 128):
 option xTargetWebServerAddress code 128 = ip-address; option xTargetWebServerAddress www.habrahabr.ru; 

These parameters need to be added to dhcpd.conf .

As a bonus, I made it possible to send additional options as part of the request. In the example, the Arduino will send the Vendor Class Identifier option (number 60) with the value "MSFT 5.0".


Changes are waiting for integration into the main Arduino code on GitHub. So far, you can use my branch .

Source: https://habr.com/ru/post/167031/


All Articles