#include <WiFi.h>
#include <HTTPClient.h>
#define tr 0
#define detek 2
#define out1 4
#define out2 16
char ssid[] = "Wokwi-GUEST";//Enter your WIFI name
char pass[] = "";
const char* host="webpercobaan.my.id";
const int adc = 34;
float nilai;
float volt;
boolean x;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(tr, OUTPUT);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(detek, INPUT_PULLUP);
WiFi.begin(ssid,pass);
while(WiFi.status()!=WL_CONNECTED)
{Serial.print('.');delay(500);}
Serial.print("Berhasil Konek");
}
void loop()
{
// put your main code here, to run repeatedly:
isi();
baca();
konek();
delay(500);
while((digitalRead(detek)==LOW)||(x==1))
{
HTTPClient httpRelay1;
httpRelay1.begin("http://webpercobaan.my.id/bacarelay1.php");
httpRelay1.GET();
String responseRelay1=httpRelay1.getString();
//Serial.println(responseRelay1);
httpRelay1.end();
x=responseRelay1.toInt();
digitalWrite(out1, HIGH);delay(500);
digitalWrite(out1, LOW);delay(500);
}
}
void isi()
{
/*if((millis()-waktu)==tunda)
{
if(volt<5) {status=!status;}else status=LOW;
digitalWrite(tr, status);waktu=millis();
Serial.println(waktu);Serial. println(volt);
}*/
if(volt<5)
{
digitalWrite(tr,HIGH);
}
else digitalWrite(tr,LOW);
}
void baca()
{
nilai=analogRead(adc);
volt=(nilai*5/4095);
Serial.println(volt);
}
void konek()
{
WiFiClient client;
const int httpPort=80;
if(!client.connect(host, httpPort))
{
Serial.println("Gagal Koneksi Ke Server");
return;
}
Serial.println("Berhasil Koneksi Ke Server");
HTTPClient httpRelay1;
httpRelay1.begin("http://webpercobaan.my.id/bacarelay1.php");
httpRelay1.GET();
//Serial.println(responkode);
String responseRelay1=httpRelay1.getString();
Serial.println(responseRelay1);
httpRelay1.end();
x=responseRelay1.toInt();
HTTPClient http;
http.begin("http://webpercobaan.my.id/kirim.php?volt=" + String(volt));
http.GET();
http.end();
}