Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

IP settings from web page?

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> EASY TCP/IP
View previous topic :: View next topic  
Author Message
jboavida

Bascom Member



Joined: 12 Feb 2005
Posts: 88
Location: Portugal

portugal.gif
PostPosted: Tue Feb 07, 2017 7:27 pm    Post subject: IP settings from web page? Reply with quote

Hello,

In the examples I see here, the IP, subnet and gateway are fixed at compiler time. But... if you ship a product in the real world, the costumer as to change this settings to adapt them to is own environment.
Most of the commercial products have a default configuration that we can access from the browser and then change the IP etc. to match the network setup (and then reboot).

Anyone as experience with this? It will be only a simple webpage with 3 or for variables to change... and store in EEPROM. I'm thinking in W5500 or better saying WIZ550io...

Thanks in advance for any help

Joaquim
Back to top
View user's profile Visit poster's website MSN Messenger
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5255
Location: Holland

blank.gif
PostPosted: Tue Feb 07, 2017 10:02 pm    Post subject: Reply with quote

yes that is one way to do it. Another way is to use dhcp.
Here is a sample tested on the w5200.

Code:

'-----------------------------------------------------------------------------------------
'name                     : dhcp_SPI-5200.bas
'copyright                : (c) 1995-2017, MCS Electronics   Based on dhcp.bas from Atilio Mosca
'purpose                  : demo: DHCP
'micro                    : Mega88
'suited for demo          : no
'commercial addon needed  : only hardware
'-----------------------------------------------------------------------------------------

$regfile = "M88def.dat"
$crystal = 8000000
$baud = 19200
$hwstack = 40
$swstack = 40
$framesize = 40

Rst Alias Portb.0                                           'WIZ820 used which has a reset pin
Config Rst = Output


Declare Function Xidcookie_ok() As Byte
Declare Function Parse_dhcp_msg(byval Doption As Byte) As Byte
Declare Function Dhcp_ok() As Byte
Declare Sub Print_parse()

'we will use the tt array only in the begin
Dim Tt(548) As Byte                                         'DHCP buffer
Dim Xid(4) As Byte
Dim Mac_add(6) As Byte
Dim Pa(4) As Byte
Dim Result As Word
Dim Ip(4) As Byte , Submask(4) As Byte , Gateway(4) As Byte

'Transaction ID
'This number should be random but we use the MAC address
Xid(1) = 12
Xid(2) = 34
Xid(3) = 56
Xid(4) = 78
'Mac Address
'This number should match the one in the 'Config Tcpip...' line
'Do not use first byte
Mac_add(1) = 0
Mac_add(2) = 128
Mac_add(3) = 12
Mac_add(4) = 34
Mac_add(5) = 56
Mac_add(6) = 78

Config Spi = Hard , Interrupt = Off , Data_order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0
'Init the spi pins
Spiinit

Rst = 0
Waitms 10
Rst = 1
Waitms 150

'we specify IP 0.0.0.0  and gateway 0.0.0.0. Really important is to use local port 68 !
Config Tcpip = Noint , Mac = Mac_add(1) , Ip = 0.0.0.0 , Submask = 0.0.0.0 , Gateway = 0.0.0.0 , Localport = 68 , Chip = W5200 , Spi = 1
Print "Init"

If Dhcp_ok() = 1 Then
   Print "DHCP OK"
Else
   Print "err"
End If

End

Do
   ! nop
  ' a ping should now work
Loop
End


Function Dhcp_ok() As Byte
   Local Btmp As Byte
   Local Try As Byte

   Dhcp_ok = 0                                              'assume error

   If Getsocket(0 , Sock_dgram , 68 , 0) = 0 Then
      For Result = 1 To 548                                 'clear buffer
         Tt(result) = 0
      Next
      Try = 0
      Do
         Btmp = Socketstat(0 , Sel_control)
         If Btmp = Sock_udp Then Exit Do
         Waitms 10
         Incr Try
      Loop Until Try > 100

      'DHCP
      Tt(1) = 1                                             'Dhcp_pack_request
      Tt(2) = 1                                             ' Dhcp_htype10mb
      Tt(3) = 6                                             '                                'mac address lenght Dhcp_hlenethernet
      Tt(4) = 0                                             'Dhcp_hops
      Tt(5) = Xid(1)
      Tt(6) = Xid(2)
      Tt(7) = Xid(3)
      Tt(8) = Xid(4)
      Tt(11) = &H80                                         'dhcp flags
      Tt(29) = Mac_add(1)                                   'MAC address
      Tt(30) = Mac_add(2)
      Tt(31) = Mac_add(3)
      Tt(32) = Mac_add(4)
      Tt(33) = Mac_add(5)
      Tt(34) = Mac_add(6)
      Tt(237) = 99                                             'DHCP cookie
      Tt(238) = 130
      Tt(239) = 83
      Tt(240) = 99
      Tt(241) = 53                                          'options  Dhcpmessagetype
      Tt(242) = 1
      Tt(243) = 1                                           'Dhcp_discover
      Tt(244) = 55                                          'Dhcpparamrequest
      Tt(245) = 4                                           ' Request list
      Tt(246) = 1                                           'Subnet mask
      Tt(247) = 3                                           'Default Gateway router
      Tt(248) = 6                                           'DNS server
      Tt(249) = 255                                         'end options

      Try = 0
      Do
         Incr Try
         Result = Udpwrite(255.255.255.255 , 67 , 0 , Tt(1) , 548 )
         Print "(" ; Result ; " bytes)"
         For Btmp = 1 To 100
            Waitms 10
            Result = Socketstat(0 , Sel_recv)
            If Result > 0 Then Exit For
         Next
      Loop Until Result > 0 Or Try > 9

      If Result > 0 Then
         Print "<-- Receive DHCP Offer (" ; Result ; " bytes)"
         Udpreadheader 0                                    ' read the udp header
         Result = Result - 8
         Result = Udpread(0 , Tt(1) , Result )
         If Tt(1) = 2 Then                                  'DHCP pack reply
            If Xidcookie_ok() = 1 Then                      'same XactionID
               If Tt(241) = 53 Then                         ' Dhcp_offer_ok Dhcpmessagetype
                  If Tt(242) = 1 Then
                     If Tt(243) = 2 Then                                ' Dhcp_offer
                        Tt(1) = 1                           'Dhcp_pack_request
                        Tt(13) = Tt(17)                     'IP client
                        Tt(14) = Tt(18)
                        Tt(15) = Tt(19)
                        Tt(16) = Tt(20)
                        Tt(17) = 0                                               'my IP
                        Tt(18) = 0
                        Tt(19) = 0
                        Tt(20) = 0
                        Tt(241) = 53                        ' Dhcpmessagetype options
                        Tt(242) = 1
                        Tt(243) = 3                         'Dhcp_request
                        Tt(244) = 55                        'Dhcpparamrequest
                        Tt(245) = 4
                        Tt(246) = 1                         'Subnetmask
                        Tt(247) = 3                         'Router
                        Tt(248) = 6                         'Dns
                        Tt(249) = 255                       'Endoption
                        For Result = 250 To 548             'refill
                           Tt(result) = 0
                        Next
                        Result = Udpwrite(255.255.255.255 , 67 , 0 , Tt(1) , 548 )

                        'Print "(" ; Result ; " bytes)"
                        For Btmp = 1 To 100
                           Waitms 10
                           Result = Socketstat(0 , Sel_recv)
                           If Result > 0 Then Exit For
                        Next
                        If Result > 0 Then
                           'Print "<-- Receive DHCP Pack (" ; Result ; " bytes)"
                           'Clearbuff
                           Udpreadheader 0                  ' read the udp header
                           Result = Result - 8
                           Result = Udpread(0 , Tt(1) , Result )
                           If Tt(1) = 2 Then                'DHCP pack reply?
                              If Xidcookie_ok() = 1 Then    'same XactionID?
                                 If Tt(241) = 53 Then       'Dhcpmessagetype  DHCPACK
                                    If Tt(242) = 1 Then
                                       If Tt(243) = 5 Then                                ' Dhcp_ack
                                          Pa(1) = Tt(20)    'Get IP in buffer
                                          Pa(2) = Tt(19)
                                          Pa(3) = Tt(18)
                                          Pa(4) = Tt(17)
                                          Btmp = Memcopy(pa(1) , Ip(1) , 4 , 3)
                                          Print "My IP:  ";
                                          Print_parse
                                          If Parse_dhcp_msg(1) = 1 Then       'subnet mask
                                             Print "Sub Net Mask:  ";
                                             Print_parse
                                             Btmp = Memcopy(pa(1) , Submask(1) , 4 , 3)
                                          End If
                                          If Parse_dhcp_msg(3) = 1 Then       'router
                                             Print "Default Gateway:  ";
                                             Print_parse
                                             Btmp = Memcopy(pa(1) , Gateway(1) , 4 , 3)
                                          End If
                                          If Parse_dhcp_msg(54) = 1 Then       'dhcpserveridentifier
                                             Print "DHCP Server:  ";
                                             Print_parse
                                          End If
                                          If Parse_dhcp_msg(6) = 1 Then       'DNS
                                             Print "DNS Server:  ";
                                             Print_parse
                                          End If
                                          Print "Reconfig..."
                                          Settcp Mac_add(1) , Ip(1) , Submask(1) , Gateway(1)
                                          Dhcp_ok = 1
                                          '*****************************************************************************
                                          '*                         At this point, the Wxx00, responds to the new IP
                                          '*                         provided by the DHCP server.
                                          '*                         You can check this with PING
                                          '*****************************************************************************
                                       End If
                                    End If
                                 End If
                              End If
                           End If
                        End If
                     End If
                  End If
               End If
            End If
         End If
      End If

   End If
   Closesocket 0                                            'do not forget to close the socket
End Function
'-------------------------------------------------------------------------------
'-------------------------------------------------------------------------------
Function Xidcookie_ok() As Byte
   Xidcookie_ok = 0
   If Tt(5) = Xid(1) Then
      If Tt(6) = Xid(2) Then
         If Tt(7) = Xid(3) Then
            If Tt(8) = Xid(4) Then                          'end of XID check
               If Tt(237) = 99 Then                         'cookie check
                  If Tt(238) = 130 Then
                     If Tt(239) = 83 Then
                        If Tt(240) = 99 Then
                           Xidcookie_ok = 1
                        End If
                     End If
                  End If
               End If
            End If
         End If
      End If
   End If
End Function
'-------------------------------------------------------------------------------
Function Parse_dhcp_msg(byval Doption As Byte) As Byte
   Local Ax As Word
   Local Ay As Byte

   Pa(1) = 0
   Pa(2) = 0
   Pa(3) = 0
   Pa(4) = 0
   Parse_dhcp_msg = 0
   Ax = 244

   Do
      Ay = Tt(ax)
      If Ay = 255 Then Exit Do
      If Ay = Doption Then
         Ax = Ax + 5                                        '2
         Pa(1) = Tt(ax)
         Decr Ax
         Pa(2) = Tt(ax)
         Decr Ax
         Pa(3) = Tt(ax)
         Decr Ax
         Pa(4) = Tt(ax)
         Parse_dhcp_msg = 1
         Exit Do
      End If
      Incr Ax
      Ay = Tt(ax)
      Ax = Ax + Ay
      Incr Ax
      If Ax > 548 Then Exit Do
   Loop
End Function

'-------------------------------------------------------------------------------
Sub Print_parse()
   Print Pa(4) ; "." ; Pa(3) ; "." ; Pa(2) ; "." ; Pa(1) ; " "
End Sub
'-------------------------------------------------------------------------------
 

_________________
Mark
Back to top
View user's profile Visit poster's website
jboavida

Bascom Member



Joined: 12 Feb 2005
Posts: 88
Location: Portugal

portugal.gif
PostPosted: Wed Feb 08, 2017 12:03 am    Post subject: Reply with quote

Thanks Mark for the code.

My plan is to use DHCP if the user chooses to do so. But I want also the possibility of using static addresses also.
The costumers always ask for a feature that you choose not to implement.... Smile So I want to cover both methods.

The application is a large LED clock synchronized by NTP. the plan is to do something like the attached image:
in this case the costumer as a internal NTP server. If the IP is set at 0.0.0.0 the code will switch to DHCP.


Joaquim
Back to top
View user's profile Visit poster's website MSN Messenger
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1210
Location: SYDNEY

australia.gif
PostPosted: Wed Feb 08, 2017 10:53 am    Post subject: Reply with quote

Hello Joaquim
Would this be ok?
This is from the project blog section my blog "Building a Webserver with a Wiznet W5100"
Back to top
View user's profile
jboavida

Bascom Member



Joined: 12 Feb 2005
Posts: 88
Location: Portugal

portugal.gif
PostPosted: Wed Feb 08, 2017 1:11 pm    Post subject: Reply with quote

Hello,

Wow, That's even more than I expected. Smile
If you can give me more info it will be highly appreciated.
Thank you

Joaquim
Back to top
View user's profile Visit poster's website MSN Messenger
jboavida

Bascom Member



Joined: 12 Feb 2005
Posts: 88
Location: Portugal

portugal.gif
PostPosted: Wed Feb 08, 2017 11:20 pm    Post subject: Reply with quote

Found the code... Impressive work!

Joaquim
Back to top
View user's profile Visit poster's website MSN Messenger
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 740

poland.gif
PostPosted: Thu Feb 09, 2017 12:10 am    Post subject: Reply with quote

For my purposes I found that light AT commands is sufficient.
You simply type AT+IP=192.168.0.1 and its done. Same for AT+GATE etc.
I add code soon. I think I have english description but must recheck this Very Happy
You can watch how this work (configuring two nodes for work and communication) https://youtu.be/7fQWQiKdWYw?t=1m47s

Pauls Server is indeed great work.
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1210
Location: SYDNEY

australia.gif
PostPosted: Thu Feb 09, 2017 11:39 am    Post subject: Reply with quote

If you also look at "Build a Webserver with ENC28J60 +AVR"
you will see how to put web pages into flash
and how to create them dynamically
also what you want is also contained in that code

You might want to look at my Mega1284 board with RTC
it takes an Arduino W5100 shield
You will find it in the Arduino section "Mega 1284P smt board takes Arduino shields"
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> EASY TCP/IP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum