'With a few modifications this works on a Arduino Duemilanove 'with an W5100 Ethernet Shield '-------------------------------------------------------------- ' Atmega168 and WIZ810MJ I/O-Webserver ' Build for the WIZ810MJ Evaluation Board build by Thomas Heldt '-------------------------------------------------------------- ' Software Version 3.0 Copyright by Thomas Heldt ' Free only for non comercial use !!! ' For commercial use contact thomas@heldt-web.de '-------------------------------------------------------------- $regfile = "m328pdef.dat" $crystal = 16000000 $baud = 9600 'put terminal on 9600 baud $hwstack = 80 $swstack = 80 $framesize = 90 'Used variables Dim Second As Byte Dim Minute As Byte Dim Hour As Byte Dim Day As Word Dim A As Byte Dim Wert As Byte Dim Status As Byte Dim Oldstatus As Byte Dim Pointer As Word Dim Tmp_value As Byte Dim Tmp_str As String * 1 Dim Highbyte As Byte Dim Lowbyte As Byte Dim X As Integer Dim Userpass As String * 20 Dim Tmp_auth As String * 20 Dim Authentification As Byte Dim Website As Byte 'Flag for website ok / websitewrong selection Dim Buffer As String * 700 '700 Bytes should be enough for the great header of firefox browsers Const Http_auth = 0 '0= no HTTP-AUTH, 1= with HTTP-AUTH Userpass = "admin:demo" 'Place a : between username and password (max. 20 character all together) Tmp_auth = Base64enc(userpass) 'Used Wiz5100 ports and pins Wiz5100_cs Alias Portb.2 'Chipselect WIZ5100 'Wiz5100_int Alias Portb.1 'INT of WIZ5100 'Wiz5100_res Alias Portc.0 'Reset of WIZ5100 'Used ports and pins Config Wiz5100_cs = Output 'Config Wiz5100_int = Input 'Config Wiz5100_res = Output 'Other used ports and pins Relais1 Alias Portd.4 Relais2 Alias Portd.5 Relais3 Alias Portd.6 Relais4 Alias Portd.7 'Config the port/pin directions 'Config Led = Output Config Relais1 = Output Config Relais2 = Output Config Relais3 = Output Config Relais4 = Output Relais1 = 0 Relais2 = 0 Relais3 = 0 Relais4 = 0 Config Watchdog = 2048 'Watchdog configuration for stable reboot if system hangs 'Configuration of the SPI-bus Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 0 'Init the spi pins Spiinit Const Ip[1] = 192 Const Ip[2] = 168 'Own IP-Adress Const Ip[3] = 0 Const Ip[4] = 50 Const Sn[1] = 255 'Own Networkmask Const Sn[2] = 255 Const Sn[3] = 255 Const Sn[4] = 0 Const Gw[1] = 192 'IP-Adress of the gateway Const Gw[2] = 168 Const Gw[3] = 0 Const Gw[4] = 1 Const Mac[1] = &H00 'Own MAC-Adress Const Mac[2] = &H20 Const Mac[3] = &H18 Const Mac[4] = &HB1 Const Mac[5] = &H15 Const Mac[6] = &H5F Const Wiz5100_opcode_read = 15 'OP-Code for Read from the Wiz5100 Const Wiz5100_opcode_write = 240 'OP-Code for write to the Wiz5100 'Here we declare the used sub routines Declare Sub Wiz5100_init Declare Sub Wiz5100_readvalue(byval Highbyte As Byte , Byval Lowbyte As Byte) Declare Sub Wiz5100_writevalue(byval Highbyte As Byte , Byval Lowbyte As Byte , Byval Value As Byte) Declare Sub Wiz5100_receive Declare Sub Wiz5100_send Declare Sub Wiz5100_disconnect Declare Sub Wiz5100_reset Call Wiz5100_init 'We initialize the Wiz5100 Start Watchdog 'Start the watchdog Config Timer1 = Timer , Prescale = 256 'Config timer1 fora 1 second interval Enable Timer1 On Timer1 Isr_timer1 'Timer interrupt routine Enable Interrupts 'Activate the Interrupts Timer1 = 68570 '34285 'Startvalue for the second timer Do Reset Watchdog 'Reset the watchdog 'Get socket status Call Wiz5100_readvalue(&H4 , &H3) Status = Wert If Status <> Oldstatus Then Oldstatus = Status End If 'Connection was closed, we start the socket new If Status = &H0 Or Status = &H1C Then Call Wiz5100_disconnect() End If 'As long as connection is etablished (&H17) we will look if client send new data If Status = &H17 Then Buffer = "" Call Wiz5100_receive() End If Loop End 'Disconnect connection: close socket, reopen socket and wait for new connection Sub Wiz5100_disconnect Call Wiz5100_writevalue(&H4 , &H1 , &H0) 'CLOSE Call Wiz5100_writevalue(&H4 , &H1 , &H1) 'OPEN Call Wiz5100_writevalue(&H4 , &H1 , &H2) 'LISTEN End Sub 'Send headers and websites Sub Wiz5100_send Local Sendsize As Integer , Pos_tx As Word , Freesize As Word , Offset As Word , Tx_wr As Word Local Startadress As Word , Offsend As Word , Uppersize As Word , Glaenge As Word , Startpos As Word Local Adctemp As Word , Tablewidth As Word , Color As String * 7 'We restore the used website data If Website = 0 Then Restore Websitewrong Elseif Authentification = 0 Then Restore Websiteok Else Restore Websiteauth End If Do Read Buffer 'Look for the end of a website If Buffer = "%END%" Then Exit Do End If 'Look for variable and replace them, here we can insert new own variables if we want If Buffer = "%MRELAIS1%" Then Buffer = "
" Buffer = Buffer + "" Buffer = Buffer + "
" End If If Buffer = "%MRELAIS2%" Then Buffer = "
" Buffer = Buffer + "" Buffer = Buffer + "
" End If If Buffer = "%MRELAIS3%" Then Buffer = "
" Buffer = Buffer + "" Buffer = Buffer + "
" End If If Buffer = "%MRELAIS4%" Then Buffer = "
" Buffer = Buffer + "" Buffer = Buffer + "
" End If If Buffer = "%IP%" Then 'Here we show the client IP-adress Call Wiz5100_readvalue(&H4 , &HC) Buffer = Str(wert) + "." Call Wiz5100_readvalue(&H4 , &HD) Buffer = Buffer + Str(wert) + "." Call Wiz5100_readvalue(&H4 , &HE) Buffer = Buffer + Str(wert) + "." Call Wiz5100_readvalue(&H4 , &HF) Buffer = Buffer + Str(wert) Elseif Buffer = "%DAY%" Then Buffer = Str(day) Elseif Buffer = "%HOUR%" Then Buffer = Str(hour) Elseif Buffer = "%MINUTE%" Then Buffer = Str(minute) Elseif Buffer = "%SECOND%" Then Buffer = Str(second) End If Sendsize = Len(buffer) Freesize: Call Wiz5100_readvalue(&H4 , &H20) Freesize = Wert Shift , Freesize , Left , 8 Call Wiz5100_readvalue(&H4 , &H21) Freesize = Freesize + Wert If Freesize < Sendsize Then Goto Freesize End If Call Wiz5100_readvalue(&H4 , &H24) Tx_wr = Wert Shift , Tx_wr , Left , 8 Call Wiz5100_readvalue(&H4 , &H25) Tx_wr = Tx_wr + Wert Startpos = Tx_wr Offset = Tx_wr And &H7FF Startadress = &H4000 + Offset Offsend = Offset + Sendsize Pointer = Startadress If Offsend > &H800 Then Uppersize = &H800 - Offset For X = 1 To Uppersize Highbyte = High(pointer) Lowbyte = Low(pointer) Tmp_str = Mid(buffer , X , 1) Tmp_value = Asc(tmp_str) Call Wiz5100_writevalue(highbyte , Lowbyte , Tmp_value) Incr Pointer Next X Pointer = &H4000 Incr Uppersize For X = Uppersize To Sendsize Highbyte = High(pointer) Lowbyte = Low(pointer) Tmp_str = Mid(buffer , X , 1) Tmp_value = Asc(tmp_str) Call Wiz5100_writevalue(highbyte , Lowbyte , Tmp_value) Incr Pointer Next X Else For X = 1 To Sendsize Highbyte = High(pointer) Lowbyte = Low(pointer) Tmp_str = Mid(buffer , X , 1) Tmp_value = Asc(tmp_str) Call Wiz5100_writevalue(highbyte , Lowbyte , Tmp_value) Incr Pointer Next X End If Call Wiz5100_readvalue(&H4 , &H24) Startpos = Wert Shift , Startpos , Left , 8 Call Wiz5100_readvalue(&H4 , &H25) Startpos = Startpos + Wert Glaenge = Startpos + Sendsize 'Send Site Highbyte = High(glaenge) Call Wiz5100_writevalue(&H4 , &H24 , Highbyte) Lowbyte = Low(glaenge) Call Wiz5100_writevalue(&H4 , &H25 , Lowbyte) 'Set SEND flag Call Wiz5100_writevalue(&H4 , &H1 , &H20) Loop 'Set DISCON flag Call Wiz5100_writevalue(&H4 , &H1 , &H8) End Sub 'Receive data Sub Wiz5100_receive Local I As Word Local Top As Word Local Bytes_rcv As Word Local Headerend As String * 4 'Check for new incomming data Call Wiz5100_readvalue(&H4 , &H26) Bytes_rcv = Wert Shift , Bytes_rcv , Left , 8 Call Wiz5100_readvalue(&H4 , &H27) Bytes_rcv = Bytes_rcv + Wert If Bytes_rcv > 0 Then Bytes_rcv = Bytes_rcv - 1 For I = 0 To Bytes_rcv Top = I + &H6000 Highbyte = High(top) Lowbyte = Low(top) Call Wiz5100_readvalue(highbyte , Lowbyte) 'We only use correct header, correct header must be < 700 bytes! If Bytes_rcv < 700 Then Buffer = Buffer + Chr(wert) End If Next 'Is header complete or is it a post reply? Headerend = Chr(13) + Chr(10) + Chr(13) + Chr(10) If Instr(buffer , Headerend) > 0 Then Website = 1 #if Http_auth If Instr(buffer , Tmp_auth) > 0 Then 'Authentification Found And Correct Authentification = 0 If Instr(buffer , "POST /") = 1 Then 'Toggle RELAIS1 If Instr(buffer , "MRELAIS1=0") > 0 Then Relais1 = 1 Waitms 500 Relais1 = 0 End If 'Toggle RELAIS2 If Instr(buffer , "MRELAIS2=0") > 0 Then Relais2 = 1 Waitms 500 Relais2 = 0 End If 'Toggle RELAIS3 If Instr(buffer , "MRELAIS3=0") > 0 Then Relais3 = 1 Waitms 500 Relais3 = 0 End If 'Toggle RELAIS4 If Instr(buffer , "MRELAIS4=0") > 0 Then Relais4 = 1 Waitms 500 Relais4 = 0 End If End If Else 'Authentification not found or not correct Authentification = 1 End If #else Authentification = 0 If Instr(buffer , "POST /") = 1 Then 'Toggle RELAIS1 If Instr(buffer , "MRELAIS1=0") > 0 Then Relais1 = 1 Waitms 500 Relais1 = 0 End If 'Toggle RELAIS2 If Instr(buffer , "MRELAIS2=0") > 0 Then Relais2 = 1 Waitms 500 Relais2 = 0 End If 'Toggle RELAIS3 If Instr(buffer , "MRELAIS3=0") > 0 Then Relais3 = 1 Waitms 500 Relais3 = 0 End If 'Toggle RELAIS4 If Instr(buffer , "MRELAIS4=0") > 0 Then Relais4 = 1 Waitms 500 Relais4 = 0 End If End If #endif 'Set RECV flag Call Wiz5100_writevalue(&H4 , &H1 , &H40) Call Wiz5100_send() Elseif Bytes_rcv > 700 Then 'Set RECV flag Call Wiz5100_writevalue(&H4 , &H1 , &H40) Website = 0 Call Wiz5100_send() End If End If End Sub Sub Wiz5100_init Call Wiz5100_reset 'Hardware reset 'Register reset Call Wiz5100_writevalue(&H0 , &H0 , &H80) 'Set gateway IP adress Call Wiz5100_writevalue(&H0 , &H1 , Gw[1]) Call Wiz5100_writevalue(&H0 , &H2 , Gw[2]) Call Wiz5100_writevalue(&H0 , &H3 , Gw[3]) Call Wiz5100_writevalue(&H0 , &H4 , Gw[4]) 'Set Subnetmask Call Wiz5100_writevalue(&H0 , &H5 , Sn[1]) Call Wiz5100_writevalue(&H0 , &H6 , Sn[2]) Call Wiz5100_writevalue(&H0 , &H7 , Sn[3]) Call Wiz5100_writevalue(&H0 , &H8 , Sn[4]) 'Set MAC Call Wiz5100_writevalue(&H0 , &H9 , Mac[1]) Call Wiz5100_writevalue(&H0 , &HA , Mac[2]) Call Wiz5100_writevalue(&H0 , &HB , Mac[3]) Call Wiz5100_writevalue(&H0 , &HC , Mac[4]) Call Wiz5100_writevalue(&H0 , &HD , Mac[5]) Call Wiz5100_writevalue(&H0 , &HE , Mac[6]) 'Set own IP adress Call Wiz5100_writevalue(&H0 , &HF , Ip[1]) Call Wiz5100_writevalue(&H0 , &H10 , Ip[2]) Call Wiz5100_writevalue(&H0 , &H11 , Ip[3]) Call Wiz5100_writevalue(&H0 , &H12 , Ip[4]) 'Initialize socket 0 with TCP on port 80 Call Wiz5100_writevalue(&H4 , &H0 , &H1 ) 'TCP Call Wiz5100_writevalue(&H4 , &H4 , &H0 ) 'Port 80 HTTP Call Wiz5100_writevalue(&H4 , &H5 , &H50 ) Call Wiz5100_disconnect() End Sub Sub Wiz5100_writevalue(byval Highbyte As Byte , Byval Lowbyte As Byte , Byval Value As Byte) Reset Wiz5100_cs A = Wiz5100_opcode_write Spiout A , 1 A = Highbyte Spiout A , 1 A = Lowbyte Spiout A , 1 Spiout Value , 1 Set Wiz5100_cs End Sub Sub Wiz5100_readvalue(byval Highbyte As Byte , Byval Lowbyte As Byte) Reset Wiz5100_cs A = Wiz5100_opcode_read Spiout A , 1 A = Highbyte Spiout A , 1 A = Lowbyte Spiout A , 1 Spiin Wert , 1 Set Wiz5100_cs End Sub 'The W5100 gets its reset from the general Arduino reset Sub Wiz5100_reset ' Wiz5100_res = 0 ' Waitus 30 'Minimum 20 µs ' Wiz5100_res = 1 End Sub Isr_timer1: 'ISR of Timer1 Timer1 = 68570 'Timer1 must start from 34285 again Incr Second 'Here we increase the seconds If Second = 60 Then Second = 0 Incr Minute 'Here we increase the minutes If Minute = 60 Then Minute = 0 Incr Hour 'Here we increase the hours If Hour = 24 Then Hour = 0 Incr Day 'Here we increase the days End If End If End If Return 'Every Website must end with a stand alone Data "%END%" in the last line Websiteauth: Data "HTTP/1.0 401 Unauthorized{013}{010}Server: WIZ810MJ AVR I/O-Webserver{013}{010}WWW-Authenticate: Basic realm={034}Website{034}{013}{010}Content-Type: text/html{013}{010}{013}{010}" Data "" Data "WIZ810MJ AVR I/O-Webserver
" Data "

Remote reboot


" Data "This webserver needs a username/password


" Data "

" Data "
" Data "%END%" Websitewrong: Data "HTTP/1.0 200 Document follows{013}{010}Server: WIZ810MJ AVR I/O-Webserver{013}{010}Content-Type: text/html{013}{010}{013}{010}" Data "" Data "Remote reboot
" Data "

Remote reboot


" Data "" Data "" Data "%END%" 'A Variable must stand alone in a separate Line !!! Websiteok: Data "HTTP/1.0 200 Document follows{013}{010}Server: WIZ810MJ AVR I/O-Webserver{013}{010}Content-Type: text/html{013}{010}{013}{010}" Data "" Data "Remote reboot
" Data "

Remote reboot


System 1" Data "%MRELAIS1%" Data "
System 2" Data "%MRELAIS2%" Data "
System 3" Data "%MRELAIS3%" Data "
System 4" Data "%MRELAIS4%" Data "

Your IP-number is: " Data "%IP%" Data "
This server is online: " Data "%DAY%" Data " Days, " Data "%HOUR%" Data " Hours, " Data "%MINUTE%" Data " Minutes and " Data "%SECOND%" Data " Seconds
" Data "

https://benshobbycorner.nl - 2010
" Data "
" Data "%END%"