esp8266

Dependencies:   mbed

Revision:
0:28089dd1adda
Child:
1:49b2903daa5f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Mar 28 04:29:04 2016 +0000
@@ -0,0 +1,370 @@
+#include "mbed.h"
+
+#include <string> 
+
+DigitalOut myled(LED1);
+Serial esp(p9,p10);
+Serial pc(USBTX,USBRX);
+#define end "\r\n"
+    
+class esp8266
+{
+    private:
+    
+    DigitalOut myled;
+    Serial *Seri;
+    char* buffer;
+    int datalen;
+    
+    void Reception()
+    {
+        
+            char x[100];
+            Seri->scanf("%s",x);
+            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;
+    }
+    bool check()
+    {
+                
+        Serial pc(USBTX,USBRX);
+        pc.baud(115200);
+        Seri->printf(end);
+        bool f = false,ans;
+        int i=0; 
+        while(1) {
+            char x = Seri->getc();
+            pc.putc(x);
+            if(x=='O')f=true;
+            else if(x=='K'&&f==true)
+            {
+                ans=true;
+                break;
+            }
+            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)
+            {
+                ans=false;
+                break;
+            }
+            else i=0;
+            
+        }
+        wait(0.1);
+        return ans;
+    }   
+    bool connected;
+    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)
+        {
+            
+            return 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,":");
+            datax = strtok(NULL,",");
+            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++)
+            //    *datax++=*data++;
+            //printf("%s\r\n",datax);
+            return datax;
+            //data = strtok(x,"");
+            //buffer = data;
+            //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)
+    {
+        Seri->printf("AT+SLEEP=%d",mode);
+        return check();   
+    }
+    bool deepsleep(int ms)
+    {
+        Seri->printf("AT+GSLP=%d",ms);
+        return check();
+    }
+    bool http_access(char* mode,char* url,int num=80)
+    {
+        if(connected==false)return false;
+        Seri->printf("AT+CIPSTART=\"%s\",\"%s\",%d",mode,url,num);
+        return check();  
+    }
+    void prompt()
+    {
+        Seri->attach(this,&esp8266::Reception);
+    }   
+    bool connect(const char* ssid,const char* password)
+    {
+        Seri->printf("AT+CWJAP=\"%s\",\"%s\"",ssid,password);
+        connected|=check();
+        if(connected==false)return false;  
+        Seri->printf("AT+CIFSR");
+        check();
+        return true;
+    }
+    bool softAPfromIP()
+    {
+        Seri->printf("AT+CWLIF");
+        return check();   
+    }
+    //Seri->printf("AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"",IP,gateway,netmask);
+    bool EDDHCP(int mode,int 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)
+    {
+        if(connected==false)return false;
+        Seri->printf("AT+CIPSEND=0,%d",num+2);
+        check();
+        wait(0.01);
+        Serial pc(USBTX,USBRX);
+        pc.baud(115200);
+        while(1)
+        {
+            char i = Seri->getc();
+            pc.putc(i);
+            if(i=='>')break;
+        }
+        Seri->printf("%s",data);
+        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)
+    {
+        Seri->printf("AT+CIPMUX=%d",i);
+        return check();
+    }
+    void reset()
+    {
+        Serial pc(USBTX,USBRX);
+        pc.baud(115200);
+        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)
+    {
+        Seri->printf("AT+CIPSERVER=%d,%d",mode,port);
+        check();
+        wait(0.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)
+    {
+        connectmode=mode;
+        Seri->printf("AT+CWMODE=%d",mode);
+        return check();
+    }
+    bool disconnect()
+    {
+        Seri->printf("AT+CWQAP");
+        connected=false;
+        return check();
+    }
+    bool ATcommand(const char* cmd)
+    {
+        char a[20];
+        strcat(a,"AT+");
+        strcat(a,cmd);
+        Seri->printf(a);
+        return check();  
+    }     
+    const char AP=2;
+    const char ST=1;
+    const char APST=3;
+    char  connectmode;
+    esp8266(Serial *ser) : myled(LED2)
+    {
+        myled=1;
+        connected=false;
+        //string ;
+        Seri = ser;
+        bool f = false;
+        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);*/
+        wait(0.5);
+    }
+    
+};
+/*void end()
+{
+    esp.printf("\r\n");
+}*/
+void send_AT()
+{
+    esp.printf("AT");
+}
+int main() {
+    string a;
+    myled = 1;
+    pc.baud(115200);
+    esp.baud(115200);
+    printf("Start\r\n");
+    wait(0.1);
+    esp8266 ESP(&esp);
+    //send_AT();
+    //end();
+    //ESP.reset();
+    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];/*
+    pc.printf("\r\n tcp send data\r\n");
+    for(int i=0;i<10;i++)
+    {
+        senddata[i]=pc.getc();
+        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);
+        myled=1;
+        wait(0.01);
+        myled=0;
+        data=ESP.get_data(data,num);
+        printf("%s\r\n",data);
+        if (strstr(data,"ON")!=NULL)
+            LED=1;
+        if (strstr(data,"OFF")!=NULL)
+            LED=0;
+       //pc.putc( esp.getc());
+    }
+    /*while(1) {
+        char x = esp.getc();
+        pc.putc(x);
+        if(x=='O')f=true;
+        else if(x=='K'&&f==true)break;
+        else f=false;
+    }*/
+    myled=0;
+}