Wimos

Dari widuri
Revisi per 29 Agustus 2019 14.52 oleh Yudhariz (bicara | kontrib) (←Membuat halaman berisi '<pre> #include <GDBStub.h> #include <ESP8266WiFi.h> #include <ESP8266WiFiMulti.h> #include <ESP8266HTTPClient.h> #include <WiFiClient.h> ESP8266WiFiMulti WiFiMulti;...')

(beda) ← Revisi sebelumnya | Revisi terkini (beda) | Revisi selanjutnya → (beda)

Lompat ke: navigasi, cari
#include <GDBStub.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
ESP8266WiFiMulti WiFiMulti;
boolean connectionWasAlive = true;
char ssid[] = "ISTANA_BONEKA";
char pass[] = "terminal";

#include <Arduino.h>
#include <ArduinoJson.h>

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int data;
int kmSekarang;
int kmAkhir;
int kmService;
int serviceTime = 5000;
String datas;
String url;
String payload;
String payload2;
String dataIsi;
bool pertama = true;
const char *host = "http://reminder.96.lt/ambilData.php";

void setup()
{
  {
    Wire.begin(D4,D3);
    Serial.begin(115200);
    pinMode(A0,INPUT);
    lcd.init();
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print("Reminder Oli SM");
    lcd.setCursor(1,0);
    lcd.print("");
    delay(500);
    }
  {
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.print("Menghubungkan WiFi = ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
    }
  Serial.println("");
  Serial.println("WiFi Terhubung");
  Serial.println("=============================================");
  }
  {
    Serial.println("Mengambil Data Dari Server");
    {
      url = "http://reminder.96.lt/ambilData.php";
      Serial.println(url);
      }
      HTTPClient http;
      Serial.print("[HTTP] begin...\n");
      http.begin(url);
      Serial.print("[HTTP] GET...\n");
      int httpCode = http.GET();
      if (httpCode > 0)
      {
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);
        if (httpCode == HTTP_CODE_OK)
        {
          const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 88;
          DynamicJsonBuffer jsonBuffer(capacity);
          payload2 = http.getString();
          JsonObject& root = jsonBuffer.parseObject(payload2);
          if (!root.success())
          {
            Serial.println(F("Parsing failed!"));
            return;
            }
            Serial.print("KM Awal = ");
            Serial.println(root["data"][0]["<br>kmAwal"].as<char*>());
            Serial.println("=============================================");
            Serial.println();
          }
          else
          {
            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
            }
        }
        http.end();
  }
}

void loop()
{
  {
    int dataCetak = ambildataSensor();
    Serial.flush();
    kmSekarang =  dataCetak;
    kmService += kmSekarang;
    Serial.println(kmService);
    if(kmService > serviceTime)
    {
      Serial.println("Waktunya Service");
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Service Time");
      serviceTime += 5000;
      lcd.clear();
      lcd.print("Service:");
      lcd.setCursor(10,0);
      lcd.print(serviceTime);
      }
      else
      {
        lcd.setCursor(0,1);
        lcd.print("Km:");
        lcd.setCursor(4,1);
        lcd.print(kmService);
        }
        datas = String(ambildataSensor());
        tulisDataSdCard(datas);
        delay(1000);
        httpGet();
        delay(1000); 
   }
}

void httpGet()
{
  {
    Serial.println("Mengirim Data Ke Server");
    String noPol= "b2525via";
    int dataSensor = ambildataSensor();
    if (pertama)
    {
      url = "http://reminder.96.lt/pertama.php?no=b2525via";
      pertama = false;
      Serial.println(url);
      }
      else
      {
      url = "http://reminder.96.lt/SETTER2.php?km="+String(kmService);
      Serial.println (url);
      String urlPlat = "&no="+noPol;
      }
      HTTPClient http;
      Serial.print("[HTTP] begin...\n");
      http.begin(url);
      Serial.print("[HTTP] GET...\n");
      int httpCode = http.GET();
      if (httpCode > 0)
      {
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);
        if (httpCode == HTTP_CODE_OK)
        {
          payload = http.getString();
          Serial.println(payload);
          Serial.println("=============================================");
          Serial.println();
          }
          else
          {
            Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
            }
        }
        http.end();
     }
     delay(1000);
}

int ambildataSensor()
{
  Serial.println("Mengambil Data Sensor Jarak Tempuh/Jam");
  int dataKmRata2 = 0;
  for(int i = 0 ;i < 100;i++)
  {
    int data = analogRead(A0);
    int dataDalamKm = map(data,100,1024,0,120);
    dataKmRata2 += dataDalamKm;
    delay(10);
    }
    if(dataKmRata2/100 <= 0 || dataKmRata2/100 >= 120)
    {
      Serial.println("Rata-Rata Data Sensor = 0 ");
      Serial.println("=============================================");
      return 0;
      }
      else
      {
        Serial.println("Rata-Rata Data Sensor = "+String(dataKmRata2/100));
        Serial.println("=============================================");
        return dataKmRata2/100+200;
        }
        delay(1000);
}

Contributors

Yudhariz