Untitled
raw download clone
CPP
views 63
,
size 2452 b
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

WiFiClient client;
String request_string;
const char* ssid     = "Referensi Lab";
const char* password = "102engineering";
const char* host = "192.168.88.15";
HTTPClient http;

SoftwareSerial node(D7, D8);

float v1,a1;
void setup() {
  Serial.begin(115200);
  node.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid,password);
  while ((!(WiFi.status() == WL_CONNECTED))){
    delay(300);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
 while (node.available() > 0) {
     for (int i=0; i<260; i++){
    kirimReal();
    }
    kirimDatabase();
  }
}

void bacaSensor(){
    v1 = node.parseFloat();
    a1 = node.parseFloat();
    //Serial.print("data v1:");Serial.println(Volt1);
    //Serial.print("data v2:");Serial.println(Volt2);
    
}

void clientTimeout(){
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
}

void clientConnect(){
  if (!client.connect(host,80)) {
    Serial.println("Gagal Konek");
    return;
  }
}

void kirimDatabase(){
    bacaSensor();
    if (node.read()>0) {
      clientConnect();
      request_string = "/monitoring/add.php?v1=";
      request_string += v1;
      request_string = "&a1=";
      request_string += a1;
      Serial.print("requesting URL: ");
      Serial.println(request_string);
      client.print(String("GET ") + request_string + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");      
      clientTimeout();
      delay(1000);
     }
}
void kirimReal(){
    bacaSensor();
    if (node.read()>0) {
      clientConnect();
      request_string = "/monitoring/realtime.php?v1=";
      request_string += v1;
      request_string = "&a1=";
      request_string += a1;
      Serial.print("requesting URL: ");
      Serial.println(request_string);
      client.print(String("GET ") + request_string + "HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
      clientTimeout();
      delay(1000);
     }
}
close fullscreen
Login or Register to edit or fork this paste. It's free.