Advertisement
galang_pratama

monitoring.ino

Nov 12th, 2018
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. SoftwareSerial wifi (10,11); // Rx Tx
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5.  
  6. #define SensorPin A2            //pH meter Analog output to Arduino Analog Input 0 A2
  7. #define SensorSoilMeasure A0  //soil measure sensor A0
  8. #define ONE_WIRE_BUS A1  // sensor suhu
  9.  
  10. #define nama_wifi "SSID_WIFI" //ganti dengan ssid wifi kamu
  11. #define pass_wifi "PASSWORDNYA" //ganti dengan password wifi kamu
  12. #define ip_host "192.168.43.207"  // ip computer kamu, bisa liat di CMD (ipconfig)
  13.  
  14. int sensorPin = A0;
  15. int nilai_sensor;
  16. boolean connected = false;
  17.  
  18. OneWire oneWire(ONE_WIRE_BUS);
  19. DallasTemperature sensorSuhu(&oneWire);
  20.  
  21. float suhuSekarang;
  22. float ph;
  23. int sensorValue = 0;        //ADC value from sensor
  24. float outputValue = 0.0;        //pH value after conversion
  25. int SoilMeasture;
  26.  
  27. void setup() {
  28.   // put your setup code here, to run once:
  29. wifi.begin(115200);
  30. Serial.begin(9600);
  31. wifi.setTimeout(5000);
  32. Serial.println("ESP8266 cek cek");
  33. delay (1000);
  34. wifi.println("AT+RST");
  35. delay(1000);
  36. if(wifi.find("WIFI GOT IP"))
  37. {
  38.   Serial.println(" ESP8266 SIAP ");
  39. }
  40. else {
  41.   Serial.println(" Tidak Ada Response dari ESP8266 ");
  42.   while(1);
  43. }
  44. delay(1000);
  45.  
  46. for (int i=0; i<5; i++){
  47.   connect_to_wifi();
  48.   if (connected){
  49.     break;
  50.   }
  51. }
  52.   if (!connected){
  53.     while(1);
  54.   }
  55.   delay(5000);
  56.   wifi.println("AT+CIPMUX=0");
  57.   delay(1000);
  58.  
  59.    // Serial.begin(9600);
  60.   sensorSuhu.begin();
  61. }
  62.  
  63. void loop() {
  64.  
  65.    suhuSekarang = ambilSuhu();
  66.   SoilMeasture = ambilSoilMeasture();
  67.   ph = hasil();
  68.   String tempSuhu = String(suhuSekarang);
  69.   String tempSoil = String(SoilMeasture);
  70.   String tempPh = String(ph);
  71.  
  72.   // put your main code here, to run repeatedly:
  73. String cmd = "AT+CIPSTART=\"TCP\",\"";
  74. cmd+= ip_host;
  75. cmd+="\",80";
  76. wifi.println(cmd);
  77. Serial.println(cmd);
  78. if (wifi.find("Error")){
  79.   Serial.println("Koneksi eror");
  80.   return;
  81. }
  82. nilai_sensor = analogRead(sensorPin);
  83. cmd = "GET /monitoring/insert_data.php?suhu=";
  84. cmd+=tempSuhu;
  85. cmd+="&humidity=";
  86. cmd+=tempSoil;
  87. cmd+="&ph_tanah=";
  88. cmd+=tempPh;
  89. cmd+="\r\n";
  90. cmd+="HTTP/1.0/1/\r\n";
  91. cmd+="\r\n";
  92.  
  93. wifi.print("AT+CIPSEND=");
  94. wifi.println(cmd.length());
  95. if (wifi.find(">")){
  96.   Serial.print(">");
  97. } else {
  98.   wifi.println("AT+CIPCLOSE");
  99.   Serial.println("Koneksi Timeout");
  100.   delay(1000);
  101.   return;
  102. }
  103. wifi.print(cmd);
  104. delay(2000);
  105.  
  106. while(wifi.available())
  107. {
  108.   char c =wifi.read();
  109.   Serial.write(c);
  110.   if (c=='\r') Serial.print('\n');
  111. }
  112. Serial.println("-----end");
  113. delay(10000);
  114. }
  115.  
  116. void connect_to_wifi()
  117. {
  118.   wifi.println("AT+CWMODE=1");
  119.   String cmd = "AT+CWJAP=\"";
  120.   cmd+=nama_wifi;
  121.   cmd+="\",\"";
  122.   cmd+=pass_wifi;
  123.   cmd+="\"";
  124.   wifi.println(cmd);
  125.   Serial.println(cmd);
  126.   if (wifi.find("OK")){
  127.     Serial.println("Berhasil Terkoneksi ke internet");
  128.   connected=true;
  129.   } else {
  130.     Serial.println("Gagal Terkoneksi");
  131.   connected=false;
  132.   }
  133.  
  134. }
  135.  
  136. float ambilSuhu(){
  137.    sensorSuhu.requestTemperatures();
  138.    float suhu = sensorSuhu.getTempCByIndex(0);
  139.    return suhu;  
  140.    delay(100);
  141. }
  142.  
  143. int ambilSoilMeasture(){
  144.    int measture = analogRead(A0);
  145.    return measture;  
  146.    delay(100);
  147. }
  148.  
  149. float hasil(){
  150.   sensorValue = analogRead(SensorPin);
  151.  
  152.   //Mathematical conversion from ADC to pH
  153.   //rumus didapat be  rdasarkan datasheet
  154.   outputValue = (-0.0693*sensorValue)+7.3855;
  155.  
  156.   return outputValue;
  157.   delay(100);
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement