working code

Dependencies:   mbed

Fork of Working_Client_Code by iX

Committer:
jcochra
Date:
Thu Jun 09 09:55:10 2016 +0000
Revision:
3:c99ef21cb4b3
Parent:
1:e2385bba1ab1
Child:
4:d205b6061e66
SERVER

Who changed what in which revision?

UserRevisionLine numberNew contents of line
epgmdm 0:7176e20dbdf6 1 #include "mbed.h"
epgmdm 0:7176e20dbdf6 2
epgmdm 0:7176e20dbdf6 3 #define DEBUG
epgmdm 0:7176e20dbdf6 4 #define INFOMESSAGES
epgmdm 0:7176e20dbdf6 5 #define WARNMESSAGES
epgmdm 0:7176e20dbdf6 6 #define ERRMESSAGES
epgmdm 0:7176e20dbdf6 7
epgmdm 0:7176e20dbdf6 8 #define FUNCNAME "MAIN"
epgmdm 0:7176e20dbdf6 9
epgmdm 1:e2385bba1ab1 10 #ifdef NoDEBUG
epgmdm 0:7176e20dbdf6 11 #define DBG(x, ...) pc.printf(" ["FUNCNAME" : DBG] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__);
epgmdm 0:7176e20dbdf6 12 #else
epgmdm 0:7176e20dbdf6 13 #define DBG(x, ...)
epgmdm 0:7176e20dbdf6 14 #endif
epgmdm 0:7176e20dbdf6 15
epgmdm 0:7176e20dbdf6 16 #ifdef ERRMESSAGES
epgmdm 0:7176e20dbdf6 17 #define ERR(x, ...) pc.printf(" ["FUNCNAME" : ERR] "x"\r\n", ##__VA_ARGS__);
epgmdm 0:7176e20dbdf6 18 #else
epgmdm 0:7176e20dbdf6 19 #define ERR(x, ...)
epgmdm 0:7176e20dbdf6 20 #endif
epgmdm 0:7176e20dbdf6 21
epgmdm 0:7176e20dbdf6 22 #ifdef WARNMESSAGES
epgmdm 0:7176e20dbdf6 23 #define WARN(x, ...) printf("["FUNCNAME" : WARN] "x"\r\n", ##__VA_ARGS__);
epgmdm 0:7176e20dbdf6 24 #else
epgmdm 0:7176e20dbdf6 25 #define WARN(x, ...)
epgmdm 0:7176e20dbdf6 26 #endif
epgmdm 0:7176e20dbdf6 27
epgmdm 0:7176e20dbdf6 28 #ifdef INFOMESSAGES
epgmdm 1:e2385bba1ab1 29 #define INFO(x, ...) pc.printf("["FUNCNAME" : INFO] "x"\r\n", ##__VA_ARGS__);
epgmdm 0:7176e20dbdf6 30 #else
epgmdm 0:7176e20dbdf6 31 #define INFO(x, ...)
epgmdm 0:7176e20dbdf6 32 #endif
epgmdm 0:7176e20dbdf6 33
epgmdm 0:7176e20dbdf6 34 #define BUFF_SIZE 2048
epgmdm 0:7176e20dbdf6 35 RawSerial pc(USBTX, USBRX);
epgmdm 0:7176e20dbdf6 36 //RawSerial dev(D1, D0);
jcochra 3:c99ef21cb4b3 37 RawSerial dev(p9, p10); //tx,rx
epgmdm 0:7176e20dbdf6 38 DigitalOut led1(LED1);
epgmdm 0:7176e20dbdf6 39 DigitalOut led4(LED4);
jcochra 3:c99ef21cb4b3 40 DigitalOut reset(p21,1);
epgmdm 0:7176e20dbdf6 41 volatile int state=0;
epgmdm 0:7176e20dbdf6 42 volatile int ready=0;
epgmdm 1:e2385bba1ab1 43
epgmdm 1:e2385bba1ab1 44 char ipAddress[20];
epgmdm 1:e2385bba1ab1 45 char macAddress[32];
epgmdm 0:7176e20dbdf6 46 char *buffer;
epgmdm 0:7176e20dbdf6 47 unsigned int bufferPnt=0;
epgmdm 0:7176e20dbdf6 48
epgmdm 0:7176e20dbdf6 49 void dev_recv()
epgmdm 0:7176e20dbdf6 50 {
epgmdm 0:7176e20dbdf6 51 char c;
epgmdm 0:7176e20dbdf6 52
epgmdm 0:7176e20dbdf6 53 int count = 0;
epgmdm 0:7176e20dbdf6 54 led1 = !led1;
epgmdm 0:7176e20dbdf6 55 if(bufferPnt==0) {
epgmdm 0:7176e20dbdf6 56 memset(buffer,0,BUFF_SIZE);
epgmdm 0:7176e20dbdf6 57 }
epgmdm 0:7176e20dbdf6 58 while(dev.readable()) {
epgmdm 0:7176e20dbdf6 59 c = (char)dev.getc();
epgmdm 1:e2385bba1ab1 60 #ifdef DEBUG
jcochra 3:c99ef21cb4b3 61 pc.putc(c);
epgmdm 1:e2385bba1ab1 62 #endif
epgmdm 0:7176e20dbdf6 63 buffer[bufferPnt]=c;
epgmdm 0:7176e20dbdf6 64 bufferPnt++;
epgmdm 1:e2385bba1ab1 65 if (bufferPnt>1024) {
epgmdm 0:7176e20dbdf6 66 ready=1;
epgmdm 1:e2385bba1ab1 67 }
jcochra 3:c99ef21cb4b3 68 // if ((c==0x0a)||(c==0x0d)){
jcochra 3:c99ef21cb4b3 69 // ready=1;
jcochra 3:c99ef21cb4b3 70 // }else
epgmdm 0:7176e20dbdf6 71 if (c==0x0a) {
epgmdm 0:7176e20dbdf6 72 if (bufferPnt>1) {
epgmdm 0:7176e20dbdf6 73 if (buffer[bufferPnt -2]==0x0d) {
epgmdm 0:7176e20dbdf6 74 ready=1;
epgmdm 0:7176e20dbdf6 75 break;
epgmdm 0:7176e20dbdf6 76 }
epgmdm 0:7176e20dbdf6 77 }
epgmdm 0:7176e20dbdf6 78 }
epgmdm 1:e2385bba1ab1 79 if (!dev.readable()) {
epgmdm 1:e2385bba1ab1 80 wait_us(100);
epgmdm 0:7176e20dbdf6 81 }
epgmdm 0:7176e20dbdf6 82 }
epgmdm 0:7176e20dbdf6 83 }
epgmdm 0:7176e20dbdf6 84
epgmdm 0:7176e20dbdf6 85 void pc_recv()
epgmdm 0:7176e20dbdf6 86 {
epgmdm 0:7176e20dbdf6 87 char c;
epgmdm 0:7176e20dbdf6 88 led4 = !led4;
epgmdm 0:7176e20dbdf6 89 while(pc.readable()) {
epgmdm 0:7176e20dbdf6 90 c=(char)pc.getc();
epgmdm 0:7176e20dbdf6 91 dev.putc(c);
epgmdm 0:7176e20dbdf6 92 pc.putc(c);
jcochra 3:c99ef21cb4b3 93 if(c==13) {
jcochra 3:c99ef21cb4b3 94 dev.putc(10);
jcochra 3:c99ef21cb4b3 95 pc.putc(10);
jcochra 3:c99ef21cb4b3 96 }
epgmdm 0:7176e20dbdf6 97
epgmdm 0:7176e20dbdf6 98 }
epgmdm 0:7176e20dbdf6 99 }
epgmdm 0:7176e20dbdf6 100
epgmdm 0:7176e20dbdf6 101 char * OKResponse(char *test, const char *pattern)
epgmdm 0:7176e20dbdf6 102 {
epgmdm 0:7176e20dbdf6 103 char *p= strstr(test,pattern);
epgmdm 0:7176e20dbdf6 104 if (p==NULL) {
epgmdm 0:7176e20dbdf6 105 // DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
epgmdm 0:7176e20dbdf6 106 return NULL;
epgmdm 0:7176e20dbdf6 107 } else {
epgmdm 0:7176e20dbdf6 108 // DBG("YAY Test=<%s> Patter=<%s> [p=%s]",test,pattern,p);
epgmdm 0:7176e20dbdf6 109 }
epgmdm 0:7176e20dbdf6 110 return p;
epgmdm 0:7176e20dbdf6 111 }
epgmdm 0:7176e20dbdf6 112 int main()
epgmdm 0:7176e20dbdf6 113 {
epgmdm 0:7176e20dbdf6 114 buffer=(char *)calloc(BUFF_SIZE,1);
epgmdm 0:7176e20dbdf6 115 reset=0;
epgmdm 0:7176e20dbdf6 116
epgmdm 0:7176e20dbdf6 117 pc.baud(115200);
epgmdm 0:7176e20dbdf6 118 dev.baud(115200);
epgmdm 0:7176e20dbdf6 119 pc.attach(&pc_recv, Serial::RxIrq);
epgmdm 0:7176e20dbdf6 120 dev.attach(&dev_recv, Serial::RxIrq);
jcochra 3:c99ef21cb4b3 121 pc.printf("Start up 3\n\r");
epgmdm 0:7176e20dbdf6 122 wait(1.5);
epgmdm 0:7176e20dbdf6 123 reset=1;
epgmdm 0:7176e20dbdf6 124 char * resp=NULL;
epgmdm 0:7176e20dbdf6 125 pc.printf("Here \n\r");
epgmdm 0:7176e20dbdf6 126 while(1) {
epgmdm 0:7176e20dbdf6 127 if (ready) {
epgmdm 0:7176e20dbdf6 128 ready=0;
epgmdm 0:7176e20dbdf6 129 bufferPnt=0;
epgmdm 1:e2385bba1ab1 130 INFO("[%d]",state);
epgmdm 0:7176e20dbdf6 131 switch (state) {
epgmdm 0:7176e20dbdf6 132 case 0: {
epgmdm 0:7176e20dbdf6 133 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 134 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 135 wait(1);
epgmdm 0:7176e20dbdf6 136 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 137 state++;
epgmdm 0:7176e20dbdf6 138 }
epgmdm 0:7176e20dbdf6 139 break;
epgmdm 0:7176e20dbdf6 140 }
epgmdm 0:7176e20dbdf6 141 case 1:
epgmdm 0:7176e20dbdf6 142 case 2: {
epgmdm 0:7176e20dbdf6 143 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 144 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 145 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 146 state++;
epgmdm 0:7176e20dbdf6 147 }
epgmdm 0:7176e20dbdf6 148 break;
epgmdm 0:7176e20dbdf6 149 }
epgmdm 0:7176e20dbdf6 150 case 3: {
epgmdm 0:7176e20dbdf6 151 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 152 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 153 dev.printf("AT+RST\r\n");
epgmdm 0:7176e20dbdf6 154 state++;
epgmdm 0:7176e20dbdf6 155 }
epgmdm 0:7176e20dbdf6 156
epgmdm 0:7176e20dbdf6 157 break;
epgmdm 0:7176e20dbdf6 158 }
epgmdm 0:7176e20dbdf6 159 case 4: {
epgmdm 0:7176e20dbdf6 160 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 161 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 162 dev.printf("AT+CWMODE=1\r\n");
epgmdm 0:7176e20dbdf6 163 state++;
epgmdm 0:7176e20dbdf6 164 }
epgmdm 0:7176e20dbdf6 165
epgmdm 0:7176e20dbdf6 166 break;
epgmdm 0:7176e20dbdf6 167 }
epgmdm 0:7176e20dbdf6 168 case 5: {
epgmdm 0:7176e20dbdf6 169 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 170 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 171
epgmdm 0:7176e20dbdf6 172 dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
epgmdm 0:7176e20dbdf6 173 state++;
epgmdm 0:7176e20dbdf6 174 }
epgmdm 0:7176e20dbdf6 175
epgmdm 0:7176e20dbdf6 176 break;
epgmdm 0:7176e20dbdf6 177 }
epgmdm 0:7176e20dbdf6 178 case 6: {
epgmdm 0:7176e20dbdf6 179 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 180 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 181 wait(1);
epgmdm 0:7176e20dbdf6 182 dev.printf("AT+CIFSR\r\n");
epgmdm 0:7176e20dbdf6 183 state++;
epgmdm 0:7176e20dbdf6 184 }
epgmdm 0:7176e20dbdf6 185
epgmdm 0:7176e20dbdf6 186 break;
epgmdm 0:7176e20dbdf6 187 }
epgmdm 0:7176e20dbdf6 188 case 7: {
epgmdm 1:e2385bba1ab1 189 resp=OKResponse(buffer,"+CIFSR:STAIP,");
epgmdm 0:7176e20dbdf6 190 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 191 char *strt = strtok(buffer,"\"");
epgmdm 1:e2385bba1ab1 192 strcpy(ipAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 193 strtok(NULL,"\"");
epgmdm 1:e2385bba1ab1 194 strcpy(macAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 195 INFO("mac Address = %s", macAddress);
epgmdm 1:e2385bba1ab1 196 INFO("IP Address = %s", ipAddress);
epgmdm 1:e2385bba1ab1 197 dev.printf("AT+CIPMUX=1\r\n");
epgmdm 0:7176e20dbdf6 198 state++;
epgmdm 0:7176e20dbdf6 199 }
epgmdm 0:7176e20dbdf6 200
epgmdm 0:7176e20dbdf6 201 break;
epgmdm 0:7176e20dbdf6 202 }
epgmdm 1:e2385bba1ab1 203 case 8: {
epgmdm 1:e2385bba1ab1 204 resp=OKResponse(buffer,"OK");
epgmdm 1:e2385bba1ab1 205 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 206 INFO("Ready");
jcochra 3:c99ef21cb4b3 207 dev.printf("AT+CIPSERVER= 1,6060\r\n");
jcochra 3:c99ef21cb4b3 208 //dev.printf("AT+CIPSTART=0,\"TCP\",192.168.1.8, 6060");
epgmdm 1:e2385bba1ab1 209 state++;
epgmdm 1:e2385bba1ab1 210 }
epgmdm 1:e2385bba1ab1 211
epgmdm 1:e2385bba1ab1 212 break;
epgmdm 1:e2385bba1ab1 213 }
jcochra 3:c99ef21cb4b3 214 case 9: { resp=OKResponse(buffer,"OK");
jcochra 3:c99ef21cb4b3 215 if (resp!=NULL) {
jcochra 3:c99ef21cb4b3 216 INFO("Ready");
jcochra 3:c99ef21cb4b3 217 //dev.printf("AT+CIPSERVER= 1,6060");
jcochra 3:c99ef21cb4b3 218 //dev.printf("AT+CIPSEND= 0,10"); //10 = length of string being sent
jcochra 3:c99ef21cb4b3 219 state++;
jcochra 3:c99ef21cb4b3 220 }
jcochra 3:c99ef21cb4b3 221 pc.printf("%s\n\r",buffer);
epgmdm 0:7176e20dbdf6 222
jcochra 3:c99ef21cb4b3 223 break;
epgmdm 0:7176e20dbdf6 224
epgmdm 0:7176e20dbdf6 225 }
jcochra 3:c99ef21cb4b3 226
jcochra 3:c99ef21cb4b3 227 case 10: { resp=OKResponse(buffer,"OK");
jcochra 3:c99ef21cb4b3 228 if (resp!=NULL) {
jcochra 3:c99ef21cb4b3 229 INFO("Ready");
jcochra 3:c99ef21cb4b3 230 // dev.printf("AT+CIPSERVER= 1[,6060]");
jcochra 3:c99ef21cb4b3 231 state++;
jcochra 3:c99ef21cb4b3 232 }
jcochra 3:c99ef21cb4b3 233
jcochra 3:c99ef21cb4b3 234 break;
jcochra 3:c99ef21cb4b3 235 }
jcochra 3:c99ef21cb4b3 236 }
epgmdm 0:7176e20dbdf6 237 }
epgmdm 0:7176e20dbdf6 238 __WFI();
epgmdm 0:7176e20dbdf6 239 }
jcochra 3:c99ef21cb4b3 240 }