'--------------------------------------------------------------
' Atmega168 and WIZ810MJ I/O-Webserver
'--------------------------------------------------------------
' fusebits:
' KLA987 = 100010 (default 8 Mhz Int RC)
' C = divide clock by 8 disabled
' G = Preserve EEPROM
$regfile = "m168def.dat"
$crystal = 8000000
$baud = 9600
'EEPROM:
'001 = IP(1)
'002 = IP(2)
'003 = IP(3)
'004 = IP(4)
'005 = MAC(1)
'006 = MAC(2)
'007 = MAC(3)
'008 = MAC(4)
'009 = MAC(5)
'010 = MAC(6)
'011 = Netmask(1)
'012 = Netmask(2)
'013 = Netmask(3)
'014 = Netmask(4)
'015 = Gateway(1)
'016 = Gateway(2)
'017 = Gateway(3)
'018 = Gateway(4)
'019 =
'020 =
'021 = password on buttons (F0 = yes / 0F = other)
'022 = temperaturesensors (F0 = yes / 0F = other)
'023 =
'024 = systemnames changed (0 = default names / other is changed)
'025 - 049 = systeemname1
'050 - 074 = systeemname2
'075 - 099 = systeemname3
'100 - 125 = systeemname4
'126 =
'127 =
'128 =
'129 = password changed
'130 - 139 = password button1 (0 = default names / other is changed)
'140 - 149 = password button2
'150 - 159 = password button3
'160 - 169 = password button4
$hwstack = 80
$swstack = 80
$framesize = 90
' 0 = long board
' 1 = sandwichboard
Const Board = 1
'W5100 - registers
$include "w5100.inc"
Const User = "admin"
Const Password = "bird"
Dim Adres As Word
Dim Adresl As Byte At Adres Overlay
Dim Adresh As Byte At Adres + 1 Overlay
Dim Value As Byte
'Used variables
Dim Second As Byte
Dim Minute As Byte
Dim Hour As Byte
Dim Day As Word
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 Authentification As Byte
Dim Website As Byte 'Flag for website ok / websitewrong selection
Dim Buffer As String * 670 '670 Bytes should be enough for the great header of firefox browsers
Dim Mymac(6) As Byte
Dim Ippart(4) As String * 4
Dim Myip(4) As Byte
Dim Mygw(4) As Byte
Dim Mymsk(4) As Byte
Dim Mymacs(6) As String * 3
Dim Http_auth As Byte
Dim Psw As Byte
Dim X1 As Byte
Dim X2 As Byte
'LM75
Dim Lm75read As Byte '&H91/&H95 read-address LM75
Dim Lm75write As Byte '&H90/&H94 write-address LM75
Dim Temp_vk As Integer 'Before the decimal point
Dim Temp_nk As Byte 'After the decimal point
Dim Msb As Byte 'Temperature
Dim Lsb As Byte
Dim Vz As Byte 'pos./neg. (-/+)
'Systemnames in EEPROM
Dim System1 As Eram String * 20 At 25
Dim System2 As Eram String * 20 At 50
Dim System3 As Eram String * 20 At 75
Dim System4 As Eram String * 20 At 100
'Passwords in EEPROM
Dim Psw1 As Eram String * 6 At 130
Dim Psw2 As Eram String * 6 At 140
Dim Psw3 As Eram String * 6 At 150
Dim Psw4 As Eram String * 6 At 160
'Put default systemnames in the EEPROM
Readeeprom Wert , 24
If Wert = &HFF Then
System1 = "System1 "
System2 = "System2 "
System3 = "System3 "
System4 = "System4 "
Wert = 0
Writeeeprom Wert , 24
End If
'Put default passwords in the EEPROM
Readeeprom Wert , 129
If Wert = &HFF Then
Psw1 = "pass01"
Psw2 = "pass02"
Psw3 = "pass03"
Psw4 = "pass04"
Wert = 0
Writeeeprom Wert , 129
End If
'no password configured, put them ON
Readeeprom Wert , 21
If Wert = &HFF Then
Wert = &HF0
Writeeeprom Wert , 21
End If
'No temperaturesensors configured, put them OFF
Readeeprom Wert , 22
If Wert = &HFF Then
Wert = &H0F
Writeeeprom Wert , 22
End If
Http_auth = 0
Wiz5100_cs Alias Portb.2 'Chipselect WIZ5100
' 0 = long board
' 1 = sandwichboard
#if Board 'sandwichboard
Wiz5100_res Alias Portd.3 'reset of WIZ810MJ
'Other used ports and pins
Relais1 Alias Portd.4
Relais2 Alias Portd.5
Relais3 Alias Portd.6
Relais4 Alias Portd.7
#elseif
Wiz5100_res Alias Portc.0 'long board
Relais1 Alias Portc.1
Relais2 Alias Portc.2
Relais3 Alias Portc.3
Relais4 Alias Portc.4
#endif
'Used ports and pins
Config Wiz5100_cs = Output
Config Wiz5100_res = Output
'Config the port/pin directions
Config Relais1 = Output
Config Relais2 = Output
Config Relais3 = Output
Config Relais4 = Output
Relais1 = 1
Relais2 = 1
Relais3 = 1
Relais4 = 1
'I2c
Config Sda = Portb.7
Config Scl = Portb.6
I2cinit
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
'Here we declare the used sub routines
Declare Sub Wiz5100_init
Declare Sub Wiz5100_receive
Declare Sub Wiz5100_send
Declare Sub Wiz5100_disconnect
Declare Sub Wiz5100_reset
Declare Sub Configure
Declare Sub Banner
Declare Sub Read_ip
Declare Sub Print_ip
Declare Sub Read_mac
Declare Sub Print_mac
Declare Sub Read_mask
Declare Sub Print_mask
Declare Sub Read_gw
Declare Sub Print_gw
Declare Sub Wiz5100_readvalue(byval Reg As Word)
Declare Sub Wiz5100_writevalue(byval Reg As Word , Byval Value As Byte)
Declare Sub Part1
Declare Sub Mrelais1
Declare Sub Mrelais2
Declare Sub Mrelais3
Declare Sub Mrelais4
Declare Sub Msg1
Declare Sub Msg2
'LM75
Declare Sub Dec_in_2er
Declare Sub 2er_in_dec
Declare Sub Read_temp
'Declare Sub Resetpointer
Declare Sub Check
Dim Wiz5100_opcode_read As Byte
Wiz5100_opcode_read = 15
Dim Wiz5100_opcode_write As Byte
Wiz5100_opcode_write = 240
Wait 5
Call Banner
Print "Press key for configuration..."
Call Read_ip
Call Read_gw
Call Read_mask
Call Read_mac
Call Wiz5100_init 'We initialize the Wiz5100
Start Watchdog 'Start the watchdog
Config Timer1 = Timer , Prescale = 256 'Config timer1 for a 1 second interval
Enable Timer1
On Timer1 Isr_timer1 'Timer interrupt routine
Enable Interrupts 'Activate the Interrupts
Timer1 = 34285 'Startvalue for the second timer
Do
Reset Watchdog 'Reset the watchdog
Value = Ischarwaiting()
If Value = 1 Then
Value = Waitkey()
Call Configure
End If
'Get socket status
Call Wiz5100_readvalue(w5100_s0_sr)
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(w5100_s0_cr , &H0) '0401 = S0_CR CLOSE
Call Wiz5100_writevalue(w5100_s0_cr , &H1) '0401 = S0_CR OPEN
Call Wiz5100_writevalue(w5100_s0_cr , &H2) '0401 = S0_CR 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 Bfr As String * 20
Local Tmp As Byte
'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
Readeeprom Psw , 21
'Look for variable and replace them, here we can insert new own variables if we want
If Buffer = "%MRELAIS1%" Then
Call Part1
Mid(buffer , X1) = "1"
If Psw = &HF0 Then
Mid(buffer , X2 , 6) = "MPSW01"
End If
Bfr = System1
Buffer = Buffer + Bfr + Chr(34) + ">"
End If
If Buffer = "%MRELAIS2%" Then
Call Part1
Mid(buffer , X1) = "2"
If Psw = &HF0 Then
Mid(buffer , X2 , 6) = "MPSW02"
End If
Bfr = System2
Buffer = Buffer + Bfr + Chr(34) + ">"
End If
If Buffer = "%MRELAIS3%" Then
Call Part1
Mid(buffer , X1) = "3"
If Psw = &HF0 Then
Mid(buffer , X2 , 6) = "MPSW03"
End If
Bfr = System3
Buffer = Buffer + Bfr + Chr(34) + ">"
End If
If Buffer = "%MRELAIS4%" Then
Call Part1
Mid(buffer , X1) = "4"
If Psw = &HF0 Then
Mid(buffer , X2 , 6) = "MPSW04"
End If
Bfr = System4
Buffer = Buffer + Bfr + Chr(34) + ">"
End If
Readeeprom Tmp , 22
If Buffer = "%TEMP1%" Then
If Tmp = &HF0 Then
Lm75read = &H91
Lm75write = &H90
Call Read_temp
Buffer = "
Temperature 1 " + Chr(vz) + Str(temp_vk) + "." + Chr(temp_nk) + " C" '
Else
Buffer = " "
End If
End If
If Buffer = "%TEMP2%" Then
If Tmp = &HF0 Then
Lm75read = &H95
Lm75write = &H94
Call Read_temp
Buffer = "
Temperature 2 " + Chr(vz) + Str(temp_vk) + "." + Chr(temp_nk) + " C"
Else
Buffer = " "
End If
End If
If Buffer = "%IP%" Then
'Here we show the client IP-adress
Call Wiz5100_readvalue(w5100_s0_dipr0)
Buffer = Str(wert) + "."
Call Wiz5100_readvalue(w5100_s0_dipr1)
Buffer = Buffer + Str(wert) + "."
Call Wiz5100_readvalue(w5100_s0_dipr2)
Buffer = Buffer + Str(wert) + "."
Call Wiz5100_readvalue(w5100_s0_dipr3)
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(w5100_s0_tx_fsr0)
Freesize = Wert
Shift , Freesize , Left , 8
Call Wiz5100_readvalue(w5100_s0_tx_fsr1)
Freesize = Freesize + Wert
If Freesize < Sendsize Then
Goto Freesize
End If
Call Wiz5100_readvalue(w5100_s0_tx_wr0)
Tx_wr = Wert
Shift , Tx_wr , Left , 8
Call Wiz5100_readvalue(w5100_s0_tx_wr1)
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
Tmp_str = Mid(buffer , X , 1)
Tmp_value = Asc(tmp_str)
Call Wiz5100_writevalue(pointer , Tmp_value)
Incr Pointer
Next X
Pointer = &H4000
Incr Uppersize
For X = Uppersize To Sendsize
Tmp_str = Mid(buffer , X , 1)
Tmp_value = Asc(tmp_str)
Call Wiz5100_writevalue(pointer , Tmp_value)
Incr Pointer
Next X
Else
For X = 1 To Sendsize
Tmp_str = Mid(buffer , X , 1)
Tmp_value = Asc(tmp_str)
Call Wiz5100_writevalue(pointer , Tmp_value)
Incr Pointer
Next X
End If
Call Wiz5100_readvalue(w5100_s0_tx_wr0)
Startpos = Wert
Shift , Startpos , Left , 8
Call Wiz5100_readvalue(w5100_s0_tx_wr1)
Startpos = Startpos + Wert
Glaenge = Startpos + Sendsize
'Send Site
Highbyte = High(glaenge)
Call Wiz5100_writevalue(w5100_s0_tx_wr0 , Highbyte)
Lowbyte = Low(glaenge)
Call Wiz5100_writevalue(w5100_s0_tx_wr1 , Lowbyte)
'Set SEND flag
Call Wiz5100_writevalue(w5100_s0_cr , &H20)
Loop
'Set DISCON flag
Call Wiz5100_writevalue(w5100_s0_cr , &H8)
End Sub
Sub Part1
Buffer = ""
Buffer = "