owo/
Dependencies: BSP_DISCO_F469NI LCD_DISCO_F469NI mbed
main.cpp@0:67c4bcd9b4c9, 2017-10-10 (annotated)
- Committer:
- Toby_Chen
- Date:
- Tue Oct 10 16:19:30 2017 +0000
- Revision:
- 0:67c4bcd9b4c9
owo/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Toby_Chen | 0:67c4bcd9b4c9 | 1 | #include "mbed.h" |
Toby_Chen | 0:67c4bcd9b4c9 | 2 | #include "LCD_DISCO_F469NI.h" |
Toby_Chen | 0:67c4bcd9b4c9 | 3 | #include "string.h" |
Toby_Chen | 0:67c4bcd9b4c9 | 4 | #include "stdio.h" |
Toby_Chen | 0:67c4bcd9b4c9 | 5 | |
Toby_Chen | 0:67c4bcd9b4c9 | 6 | Serial pc(USBTX, USBRX); |
Toby_Chen | 0:67c4bcd9b4c9 | 7 | Serial esp(D1, D0); // tx, rx |
Toby_Chen | 0:67c4bcd9b4c9 | 8 | DigitalOut reset(D10); |
Toby_Chen | 0:67c4bcd9b4c9 | 9 | Timer t; |
Toby_Chen | 0:67c4bcd9b4c9 | 10 | |
Toby_Chen | 0:67c4bcd9b4c9 | 11 | int count,ended,timeout; |
Toby_Chen | 0:67c4bcd9b4c9 | 12 | char buf[102400]; |
Toby_Chen | 0:67c4bcd9b4c9 | 13 | char snd[255]; |
Toby_Chen | 0:67c4bcd9b4c9 | 14 | |
Toby_Chen | 0:67c4bcd9b4c9 | 15 | uint8_t text[100]; |
Toby_Chen | 0:67c4bcd9b4c9 | 16 | char textc[100]; |
Toby_Chen | 0:67c4bcd9b4c9 | 17 | |
Toby_Chen | 0:67c4bcd9b4c9 | 18 | |
Toby_Chen | 0:67c4bcd9b4c9 | 19 | char ssid[32] = "TobyTechnology"; // enter WiFi router ssid inside the quotes |
Toby_Chen | 0:67c4bcd9b4c9 | 20 | char pwd [32] = "19980106"; // enter WiFi router password inside the quotes |
Toby_Chen | 0:67c4bcd9b4c9 | 21 | |
Toby_Chen | 0:67c4bcd9b4c9 | 22 | void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(); |
Toby_Chen | 0:67c4bcd9b4c9 | 23 | LCD_DISCO_F469NI lcd; |
Toby_Chen | 0:67c4bcd9b4c9 | 24 | |
Toby_Chen | 0:67c4bcd9b4c9 | 25 | int main() |
Toby_Chen | 0:67c4bcd9b4c9 | 26 | { |
Toby_Chen | 0:67c4bcd9b4c9 | 27 | reset=0; //hardware reset for 8266 |
Toby_Chen | 0:67c4bcd9b4c9 | 28 | |
Toby_Chen | 0:67c4bcd9b4c9 | 29 | reset=1; |
Toby_Chen | 0:67c4bcd9b4c9 | 30 | timeout=2; |
Toby_Chen | 0:67c4bcd9b4c9 | 31 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 32 | |
Toby_Chen | 0:67c4bcd9b4c9 | 33 | esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. |
Toby_Chen | 0:67c4bcd9b4c9 | 34 | |
Toby_Chen | 0:67c4bcd9b4c9 | 35 | //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** |
Toby_Chen | 0:67c4bcd9b4c9 | 36 | |
Toby_Chen | 0:67c4bcd9b4c9 | 37 | ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** |
Toby_Chen | 0:67c4bcd9b4c9 | 38 | |
Toby_Chen | 0:67c4bcd9b4c9 | 39 | lcd.Clear(LCD_COLOR_WHITE); |
Toby_Chen | 0:67c4bcd9b4c9 | 40 | |
Toby_Chen | 0:67c4bcd9b4c9 | 41 | // continuosly get AP list and IP |
Toby_Chen | 0:67c4bcd9b4c9 | 42 | while(1) { |
Toby_Chen | 0:67c4bcd9b4c9 | 43 | |
Toby_Chen | 0:67c4bcd9b4c9 | 44 | |
Toby_Chen | 0:67c4bcd9b4c9 | 45 | lcd.DisplayStringAt(10, LINE(0), (uint8_t *)"---------- Connecting ----------", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 46 | strcpy(snd, "AT+CIPSTART=\"TCP\",\"192.168.137.12\",80\r\n"); |
Toby_Chen | 0:67c4bcd9b4c9 | 47 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 48 | timeout=5; |
Toby_Chen | 0:67c4bcd9b4c9 | 49 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 50 | lcd.DisplayStringAt(11, LINE(1), (uint8_t *)buf, LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 51 | wait(0.1); |
Toby_Chen | 0:67c4bcd9b4c9 | 52 | |
Toby_Chen | 0:67c4bcd9b4c9 | 53 | lcd.DisplayStringAt(10, LINE(5), (uint8_t *)"---------- Sending Data ----------", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 54 | |
Toby_Chen | 0:67c4bcd9b4c9 | 55 | char data[500] = "GET /index.php?id=5 HTTP/1.1\r\nHost: 192.168.137.12\r\n\r\n"; |
Toby_Chen | 0:67c4bcd9b4c9 | 56 | sprintf(textc,"AT+CIPSEND=%d\r\n", strlen(data)); |
Toby_Chen | 0:67c4bcd9b4c9 | 57 | strcpy(snd, textc); |
Toby_Chen | 0:67c4bcd9b4c9 | 58 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 59 | timeout=3; |
Toby_Chen | 0:67c4bcd9b4c9 | 60 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 61 | lcd.DisplayStringAt(11, LINE(6), (uint8_t *)buf, LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 62 | wait(0.1); |
Toby_Chen | 0:67c4bcd9b4c9 | 63 | |
Toby_Chen | 0:67c4bcd9b4c9 | 64 | strcpy(snd, data); |
Toby_Chen | 0:67c4bcd9b4c9 | 65 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 66 | timeout=3; |
Toby_Chen | 0:67c4bcd9b4c9 | 67 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 68 | lcd.DisplayStringAt(11, LINE(10), (uint8_t *)buf, LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 69 | |
Toby_Chen | 0:67c4bcd9b4c9 | 70 | // |
Toby_Chen | 0:67c4bcd9b4c9 | 71 | wait(5); |
Toby_Chen | 0:67c4bcd9b4c9 | 72 | lcd.Clear(LCD_COLOR_WHITE); |
Toby_Chen | 0:67c4bcd9b4c9 | 73 | |
Toby_Chen | 0:67c4bcd9b4c9 | 74 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 75 | |
Toby_Chen | 0:67c4bcd9b4c9 | 76 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 77 | |
Toby_Chen | 0:67c4bcd9b4c9 | 78 | // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed |
Toby_Chen | 0:67c4bcd9b4c9 | 79 | void ESPsetbaudrate() |
Toby_Chen | 0:67c4bcd9b4c9 | 80 | { |
Toby_Chen | 0:67c4bcd9b4c9 | 81 | strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate |
Toby_Chen | 0:67c4bcd9b4c9 | 82 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 83 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 84 | |
Toby_Chen | 0:67c4bcd9b4c9 | 85 | // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ |
Toby_Chen | 0:67c4bcd9b4c9 | 86 | void ESPconfig() |
Toby_Chen | 0:67c4bcd9b4c9 | 87 | { |
Toby_Chen | 0:67c4bcd9b4c9 | 88 | //RESETTING |
Toby_Chen | 0:67c4bcd9b4c9 | 89 | lcd.DisplayStringAt(0, LINE(0), (uint8_t *)"---------- Reset... ----------", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 90 | strcpy(snd,"AT+RST\r\n"); |
Toby_Chen | 0:67c4bcd9b4c9 | 91 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 92 | timeout=1; |
Toby_Chen | 0:67c4bcd9b4c9 | 93 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 94 | lcd.DisplayStringAt(0, LINE(1), (uint8_t *)"<Reset Done>", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 95 | |
Toby_Chen | 0:67c4bcd9b4c9 | 96 | |
Toby_Chen | 0:67c4bcd9b4c9 | 97 | // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
Toby_Chen | 0:67c4bcd9b4c9 | 98 | lcd.DisplayStringAt(0, LINE(3), (uint8_t *)"---------- Mode Setting... ----------", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 99 | strcpy(snd, "AT+CWMODE=1\r\n"); |
Toby_Chen | 0:67c4bcd9b4c9 | 100 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 101 | timeout=1; |
Toby_Chen | 0:67c4bcd9b4c9 | 102 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 103 | /* |
Toby_Chen | 0:67c4bcd9b4c9 | 104 | for( int i=0 ; buf[i]!='\0' ; i++ ){ |
Toby_Chen | 0:67c4bcd9b4c9 | 105 | char str[10]; |
Toby_Chen | 0:67c4bcd9b4c9 | 106 | sprintf(str,"%d ",(int)buf[i]); |
Toby_Chen | 0:67c4bcd9b4c9 | 107 | lcd.DisplayStringAt(i*30, LINE(5), (uint8_t *)str, LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 108 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 109 | */ |
Toby_Chen | 0:67c4bcd9b4c9 | 110 | //lcd.DisplayStringAt(0, LINE(10), (uint8_t *)buf, LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 111 | |
Toby_Chen | 0:67c4bcd9b4c9 | 112 | if( !strcmp( buf , "AT+CWMODE=1\r\r\n\r\nOK\r\n" ) ) |
Toby_Chen | 0:67c4bcd9b4c9 | 113 | lcd.DisplayStringAt(0, LINE(4), (uint8_t *)"<Mode Setting done>", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 114 | else { lcd.DisplayStringAt(0, LINE(4), (uint8_t *)"<Mode Setting failed>...FUCK...", LEFT_MODE ); wait(5); } |
Toby_Chen | 0:67c4bcd9b4c9 | 115 | |
Toby_Chen | 0:67c4bcd9b4c9 | 116 | // set CIPMUX to 0=Single,1=Multi |
Toby_Chen | 0:67c4bcd9b4c9 | 117 | lcd.DisplayStringAt(0, LINE(6), (uint8_t *)"---------- Connection Mode Setting... ----------", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 118 | strcpy(snd, "AT+CIPMUX=0\r\n"); |
Toby_Chen | 0:67c4bcd9b4c9 | 119 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 120 | timeout=1; |
Toby_Chen | 0:67c4bcd9b4c9 | 121 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 122 | |
Toby_Chen | 0:67c4bcd9b4c9 | 123 | if( !strcmp( buf , "AT+CIPMUX=0\r\r\n\r\nOK\r\n" ) ) |
Toby_Chen | 0:67c4bcd9b4c9 | 124 | lcd.DisplayStringAt(0, LINE(7), (uint8_t *)"<CIPMUX=0 done>", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 125 | else{ lcd.DisplayStringAt(0, LINE(7), (uint8_t *)"<Mux Setting failed>...FUCK...-", LEFT_MODE ); wait(5);} |
Toby_Chen | 0:67c4bcd9b4c9 | 126 | |
Toby_Chen | 0:67c4bcd9b4c9 | 127 | lcd.DisplayStringAt(0, LINE(9), (uint8_t *)"---------- Connecting to AP... ----------", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 128 | sprintf((char*)text,"ssid = %s pwd = %s",ssid,pwd); |
Toby_Chen | 0:67c4bcd9b4c9 | 129 | strcpy(snd, "AT+CWJAP=\""); |
Toby_Chen | 0:67c4bcd9b4c9 | 130 | strcat(snd, ssid); |
Toby_Chen | 0:67c4bcd9b4c9 | 131 | strcat(snd, "\",\""); |
Toby_Chen | 0:67c4bcd9b4c9 | 132 | strcat(snd, pwd); |
Toby_Chen | 0:67c4bcd9b4c9 | 133 | strcat(snd, "\"\r\n"); |
Toby_Chen | 0:67c4bcd9b4c9 | 134 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 135 | timeout=3; |
Toby_Chen | 0:67c4bcd9b4c9 | 136 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 137 | /* |
Toby_Chen | 0:67c4bcd9b4c9 | 138 | if( !strcmp( buf , |
Toby_Chen | 0:67c4bcd9b4c9 | 139 | "TobyTechnology\",\"19980106\"\r\nWIFI DISCONNECT\r\nWIFI CONNECTED\r\nWIFI GOT IP\r\r\n\r\nOK" ) |
Toby_Chen | 0:67c4bcd9b4c9 | 140 | lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"<WIFI Connected>", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 141 | else{ lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"<WIFI Connectting failed>...FUCK...-", LEFT_MODE ); wait(5);} |
Toby_Chen | 0:67c4bcd9b4c9 | 142 | */ |
Toby_Chen | 0:67c4bcd9b4c9 | 143 | |
Toby_Chen | 0:67c4bcd9b4c9 | 144 | lcd.DisplayStringAt(0, LINE(10), (uint8_t *)"---------- Get IP... ----------", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 145 | strcpy(snd, "AT+CIFSR\r\n"); |
Toby_Chen | 0:67c4bcd9b4c9 | 146 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 147 | timeout=1; |
Toby_Chen | 0:67c4bcd9b4c9 | 148 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 149 | lcd.DisplayStringAt(0, LINE(11), (uint8_t *)buf, LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 150 | |
Toby_Chen | 0:67c4bcd9b4c9 | 151 | strcpy(snd, "AT+CIPSTATUS\r\n"); |
Toby_Chen | 0:67c4bcd9b4c9 | 152 | SendCMD(); |
Toby_Chen | 0:67c4bcd9b4c9 | 153 | timeout=1; |
Toby_Chen | 0:67c4bcd9b4c9 | 154 | getreply(); |
Toby_Chen | 0:67c4bcd9b4c9 | 155 | lcd.DisplayStringAt(0, LINE(12), (uint8_t *)"Status:", LEFT_MODE ); |
Toby_Chen | 0:67c4bcd9b4c9 | 156 | lcd.DisplayStringAt(50, LINE(12), (uint8_t *)buf, LEFT_MODE ); wait(1); |
Toby_Chen | 0:67c4bcd9b4c9 | 157 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 158 | |
Toby_Chen | 0:67c4bcd9b4c9 | 159 | void SendCMD() |
Toby_Chen | 0:67c4bcd9b4c9 | 160 | { |
Toby_Chen | 0:67c4bcd9b4c9 | 161 | esp.printf("%s", snd); |
Toby_Chen | 0:67c4bcd9b4c9 | 162 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 163 | |
Toby_Chen | 0:67c4bcd9b4c9 | 164 | void getreply() |
Toby_Chen | 0:67c4bcd9b4c9 | 165 | { |
Toby_Chen | 0:67c4bcd9b4c9 | 166 | memset(buf, '\0', sizeof(buf)); |
Toby_Chen | 0:67c4bcd9b4c9 | 167 | t.start(); |
Toby_Chen | 0:67c4bcd9b4c9 | 168 | ended=0; |
Toby_Chen | 0:67c4bcd9b4c9 | 169 | count=0; |
Toby_Chen | 0:67c4bcd9b4c9 | 170 | while(!ended) { |
Toby_Chen | 0:67c4bcd9b4c9 | 171 | if(esp.readable()) { |
Toby_Chen | 0:67c4bcd9b4c9 | 172 | buf[count] = esp.getc(); |
Toby_Chen | 0:67c4bcd9b4c9 | 173 | count++; |
Toby_Chen | 0:67c4bcd9b4c9 | 174 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 175 | if(t.read() > timeout) { |
Toby_Chen | 0:67c4bcd9b4c9 | 176 | ended = 1; |
Toby_Chen | 0:67c4bcd9b4c9 | 177 | t.stop(); |
Toby_Chen | 0:67c4bcd9b4c9 | 178 | t.reset(); |
Toby_Chen | 0:67c4bcd9b4c9 | 179 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 180 | } |
Toby_Chen | 0:67c4bcd9b4c9 | 181 | } |