
Kendali Alat berbasis TCP/IP
I
Pendahuluan
Pada bab ini, dicoba penerapan modul TCP/IP menggunakan NM7010A yang
berfungsi sebagai jembatan antara ethernet dengan mikrokontroler.
Mengenal SmartAVR-TCP/IP
Gambar 3.1 Modul NM7010A-lF Rancangan Aplikasi
Berbasis TCP/IP Gambar berikut menampilkan model sistem berbasis
TCP/IP, dimana dapat digunakan sebagai pengontrol alat jarak jauh berbasis
web dengan biaya yang relatif murah. Gambar 3.2 Model Sistem Berbasis TCP/IP
Tabel 3.1 Susunan kabel sistem
Berikut contoh program: Tcpip.bas: '$programmer = 1 $prog , &HA4 , &HD9, $crystal = 8000000 ' used crystal frequency $baud = 19200 ' use baud rate $hwstack = 32 ' default use 32 for the
hardware stack $swstack = 10 ' default use 10 for the
SW stack $framesize = 40 ' default use 40 for the
frame space 'used constants Const Sock_stream = $01 ' Tcp 'Const Sock_dgram = $02 ' Udp 'Const Sock_ipl_raw = $03 ' Ip Layer Raw Sock 'Const Sock_macl_raw = $04 ' Mac Layer Raw Sock 'Const Sel_control = 0 ' Confirm Socket Status 'Const Sel_send = 1 ' Confirm Tx Free Buffer
Size Const Sel_recv = 2 ' Confirm Rx Data Size 'socket status Const Sock_closed = $00 ' Status Of Connection Closed 'Const Sock_arp = $01 ' Status Of Arp 'Const Sock_listen = $02 ' Status Of Waiting For Tcp Connection
Setup 'Const Sock_synsent = $03 ' Status Of Setting Up Tcp Connection 'Const Sock_synsent_ack =
$04 ' Status Of Setting Up Tcp
Connection 'Const Sock_synrecv = $05 ' Status Of Setting Up Tcp Connection Const Sock_established = $06 ' Status Of Tcp Connection Established Const Sock_close_wait = $07 ' Status Of Closing Tcp Connection 'Const Sock_last_ack = $08 ' Status Of Closing Tcp Connection 'Const Sock_fin_wait1 = $09 ' Status Of Closing Tcp Connection 'Const Sock_fin_wait2 = $0a ' Status Of Closing Tcp Connection 'Const Sock_closing = $0b ' Status Of Closing Tcp Connection 'Const Sock_time_wait = $0c ' Status Of Closing Tcp Connection 'Const Sock_reset = $0d ' Status Of Closing Tcp Connection 'Const Sock_init = $0e ' Status Of Socket Initialization 'Const Sock_udp = $0f ' Status Of Udp 'Const Sock_raw = $10 ' Status of IP RAW Config Pind.4 = Output ' Hardware reset NM7010A module Set Portd.4 Reset Portd.4 Waitms 12 Set Portd.4 Enable Interrupts Config
Tcpip = Int0 , Mac = 12.128.12.34.56.78 , Ip = 192.168.1.8 , Submask =
255.255.255.0 , Gateway = 192.168.1.1 , Localport = 1000 , Tx = $55 , Rx =
$55 , Twi = &HCC , Clock = 300000 'dim used variables Dim S As String * 200 , Shtml As
String * 15 , Sheader As String * 30 Dim Tempw As Word Dim I As Byte , P1 As Byte , P2
As Byte Do
Tempw = Socketstat(0 , 0)
' get status
If Tempw = Sock_established Then Tempw = Socketstat(0 , Sel_recv) ' get received bytes If Tempw > 0 Then ' if there is something received Do Tempw = Tcpread(0 , S) ' read a line If Left(s , 3) = "GET"
Then Gosub Page End If Loop Until S = "" ' wait until we get an empty line Tempw = Tcpwrite(0 , "HTTP/1.0
200 OK{013}{010}") ' send ok Gosub Stuur ' GET or HEAD or POST feedback so
send it Closesocket 0 ' close the connection End If
Elseif Tempw = Sock_close_wait Then Closesocket 0 ' we need to close
Elseif Tempw = Sock_closed Then I = Getsocket(0 , Sock_stream , 80 ,
0) ' get a new socket Socketlisten 0 ' listen
End If Loop End 'get html page out of data Page:
P1 = Instr(s , " ")
' find first space
P1 = P1 + 1
' 4
P2 = Instr(p1 , S , " ")
' find second space
P2 = P2 - P1
Shtml = Mid(s , P1 , P2) ' dont use too long page names
Shtml = Lcase(shtml)
' make lower case Return 'send data Stuur:
Dim Wsize As Word , Ihitcounter As Integer
Tempw = Tcpwrite(0 , "Content-Type: text/html{013}{010}")
If Shtml = "/index.htm" Then ' data from eeprom S =
"<html><head><title>Innovative
Electronics</title></head><body><p><b>DEMO
PENERAPAN SMARTAVR-TCP/IP<br></b>By e-Technology Center 2010<br><br>Hits
: " + Str(ihitcounter) +
"</p></body></html>" Incr Ihitcounter ' increase hitcounter Wsize = Len(s) ' size of body Sheader = "Content-Length: "
+ Str(wsize) + "{013}{010}" Tempw = Tcpwritestr(0 , Sheader ,
255) ' add additional CR and LF Tempw = Tcpwrite(0 , S , Wsize) ' send body
End If
Shtml = "" Return |
|||||||||||||||
|
Gambar 3.3 Flowchart Sistem menggunakan Bascom AVR |

Gambar 3.4 Hasil program web server
Latihan :
1.
Kembangkan aplikasi di atas
sehingga dapat menampilkan suhu dan mengendalikan peralatan secara remote
seperti gambar di bawah ini :

Gambar 3.5 Hasil program web server

Kendali Alat berbasis Web
Pendahuluan
Pada bab ini, dicoba penerapan ASP .Net sebagai aplikasi web yang dapat
diakses secara jarak jauh untuk keperluan pengendalian alat.
Aplikasi Web ASP .Net
ASP .Net ialah program web yang disertakan pada Visual Studio .Net. Untuk pengontrolan alat melalui port serial
pada web server komputer berbasis mikrokontroler AVR. Susunlah rangkaian seperti gambar berikut :

Gambar 5.1 Blok diagram Pengontrol alat berbasis Web
Untuk kode mikrokontroler
AVR ini, kode berikut dapat digunakan :
Config Portb = Output
Dim A As Byte
Do
'start loop
A = Inkey()’terima input dari keyboard/serial
If A = "a" Then
Portb = 1
End If
If A = "b" Then
Portb = 2
End If
If A = "c" Then
Portb = 4
End If
If A = "d" Then
Portb = 8
End If
If A = "e" Then
Portb = 16
End If
If A = "f" Then
Portb = 32
End If
If A = "g" Then
Portb = 64
End If
If A = "h" Then
Portb = 128
End If
Loop
Untuk antarmuka web dengan port serial, digunakan file MR_Widodo.dll yang
sudah dibahas pada buku-buku penulis sebelumnya. Untuk membuat aplikasi web
ini:
1.
Tambahkanlah
referensi file tersebut ke proyek Anda. Kemudian, buatlah proyek baru dan desainlah
seperti gambar berikut, dengan cara menempelkan label, textbox dan button.

Gambar 5.2 Desain program web
2.
Jalankan
aplikasi tersebut, hasilnya jika Relay 1 diklik, harus tampil seperti gambar
berikut :

Gambar 5.3 Hasil demo program
Masukkan kode demo sebagai
berikut :
Website1.sln:
Imports System.IO
Partial Class _Default
Inherits System.Web.UI.Page
Public status1 As Boolean = False
Public status2 As Boolean = False
Public status3 As Boolean = False
Public status4 As Boolean = False
Dim ser As New Mr_Widodo.KomunikasiSerial
Protected Sub Button1_Click(ByVal
sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
ser.KirimDataSerial("a",
"com12")
If status1 = False Then
status1 = True
Label3.Text = "Hidup"
Label3.ForeColor =
Drawing.Color.Red
Label4.Text = "Padam"
Label4.ForeColor =
Drawing.Color.Black
Label5.Text = "Padam"
Label6.Text = "Padam"
Exit Sub
End If
End Sub
Protected Sub Button2_Click(ByVal
sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
ser.KirimDataSerial("b",
"com12")
If status2 = False Then
status2 = True
Label4.Text = "Hidup"
Label4.ForeColor =
Drawing.Color.Red
Label3.Text = "Padam"
Label3.ForeColor = Drawing.Color.Black
Label5.Text = "Padam"
Label6.Text = "Padam"
Exit Sub
End If
End Sub
Latihan :
1.
Kembangkan aplikasi diatas agar
dapat mengetahui response dari keaktifan relay (ada /tidak ada beban, pembacaan sensor dan
lainnya).
Untuk materi lengkap artikel ini,
miliki segera buku :
Kendali Cerdas Alat berbasis Web/TCP-IP/SMS
Terbitan Elex Media Komputindo,
akan terbit Desember 2009