kaems

Dependencies:   TextLCD WakeUp mbed

Fork of Final by Sandesh G

main.cpp

Committer:
Lakshmikumar
Date:
2015-01-19
Revision:
0:e876ef14b820
Child:
1:afb1111bab40

File content as of revision 0:e876ef14b820:

#include"mbed.h"
#include"TextLCD.h"
//AnalogIn sm1(p16);
//AnalogIn sm2(p17);
//AnalogIn sm3(p18);
//AnalogIn sm4(p19);
//DigitalOut led(LED1);
Serial GPRS(p9,p10);
Serial pc(USBTX,USBRX);
TextLCD lcd(p26, p25, p24, p23, p22, p21); //rs,e,d0-d3
LocalFileSystem local("local");
char   a=0x1A,c=0x0D;
char num[]="7411755981";
float s[5],m[5];
float ma;
int z,j=0;
int b=100,count;
char str;
char col1[]="ITERATION",col2[]="SM1",col3[]="SM2",col4[]="SM3",col5[]="SM4",col6[]="Mean";

void SensorRead()
{
         lcd.cls();
         lcd.locate(0,0);
         lcd.printf("Sensor Readings\r");
         
         s[1]=0.21;
         m[1]=(0.057*(s[1]*1024))+25.072;
        
         lcd.cls();
         lcd.printf("Raw SM1=%0.3f",s[1]); 
         wait(2);
         
         s[2]=0.2;
         m[2]=(0.057*(s[2]*1024))+25.072;
         
         lcd.cls();
         lcd.printf("Raw SM2=%0.3f",s[2]); 
         wait(2);
         
          s[3]=0.23;
         m[3]=(0.057*(s[3]*1024))+25.072;
         lcd.cls();
         lcd.printf("Raw SM3=%0.3f",s[3]); 
         wait(2);
         
         s[4]=0.55;
         m[4]=(0.057*(s[4]*1024))+25.072;
         
         lcd.cls();
         lcd.printf("Raw SM4=%0.3f",s[4]); 
         wait(2);
         
         ma=(m[1]+m[2]+m[3]+m[4])/4;
         
         lcd.cls();
         lcd.locate(0,0);
         lcd.printf("Mean =%0.4f",ma);
         wait(2);
}

void xlfile()
{        
        //Local storage (On board flash memory of 2MB) 
        count++;
        FILE *fp=fopen("/local/KAEMS.csv","a");
        fprintf(fp,"%d,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f\n",count,m[1],m[2],m[3],m[4],ma);
        fclose(fp);
        lcd.cls();
        lcd.printf("File updated");
        wait(2);
}
void flushSerialBuffer(void) 
{ char char1 = 0;
    pc.printf("Flushing..");
 while (GPRS.readable()) 
 { char1 = GPRS.getc();
  } return; 
 }
void Upload()
{
        //GPRS commands
        lcd.cls();
        lcd.locate(0,0);
        lcd.printf("   Updating\n www.kaems.org");
        wait(3);
        GPRS.printf("AT+SAPBR=0,1\r\n");
        wait(3);
        GPRS.printf("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n");
        wait(3);
        GPRS.printf("AT+SAPBR=3,1,\"APN\",\"AIRCELGPRS.COM\"\r\n");
        wait(3);
        GPRS.printf("AT+SAPBR=1,1\r\n");
        wait(3);
        GPRS.printf("AT+HTTPINIT\r\n");
        wait(2);
        GPRS.printf("AT+HTTPPARA=\"CID\",1\r\n");
        wait(2);
        GPRS.printf("AT+HTTPPARA=\"URL\",\"http://kaems.org/update.php?u=3&t=23&h=34&s1=%f&s2=%f&s3=%f&s4=%f&ph=5&b=%d\"\r\n",m[1],m[2],m[3],m[4],b--);
        wait(2);
        GPRS.printf("AT+HTTPDATA=1000,5000\r\n");
        wait(10);
        GPRS.printf("AT+HTTPACTION=1\r\n");
        wait(5);
        GPRS.printf("AT+HTTPTERM\r\n");
        lcd.cls();
        lcd.printf("KAEMS UPDATED");
        wait(3);
}
void feedback()
{       lcd.cls();
        lcd.locate(0,0);
        lcd.printf("Waiting for feedback");
        wait(3);
        GPRS.printf("AT+SAPBR=0,1\r\n");
        wait(3);
        GPRS.printf("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"\r\n");
        wait(3);
        GPRS.printf("AT+SAPBR=3,1,\"APN\",\"AIRCELGPRS.COM\"\r\n");
        wait(3);
        GPRS.printf("AT+SAPBR=1,1\r\n");
        wait(3);
        GPRS.printf("AT+HTTPINIT\r\n");
        wait(2);
        GPRS.printf("AT+HTTPPARA=\"CID\",1\r\n");
        wait(2);
        GPRS.printf("AT+HTTPPARA=\"URL\",\"http://kaems.org/feedback.php\"\r\n");
        wait(2);
        GPRS.printf("AT+HTTPACTION=0\r\n");
        wait(5);
        flushSerialBuffer();
        GPRS.printf("AT+HTTPREAD\r\n"); 
        while(!GPRS.readable()){    }
        char *str2;
        GPRS.scanf("%s",str2);
        GPRS.scanf("%s",str2);
        GPRS.scanf("%s",str2); // Getting op
        pc.printf("Str=%c\r\n",str2[0]);
        lcd.cls();
        lcd.printf("Fbk=%c",str2[0]);
//     switch(str2[0]){
//            case '1': break;
//            
//        }
        wait(5);
        GPRS.printf("AT+HTTPTERM\r\n");
        }
    
void sms()
{
    GPRS.printf("AT\r\n");
    wait(2);
    GPRS.printf("AT+CMGF=1\r\n");
    wait(2);
    GPRS.printf("AT+CMGS=\"%s\"\r\n",num);
    wait(3);
  
        if(ma<40)
        {
            GPRS.printf(" Water level is low \nMoisture level is  %f \r\n",ma);
            wait(1);
            GPRS.printf("%c",a);
            wait(2);
        }
        
        if(ma>40)
        {
            GPRS.printf(" Land has sufficicient amount of water \n Moisture IS %f \r\n",ma);
            wait(1);
            GPRS.printf("%c",a);
            wait(2);
        }    
        lcd.printf("SMS Sent");
    
    }
int main() 
{        GPRS.baud(9600);
         GPRS.printf("AT\n");
         wait(3);
         lcd.cls();
         lcd.printf("   PRECISION\n  AGRICULTURE");
    while(1) 
    {
         FILE *fp=fopen("/local/KAEMS.csv","a");
         fprintf(fp,"%s,%s,%s,%s,%s,%s\n",col1,col2,col3,col4,col5,col6);
         fclose(fp);
         SensorRead();
         xlfile();
         //Upload(); 
         feedback();
         //sms();
         lcd.cls();
         lcd.printf("   PRECISION\n  AGRICULTURE");
         wait(1200);
    }
}