Arduino: configuration using DHCP
For a small project, I got an 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 dumping information on a Web server.
The project is designed for long-term use. Devices can be installed in hard-to-reach places or in vandal proof housings. During the operation of the system, I want to be able to replace or add new devices to the system without using a programmer.
Given all this, I decided to be puzzled by the issue of transferring the configuration from a central source (server) during the start of the board, rather than writing the configuration to EEPROM or Flash.
BOOTP / DHCP Protocol, 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 features is the definition of its own types of variables and their processing on the client side. I just want to use this opportunity.
The implementation of the DHCP protocol in the 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, netmask, DNS server and default route.
I modified the corresponding code in the library and now it has become possible to implement this configuration method:
Thus, in order to receive parameters from the server and configure your application, you must implement the function
This method can be implemented on any DHCP server where additional options can be defined. For example, for ISC-DHCP this will look as follows (we define a new option xTargetWebServerAddress with number 128):
These options need to be added to
As a bonus, I made it possible to send additional options as part of the request. In the example, Arduino will send the Vendor Class Identifier option (number 60) with a value of "MSFT 5.0".
Changes await integration into the main Arduino code on GitHub. So far, you can use my branch .
The project is designed for long-term use. Devices can be installed in hard-to-reach places or in vandal proof housings. During the operation of the system, I want to be able to replace or add new devices to the system without using a programmer.
Given all this, I decided to be puzzled by the issue of transferring the configuration from a central source (server) during the start of the board, rather than writing the configuration to EEPROM or Flash.
BOOTP / DHCP Protocol, 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 features is the definition of its own types of variables and their processing on the client side. I just want to use this opportunity.
The implementation of the DHCP protocol in the 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, netmask, DNS server and default route.
I modified the corresponding code in the library and now it has become possible to implement this configuration method:
// Определяем функцию-обработчик для неизвестных опций
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 receive parameters from the server and configure your application, you must implement the function
dhcpOptionParser
and read the data received from the DHCP server in it. This method can be implemented on any DHCP server where additional options can be defined. For example, for ISC-DHCP this will look as follows (we define a new option xTargetWebServerAddress with number 128):
option xTargetWebServerAddress code 128 = ip-address;
option xTargetWebServerAddress www.habrahabr.ru;
These options 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, Arduino will send the Vendor Class Identifier option (number 60) with a value of "MSFT 5.0".
Changes await integration into the main Arduino code on GitHub. So far, you can use my branch .