Just for testing

Dependencies:   mbed

Fork of ESP8266_MDM by Oxford CWM Team

Committer:
epgmdm
Date:
Wed Jun 08 11:05:52 2016 +0000
Revision:
1:e2385bba1ab1
Parent:
0:7176e20dbdf6
Setup done

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);
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 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
epgmdm 1:e2385bba1ab1 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 }
epgmdm 1:e2385bba1ab1 68 if ((c==0x0a)||(c==0x0d)){
epgmdm 1:e2385bba1ab1 69 ready=1;
epgmdm 1:e2385bba1ab1 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);
epgmdm 0:7176e20dbdf6 93
epgmdm 0:7176e20dbdf6 94 }
epgmdm 0:7176e20dbdf6 95 }
epgmdm 0:7176e20dbdf6 96
epgmdm 0:7176e20dbdf6 97 char * OKResponse(char *test, const char *pattern)
epgmdm 0:7176e20dbdf6 98 {
epgmdm 0:7176e20dbdf6 99 char *p= strstr(test,pattern);
epgmdm 0:7176e20dbdf6 100 if (p==NULL) {
epgmdm 0:7176e20dbdf6 101 // DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
epgmdm 0:7176e20dbdf6 102 return NULL;
epgmdm 0:7176e20dbdf6 103 } else {
epgmdm 0:7176e20dbdf6 104 // DBG("YAY Test=<%s> Patter=<%s> [p=%s]",test,pattern,p);
epgmdm 0:7176e20dbdf6 105 }
epgmdm 0:7176e20dbdf6 106 return p;
epgmdm 0:7176e20dbdf6 107 }
epgmdm 0:7176e20dbdf6 108 int main()
epgmdm 0:7176e20dbdf6 109 {
epgmdm 0:7176e20dbdf6 110 buffer=(char *)calloc(BUFF_SIZE,1);
epgmdm 0:7176e20dbdf6 111 reset=0;
epgmdm 0:7176e20dbdf6 112
epgmdm 0:7176e20dbdf6 113 pc.baud(115200);
epgmdm 0:7176e20dbdf6 114 dev.baud(115200);
epgmdm 0:7176e20dbdf6 115 pc.attach(&pc_recv, Serial::RxIrq);
epgmdm 0:7176e20dbdf6 116 dev.attach(&dev_recv, Serial::RxIrq);
epgmdm 0:7176e20dbdf6 117 pc.printf("Start up\n\r");
epgmdm 0:7176e20dbdf6 118 wait(1.5);
epgmdm 0:7176e20dbdf6 119 reset=1;
epgmdm 0:7176e20dbdf6 120 char * resp=NULL;
epgmdm 0:7176e20dbdf6 121 pc.printf("Here \n\r");
epgmdm 0:7176e20dbdf6 122 while(1) {
epgmdm 0:7176e20dbdf6 123 if (ready) {
epgmdm 0:7176e20dbdf6 124 ready=0;
epgmdm 0:7176e20dbdf6 125 bufferPnt=0;
epgmdm 1:e2385bba1ab1 126 INFO("[%d]",state);
epgmdm 0:7176e20dbdf6 127 switch (state) {
epgmdm 0:7176e20dbdf6 128 case 0: {
epgmdm 0:7176e20dbdf6 129 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 130 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 131 wait(1);
epgmdm 0:7176e20dbdf6 132 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 133 state++;
epgmdm 0:7176e20dbdf6 134 }
epgmdm 0:7176e20dbdf6 135 break;
epgmdm 0:7176e20dbdf6 136 }
epgmdm 0:7176e20dbdf6 137 case 1:
epgmdm 0:7176e20dbdf6 138 case 2: {
epgmdm 0:7176e20dbdf6 139 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 140 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 141 dev.printf("AT\r\n");
epgmdm 0:7176e20dbdf6 142 state++;
epgmdm 0:7176e20dbdf6 143 }
epgmdm 0:7176e20dbdf6 144 break;
epgmdm 0:7176e20dbdf6 145 }
epgmdm 0:7176e20dbdf6 146 case 3: {
epgmdm 0:7176e20dbdf6 147 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 148 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 149 dev.printf("AT+RST\r\n");
epgmdm 0:7176e20dbdf6 150 state++;
epgmdm 0:7176e20dbdf6 151 }
epgmdm 0:7176e20dbdf6 152
epgmdm 0:7176e20dbdf6 153 break;
epgmdm 0:7176e20dbdf6 154 }
epgmdm 0:7176e20dbdf6 155 case 4: {
epgmdm 0:7176e20dbdf6 156 resp=OKResponse(buffer,"WIFI GOT IP");
epgmdm 0:7176e20dbdf6 157 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 158 dev.printf("AT+CWMODE=1\r\n");
epgmdm 0:7176e20dbdf6 159 state++;
epgmdm 0:7176e20dbdf6 160 }
epgmdm 0:7176e20dbdf6 161
epgmdm 0:7176e20dbdf6 162 break;
epgmdm 0:7176e20dbdf6 163 }
epgmdm 0:7176e20dbdf6 164 case 5: {
epgmdm 0:7176e20dbdf6 165 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 166 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 167
epgmdm 0:7176e20dbdf6 168 dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
epgmdm 0:7176e20dbdf6 169 state++;
epgmdm 0:7176e20dbdf6 170 }
epgmdm 0:7176e20dbdf6 171
epgmdm 0:7176e20dbdf6 172 break;
epgmdm 0:7176e20dbdf6 173 }
epgmdm 0:7176e20dbdf6 174 case 6: {
epgmdm 0:7176e20dbdf6 175 resp=OKResponse(buffer,"OK");
epgmdm 0:7176e20dbdf6 176 if (resp!=NULL) {
epgmdm 0:7176e20dbdf6 177 wait(1);
epgmdm 0:7176e20dbdf6 178 dev.printf("AT+CIFSR\r\n");
epgmdm 0:7176e20dbdf6 179 state++;
epgmdm 0:7176e20dbdf6 180 }
epgmdm 0:7176e20dbdf6 181
epgmdm 0:7176e20dbdf6 182 break;
epgmdm 0:7176e20dbdf6 183 }
epgmdm 0:7176e20dbdf6 184 case 7: {
epgmdm 1:e2385bba1ab1 185 resp=OKResponse(buffer,"+CIFSR:STAIP,");
epgmdm 0:7176e20dbdf6 186 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 187 char *strt = strtok(buffer,"\"");
epgmdm 1:e2385bba1ab1 188 strcpy(ipAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 189 strtok(NULL,"\"");
epgmdm 1:e2385bba1ab1 190 strcpy(macAddress,strtok(NULL,"\""));
epgmdm 1:e2385bba1ab1 191 INFO("mac Address = %s", macAddress);
epgmdm 1:e2385bba1ab1 192 INFO("IP Address = %s", ipAddress);
epgmdm 1:e2385bba1ab1 193 dev.printf("AT+CIPMUX=1\r\n");
epgmdm 0:7176e20dbdf6 194 state++;
epgmdm 0:7176e20dbdf6 195 }
epgmdm 0:7176e20dbdf6 196
epgmdm 0:7176e20dbdf6 197 break;
epgmdm 0:7176e20dbdf6 198 }
epgmdm 1:e2385bba1ab1 199 case 8: {
epgmdm 1:e2385bba1ab1 200 resp=OKResponse(buffer,"OK");
epgmdm 1:e2385bba1ab1 201 if (resp!=NULL) {
epgmdm 1:e2385bba1ab1 202 INFO("Ready");
epgmdm 1:e2385bba1ab1 203 state++;
epgmdm 1:e2385bba1ab1 204 }
epgmdm 1:e2385bba1ab1 205
epgmdm 1:e2385bba1ab1 206 break;
epgmdm 1:e2385bba1ab1 207 }
epgmdm 0:7176e20dbdf6 208
epgmdm 0:7176e20dbdf6 209
epgmdm 0:7176e20dbdf6 210 }
epgmdm 0:7176e20dbdf6 211 }
epgmdm 0:7176e20dbdf6 212 __WFI();
epgmdm 0:7176e20dbdf6 213 }
epgmdm 0:7176e20dbdf6 214 }