dfasafd
Dependencies: mbed
main.cpp@0:b829f4597400, 2016-06-16 (annotated)
- Committer:
- muyiq
- Date:
- Thu Jun 16 14:10:41 2016 +0000
- Revision:
- 0:b829f4597400
- Child:
- 1:b7470b5a6b23
1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
muyiq | 0:b829f4597400 | 1 | #include "mbed.h" |
muyiq | 0:b829f4597400 | 2 | //#include "TSISensor.h" |
muyiq | 0:b829f4597400 | 3 | #define DEBUG |
muyiq | 0:b829f4597400 | 4 | #define INFOMESSAGES |
muyiq | 0:b829f4597400 | 5 | #define WARNMESSAGES |
muyiq | 0:b829f4597400 | 6 | #define ERRMESSAGES |
muyiq | 0:b829f4597400 | 7 | |
muyiq | 0:b829f4597400 | 8 | #define FUNCNAME "MAIN" |
muyiq | 0:b829f4597400 | 9 | |
muyiq | 0:b829f4597400 | 10 | #ifdef NoDEBUG |
muyiq | 0:b829f4597400 | 11 | #define DBG(x, ...) pc.printf(" ["FUNCNAME" : DBG] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__); |
muyiq | 0:b829f4597400 | 12 | #else |
muyiq | 0:b829f4597400 | 13 | #define DBG(x, ...) |
muyiq | 0:b829f4597400 | 14 | #endif |
muyiq | 0:b829f4597400 | 15 | |
muyiq | 0:b829f4597400 | 16 | #ifdef ERRMESSAGES |
muyiq | 0:b829f4597400 | 17 | #define ERR(x, ...) pc.printf(" ["FUNCNAME" : ERR] "x"\r\n", ##__VA_ARGS__); |
muyiq | 0:b829f4597400 | 18 | #else |
muyiq | 0:b829f4597400 | 19 | #define ERR(x, ...) |
muyiq | 0:b829f4597400 | 20 | #endif |
muyiq | 0:b829f4597400 | 21 | |
muyiq | 0:b829f4597400 | 22 | #ifdef WARNMESSAGES |
muyiq | 0:b829f4597400 | 23 | #define WARN(x, ...) printf("["FUNCNAME" : WARN] "x"\r\n", ##__VA_ARGS__); |
muyiq | 0:b829f4597400 | 24 | #else |
muyiq | 0:b829f4597400 | 25 | #define WARN(x, ...) |
muyiq | 0:b829f4597400 | 26 | #endif |
muyiq | 0:b829f4597400 | 27 | |
muyiq | 0:b829f4597400 | 28 | #ifdef INFOMESSAGES |
muyiq | 0:b829f4597400 | 29 | #define INFO(x, ...) pc.printf("["FUNCNAME" : INFO] "x"\r\n", ##__VA_ARGS__); |
muyiq | 0:b829f4597400 | 30 | #else |
muyiq | 0:b829f4597400 | 31 | #define INFO(x, ...) |
muyiq | 0:b829f4597400 | 32 | #endif |
muyiq | 0:b829f4597400 | 33 | |
muyiq | 0:b829f4597400 | 34 | #define BUFF_SIZE 1024 |
muyiq | 0:b829f4597400 | 35 | // PIN DEFS NOW FOR OLD FIRMWARE VERSION OF THE KL25Z |
muyiq | 0:b829f4597400 | 36 | Serial pc(USBTX, USBRX); |
muyiq | 0:b829f4597400 | 37 | //RawSerial dev(D1, D0); |
muyiq | 0:b829f4597400 | 38 | Serial dev(PTE22,PTE23); //tx,rx |
muyiq | 0:b829f4597400 | 39 | DigitalOut led1(LED1); |
muyiq | 0:b829f4597400 | 40 | DigitalOut led4(LED3); // CHANGE TO LED 3 |
muyiq | 0:b829f4597400 | 41 | DigitalOut reset(PTB0,1); |
muyiq | 0:b829f4597400 | 42 | |
muyiq | 0:b829f4597400 | 43 | |
muyiq | 0:b829f4597400 | 44 | PwmOut led(LED_GREEN); |
muyiq | 0:b829f4597400 | 45 | // TSISensor tsi; |
muyiq | 0:b829f4597400 | 46 | volatile int state=0; |
muyiq | 0:b829f4597400 | 47 | volatile int ready=0; |
muyiq | 0:b829f4597400 | 48 | |
muyiq | 0:b829f4597400 | 49 | char ipAddress[20]; |
muyiq | 0:b829f4597400 | 50 | char macAddress[32]; |
muyiq | 0:b829f4597400 | 51 | char *buffer; |
muyiq | 0:b829f4597400 | 52 | unsigned int bufferPnt=0; |
muyiq | 0:b829f4597400 | 53 | |
muyiq | 0:b829f4597400 | 54 | void dev_recv() |
muyiq | 0:b829f4597400 | 55 | { |
muyiq | 0:b829f4597400 | 56 | char c; |
muyiq | 0:b829f4597400 | 57 | |
muyiq | 0:b829f4597400 | 58 | int count = 0; |
muyiq | 0:b829f4597400 | 59 | led1 = !led1; |
muyiq | 0:b829f4597400 | 60 | if(bufferPnt==0) { |
muyiq | 0:b829f4597400 | 61 | memset(buffer,0,BUFF_SIZE); |
muyiq | 0:b829f4597400 | 62 | } |
muyiq | 0:b829f4597400 | 63 | while(dev.readable()) { |
muyiq | 0:b829f4597400 | 64 | c = (char)dev.getc(); |
muyiq | 0:b829f4597400 | 65 | #ifdef DEBUG |
muyiq | 0:b829f4597400 | 66 | pc.putc(c); |
muyiq | 0:b829f4597400 | 67 | #endif |
muyiq | 0:b829f4597400 | 68 | buffer[bufferPnt]=c; |
muyiq | 0:b829f4597400 | 69 | bufferPnt++; |
muyiq | 0:b829f4597400 | 70 | if (bufferPnt>1000) { |
muyiq | 0:b829f4597400 | 71 | ready=1; |
muyiq | 0:b829f4597400 | 72 | } |
muyiq | 0:b829f4597400 | 73 | // if ((c==0x0a)||(c==0x0d)){ |
muyiq | 0:b829f4597400 | 74 | // ready=1; |
muyiq | 0:b829f4597400 | 75 | // }else |
muyiq | 0:b829f4597400 | 76 | if (c==0x0a) { |
muyiq | 0:b829f4597400 | 77 | if (bufferPnt>1) { |
muyiq | 0:b829f4597400 | 78 | if (buffer[bufferPnt -2]==0x0d) { |
muyiq | 0:b829f4597400 | 79 | ready=1; |
muyiq | 0:b829f4597400 | 80 | break; |
muyiq | 0:b829f4597400 | 81 | } |
muyiq | 0:b829f4597400 | 82 | } |
muyiq | 0:b829f4597400 | 83 | } |
muyiq | 0:b829f4597400 | 84 | if (!dev.readable()) { |
muyiq | 0:b829f4597400 | 85 | wait_us(10); |
muyiq | 0:b829f4597400 | 86 | } |
muyiq | 0:b829f4597400 | 87 | } |
muyiq | 0:b829f4597400 | 88 | } |
muyiq | 0:b829f4597400 | 89 | |
muyiq | 0:b829f4597400 | 90 | void pc_recv() |
muyiq | 0:b829f4597400 | 91 | { |
muyiq | 0:b829f4597400 | 92 | char c; |
muyiq | 0:b829f4597400 | 93 | led4 = !led4; |
muyiq | 0:b829f4597400 | 94 | while(pc.readable()) { |
muyiq | 0:b829f4597400 | 95 | c=(char)pc.getc(); |
muyiq | 0:b829f4597400 | 96 | dev.putc(c); |
muyiq | 0:b829f4597400 | 97 | pc.putc(c); |
muyiq | 0:b829f4597400 | 98 | if(c==13) { |
muyiq | 0:b829f4597400 | 99 | dev.putc(10); |
muyiq | 0:b829f4597400 | 100 | pc.putc(10); |
muyiq | 0:b829f4597400 | 101 | } |
muyiq | 0:b829f4597400 | 102 | |
muyiq | 0:b829f4597400 | 103 | } |
muyiq | 0:b829f4597400 | 104 | } |
muyiq | 0:b829f4597400 | 105 | |
muyiq | 0:b829f4597400 | 106 | char * OKResponse(char *test, const char *pattern) |
muyiq | 0:b829f4597400 | 107 | { |
muyiq | 0:b829f4597400 | 108 | char *p= strstr(test,pattern); |
muyiq | 0:b829f4597400 | 109 | if (p==NULL) { |
muyiq | 0:b829f4597400 | 110 | // DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p); |
muyiq | 0:b829f4597400 | 111 | return NULL; |
muyiq | 0:b829f4597400 | 112 | } else { |
muyiq | 0:b829f4597400 | 113 | // DBG("YAY Test=<%s> Patter=<%s> [p=%s]",test,pattern,p); |
muyiq | 0:b829f4597400 | 114 | } |
muyiq | 0:b829f4597400 | 115 | return p; |
muyiq | 0:b829f4597400 | 116 | } |
muyiq | 0:b829f4597400 | 117 | int main() |
muyiq | 0:b829f4597400 | 118 | { |
muyiq | 0:b829f4597400 | 119 | float touch; |
muyiq | 0:b829f4597400 | 120 | buffer=(char *)calloc(BUFF_SIZE,1); |
muyiq | 0:b829f4597400 | 121 | reset=0; |
muyiq | 0:b829f4597400 | 122 | wait(3.0); |
muyiq | 0:b829f4597400 | 123 | reset=1; |
muyiq | 0:b829f4597400 | 124 | int counter=0; |
muyiq | 0:b829f4597400 | 125 | pc.baud(115200); |
muyiq | 0:b829f4597400 | 126 | dev.baud(115200); |
muyiq | 0:b829f4597400 | 127 | pc.attach(&pc_recv, Serial::RxIrq); |
muyiq | 0:b829f4597400 | 128 | dev.attach(&dev_recv, Serial::RxIrq); |
muyiq | 0:b829f4597400 | 129 | pc.printf("Start up\n\r"); |
muyiq | 0:b829f4597400 | 130 | char * resp=NULL; |
muyiq | 0:b829f4597400 | 131 | pc.printf("Here \n\r"); |
muyiq | 0:b829f4597400 | 132 | while(1) { |
muyiq | 0:b829f4597400 | 133 | if (ready) { |
muyiq | 0:b829f4597400 | 134 | ready=0; |
muyiq | 0:b829f4597400 | 135 | bufferPnt=0; |
muyiq | 0:b829f4597400 | 136 | INFO("[%d],##%s##",state,buffer); |
muyiq | 0:b829f4597400 | 137 | switch (state) { |
muyiq | 0:b829f4597400 | 138 | case 0: { |
muyiq | 0:b829f4597400 | 139 | resp=OKResponse(buffer,"WIFI GOT IP"); |
muyiq | 0:b829f4597400 | 140 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 141 | wait(1); |
muyiq | 0:b829f4597400 | 142 | dev.printf("AT\r\n"); |
muyiq | 0:b829f4597400 | 143 | state++; |
muyiq | 0:b829f4597400 | 144 | } |
muyiq | 0:b829f4597400 | 145 | break; |
muyiq | 0:b829f4597400 | 146 | } |
muyiq | 0:b829f4597400 | 147 | case 1: |
muyiq | 0:b829f4597400 | 148 | case 2: { |
muyiq | 0:b829f4597400 | 149 | resp=OKResponse(buffer,"OK"); |
muyiq | 0:b829f4597400 | 150 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 151 | dev.printf("AT\r\n"); |
muyiq | 0:b829f4597400 | 152 | state++; |
muyiq | 0:b829f4597400 | 153 | } |
muyiq | 0:b829f4597400 | 154 | break; |
muyiq | 0:b829f4597400 | 155 | } |
muyiq | 0:b829f4597400 | 156 | case 3: { |
muyiq | 0:b829f4597400 | 157 | resp=OKResponse(buffer,"OK"); |
muyiq | 0:b829f4597400 | 158 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 159 | dev.printf("AT+RST\r\n"); |
muyiq | 0:b829f4597400 | 160 | state++; |
muyiq | 0:b829f4597400 | 161 | } |
muyiq | 0:b829f4597400 | 162 | |
muyiq | 0:b829f4597400 | 163 | break; |
muyiq | 0:b829f4597400 | 164 | } |
muyiq | 0:b829f4597400 | 165 | case 4: { |
muyiq | 0:b829f4597400 | 166 | resp=OKResponse(buffer,"WIFI GOT IP"); |
muyiq | 0:b829f4597400 | 167 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 168 | dev.printf("AT+CWMODE=1\r\n"); |
muyiq | 0:b829f4597400 | 169 | state++; |
muyiq | 0:b829f4597400 | 170 | } |
muyiq | 0:b829f4597400 | 171 | |
muyiq | 0:b829f4597400 | 172 | break; |
muyiq | 0:b829f4597400 | 173 | } |
muyiq | 0:b829f4597400 | 174 | case 5: { |
muyiq | 0:b829f4597400 | 175 | resp=OKResponse(buffer,"OK"); |
muyiq | 0:b829f4597400 | 176 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 177 | dev.printf("AT+CWJAP=\"BTHub3-WXWX\",\"fdd6f7c682\"\r\n"); |
muyiq | 0:b829f4597400 | 178 | state++; |
muyiq | 0:b829f4597400 | 179 | } |
muyiq | 0:b829f4597400 | 180 | |
muyiq | 0:b829f4597400 | 181 | break; |
muyiq | 0:b829f4597400 | 182 | } |
muyiq | 0:b829f4597400 | 183 | case 6: { |
muyiq | 0:b829f4597400 | 184 | resp=OKResponse(buffer,"OK"); |
muyiq | 0:b829f4597400 | 185 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 186 | wait(1); |
muyiq | 0:b829f4597400 | 187 | dev.printf("AT+CIFSR\r\n"); |
muyiq | 0:b829f4597400 | 188 | state++; |
muyiq | 0:b829f4597400 | 189 | } |
muyiq | 0:b829f4597400 | 190 | |
muyiq | 0:b829f4597400 | 191 | break; |
muyiq | 0:b829f4597400 | 192 | } |
muyiq | 0:b829f4597400 | 193 | case 7: { |
muyiq | 0:b829f4597400 | 194 | resp=OKResponse(buffer,"+CIFSR:STAIP,"); |
muyiq | 0:b829f4597400 | 195 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 196 | char *strt = strtok(buffer,"\""); |
muyiq | 0:b829f4597400 | 197 | strcpy(ipAddress,strtok(NULL,"\"")); |
muyiq | 0:b829f4597400 | 198 | strtok(NULL,"\""); |
muyiq | 0:b829f4597400 | 199 | strcpy(macAddress,strtok(NULL,"\"")); |
muyiq | 0:b829f4597400 | 200 | INFO("mac Address = %s", macAddress); |
muyiq | 0:b829f4597400 | 201 | INFO("IP Address = %s", ipAddress); |
muyiq | 0:b829f4597400 | 202 | dev.printf("AT+CIPMUX=1\r\n"); |
muyiq | 0:b829f4597400 | 203 | state=20; |
muyiq | 0:b829f4597400 | 204 | } |
muyiq | 0:b829f4597400 | 205 | |
muyiq | 0:b829f4597400 | 206 | break; |
muyiq | 0:b829f4597400 | 207 | } |
muyiq | 0:b829f4597400 | 208 | case 8: { |
muyiq | 0:b829f4597400 | 209 | resp=OKResponse(buffer,"OK"); |
muyiq | 0:b829f4597400 | 210 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 211 | INFO("Ready"); |
muyiq | 0:b829f4597400 | 212 | dev.printf("AT+CIPSERVER=1,8080\r\n"); |
muyiq | 0:b829f4597400 | 213 | state++; |
muyiq | 0:b829f4597400 | 214 | |
muyiq | 0:b829f4597400 | 215 | } |
muyiq | 0:b829f4597400 | 216 | |
muyiq | 0:b829f4597400 | 217 | } |
muyiq | 0:b829f4597400 | 218 | /* |
muyiq | 0:b829f4597400 | 219 | case 9: { |
muyiq | 0:b829f4597400 | 220 | resp=OKResponse(buffer,"OK"); |
muyiq | 0:b829f4597400 | 221 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 222 | INFO("Ready"); |
muyiq | 0:b829f4597400 | 223 | strtok(NULL,"\""); |
muyiq | 0:b829f4597400 | 224 | strcpy(macAddress,strtok(NULL,"\"")); |
muyiq | 0:b829f4597400 | 225 | INFO("mac Address = %s", macAddress); |
muyiq | 0:b829f4597400 | 226 | INFO("IP Address = %s", ipAddress); |
muyiq | 0:b829f4597400 | 227 | dev.printf("AT+CIPMUX=1\r\n"); |
muyiq | 0:b829f4597400 | 228 | state++; |
muyiq | 0:b829f4597400 | 229 | state++; |
muyiq | 0:b829f4597400 | 230 | dev.printf("AT+CIPSEND=4,10\r\n"); |
muyiq | 0:b829f4597400 | 231 | } |
muyiq | 0:b829f4597400 | 232 | |
muyiq | 0:b829f4597400 | 233 | break; |
muyiq | 0:b829f4597400 | 234 | } |
muyiq | 0:b829f4597400 | 235 | // case 10: { |
muyiq | 0:b829f4597400 | 236 | // resp=OKResponse(buffer,">"); |
muyiq | 0:b829f4597400 | 237 | // if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 238 | // INFO("SENDING"); |
muyiq | 0:b829f4597400 | 239 | // state++; |
muyiq | 0:b829f4597400 | 240 | // touch=tsi.readPercentage(); |
muyiq | 0:b829f4597400 | 241 | // dev.printf("TSI=%1.2f\n\r",touch); |
muyiq | 0:b829f4597400 | 242 | // pc.printf(" ############ tsi=%1.2f\n\r",touch); |
muyiq | 0:b829f4597400 | 243 | //INFO("sensor=%1.2f",float)tsi.readPercentage()); |
muyiq | 0:b829f4597400 | 244 | } |
muyiq | 0:b829f4597400 | 245 | case 11: { |
muyiq | 0:b829f4597400 | 246 | resp=OKResponse(buffer,"SEND OK"); |
muyiq | 0:b829f4597400 | 247 | if (resp!=NULL) { |
muyiq | 0:b829f4597400 | 248 | INFO("SEND OK"); |
muyiq | 0:b829f4597400 | 249 | // state=9; |
muyiq | 0:b829f4597400 | 250 | //wait(2); |
muyiq | 0:b829f4597400 | 251 | } |
muyiq | 0:b829f4597400 | 252 | wait(1); |
muyiq | 0:b829f4597400 | 253 | // state=9; |
muyiq | 0:b829f4597400 | 254 | } |
muyiq | 0:b829f4597400 | 255 | |
muyiq | 0:b829f4597400 | 256 | |
muyiq | 0:b829f4597400 | 257 | */ |
muyiq | 0:b829f4597400 | 258 | break; |
muyiq | 0:b829f4597400 | 259 | } |
muyiq | 0:b829f4597400 | 260 | |
muyiq | 0:b829f4597400 | 261 | |
muyiq | 0:b829f4597400 | 262 | } |
muyiq | 0:b829f4597400 | 263 | } |
muyiq | 0:b829f4597400 | 264 | //__WFI(); // DELETED AS IT KILLS THE KL25 RESPONSIVITY |
muyiq | 0:b829f4597400 | 265 | } |
muyiq | 0:b829f4597400 | 266 | //} |