Advertisement
bangnaga

Prototype Meteran Digital PDAM

Jan 19th, 2017
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.58 KB | None | 0 0
  1. /*
  2.  *Created by : Bang Naga (Ucok Sinaga)
  3.  *Date       : 05 September 2016
  4.  *Email      : emailsinaga@gmail.com
  5.  *FB         : www.facebook.com/nagasakti
  6. */
  7.  
  8. // Pin 3 Sensor Low dengan Sensort Interupt =1 jika sensor pin2 Sensor Interap =0 (Khusus Arduino Uno)
  9. #include <Keypad.h>
  10. #include <EEPROM.h>
  11. #include <LiquidCrystal_I2C.h>
  12. #include <Wire.h>         // this #include still required because the RTClib depends on it
  13. #include "RTClib.h"
  14. #include <Time.h>
  15. #include <TimeLib.h>
  16.  
  17. #include <string.h>
  18. #include <stdlib.h>
  19.  
  20. LiquidCrystal_I2C lcd(0x3F,16,2);
  21. int itgl1, ibln1, ithn1, itgl2, ibln2, ithn2;
  22.  
  23. struct date {
  24.   int dd, mm, yy;
  25.   };
  26.  
  27. RTC_Millis rtc;
  28. String strnovoucher;
  29. String strtglexpire;
  30. String strnomesin;
  31. String strkodepulsa;
  32. String internaltanggal;
  33. String internalnomesin;
  34. String strtgl, strbln, strthn;
  35.  
  36.  
  37. boolean BKode = false;
  38. boolean iVoucher = false;
  39. char novoucher[16]; //6 Digit Tanggal, 4 Digit NoMesin, 4 Digit Pulsa
  40. char tglexpire[6];
  41. char nomesin[4];
  42. char kodepulsa[4];
  43. int validasi1, validasi2;
  44. long tglmemory;
  45.  
  46. int b,i,j,k,l,x,y,z = 1;
  47. String strKode;
  48.  
  49. String kuncireset; //Kode
  50. long EEPNonMesin = 1234; // No Mesin
  51. long EEPMilliLiter = 0; // Total Pemakaian Air - ml
  52. long EEPSaldo = 0; // Sisa Pemakaian Air - ml
  53. int PinTone = 13;
  54. int pinSelenoid    = 12;
  55. byte sensorInterrupt = 0;  
  56. byte sensorPin       = 2;
  57. float calibrationFactor = 4.5;
  58. volatile byte pulseCount;  
  59. float flowRate;
  60. unsigned int flowMilliLitres;                                                        
  61. unsigned long totalMilliLitres;
  62. unsigned long oldTime;
  63.  
  64.  
  65. const byte ROWS = 4;  
  66. const byte COLS = 3;
  67. char keys[ROWS][COLS] = {
  68.   {'1','2','3'},
  69.   {'4','5','6'},
  70.   {'7','8','9'},
  71.   {'*','0','#'}
  72. };
  73.  
  74.  
  75.  
  76. byte rowPins[ROWS] = {9,8,7,6};//{5,4,3,2};
  77. byte colPins[COLS] = {5,4,3};// {9,8,7,6};
  78. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  79.  
  80.  
  81. void setup()
  82. {
  83.  
  84.   lcd.init();                      // initialize the lcd
  85.   lcd.init();
  86.   lcd.backlight();
  87.   lcd.clear();
  88.   lcd.print("Meteran Air");
  89.   lcd.setCursor(0,1);
  90.   lcd.print("PDAM Prabayar");  
  91.   delay(3000);
  92.   internalnomesin = EEPROMReadlong(0);
  93.   //EEPSaldo =0;
  94.   EEPSaldo= EEPROMReadlong(8); //Saldo Milli Liter  
  95.  
  96.   rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
  97.   // Aktifkan jika ingin merubah tanggal
  98.   // rtc.adjust(DateTime(2016, 10, 18, 17, 21, 59));
  99.  
  100.  
  101.   Serial.begin(9600);
  102.   pinMode(PinTone, OUTPUT); //piezo Pin
  103.   pinMode(pinSelenoid, OUTPUT);
  104.  
  105.   digitalWrite(pinSelenoid, LOW);
  106.   delay (1000);
  107.   digitalWrite (pinSelenoid, HIGH); // Tutup Keran
  108.  
  109.   pinMode(sensorPin, INPUT);
  110.   digitalWrite(sensorPin, HIGH);
  111.  
  112.   pulseCount        = 0;
  113.   flowRate          = 0.0;
  114.   flowMilliLitres   = 0;
  115.   totalMilliLitres  = 0;
  116.   oldTime           = 0;
  117.   attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  118. }
  119.  
  120. void loop()
  121.   {
  122.     bacaflow();
  123.     char key = keypad.getKey();
  124.    
  125.     if(key){ //Akhir Baca Keypad
  126.       switch (key)
  127.       {
  128.         case '*':
  129.           BKode == true;
  130.          
  131.           inputVoucher(key);
  132.           bacakode(key);
  133.           iVoucher = true;
  134.           i=0;
  135.           tone(PinTone, 2000, 100);  
  136.           delay(100);
  137.          
  138.           break;
  139.         case '#':
  140.           BKode == false;
  141.           tone(PinTone, 2000, 100);  
  142.           delay(100);
  143.          
  144.          
  145.           Serial.println ("--------- Mode Validasi -------------");
  146.           //--------------- Awal Validasi Voucher
  147.           if (iVoucher == true){
  148.               int b;
  149.               for(int b=0; b<3; b++){
  150.                   Serial.print(novoucher[b]);
  151.                   strKode += novoucher[b];
  152.               }
  153.               Serial.println ("");
  154.               if (strKode == "123") {
  155.                 Serial.println ("RESET SALDO = 0 ");
  156.                 Serial.println ("----------------------------");
  157.                 ResetSaldo();
  158.                 Serial.println ("----------------------------");
  159.                  //kosongkan Array No Voucher
  160.                 int j;
  161.                 for (j=0; j<=16; j++){
  162.                   novoucher[j] = ' ';
  163.                 }
  164.                 strKode = "";
  165.                 strtgl = "";
  166.                 strbln = "";
  167.                 strthn = "";
  168.                 iVoucher = false;
  169.                 break;
  170.               }
  171.  
  172.  
  173.             int dcode ;
  174.             Serial.print ("Decode Chipper:" );
  175.             for (dcode = 0; dcode < 16; dcode++) {
  176.               Serial.print(trans(charToInt(novoucher[dcode])));
  177.               //strnovoucher += novoucher[dcode];
  178.              
  179.             }
  180.  
  181.               Serial.println ("##############################");
  182.               Serial.println (strnovoucher);
  183.               Serial.println ("##############################");
  184.  
  185.            //   int lvoucher;
  186.            //   lvoucher = strnovoucher.length();
  187.            //   for (lv = 0; lv <  lvoucher
  188.      
  189.              
  190.            //   Serial.println (strnovoucher.toInt());
  191.  
  192.  
  193.  
  194.              
  195.               Serial.println ("----------------------------");
  196.               Serial.print ("Voucher : ");
  197.               Serial.println (novoucher);
  198.               Serial.print ("Expire     : " );
  199.               int j;
  200.               strtglexpire = "";
  201.               tglmemory = "";
  202.               for(int j=0; j<8; j++){
  203.                   Serial.print(trans(charToInt(novoucher[j])));
  204.                   strtglexpire += trans(charToInt(novoucher[j]));
  205.                   tglmemory = strtglexpire.toInt();
  206.               }
  207.               Serial.println();
  208.               int x;
  209.               strtgl = "";
  210.               for (int x=0; x<2; x++){
  211.                     strtgl += trans(charToInt(novoucher[x]));
  212.               }
  213.              
  214.               Serial.print ("Tanggal    : ");
  215.               Serial.println(strtgl.toInt());
  216.               itgl2 = strtgl.toInt();
  217.               int y;
  218.               strbln = "";
  219.               for (int y=2; y<4; y++){
  220.                   strbln += trans(charToInt(novoucher[y]));
  221.               }
  222.               Serial.print ("Bulan      : ");
  223.               Serial.println(strbln.toInt());
  224.               ibln2 = strbln.toInt();
  225.          
  226.               int z;
  227.               strthn = "";
  228.               for (int z=4; z<8; z++){
  229.                 strthn += trans(charToInt(novoucher[z]));
  230.               }
  231.               Serial.print ("Tahun      : ");
  232.               Serial.println(strthn.toInt());
  233.               ithn2 = strthn.toInt();
  234.            
  235.               int k;
  236.               Serial.print ("No Mesin   : ");
  237.               strnomesin ="";
  238.               for (int k=8; k<12; k++){
  239.                   Serial.print(trans(charToInt(novoucher[k])));
  240.                  
  241.                   strnomesin += trans(charToInt(novoucher[k]));
  242.               }
  243.               Serial.println();
  244.               strkodepulsa = "";  
  245.               Serial.print ("Pulsa Air  : ");
  246.               for (int l=12; l<16; l++){
  247.                   Serial.print(trans(charToInt(novoucher[l])));
  248.                   strkodepulsa += trans(charToInt(novoucher[l]));
  249.               }
  250.               Serial.println ("");
  251.               internalnomesin = EEPROMReadlong(0);
  252.               if (cektanggal() == 1){
  253.               Serial.println("======== Validasi  No Mesin ==========");    
  254.                   if (strnomesin == internalnomesin) {
  255.                     // Cek Voucher Terakhir
  256.                     int lastNominal = EEPROMReadlong(12);
  257.                     int LastTanggal = EEPROMReadlong(16);
  258.                      
  259.                      //strtglexpire.toInt();
  260.  
  261.                 Serial.println(lastNominal);
  262.                 Serial.println(LastTanggal);
  263.                 Serial.println(strkodepulsa.toInt());
  264.                 Serial.println(strtgl.toInt());
  265.                      
  266.  
  267.                       if ((lastNominal == strkodepulsa.toInt()) && (LastTanggal == strtgl.toInt())){
  268.                           Serial.println ("Nomor Voucher sudah pernah digunakan");
  269.                           lcd.clear();
  270.                           lcd.setCursor(0,0);
  271.                           lcd.print("Voucher Invalid");
  272.                           lcd.setCursor(0,1);
  273.                           lcd.print("Sudah terpakai");    
  274.                           delay(2000);            
  275.                       }else{
  276.                           Serial.println("Tanggal Voucher &  No Mesin valid");
  277.                           Serial.println("UPDATE PULSA METERAN : ");
  278.                           Serial.print(strkodepulsa);
  279.                           long pulsabaru = strkodepulsa.toInt();
  280.                           Serial.print(" x 10 x 1000  ltr = ");
  281.                           TambahSaldo (pulsabaru * 10000); // Milli Liter * 10 <<<<<<<<<<<<<<<<
  282.                           EEPROMWritelong(12,strkodepulsa.toInt()); //Rekam Pulsa Terakhir
  283.                           EEPROMWritelong(16,strtgl.toInt()); //Rekam Pulsa Terakhir
  284.                           Serial.print (pulsabaru * 10000);
  285.                           Serial.println(" ml");
  286.                           Serial.println(" ################################################");
  287.                           validasi1 = 1;
  288.                           lcd.clear();
  289.                           lcd.setCursor(0,0);
  290.                           lcd.print("Voucher Valid");
  291.                           lcd.setCursor(0,1);
  292.                           lcd.print("Stok bertambah");
  293.                           delay(3000);
  294.                       }
  295.  
  296.  
  297.  
  298.  
  299.                    
  300.                  
  301.                   }else{
  302.                     Serial.println("Tanggal Voucher  Valid  tetapi ID Mesin tidak valid");
  303.                     Serial.println (strnomesin);
  304.                     lcd.clear();
  305.                     lcd.setCursor(0,0);
  306.                     lcd.print("Keyword/Voucher");
  307.                     lcd.setCursor(0,1);
  308.                     lcd.print("anda tidak valid");
  309.                     validasi1 = 0;
  310.                   }
  311.               }else{
  312.                 Serial.println("Voucher Expire, Voucher diabaikan");
  313.                 lcd.clear();
  314.                 lcd.setCursor(0,0);
  315.                 lcd.print("Keyword/Voucher");
  316.                 lcd.setCursor(0,1);
  317.                 lcd.print("anda tidak valid");
  318.                 delay(3000);    
  319.               }
  320.            
  321.               Serial.println("=================================================");
  322.           }
  323.      
  324.           //--------------- Akhir Validasi Voucher
  325.  
  326.           //kosongkan Array No Voucher
  327.           int j;
  328.           for (j=0; j<=16; j++){
  329.             novoucher[j] = ' ';
  330.           }
  331.           strtgl = "";
  332.           strbln = "";
  333.           strthn = "";
  334.           strKode = "";
  335.  
  336.          
  337.           iVoucher = false;
  338.    
  339.          
  340.          
  341.           break;
  342.         default:
  343.           tone(PinTone, 2000, 100);  
  344.           delay(100);
  345.           if (iVoucher == true){
  346.             //Serial.print(key);
  347.             novoucher[i] = key;
  348.              lcd.print(key);
  349.             i++;
  350.           }
  351.        
  352.  
  353.       }
  354.     }//-------------------------Akhir Baca Keypad
  355. }
  356.  
  357.  
  358. void bacakode(char key){
  359.   if (BKode == true){
  360.     lcd.print(key);
  361.   }
  362. }
  363.  
  364. void inputVoucher(char key) {
  365.   lcd.clear();
  366.   lcd.setCursor(0, 0);
  367.   lcd.print("Input Token:");
  368.  
  369.   //processNumberKey(key);
  370.   lcd.setCursor(0, 1);
  371.   iVoucher = true;
  372.   lcd.blink();
  373.   delay(1000);
  374.  
  375. }
  376.  
  377. void bacaflow(){
  378.   if((millis() - oldTime) > 1000)  
  379.     {
  380.       detachInterrupt(sensorInterrupt);
  381.       flowRate = ((1000.0 / (millis() - oldTime)) * pulseCount) / calibrationFactor;
  382.       oldTime = millis();
  383.       flowMilliLitres = (flowRate / 60) * 1000;
  384.       totalMilliLitres += flowMilliLitres;
  385.       unsigned int frac;
  386.       frac = (flowRate - int(flowRate)) * 10;
  387.       EEPMilliLiter = totalMilliLitres;
  388.       pulseCount = 0;
  389.       attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
  390.       EEPSaldo = EEPSaldo -  flowMilliLitres;
  391.       TulisMemory (EEPNonMesin, EEPMilliLiter, EEPSaldo);
  392.       BacaMemory();
  393.     }
  394. }
  395.  
  396. void pulseCounter()
  397. {
  398.   pulseCount++;
  399. }
  400.  
  401. int validasivoucher(){  
  402. //validasi tanggal
  403.  
  404.   DateTime now = rtc.now();
  405.   internaltanggal +=  now.day();
  406.   internaltanggal +=  now.month();
  407.   internaltanggal +=  now.year();
  408.   Serial.println (internaltanggal);
  409.   Serial.println (strtglexpire);
  410.  
  411.  
  412.   //validasi nomor mesin;
  413.   Serial.println(strnomesin);
  414.   Serial.println(nomesin);
  415. }
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422. int cektanggal(){
  423.   DateTime now = rtc.now();
  424.   itgl1 = now.day(), DEC;
  425.   ibln1 = now.month(), DEC;
  426.   ithn1 = now.year(), DEC;
  427.   Serial.println ("Tanggal Sekarang: ");
  428.   Serial.println (String(itgl1) + "/" + String(ibln1) + "/" + String(ithn1));
  429.   Serial.println ("------------------------------------------------");
  430.   Serial.print ("Tanggal Voucher: ");
  431.   Serial.println (String(itgl2) + "/" + String(ibln2) + "/" + String(ithn2));
  432.   Serial.println ("------------------------------------------------");
  433.  
  434.   struct date d1 = {itgl1, ibln1, ithn1};
  435.   struct date d2 = {itgl2, ibln2, ithn2};
  436.  
  437.   int cmp = date_cmp(d1, d2);
  438.   if (cmp == 0){
  439.       Serial.println("Tanggal Voucher Expire");
  440.       return  0;}
  441.        
  442.   else if (cmp > 0){
  443.       Serial.println ("Tanggal Voucher Expire");
  444.        return  0;}
  445.      
  446.  
  447.   else {Serial.println("Tanggal Voucher Valid");
  448.       return  1;
  449.   }
  450. }
  451.  
  452. int date_cmp(struct date d1, struct date d2){
  453.     if (d1.dd == d2.dd && d1.mm == d2.mm && d1.yy ==d2.yy)
  454.        return 0;
  455.     else if (d1.yy > d2.yy || d1.yy == d2.yy && d1.mm > d2.mm || d1.yy == d2.yy && d1.mm == d2.mm && d1.dd > d2.dd)
  456.     return 1;
  457.     else return -1;
  458. }
  459.  
  460.  
  461.  
  462.  
  463.  
  464. // --------------------------  Awal Fungsi yang berkaitan dengan Memory
  465. void TulisMemory(long nilai1, long nilai2, long nilai3 ){
  466.   EEPROMWritelong(0, nilai1); //Nomor Mesin
  467.   EEPROMWritelong(4, nilai2); //pemakian
  468.   EEPROMWritelong(8, nilai3); //Saldo Liter    
  469.  
  470. }
  471.  
  472. void BacaMemory(){
  473.   Serial.print("Last Voucher Key: ");
  474.   Serial.println(EEPROMReadlong(12));
  475.   Serial.println(EEPROMReadlong(16));
  476.   Serial.println("************************************************************** ");
  477.  
  478.   Serial.print("Nomor Mesin : ");
  479.   Serial.println(EEPROMReadlong(0));
  480.   Serial.print("Pemakaian   : ");
  481.   Serial.print(EEPROMReadlong(4));
  482.   Serial.println(" ml");        
  483.   Serial.print("Sisa Saldo  : ");
  484.   Serial.print(EEPROMReadlong(8));
  485.   Serial.println(" ml");    
  486.   Serial.print("Sisa Saldo  : ");
  487.   Serial.print(EEPROMReadlong(8)/1000);
  488.   Serial.println(" liter");    
  489.   Serial.println("--------------------------------------------");
  490.   if (iVoucher == false){
  491.     lcd.clear();
  492.     lcd.setCursor(0,0);
  493.     lcd.print("Total:");
  494.     lcd.setCursor(7,0);
  495.     lcd.print(EEPROMReadlong(4));
  496.     lcd.setCursor(14,0);
  497.     lcd.print("mL");
  498.     lcd.setCursor(0,1);
  499.     lcd.print("Sisa :");
  500.     lcd.setCursor(7,1);
  501.     lcd.print(EEPROMReadlong(8)/1000);
  502.     long sisa = EEPROMReadlong(8)/1000;
  503.     if (sisa <=0){
  504.       digitalWrite (pinSelenoid, HIGH); // Tutup Keran
  505.       playTone(1000, 7000);
  506.     }else{
  507.       digitalWrite (pinSelenoid, LOW); // Buka Keran
  508.     }
  509.  
  510.     if (sisa <= 100 && sisa >= 0){
  511.       playTone(300, 160);
  512.     }
  513.  
  514.    
  515.     lcd.setCursor(15,1);
  516.     lcd.print("L");
  517.   }
  518.  
  519.  
  520. }
  521.  
  522.  
  523. void TambahSaldo(long saldoml){
  524.   EEPSaldo =  EEPROMReadlong(8) + saldoml;
  525.   EEPROMWritelong(8,EEPSaldo );
  526.   EEPROMWritelong(4,0); //Reset ke 0 pemakaian  
  527.  
  528.  
  529. }
  530. void ResetSaldo(){
  531.   EEPSaldo =  0;
  532.   EEPROMWritelong(8,EEPSaldo );
  533.   EEPROMWritelong(4,0 );
  534.   lcd.clear();
  535.   lcd.setCursor(0,0);
  536.   lcd.print("Reset meteran");
  537.   lcd.setCursor(0,1);
  538.   lcd.print("berhasil");
  539.   delay(3000);  
  540.  
  541. }
  542. void EEPROMWritelong(int address, long value)
  543. {
  544.   //Write the 4 bytes into the eeprom memory.
  545.   byte four = (value & 0xFF);
  546.   byte three = ((value >> 8) & 0xFF);
  547.   byte two = ((value >> 16) & 0xFF);
  548.   byte one = ((value >> 24) & 0xFF);
  549.   EEPROM.write(address, four);
  550.   EEPROM.write(address + 1, three);
  551.   EEPROM.write(address + 2, two);
  552.   EEPROM.write(address + 3, one);
  553. }
  554.  
  555. long EEPROMReadlong(long address)
  556. {
  557.   long four = EEPROM.read(address);
  558.   long three = EEPROM.read(address + 1);
  559.   long two = EEPROM.read(address + 2);
  560.   long one = EEPROM.read(address + 3);
  561.   return ((four << 0) & 0xFF) + ((three << 8) & 0xFFFF) + ((two << 16) & 0xFFFFFF) + ((one << 24) & 0xFFFFFFFF);
  562. }
  563. // -------------------------- Akhir Fungsi yang berkaitan dengan Memory
  564.  
  565.  
  566.  
  567.  
  568. void playTone(long duration, int freq) {
  569.     duration *= 1000;
  570.     int period = (1.0 / freq) * 1000000;
  571.     long elapsed_time = 0;
  572.     while (elapsed_time < duration) {
  573.         digitalWrite(PinTone,HIGH);
  574.         delayMicroseconds(period / 2);
  575.         digitalWrite(PinTone, LOW);
  576.         delayMicroseconds(period / 2);
  577.         elapsed_time += (period);
  578.     }
  579. }
  580.  
  581.  
  582.  
  583.  
  584.  
  585. int trans(int x2) {
  586. //int x2;
  587. //x2 = chr(x1);
  588.  
  589. Serial.println ("  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  590. //Serial.print (x1);
  591. //Serial.print (" ");
  592. Serial.print (x2);
  593. Serial.print (" ");
  594.  
  595.   int result;
  596.   if (x2 >= 0 && x2 <= 2)
  597.   {
  598.     result = x2 + 7;
  599.     return result;
  600.   } else if (x2 >= 3 && x2 <= 9) {
  601.     result = x2 - 3;
  602.     return result;
  603.   }
  604.  
  605. Serial.print (result);
  606. }
  607.  
  608.  
  609.  
  610.  
  611.  
  612. int charToInt (char ch){ // ubah Charter ke Integer
  613.   return ch-'0';
  614. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement