Working Version Rev 1. Connect with local server :)

Dependencies:   ESP8266 mbed

Committer:
mustwillza
Date:
Sun Dec 06 18:17:40 2015 +0000
Revision:
0:05c6874e0a54
First Working!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mustwillza 0:05c6874e0a54 1 #include "mbed.h"
mustwillza 0:05c6874e0a54 2 #include <string>
mustwillza 0:05c6874e0a54 3 #include "lfCall.h" //Lost and Found Box Library for HTTP Req.
mustwillza 0:05c6874e0a54 4 #define BUFFER_SIZE 100
mustwillza 0:05c6874e0a54 5
mustwillza 0:05c6874e0a54 6 int count,ended,timeout;
mustwillza 0:05c6874e0a54 7 Serial esp(D8, D2); //tx rx
mustwillza 0:05c6874e0a54 8 Serial pc(USBTX, USBRX);
mustwillza 0:05c6874e0a54 9 Timer t; //Timeout comparison for get reply ESP8266
mustwillza 0:05c6874e0a54 10 char buf[1024]; //reply buffer for esp8266
mustwillza 0:05c6874e0a54 11 int setupESP(string,string);
mustwillza 0:05c6874e0a54 12 char snd[256];
mustwillza 0:05c6874e0a54 13
mustwillza 0:05c6874e0a54 14 void getDataHTTP(const char domain[],const char dir[],const char value[],char uid[] , char name[] ,char tel[]);
mustwillza 0:05c6874e0a54 15
mustwillza 0:05c6874e0a54 16
mustwillza 0:05c6874e0a54 17 void SendCMD()
mustwillza 0:05c6874e0a54 18 {
mustwillza 0:05c6874e0a54 19 esp.printf("%s", snd);
mustwillza 0:05c6874e0a54 20 }
mustwillza 0:05c6874e0a54 21
mustwillza 0:05c6874e0a54 22
mustwillza 0:05c6874e0a54 23 void getreply()
mustwillza 0:05c6874e0a54 24 {
mustwillza 0:05c6874e0a54 25 memset(buf, '\0', sizeof(buf));
mustwillza 0:05c6874e0a54 26 t.start();
mustwillza 0:05c6874e0a54 27 ended=0;count=0;
mustwillza 0:05c6874e0a54 28 while(!ended) {
mustwillza 0:05c6874e0a54 29 if(esp.readable()) {
mustwillza 0:05c6874e0a54 30 buf[count] = esp.getc();count++;
mustwillza 0:05c6874e0a54 31 }
mustwillza 0:05c6874e0a54 32 if(t.read() > timeout) {
mustwillza 0:05c6874e0a54 33 ended = 1;t.stop();t.reset();
mustwillza 0:05c6874e0a54 34 }
mustwillza 0:05c6874e0a54 35 }
mustwillza 0:05c6874e0a54 36 }
mustwillza 0:05c6874e0a54 37
mustwillza 0:05c6874e0a54 38
mustwillza 0:05c6874e0a54 39 int main()
mustwillza 0:05c6874e0a54 40 {
mustwillza 0:05c6874e0a54 41
mustwillza 0:05c6874e0a54 42 string ssid = "1544_2G";
mustwillza 0:05c6874e0a54 43 string pass = "fiborobot" ;
mustwillza 0:05c6874e0a54 44 esp.baud(9600);
mustwillza 0:05c6874e0a54 45 pc.baud(115200);
mustwillza 0:05c6874e0a54 46
mustwillza 0:05c6874e0a54 47
mustwillza 0:05c6874e0a54 48 pc.printf("Starting System...\n");
mustwillza 0:05c6874e0a54 49
mustwillza 0:05c6874e0a54 50 if(setupESP(ssid,pass) == 1) {
mustwillza 0:05c6874e0a54 51 pc.printf("System connected to %s with %s as password\n",ssid.c_str(),pass.c_str());
mustwillza 0:05c6874e0a54 52 pc.printf("System Initialized :: ESP8266 Module Ready!\n");
mustwillza 0:05c6874e0a54 53 wait(2);
mustwillza 0:05c6874e0a54 54 }
mustwillza 0:05c6874e0a54 55 getreply();
mustwillza 0:05c6874e0a54 56 char name[20];
mustwillza 0:05c6874e0a54 57 char uid[20];
mustwillza 0:05c6874e0a54 58 char tel[10];
mustwillza 0:05c6874e0a54 59
mustwillza 0:05c6874e0a54 60
mustwillza 0:05c6874e0a54 61 getDataHTTP("192.168.1.88","/l-f-box/getdata.php?rfid=","1",name,uid,tel);
mustwillza 0:05c6874e0a54 62 pc.printf("after call function : \n");
mustwillza 0:05c6874e0a54 63 pc.printf("ID : %s\n",uid);
mustwillza 0:05c6874e0a54 64 pc.printf("Name : %s\n",name);
mustwillza 0:05c6874e0a54 65 pc.printf("Tel : %s\n",tel);
mustwillza 0:05c6874e0a54 66
mustwillza 0:05c6874e0a54 67
mustwillza 0:05c6874e0a54 68 }
mustwillza 0:05c6874e0a54 69
mustwillza 0:05c6874e0a54 70 int setupESP(string ssid,string password)
mustwillza 0:05c6874e0a54 71 {
mustwillza 0:05c6874e0a54 72 pc.printf("Sending Reset CMD\n");
mustwillza 0:05c6874e0a54 73 esp.printf("AT+RST\r\n"); /* reset module */
mustwillza 0:05c6874e0a54 74 wait(3);
mustwillza 0:05c6874e0a54 75
mustwillza 0:05c6874e0a54 76 esp.printf("AT+CWMODE=1\r\n"); /* configure as access point */
mustwillza 0:05c6874e0a54 77 timeout = 2;
mustwillza 0:05c6874e0a54 78 getreply();
mustwillza 0:05c6874e0a54 79 pc.printf(buf);
mustwillza 0:05c6874e0a54 80 /* Error checking is not required */
mustwillza 0:05c6874e0a54 81
mustwillza 0:05c6874e0a54 82 pc.printf("Access Point List!\n");
mustwillza 0:05c6874e0a54 83 esp.printf("AT+CWLAP\r\n");
mustwillza 0:05c6874e0a54 84 timeout = 5;
mustwillza 0:05c6874e0a54 85 getreply();
mustwillza 0:05c6874e0a54 86 pc.printf(buf);
mustwillza 0:05c6874e0a54 87 wait(1);
mustwillza 0:05c6874e0a54 88
mustwillza 0:05c6874e0a54 89
mustwillza 0:05c6874e0a54 90 string buffer("AT+CWJAP=\"");
mustwillza 0:05c6874e0a54 91 buffer += ssid;
mustwillza 0:05c6874e0a54 92 buffer += "\",\"";
mustwillza 0:05c6874e0a54 93 buffer += password;
mustwillza 0:05c6874e0a54 94 buffer += "\"\r\n";
mustwillza 0:05c6874e0a54 95
mustwillza 0:05c6874e0a54 96 esp.printf(buffer.c_str());
mustwillza 0:05c6874e0a54 97 timeout = 7;
mustwillza 0:05c6874e0a54 98 getreply();
mustwillza 0:05c6874e0a54 99 pc.printf(buf);
mustwillza 0:05c6874e0a54 100 wait(1);
mustwillza 0:05c6874e0a54 101
mustwillza 0:05c6874e0a54 102 esp.printf("AT+CIPMUX=1\r\n");
mustwillza 0:05c6874e0a54 103 timeout =5;
mustwillza 0:05c6874e0a54 104 getreply();
mustwillza 0:05c6874e0a54 105 pc.printf("Open Multi Connection : %s\n",buf);
mustwillza 0:05c6874e0a54 106 wait(1);
mustwillza 0:05c6874e0a54 107
mustwillza 0:05c6874e0a54 108 pc.printf("\n---------- Get IP's ----------\r\n");
mustwillza 0:05c6874e0a54 109 char aa[]= "AT+CIFSR\r\n";
mustwillza 0:05c6874e0a54 110 esp.printf(aa);
mustwillza 0:05c6874e0a54 111 timeout=4;
mustwillza 0:05c6874e0a54 112 getreply();
mustwillza 0:05c6874e0a54 113 pc.printf(buf);
mustwillza 0:05c6874e0a54 114
mustwillza 0:05c6874e0a54 115 wait(1);
mustwillza 0:05c6874e0a54 116
mustwillza 0:05c6874e0a54 117 return 1;
mustwillza 0:05c6874e0a54 118 }
mustwillza 0:05c6874e0a54 119
mustwillza 0:05c6874e0a54 120
mustwillza 0:05c6874e0a54 121 // domain : 192.168.1.88
mustwillza 0:05c6874e0a54 122 // directory : /l-f-box/getdata.php?rfid=
mustwillza 0:05c6874e0a54 123 // value : 1
mustwillza 0:05c6874e0a54 124 void getDataHTTP(const char domain[],const char dir[],const char value[],char uid[] , char name[] ,char tel[]){
mustwillza 0:05c6874e0a54 125
mustwillza 0:05c6874e0a54 126 pc.printf("Start HTTP Request\n");
mustwillza 0:05c6874e0a54 127 esp.printf("AT+CIPSTART=0,\"TCP\",\"%s\",80\r\n",domain);
mustwillza 0:05c6874e0a54 128 timeout=5;
mustwillza 0:05c6874e0a54 129 getreply();
mustwillza 0:05c6874e0a54 130 pc.printf(buf);
mustwillza 0:05c6874e0a54 131
mustwillza 0:05c6874e0a54 132 wait(1);
mustwillza 0:05c6874e0a54 133
mustwillza 0:05c6874e0a54 134 char cmd[128] = "GET http://";
mustwillza 0:05c6874e0a54 135 strcat(cmd,domain);
mustwillza 0:05c6874e0a54 136 strcat(cmd,dir);
mustwillza 0:05c6874e0a54 137 strcat(cmd,value);
mustwillza 0:05c6874e0a54 138 //="GET http://%s%s%s",domain,dir,value;
mustwillza 0:05c6874e0a54 139 char buffer[2] = "";
mustwillza 0:05c6874e0a54 140 strcat(cmd," HTTP/1.0\r\n\r\n");
mustwillza 0:05c6874e0a54 141 sprintf(buffer , "%d",strlen(cmd) );
mustwillza 0:05c6874e0a54 142
mustwillza 0:05c6874e0a54 143 strcpy(snd,"AT+CIPSEND=0,");
mustwillza 0:05c6874e0a54 144 strcat(snd,buffer);
mustwillza 0:05c6874e0a54 145 strcat(snd,"\r\n");
mustwillza 0:05c6874e0a54 146 pc.printf("%d %d",strlen(cmd) ,strlen(snd));
mustwillza 0:05c6874e0a54 147 pc.printf(snd);
mustwillza 0:05c6874e0a54 148 pc.printf(cmd);
mustwillza 0:05c6874e0a54 149
mustwillza 0:05c6874e0a54 150
mustwillza 0:05c6874e0a54 151 SendCMD();
mustwillza 0:05c6874e0a54 152 timeout=5;
mustwillza 0:05c6874e0a54 153 getreply();
mustwillza 0:05c6874e0a54 154 pc.printf(buf);
mustwillza 0:05c6874e0a54 155 wait(2);
mustwillza 0:05c6874e0a54 156
mustwillza 0:05c6874e0a54 157 pc.printf("Start point to url...\n");
mustwillza 0:05c6874e0a54 158 strcpy(snd,cmd);
mustwillza 0:05c6874e0a54 159 SendCMD();
mustwillza 0:05c6874e0a54 160 timeout=5;
mustwillza 0:05c6874e0a54 161 getreply();
mustwillza 0:05c6874e0a54 162 pc.printf(buf);
mustwillza 0:05c6874e0a54 163 char* data;
mustwillza 0:05c6874e0a54 164 /* char * uid;
mustwillza 0:05c6874e0a54 165 char * name;
mustwillza 0:05c6874e0a54 166 char * tel;
mustwillza 0:05c6874e0a54 167 */
mustwillza 0:05c6874e0a54 168 data = strstr(buf,"</data>");
mustwillza 0:05c6874e0a54 169 data = data+7;
mustwillza 0:05c6874e0a54 170 data = strtok(data,";");
mustwillza 0:05c6874e0a54 171 strcpy(uid,data+2);
mustwillza 0:05c6874e0a54 172 //uid = data+2;
mustwillza 0:05c6874e0a54 173 data = strtok(NULL,";");
mustwillza 0:05c6874e0a54 174 strcpy(name,data+2);
mustwillza 0:05c6874e0a54 175 //name = data+2;
mustwillza 0:05c6874e0a54 176 data = strtok(NULL,";");
mustwillza 0:05c6874e0a54 177 strcpy(tel,data+2);
mustwillza 0:05c6874e0a54 178 // tel = data+2;
mustwillza 0:05c6874e0a54 179
mustwillza 0:05c6874e0a54 180 pc.printf("--Extract data from http--\n");
mustwillza 0:05c6874e0a54 181
mustwillza 0:05c6874e0a54 182 pc.printf("ID : %s : %d\n",uid,strlen(uid));
mustwillza 0:05c6874e0a54 183 pc.printf("Name : %s : %d\n",name,strlen(name));
mustwillza 0:05c6874e0a54 184 pc.printf("Tel : %s : %d\n",tel,strlen(tel));
mustwillza 0:05c6874e0a54 185
mustwillza 0:05c6874e0a54 186 esp.printf("AT+CIPCLOSE=0\r\n");
mustwillza 0:05c6874e0a54 187 timeout=5;
mustwillza 0:05c6874e0a54 188 getreply();
mustwillza 0:05c6874e0a54 189 pc.printf(buf);
mustwillza 0:05c6874e0a54 190 }
mustwillza 0:05c6874e0a54 191