Rabu, 04 Juli 2018

Arduino Code Send Data to URL WEBCLIENT

Ini dasarnya ,..dida pat dari source code bawanarduino:


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

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
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");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /search?q=arduino HTTP/1.1");
    client.println("Host: www.google.com");
    client.println("Connection: close");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop(){
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while (true);
  }
}
=====================

Implementasinya:

INIT:

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

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};

boolean received = false;
EthernetClient client;
float gas;
char server[] = "192.168.1.10"; 
unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 5000;  // delay between updates, in milliseconds

IPAddress ip(192, 168, 1, 212);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);



SETUP:

 Ethernet.begin(mac, ip, gateway, subnet); //jika hanya ingin menggunakan ip static
   
////  Serial.println("Trying to get an IP address using DHCP");
//  if (Ethernet.begin(mac) == 0) {
//    Serial.println("Failed to configure Ethernet using DHCP");
//    // initialize the ethernet device not using DHCP:
//    Ethernet.begin(mac, ip, gateway, subnet);
//  }

 // Ethernet.begin(mac, ip, gateway, subnet);
  // print your local IP address:
  Serial.print("My IP address: ");
  ip = Ethernet.localIP();
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(ip[thisByte], DEC);
    Serial.print(".");
  }
  Serial.println();
  // start listening for clients




void loop() {
  
  int sensorValue = analogRead(A0);
  gas = sensorValue * (100.0 / 1023.0);

....................................................

 while (client.available()) {
    char c = client.read();
    Serial.print(c);
    received = true;
  }
 if(received) {
        client.stop();
        received = false;
 }

  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    int i = random(10000);
    String reading = "1:" + String(i);
    httpRequest(reading);
  }
  lastConnected = client.connected();
  delay(100);
}//loop



void httpRequest(String val) {
  if (client.connect(server, 80)) {    
     client.println("GET /20185/lp2maray/webbiogas/simpan.php?p="+String(gas)+" HTTP/1.1");    

    //client.println("GET /appServer/simpan.php?kelembapan=80&suhu=30&tinggi=45&volume=80 HTTP/1.1");    
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    lastConnectionTime = millis();
  } 
  else {
    Serial.println("connection failed");
    Serial.println("disconnecting.");
    client.stop();
  }
}

====================================

Sedang code di servernya sbb:

<?php
//http://192.168.1.2/appServer/simpan.php?kelembapan=80&suhu=30&tinggi=45&volume=80
//client.println("GET /appServer/simpan.php?kelembapan=80&suhu=30&tinggi=45&volume=80 HTTP/1.1");
 //http://localhost/20185/pnj/webbiogas/simpan.php?p=30.4
  
  
$DBServer = 'localhost';
$DBUser   = 'root';
$DBPass   = '';
$DBName   = 'db_biogaspnj';

$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($conn->connect_error) {
  trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
}

date_default_timezone_set("Asia/Jakarta");
$waktu=date("Y-m-d H:i:s");

$p=strip_tags($_GET["p"]);
$tanggal=date("Y-m-d");
$jam=date("H:i:s");
$sql=" INSERT INTO `tb_biogas` (`id`, `tanggal`, `jam`, `pressure`, `note`) VALUES ('', '$tanggal', '$jam', '$p', '-');";

$simpan=process($conn,$sql);
echo"#SUKSES".$waktu."<br>";
function process($conn,$sql){
$s=false;
$conn->autocommit(FALSE);
try {
  $rs = $conn->query($sql);
  if($rs){
    $conn->commit();
    $last_inserted_id = $conn->insert_id;
  $affected_rows = $conn->affected_rows;
  $s=true;
  }
catch (Exception $e) {
echo 'fail: ' . $e->getMessage();
  $conn->rollback();
}
$conn->autocommit(TRUE);
return $s;
}


?>




Tidak ada komentar:

Posting Komentar