Advertisement
ferrynurr

SAC 2

Dec 10th, 2018
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <ESP8266HTTPClient.h>
  3. #include <ArduinoJson.h>
  4. #include <Servo.h>
  5.  
  6. const char* ssid = "skeletonKing";
  7. const char* password = "hayumainyuk";
  8.  
  9. // ---- api login ----
  10.   const char* user = "ferrynurr";
  11.   const char* passwd = "ferrynurr";
  12.   const char* no_key = "1";
  13. //------------------------
  14.  
  15. // --- ultrasonik ------
  16. const int trigPin = D6;
  17. const int echoPin = D5;
  18. long durasi, jarak;
  19.  
  20. // ------- POMPA ----
  21. const int pompa = D2;
  22. const int servo_pin = D4;
  23.  
  24. const int buzzer = D8;
  25.  
  26. // ------ SERVO ---------
  27. Servo myservo;
  28.  
  29. void setup()
  30. {
  31.   Serial.begin(115200);
  32.   Serial.println();
  33.  
  34.   WiFi.begin(ssid, password);
  35.   pinMode(buzzer, OUTPUT);
  36.   pinMode(pompa, OUTPUT);  // POMPA 12V
  37.  // pinMode(servo_pin, OUTPUT);  // POMPA 12V
  38.   pinMode(trigPin, OUTPUT);
  39.   pinMode(echoPin, INPUT);
  40.   myservo.attach(servo_pin);
  41.  
  42.   digitalWrite(pompa, HIGH);
  43.   digitalWrite(servo_pin, HIGH);
  44.  
  45.   Serial.print("Connecting");
  46.   while (WiFi.status() != WL_CONNECTED)
  47.   {
  48.     delay(500);
  49.     Serial.print(".");
  50.    
  51.   }
  52.  
  53.    Serial.println("");
  54.    Serial.print("Connected, IP address: ");
  55.    Serial.println(WiFi.localIP());
  56.    Serial.println("");
  57.  
  58.  
  59. }
  60.  
  61. void loop() {
  62.  if(WiFi.status() == WL_CONNECTED)
  63.  {
  64.    delay(1000);
  65.    
  66.   // ----------------- tanah ---------------
  67.     int yl = analogRead(A0); //pembacaan nilai sensor
  68.     int kalibrasi = map(yl,1023,0,0,100);
  69.     Serial.print("kel tanah : ");  
  70.     Serial.println(kalibrasi);  
  71.     tanah_post(kalibrasi);
  72.     delay(3000);
  73.     ultrason();
  74.     delay(3000);
  75.     pintu_air();
  76.     delay(3000);
  77.     pompa_isi_parit();
  78.      delay(3000);
  79.    
  80.  }else{
  81.   Serial.println("error wifi connection");   //Print HTTP return code
  82.  }
  83.   delay(5000);
  84.   delay(5000);
  85.   delay(5000);
  86.   delay(5000);
  87. }
  88.  
  89. void tanah_post(int kalib)
  90. {
  91.  
  92.     StaticJsonBuffer<300> JSONbuffer;   //Declaring static JSON buffer
  93.     JsonObject& JSONencoder = JSONbuffer.createObject();
  94.     JSONencoder["user"] = user;
  95.     JSONencoder["passwd"] = passwd;
  96.     JSONencoder["no_key"] = no_key;
  97.     JSONencoder["output_sensor"] = kalib;
  98.     JSONencoder["letak"] = "B2";
  99.    
  100.     char JSONmessageBuffer[300];
  101.     JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  102.     //Serial.println(JSONmessageBuffer);
  103.  
  104.     HTTPClient http;    //Declare object of class HTTPClient
  105.     http.begin("http://ferrynurr.com/rest_json/kel_tanah");
  106.     http.addHeader("Content-Type", "application/json");
  107.    
  108.     int httpCode = http.POST(JSONmessageBuffer);   //Send the request
  109.     if(httpCode > 0){
  110.        String payload = http.getString();                                        //Get the response payload
  111.       Serial.println(payload);    //Print request response payload
  112.     }else{
  113.        Serial.println(http.errorToString(httpCode).c_str());
  114.     }
  115.  
  116.     http.end();  //Close connection
  117.  
  118. }
  119.  
  120.  
  121. void ultrason()
  122. {
  123.   // program dibawah ini agar trigger memancarakan suara ultrasonic
  124.   digitalWrite(trigPin, LOW);
  125.   delayMicroseconds(8);
  126.   digitalWrite(trigPin, HIGH);
  127.   delayMicroseconds(8);
  128.   digitalWrite(trigPin, LOW);
  129.   delayMicroseconds(8);
  130.    
  131.   durasi = pulseIn(echoPin, HIGH);  // menerima suara ultrasonic
  132.   jarak = (durasi/2) / 29.1;     // mengubah durasi menjadi jarak (cm)
  133.  
  134.  
  135.   Serial.print("jarak : ");
  136.   Serial.print(jarak);
  137.   Serial.println(" cm");
  138.  
  139.   if(jarak <= 6){
  140.  
  141.        digitalWrite(buzzer, HIGH);
  142.        Serial.println("alarm on");
  143.    
  144.   }else{
  145.      
  146.        digitalWrite(buzzer, LOW);
  147.        Serial.println("alarm OFF");
  148.    
  149.   }
  150.  
  151.   StaticJsonBuffer<300> JSONbuffer;   //Declaring static JSON buffer
  152.   JsonObject& JSONencoder = JSONbuffer.createObject();
  153.   JSONencoder["user"] = user;
  154.   JSONencoder["passwd"] = passwd;
  155.   JSONencoder["no_key"] = no_key;
  156.   JSONencoder["output_sensor"] = jarak;
  157.    
  158.     char JSONmessageBuffer[300];
  159.     JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
  160.     //Serial.println(JSONmessageBuffer);
  161.  
  162.     HTTPClient http;    //Declare object of class HTTPClient
  163.     http.begin("http://ferrynurr.com/rest_json/air_parit");
  164.     http.addHeader("Content-Type", "application/json");
  165.    
  166.     int httpCode = http.POST(JSONmessageBuffer);   //Send the request
  167.     if(httpCode > 0){
  168.        String payload = http.getString();                                        //Get the response payload
  169.       Serial.println(payload);    //Print request response payload
  170.     }else{
  171.        Serial.println(http.errorToString(httpCode).c_str());
  172.     }
  173.  
  174.     http.end();  //Close connection
  175.  
  176. }
  177.  
  178. void pintu_air(){
  179.    HTTPClient http;
  180.    int pos ;
  181.    //const char* kondisi = "";
  182.    http.begin("http://ferrynurr.com/rest_json/pintu_air?no_key=1");
  183.    int httpCode = http.GET();              
  184.     if (httpCode > 0)
  185.     {
  186.         String json  = http.getString();                      //Get the request response payload
  187.         const size_t bufferSize = JSON_OBJECT_SIZE(4) + 100;
  188.         DynamicJsonBuffer jsonBuffer(bufferSize);
  189.         JsonArray& root = jsonBuffer.parseArray(json);
  190.         String kon_pintu_air = root[0]["output_sensor"];    
  191.         Serial.print("pintu air: ");Serial.println(kon_pintu_air);
  192.          
  193.         if(kon_pintu_air == "buka"){
  194.            Serial.println("PINTU AIR BUKA");
  195.          
  196.                 myservo.write(90);              // tell servo to go to position in variable 'pos'
  197.                
  198.         }else if(kon_pintu_air == "tutup"){
  199.            Serial.println("PINTU AIR TUTUP");
  200.             //digitalWrite(servo_pin, HIGH);
  201.            
  202.               myservo.write(0);              // tell servo to go to position in variable 'pos'
  203.          
  204.         }
  205.     }
  206.    http.end();   //Close connection
  207. }
  208.  
  209. void pompa_isi_parit(){
  210.  
  211.    HTTPClient http;
  212.    //const char* kondisi = "";
  213.    http.begin("http://ferrynurr.com/rest_json/pompa?param=P2&no_key=1");
  214.    int httpCode = http.GET();              
  215.     if (httpCode > 0)
  216.     {
  217.         String json  = http.getString();                      //Get the request response payload
  218.         const size_t bufferSize = JSON_OBJECT_SIZE(4) + 100;
  219.         DynamicJsonBuffer jsonBuffer(bufferSize);
  220.         JsonArray& root = jsonBuffer.parseArray(json);
  221.         String pompa_dua = root[0]["output_sensor"];    
  222.         Serial.print("pompa : ");Serial.println(pompa_dua);
  223.  
  224.         if(pompa_dua == "ON"){
  225.            Serial.println("POMPA 2 ON");  
  226.           digitalWrite(pompa, LOW);
  227.      
  228.         }else if(pompa_dua == "OFF"){
  229.            Serial.println("POMPA 2 OFF");
  230.            digitalWrite(pompa, HIGH);
  231.                
  232.        
  233.         }
  234.     }
  235.  http.end();   //Close connection
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement