bangnaga

Arduino GPRS

Oct 2nd, 2013 (edited)
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.60 KB | None | 0 0
  1. volatile int NbTopsFan; //measuring the rising edges of the signal
  2.   int Calc;                              
  3.   int hallsensor = 2;    //The pin location of the sensor
  4.   int total = 0;
  5.   int temp=0;
  6.   int counter =0;
  7.   int hitung = 0;
  8.   char buffer_kirim[100];
  9.   char url[]="www.cobaserver.hosting.com";
  10.  
  11.  
  12. int8_t answer, respond;
  13. byte gsmDriverPin[3] = {
  14.   3,4,5};
  15.  
  16. void rpm ()     //This is the function that the interupt calls
  17. {
  18.   NbTopsFan++;  //This function measures the rising and falling edge of the hall effect sensors signal
  19. }
  20.  
  21. void setup()
  22. {
  23.     for(int i = 0 ; i < 3; i++){
  24.       pinMode(gsmDriverPin[i],OUTPUT);
  25.     }
  26.     //Output GSM Timing
  27.     digitalWrite(5,HIGH);
  28.     delay(1500);
  29.     digitalWrite(5,LOW);
  30.     delay(2000);
  31.     pinMode(hallsensor, INPUT);
  32.     Serial.begin(9600); //set the baud rate
  33.     delay(5000);
  34.     attachInterrupt(0, rpm, RISING);
  35.     delay(5000);
  36.  
  37.     Serial.println("Starting...");
  38.     power_on();
  39.     delay(3000);
  40.     sendATcommand("AT+CPIN=****", "OK", 2000);
  41.     delay(3000);
  42.    
  43.     /*Konfigurasi APN*/
  44.     while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);
  45.     sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
  46.     sendATcommand("AT+SAPBR=3,1,\"APN\",\"indosatgprs\"", "OK", 2000);
  47.     sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000);
  48.     sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000);
  49.      
  50.     if ((sendATcommand("AT+SAPBR=1,1", "OK", 10000)) == 0)
  51.     Serial.println ("Waiting...");
  52. }
  53.  
  54. void loop()
  55. {
  56.   /*Fungsi mengambil data dari sensor flowemeter*/
  57.   NbTopsFan = 0;   //Set NbTops to 0 ready for calculations
  58.   sei();      //Enables interrupts
  59.   delay (1000);   //Wait 1 second
  60.   cli();      //Disable interrupts
  61.   Calc = (NbTopsFan / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
  62.  
  63.     Serial.print("Counter : ");
  64.     Serial.println(counter);
  65.     Serial.print(Calc);
  66.     Serial.println(" L/min");
  67.  
  68.     if(Calc == 0 && total!=0 && counter==4)
  69.     {
  70.        HTTP(total); //kirim data melalui HTTP POST
  71.        total=0; //ubah data total menjadi 0
  72.     }
  73.     else
  74.     {
  75.       temp=Calc;
  76.       total=total+temp;
  77.      
  78.     }
  79.     counter++;
  80.       if(counter==5)
  81.       {
  82.         Serial.print("Total : ");
  83.         Serial.println(total);
  84.         Serial.println("");
  85.         counter=0;
  86.       }    
  87.  
  88. }
  89.  
  90. /*Fungsi Kirim data via HTTP*/
  91. void HTTP(int data)
  92. {
  93.   Serial.println("AT+HTTPINIT");
  94.   delay(5000);
  95.   Serial.println("AT+HTTPPARA=\"CID\",1");
  96.   delay(5000);
  97.  
  98.   sprintf(buffer_kirim,"AT+HTTPPARA=\"URL\",\"http://%s/insert_data.php?lat=70.0000&lon=140.00000&data_air=%d\" ",url,data);
  99.   Serial.println(buffer_kirim);
  100.   delay(5000);
  101.   Serial.println("AT+HTTPACTION=0");
  102.   delay(5000);
  103.   Serial.println("AT+HTTPREAD=0");
  104. }
  105.  
  106. /*Fungsi Inisialisasi Hardware*/
  107. uint8_t power_on(){
  108.  
  109.     uint8_t answer=0;
  110.  
  111.     // checks if the module is started
  112.     answer = sendATcommand("AT", "OK", 2000);
  113.     if (answer == 0)
  114.     {
  115.         // power on pulse
  116.         digitalWrite(4,HIGH); //Disable GPS
  117.         delay(2000);
  118.         digitalWrite(3,LOW); //Enable GSM
  119.  
  120.         // waits for an answer from the module
  121.         while(answer == 0){  
  122.             // Send AT every two seconds and wait for the answer  
  123.             answer = sendATcommand("AT", "OK", 2000);    
  124.         }
  125.     }
  126.     return answer;
  127. }
  128.  
  129. /*Fungsi sendATcommand untuk memudahkan komunikasi dengan output 1 jawaban*/
  130. int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
  131.  
  132.     uint8_t x=0,  answer=0;
  133.     char response[100];
  134.     unsigned long previous;
  135.  
  136.     memset(response, '\0', 100);    // Initialize the string
  137.  
  138.     delay(100);
  139.  
  140.     while( Serial.available() > 0) Serial.read();    // Clean the input buffer
  141.  
  142.     Serial.println(ATcommand);    // Send the AT command
  143.  
  144.  
  145.         x = 0;
  146.     previous = millis();
  147.  
  148.     // this loop waits for the answer
  149.     do{
  150.         if(Serial.available() != 0){    
  151.             response[x] = Serial.read();
  152.             x++;
  153.             // check if the desired answer is in the response of the module
  154.             if (strstr(response, expected_answer1) != NULL)    
  155.             {
  156.                 answer = 1;
  157.             }
  158.         }
  159.         // Waits for the asnwer with time out
  160.     }
  161.     while((answer == 0) && ((millis() - previous) < timeout));    
  162.  
  163.     return answer;
  164. }
  165.  
  166. /*Fungsi sendATcommand untuk memudahkan komunikasi dengan output 2 jawaban*/
  167. int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
  168. char* expected_answer2, unsigned int timeout){
  169.  
  170.     uint8_t x=0,  answer=0;
  171.     char response[100];
  172.     unsigned long previous;
  173.  
  174.     memset(response, '\0', 100);    // Initialize the string
  175.  
  176.     delay(100);
  177.  
  178.     while( Serial.available() > 0) Serial.read();    // Clean the input buffer
  179.  
  180.     Serial.println(ATcommand);    // Send the AT command
  181.  
  182.  
  183.         x = 0;
  184.     previous = millis();
  185.  
  186.     // this loop waits for the answer
  187.     do{        
  188.         if(Serial.available() != 0){    
  189.             response[x] = Serial.read();
  190.             x++;
  191.             // check if the desired answer 1 is in the response of the module
  192.             if (strstr(response, expected_answer1) != NULL)    
  193.             {
  194.                 answer = 1;
  195.             }
  196.             // check if the desired answer 2 is in the response of the module
  197.             if (strstr(response, expected_answer2) != NULL)    
  198.             {
  199.                 answer = 2;
  200.             }
  201.         }
  202.         // Waits for the asnwer with time out
  203.     }while((answer == 0) && ((millis() - previous) < timeout));    
  204.  
  205.     return answer;
  206. }
Advertisement
Add Comment
Please, Sign In to add comment