Rabu, 01 Juli 2020

HOW SENT AND RECEIVE DATA TO IOT

Ceritanya kita akan mengirim data sensot dan menerima feetback dari server dengan kode @4!
nanti tanda 4 aan kita kalibraskan untuk kontrol alat


1.Buat Initialisasi Timer (Misalkan)

unsigned long lastConnectionTime = 0;       
const unsigned long postingInterval = 10*1000;
String gab="",perintah="";
boolean sudah=false;


2.Pada Loop:

void loop(){
  while (client.available()) {
    char c = client.read();
   // Serial.write(c);
    if(c=='!'){sudah=false;}
    if(sudah==true){
        gab=gab+String(c);
      }
    if(c=='@'){sudah=true;}
  }
.
.
..


3.Buktikan jika data dari server diperoleh,,,,
  if(gab.length()>0){ 
    Serial.println("");
    Serial.println("~~~~~~~~~~#"+gab+"#~~~~~~~~~~");
    Serial.println("");
    perintah=gab;
    gab="";
   }
 if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }


btn1 = digitalRead(button1);
  btn2 = digitalRead(button2);
  btn3 = digitalRead(button3);

  if (btn1 == HIGH || perintah=="4") {
    servo.write(0);
    stusia = 1;
     perintah="";
  }
  else if (btn2 == HIGH || perintah=="2") {
    servo.write(90);
    stusia = 2;
    perintah="";
  }
  else if (btn3 == HIGH || perintah=="3") {
    servo.write(100);
    stusia = 3;
    perintah="";
  }


void httpRequest() {
  client.stop();
  if (client.connect(server, 80)) {
    //Serial.println("connecting...");
    url = "/websaya/simpan.php?status=0&id_alat=" + String(IDALAT) + "&sisa_cairan=" + String(ML) + "&detak_jantung=" + String(sensorjantung_value) + "&laju_infus=" + String(TetesPerMenit) + "&catatan=1&ket=ok";
    //Serial.println(url);
    
    client.print(String ("GET " ) + url + " HTTP/1.1\r\n" + "Host: 192.168.0.107\r\n" + "Connection: close\r\n\r\n");    
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    lastConnectionTime = millis();
  } 
}

}//loop



Atau mngkin butuh koding split untuk memilah data nanatinya
String value1 = splitString(gab, '@', 0);
String value2 = splitString(gab, '@', 1);

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]) : "";
}




NB ini adalah pengembanga dari Contoh koding arduino bawaanyaa...


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

// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);
IPAddress myDns(192, 168, 0, 1);

// initialize the library instance:
EthernetClient client;

char server[] = "www.arduino.cc";  // also change the Host line in httpRequest()
//IPAddress server(64,131,82,241);

unsigned long lastConnectionTime = 0;           // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10*1000;  // delay between updates, in milliseconds

void setup() {
  // You can use Ethernet.init(pin) to configure the CS pin
  //Ethernet.init(10);  // Most Arduino shields
  //Ethernet.init(5);   // MKR ETH shield
  //Ethernet.init(0);   // Teensy 2.0
  //Ethernet.init(20);  // Teensy++ 2.0
  //Ethernet.init(15);  // ESP8266 with Adafruit Featherwing Ethernet
  //Ethernet.init(33);  // ESP32 with Adafruit Featherwing Ethernet

  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // start the Ethernet connection:
  Serial.println("Initialize Ethernet with DHCP:");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
      while (true) {
        delay(1); // do nothing, no point running without Ethernet hardware
      }
    }
    if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip, myDns);
    Serial.print("My IP address: ");
    Serial.println(Ethernet.localIP());
  } else {
    Serial.print("  DHCP assigned IP ");
    Serial.println(Ethernet.localIP());
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  if (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  if (client.connect(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP GET request:
    client.println("GET /latest.txt HTTP/1.1");
    client.println("Host: www.arduino.cc");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made:
    lastConnectionTime = millis();
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}








Tidak ada komentar:

Posting Komentar