dimar_hanung

moisture

Feb 16th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1.   #include <string.h>
  2.   #include <Wire.h>
  3.   #include <LiquidCrystal_I2C.h>
  4.   #include <ESP8266WiFi.h>
  5.   #include <UniversalTelegramBot.h>
  6.   #include <WiFiClientSecure.h>
  7.   #include <WiFiUdp.h>
  8.  
  9.   #include <ESP8266HTTPClient.h>
  10. #include <ArduinoJson.h>
  11. #include <string.h>
  12.  
  13.   int jjk=0; //jejak1
  14.   int jjkx=0;//jejak2
  15.  
  16.   // nyambung wifi
  17.   char ssid[] = "MEIDA";  //  SSID wifi
  18.   char password[] = "vitoganteng";  // password
  19.   char wkt[] = "a"; //tidak guna (abaikan)
  20.  
  21.   //tele
  22.   #define BOTtoken "1014035042:AAGqzVBVqQwiCO-E0ykWE1ddns_CG1RWcRc"  // BOT Token di ambil dari bot father (telegram)
  23.  
  24.   WiFiClientSecure client;
  25.   UniversalTelegramBot bot(BOTtoken, client);
  26.  
  27.   int Bot_mtbs = 1000;  //mean time between scan messages
  28.   long Bot_lasttime;  //last time messages' scan has been done
  29.   //tele-end
  30.  
  31.   // variable ultrasonik
  32.   const int trigPin = 2;  //D4
  33.   const int echoPin = 0;  //D3
  34.  
  35.   // defines variables ultrasionik
  36.   long duration;
  37.   int distance;
  38.  
  39.   //variabel soil
  40.   int WET = 16; // Wet Indicator at Digital PIN D0
  41.   int DRY = 2;  // Dry Indicator at Digital PIN D4
  42.  
  43.   //variabel print lcd
  44.   String prin, stringx, sa, sb, sc;
  45.   //variabel soil
  46.   int sense_Pin = 0;  // Soil Sensor input at Analog PIN A0
  47.   int value = 0;
  48.  
  49.   //fungsi i2c
  50.   LiquidCrystal_I2C lcd(0x27, 16, 2);
  51.  
  52.   void setup()
  53.   {
  54.     Serial.begin(9600);
  55.     lcd.begin();
  56.  
  57.     //memulai koneksi
  58.     WiFi.mode(WIFI_STA);
  59.     WiFi.disconnect();
  60.     delay(100);
  61.  
  62.     Serial.print("Connecting Wifi: ");
  63.     Serial.println(ssid);
  64.     WiFi.begin(ssid, password);
  65.  
  66.     while (WiFi.status() != WL_CONNECTED)
  67.     {
  68.       Serial.print(".");
  69.       lcd.setCursor(0, 0);
  70.       lcd.print("Connecting");
  71.       lcd.setCursor(0, 1);
  72.       lcd.print("to wifi..");
  73.       delay(500);
  74.     }
  75.  
  76.     Serial.println("");
  77.     Serial.println("WiFi connected");
  78.     Serial.print("IP address: ");
  79.     Serial.println(WiFi.localIP());
  80.     //mengirim pesan apabila tersambung
  81.     bot.sendMessage("970058254", "Selamat Datang XII TKJ 1 \n Koneksi Moisture", "");
  82.  
  83.     //mendefinisikan sebagai input dan output pin ultrasonnik
  84.     pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  85.     pinMode(echoPin, INPUT);  // Sets the echoPin as an Input
  86.  
  87.     stringx = String();
  88.     pinMode(14, OUTPUT);
  89.     // pinMode(WET, OUTPUT);
  90.     // pinMode(DRY, OUTPUT);
  91.     delay(2000);
  92.   }
  93.  
  94.   void loop()
  95.   {
  96.     //xx
  97.     digitalWrite(trigPin, LOW);
  98.     delayMicroseconds(2);
  99.  
  100.     // Sets the trigPin on HIGH state for 10 micro seconds
  101.     digitalWrite(trigPin, HIGH);
  102.     delayMicroseconds(10);
  103.     digitalWrite(trigPin, LOW);
  104.  
  105.     // Reads the echoPin, returns the sound wave travel time in microseconds
  106.     duration = pulseIn(echoPin, HIGH);
  107.  
  108.     // Calculating the distance
  109.     distance = duration *0.034 / 2;
  110.     // Prints the distance on the Serial Monitor
  111.     //Serial.print("Distance: ");
  112.     Serial.println(distance);
  113.  
  114.     //batas
  115.     Serial.print("MOISTURE LEVEL : ");
  116.     value = analogRead(sense_Pin);
  117.     value = value / 10;
  118.     lcd.setCursor(0, 0);
  119.     lcd.print("Lembab,Air :");
  120.     lcd.setCursor(0, 1);
  121.     sa = "xx";
  122.     sb = ",";
  123.     sc = value;
  124.     stringx = "tes" + value;
  125.     lcd.println(sc + sb + distance);
  126.     Serial.println(prin);
  127.      int harga= distance;
  128.   String h = String(harga);
  129.   String json = "{\"id\":\"71\",\"name\":\"update3\",\"price\":"+h+",\"description\":\"gps\",\"category_id\":\"1\",\"created\":\"gps\"}";
  130.      
  131.   HTTPClient http;
  132. http.begin("http://192.168.100.17/laravel/restapi8/api/product/update.php");
  133. http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  134. http.POST(json);
  135. http.writeToStream(&Serial);
  136. http.end();
  137.  
  138.     if (value < 90)
  139.     {
  140.  
  141.     if(jjk==jjkx){
  142.       jjkx=jjk+1;
  143.        String lembab = (String)
  144.       "Kelembapan rendah\n" + value;
  145.       bot.sendMessage("970058254", lembab, "");
  146.     }
  147.      
  148.      
  149.       digitalWrite(14, HIGH);
  150.     }
  151.     else
  152.     {
  153.       jjk=jjkx;
  154.       digitalWrite(14, LOW);
  155.     }
  156.  
  157.     delay(1500);
  158.     // digitalWrite(WET,LOW);
  159.     // digitalWrite(DRY, LOW);
  160.   }
Add Comment
Please, Sign In to add comment