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