2輪に移行

Dependencies:   MPU92502 Motor2 PIDonerobot Stepper mbed

Committer:
kikoaac
Date:
Sun Aug 07 12:50:56 2016 +0000
Revision:
1:7ebc0f5f847b
Parent:
0:1faa9570d725
??????;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kikoaac 0:1faa9570d725 1 #include "mbed.h"
kikoaac 0:1faa9570d725 2 #include <string>
kikoaac 0:1faa9570d725 3
kikoaac 0:1faa9570d725 4 #define end "\r\n"
kikoaac 0:1faa9570d725 5
kikoaac 0:1faa9570d725 6 class esp8266
kikoaac 0:1faa9570d725 7 {
kikoaac 0:1faa9570d725 8 //AP アクセスポイント
kikoaac 0:1faa9570d725 9 //ST client
kikoaac 0:1faa9570d725 10 //APST Server and Client
kikoaac 0:1faa9570d725 11 private:
kikoaac 0:1faa9570d725 12
kikoaac 0:1faa9570d725 13 Serial *Seri;
kikoaac 0:1faa9570d725 14 char* buffer;
kikoaac 0:1faa9570d725 15 int datalen;
kikoaac 0:1faa9570d725 16 void strChack(char data[]) {
kikoaac 0:1faa9570d725 17 int f = 0;
kikoaac 0:1faa9570d725 18 for (;;) {
kikoaac 0:1faa9570d725 19 if(f >= strlen(data))
kikoaac 0:1faa9570d725 20 {
kikoaac 0:1faa9570d725 21 break;
kikoaac 0:1faa9570d725 22 }
kikoaac 0:1faa9570d725 23 char a=Seri->getc();
kikoaac 0:1faa9570d725 24 if(a!=data[f])f=0;
kikoaac 0:1faa9570d725 25 else f++;
kikoaac 0:1faa9570d725 26
kikoaac 0:1faa9570d725 27 }
kikoaac 0:1faa9570d725 28 wait(0.1);
kikoaac 0:1faa9570d725 29 }
kikoaac 0:1faa9570d725 30 void Reception() {
kikoaac 0:1faa9570d725 31
kikoaac 0:1faa9570d725 32 char inputData[100];
kikoaac 0:1faa9570d725 33 Seri->scanf("%s",inputData);
kikoaac 0:1faa9570d725 34 //Serial pc(USBTX,USBRX);
kikoaac 0:1faa9570d725 35 //pc.baud(115200);
kikoaac 0:1faa9570d725 36 //pc.printf("%s\r\n",inputData);
kikoaac 0:1faa9570d725 37 if(strstr(inputData,"+IPD,")==NULL) {
kikoaac 0:1faa9570d725 38 /*
kikoaac 0:1faa9570d725 39 if(strcmp(x,"0,CONNECT")==0)break;
kikoaac 0:1faa9570d725 40 if(strcmp(x,"0,CLOSED")==0)break;
kikoaac 0:1faa9570d725 41 if(strcmp(x,">")==0)break;*/
kikoaac 0:1faa9570d725 42 } else {
kikoaac 0:1faa9570d725 43 strtok(inputData,",");
kikoaac 0:1faa9570d725 44 strtok(NULL,":");
kikoaac 0:1faa9570d725 45 char *data = strtok(NULL,",");
kikoaac 0:1faa9570d725 46 strtok(inputData,",");
kikoaac 0:1faa9570d725 47 char *data_num = strtok(NULL,",");
kikoaac 0:1faa9570d725 48 datalen = atoi(data_num);
kikoaac 0:1faa9570d725 49 buffer=data;
kikoaac 0:1faa9570d725 50 //data = strtok(x,"");
kikoaac 0:1faa9570d725 51 //buffer = data;
kikoaac 0:1faa9570d725 52 //pc.printf("%s , %s , %s\r\n",y,data_num,data);
kikoaac 0:1faa9570d725 53 }
kikoaac 0:1faa9570d725 54 }
kikoaac 0:1faa9570d725 55 bool check() {
kikoaac 0:1faa9570d725 56
kikoaac 0:1faa9570d725 57 //Serial pc(USBTX,USBRX);
kikoaac 0:1faa9570d725 58 //pc.baud(115200);
kikoaac 0:1faa9570d725 59 Seri->printf(end);
kikoaac 0:1faa9570d725 60 bool f = false,ans;
kikoaac 0:1faa9570d725 61 int i=0;
kikoaac 0:1faa9570d725 62 while(1) {
kikoaac 0:1faa9570d725 63 char x = Seri->getc();
kikoaac 0:1faa9570d725 64 //pc.putc(x);
kikoaac 0:1faa9570d725 65 if(x=='O')f=true;
kikoaac 0:1faa9570d725 66 else if(x=='K'&&f==true) {
kikoaac 0:1faa9570d725 67 ans=true;
kikoaac 0:1faa9570d725 68 break;
kikoaac 0:1faa9570d725 69 } else f=false;
kikoaac 0:1faa9570d725 70
kikoaac 0:1faa9570d725 71 if(x=='F')i=1;
kikoaac 0:1faa9570d725 72 else if(x=='A'&&i==1)i=2;
kikoaac 0:1faa9570d725 73 else if(x=='I'&&i==2)i=3;
kikoaac 0:1faa9570d725 74 else if(x=='L'&&i==3) {
kikoaac 0:1faa9570d725 75 ans=false;
kikoaac 0:1faa9570d725 76 break;
kikoaac 0:1faa9570d725 77 } else i=0;
kikoaac 0:1faa9570d725 78
kikoaac 0:1faa9570d725 79 }
kikoaac 0:1faa9570d725 80 wait(0.1);
kikoaac 0:1faa9570d725 81 return ans;
kikoaac 0:1faa9570d725 82 }
kikoaac 0:1faa9570d725 83 bool connected;
kikoaac 0:1faa9570d725 84 public:
kikoaac 0:1faa9570d725 85 char* get_data(char *datax,int num) {
kikoaac 0:1faa9570d725 86 get:
kikoaac 0:1faa9570d725 87 char x[num+2];
kikoaac 0:1faa9570d725 88 Seri->scanf("%s",x);
kikoaac 0:1faa9570d725 89 //Serial pc(USBTX,USBRX);
kikoaac 0:1faa9570d725 90 //pc.baud(115200);
kikoaac 0:1faa9570d725 91 if(strstr(x,"+IPD,")==NULL) {
kikoaac 0:1faa9570d725 92
kikoaac 0:1faa9570d725 93 return NULL;
kikoaac 0:1faa9570d725 94 /*
kikoaac 0:1faa9570d725 95 if(strcmp(x,"0,CONNECT")==0)break;
kikoaac 0:1faa9570d725 96 if(strcmp(x,"0,CLOSED")==0)break;
kikoaac 0:1faa9570d725 97 if(strcmp(x,">")==0)break;*/
kikoaac 0:1faa9570d725 98 } else {
kikoaac 0:1faa9570d725 99
kikoaac 0:1faa9570d725 100 strtok(x,",");
kikoaac 0:1faa9570d725 101 strtok(NULL,":");
kikoaac 0:1faa9570d725 102 datax = strtok(NULL,",");
kikoaac 0:1faa9570d725 103 strtok(x,",");
kikoaac 0:1faa9570d725 104 char *data_num = strtok(NULL,",");
kikoaac 0:1faa9570d725 105 char len = atoi(data_num);
kikoaac 0:1faa9570d725 106
kikoaac 0:1faa9570d725 107 if(strcmp(datax,"\r\n")==0)goto get;
kikoaac 0:1faa9570d725 108 else if(len > num)return NULL;
kikoaac 0:1faa9570d725 109 //for(int i=0;i<len;i++)
kikoaac 0:1faa9570d725 110 // *datax++=*data++;
kikoaac 0:1faa9570d725 111 //printf("%s\r\n",datax);
kikoaac 0:1faa9570d725 112 return datax;
kikoaac 0:1faa9570d725 113 //data = strtok(x,"");
kikoaac 0:1faa9570d725 114 //buffer = data;
kikoaac 0:1faa9570d725 115 //pc.printf("%s , %s , %s\r\n",y,data_num,data);
kikoaac 0:1faa9570d725 116 }
kikoaac 0:1faa9570d725 117 }
kikoaac 0:1faa9570d725 118 bool sleep(int mode) {
kikoaac 0:1faa9570d725 119 Seri->printf("AT+SLEEP=%d",mode);
kikoaac 0:1faa9570d725 120 return check();
kikoaac 0:1faa9570d725 121 }
kikoaac 0:1faa9570d725 122 bool deepsleep(int ms) {
kikoaac 0:1faa9570d725 123 Seri->printf("AT+GSLP=%d",ms);
kikoaac 0:1faa9570d725 124 return check();
kikoaac 0:1faa9570d725 125 }
kikoaac 0:1faa9570d725 126 bool httpAccess(char* mode,char* url,int port=80) {
kikoaac 0:1faa9570d725 127 if(connected==false)return false;
kikoaac 0:1faa9570d725 128 Seri->printf("AT+CIPSTART=\"%s\",\"%s\",%d",mode,url,port);
kikoaac 0:1faa9570d725 129 return check();
kikoaac 0:1faa9570d725 130 }
kikoaac 0:1faa9570d725 131 bool httpAccess(char id,char* url,char* mode = "TCP",int port=80) {
kikoaac 0:1faa9570d725 132 if(connected==false)return false;
kikoaac 0:1faa9570d725 133 Seri->printf("AT+CIPSTART=\"%d\",\"%s\",\"%s\",%d",id,mode,url,port);
kikoaac 0:1faa9570d725 134 return check();
kikoaac 0:1faa9570d725 135 }
kikoaac 0:1faa9570d725 136 bool Disconnection(int id = 0)
kikoaac 0:1faa9570d725 137 {
kikoaac 0:1faa9570d725 138 printf("AT+CIPCLOSE==%d",id);
kikoaac 0:1faa9570d725 139 return check();
kikoaac 0:1faa9570d725 140 }
kikoaac 0:1faa9570d725 141 void prompt() {
kikoaac 0:1faa9570d725 142 Seri->attach(this,&esp8266::Reception);
kikoaac 0:1faa9570d725 143 }
kikoaac 0:1faa9570d725 144 //Seri->printf("AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"",IP,gateway,netmask);
kikoaac 0:1faa9570d725 145 bool setDHCP(int mode,int en) {
kikoaac 0:1faa9570d725 146 DHCPMode = mode;
kikoaac 0:1faa9570d725 147 DHCPenable = en;
kikoaac 0:1faa9570d725 148 printf("AT+CWDHCP_CUR=%d,%d",mode,en);
kikoaac 0:1faa9570d725 149 return check();
kikoaac 0:1faa9570d725 150 }
kikoaac 0:1faa9570d725 151 bool send( int num,char* data,int id) {
kikoaac 0:1faa9570d725 152 if(connected==false)return false;
kikoaac 0:1faa9570d725 153 Seri->printf("AT+CIPSEND=%d,%d",id,num+2);
kikoaac 0:1faa9570d725 154 check();
kikoaac 0:1faa9570d725 155 wait(0.01);
kikoaac 0:1faa9570d725 156 while(1) {
kikoaac 0:1faa9570d725 157 char i = Seri->getc();
kikoaac 0:1faa9570d725 158 if(i=='>')break;
kikoaac 0:1faa9570d725 159 }
kikoaac 0:1faa9570d725 160 Seri->printf("%s",data);
kikoaac 0:1faa9570d725 161 Seri->printf("\r\n");
kikoaac 0:1faa9570d725 162 return check();
kikoaac 0:1faa9570d725 163 }
kikoaac 0:1faa9570d725 164
kikoaac 0:1faa9570d725 165 bool multipull(int i) {
kikoaac 0:1faa9570d725 166 Seri->printf("AT+CIPMUX=%d",i);
kikoaac 0:1faa9570d725 167 return check();
kikoaac 0:1faa9570d725 168 }
kikoaac 0:1faa9570d725 169 void reset() {
kikoaac 0:1faa9570d725 170 Seri->printf("AT+RST\r\n");
kikoaac 0:1faa9570d725 171 strChack("ready");
kikoaac 0:1faa9570d725 172 wait(0.1);
kikoaac 0:1faa9570d725 173 }
kikoaac 0:1faa9570d725 174 void serverCreate(int mode,int port) {
kikoaac 0:1faa9570d725 175 Seri->printf("AT+CIPSERVER=%d,%d",mode,port);
kikoaac 0:1faa9570d725 176 check();
kikoaac 0:1faa9570d725 177 wait(0.1);
kikoaac 0:1faa9570d725 178 /*printf("\r\nwait connection\r\n");
kikoaac 0:1faa9570d725 179 while(1) {
kikoaac 0:1faa9570d725 180 char x[10];
kikoaac 0:1faa9570d725 181 Seri->scanf("%s",x);
kikoaac 0:1faa9570d725 182 if(strcmp(x,"0,CONNECT")==0)break;
kikoaac 0:1faa9570d725 183 }*/
kikoaac 0:1faa9570d725 184 //printf("\r\OK\r\n");
kikoaac 0:1faa9570d725 185 }
kikoaac 0:1faa9570d725 186 void serverTimeOutSet(int time = 60)
kikoaac 0:1faa9570d725 187 {
kikoaac 0:1faa9570d725 188 Seri->printf("AT+CIPSTO=%d",time);
kikoaac 0:1faa9570d725 189 check();
kikoaac 0:1faa9570d725 190 }
kikoaac 0:1faa9570d725 191
kikoaac 0:1faa9570d725 192 bool accesspoint() {
kikoaac 0:1faa9570d725 193 Seri->printf("AT+CWLAP\r\n");
kikoaac 0:1faa9570d725 194 return check();
kikoaac 0:1faa9570d725 195 }
kikoaac 0:1faa9570d725 196
kikoaac 0:1faa9570d725 197 bool wifiSoftAPSetting(const char* ssid,const char* password,int ch,int ecn) {
kikoaac 0:1faa9570d725 198 if(connectmode==AP||connectmode==APST)
kikoaac 0:1faa9570d725 199 Seri->printf("AT+CWSAP_CUR=\"%s\",\"%s\",%d,%d",ssid,password,ch,ecn);
kikoaac 0:1faa9570d725 200 else false;
kikoaac 0:1faa9570d725 201 return check();
kikoaac 0:1faa9570d725 202 }
kikoaac 0:1faa9570d725 203 bool wifiConnectionMode(int mode) {
kikoaac 0:1faa9570d725 204 connectmode=mode;
kikoaac 0:1faa9570d725 205 Seri->printf("AT+CWMODE_CUR=%d",mode);
kikoaac 0:1faa9570d725 206 return check();
kikoaac 0:1faa9570d725 207 }
kikoaac 0:1faa9570d725 208 bool wifiDisconnect() {
kikoaac 0:1faa9570d725 209 Seri->printf("AT+CWQAP");
kikoaac 0:1faa9570d725 210 connected=false;
kikoaac 0:1faa9570d725 211 return check();
kikoaac 0:1faa9570d725 212 }
kikoaac 0:1faa9570d725 213 bool WifiFindAccesspoint(const char *wifi) {
kikoaac 0:1faa9570d725 214 Seri->printf("AT+CWLAP=\"%s\"");
kikoaac 0:1faa9570d725 215 return check();
kikoaac 0:1faa9570d725 216 }
kikoaac 0:1faa9570d725 217 bool WifiFindAccesspoint() {
kikoaac 0:1faa9570d725 218 Seri->printf("AT+CWLAP");
kikoaac 0:1faa9570d725 219 return check();
kikoaac 0:1faa9570d725 220 }
kikoaac 0:1faa9570d725 221 bool wifiConnect(const char* ssid,const char* password) {
kikoaac 0:1faa9570d725 222 Seri->printf("AT+CWJAP_CUR=\"%s\",\"%s\"",ssid,password);
kikoaac 0:1faa9570d725 223 connected|=check();
kikoaac 0:1faa9570d725 224 //if(connected==false)return false;
kikoaac 0:1faa9570d725 225 //Seri->printf("AT+CIFSR");
kikoaac 0:1faa9570d725 226 //check();
kikoaac 0:1faa9570d725 227 return connected;
kikoaac 0:1faa9570d725 228 }
kikoaac 0:1faa9570d725 229 bool wifiSoftAPFromIP() {
kikoaac 0:1faa9570d725 230 if(connectmode == ST) return false;
kikoaac 0:1faa9570d725 231 Seri->printf("AT+CWLIF");
kikoaac 0:1faa9570d725 232 return check();
kikoaac 0:1faa9570d725 233 }
kikoaac 0:1faa9570d725 234 bool setSTMACAddress(char addr[6])
kikoaac 0:1faa9570d725 235 {
kikoaac 0:1faa9570d725 236 Seri->printf("AT+CIPSTART_CUR=\"%2x:%2x:%2x:%2x:%2x:%2x\"",addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]);
kikoaac 0:1faa9570d725 237 return check();
kikoaac 0:1faa9570d725 238 }
kikoaac 0:1faa9570d725 239 bool setAPMACAddress(char addr[6])
kikoaac 0:1faa9570d725 240 {
kikoaac 0:1faa9570d725 241 Seri->printf("AT+CIPAPMAC_CUR=\"%2x:%2x:%2x:%2x:%2x:%2x\"",addr[0],addr[1],addr[2],addr[3],addr[4],addr[5]);
kikoaac 0:1faa9570d725 242 return check();
kikoaac 0:1faa9570d725 243 }
kikoaac 0:1faa9570d725 244 bool setSTIPAddress(char addr[4])
kikoaac 0:1faa9570d725 245 {
kikoaac 0:1faa9570d725 246 Seri->printf("AT+CIPSTA_CUR=\"%3d:%3d:%3d:%3d\"",addr[0],addr[1],addr[2],addr[3]);
kikoaac 0:1faa9570d725 247 return check();
kikoaac 0:1faa9570d725 248 }
kikoaac 0:1faa9570d725 249 bool setAPIPAddress(char addr[4])
kikoaac 0:1faa9570d725 250 {
kikoaac 0:1faa9570d725 251 Seri->printf("AT+CIPAP_CUR=\"%3d:%3d:%3d:%3d\"",addr[0],addr[1],addr[2],addr[3]);
kikoaac 0:1faa9570d725 252 return check();
kikoaac 0:1faa9570d725 253 }
kikoaac 0:1faa9570d725 254
kikoaac 0:1faa9570d725 255
kikoaac 0:1faa9570d725 256
kikoaac 0:1faa9570d725 257 bool ATcommand(const char* cmd) {
kikoaac 0:1faa9570d725 258 char a[20] = "";
kikoaac 0:1faa9570d725 259 strcat(a,"AT");
kikoaac 0:1faa9570d725 260 strcat(a,cmd);
kikoaac 0:1faa9570d725 261 Seri->printf(a);
kikoaac 0:1faa9570d725 262 return check();
kikoaac 0:1faa9570d725 263 }
kikoaac 0:1faa9570d725 264 const char AP=2;
kikoaac 0:1faa9570d725 265 const char ST=1;
kikoaac 0:1faa9570d725 266 const char APST=3;
kikoaac 0:1faa9570d725 267 char connectmode;
kikoaac 0:1faa9570d725 268 char DHCPMode;
kikoaac 0:1faa9570d725 269 char DHCPenable;
kikoaac 0:1faa9570d725 270 esp8266(){
kikoaac 0:1faa9570d725 271 connected=false;
kikoaac 0:1faa9570d725 272
kikoaac 0:1faa9570d725 273 DHCPMode = 0;
kikoaac 0:1faa9570d725 274 DHCPenable = 1;
kikoaac 0:1faa9570d725 275 //Seri = ser;
kikoaac 0:1faa9570d725 276 //Seri->printf("AT\r\n");
kikoaac 0:1faa9570d725 277
kikoaac 0:1faa9570d725 278 //strChack("OK");
kikoaac 0:1faa9570d725 279 //reset();
kikoaac 0:1faa9570d725 280 wait(0.5);
kikoaac 0:1faa9570d725 281 }
kikoaac 0:1faa9570d725 282 void setSerial(Serial *ser)
kikoaac 0:1faa9570d725 283 {
kikoaac 0:1faa9570d725 284 Seri = ser;
kikoaac 0:1faa9570d725 285
kikoaac 0:1faa9570d725 286 //Seri->printf("AT\r\n");
kikoaac 0:1faa9570d725 287
kikoaac 0:1faa9570d725 288 //strChack("OK");
kikoaac 0:1faa9570d725 289 }
kikoaac 0:1faa9570d725 290 };