volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
int hallsensor = 2; //The pin location of the sensor
int total = 0;
int temp=0;
int counter =0;
int hitung = 0;
char buffer_kirim[100];
char url[]="www.cobaserver.hosting.com";
int8_t answer, respond;
byte gsmDriverPin[3] = {
3,4,5};
void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
}
void setup()
{
for(int i = 0 ; i < 3; i++){
pinMode(gsmDriverPin[i],OUTPUT);
}
//Output GSM Timing
digitalWrite(5,HIGH);
delay(1500);
digitalWrite(5,LOW);
delay(2000);
pinMode(hallsensor, INPUT);
Serial.begin(9600); //set the baud rate
delay(5000);
attachInterrupt(0, rpm, RISING);
delay(5000);
Serial.println("Starting...");
power_on();
delay(3000);
sendATcommand("AT+CPIN=****", "OK", 2000);
delay(3000);
/*Konfigurasi APN*/
while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);
sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"indosatgprs\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000);
if ((sendATcommand("AT+SAPBR=1,1", "OK", 10000)) == 0)
Serial.println ("Waiting...");
}
void loop()
{
/*Fungsi mengambil data dari sensor flowemeter*/
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (NbTopsFan / 7.5); //(Pulse frequency x 60) / 7.5Q, = flow rate in L/hour
Serial.print("Counter : ");
Serial.println(counter);
Serial.print(Calc);
Serial.println(" L/min");
if(Calc == 0 && total!=0 && counter==4)
{
HTTP(total); //kirim data melalui HTTP POST
total=0; //ubah data total menjadi 0
}
else
{
temp=Calc;
total=total+temp;
}
counter++;
if(counter==5)
{
Serial.print("Total : ");
Serial.println(total);
Serial.println("");
counter=0;
}
}
/*Fungsi Kirim data via HTTP*/
void HTTP(int data)
{
Serial.println("AT+HTTPINIT");
delay(5000);
Serial.println("AT+HTTPPARA=\"CID\",1");
delay(5000);
sprintf(buffer_kirim,"AT+HTTPPARA=\"URL\",\"http://%s/insert_data.php?lat=70.0000&lon=140.00000&data_air=%d\" ",url,data);
Serial.println(buffer_kirim);
delay(5000);
Serial.println("AT+HTTPACTION=0");
delay(5000);
Serial.println("AT+HTTPREAD=0");
}
/*Fungsi Inisialisasi Hardware*/
uint8_t power_on(){
uint8_t answer=0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
if (answer == 0)
{
// power on pulse
digitalWrite(4,HIGH); //Disable GPS
delay(2000);
digitalWrite(3,LOW); //Enable GSM
// waits for an answer from the module
while(answer == 0){
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
}
}
return answer;
}
/*Fungsi sendATcommand untuk memudahkan komunikasi dengan output 1 jawaban*/
int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
/*Fungsi sendATcommand untuk memudahkan komunikasi dengan output 2 jawaban*/
int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
char* expected_answer2, unsigned int timeout){
uint8_t x=0, answer=0;
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
while( Serial.available() > 0) Serial.read(); // Clean the input buffer
Serial.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do{
if(Serial.available() != 0){
response[x] = Serial.read();
x++;
// check if the desired answer 1 is in the response of the module
if (strstr(response, expected_answer1) != NULL)
{
answer = 1;
}
// check if the desired answer 2 is in the response of the module
if (strstr(response, expected_answer2) != NULL)
{
answer = 2;
}
}
// Waits for the asnwer with time out
}while((answer == 0) && ((millis() - previous) < timeout));
return answer;
}
Comments