
Final Field
main.cpp@0:353a3e2f6d51, 2014-10-21 (annotated)
- Committer:
- precision
- Date:
- Tue Oct 21 11:54:16 2014 +0000
- Revision:
- 0:353a3e2f6d51
NXP 5.24
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
precision | 0:353a3e2f6d51 | 1 | |
precision | 0:353a3e2f6d51 | 2 | |
precision | 0:353a3e2f6d51 | 3 | #include "mbed.h" |
precision | 0:353a3e2f6d51 | 4 | #include "TextLCD.h" |
precision | 0:353a3e2f6d51 | 5 | |
precision | 0:353a3e2f6d51 | 6 | |
precision | 0:353a3e2f6d51 | 7 | AnalogIn sm1(p15); |
precision | 0:353a3e2f6d51 | 8 | AnalogIn sm2(p16); |
precision | 0:353a3e2f6d51 | 9 | AnalogIn sm3(p17); |
precision | 0:353a3e2f6d51 | 10 | AnalogIn sm4(p18); |
precision | 0:353a3e2f6d51 | 11 | DigitalOut relay(p30); |
precision | 0:353a3e2f6d51 | 12 | Serial GPRS(p9,p10); |
precision | 0:353a3e2f6d51 | 13 | Serial uart(p13,p14); |
precision | 0:353a3e2f6d51 | 14 | Serial pc(USBTX, USBRX); // tx, rx |
precision | 0:353a3e2f6d51 | 15 | TextLCD lcd(p26, p25, p24, p23, p22, p21); // rs, e, d4, d5, d6, d7 |
precision | 0:353a3e2f6d51 | 16 | LocalFileSystem local("local"); |
precision | 0:353a3e2f6d51 | 17 | DigitalOut uart_activity(LED2); |
precision | 0:353a3e2f6d51 | 18 | char a=26,e=0x22,c=0x0d,t,ack; |
precision | 0:353a3e2f6d51 | 19 | char num[]="9538765141"; |
precision | 0:353a3e2f6d51 | 20 | float ma; |
precision | 0:353a3e2f6d51 | 21 | float s[5],m[5]; |
precision | 0:353a3e2f6d51 | 22 | int b=100,count; |
precision | 0:353a3e2f6d51 | 23 | char col1[]="TIMES",col2[]="SM1",col3[]="SM2",col4[]="SM3",col5[]="SM4"; |
precision | 0:353a3e2f6d51 | 24 | DigitalOut zig(LED1); |
precision | 0:353a3e2f6d51 | 25 | int mz[5]; |
precision | 0:353a3e2f6d51 | 26 | |
precision | 0:353a3e2f6d51 | 27 | void SensorRead() |
precision | 0:353a3e2f6d51 | 28 | { |
precision | 0:353a3e2f6d51 | 29 | |
precision | 0:353a3e2f6d51 | 30 | lcd.locate(0,0); |
precision | 0:353a3e2f6d51 | 31 | lcd.printf("sensor readings\r"); |
precision | 0:353a3e2f6d51 | 32 | s[1]=sm1.read(); |
precision | 0:353a3e2f6d51 | 33 | m[1]=(0.057*(s[1]*1024))+25.072; |
precision | 0:353a3e2f6d51 | 34 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 35 | lcd.printf("Raw SM1=%0.3f",s[1]); |
precision | 0:353a3e2f6d51 | 36 | wait(2); |
precision | 0:353a3e2f6d51 | 37 | |
precision | 0:353a3e2f6d51 | 38 | s[2]=sm2.read(); |
precision | 0:353a3e2f6d51 | 39 | m[2]=(0.057*(s[2]*1024))+25.072; |
precision | 0:353a3e2f6d51 | 40 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 41 | lcd.printf("Raw SM2=%0.3f",s[2]); |
precision | 0:353a3e2f6d51 | 42 | wait(2); |
precision | 0:353a3e2f6d51 | 43 | |
precision | 0:353a3e2f6d51 | 44 | s[3]=sm3.read(); |
precision | 0:353a3e2f6d51 | 45 | m[3]=(0.057*(s[3]*1024))+25.072; |
precision | 0:353a3e2f6d51 | 46 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 47 | lcd.printf("Raw SM3=%0.3f",s[3]); |
precision | 0:353a3e2f6d51 | 48 | wait(2); |
precision | 0:353a3e2f6d51 | 49 | |
precision | 0:353a3e2f6d51 | 50 | s[4]=sm4.read(); |
precision | 0:353a3e2f6d51 | 51 | m[4]=(0.057*(s[4]*1024))+25.072; |
precision | 0:353a3e2f6d51 | 52 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 53 | lcd.printf("Raw SM4=%0.3f",s[4]); |
precision | 0:353a3e2f6d51 | 54 | wait(2); |
precision | 0:353a3e2f6d51 | 55 | |
precision | 0:353a3e2f6d51 | 56 | ma=(m[1]+m[2]+m[3]+m[4])/4; |
precision | 0:353a3e2f6d51 | 57 | lcd.locate(0,0); |
precision | 0:353a3e2f6d51 | 58 | lcd.printf("average value is=%f\n\r",ma); |
precision | 0:353a3e2f6d51 | 59 | wait(2); |
precision | 0:353a3e2f6d51 | 60 | } |
precision | 0:353a3e2f6d51 | 61 | |
precision | 0:353a3e2f6d51 | 62 | void xlfile() |
precision | 0:353a3e2f6d51 | 63 | { |
precision | 0:353a3e2f6d51 | 64 | //Local storage (On board flash memory of 2MB) |
precision | 0:353a3e2f6d51 | 65 | count++; |
precision | 0:353a3e2f6d51 | 66 | FILE *fp=fopen("/local/KAEMS.csv","a"); |
precision | 0:353a3e2f6d51 | 67 | fprintf(fp,"%d,%0.3f,%0.3f,%0.3f,%0.3f,%0.3f\n",count,m[1],m[2],m[3],m[4],m); |
precision | 0:353a3e2f6d51 | 68 | fclose(fp); |
precision | 0:353a3e2f6d51 | 69 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 70 | lcd.printf("File updated"); |
precision | 0:353a3e2f6d51 | 71 | wait(2); |
precision | 0:353a3e2f6d51 | 72 | } |
precision | 0:353a3e2f6d51 | 73 | //Realy control |
precision | 0:353a3e2f6d51 | 74 | void RelayControl() |
precision | 0:353a3e2f6d51 | 75 | { |
precision | 0:353a3e2f6d51 | 76 | if(ma>40) |
precision | 0:353a3e2f6d51 | 77 | relay=0; |
precision | 0:353a3e2f6d51 | 78 | else |
precision | 0:353a3e2f6d51 | 79 | relay=1; |
precision | 0:353a3e2f6d51 | 80 | wait(2); |
precision | 0:353a3e2f6d51 | 81 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 82 | lcd.locate(0,0); |
precision | 0:353a3e2f6d51 | 83 | lcd.printf("Relay Function..."); |
precision | 0:353a3e2f6d51 | 84 | wait(2); |
precision | 0:353a3e2f6d51 | 85 | } |
precision | 0:353a3e2f6d51 | 86 | |
precision | 0:353a3e2f6d51 | 87 | void Upload() |
precision | 0:353a3e2f6d51 | 88 | { |
precision | 0:353a3e2f6d51 | 89 | //GPRS commands |
precision | 0:353a3e2f6d51 | 90 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 91 | lcd.locate(0,0); |
precision | 0:353a3e2f6d51 | 92 | lcd.printf("Updating KAEMS..."); |
precision | 0:353a3e2f6d51 | 93 | wait(3); |
precision | 0:353a3e2f6d51 | 94 | GPRS.printf("AT+SAPBR=0,1\r\n"); |
precision | 0:353a3e2f6d51 | 95 | wait(3); |
precision | 0:353a3e2f6d51 | 96 | GPRS.printf("AT+SAPBR=3,1,%cCONTYPE%c,%cGPRS%c\r\n",e,e,e,e); |
precision | 0:353a3e2f6d51 | 97 | wait(3); |
precision | 0:353a3e2f6d51 | 98 | GPRS.printf("AT+SAPBR=3,1,%cAPN%c,%cAIRCELGPRS.COM%c\r\n",e,e,e,e); |
precision | 0:353a3e2f6d51 | 99 | wait(3); |
precision | 0:353a3e2f6d51 | 100 | GPRS.printf("AT+SAPBR=1,1\r\n"); |
precision | 0:353a3e2f6d51 | 101 | wait(3); |
precision | 0:353a3e2f6d51 | 102 | GPRS.printf("AT+HTTPINIT\r\n"); |
precision | 0:353a3e2f6d51 | 103 | wait(2); |
precision | 0:353a3e2f6d51 | 104 | GPRS.printf("AT+HTTPPARA=%cCID%c,1\r\n",e,e); |
precision | 0:353a3e2f6d51 | 105 | wait(2); |
precision | 0:353a3e2f6d51 | 106 | 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); |
precision | 0:353a3e2f6d51 | 107 | wait(2); |
precision | 0:353a3e2f6d51 | 108 | GPRS.printf("AT+HTTPDATA=1000,5000\r\n"); |
precision | 0:353a3e2f6d51 | 109 | wait(10); |
precision | 0:353a3e2f6d51 | 110 | GPRS.printf("AT+HTTPACTION=1\r\n"); |
precision | 0:353a3e2f6d51 | 111 | wait(5); |
precision | 0:353a3e2f6d51 | 112 | GPRS.printf("AT+HTTPTERM\r\n"); |
precision | 0:353a3e2f6d51 | 113 | wait(5); |
precision | 0:353a3e2f6d51 | 114 | |
precision | 0:353a3e2f6d51 | 115 | |
precision | 0:353a3e2f6d51 | 116 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 117 | lcd.locate(0,0); |
precision | 0:353a3e2f6d51 | 118 | lcd.printf("KAEMS UPDATED"); |
precision | 0:353a3e2f6d51 | 119 | wait(3); |
precision | 0:353a3e2f6d51 | 120 | } |
precision | 0:353a3e2f6d51 | 121 | |
precision | 0:353a3e2f6d51 | 122 | void SendMessage() |
precision | 0:353a3e2f6d51 | 123 | { |
precision | 0:353a3e2f6d51 | 124 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 125 | lcd.locate(0,0); |
precision | 0:353a3e2f6d51 | 126 | lcd.printf("SENDING MESSAGE"); |
precision | 0:353a3e2f6d51 | 127 | if(ma<40) |
precision | 0:353a3e2f6d51 | 128 | { |
precision | 0:353a3e2f6d51 | 129 | GPRS.printf("AT+CMGS=%c%s%c\r\n",e,num,e); |
precision | 0:353a3e2f6d51 | 130 | wait(3); |
precision | 0:353a3e2f6d51 | 131 | GPRS.printf(" WATER LEVEL IS LOW \nMoisture IS %f \r\n",ma); |
precision | 0:353a3e2f6d51 | 132 | wait(1); |
precision | 0:353a3e2f6d51 | 133 | GPRS.printf("%c",a); |
precision | 0:353a3e2f6d51 | 134 | wait(2); |
precision | 0:353a3e2f6d51 | 135 | } |
precision | 0:353a3e2f6d51 | 136 | if(ma>40) |
precision | 0:353a3e2f6d51 | 137 | |
precision | 0:353a3e2f6d51 | 138 | { |
precision | 0:353a3e2f6d51 | 139 | GPRS.printf("AT+CMGS=%c%s%c\r\n",e,num,e); |
precision | 0:353a3e2f6d51 | 140 | wait(3); |
precision | 0:353a3e2f6d51 | 141 | GPRS.printf(" land has sufficicient amount of water \n Moisture IS %f \r\n",ma); |
precision | 0:353a3e2f6d51 | 142 | wait(1); |
precision | 0:353a3e2f6d51 | 143 | GPRS.printf("%c",a); |
precision | 0:353a3e2f6d51 | 144 | wait(2); |
precision | 0:353a3e2f6d51 | 145 | } |
precision | 0:353a3e2f6d51 | 146 | } |
precision | 0:353a3e2f6d51 | 147 | //zigbee sending |
precision | 0:353a3e2f6d51 | 148 | void Zigbee() |
precision | 0:353a3e2f6d51 | 149 | { |
precision | 0:353a3e2f6d51 | 150 | while(!uart.writeable()) // send thde signal to slave to indacate that the remote station is ready |
precision | 0:353a3e2f6d51 | 151 | {} |
precision | 0:353a3e2f6d51 | 152 | uart.printf("r"); |
precision | 0:353a3e2f6d51 | 153 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 154 | lcd.printf("command sent"); |
precision | 0:353a3e2f6d51 | 155 | wait(5); |
precision | 0:353a3e2f6d51 | 156 | if(uart.readable()) //recieve the signal from remote station |
precision | 0:353a3e2f6d51 | 157 | { |
precision | 0:353a3e2f6d51 | 158 | |
precision | 0:353a3e2f6d51 | 159 | ack=uart.getc(); |
precision | 0:353a3e2f6d51 | 160 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 161 | lcd.printf("recieved ack is %c",ack); |
precision | 0:353a3e2f6d51 | 162 | } |
precision | 0:353a3e2f6d51 | 163 | |
precision | 0:353a3e2f6d51 | 164 | if(ack =='s') |
precision | 0:353a3e2f6d51 | 165 | { |
precision | 0:353a3e2f6d51 | 166 | for(int i=1;i<5;i++) |
precision | 0:353a3e2f6d51 | 167 | { |
precision | 0:353a3e2f6d51 | 168 | mz[i]=m[i]; |
precision | 0:353a3e2f6d51 | 169 | while(!uart.writeable()) |
precision | 0:353a3e2f6d51 | 170 | {} |
precision | 0:353a3e2f6d51 | 171 | |
precision | 0:353a3e2f6d51 | 172 | uart.printf("%c",mz[i]); |
precision | 0:353a3e2f6d51 | 173 | wait(1); |
precision | 0:353a3e2f6d51 | 174 | lcd.cls(); |
precision | 0:353a3e2f6d51 | 175 | zig=!zig; |
precision | 0:353a3e2f6d51 | 176 | lcd.printf("Zigbee sent a value=%d",mz[i]); |
precision | 0:353a3e2f6d51 | 177 | wait(3); |
precision | 0:353a3e2f6d51 | 178 | } |
precision | 0:353a3e2f6d51 | 179 | } |
precision | 0:353a3e2f6d51 | 180 | } |
precision | 0:353a3e2f6d51 | 181 | |
precision | 0:353a3e2f6d51 | 182 | |
precision | 0:353a3e2f6d51 | 183 | int main() |
precision | 0:353a3e2f6d51 | 184 | { |
precision | 0:353a3e2f6d51 | 185 | GPRS.baud(9600); |
precision | 0:353a3e2f6d51 | 186 | GPRS.printf("AT+CMGF=1\r\n"); |
precision | 0:353a3e2f6d51 | 187 | GPRS.printf("AT\n"); |
precision | 0:353a3e2f6d51 | 188 | wait(3); |
precision | 0:353a3e2f6d51 | 189 | lcd.locate(0,0); |
precision | 0:353a3e2f6d51 | 190 | lcd.printf("SYSTEM IS ON\n\r"); |
precision | 0:353a3e2f6d51 | 191 | wait(1); |
precision | 0:353a3e2f6d51 | 192 | FILE *fp=fopen("/local/KAEMS.csv","a"); |
precision | 0:353a3e2f6d51 | 193 | fprintf(fp,"%s,%s,%s,%s,%s\n",col1,col2,col3,col4,col5); |
precision | 0:353a3e2f6d51 | 194 | fclose(fp); |
precision | 0:353a3e2f6d51 | 195 | |
precision | 0:353a3e2f6d51 | 196 | while(1) |
precision | 0:353a3e2f6d51 | 197 | { |
precision | 0:353a3e2f6d51 | 198 | SensorRead(); |
precision | 0:353a3e2f6d51 | 199 | xlfile(); |
precision | 0:353a3e2f6d51 | 200 | // Zigbee(); |
precision | 0:353a3e2f6d51 | 201 | RelayControl(); |
precision | 0:353a3e2f6d51 | 202 | Upload(); |
precision | 0:353a3e2f6d51 | 203 | SendMessage(); |
precision | 0:353a3e2f6d51 | 204 | wait(3600); |
precision | 0:353a3e2f6d51 | 205 | } |
precision | 0:353a3e2f6d51 | 206 | } |
precision | 0:353a3e2f6d51 | 207 | |
precision | 0:353a3e2f6d51 | 208 |