Just for testing

Dependencies:   mbed

Fork of ESP8266_MDM by Oxford CWM Team

Committer:
magd4235
Date:
Fri Jun 10 08:56:50 2016 +0000
Revision:
3:a608548f63b1
server test;

Who changed what in which revision?

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