Jumat, 10 Agustus 2018

Koleksi String dan Implementasinya

sbb kode u upload data ke server dan mengolah data dari server............
Port 80 atau HTTP

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 53
#define RST_PIN 49
MFRC522 mfrc522(SS_PIN, RST_PIN);
int kirim = 0;
String content = "";
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
boolean received = false;
EthernetClient client;
IPAddress server(192, 168, 1, 2);
IPAddress ip(192, 168, 1, 3);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 5000;

String pergi = "0";
String record = "myrfid";
String value2 = "-";
String value3 = "-";
int sudahbuka1 = 0;
int sudahbuka3 = 0;
const int relay = 48;
const int buzzer = 22;
int ledunlock = 23;
int ledlock = 24;


void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  Serial.println();

  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  pinMode(buzzer, OUTPUT);
  pinMode(ledunlock, OUTPUT);
  pinMode(ledlock, OUTPUT);
  lcd.init();
  lcd.backlight();

  SPI.begin();
  mfrc522.PCD_Init();
  //  lcd.setCursor(0, 0);
  //  lcd.print("TUGAS AKHIR PNJ");
  //  lcd.setCursor(0, 1);
  //  lcd.print(" ERWIN - SHEILA ");
  //  delay(3000);
  //  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  UNTUK MULAI   ");
  lcd.setCursor(0, 1);
  lcd.print("   TAP e-KTP    ");
  digitalWrite(ledunlock, LOW);
  digitalWrite(ledlock, HIGH);
}

void loop() {
  //  if (client.available()) {
  //    char c = client.read();
  //    Serial.write(c);
  //    if (c == '@') {
  //      kirim = 0;
  //    }
  //  }


  int status = 1;
  String gabs = "";
  while (client.available() and status == 1) {
    char c = client.read();
    //      Serial.print(c);
    gabs = gabs + String(c);
    received = true;
    if (c == '@') {
      status = 0;
      //break;
    }
  }


  if (gabs.length() > 1) {
    Serial.println("GABS:" + gabs);

    pergi = "0";
    String value1 = splitString(gabs, '#', 0);
    value2 = splitString(gabs, '#', 1);//TX
    value3 = splitString(gabs, '#', 2);
    pergi = splitString(gabs, '#', 3);//pergi
    record = splitString(gabs, '#', 4); //rfid_ktp

    Serial.println("X-TX:" + value2);
    Serial.println("Y-Nama:" + value3);
    Serial.println("Z-PErgi:" + pergi);
    cekstatus();
  }


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


  MFRC522::MIFARE_Key key;

  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  else {
    for (byte i = 0; i < 6; i++) {
      key.keyByte[i] = 0xFF;
    }
  }

  content = "";
  for (byte i = 0; i < mfrc522.uid.size; i++)  {
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  //  Serial.println(kirim);

  int sudah = 0;
  if (content.length() > 10) {
    sudah = 1;
    Serial.println("Kirim0");
    httpRequest0();//CEK validasi
  }
  else if (pergi == "1" && content.length() < 10) {
    sudah = 2;
    Serial.println("Kirim1");
    httpRequest1();
  }
  else if (pergi == "3" && content.length() < 10) {
    sudah = 3;
    Serial.println("Kirim2");
    httpRequest2();
  }

  Serial.println("");
  Serial.println("+++++++++++++++++++");
  Serial.println(sudah);
  Serial.println(content);
  Serial.println(content.length());
  Serial.println(kirim);
  Serial.println("+++++++++++++++++++");

  //=========================

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



  // delay(1000);
  //============================================
  cekstatus();
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == '@') {
      Serial.print("OK.");
      Serial.println(inChar);
    }
  }
}

void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void kunci_box () {
  digitalWrite(relay, LOW);
  digitalWrite(ledunlock, HIGH);
  digitalWrite(ledlock, LOW);
  delay(8000);
  digitalWrite(relay, HIGH);
  digitalWrite(ledunlock, LOW);
  digitalWrite(ledlock, HIGH);
}


void httpRequest0() {
  client.stop();
  client.stop();
  if (client.connect(server, 80)) {
    String myrfid = String(content);
    String SQL = "";
    SQL = "GET /webkunci/prosescek.php?rfid=" + String(myrfid) + "&rfid2=ok&status=ok HTTP/1.1";
    client.println(SQL);
    Serial.print(SQL);
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    client.flush();

    // lastConnectionTime = millis();
  }
  else {
    Serial.println("auto-refresh.");
    client.stop();
  }
}
//===============================================
void httpRequest1() {
  client.stop();
  client.stop();
  if (client.connect(server, 80)) {
    String myrfid = String(content);
    String SQL = "";
    SQL = "GET /webkunci/prosesinsert.php?&ktp=" + String(record) + "&rfid=" + String(myrfid) + "&status=ok HTTP/1.1";
    client.println(SQL);
    Serial.print(SQL);
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    client.flush();

    // lastConnectionTime = millis();
  }
  else {
    Serial.println("auto-refresh.");
    client.stop();
  }
}
//===============================================

void httpRequest2() {
  client.stop();
  client.stop();
  if (client.connect(server, 80)) {
    String myrfid = String(content);
    String SQL = "";
    SQL = "GET /webkunci/prosesupdate.php?ktp=" + String(record) + "&rfid=" + String(myrfid) + "&status=ok HTTP/1.1";
    client.println(SQL);
    Serial.print(SQL);
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    client.flush();

    // lastConnectionTime = millis();
  }
  else {
    Serial.println("auto-refresh.");
    client.stop();
  }
}
//===============================================

void httpRequest() {
  client.stop();
  client.stop();
  if (client.connect(server, 80)) {
    String myrfid = String(content);
    String SQL = "";
    if (pergi.equals("0")) { //cek
      SQL = "GET /webkunci/prosescek.php?rfid=" + String(myrfid) + "&rfid2=ok&status=ok HTTP/1.1";
      client.println(SQL);
      Serial.print(SQL);
      client.println("Host: *HOST*");
      client.println("Connection: keep-open");
      client.println();
      client.flush();
    }
    else if (kirim == 1) { //PINJAM
      SQL = "GET /webkunci/prosesinsert.php?&ktp=" + String(record) + "&rfid=" + String(myrfid) + "&status=ok HTTP/1.1";
      client.println(SQL);
      Serial.print(SQL);
      client.println("Host: *HOST*");
      client.println("Connection: keep-open");
      client.println();
      client.flush();
    }
    else  if (pergi == "3") { //BALIK
      SQL = "GET /webkunci/prosesupdate.php?ktp=" + String(record) + "&rfid=" + String(myrfid) + "&status=ok HTTP/1.1";
      client.println(SQL);
      Serial.print(SQL);
      client.println("Host: *HOST*");
      client.println("Connection: keep-open");
      client.println();
      client.flush();
    }
    // lastConnectionTime = millis();
  }
  else {
    Serial.println("auto-refresh.");
    client.stop();
  }
}

String splitString(String str, char sep, int index)
{
  int found = 0;
  int strIdx[] = { 0, -1 };
  int maxIdx = str.length() - 1;

  for (int i = 0; i <= maxIdx && found <= index; i++)
  {
    if (str.charAt(i) == sep || i == maxIdx)
    {
      found++;
      strIdx[0] = strIdx[1] + 1;
      strIdx[1] = (i == maxIdx) ? i + 1 : i;
    }
  }
  return found > index ? str.substring(strIdx[0], strIdx[1]) : "";
}


void cekstatus() {
  if (pergi == "1") {
    sudahbuka3 = 0;
    if (sudahbuka1 == 0) {
      //      tone(buzzer, 500);
      //      delay(500);
      //      noTone(buzzer);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(value2);
      lcd.setCursor(0, 1);
      lcd.print(value3);
      delay(2000);
      kunci_box ();
      sudahbuka1 = 1;
    }//sudahbuka
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAKAN     ");
    lcd.setCursor(0, 1);
    lcd.print("   TAP KUNCI    ");
    delay(2000);
  }
  else if (pergi == "2" ) {
    sudahbuka1 = 0;
    sudahbuka3 = 0;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(value2);
    lcd.setCursor(0, 1);
    lcd.print(value3);
    delay(1000);
    pergi = "0";


    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
    kirim = 0;
  }
  else if (pergi == "6" ) {
    //    tone(buzzer, 500);
    //    delay(500);
    //    noTone(buzzer);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(value2);
    lcd.setCursor(0, 1);
    lcd.print( record );
    delay(3000);
    pergi = "0";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
    kirim = 0;
  }
  else if (pergi == "3" ) {
    sudahbuka1 = 0;
    if (sudahbuka3 == 0) {
      //      tone(buzzer, 500);
      //      delay(500);
      //      noTone(buzzer);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(value2);
      lcd.setCursor(0, 1);
      lcd.print(value3);
      delay(3000);
      sudahbuka3 = 1;
    }

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAKAN     ");
    lcd.setCursor(0, 1);
    lcd.print("   TAP KUNCI    ");
    delay(1000);

  }
  else if (pergi == "4" ) {
    sudahbuka1 = 0;
    sudahbuka3 = 0;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" MASUKAN KUNCI  ");
    lcd.setCursor(0, 1);
    lcd.print("     KE RAK     ");
    delay(1000);
    kunci_box();
    delay(1000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(value2);
    lcd.setCursor(0, 1);
    lcd.print(value3);
    delay(3000);
    pergi = "0";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
    kirim = 0;
  }//
  else if (pergi == "5" ) {
    sudahbuka1 = 0;
    sudahbuka3 = 0;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(value2);
    lcd.setCursor(0, 1);
    lcd.print(value3);
    delay(3000);
    pergi = "0";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
    kirim = 0;
  }
  else {
    pergi = "0";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
  }
}

Minggu, 29 Juli 2018

Arduino WebClient.....Oh my GOD

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 53
#define RST_PIN 49
MFRC522 mfrc522(SS_PIN, RST_PIN);
int kirim = 0;
String content = "";
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
boolean received = false;
EthernetClient client;
IPAddress server(192, 168, 1, 2);
IPAddress ip(192, 168, 1, 3);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

unsigned long lastConnectionTime = 0;
boolean lastConnected = false;
const unsigned long postingInterval = 5000;

String pergi = "0";
String record = "myrfid";
String value2 = "-";
String value3 = "-";
int sudahbuka1 = 0;
int sudahbuka3 = 0;
const int relay = 48;
const int buzzer = 22;
int ledunlock = 23;
int ledlock = 24;


void setup() {
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  Serial.println();

  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  pinMode(buzzer, OUTPUT);
  pinMode(ledunlock, OUTPUT);
  pinMode(ledlock, OUTPUT);
  lcd.init();
  lcd.backlight();

  SPI.begin();
  mfrc522.PCD_Init();
  //  lcd.setCursor(0, 0);
  //  lcd.print("TUGAS AKHIR PNJ");
  //  lcd.setCursor(0, 1);
  //  lcd.print(" ERWIN - SHEILA ");
  //  delay(3000);
  //  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  UNTUK MULAI   ");
  lcd.setCursor(0, 1);
  lcd.print("   TAP e-KTP    ");
  digitalWrite(ledunlock, LOW);
  digitalWrite(ledlock, HIGH);
}

void loop() {
  //  if (client.available()) {
  //    char c = client.read();
  //    Serial.write(c);
  //    if (c == '@') {
  //      kirim = 0;
  //    }
  //  }


  int status = 1;
  String gabs = "";
  while (client.available() and status == 1) {
    char c = client.read();
    //      Serial.print(c);
    gabs = gabs + String(c);
    received = true;
    if (c == '@') {
      status = 0;
      //break;
    }
  }


  if (gabs.length() > 1) {
    Serial.println("GABS:" + gabs);

    pergi = "0";
    String value1 = splitString(gabs, '#', 0);
    value2 = splitString(gabs, '#', 1);//TX
    value3 = splitString(gabs, '#', 2);
    pergi = splitString(gabs, '#', 3);//pergi
    record = splitString(gabs, '#', 4); //rfid_ktp

    Serial.println("X-TX:" + value2);
    Serial.println("Y-Nama:" + value3);
    Serial.println("Z-PErgi:" + pergi);
    cekstatus();
  }


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


  MFRC522::MIFARE_Key key;

  if ( ! mfrc522.PICC_IsNewCardPresent()) {
    return;
  }
  if ( ! mfrc522.PICC_ReadCardSerial()) {
    return;
  }
  else {
    for (byte i = 0; i < 6; i++) {
      key.keyByte[i] = 0xFF;
    }
  }

  content = "";
  for (byte i = 0; i < mfrc522.uid.size; i++)  {
    content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
    content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  content.toUpperCase();
  //  Serial.println(kirim);

  int sudah = 0;
  if (content.length() > 10) {
    sudah = 1;
    Serial.println("Kirim0");
    httpRequest0();//CEK validasi
  }
  else if (pergi == "1" && content.length() < 10) {
    sudah = 2;
    Serial.println("Kirim1");
    httpRequest1();
  }
  else if (pergi == "3" && content.length() < 10) {
    sudah = 3;
    Serial.println("Kirim2");
    httpRequest2();
  }

  Serial.println("");
  Serial.println("+++++++++++++++++++");
  Serial.println(sudah);
  Serial.println(content);
  Serial.println(content.length());
  Serial.println(kirim);
  Serial.println("+++++++++++++++++++");

  //=========================

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



  // delay(1000);
  //============================================
  cekstatus();
}

void serialEvent() {
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    if (inChar == '@') {
      Serial.print("OK.");
      Serial.println(inChar);
    }
  }
}

void dump_byte_array(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

void kunci_box () {
  digitalWrite(relay, LOW);
  digitalWrite(ledunlock, HIGH);
  digitalWrite(ledlock, LOW);
  delay(8000);
  digitalWrite(relay, HIGH);
  digitalWrite(ledunlock, LOW);
  digitalWrite(ledlock, HIGH);
}


void httpRequest0() {
  client.stop();
  client.stop();
  if (client.connect(server, 80)) {
    String myrfid = String(content);
    String SQL = "";
    SQL = "GET /webkunci/prosescek.php?rfid=" + String(myrfid) + "&rfid2=ok&status=ok HTTP/1.1";
    client.println(SQL);
    Serial.print(SQL);
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    client.flush();

    // lastConnectionTime = millis();
  }
  else {
    Serial.println("auto-refresh.");
    client.stop();
  }
}
//===============================================
void httpRequest1() {
  client.stop();
  client.stop();
  if (client.connect(server, 80)) {
    String myrfid = String(content);
    String SQL = "";
    SQL = "GET /webkunci/prosesinsert.php?&ktp=" + String(record) + "&rfid=" + String(myrfid) + "&status=ok HTTP/1.1";
    client.println(SQL);
    Serial.print(SQL);
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    client.flush();

    // lastConnectionTime = millis();
  }
  else {
    Serial.println("auto-refresh.");
    client.stop();
  }
}
//===============================================

void httpRequest2() {
  client.stop();
  client.stop();
  if (client.connect(server, 80)) {
    String myrfid = String(content);
    String SQL = "";
    SQL = "GET /webkunci/prosesupdate.php?ktp=" + String(record) + "&rfid=" + String(myrfid) + "&status=ok HTTP/1.1";
    client.println(SQL);
    Serial.print(SQL);
    client.println("Host: *HOST*");
    client.println("Connection: keep-open");
    client.println();
    client.flush();

    // lastConnectionTime = millis();
  }
  else {
    Serial.println("auto-refresh.");
    client.stop();
  }
}
//===============================================

void httpRequest() {
  client.stop();
  client.stop();
  if (client.connect(server, 80)) {
    String myrfid = String(content);
    String SQL = "";
    if (pergi.equals("0")) { //cek
      SQL = "GET /webkunci/prosescek.php?rfid=" + String(myrfid) + "&rfid2=ok&status=ok HTTP/1.1";
      client.println(SQL);
      Serial.print(SQL);
      client.println("Host: *HOST*");
      client.println("Connection: keep-open");
      client.println();
      client.flush();
    }
    else if (kirim == 1) { //PINJAM
      SQL = "GET /webkunci/prosesinsert.php?&ktp=" + String(record) + "&rfid=" + String(myrfid) + "&status=ok HTTP/1.1";
      client.println(SQL);
      Serial.print(SQL);
      client.println("Host: *HOST*");
      client.println("Connection: keep-open");
      client.println();
      client.flush();
    }
    else  if (pergi == "3") { //BALIK
      SQL = "GET /webkunci/prosesupdate.php?ktp=" + String(record) + "&rfid=" + String(myrfid) + "&status=ok HTTP/1.1";
      client.println(SQL);
      Serial.print(SQL);
      client.println("Host: *HOST*");
      client.println("Connection: keep-open");
      client.println();
      client.flush();
    }
    // lastConnectionTime = millis();
  }
  else {
    Serial.println("auto-refresh.");
    client.stop();
  }
}

String splitString(String str, char sep, int index)
{
  int found = 0;
  int strIdx[] = { 0, -1 };
  int maxIdx = str.length() - 1;

  for (int i = 0; i <= maxIdx && found <= index; i++)
  {
    if (str.charAt(i) == sep || i == maxIdx)
    {
      found++;
      strIdx[0] = strIdx[1] + 1;
      strIdx[1] = (i == maxIdx) ? i + 1 : i;
    }
  }
  return found > index ? str.substring(strIdx[0], strIdx[1]) : "";
}


void cekstatus() {
  if (pergi == "1") {
    sudahbuka3 = 0;
    if (sudahbuka1 == 0) {
      //      tone(buzzer, 500);
      //      delay(500);
      //      noTone(buzzer);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(value2);
      lcd.setCursor(0, 1);
      lcd.print(value3);
      delay(2000);
      kunci_box ();
      sudahbuka1 = 1;
    }//sudahbuka
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAKAN     ");
    lcd.setCursor(0, 1);
    lcd.print("   TAP KUNCI    ");
    delay(2000);
  }
  else if (pergi == "2" ) {
    sudahbuka1 = 0;
    sudahbuka3 = 0;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(value2);
    lcd.setCursor(0, 1);
    lcd.print(value3);
    delay(1000);
    pergi = "0";


    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
    kirim = 0;
  }
  else if (pergi == "6" ) {
    //    tone(buzzer, 500);
    //    delay(500);
    //    noTone(buzzer);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(value2);
    lcd.setCursor(0, 1);
    lcd.print( record );
    delay(3000);
    pergi = "0";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
    kirim = 0;
  }
  else if (pergi == "3" ) {
    sudahbuka1 = 0;
    if (sudahbuka3 == 0) {
      //      tone(buzzer, 500);
      //      delay(500);
      //      noTone(buzzer);
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print(value2);
      lcd.setCursor(0, 1);
      lcd.print(value3);
      delay(3000);
      sudahbuka3 = 1;
    }

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAKAN     ");
    lcd.setCursor(0, 1);
    lcd.print("   TAP KUNCI    ");
    delay(1000);

  }
  else if (pergi == "4" ) {
    sudahbuka1 = 0;
    sudahbuka3 = 0;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(" MASUKAN KUNCI  ");
    lcd.setCursor(0, 1);
    lcd.print("     KE RAK     ");
    delay(1000);
    kunci_box();
    delay(1000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(value2);
    lcd.setCursor(0, 1);
    lcd.print(value3);
    delay(3000);
    pergi = "0";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
    kirim = 0;
  }//
  else if (pergi == "5" ) {
    sudahbuka1 = 0;
    sudahbuka3 = 0;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print(value2);
    lcd.setCursor(0, 1);
    lcd.print(value3);
    delay(3000);
    pergi = "0";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
    kirim = 0;
  }
  else {
    pergi = "0";
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("    SILAHKAN    ");
    lcd.setCursor(0, 1);
    lcd.print("    TAP e-KTP   ");
    delay(1000);
  }
}



Coba rekan2 mati..ada 10 materi tersembunyi di atas...
1.RFID reader
2.LED IO
3.LCD
4.Web CLient Send data
5.Web Client Read Data
6.Web Client Standby
7.Explode String
8.Serial Event
9.Reset data Buffer
10.Konversi input data
11.Pengalaman......
..

Rabu, 04 Juli 2018

Java Code baca isi tasklist

run:
# =>
#csrss.exe                      824 Console                    1      3,428 K =>csrss.exe
#winlogon.exe                   988 Console                    1      4,088 K =>winlogon.exe
#fontdrvhost.exe                528 Console                    1      3,364 K =>fontdrvhost.exe
#dwm.exe                       1184 Console                    1     47,680 K =>dwm.exe
#nvxdsync.exe                  6912 Console                    1     10,952 K =>nvxdsync.exe
#ByteFence.exe                15160 Console                    1    153,172 K =>ByteFence.exe
#explorer.exe                 16348 Console                    1    126,088 K =>explorer.exe
#sihost.exe                    9424 Console                    1     28,016 K =>sihost.exe
#rtop_bg.exe                  20976 Console                    1     10,168 K =>rtop_bg.exe
#svchost.exe                  16436 Console                    1     21,636 K =>svchost.exe
#dllhost.exe                  16072 Console                    1     11,336 K =>dllhost.exe
#svchost.exe                  20632 Console                    1     34,052 K =>svchost.exe
#ShellExperienceHost.exe       9332 Console                    1     72,164 K =>ShellExperienceHost.exe
#RemindersServer.exe           8908 Console                    1     17,568 K =>RemindersServer.exe
#svchost.exe                  17176 Console                    1     17,096 K =>svchost.exe
#RuntimeBroker.exe            12256 Console                    1     25,676 K =>RuntimeBroker.exe
#RuntimeBroker.exe            11756 Console                    1     34,568 K =>RuntimeBroker.exe
#dllhost.exe                   1808 Console                    1     15,012 K =>dllhost.exe
#ctfmon.exe                    6928 Console                    1     15,388 K =>ctfmon.exe
#dllhost.exe                   5988 Console                    1      9,108 K =>dllhost.exe
#ApplicationFrameHost.exe     14608 Console                    1     23,028 K =>ApplicationFrameHost.exe
#xampp-control.exe            11152 Console                    1     16,828 K =>xampp-control.exe
#mysqld.exe                   11924 Console                    1      8,992 K =>mysqld.exe
#httpd.exe                     7684 Console                    1     11,788 K =>httpd.exe
#conhost.exe                   4196 Console                    1      6,300 K =>conhost.exe
#httpd.exe                    20920 Console                    1     15,088 K =>httpd.exe
#LockApp.exe                   1432 Console                    1     39,696 K =>LockApp.exe
#RuntimeBroker.exe            14904 Console                    1     29,820 K =>RuntimeBroker.exe
#SettingSyncHost.exe           9032 Console                    1      3,288 K =>SettingSyncHost.exe
#SkypeHost.exe                11904 Console                    1     11,388 K =>SkypeHost.exe
#SearchUI.exe                  3808 Console                    1     91,732 K =>SearchUI.exe
#RuntimeBroker.exe            13956 Console                    1     24,168 K =>RuntimeBroker.exe
#chrome.exe                   20948 Console                    1    179,828 K =>chrome.exe
#chrome.exe                    4260 Console                    1      7,732 K =>chrome.exe
#chrome.exe                   16736 Console                    1      8,420 K =>chrome.exe
#chrome.exe                     324 Console                    1    146,356 K =>chrome.exe
#chrome.exe                   10844 Console                    1    144,436 K =>chrome.exe
#chrome.exe                   10596 Console                    1     30,024 K =>chrome.exe
#chrome.exe                   15216 Console                    1     33,724 K =>chrome.exe
#chrome.exe                   10704 Console                    1    266,568 K =>chrome.exe
#chrome.exe                   17144 Console                    1    288,984 K =>chrome.exe
#chrome.exe                   11124 Console                    1    143,640 K =>chrome.exe
#chrome.exe                   20416 Console                    1    143,180 K =>chrome.exe
#chrome.exe                   14028 Console                    1    125,476 K =>chrome.exe
#WsftpCOMHelper.exe            8176 Console                    1     12,064 K =>WsftpCOMHelper.exe
#chrome.exe                    4648 Console                    1     87,144 K =>chrome.exe
#chrome.exe                    8096 Console                    1     23,452 K =>chrome.exe
#studio64.exe                  4080 Console                    1    289,272 K =>studio64.exe
#fsnotifier64.exe             10188 Console                    1      2,836 K =>fsnotifier64.exe
#conhost.exe                   4764 Console                    1      5,096 K =>conhost.exe
#netbeans64.exe                5400 Console                    1    537,440 K =>netbeans64.exe
#chrome.exe                    6172 Console                    1     94,432 K =>chrome.exe
#chrome.exe                    2368 Console                    1     65,776 K =>chrome.exe
#java.exe                      7064 Console                    1    106,756 K =>java.exe
#chrome.exe                   20576 Console                    1     53,508 K =>chrome.exe
#notepad++.exe                10244 Console                    1     18,272 K =>notepad++.exe
#chrome.exe                     388 Console                    1    114,788 K =>chrome.exe
#chrome.exe                   13596 Console                    1     26,772 K =>chrome.exe
#chrome.exe                   14640 Console                    1     38,508 K =>chrome.exe
#chrome.exe                   14472 Console                    1     66,508 K =>chrome.exe
#chrome.exe                     212 Console                    1    178,640 K =>chrome.exe
#chrome.exe                    6736 Console                    1     74,724 K =>chrome.exe
#chrome.exe                   12968 Console                    1     87,564 K =>chrome.exe
#chrome.exe                    6800 Console                    1     79,912 K =>chrome.exe
#chrome.exe                   16952 Console                    1     90,992 K =>chrome.exe
#chrome.exe                    9380 Console                    1     96,300 K =>chrome.exe
#chrome.exe                    9004 Console                    1     22,512 K =>chrome.exe
#backgroundTaskHost.exe        6556 Console                    1     21,656 K =>backgroundTaskHost.exe
#RuntimeBroker.exe            20584 Console                    1     33,636 K =>RuntimeBroker.exe
#RuntimeBroker.exe            19708 Console                    1      7,116 K =>RuntimeBroker.exe
#java.exe                     11632 Console                    1     18,916 K =>java.exe
#conhost.exe                   6748 Console                    1      9,836 K =>conhost.exe
#tasklist.exe                 11908 Console                    1      7,632 K =>tasklist.exe
#conhost.exe                   4400 Console                    1      9,692 K =>conhost.exe
######################
Lalu dibuat UNIQ
*
*csrss.exe
*winlogon.exe
*fontdrvhost.exe
*dwm.exe
*nvxdsync.exe
*ByteFence.exe
*explorer.exe
*sihost.exe
*rtop_bg.exe
*svchost.exe
*dllhost.exe
*ShellExperienceHost.exe
*RemindersServer.exe
*RuntimeBroker.exe
*ctfmon.exe
*ApplicationFrameHost.exe
*xampp-control.exe
*mysqld.exe
*httpd.exe
*conhost.exe
*LockApp.exe
*SettingSyncHost.exe
*SkypeHost.exe
*SearchUI.exe
*chrome.exe
*WsftpCOMHelper.exe
*studio64.exe
*fsnotifier64.exe
*netbeans64.exe
*java.exe
*notepad++.exe
*backgroundTaskHost.exe
*tasklist.exe

LALU DIRANGKUM:
isitaskList,csrss.exe,winlogon.exe,fontdrvhost.exe,dwm.exe,nvxdsync.exe,ByteFence.exe,explorer.exe,sihost.exe,rtop_bg.exe,svchost.exe,dllhost.exe,ShellExperienceHost.exe,RemindersServer.exe,RuntimeBroker.exe,ctfmon.exe,ApplicationFrameHost.exe,xampp-control.exe,mysqld.exe,httpd.exe,conhost.exe,LockApp.exe,SettingSyncHost.exe,SkypeHost.exe,SearchUI.exe,chrome.exe,WsftpCOMHelper.exe,studio64.exe,fsnotifier64.exe,netbeans64.exe,java.exe,notepad++.exe,backgroundTaskHost.exe,tasklist.exe,

BUILD SUCCESSFUL (total time: 0 seconds)



package currentos;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class CurrentOS {

    public static void main(String[] args) {
        CurrentOS x=new CurrentOS();
        String isitaskList=x.baca();
        System.out.println("isitaskList"+isitaskList);
    }
   
    String baca() {
         String gab="";
        try{
        String line="";
            Process p = Runtime.getRuntime().exec("tasklist.exe /nh");
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
           
            List listA = new ArrayList();
            while ((line = input.readLine()) != null) {
                if(line.indexOf("Services")<0){
                    String[]ar=line.split(" ");
                    String console=ar[0];
                        //System.out.println("#"+line+" =>"+console);
                        listA.add(console);
                }
            }//while
           
        int jd=listA.size();
        //System.out.println("######################");   
        ArrayList<String> unique = removeDuplicates((ArrayList<String>) listA);
     
        for (String element : unique) {
           // System.out.println("*"+element);
            gab+=element+",";
        }
       
        }
        catch(Exception ee){}
           return gab;
    }
   
   
   
   
    static ArrayList<String> removeDuplicates(ArrayList<String> list) {
        ArrayList<String> result = new ArrayList<>();
        HashSet<String> set = new HashSet<>();
        for (String item : list) {
            if (!set.contains(item)) {
                result.add(item);
                set.add(item);
            }
        }
        return result;
    }
}



Java Code LIST ADD


package currentos;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class myList {
   
      public static void main(String[] args) throws IOException {
        myList x=new myList();
        x.mlist();
    }
   
     
  void mlist(){
    List listA = new ArrayList();

    listA.add("element 1");
    listA.add("element 2");
    listA.add("element 3");
    listA.add(2, "element 0");//insert

int size = listA.size();
System.out.println("JUM:"+size);
 System.out.println("+++++++++++++++++++++++++");
 
    //access via index
String element0 = String.valueOf(listA.get(0));
String element1 = String.valueOf(listA.get(1));
String element2 = String.valueOf(listA.get(2));
String element3 = String.valueOf(listA.get(3));

System.out.println("E0L-"+element0);
System.out.println("EL1-"+element1);
System.out.println("EL2-"+element2);
System.out.println("EL3-"+element3);
System.out.println("+++++++++++++++++++++++++");

//access via Iterator
Iterator iterator = listA.iterator();
while(iterator.hasNext()){
  String element = (String) iterator.next();
  System.out.println("IT-"+element);
}
System.out.println("+++++++++++++++++++++++++");



for(Object object : listA) {
    String element = (String) object;
     System.out.println("OBJ-"+element);
}
System.out.println("+++++++++++++++++++++++++");

listA.clear();


  }   
}

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

run:
JUM:4
+++++++++++++++++++++++++
E0L-element 1
EL1-element 2
EL2-element 0
EL3-element 3
+++++++++++++++++++++++++
IT-element 1
IT-element 2
IT-element 0
IT-element 3
+++++++++++++++++++++++++
OBJ-element 1
OBJ-element 2
OBJ-element 0
OBJ-element 3
+++++++++++++++++++++++++
BUILD SUCCESSFUL (total time: 0 seconds)

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;
}


?>




Rabu, 17 Januari 2018

Komunikasi UDP Arduino

hmmm.....seharian dibuat pusing dengan UDP.....ternyata Mudah banget....
jadinya spt ni komunikasinya:



KODE SENT DATA

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


//#define UDP_TX_PACKET_MAX_SIZE 0
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 3,101);

unsigned int localPort = 8888;      // local port to listen on
//char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
char G11='1';
char G10='2';
void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
   Serial.begin(9600);
}

void loop() {
 
     long R=random(1,12); 
  if(R>11){G11='A';}
  else if(R>9){G11='B';}
  else if(R>7){G11='C';}
  else if(R>5){G11='D';}
  else if(R>4){G11='P';}
  else if(R>3){G11='Q';}
  else if(R>2){G11='R';}
  else{G11='S';}
  Serial.println(G11);

    //Udp.parsePacket(); 
    //Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.beginPacket("192.168.3.104", 88888);
    Udp.write(G11);
    Udp.endPacket();
    delay(1000);
    Udp.flush();
}




KODE RECEIVE DATA


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 3, 100);

unsigned int localPort = 8888;     
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; 
EthernetUDP Udp;

void setup() {
  Ethernet.begin(mac, ip);
 
  Udp.begin(localPort);
  Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to conn
 }
 Serial.print("IP  address:");
 Serial.println(Ethernet.localIP());
}

void loop() {
  int packetSize = Udp.parsePacket(); 
  if (packetSize) {
    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println(packetBuffer);
    //Serial.println("^^^^^^^^^");
    //Serial.println(packetSize);
   // Serial.println("+++++++++");
 
  }
  delay(1000);
}


hanya itu?...ya.....tapi ada makna mendalam di dalamnyaaaaaaaaaaaaaa,,,, ,-)