Advertisement
dimar_hanung

file Arduino Moisture Request

Feb 17th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1.   #include <string.h>
  2.   #include <Wire.h>
  3.   #include <LiquidCrystal_I2C.h>
  4.   #include <ESP8266WiFi.h>
  5.   #include <WiFiClientSecure.h>
  6.   #include <WiFiUdp.h>
  7.  
  8. #include <ESP8266HTTPClient.h>
  9. #include <ArduinoJson.h>
  10. #include <string.h>
  11.  
  12.   // nyambung wifi
  13.   char ssid[] = "MEIDA";  //  SSID WIFI
  14.   char password[] = "vitoganteng";  // password WIFI
  15.  
  16.   // variable ultrasonik
  17.   const int trigPin = 2;  //D4
  18.   const int echoPin = 0;  //D3
  19.   const int pompa   = 14;
  20.  
  21.   // defines variables ultrasionik
  22.   long duration;
  23.   int distance;
  24.  
  25.   //variabel soil
  26.   int sense_Pin = 0;  // Soil Sensor input at Analog PIN A0
  27.   int value = 0;
  28.   String pstatus;
  29.   //fungsi i2c
  30.   LiquidCrystal_I2C lcd(0x27, 16, 2);
  31.  
  32.   void setup()
  33.   {
  34.     Serial.begin(9600);
  35.     lcd.begin();
  36.  
  37.     //memulai koneksi
  38.     WiFi.mode(WIFI_STA);
  39.     WiFi.disconnect();
  40.     delay(100);
  41.  
  42.     Serial.print("Connecting Wifi: ");
  43.     Serial.println(ssid);
  44.     WiFi.begin(ssid, password);
  45.  
  46.     //selama wifi tidak tersambung menjalankan perintah while ini
  47.     while (WiFi.status() != WL_CONNECTED)
  48.     {
  49.       Serial.print(".");
  50.       lcd.setCursor(0, 0);
  51.       lcd.print("Menyambung");
  52.       lcd.setCursor(0, 1);
  53.       lcd.print("ke wifi..");
  54.       delay(500);
  55.     }
  56.  
  57.     //ketika sudah tersambung
  58.     Serial.println("");
  59.     Serial.println("WiFi tersambung");
  60.     Serial.print("IP address: ");
  61.     Serial.println(WiFi.localIP());
  62.  
  63.     //mendefinisikan sebagai input dan output pin ultrasonnik
  64.     pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  65.     pinMode(echoPin, INPUT);  // Sets the echoPin as an Input
  66.  
  67.     pinMode(pompa, OUTPUT);
  68.     delay(2000);
  69.   }
  70.  
  71.   void loop()
  72.   {
  73.     //xx
  74.     digitalWrite(trigPin, LOW);
  75.     delayMicroseconds(2);
  76.  
  77.     // Sets the trigPin on HIGH state for 10 micro seconds
  78.     digitalWrite(trigPin, HIGH);
  79.     delayMicroseconds(10);
  80.     digitalWrite(trigPin, LOW);
  81.  
  82.     // Reads the echoPin, returns the sound wave travel time in microseconds
  83.     duration = pulseIn(echoPin, HIGH);
  84.  
  85.     // Calculating the distance
  86.     distance = duration *0.034 / 2;
  87.     // Prints the distance on the Serial Monitor
  88.     Serial.println(distance);
  89.    
  90.     //batas
  91.     Serial.print("MOISTURE LEVEL : ");
  92.     value = analogRead(sense_Pin);
  93.     value = value / 10;
  94.  
  95.    
  96.     lcd.setCursor(0, 0);
  97.     lcd.print("Lembab,Air :");
  98.     lcd.setCursor(0, 1);
  99.     lcd.println(String(value) + "," + String(distance));
  100.  
  101. //    if(distance<5){
  102. //      digitalWrite(pompa, HIGH);
  103. //    }else
  104. //    {
  105. //      digitalWrite(pompa, LOW);
  106. //    }
  107.  
  108.  
  109.     HTTPClient http;
  110.   http.begin("http://ec2-18-208-181-162.compute-1.amazonaws.com/restapi8/api/product/read_one.php?id=60");
  111.   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  112.   int httpCode = http.GET();
  113.   String payload = http.getString();
  114.   http.writeToStream(&Serial);
  115.     if(httpCode == 200)
  116.   {
  117.     // Allocate JsonBuffer
  118.     // Use arduinojson.org/assistant to compute the capacity.
  119.     const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
  120.     DynamicJsonBuffer jsonBuffer(capacity);
  121.  
  122.    // Parse JSON object
  123.     JsonObject& root = jsonBuffer.parseObject(payload);
  124.     if (!root.success()) {
  125.       Serial.println(F("Parsing failed!"));
  126.       return;
  127.     }
  128.  
  129.     // Decode JSON/Extract values
  130.     Serial.println(F("Response:"));
  131.  
  132.     pstatus = root["name"].as<char*>();
  133.    
  134.     Serial.println(pstatus);
  135.     Serial.println(root["price"].as<char*>());
  136.  
  137.   }else
  138.   {
  139.     Serial.println("Error in response");
  140.   }
  141.  
  142.   http.end();
  143.  
  144.   if(pstatus=="LOW"){
  145.   digitalWrite(pompa, LOW);
  146.   }else{
  147.     digitalWrite(pompa, HIGH);
  148.   }
  149.     String json = "{\"id\":\"60\",\"name\":"+String(pstatus)+",\"price\":"+String(distance)+",\"description\":\"gps\",\"category_id\":\"1\",\"created\":\"gps\"}";
  150.      
  151.  
  152.   http.begin("http://ec2-18-208-181-162.compute-1.amazonaws.com/restapi8/api/product/update.php");
  153.   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  154.   http.POST(json);
  155.   http.writeToStream(&Serial);
  156.   http.end();
  157.  
  158.  
  159.  
  160.     delay(1500);
  161.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement