Kiko Ishimoto
/
mbed_esp8266
esp8266
Revision 1:49b2903daa5f, committed 2016-05-09
- Comitter:
- kikoaac
- Date:
- Mon May 09 05:50:49 2016 +0000
- Parent:
- 0:28089dd1adda
- Commit message:
- update2;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 28089dd1adda -r 49b2903daa5f main.cpp --- a/main.cpp Mon Mar 28 04:29:04 2016 +0000 +++ b/main.cpp Mon May 09 05:50:49 2016 +0000 @@ -1,106 +1,109 @@ #include "mbed.h" -#include <string> +#include <string> DigitalOut myled(LED1); -Serial esp(p9,p10); +Serial esp(p13,p14); Serial pc(USBTX,USBRX); #define end "\r\n" - + class esp8266 { - private: - + //AP アクセスポイント + //ST client + //APST Server and Client +private: + DigitalOut myled; Serial *Seri; char* buffer; int datalen; - - void Reception() - { - - char x[100]; - Seri->scanf("%s",x); - myled=1; + void strChack(char data[]) { + int f = 0; + for (;;) { + if(f == strlen(data)) + { + break; + } + char a=Seri->getc(); + if(a!=data[f])f=0; + else f++; + + } + wait(0.1); + } + void Reception() { + + char inputData[100]; + Seri->scanf("%s",inputData); + myled=1; Serial pc(USBTX,USBRX); pc.baud(115200); - pc.printf("%s\r\n",x); - if(strstr(x,"+IPD,")==NULL) - { - /* - if(strcmp(x,"0,CONNECT")==0)break; - if(strcmp(x,"0,CLOSED")==0)break; - if(strcmp(x,">")==0)break;*/ - } - else - { - strtok(x,","); - strtok(NULL,":"); - char *data = strtok(NULL,","); - strtok(x,","); - char *data_num = strtok(NULL,","); - datalen = atoi(data_num); - buffer=data; - //data = strtok(x,""); - //buffer = data; - //pc.printf("%s , %s , %s\r\n",y,data_num,data); - } - myled=1; + pc.printf("%s\r\n",inputData); + if(strstr(inputData,"+IPD,")==NULL) { + /* + if(strcmp(x,"0,CONNECT")==0)break; + if(strcmp(x,"0,CLOSED")==0)break; + if(strcmp(x,">")==0)break;*/ + } else { + strtok(inputData,","); + strtok(NULL,":"); + char *data = strtok(NULL,","); + strtok(inputData,","); + char *data_num = strtok(NULL,","); + datalen = atoi(data_num); + buffer=data; + //data = strtok(x,""); + //buffer = data; + //pc.printf("%s , %s , %s\r\n",y,data_num,data); + } + myled=1; } - bool check() - { - + bool check() { + Serial pc(USBTX,USBRX); pc.baud(115200); Seri->printf(end); bool f = false,ans; - int i=0; + int i=0; while(1) { char x = Seri->getc(); pc.putc(x); if(x=='O')f=true; - else if(x=='K'&&f==true) - { + else if(x=='K'&&f==true) { ans=true; break; - } - else f=false; - + } else f=false; + if(x=='F')i=1; else if(x=='A'&&i==1)i=2; else if(x=='I'&&i==2)i=3; - else if(x=='L'&&i==3) - { + else if(x=='L'&&i==3) { ans=false; break; - } - else i=0; - + } else i=0; + } wait(0.1); return ans; - } + } bool connected; - public: - char* get_data(char *datax,int num) - { - get: +public: + char* get_data(char *datax,int num) { + get: char x[num+2]; Seri->scanf("%s",x); myled=1; Serial pc(USBTX,USBRX); pc.baud(115200); - if(strstr(x,"+IPD,")==NULL) - { - + if(strstr(x,"+IPD,")==NULL) { + return NULL; /* if(strcmp(x,"0,CONNECT")==0)break; if(strcmp(x,"0,CLOSED")==0)break; if(strcmp(x,">")==0)break;*/ - } - else - { + } else { strtok(x,","); strtok(NULL,":"); @@ -108,7 +111,7 @@ strtok(x,","); char *data_num = strtok(NULL,","); char len = atoi(data_num); - + if(strcmp(datax,"\r\n")==0)goto get; else if(len > num)return NULL; //for(int i=0;i<len;i++) @@ -120,72 +123,45 @@ //pc.printf("%s , %s , %s\r\n",y,data_num,data); } } - /*void uart_setting(int fre=115200,int bit=8,int stopbit=1,int parity=0,int flow_control=3) - { - - Seri->printf("AT+UART_CUR=%d,%d,%d,%d,%d\r\n",fre,bit,stopbit,parity,flow_control); - Seri->baud(fre); - Seri->format(bit); - Seri->set_flow_control(Serial::RTSCTS,p9,p10); - //check(); - wait(0.1); - }*/ - bool sleep(int mode) - { + bool sleep(int mode) { Seri->printf("AT+SLEEP=%d",mode); - return check(); + return check(); } - bool deepsleep(int ms) - { + bool deepsleep(int ms) { Seri->printf("AT+GSLP=%d",ms); return check(); } - bool http_access(char* mode,char* url,int num=80) - { + bool httpAccess(char* mode,char* url,int port=80) { if(connected==false)return false; - Seri->printf("AT+CIPSTART=\"%s\",\"%s\",%d",mode,url,num); - return check(); + Seri->printf("AT+CIPSTART=\"%s\",\"%s\",%d",mode,url,port); + return check(); + } + bool httpAccess(char id,char* url,char* mode = "TCP",int port=80) { + if(connected==false)return false; + Seri->printf("AT+CIPSTART=\"%d\",\"%s\",\"%s\",%d",id,mode,url,port); + return check(); } - void prompt() - { - Seri->attach(this,&esp8266::Reception); - } - bool connect(const char* ssid,const char* password) + bool Disconnection(int id = 0) { - Seri->printf("AT+CWJAP=\"%s\",\"%s\"",ssid,password); - connected|=check(); - if(connected==false)return false; - Seri->printf("AT+CIFSR"); - check(); - return true; + printf("AT+CIPCLOSE==%d",id); + return check(); } - bool softAPfromIP() - { - Seri->printf("AT+CWLIF"); - return check(); + void prompt() { + Seri->attach(this,&esp8266::Reception); } //Seri->printf("AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"",IP,gateway,netmask); - bool EDDHCP(int mode,int en) - { + bool setDHCP(int mode,int en) { + DHCPMode = mode; + DHCPenable = en; printf("AT+CWDHCP_CUR=%d,%d",mode,en); return check(); } - bool softAP(const char* ssid,const char* password,int ch,int ecn) - { - if(connectmode==AP||connectmode==APST) - Seri->printf("AT+CWSAP=\"%s\",\"%s\",%d,%d",ssid,password,ch,ecn); - return check(); - } - bool send(int num,char* data) - { + bool send( int num,char* data,int id) { if(connected==false)return false; - Seri->printf("AT+CIPSEND=0,%d",num+2); + Seri->printf("AT+CIPSEND=%d,%d",id,num+2); check(); wait(0.01); - Serial pc(USBTX,USBRX); - pc.baud(115200); - while(1) - { + while(1) { char i = Seri->getc(); pc.putc(i); if(i=='>')break; @@ -194,116 +170,128 @@ Seri->printf("\r\n"); return check(); } - bool accesspoint() - { - Seri->printf("AT+CWLAP\r\n"); - return check(); - } - bool find_accesspoint(char *wifi) - { - Seri->printf("AT+CWLAP=\"%s\""); - return check(); - } - bool find_accesspoint() - { - Seri->printf("AT+CWLAP"); - return check(); - } - bool multipull(int i) - { + + bool multipull(int i) { Seri->printf("AT+CIPMUX=%d",i); return check(); } - void reset() - { - Serial pc(USBTX,USBRX); - pc.baud(115200); + void reset() { Seri->printf("AT+RST\r\n"); - int f=0; - while(1) - { - char a=Seri->getc(); - pc.putc(a); - if(a=='r')f=1; - else if(a=='e'&&f==1)f=2; - else if(a=='a'&&f==2)f=3; - else if(a=='d'&&f==3)f=4; - else if(a=='y'&&f==4)break; - else f=0; - - } - wait(0.1); - }/* - char *read() - { - return buffer; - }*/ - void tcp_setting(int mode,int port) - { + strChack("ready"); + wait(0.1); + } + void serverCreate(int mode,int port) { Seri->printf("AT+CIPSERVER=%d,%d",mode,port); check(); wait(0.1); - printf("\r\nwait connection\r\n"); - while(1) - { + /*printf("\r\nwait connection\r\n"); + while(1) { char x[10]; Seri->scanf("%s",x); if(strcmp(x,"0,CONNECT")==0)break; - } + }*/ printf("\r\OK\r\n"); } - bool connection_mode(int mode) + void serverTimeOutSet(int time = 60) { - connectmode=mode; - Seri->printf("AT+CWMODE=%d",mode); + Seri->printf("AT+CIPSTO=%d",time); + check(); + } + + bool accesspoint() { + Seri->printf("AT+CWLAP\r\n"); return check(); } - bool disconnect() - { + + bool wifiSoftAPSetting(const char* ssid,const char* password,int ch,int ecn) { + if(connectmode==AP||connectmode==APST) + Seri->printf("AT+CWSAP_CUR=\"%s\",\"%s\",%d,%d",ssid,password,ch,ecn); + else false; + return check(); + } + bool wifiConnectionMode(int mode) { + connectmode=mode; + Seri->printf("AT+CWMODE_CUR=%d",mode); + return check(); + } + bool wifiDisconnect() { Seri->printf("AT+CWQAP"); connected=false; return check(); } - bool ATcommand(const char* cmd) + bool WifiFindAccesspoint(char *wifi) { + Seri->printf("AT+CWLAP=\"%s\""); + return check(); + } + bool WifiFindAccesspoint() { + Seri->printf("AT+CWLAP"); + return check(); + } + bool wifiConnect(const char* ssid,const char* password) { + Seri->printf("AT+CWJAP_CUR=\"%s\",\"%s\"",ssid,password); + connected|=check(); + //if(connected==false)return false; + //Seri->printf("AT+CIFSR"); + //check(); + return connected; + } + bool wifiSoftAPFromIP() { + if(connectmode == ST) return false; + Seri->printf("AT+CWLIF"); + return check(); + } + bool setSTMACAddress(char addr[6]) { + Seri->printf("AT+CIPSTART_CUR=\"%2x:%2x:%2x:%2x:%2x:%2x\"",addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]); + return check(); + } + bool setAPMACAddress(char addr[6]) + { + Seri->printf("AT+CIPAPMAC_CUR=\"%2x:%2x:%2x:%2x:%2x:%2x\"",addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]); + return check(); + } + bool setSTIPAddress(char addr[4]) + { + Seri->printf("AT+CIPSTA_CUR=\"%3d:%3d:%3d:%3d\"",addr[0],addr[1],addr[2],addr[3]); + return check(); + } + bool setAPIPAddress(char addr[4]) + { + Seri->printf("AT+CIPAP_CUR=\"%3d:%3d:%3d:%3d\"",addr[0],addr[1],addr[2],addr[3]); + return check(); + } + + + + bool ATcommand(const char* cmd) { char a[20]; strcat(a,"AT+"); strcat(a,cmd); Seri->printf(a); - return check(); - } + return check(); + } const char AP=2; const char ST=1; const char APST=3; char connectmode; - esp8266(Serial *ser) : myled(LED2) - { + char DHCPMode; + char DHCPenable; + esp8266(Serial *ser) : myled(LED2) { myled=1; connected=false; - //string ; + + DHCPMode = 0; + DHCPenable = 1; Seri = ser; bool f = false; - int i=0; - + int i=0; Seri->printf("AT\r\n"); - while(1) { - if(Seri->readable()) - { - char x = Seri->getc(); - pc.putc(x); - if(x=='O')f=true; - else if(x=='K'&&f==true)break; - else f=false; - } - } - //Seri->attach(this,&esp8266::Reception); - /*data = "AT"; - //data +=end; - Seri->printf(data); - Seri->printf(end);*/ + + strChack("OK"); + reset(); wait(0.5); } - + }; /*void end() { @@ -311,43 +299,53 @@ }*/ void send_AT() { - esp.printf("AT"); + esp.printf("AT\r\n"); } -int main() { - string a; - myled = 1; +int main() +{ + //myled = 1; pc.baud(115200); esp.baud(115200); printf("Start\r\n"); wait(0.1); esp8266 ESP(&esp); - //send_AT(); + send_AT(); + myled = 1; + bool f = true; + while(f) + { + /*char *x; + esp.scanf("%s",x); + printf("%s",x); + f = !strcmp(x,"OK\r\n");*/ + //esp.putc(pc.getc()); + //pc.putc(esp.getc()); + } //end(); //ESP.reset(); - ESP.connection_mode(3); + /*ESP.connection_mode(3); ESP.multipull(1); ESP.accesspoint(); - + ESP.connect("biz","roborobo"); //ESP.http_access("TCP","imaoca.webcrow.jp",80); ESP.tcp_setting(1,8888); - char senddata[10];/* + char senddata[10]; pc.printf("\r\n tcp send data\r\n"); for(int i=0;i<10;i++) { senddata[i]=pc.getc(); - pc.putc(senddata[i]); - }*/ + pc.putc(senddata[i]); + } //ESP.send(10,senddata); //ESP.prompt(); ESP.softAP("ESP8266","",10,0); char *data,num=30; DigitalOut LED(LED4); - while(1) - { - // data = ESP.get_data(data,num); - // if(data!=NULL) - // printf("GET->%s\r\n",data); + while(1) { + // data = ESP.get_data(data,num); + // if(data!=NULL) + // printf("GET->%s\r\n",data); myled=1; wait(0.01); myled=0; @@ -357,7 +355,7 @@ LED=1; if (strstr(data,"OFF")!=NULL) LED=0; - //pc.putc( esp.getc()); + //pc.putc( esp.getc()); } /*while(1) { char x = esp.getc();