working code

Dependencies:   mbed

Fork of Working_Client_Code by iX

Committer:
jcochra
Date:
Thu Jun 09 14:32:09 2016 +0000
Revision:
5:d25327fb7a14
Parent:
4:d205b6061e66
Child:
6:1c35ca876e09
3:30PM;

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 {
jcochra 5:d25327fb7a14 114 int range=3456;
jcochra 5:d25327fb7a14 115
epgmdm 0:7176e20dbdf6 116 buffer=(char *)calloc(BUFF_SIZE,1);
epgmdm 0:7176e20dbdf6 117 reset=0;
epgmdm 0:7176e20dbdf6 118
epgmdm 0:7176e20dbdf6 119 pc.baud(115200);
epgmdm 0:7176e20dbdf6 120 dev.baud(115200);
epgmdm 0:7176e20dbdf6 121 pc.attach(&pc_recv, Serial::RxIrq);
epgmdm 0:7176e20dbdf6 122 dev.attach(&dev_recv, Serial::RxIrq);
jcochra 3:c99ef21cb4b3 123 pc.printf("Start up 3\n\r");
epgmdm 0:7176e20dbdf6 124 wait(1.5);
epgmdm 0:7176e20dbdf6 125 reset=1;
epgmdm 0:7176e20dbdf6 126 char * resp=NULL;
epgmdm 0:7176e20dbdf6 127 pc.printf("Here \n\r");
epgmdm 0:7176e20dbdf6 128 while(1) {
epgmdm 0:7176e20dbdf6 129 if (ready) {
epgmdm 0:7176e20dbdf6 130 ready=0;
epgmdm 0:7176e20dbdf6 131 bufferPnt=0;
epgmdm 1:e2385bba1ab1 132 INFO("[%d]",state);
epgmdm 0:7176e20dbdf6 133 switch (state) {
epgmdm 0:7176e20dbdf6 134 case 0: {
epgmdm 0:7176e20dbdf6 135 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 136 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 137 wait(1);
epgmdm 0:7176e20dbdf6 138 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 139 state++;
epgmdm 0:7176e20dbdf6 140 }
epgmdm 0:7176e20dbdf6 141 break;
epgmdm 0:7176e20dbdf6 142 }
epgmdm 0:7176e20dbdf6 143 case 1:
epgmdm 0:7176e20dbdf6 144 case 2: {
epgmdm 0:7176e20dbdf6 145 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 146 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 147 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 148 state++;
epgmdm 0:7176e20dbdf6 149 }
epgmdm 0:7176e20dbdf6 150 break;
epgmdm 0:7176e20dbdf6 151 }
epgmdm 0:7176e20dbdf6 152 case 3: {
epgmdm 0:7176e20dbdf6 153 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 154 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 155 dev.printf("AT+RST\r\n");
epgmdm 0:7176e20dbdf6 156 state++;
epgmdm 0:7176e20dbdf6 157 }
epgmdm 0:7176e20dbdf6 158
epgmdm 0:7176e20dbdf6 159 break;
epgmdm 0:7176e20dbdf6 160 }
epgmdm 0:7176e20dbdf6 161 case 4: {
epgmdm 0:7176e20dbdf6 162 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 163 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 164 dev.printf("AT+CWMODE=1\r\n");
epgmdm 0:7176e20dbdf6 165 state++;
epgmdm 0:7176e20dbdf6 166 }
epgmdm 0:7176e20dbdf6 167
epgmdm 0:7176e20dbdf6 168 break;
epgmdm 0:7176e20dbdf6 169 }
epgmdm 0:7176e20dbdf6 170 case 5: {
epgmdm 0:7176e20dbdf6 171 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 172 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 173
epgmdm 0:7176e20dbdf6 174 dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
epgmdm 0:7176e20dbdf6 175 state++;
epgmdm 0:7176e20dbdf6 176 }
epgmdm 0:7176e20dbdf6 177
epgmdm 0:7176e20dbdf6 178 break;
epgmdm 0:7176e20dbdf6 179 }
epgmdm 0:7176e20dbdf6 180 case 6: {
epgmdm 0:7176e20dbdf6 181 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 182 if (resp!=NULL) {
jcochra 5:d25327fb7a14 183 wait(10);
epgmdm 0:7176e20dbdf6 184 dev.printf("AT+CIFSR\r\n");
epgmdm 0:7176e20dbdf6 185 state++;
epgmdm 0:7176e20dbdf6 186 }
epgmdm 0:7176e20dbdf6 187
epgmdm 0:7176e20dbdf6 188 break;
epgmdm 0:7176e20dbdf6 189 }
epgmdm 0:7176e20dbdf6 190 case 7: {
epgmdm 1:e2385bba1ab1 191 resp=OKResponse(buffer,"+CIFSR:STAIP,");
epgmdm 0:7176e20dbdf6 192 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 193 char *strt = strtok(buffer,"\"");
epgmdm 1:e2385bba1ab1 194 strcpy(ipAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 195 strtok(NULL,"\"");
epgmdm 1:e2385bba1ab1 196 strcpy(macAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 197 INFO("mac Address = %s", macAddress);
epgmdm 1:e2385bba1ab1 198 INFO("IP Address = %s", ipAddress);
epgmdm 1:e2385bba1ab1 199 dev.printf("AT+CIPMUX=1\r\n");
epgmdm 0:7176e20dbdf6 200 state++;
epgmdm 0:7176e20dbdf6 201 }
epgmdm 0:7176e20dbdf6 202
epgmdm 0:7176e20dbdf6 203 break;
epgmdm 0:7176e20dbdf6 204 }
epgmdm 1:e2385bba1ab1 205 case 8: {
epgmdm 1:e2385bba1ab1 206 resp=OKResponse(buffer,"OK");
epgmdm 1:e2385bba1ab1 207 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 208 INFO("Ready");
jcochra 4:d205b6061e66 209 //dev.printf("AT+CIPSERVER= 1,6060\r\n");
jcochra 4:d205b6061e66 210 dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.8\",6060\r\n");
epgmdm 1:e2385bba1ab1 211 state++;
epgmdm 1:e2385bba1ab1 212 }
epgmdm 1:e2385bba1ab1 213
epgmdm 1:e2385bba1ab1 214 break;
epgmdm 1:e2385bba1ab1 215 }
jcochra 3:c99ef21cb4b3 216 case 9: { resp=OKResponse(buffer,"OK");
jcochra 3:c99ef21cb4b3 217 if (resp!=NULL) {
jcochra 3:c99ef21cb4b3 218 INFO("Ready");
jcochra 5:d25327fb7a14 219 wait(2);
jcochra 5:d25327fb7a14 220 char buf[32];
jcochra 5:d25327fb7a14 221 sprintf(buf,"Range=%04d\r\n",range);
jcochra 3:c99ef21cb4b3 222 //dev.printf("AT+CIPSERVER= 1,6060");
jcochra 5:d25327fb7a14 223 dev.printf("AT+CIPSEND=0,%d\r\n",strlen(buf)); //10 = length of string being sent
jcochra 5:d25327fb7a14 224 wait(2);
jcochra 5:d25327fb7a14 225 dev.printf("%s",buf); //10 = length of string being sent
jcochra 5:d25327fb7a14 226 range=(range++)%6000;
jcochra 5:d25327fb7a14 227
jcochra 5:d25327fb7a14 228 state=9;
jcochra 3:c99ef21cb4b3 229 }
jcochra 3:c99ef21cb4b3 230 pc.printf("%s\n\r",buffer);
epgmdm 0:7176e20dbdf6 231
jcochra 3:c99ef21cb4b3 232 break;
epgmdm 0:7176e20dbdf6 233
epgmdm 0:7176e20dbdf6 234 }
jcochra 5:d25327fb7a14 235 case 10: { resp=OKResponse(buffer,">");
jcochra 5:d25327fb7a14 236 if (resp!=NULL) {
jcochra 5:d25327fb7a14 237 INFO("Sending");
jcochra 5:d25327fb7a14 238 //dev.printf("AT+CIPSERVER= 1,6060");
jcochra 5:d25327fb7a14 239 // code to get range and format to max 4 digits to be added
jcochra 5:d25327fb7a14 240 dev.printf("Range=%04d\r\n",range); //10 = length of string being sent
jcochra 5:d25327fb7a14 241 range=(range+500)%6000;
jcochra 5:d25327fb7a14 242 wait(2);
jcochra 5:d25327fb7a14 243 state=9;
jcochra 5:d25327fb7a14 244 }
jcochra 5:d25327fb7a14 245 // pc.printf("%s\n\r",buffer);
jcochra 5:d25327fb7a14 246
jcochra 5:d25327fb7a14 247 break;
jcochra 5:d25327fb7a14 248
jcochra 5:d25327fb7a14 249 }
jcochra 5:d25327fb7a14 250
jcochra 5:d25327fb7a14 251
jcochra 3:c99ef21cb4b3 252
jcochra 4:d205b6061e66 253
jcochra 3:c99ef21cb4b3 254 }
epgmdm 0:7176e20dbdf6 255 }
epgmdm 0:7176e20dbdf6 256 __WFI();
epgmdm 0:7176e20dbdf6 257 }
jcochra 3:c99ef21cb4b3 258 }