Step 4: Adding More Bells and Whistles
This and other projects in the Code Examples section are published on our website under the name "test_dhcp_lib".
OK, DHCP now works but your device is probably getting a new IP each time it boots. This is because the device does not "suggest" an IP address when calling dhcp_start(). It is a good practice to do so — memorize the previously obtained IP, then request the same from the DHCP server during the next boot.
Since the IP address must be preserved between boots, your device needs to store it in the EEPROM. Use the STG (settings) library for the purpose (this is, by the way, a shining example of STG's immense usefulness). In the code below, two settings — "IPN" and "IPW" — remember the IP addresses for the Ethernet and Wi-Fi interfaces. Note that the STG library is integrated here in a rather sloppy way. Everything will work but don't take this as a golden standard for STG library use.
Suggested IP address is provided in the on_sys_init() handler, when executing dhcp_start(), first for Ethernet, and then for Wi-Fi. When you run the code for the first time, settings won't have valid values, so stg_get() will return default values of the settings, which are "0.0.0.0" (see settings.txt descriptor file). This means that no specific IP is being requested.
Notice also how device names ("tibbo_net" and "tibbo_wln") are supplied with dhcp_start().
** Tibbo Basic **
'DEFINES-------------------------------------------------------------
#define DHCP_DEBUG_PRINT 1 'to see debug output by the DHCP lib
'this is for WLN library
#if PLATFORM_ID=EM500 or PLATFORM_ID=EM500W
#define WLN_RESET_MODE 1 'there will be no dedicated reset, and all other lines are fixed
#elif PLATFORM_ID=EM1206 or PLATFORM_ID=EM1206W
#define WLN_CLK PL_IO_NUM_14
#define WLN_CS PL_IO_NUM_15
#define WLN_DI PL_IO_NUM_12
#define WLN_DO PL_IO_NUM_13
#define WLN_RST PL_IO_NUM_11
#else
'EM1000, NB1010,...
#define WLN_CLK PL_IO_NUM_53
#define WLN_CS PL_IO_NUM_49
#define WLN_DI PL_IO_NUM_52
#define WLN_DO PL_IO_NUM_50
#define WLN_RST PL_IO_NUM_51
#endif
'INCLUDES------------------------------------------------------------
include "sock\trunk\sock.tbh" 'this lib is necessary for the DHCP lib's operation
include "settings\trunk\settings.tbh" 'this lib is for saving obtained IPs
includepp "settings.txt" 'for STG library
include "wln\trunk\wln.tbh" 'this lib is necessary for the DHCP on Wi-Fi interface
include "dhcp\trunk\dhcp.tbh"
** Tibbo Basic **
...
sub on_sys_init()
stg_start()
#if NET_AVAILABLE
dhcp_start(PL_SOCK_INTERFACE_NET,stg_get("IPN",0),"tibbo_net")
#endif
#if WLN_AVAILABLE
wln_start("TIBB1",WLN_SECURITY_MODE_WEP64,"1234567890",PL_WLN_DOMAIN_FCC) '<-- set what you need here, see WLN lib docs
dhcp_start(PL_SOCK_INTERFACE_WLN,stg_get("IPW",0),"tibbo_wln")
#endif
end sub
...
When DHCP configuration completes successfully, callback_dhcp_ok() is called and the obtained IP is saved into the corresponding setting using stg_set(). This also "repairs" the setting if its previous data was invalid. This way, when the device boots next time, stg_get() will return the actual saved data.
** Tibbo Basic **
...
sub callback_dhcp_ok(renew as no_yes, interface as pl_sock_interfaces, byref ip as string, byref gateway_ip as string, byref netmask as string, lease_time as dword)
dim f as byte
#if NET_AVAILABLE
if interface=PL_SOCK_INTERFACE_NET then
if renew=YES and net.ip<>ip then
'this is a lease renewal and the DHCP server has issues new IP
'it is better to reboot than deal with the implications of the changed IP
sys.reboot
end if
if net.ip<>ip then
'no need to close sockets -- this is definitely NOT a renewal an the DHCP lib
'has already closed all socket connections on this interface prior to setting
'the IP to 0.0.0.0
net.ip=ip
net.gatewayip=gateway_ip
net.netmask=netmask
net_dhcp_fail_ctr=0
'save this IP into the EEPROM for future use
if stg_get("IPN",0)<>ip then
stg_set("IPN",0,ip)
end if
end if
end if
#endif
#if WLN_AVAILABLE
if interface=PL_SOCK_INTERFACE_WLN then
if renew=YES and wln.ip<>ip then
'this is a lease renewal and the DHCP server has issues new IP
'it is better to reboot than deal with the implications of the changed IP
sys.reboot
end if
if wln.ip<>ip then
'no need to close sockets -- this is definitely NOT a renewal an the DHCP lib
'has already closed all socket connections on this interface prior to setting
'the IP to 0.0.0.0
wln.ip=ip
wln.gatewayip=gateway_ip
wln.netmask=netmask
wln_dhcp_fail_ctr=0
'save this IP into the EEPROM for future use
if stg_get("IPW",0)<>ip then
stg_set("IPW",0,ip)
end if
end if
end if
#endif
end sub
...
'--------------------------------------------------------------------
sub callback_stg_error(byref stg_name_or_num as string,index as byte,status as en_stg_status_codes)
end sub
'--------------------------------------------------------------------
sub callback_stg_pre_get(byref stg_name_or_num as string,index as byte,byref stg_value as string)
end sub
'--------------------------------------------------------------------
sub callback_stg_post_set(byref stg_name_or_num as string, index as byte,byref stg_value as string)
end sub
...
** Tibbo Basic **
>>IPN E D 1 4 4 A 0.0.0.0 Ethernet IP
>>IPW E D 1 4 4 A 0.0.0.0 Wi-Fi IP
#define STG_DESCRIPTOR_FILE "settings.xtxt"
#define STG_MAX_NUM_SETTINGS 2
#define STG_MAX_SETTING_NAME_LEN 3
#define STG_MAX_SETTING_VALUE_LEN 15