Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of MBED_SERVER_SETUP by
main.cpp@6:01a64a6bc702, 2016-06-17 (annotated)
- Committer:
- muyiq
- Date:
- Fri Jun 17 12:07:55 2016 +0000
- Revision:
- 6:01a64a6bc702
- Parent:
- 5:1056f0dfb3ba
- Child:
- 7:358aa0eeafdb
ty
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 | 6:01a64a6bc702 | 51 | char pressup_channel[1]; |
| muyiq | 6:01a64a6bc702 | 52 | char dummy[4]; |
| muyiq | 6:01a64a6bc702 | 53 | char *important_number; |
| muyiq | 0:b829f4597400 | 54 | char *buffer; |
| muyiq | 0:b829f4597400 | 55 | unsigned int bufferPnt=0; |
| muyiq | 0:b829f4597400 | 56 | |
| muyiq | 0:b829f4597400 | 57 | void dev_recv() |
| muyiq | 0:b829f4597400 | 58 | { |
| muyiq | 0:b829f4597400 | 59 | char c; |
| muyiq | 0:b829f4597400 | 60 | |
| muyiq | 0:b829f4597400 | 61 | int count = 0; |
| muyiq | 0:b829f4597400 | 62 | led1 = !led1; |
| muyiq | 0:b829f4597400 | 63 | if(bufferPnt==0) { |
| muyiq | 0:b829f4597400 | 64 | memset(buffer,0,BUFF_SIZE); |
| muyiq | 0:b829f4597400 | 65 | } |
| muyiq | 0:b829f4597400 | 66 | while(dev.readable()) { |
| muyiq | 0:b829f4597400 | 67 | c = (char)dev.getc(); |
| muyiq | 0:b829f4597400 | 68 | #ifdef DEBUG |
| muyiq | 0:b829f4597400 | 69 | pc.putc(c); |
| muyiq | 0:b829f4597400 | 70 | #endif |
| muyiq | 0:b829f4597400 | 71 | buffer[bufferPnt]=c; |
| muyiq | 0:b829f4597400 | 72 | bufferPnt++; |
| muyiq | 0:b829f4597400 | 73 | if (bufferPnt>1000) { |
| muyiq | 0:b829f4597400 | 74 | ready=1; |
| muyiq | 0:b829f4597400 | 75 | } |
| muyiq | 0:b829f4597400 | 76 | // if ((c==0x0a)||(c==0x0d)){ |
| muyiq | 0:b829f4597400 | 77 | // ready=1; |
| muyiq | 0:b829f4597400 | 78 | // }else |
| muyiq | 0:b829f4597400 | 79 | if (c==0x0a) { |
| muyiq | 0:b829f4597400 | 80 | if (bufferPnt>1) { |
| muyiq | 0:b829f4597400 | 81 | if (buffer[bufferPnt -2]==0x0d) { |
| muyiq | 0:b829f4597400 | 82 | ready=1; |
| muyiq | 0:b829f4597400 | 83 | break; |
| muyiq | 0:b829f4597400 | 84 | } |
| muyiq | 0:b829f4597400 | 85 | } |
| muyiq | 0:b829f4597400 | 86 | } |
| muyiq | 0:b829f4597400 | 87 | if (!dev.readable()) { |
| muyiq | 0:b829f4597400 | 88 | wait_us(10); |
| muyiq | 0:b829f4597400 | 89 | } |
| muyiq | 0:b829f4597400 | 90 | } |
| muyiq | 0:b829f4597400 | 91 | } |
| muyiq | 0:b829f4597400 | 92 | |
| muyiq | 0:b829f4597400 | 93 | void pc_recv() |
| muyiq | 0:b829f4597400 | 94 | { |
| muyiq | 0:b829f4597400 | 95 | char c; |
| muyiq | 0:b829f4597400 | 96 | led4 = !led4; |
| muyiq | 0:b829f4597400 | 97 | while(pc.readable()) { |
| muyiq | 0:b829f4597400 | 98 | c=(char)pc.getc(); |
| muyiq | 0:b829f4597400 | 99 | dev.putc(c); |
| muyiq | 0:b829f4597400 | 100 | pc.putc(c); |
| muyiq | 0:b829f4597400 | 101 | if(c==13) { |
| muyiq | 0:b829f4597400 | 102 | dev.putc(10); |
| muyiq | 0:b829f4597400 | 103 | pc.putc(10); |
| muyiq | 0:b829f4597400 | 104 | } |
| muyiq | 0:b829f4597400 | 105 | |
| muyiq | 0:b829f4597400 | 106 | } |
| muyiq | 0:b829f4597400 | 107 | } |
| muyiq | 0:b829f4597400 | 108 | |
| muyiq | 0:b829f4597400 | 109 | char * OKResponse(char *test, const char *pattern) |
| muyiq | 0:b829f4597400 | 110 | { |
| muyiq | 0:b829f4597400 | 111 | char *p= strstr(test,pattern); |
| muyiq | 0:b829f4597400 | 112 | if (p==NULL) { |
| muyiq | 0:b829f4597400 | 113 | // DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p); |
| muyiq | 0:b829f4597400 | 114 | return NULL; |
| muyiq | 0:b829f4597400 | 115 | } else { |
| muyiq | 0:b829f4597400 | 116 | // DBG("YAY Test=<%s> Patter=<%s> [p=%s]",test,pattern,p); |
| muyiq | 0:b829f4597400 | 117 | } |
| muyiq | 0:b829f4597400 | 118 | return p; |
| muyiq | 0:b829f4597400 | 119 | } |
| muyiq | 0:b829f4597400 | 120 | int main() |
| muyiq | 0:b829f4597400 | 121 | { |
| muyiq | 0:b829f4597400 | 122 | float touch; |
| muyiq | 0:b829f4597400 | 123 | buffer=(char *)calloc(BUFF_SIZE,1); |
| muyiq | 0:b829f4597400 | 124 | reset=0; |
| muyiq | 0:b829f4597400 | 125 | wait(3.0); |
| muyiq | 0:b829f4597400 | 126 | reset=1; |
| muyiq | 0:b829f4597400 | 127 | int counter=0; |
| muyiq | 0:b829f4597400 | 128 | pc.baud(115200); |
| muyiq | 0:b829f4597400 | 129 | dev.baud(115200); |
| muyiq | 0:b829f4597400 | 130 | pc.attach(&pc_recv, Serial::RxIrq); |
| muyiq | 0:b829f4597400 | 131 | dev.attach(&dev_recv, Serial::RxIrq); |
| muyiq | 0:b829f4597400 | 132 | pc.printf("Start up\n\r"); |
| muyiq | 0:b829f4597400 | 133 | char * resp=NULL; |
| muyiq | 2:49b2668be97c | 134 | char *dummy; |
| muyiq | 2:49b2668be97c | 135 | |
| muyiq | 0:b829f4597400 | 136 | pc.printf("Here \n\r"); |
| muyiq | 0:b829f4597400 | 137 | while(1) { |
| muyiq | 0:b829f4597400 | 138 | if (ready) { |
| muyiq | 0:b829f4597400 | 139 | ready=0; |
| muyiq | 0:b829f4597400 | 140 | bufferPnt=0; |
| muyiq | 0:b829f4597400 | 141 | INFO("[%d],##%s##",state,buffer); |
| muyiq | 0:b829f4597400 | 142 | switch (state) { |
| muyiq | 0:b829f4597400 | 143 | case 0: { |
| muyiq | 0:b829f4597400 | 144 | resp=OKResponse(buffer,"WIFI GOT IP"); |
| muyiq | 0:b829f4597400 | 145 | if (resp!=NULL) { |
| muyiq | 0:b829f4597400 | 146 | wait(1); |
| muyiq | 0:b829f4597400 | 147 | dev.printf("AT\r\n"); |
| muyiq | 0:b829f4597400 | 148 | state++; |
| muyiq | 0:b829f4597400 | 149 | } |
| muyiq | 0:b829f4597400 | 150 | break; |
| muyiq | 0:b829f4597400 | 151 | } |
| muyiq | 0:b829f4597400 | 152 | case 1: |
| muyiq | 0:b829f4597400 | 153 | case 2: { |
| muyiq | 0:b829f4597400 | 154 | resp=OKResponse(buffer,"OK"); |
| muyiq | 0:b829f4597400 | 155 | if (resp!=NULL) { |
| muyiq | 0:b829f4597400 | 156 | dev.printf("AT\r\n"); |
| muyiq | 0:b829f4597400 | 157 | state++; |
| muyiq | 0:b829f4597400 | 158 | } |
| muyiq | 0:b829f4597400 | 159 | break; |
| muyiq | 0:b829f4597400 | 160 | } |
| muyiq | 0:b829f4597400 | 161 | case 3: { |
| muyiq | 0:b829f4597400 | 162 | resp=OKResponse(buffer,"OK"); |
| muyiq | 0:b829f4597400 | 163 | if (resp!=NULL) { |
| muyiq | 0:b829f4597400 | 164 | dev.printf("AT+RST\r\n"); |
| muyiq | 0:b829f4597400 | 165 | state++; |
| muyiq | 0:b829f4597400 | 166 | } |
| muyiq | 0:b829f4597400 | 167 | |
| muyiq | 0:b829f4597400 | 168 | break; |
| muyiq | 0:b829f4597400 | 169 | } |
| muyiq | 0:b829f4597400 | 170 | case 4: { |
| muyiq | 0:b829f4597400 | 171 | resp=OKResponse(buffer,"WIFI GOT IP"); |
| muyiq | 0:b829f4597400 | 172 | if (resp!=NULL) { |
| muyiq | 0:b829f4597400 | 173 | dev.printf("AT+CWMODE=1\r\n"); |
| muyiq | 0:b829f4597400 | 174 | state++; |
| muyiq | 0:b829f4597400 | 175 | } |
| muyiq | 0:b829f4597400 | 176 | |
| muyiq | 0:b829f4597400 | 177 | break; |
| muyiq | 0:b829f4597400 | 178 | } |
| muyiq | 0:b829f4597400 | 179 | case 5: { |
| muyiq | 0:b829f4597400 | 180 | resp=OKResponse(buffer,"OK"); |
| muyiq | 0:b829f4597400 | 181 | if (resp!=NULL) { |
| muyiq | 0:b829f4597400 | 182 | dev.printf("AT+CWJAP=\"BTHub3-WXWX\",\"fdd6f7c682\"\r\n"); |
| muyiq | 0:b829f4597400 | 183 | state++; |
| muyiq | 0:b829f4597400 | 184 | } |
| muyiq | 0:b829f4597400 | 185 | |
| muyiq | 0:b829f4597400 | 186 | break; |
| muyiq | 0:b829f4597400 | 187 | } |
| muyiq | 0:b829f4597400 | 188 | case 6: { |
| muyiq | 0:b829f4597400 | 189 | resp=OKResponse(buffer,"OK"); |
| muyiq | 0:b829f4597400 | 190 | if (resp!=NULL) { |
| muyiq | 0:b829f4597400 | 191 | wait(1); |
| muyiq | 0:b829f4597400 | 192 | dev.printf("AT+CIFSR\r\n"); |
| muyiq | 0:b829f4597400 | 193 | state++; |
| muyiq | 0:b829f4597400 | 194 | } |
| muyiq | 0:b829f4597400 | 195 | |
| muyiq | 0:b829f4597400 | 196 | break; |
| muyiq | 0:b829f4597400 | 197 | } |
| muyiq | 0:b829f4597400 | 198 | case 7: { |
| muyiq | 0:b829f4597400 | 199 | resp=OKResponse(buffer,"+CIFSR:STAIP,"); |
| muyiq | 0:b829f4597400 | 200 | if (resp!=NULL) { |
| muyiq | 0:b829f4597400 | 201 | char *strt = strtok(buffer,"\""); |
| muyiq | 0:b829f4597400 | 202 | strcpy(ipAddress,strtok(NULL,"\"")); |
| muyiq | 0:b829f4597400 | 203 | strtok(NULL,"\""); |
| muyiq | 0:b829f4597400 | 204 | strcpy(macAddress,strtok(NULL,"\"")); |
| muyiq | 0:b829f4597400 | 205 | INFO("mac Address = %s", macAddress); |
| muyiq | 0:b829f4597400 | 206 | INFO("IP Address = %s", ipAddress); |
| muyiq | 0:b829f4597400 | 207 | dev.printf("AT+CIPMUX=1\r\n"); |
| muyiq | 2:49b2668be97c | 208 | state++; |
| muyiq | 0:b829f4597400 | 209 | } |
| muyiq | 0:b829f4597400 | 210 | |
| muyiq | 0:b829f4597400 | 211 | break; |
| muyiq | 0:b829f4597400 | 212 | } |
| muyiq | 2:49b2668be97c | 213 | case 8: { //initialize server |
| muyiq | 2:49b2668be97c | 214 | resp=OKResponse(buffer,"OK"); |
| muyiq | 0:b829f4597400 | 215 | if (resp!=NULL) { |
| muyiq | 0:b829f4597400 | 216 | INFO("Ready"); |
| muyiq | 0:b829f4597400 | 217 | dev.printf("AT+CIPSERVER=1,8080\r\n"); |
| muyiq | 0:b829f4597400 | 218 | state++; |
| muyiq | 0:b829f4597400 | 219 | } |
| muyiq | 0:b829f4597400 | 220 | } |
| muyiq | 2:49b2668be97c | 221 | break; |
| muyiq | 2:49b2668be97c | 222 | case 9: { //gets pedometer channel |
| muyiq | 6:01a64a6bc702 | 223 | resp=OKResponse(buffer,"PRESS_UP1"); |
| muyiq | 0:b829f4597400 | 224 | if (resp!=NULL) { |
| muyiq | 6:01a64a6bc702 | 225 | INFO("Finding pressup channel2"); |
| muyiq | 2:49b2668be97c | 226 | char *strt = strtok(buffer,","); |
| muyiq | 6:01a64a6bc702 | 227 | strcpy(pressup_channel,strtok(NULL,",")); |
| muyiq | 2:49b2668be97c | 228 | strtok(NULL,","); |
| muyiq | 2:49b2668be97c | 229 | strcpy(macAddress,strtok(NULL,",")); |
| muyiq | 3:8534d8489cbc | 230 | INFO("filler = %s", macAddress); |
| muyiq | 6:01a64a6bc702 | 231 | INFO("pedometer_channel = %s", pressup_channel); |
| muyiq | 0:b829f4597400 | 232 | state++; |
| muyiq | 2:49b2668be97c | 233 | } |
| muyiq | 2:49b2668be97c | 234 | } |
| muyiq | 2:49b2668be97c | 235 | |
| muyiq | 2:49b2668be97c | 236 | case 10: { |
| muyiq | 2:49b2668be97c | 237 | if (resp!=NULL) { |
| muyiq | 2:49b2668be97c | 238 | INFO("Testing connection"); |
| muyiq | 0:b829f4597400 | 239 | state++; |
| muyiq | 6:01a64a6bc702 | 240 | dev.printf("AT+CIPSEND=%s,6\r\n",pressup_channel); |
| muyiq | 0:b829f4597400 | 241 | } |
| muyiq | 0:b829f4597400 | 242 | |
| muyiq | 0:b829f4597400 | 243 | break; |
| muyiq | 0:b829f4597400 | 244 | } |
| muyiq | 2:49b2668be97c | 245 | case 11: { |
| muyiq | 2:49b2668be97c | 246 | resp=OKResponse(buffer,">"); |
| muyiq | 0:b829f4597400 | 247 | if (resp!=NULL) { |
| muyiq | 2:49b2668be97c | 248 | INFO("SENDING"); |
| muyiq | 2:49b2668be97c | 249 | state++; |
| muyiq | 2:49b2668be97c | 250 | dev.printf("Hi ed!\r\n"); |
| muyiq | 2:49b2668be97c | 251 | } |
| muyiq | 2:49b2668be97c | 252 | } |
| muyiq | 6:01a64a6bc702 | 253 | case 12: { |
| muyiq | 6:01a64a6bc702 | 254 | resp=OKResponse(buffer,"PED"); |
| muyiq | 6:01a64a6bc702 | 255 | INFO("hello %s",resp); |
| muyiq | 6:01a64a6bc702 | 256 | if (resp!=NULL) { |
| muyiq | 6:01a64a6bc702 | 257 | pc.printf("getting important number..."); |
| muyiq | 6:01a64a6bc702 | 258 | const char s[2] = ":"; |
| muyiq | 6:01a64a6bc702 | 259 | char *token; |
| muyiq | 6:01a64a6bc702 | 260 | /* get the first token */ |
| muyiq | 6:01a64a6bc702 | 261 | token = strtok(resp, s); |
| muyiq | 6:01a64a6bc702 | 262 | token = strtok(NULL, s); |
| muyiq | 6:01a64a6bc702 | 263 | important_number=token; |
| muyiq | 6:01a64a6bc702 | 264 | pc.printf("important number %s\r\n",important_number); |
| muyiq | 6:01a64a6bc702 | 265 | } |
| muyiq | 6:01a64a6bc702 | 266 | } |
| muyiq | 0:b829f4597400 | 267 | break; |
| muyiq | 6:01a64a6bc702 | 268 | |
| muyiq | 0:b829f4597400 | 269 | } |
| muyiq | 0:b829f4597400 | 270 | |
| muyiq | 0:b829f4597400 | 271 | |
| muyiq | 0:b829f4597400 | 272 | } |
| muyiq | 0:b829f4597400 | 273 | } |
| muyiq | 0:b829f4597400 | 274 | //__WFI(); // DELETED AS IT KILLS THE KL25 RESPONSIVITY |
| muyiq | 0:b829f4597400 | 275 | } |
| muyiq | 0:b829f4597400 | 276 | //} |
