James Cochrane
/
Client_Code_Rangefinder
working code
Fork of Working_Client_Code by
main.cpp@0:7176e20dbdf6, 2016-06-08 (annotated)
- Committer:
- epgmdm
- Date:
- Wed Jun 08 10:10:31 2016 +0000
- Revision:
- 0:7176e20dbdf6
- Child:
- 1:e2385bba1ab1
Got onto wifi and gets ip address
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:7176e20dbdf6 | 10 | #ifdef DEBUG |
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 | 0:7176e20dbdf6 | 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); |
epgmdm | 0:7176e20dbdf6 | 37 | RawSerial dev(p28, p27); //tx,rx |
epgmdm | 0:7176e20dbdf6 | 38 | DigitalOut led1(LED1); |
epgmdm | 0:7176e20dbdf6 | 39 | DigitalOut led4(LED4); |
epgmdm | 0:7176e20dbdf6 | 40 | DigitalOut reset(p26,1); |
epgmdm | 0:7176e20dbdf6 | 41 | volatile int state=0; |
epgmdm | 0:7176e20dbdf6 | 42 | volatile int ready=0; |
epgmdm | 0:7176e20dbdf6 | 43 | char *buffer; |
epgmdm | 0:7176e20dbdf6 | 44 | unsigned int bufferPnt=0; |
epgmdm | 0:7176e20dbdf6 | 45 | |
epgmdm | 0:7176e20dbdf6 | 46 | void dev_recv() |
epgmdm | 0:7176e20dbdf6 | 47 | { |
epgmdm | 0:7176e20dbdf6 | 48 | char c; |
epgmdm | 0:7176e20dbdf6 | 49 | |
epgmdm | 0:7176e20dbdf6 | 50 | int count = 0; |
epgmdm | 0:7176e20dbdf6 | 51 | led1 = !led1; |
epgmdm | 0:7176e20dbdf6 | 52 | if(bufferPnt==0) { |
epgmdm | 0:7176e20dbdf6 | 53 | memset(buffer,0,BUFF_SIZE); |
epgmdm | 0:7176e20dbdf6 | 54 | } |
epgmdm | 0:7176e20dbdf6 | 55 | while(dev.readable()) { |
epgmdm | 0:7176e20dbdf6 | 56 | c = (char)dev.getc(); |
epgmdm | 0:7176e20dbdf6 | 57 | pc.putc(c); |
epgmdm | 0:7176e20dbdf6 | 58 | buffer[bufferPnt]=c; |
epgmdm | 0:7176e20dbdf6 | 59 | bufferPnt++; |
epgmdm | 0:7176e20dbdf6 | 60 | if (bufferPnt>1024){ |
epgmdm | 0:7176e20dbdf6 | 61 | ready=1; |
epgmdm | 0:7176e20dbdf6 | 62 | } |
epgmdm | 0:7176e20dbdf6 | 63 | if (c==0x0a) { |
epgmdm | 0:7176e20dbdf6 | 64 | if (bufferPnt>1) { |
epgmdm | 0:7176e20dbdf6 | 65 | if (buffer[bufferPnt -2]==0x0d) { |
epgmdm | 0:7176e20dbdf6 | 66 | ready=1; |
epgmdm | 0:7176e20dbdf6 | 67 | break; |
epgmdm | 0:7176e20dbdf6 | 68 | } |
epgmdm | 0:7176e20dbdf6 | 69 | } |
epgmdm | 0:7176e20dbdf6 | 70 | } |
epgmdm | 0:7176e20dbdf6 | 71 | if (!dev.readable()){ |
epgmdm | 0:7176e20dbdf6 | 72 | wait(0.001); |
epgmdm | 0:7176e20dbdf6 | 73 | } |
epgmdm | 0:7176e20dbdf6 | 74 | } |
epgmdm | 0:7176e20dbdf6 | 75 | } |
epgmdm | 0:7176e20dbdf6 | 76 | |
epgmdm | 0:7176e20dbdf6 | 77 | void pc_recv() |
epgmdm | 0:7176e20dbdf6 | 78 | { |
epgmdm | 0:7176e20dbdf6 | 79 | char c; |
epgmdm | 0:7176e20dbdf6 | 80 | led4 = !led4; |
epgmdm | 0:7176e20dbdf6 | 81 | while(pc.readable()) { |
epgmdm | 0:7176e20dbdf6 | 82 | c=(char)pc.getc(); |
epgmdm | 0:7176e20dbdf6 | 83 | dev.putc(c); |
epgmdm | 0:7176e20dbdf6 | 84 | pc.putc(c); |
epgmdm | 0:7176e20dbdf6 | 85 | |
epgmdm | 0:7176e20dbdf6 | 86 | } |
epgmdm | 0:7176e20dbdf6 | 87 | } |
epgmdm | 0:7176e20dbdf6 | 88 | |
epgmdm | 0:7176e20dbdf6 | 89 | char * OKResponse(char *test, const char *pattern) |
epgmdm | 0:7176e20dbdf6 | 90 | { |
epgmdm | 0:7176e20dbdf6 | 91 | char *p= strstr(test,pattern); |
epgmdm | 0:7176e20dbdf6 | 92 | if (p==NULL) { |
epgmdm | 0:7176e20dbdf6 | 93 | // DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p); |
epgmdm | 0:7176e20dbdf6 | 94 | return NULL; |
epgmdm | 0:7176e20dbdf6 | 95 | } else { |
epgmdm | 0:7176e20dbdf6 | 96 | // DBG("YAY Test=<%s> Patter=<%s> [p=%s]",test,pattern,p); |
epgmdm | 0:7176e20dbdf6 | 97 | } |
epgmdm | 0:7176e20dbdf6 | 98 | return p; |
epgmdm | 0:7176e20dbdf6 | 99 | } |
epgmdm | 0:7176e20dbdf6 | 100 | int main() |
epgmdm | 0:7176e20dbdf6 | 101 | { |
epgmdm | 0:7176e20dbdf6 | 102 | buffer=(char *)calloc(BUFF_SIZE,1); |
epgmdm | 0:7176e20dbdf6 | 103 | reset=0; |
epgmdm | 0:7176e20dbdf6 | 104 | |
epgmdm | 0:7176e20dbdf6 | 105 | pc.baud(115200); |
epgmdm | 0:7176e20dbdf6 | 106 | dev.baud(115200); |
epgmdm | 0:7176e20dbdf6 | 107 | pc.attach(&pc_recv, Serial::RxIrq); |
epgmdm | 0:7176e20dbdf6 | 108 | dev.attach(&dev_recv, Serial::RxIrq); |
epgmdm | 0:7176e20dbdf6 | 109 | pc.printf("Start up\n\r"); |
epgmdm | 0:7176e20dbdf6 | 110 | wait(1.5); |
epgmdm | 0:7176e20dbdf6 | 111 | reset=1; |
epgmdm | 0:7176e20dbdf6 | 112 | char * resp=NULL; |
epgmdm | 0:7176e20dbdf6 | 113 | pc.printf("Here \n\r"); |
epgmdm | 0:7176e20dbdf6 | 114 | while(1) { |
epgmdm | 0:7176e20dbdf6 | 115 | if (ready) { |
epgmdm | 0:7176e20dbdf6 | 116 | ready=0; |
epgmdm | 0:7176e20dbdf6 | 117 | bufferPnt=0; |
epgmdm | 0:7176e20dbdf6 | 118 | pc.printf("%d]%s\n\r",state,buffer); |
epgmdm | 0:7176e20dbdf6 | 119 | switch (state) { |
epgmdm | 0:7176e20dbdf6 | 120 | case 0: { |
epgmdm | 0:7176e20dbdf6 | 121 | resp=OKResponse(buffer,"WIFI GOT IP"); |
epgmdm | 0:7176e20dbdf6 | 122 | if (resp!=NULL) { |
epgmdm | 0:7176e20dbdf6 | 123 | DBG(">>>>>>HERE"); |
epgmdm | 0:7176e20dbdf6 | 124 | wait(1); |
epgmdm | 0:7176e20dbdf6 | 125 | dev.printf("AT\r\n"); |
epgmdm | 0:7176e20dbdf6 | 126 | state++; |
epgmdm | 0:7176e20dbdf6 | 127 | } |
epgmdm | 0:7176e20dbdf6 | 128 | break; |
epgmdm | 0:7176e20dbdf6 | 129 | } |
epgmdm | 0:7176e20dbdf6 | 130 | case 1: |
epgmdm | 0:7176e20dbdf6 | 131 | case 2: { |
epgmdm | 0:7176e20dbdf6 | 132 | resp=OKResponse(buffer,"OK"); |
epgmdm | 0:7176e20dbdf6 | 133 | if (resp!=NULL) { |
epgmdm | 0:7176e20dbdf6 | 134 | DBG(">>>>>>HERE 2"); |
epgmdm | 0:7176e20dbdf6 | 135 | dev.printf("AT\r\n"); |
epgmdm | 0:7176e20dbdf6 | 136 | state++; |
epgmdm | 0:7176e20dbdf6 | 137 | } |
epgmdm | 0:7176e20dbdf6 | 138 | break; |
epgmdm | 0:7176e20dbdf6 | 139 | } |
epgmdm | 0:7176e20dbdf6 | 140 | case 3: { |
epgmdm | 0:7176e20dbdf6 | 141 | resp=OKResponse(buffer,"OK"); |
epgmdm | 0:7176e20dbdf6 | 142 | if (resp!=NULL) { |
epgmdm | 0:7176e20dbdf6 | 143 | DBG(">>>>>>HERE 3"); |
epgmdm | 0:7176e20dbdf6 | 144 | dev.printf("AT+RST\r\n"); |
epgmdm | 0:7176e20dbdf6 | 145 | state++; |
epgmdm | 0:7176e20dbdf6 | 146 | } |
epgmdm | 0:7176e20dbdf6 | 147 | |
epgmdm | 0:7176e20dbdf6 | 148 | break; |
epgmdm | 0:7176e20dbdf6 | 149 | } |
epgmdm | 0:7176e20dbdf6 | 150 | case 4: { |
epgmdm | 0:7176e20dbdf6 | 151 | resp=OKResponse(buffer,"WIFI GOT IP"); |
epgmdm | 0:7176e20dbdf6 | 152 | if (resp!=NULL) { |
epgmdm | 0:7176e20dbdf6 | 153 | DBG(">>>>>>HERE 4"); |
epgmdm | 0:7176e20dbdf6 | 154 | dev.printf("AT+CWMODE=1\r\n"); |
epgmdm | 0:7176e20dbdf6 | 155 | state++; |
epgmdm | 0:7176e20dbdf6 | 156 | } |
epgmdm | 0:7176e20dbdf6 | 157 | |
epgmdm | 0:7176e20dbdf6 | 158 | break; |
epgmdm | 0:7176e20dbdf6 | 159 | } |
epgmdm | 0:7176e20dbdf6 | 160 | case 5: { |
epgmdm | 0:7176e20dbdf6 | 161 | resp=OKResponse(buffer,"OK"); |
epgmdm | 0:7176e20dbdf6 | 162 | if (resp!=NULL) { |
epgmdm | 0:7176e20dbdf6 | 163 | DBG(">>>>>>HERE 5"); |
epgmdm | 0:7176e20dbdf6 | 164 | dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\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 6: { |
epgmdm | 0:7176e20dbdf6 | 171 | resp=OKResponse(buffer,"OK"); |
epgmdm | 0:7176e20dbdf6 | 172 | if (resp!=NULL) { |
epgmdm | 0:7176e20dbdf6 | 173 | DBG(">>>>>>HERE 5"); |
epgmdm | 0:7176e20dbdf6 | 174 | wait(1); |
epgmdm | 0:7176e20dbdf6 | 175 | dev.printf("AT+CIFSR\r\n"); |
epgmdm | 0:7176e20dbdf6 | 176 | state++; |
epgmdm | 0:7176e20dbdf6 | 177 | } |
epgmdm | 0:7176e20dbdf6 | 178 | |
epgmdm | 0:7176e20dbdf6 | 179 | break; |
epgmdm | 0:7176e20dbdf6 | 180 | } |
epgmdm | 0:7176e20dbdf6 | 181 | case 7: { |
epgmdm | 0:7176e20dbdf6 | 182 | resp=OKResponse(buffer,"OK"); |
epgmdm | 0:7176e20dbdf6 | 183 | if (resp!=NULL) { |
epgmdm | 0:7176e20dbdf6 | 184 | DBG(">>>>>>HERE 5"); |
epgmdm | 0:7176e20dbdf6 | 185 | wait(1); |
epgmdm | 0:7176e20dbdf6 | 186 | dev.printf("AT+CIFSR\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 | |
epgmdm | 0:7176e20dbdf6 | 193 | |
epgmdm | 0:7176e20dbdf6 | 194 | } |
epgmdm | 0:7176e20dbdf6 | 195 | } |
epgmdm | 0:7176e20dbdf6 | 196 | __WFI(); |
epgmdm | 0:7176e20dbdf6 | 197 | } |
epgmdm | 0:7176e20dbdf6 | 198 | } |