DHCP client example ?

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
phil31
Posts: 348
Joined: 02 Apr 2009 15:02
Location: France
Contact:

DHCP client example ?

#1 Post by phil31 » 17 Dec 2019 15:15

Dear

using the Ethernet library from libstock, can anyone share a working DHCP client init procedure ?
i don't understand whats wrong on my side, but on a new board i use, static IP work as epxected, but no way to get DHCP IP !

can't find any example on the forum or libstock, with this library ?

thanks, best regards all

Code: Select all

   DebugString_UART("Run Network ...")
   Net_Ethernet_Intern_stackInitTCP()
   Net_Ethernet_Intern_Init(@myMacAddr, @myIpAddr, Net_Eth_Int_AUTO_NEGOTIATION and Net_Eth_Int_CUSTOM_MAC )  'init ethernet board

      DebugString_UART("DHCP mode selected")
      if Net_Ethernet_Intern_initDHCP(20) = 1 then                    ' get network configuration from DHCP server, wait 10 sec for the response
         memcpy(@myIpAddr,  Net_Ethernet_Intern_getIpAddress(), 4)    ' fetch IP address
         memcpy(@gwIpAddr,  Net_Ethernet_Intern_getGwIpAddress(), 4)  ' fetch gateway IP address
         memcpy(@dnsIpAddr, Net_Ethernet_Intern_getDnsIpAddress(), 4) ' fetch DNS server address
         memcpy(@ipMask,    Net_Ethernet_Intern_getIpMask(), 4)       ' fetch IP subnet mask
         bDHCPserverFind = true
      else
         DebugString_UART("NO reply from any DHCP server - SET fixed IP")
         bDHCPserverFind = false
      end if


   Net_Ethernet_Intern_confNetwork(@ipMask, @gwIpAddr, @dnsIpAddr)

i always go to 'set fixed ip' .. and wireshark don't show me any DHCP request !
YES, my DHCP server is right working ;o)

phil31
Posts: 348
Joined: 02 Apr 2009 15:02
Location: France
Contact:

Re: DHCP client example ?

#2 Post by phil31 » 18 Dec 2019 10:53

it seem that the "Net_Ethernet_Intern_doPacket()" function need to be 'pooled' during the DHCP request..
something like

Code: Select all

      
      while (Net_Ethernet_Intern_initDHCP(10) = 0) or (Net_Ethernet_Intern_UserTimerSec < 10)
            Net_Ethernet_Intern_doPacket()                            ' Process incoming Ethernet packets
      wend
my goal is to retreive the LAN config from the DHCP server.
it's working when the DHCP server is right running. but in case of the DHCP is OFF, the goal is to switch to a fixed IP.

i'm not sure about the right/best solution .. the help file is not helpful at all ..
please anybody can share tips ?

regards

phil31
Posts: 348
Joined: 02 Apr 2009 15:02
Location: France
Contact:

Re: DHCP client example ?

#3 Post by phil31 » 18 Dec 2019 16:24

please ME team, can you post the simplest example which demonstrate the use of the "Net_Ethernet_Intern_initDHCP" function ?

a full simple project please

thanks, regards

i try this :

Code: Select all

      Net_Ethernet_Intern_stackInitTCP()                                 '  This function initialize TCP Stack. (buffers and co)
      Net_Ethernet_Intern_Init(@myMacAddr, @myIpAddr, Net_Eth_Int_AUTO_NEGOTIATION )  'init ethernet board
      Net_Ethernet_Intern_initDHCP(10)
      memcpy(@myIpAddr,  Net_Ethernet_Intern_getIpAddress(), 4)    ' fetch IP address
      memcpy(@gwIpAddr,  Net_Ethernet_Intern_getGwIpAddress(), 4)  ' fetch gateway IP address
      memcpy(@dnsIpAddr, Net_Ethernet_Intern_getDnsIpAddress(), 4) ' fetch DNS server address
      memcpy(@ipMask,    Net_Ethernet_Intern_getIpMask(), 4)       ' fetch IP subnet mask
   DebugString_UART("end DHCP Network ...")

while true
      Net_Ethernet_Intern_doPacket()               ' Process incoming Ethernet packets
      .. do the job
wend
in Wireshark, i clearly see the DHCP discover / offer / Request and ACK lines later when the main run in the while loop and my IP adress stay to 0.0.0.0 !
the DHCP sequence appear (in wireshark) when "Net_Ethernet_Intern_doPacket()"

please explain the expected mechanism ?

anla
Posts: 7
Joined: 09 Mar 2020 10:25

Re: DHCP client example ?

#4 Post by anla » 09 Mar 2020 10:39

Did you resolve this and got DHCP working ?
I tried the Mikro http sample and changed static to dhcp in the example.
Router sees DHCP reuqest and sends DHCP offer but Ethernetlib dos not Ack or return IP.

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: DHCP client example ?

#5 Post by stefan.filipovic » 17 Mar 2020 12:07

Hi,

Please find a simple example of initDHCP in the attachment.

Kind regards,
Attachments
HTTP_Demo_Internal.zip
(786.41 KiB) Downloaded 119 times
Stefan Filipović

Post Reply

Return to “mikroBasic PRO for PIC32 General”