Translate

Komunikasi TCP-IP Server Client Arduino Ethernet dengan VB 6 dan MySQL (WEB SERVER)

Komunikasi TCP-IP Server Client Arduino Ethernet dengan VB 6 dan MySQL (WEB SERVER)

      
            Pada siang yang cerah ini saya akan menjelaskan bagaimana komunikasi antara arduino webserver dengan VB 6 via TCP-IP, biasanya komunikasi data umumnya menggunakan serial atau UART, sekarang bagaimana jika dengan menggunakan TCPIP ?, kelebihan cara ini yaitu data bisa diambil oleh komputer mana saja yang terkoneksi ke router, dan juga tanpa kabel / wireless, sedangkan kalau serial harus ada kabel dan juga hanya bisa diakses oleh 1 komputer, tapi ada juga serial yang tanpa kabel misal pakai XBee atau APC220, tapi tetap saja kurang praktis, kalau dengan TCP-IP, segala komputer bisa mengakses data yang diambil oleh arduino atau server, yang terkoneksi ke router server. selain itu pada contoh ini, VB 6 yang dibahas juga terkoneksi ke MySQL localhost. untuk lebih jelasnya berikut pembahasannya.  

>>install XAMPP dan aktifkan apache dam MySQL
>>install ODBC
>>install ARDUINO IDE
>>install Hyperterminal
>>Install VB 6
>>Instal Browser Mozilla / Chrome dll.



a. VIDEO HASILNYA





b. Minimum System Arduino + Ethernet Shield





c. Program Arduino

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,99);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
//EthernetClient client;

//char server[] = "192.168.1.100";

EthernetServer server(80);

const int ledpin21 = 21;
const int ledpin20 = 20;
const int ledpin19 = 19;
const int ledpin18 = 18;
const int ledpin17 = 17;
const int ledpin16 = 16;

String readString;

void setup() {

   pinMode(ledpin21, OUTPUT);
   pinMode(ledpin20, OUTPUT);
   pinMode(ledpin19, OUTPUT);
   pinMode(ledpin18, OUTPUT);
   pinMode(ledpin17, OUTPUT);
   pinMode(ledpin16, OUTPUT);

  Serial.begin(9600);
   while (!Serial) {
  }

  Ethernet.begin(mac, ip);
  server.begin();
//  Serial.print("server is at ");
//  Serial.println(Ethernet.localIP());
}

void loop() {
  int an0 = analogRead(0);
  float voltage0= an0 * (5.0 / 1023.0);
  int an1 = analogRead(1);
  float voltage1= an1 * (5.0 / 1023.0);
  int an2 = analogRead(2);
  float voltage2= an2 * (5.0 / 1023.0);
  int an3 = analogRead(3);
  float voltage3= an3 * (5.0 / 1023.0);
  int an4 = analogRead(4);
  float voltage4= an4 * (5.0 / 1023.0);
  int an5 = analogRead(5);
  float voltage5= an5 * (5.0 / 1023.0);
  int an8 = analogRead(8);
  float voltage8= an8 * (5.0 / 1023.0);
  int an9 = analogRead(9);
  float voltage9= an9 * (5.0 / 1023.0);

  EthernetClient client = server.available();
  if (client) {
   // Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
     //   Serial.write(c);
     if (readString.length() < 100) {

          //store characters to string
          readString += c;
          //Serial.print(c);
        }
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println("Refresh: 3");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println(" ");
          client.println("<title> Monitor </title> ");     
          client.println("<body bgcolor = #000000>");     
          client.println("<center>");
            client.println("<font color = yellow><center><h2>MONITORING ARUS DAN TEGANGAN <br> CHARGING STATION </h2></center><br></font>");
            client.println("<font color = #FFFFFF>");
            client.println("<h1>");
            client.println(" Tegangan 1 = ");
            client.print(voltage0);
            client.println("<br />");
            client.println(" Tegangan 2 = ");
            client.print(voltage1);
            client.println("<br />");
            client.println(" Tegangan 3 = ");
            client.print(voltage2);
            client.println("<br />");
            client.println(" Arus 1 = ");
            client.print(voltage3);
            client.println("<br />");
            client.println(" Arus 2 = ");
            client.print(voltage4);
            client.println("<br />");
            client.println(" Arus 3 = ");
            client.print(voltage5);
            client.println("<br />");
            client.println(" Arus 4 = ");
            client.print(voltage8);
            client.println("<br />");
            client.println(" Arus 5 = ");
            client.print(voltage9);           
            client.println("</h1>");      
            client.println("</font><br><br>");
           client.println("<a href = \"/?lightof1\"\"><input type = 'button' value = 'ON 1' ></a>");
           client.println("<a href = \"/?lightof2\"\"><input type = 'button' value = 'OFF 1' ></a> ");    
           client.println("<a href = \"/?lightof3\"\"><input type = 'button' value = 'ON 2' ></a>");
           client.println("<a href = \"/?lightof4\"\"><input type = 'button' value = 'OFF 2' ></a> ");        
           client.println("<a href = \"/?lightof5\"\"><input type = 'button' value = 'ON 3' ></a>");
           client.println("<a href = \"/?lightof6\"\"><input type = 'button' value = 'OFF 3' ></a> ");        
           client.println("<a href = \"/?lightof7\"\"><input type = 'button' value = 'ON 4' ></a>");
           client.println("<a href = \"/?lightof8\"\"><input type = 'button' value = 'OFF 4' ></a> ");           
           client.println("<a href = \"/?lightof9\"\"><input type = 'button' value = 'ON 5' ></a>");
           client.println("<a href = \"/?lightofa\"\"><input type = 'button' value = 'OFF 5' ></a> ");      
           client.println("<a href = \"/?lightofb\"\"><input type = 'button' value = 'ON 6' ></a>");
           client.println("<a href = \"/?lightofc\"\"><input type = 'button' value = 'OFF 6' ></a><br>");      
           client.println("<img src= 'http://localhost/wiring.png' witdh = '600' height ='600'><Br>");
    /*
        if (client.connect("192.168.1.111", 80)) {
            Serial.println("-> Connected");
            client.print( "GET /insertconfig.php?");
            client.print("temp=");
            client.print(an0);
            client.println( " HTTP/1.1");
            client.print( "Host: " );
            client.println("192.168.1.111");
            client.println( "Connection: close");
            client.println();
            client.println();
            client.stop();
            Serial.println("-> Close");

          }

        else {
           // you didn't get a connection to the server:
           Serial.println("--> connection failed/n");
             }       
         */
       
//untuk serial
        Serial.println(voltage0);
        Serial.println(voltage1);
        Serial.println(voltage2);
        Serial.println(voltage3);
        Serial.println(voltage4);
        Serial.println(voltage5);
        Serial.println(voltage8);
        Serial.println(voltage9);
     
//untuk kirim data ke client via TCPIP 
        server.println("data");
        server.println(voltage0);
        server.println(voltage1);
        server.println(voltage2);
        server.println(voltage3);
        server.println(voltage4);
        server.println(voltage5);
        server.println(voltage8);
        server.println(voltage9);
        server.println(" ");
        break; 
  
        }    
        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  //  Serial.println("client disonnected");
  }
 
          if(readString.indexOf("?lightof1") >0)
          {
             digitalWrite(ledpin21, LOW);   
          }
         
          if(readString.indexOf("?lightof2") >0)
          {
             digitalWrite(ledpin21, HIGH); 
          }
        
          if(readString.indexOf("?lightof3") >0)
          {
             digitalWrite(ledpin20, LOW);   
          }
         
          if(readString.indexOf("?lightof4") >0)
          {
             digitalWrite(ledpin20, HIGH);
          }
      
          if(readString.indexOf("?lightof5") >0)
          {
             digitalWrite(ledpin19, LOW);   
          }
         
          if(readString.indexOf("?lightof6") >0)
          {
            digitalWrite(ledpin19, HIGH);
          }
                   
          if(readString.indexOf("?lightof7") >0)
          {
             digitalWrite(ledpin18, LOW);  
          }
         
          if(readString.indexOf("?lightof8") >0)
          {
             digitalWrite(ledpin18, HIGH);
          }
        
          if(readString.indexOf("?lightof9") >0)
          {
             digitalWrite(ledpin17, LOW);
          }
         
          if(readString.indexOf("?lightofa") >0)
          {
             digitalWrite(ledpin17, HIGH);
          }  
         
          if(readString.indexOf("?lightofb") >0)
          {
             digitalWrite(ledpin16, LOW);
          }
         
          if(readString.indexOf("?lightofc") >0)
          {
             digitalWrite(ledpin16, HIGH);
          }                
         readString="";
}




d. Program VB 6 

 >> Pastikan install ODBC terlebih dahulu untuk bisa berhubungan dengan database MySQL


Bisa juga di Download Disini
'=========================================================
Option Explicit
Dim sHari As String      'Deklarasi variabel global, karena digunakan
Dim aHari                'oleh lebih dari satu prosedur
Dim jam, menit, detik

Dim db As Connection
Dim rs As Recordset

Sub koneksi()
Set db = New Connection
db.CursorLocation = adUseClient
db.Open "charging"
End Sub


Private Sub cmd_Connect_Click()

On Error GoTo a

If Winsock.State <> sckClosed Then Winsock.Close
Winsock.RemoteHost = Text_IP
Winsock.RemotePort = Text_PORT
Winsock.Connect
cmd_Connect.Enabled = False
cmd_Disconnect.Enabled = True

Exit Sub
a:
MsgBox "error: " & Err.Description, vbCritical, "Error"

End Sub

Private Sub cmd_Disconnect_Click()
Winsock.Close
MsgBox ("Koneksi dengan Server selesai")
cmd_Connect.Enabled = True
cmd_Disconnect.Enabled = False
End Sub


Private Sub Command_Clear_Click()
Text1.Text = ""
End Sub



Private Sub Command1_Click()
Unload Me
End Sub

Private Sub Form_Load()
aHari = Array("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu")
               
Timer1.Enabled = True

cmd_Connect.Enabled = True
cmd_Disconnect.Enabled = False

koneksi
Set rs = New Recordset
rs.Open "tbcharging", db, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = rs
DataGrid1.Refresh


End Sub


Private Sub Timer1_Timer()
sHari = aHari(Abs(Weekday(Date) - 1))
lblTanggal.Caption = "" & sHari & "," & Format(Date, "dd mmmm yyyy")
LblWaktu.Caption = Format(Time, "hh:mm:ss")

If Text5.Text <> " " Then
    Set rs = New Recordset
    rs.Open "tbcharging", db, adOpenDynamic, adLockOptimistic
    rs.AddNew
    rs.Fields("Tegangan1") = Text6(0).Text
    rs.Fields("Tegangan2") = Text6(1).Text
    rs.Fields("Tegangan3") = Text6(2).Text
    rs.Fields("Arus1") = Text6(3).Text
    rs.Fields("Arus2") = Text6(4).Text
    rs.Fields("Arus3") = Text6(5).Text
    rs.Fields("Arus4") = Text6(6).Text
    rs.Fields("Arus5") = Text6(7).Text
    rs.Update

    Set DataGrid1.DataSource = rs
    DataGrid1.Refresh
    End If
 

End Sub

Private Sub Winsock_Connect()
MsgBox ("terkoneksi dengan server")
End Sub

Private Sub Winsock_ConnectionRequest(ByVal requestID As Long)
Winsock.Accept requestID
End Sub

Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)

Dim buffer As String
Dim temp As String
Dim pisah() As String
Dim i As Integer

Dim data As String
data = ""
Winsock.GetData data, vbString
Text1.Text = Text1.Text & data & vbCr

buffer = data

pisah = Split(buffer, Chr$(13))

On Error Resume Next
If buffer <> " " Then
With Text5
    .SelStart = Len(.Text)
    .SelText = buffer
End With
For i = 0 To 8
    If 0 < 9 Then
        Text6(i).Text = CStr(pisah(i))
        End If
        Next i
    Else
    buffer = " "
    End If

End Sub


e. Program Tampilkan Database Ke Browser PHP

=> Pertama Buat dulu file data.php dan config.php, kemudian masukkan ke dalam foleder c/xampp/htdocs/ paste di dalamnya.

========
data.php
========
<html><body><center><h1> DATABASE MONITORING CHARGING STATION</h1>

<?php
include "config.php";

$tampil = mysql_query("SELECT * FROM tbcharging");

 echo "<table border = '1'>
          <tr><th bgcolor = #C87C5B>no</th><th bgcolor = #C87C5B>Tegangan1</th><th bgcolor = #C87C5B>Tegangan2</th><th bgcolor = #C87C5B>Tegangan3</th><th bgcolor = #C87C5B>Arus1</th><th bgcolor = #C87C5B >Arus2</th><th bgcolor = #C87C5B>Arus3</th><th bgcolor = #C87C5B>Arus4</th><th bgcolor = #C87C5B>Arus5</th></tr>";
    $no=1;
    while ($r=mysql_fetch_array($tampil)){
       echo "<tr><td bgcolor = #FFC848>$no</td>
             <td bgcolor = #FFC848>$r[Tegangan1]</td>
             <td bgcolor = #FFC848>$r[Tegangan2]</td>
             <td bgcolor = #FFC848>$r[Tegangan3]</td>
             <td bgcolor = #FFC848>$r[Arus1]</td>
             <td bgcolor = #FFC848>$r[Arus2]</td>
             <td bgcolor = #FFC848>$r[Arus3]</td>
             <td bgcolor = #FFC848>$r[Arus4]</td>
             <td bgcolor = #FFC848>$r[Arus5]</td>
            
             </tr>";
      $no++;
    }
    echo "</table>";

mysql_close($con);

?>

</center>
</body>
</html>
 

========
config.php
========
<?php
$con = mysql_connect("127.0.0.1","root","");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("charging", $con);

?>



f. Buat database MySQL

1. Nama Database = charging
2. Nama Table = tbcharging




g. HASIL PENGUJIAN

>> isikan address dengan alamat IP server
>> isikan juga IP server dan port 80 untuk via Hyperterminal






Selamat Mencoba...........







1 comment:

  1. Pengen Skali pelajari
    Tapi sayang, File VBnya tidak bisa di download

    ReplyDelete