Final Field

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 
00003 #include "mbed.h"
00004 #include "TextLCD.h"
00005 
00006 
00007 AnalogIn sm1(p15);
00008 AnalogIn sm2(p16);
00009 AnalogIn sm3(p17);
00010 AnalogIn sm4(p18);
00011 DigitalOut relay(p30);
00012 Serial GPRS(p9,p10);
00013 Serial uart(p13,p14);
00014 Serial pc(USBTX, USBRX); // tx, rx
00015 TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4, d5, d6, d7
00016 LocalFileSystem local("local");
00017 DigitalOut uart_activity(LED2);
00018 char  a=26,e=0x22,c=0x0d,t,ack;
00019 char num[]="9538765141";
00020 float ma;
00021 float s[5],m[5];
00022 int b=100,count;
00023 char col1[]="TIMES",col2[]="SM1",col3[]="SM2",col4[]="SM3",col5[]="SM4";
00024 DigitalOut zig(LED1);
00025 int mz[5];
00026 
00027 void SensorRead()
00028 {
00029  
00030    lcd.locate(0,0);
00031    lcd.printf("sensor readings\r");
00032          s[1]=sm1.read();
00033          m[1]=(0.057*(s[1]*1024))+25.072;
00034          lcd.cls();
00035          lcd.printf("Raw SM1=%0.3f",s[1]); 
00036          wait(2);
00037          
00038          s[2]=sm2.read();
00039          m[2]=(0.057*(s[2]*1024))+25.072;
00040          lcd.cls();
00041          lcd.printf("Raw SM2=%0.3f",s[2]); 
00042          wait(2);
00043          
00044           s[3]=sm3.read();
00045          m[3]=(0.057*(s[3]*1024))+25.072;
00046          lcd.cls();
00047          lcd.printf("Raw SM3=%0.3f",s[3]); 
00048          wait(2);
00049          
00050           s[4]=sm4.read();
00051          m[4]=(0.057*(s[4]*1024))+25.072;
00052          lcd.cls();
00053          lcd.printf("Raw SM4=%0.3f",s[4]); 
00054          wait(2);
00055          
00056          ma=(m[1]+m[2]+m[3]+m[4])/4;
00057          lcd.locate(0,0);
00058          lcd.printf("average value is=%f\n\r",ma);
00059          wait(2);
00060 }
00061 
00062 void xlfile()
00063 {        
00064         //Local storage (On board flash memory of 2MB) 
00065         count++;
00066         FILE *fp=fopen("/local/KAEMS.csv","a");
00067         fprintf(fp,"%d,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f\n",count,m[1],m[2],m[3],m[4],m);
00068         fclose(fp);
00069         lcd.cls();
00070         lcd.printf("File updated");
00071         wait(2);
00072 }
00073        //Realy control
00074 void RelayControl() 
00075 {      
00076         if(ma>40)
00077         relay=0;
00078         else
00079         relay=1;
00080         wait(2); 
00081         lcd.cls();
00082         lcd.locate(0,0);
00083         lcd.printf("Relay Function...");
00084         wait(2);
00085 }
00086 
00087 void Upload()
00088 {
00089         //GPRS commands
00090         lcd.cls();
00091         lcd.locate(0,0);
00092         lcd.printf("Updating KAEMS...");
00093         wait(3);
00094         GPRS.printf("AT+SAPBR=0,1\r\n");
00095         wait(3);
00096         GPRS.printf("AT+SAPBR=3,1,%cCONTYPE%c,%cGPRS%c\r\n",e,e,e,e);
00097         wait(3);
00098         GPRS.printf("AT+SAPBR=3,1,%cAPN%c,%cAIRCELGPRS.COM%c\r\n",e,e,e,e);
00099         wait(3);
00100         GPRS.printf("AT+SAPBR=1,1\r\n");
00101         wait(3);
00102         GPRS.printf("AT+HTTPINIT\r\n");
00103         wait(2);
00104         GPRS.printf("AT+HTTPPARA=%cCID%c,1\r\n",e,e);
00105         wait(2);
00106         GPRS.printf("AT+HTTPPARA=%cURL%c,%chttp://kaems.org/update.php?u=3&t=23&h=34&s1=%f&s2=%f&s3=%f&s4=%f&ph=5&b=%d%c\r\n",e,e,e,m[1],m[2],m[3],m[4],b--,e);
00107         wait(2);
00108         GPRS.printf("AT+HTTPDATA=1000,5000\r\n"); 
00109         wait(10);
00110         GPRS.printf("AT+HTTPACTION=1\r\n"); 
00111         wait(5);
00112         GPRS.printf("AT+HTTPTERM\r\n");
00113         wait(5);
00114         
00115         
00116         lcd.cls();
00117         lcd.locate(0,0);
00118         lcd.printf("KAEMS UPDATED");
00119         wait(3);
00120 }
00121 
00122 void SendMessage()
00123 {
00124          lcd.cls();
00125         lcd.locate(0,0);
00126         lcd.printf("SENDING MESSAGE");
00127         if(ma<40)
00128         {
00129             GPRS.printf("AT+CMGS=%c%s%c\r\n",e,num,e);
00130             wait(3);
00131             GPRS.printf(" WATER LEVEL IS LOW \nMoisture IS %f \r\n",ma);
00132             wait(1);
00133             GPRS.printf("%c",a);
00134             wait(2);
00135         }
00136         if(ma>40)
00137         
00138         {
00139              GPRS.printf("AT+CMGS=%c%s%c\r\n",e,num,e);
00140             wait(3);
00141             GPRS.printf(" land has sufficicient amount of water \n Moisture IS %f \r\n",ma);
00142             wait(1);
00143             GPRS.printf("%c",a);
00144             wait(2);
00145         }    
00146 }
00147 //zigbee sending
00148 void Zigbee()
00149 {
00150         while(!uart.writeable())         // send thde signal to slave to indacate that the remote station is ready
00151         {}
00152         uart.printf("r");
00153          lcd.cls();
00154         lcd.printf("command sent");
00155         wait(5);
00156         if(uart.readable())              //recieve the signal from remote station
00157         { 
00158        
00159             ack=uart.getc();
00160             lcd.cls();
00161             lcd.printf("recieved ack is %c",ack);
00162         }
00163         
00164         if(ack =='s')
00165         {    
00166         for(int i=1;i<5;i++)  
00167         {
00168              mz[i]=m[i];
00169              while(!uart.writeable())
00170              {}
00171            
00172              uart.printf("%c",mz[i]);
00173              wait(1);
00174              lcd.cls();
00175              zig=!zig;
00176              lcd.printf("Zigbee sent a value=%d",mz[i]);
00177              wait(3);
00178          }
00179           }
00180 }
00181        
00182        
00183 int main()
00184 {
00185          GPRS.baud(9600);
00186          GPRS.printf("AT+CMGF=1\r\n");
00187          GPRS.printf("AT\n");
00188          wait(3);
00189          lcd.locate(0,0);
00190          lcd.printf("SYSTEM IS ON\n\r");
00191          wait(1);
00192          FILE *fp=fopen("/local/KAEMS.csv","a");
00193          fprintf(fp,"%s,%s,%s,%s,%s\n",col1,col2,col3,col4,col5);
00194          fclose(fp);
00195          
00196     while(1)
00197     {
00198         SensorRead();
00199         xlfile();
00200        // Zigbee();
00201         RelayControl();
00202         Upload(); 
00203         SendMessage();
00204         wait(3600);
00205     }
00206 }    
00207 
00208