Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #include <ArduinoJson.h>
- #include <Servo.h>
- const char* ssid = "skeletonKing";
- const char* password = "hayumainyuk";
- // ---- api login ----
- const char* user = "ferrynurr";
- const char* passwd = "ferrynurr";
- const char* no_key = "1";
- //------------------------
- // --- ultrasonik ------
- const int trigPin = D6;
- const int echoPin = D5;
- long durasi, jarak;
- // ------- POMPA ----
- const int pompa = D2;
- const int servo_pin = D4;
- const int buzzer = D8;
- // ------ SERVO ---------
- Servo myservo;
- void setup()
- {
- Serial.begin(115200);
- Serial.println();
- WiFi.begin(ssid, password);
- pinMode(buzzer, OUTPUT);
- pinMode(pompa, OUTPUT); // POMPA 12V
- // pinMode(servo_pin, OUTPUT); // POMPA 12V
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- myservo.attach(servo_pin);
- digitalWrite(pompa, HIGH);
- digitalWrite(servo_pin, HIGH);
- Serial.print("Connecting");
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected, IP address: ");
- Serial.println(WiFi.localIP());
- Serial.println("");
- }
- void loop() {
- if(WiFi.status() == WL_CONNECTED)
- {
- delay(1000);
- // ----------------- tanah ---------------
- int yl = analogRead(A0); //pembacaan nilai sensor
- int kalibrasi = map(yl,1023,0,0,100);
- Serial.print("kel tanah : ");
- Serial.println(kalibrasi);
- tanah_post(kalibrasi);
- delay(3000);
- ultrason();
- delay(3000);
- pintu_air();
- delay(3000);
- pompa_isi_parit();
- delay(3000);
- }else{
- Serial.println("error wifi connection"); //Print HTTP return code
- }
- delay(5000);
- delay(5000);
- delay(5000);
- delay(5000);
- }
- void tanah_post(int kalib)
- {
- StaticJsonBuffer<300> JSONbuffer; //Declaring static JSON buffer
- JsonObject& JSONencoder = JSONbuffer.createObject();
- JSONencoder["user"] = user;
- JSONencoder["passwd"] = passwd;
- JSONencoder["no_key"] = no_key;
- JSONencoder["output_sensor"] = kalib;
- JSONencoder["letak"] = "B2";
- char JSONmessageBuffer[300];
- JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
- //Serial.println(JSONmessageBuffer);
- HTTPClient http; //Declare object of class HTTPClient
- http.begin("http://ferrynurr.com/rest_json/kel_tanah");
- http.addHeader("Content-Type", "application/json");
- int httpCode = http.POST(JSONmessageBuffer); //Send the request
- if(httpCode > 0){
- String payload = http.getString(); //Get the response payload
- Serial.println(payload); //Print request response payload
- }else{
- Serial.println(http.errorToString(httpCode).c_str());
- }
- http.end(); //Close connection
- }
- void ultrason()
- {
- // program dibawah ini agar trigger memancarakan suara ultrasonic
- digitalWrite(trigPin, LOW);
- delayMicroseconds(8);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(8);
- digitalWrite(trigPin, LOW);
- delayMicroseconds(8);
- durasi = pulseIn(echoPin, HIGH); // menerima suara ultrasonic
- jarak = (durasi/2) / 29.1; // mengubah durasi menjadi jarak (cm)
- Serial.print("jarak : ");
- Serial.print(jarak);
- Serial.println(" cm");
- if(jarak <= 6){
- digitalWrite(buzzer, HIGH);
- Serial.println("alarm on");
- }else{
- digitalWrite(buzzer, LOW);
- Serial.println("alarm OFF");
- }
- StaticJsonBuffer<300> JSONbuffer; //Declaring static JSON buffer
- JsonObject& JSONencoder = JSONbuffer.createObject();
- JSONencoder["user"] = user;
- JSONencoder["passwd"] = passwd;
- JSONencoder["no_key"] = no_key;
- JSONencoder["output_sensor"] = jarak;
- char JSONmessageBuffer[300];
- JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
- //Serial.println(JSONmessageBuffer);
- HTTPClient http; //Declare object of class HTTPClient
- http.begin("http://ferrynurr.com/rest_json/air_parit");
- http.addHeader("Content-Type", "application/json");
- int httpCode = http.POST(JSONmessageBuffer); //Send the request
- if(httpCode > 0){
- String payload = http.getString(); //Get the response payload
- Serial.println(payload); //Print request response payload
- }else{
- Serial.println(http.errorToString(httpCode).c_str());
- }
- http.end(); //Close connection
- }
- void pintu_air(){
- HTTPClient http;
- int pos ;
- //const char* kondisi = "";
- http.begin("http://ferrynurr.com/rest_json/pintu_air?no_key=1");
- int httpCode = http.GET();
- if (httpCode > 0)
- {
- String json = http.getString(); //Get the request response payload
- const size_t bufferSize = JSON_OBJECT_SIZE(4) + 100;
- DynamicJsonBuffer jsonBuffer(bufferSize);
- JsonArray& root = jsonBuffer.parseArray(json);
- String kon_pintu_air = root[0]["output_sensor"];
- Serial.print("pintu air: ");Serial.println(kon_pintu_air);
- if(kon_pintu_air == "buka"){
- Serial.println("PINTU AIR BUKA");
- myservo.write(90); // tell servo to go to position in variable 'pos'
- }else if(kon_pintu_air == "tutup"){
- Serial.println("PINTU AIR TUTUP");
- //digitalWrite(servo_pin, HIGH);
- myservo.write(0); // tell servo to go to position in variable 'pos'
- }
- }
- http.end(); //Close connection
- }
- void pompa_isi_parit(){
- HTTPClient http;
- //const char* kondisi = "";
- http.begin("http://ferrynurr.com/rest_json/pompa?param=P2&no_key=1");
- int httpCode = http.GET();
- if (httpCode > 0)
- {
- String json = http.getString(); //Get the request response payload
- const size_t bufferSize = JSON_OBJECT_SIZE(4) + 100;
- DynamicJsonBuffer jsonBuffer(bufferSize);
- JsonArray& root = jsonBuffer.parseArray(json);
- String pompa_dua = root[0]["output_sensor"];
- Serial.print("pompa : ");Serial.println(pompa_dua);
- if(pompa_dua == "ON"){
- Serial.println("POMPA 2 ON");
- digitalWrite(pompa, LOW);
- }else if(pompa_dua == "OFF"){
- Serial.println("POMPA 2 OFF");
- digitalWrite(pompa, HIGH);
- }
- }
- http.end(); //Close connection
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement