working code

Dependencies:   mbed

Fork of Working_Client_Code by iX

Committer:
jcochra
Date:
Fri Jun 10 10:37:56 2016 +0000
Revision:
8:f277f86f75bb
Parent:
7:1119c6781d17
WORKING CODE;

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