bangnaga

Arduino: SMS Sensor Curah Hujan & Ketinggian Air

Dec 1st, 2014
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ============================================
  2. SMS Sensor Curah Hujan & Ketinggian Air
  3. Sungguminasa, 1 Desember 2014
  4. www.facebook.com/nagasakti
  5. email: emailsinaga@gmail.com
  6. ----------------------------------------------
  7. Ketinggian Air
  8. Normal = <50
  9. Siaga1 = 51-74
  10. Siaga2 = 75-100
  11. Siaga3 = 100-150
  12. Siaga4 = >150
  13.  
  14.  
  15. Pin Sensor
  16. --------------------
  17. Ultrasonic = Digital 4
  18. Rain       = Digital 9
  19. Pinout Sensor Curah Hujan (Rain Guage : http://www.skyview.co.uk/acatalog/m_CM2016_RG.gif)
  20. Red  --------------------------- GND
  21.  
  22.          |--------------------- Digital 9  
  23.          |  R = 10 K ohm  
  24. Green ----/\/\/\/\------------- VCC 5 Volt
  25. ============================================ */
  26.  
  27. //Rain variable
  28. int pulsePin =9; // Pin Digital 9 (Sensor Curah Hujan)
  29. unsigned long counter = 0;
  30. unsigned long duration = 0;
  31. unsigned long timeout = 1000000; // in microseconds
  32.  
  33. // Sensor PING
  34. const int pingPin = 7;
  35. const int hmax = 5; //5 cm
  36. const int tinggisensor = 160;  // Ketinggian Sensor dari Permukaan
  37. int ketinggian;
  38.  
  39.  
  40. #include "SIM900.h"
  41. #include <SoftwareSerial.h>
  42. #include "sms.h"
  43. SMSGSM sms;
  44. char number[]="+6285242493184"; //Nomor Handphone SMS Center (Server)
  45. char number2[]="+625255544442"; //Nomor Handphone Petugas (Siaga4)
  46. int sensor1=0;
  47. int sensor2=0;
  48. char value_str[4]; // 4 Digit Integer to Str
  49. char ket_str[20];
  50. char message[40];
  51. String keterangan;
  52.  
  53.  
  54. void setup()
  55. {
  56.  Serial.begin(9600);
  57.  if (gsm.begin(2400))
  58.    Serial.println("\nstatus=READY");
  59.  else Serial.println("\nstatus=IDLE");
  60. };
  61.  
  62. void loop()
  63. {
  64. sensor1 = bacaketinggian();
  65. sensor2 = bacacurahhujan();
  66.  
  67. //Cek Kondisi Ketinggian Air
  68.  
  69. //Normal = <50
  70. //Siaga1 = 51-74
  71. //Siaga2 = 75-100
  72. //Siaga3 = 100-150
  73. //Siaga4 = >150
  74. if (ketinggian<50) {
  75.    keterangan= "*Tidak Banjir*";
  76. }
  77. else
  78. if (ketinggian>51 && ketinggian<=74) {
  79.     //keterangan = ", Siaga 1";
  80.     keterangan = "*Siaga 1*";    
  81. }
  82. else
  83. if (ketinggian>75 && ketinggian<=100) {
  84.     //keterangan = ", Siaga 2";
  85.     keterangan = "*Siaga 2*";    
  86. }
  87. else
  88. if (ketinggian>100 && ketinggian<=150) {
  89.     //keterangan = ", Siaga 3";}
  90.     keterangan = "*Siaga 3*";}    
  91. else
  92. if (ketinggian >150) {
  93.     //keterangan = ", Siaga 4";
  94.     keterangan = "*Siaga 4*";
  95.    
  96.    
  97. }
  98. else
  99. {
  100.  //keterangan = ", Tidak Terdefenisikan";
  101.   keterangan = "*Tidak Terdefenisikan*";
  102. };
  103. Serial.println(keterangan);
  104.  
  105. //Kirim SMS
  106.  kirimsms(keterangan);
  107.  
  108. };
  109.  
  110.  
  111. void kirimsms(String keterangan)
  112. {
  113.  message[0]='\0';
  114.    //strcat(message,"Curah Hujan: ");
  115.    strcat(message,"*D01*");
  116.    itoa(counter*.2794,value_str,10);
  117.    strcat(message,value_str);
  118.    
  119.    //strcat(message,", Ketinggian Air: ");
  120.    strcat(message,"*");  
  121.    itoa(ketinggian,value_str,10);
  122.    strcat(message,value_str);
  123.    
  124.    keterangan.toCharArray(ket_str, 20);
  125.  
  126.    strcat(message, ket_str);
  127.    sms.SendSMS(number,message);
  128.    if (keterangan ="Siaga 4*") {
  129.      sms.SendSMS(number2,message);
  130.    }
  131.    delay(120000); //Delay Pengiriman SMS
  132.  }
  133.  
  134. int bacaketinggian(){
  135.   //Baca Ketinggian Air Asusmsi ketinggian sensor ke permukaan 160 cm
  136.   int result;
  137.   long duration, cm;
  138.   pinMode(pingPin, OUTPUT);
  139.   digitalWrite(pingPin, LOW);
  140.   delayMicroseconds(2);
  141.   digitalWrite(pingPin, HIGH);
  142.   delayMicroseconds(5);
  143.   digitalWrite(pingPin, LOW);
  144.   pinMode(pingPin, INPUT);
  145.   duration = pulseIn(pingPin, HIGH);
  146.   cm = mmtocm(duration);
  147.   ketinggian = tinggisensor - cm;
  148.   result = ketinggian;
  149.   Serial.print("  Ketinggian: ");
  150.   Serial.println(ketinggian);
  151. //  delay (500);
  152. //===END Ketinggian Air
  153. }  
  154.  
  155. long mmtocm(long microseconds)
  156. {
  157.   return microseconds / 29 / 2;
  158. }
  159.  
  160.  
  161.  int bacacurahhujan(){
  162.   //Baca Curah Hujan
  163.  duration = pulseIn(pulsePin, HIGH, timeout);
  164.  if (duration == 0) {
  165.    Serial.print("Mulai Menghitung Curah Hujan.");
  166.    Serial.println("");
  167.  } else {
  168.    counter++;
  169.    //Serial.print(counter);
  170.   // Serial.print(", ");
  171.    Serial.print(duration*.000001);
  172.    Serial.println(" s");
  173.    //Serial.println("");
  174.    Serial.print (counter*.2794);
  175.    Serial.println(" mm");
  176. }
  177. //== END Rain
  178. }
RAW Paste Data