Mengirim Data Dari Arduino Ethernet Pada Database MySQL

 


Berikut adalah cara mengirim data :

1. Arduino + Ethernet Shield



2. Program Arduino IDE

#include <SPI.h>

#include <Ethernet.h>

int pgigi = 10 ;

int pumum = 9 ;

int petugas = 7 ;

int petugas1 = 5;

int y;

int currx;

String txData="";

byte mac[] = {

  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//char server[] = "www.xyz.com";  

IPAddress ip(192,168,1,99);

EthernetClient client;

void setup() {

  Serial.begin(9600);

  while (!Serial) {

    ; //wait for serial port to connect. Needed for Leonardo only

  }

//  if (Ethernet.begin(mac) == 0) {

//    Serial.println("Failed to configure Ethernet using DHCP");

    Ethernet.begin(mac, ip);

//  }

    delay(1000);

    Serial.println("connecting...");

    kirim ();

}


void loop()

{

  kirim ();

}

void kirim (){

  pgigi++;

  petugas++;

  pumum++;

  petugas1++;

  EthernetClient client;

  //if (client.connect(server, 80)){

   if (client.connect("192.168.1.111", 80)){

    txData = "pgigi="+ (String (pgigi)) + "&petugas="+ (String (petugas)) + "&pumum="+ (String (pumum)) + "&petugas1="+ (String (petugas1));

    Serial.println("connected");

    Serial.print(txData);

    client.println("POST /antri/update1.php HTTP/1.1");

    //jika menggunakan insert pakai client.println("POST /antri/insert.php HTTP/1.1");

    client.println("Host: 192,168,1,111");

    client.println("Connection: close");

    client.print("Content-Type: application/x-www-form-urlencoded\n");

    client.print("Content-Length: ");

    client.print(txData.length());

    client.print("\n\n");

    client.print(txData);

    Serial.println(txData);

    delay (1000);

  }   

  else{

    Serial.println("Connection Failed."); 

    Serial.println();

    delay (1500);

  }

}


3. File config1.php

<?php

$dbhost = 'localhost';

$dbuser = 'root';

//$dbpass = '';    

$dbname = 'coba'; //This is the name of your database on the mysql server

$conn = mysql_connect($dbhost,$dbuser) or die ('Error connecting to mysql'); 

mysql_select_db($dbname);

if(!$conn){

        echo "database gak konek";

    }else{

        echo "database konek";

    }

?>


4. File update1.php

<?php

require("config1.php");

$query = "UPDATE tbcoba SET pgigi='$_POST[pgigi]', petugas='$_POST[petugas]', pumum='$_POST[pumum]', petugas1='$_POST[petugas1]' WHERE no=1";

if(!@mysql_query($query))

{

    echo "&Answer; SQL Error - ".mysql_error();

    return;

}

?>


5. File insert.php

<?php

require("config1.php");

$query = "INSERT INTO tbcoba (pgigi, petugas, pumum, petugas1) VALUES('$_POST[pgigi]','$_POST[petugas]','$_POST[pumum]','$_POST[petugas1]')";

if(!@mysql_query($query))

{

    echo "&Answer; SQL Error - ".mysql_error();

    return;

}

?>


6. Cara Penggunaan Alat


a. Pertama ketik file.update1.php dan config1.php

b. Simpan di C:\xampp\htdocs\antri\  (buat folder antri)

c. Setelah itu ketik program Arduino Uno dan Upload

d. Jadikan IP komputer / laptop static dan set ke IP = 192.168.1.111 dan subnet mask = 255.255.255.0

e. Pastikan IP router satu kelas dengan IP komputer dan IP Arduino

f.  Buka PhpmyAdmin dan buat database berikut

- Nama Database = coba

- Nama Tabel = tbcoba

- Coloum Tabel = 5

>> Coloum1 = no

>> Coloum2 = pgigi

>> Coloum3 = petugas

>> Coloum4 = pumum

>> Coloum5 = petugas1

>> semua dalam bentuk INT (integer)

g. Masukkan data pertama, klik menu insert di phpmyadmin kemudian masukkan data berikut

>> no = 1 (maksudnya isikan value dengan nilai 1)

>> pgigi = 2

>> petugas = 3

>> pumum = 4

>> petugas1 = 5

h. Hidupkan XAMPP, aktifkan apache dan MySQL

i. kemudian hidupkan Arduinonya dan lihat apa yang tersimpan di database