Master Program
Dependencies: BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed
main.cpp@1:b6e184239fba, 2015-10-31 (annotated)
- Committer:
- GabDiSi
- Date:
- Sat Oct 31 23:35:40 2015 +0000
- Revision:
- 1:b6e184239fba
- Parent:
- 0:beeeb5671c2a
- Child:
- 2:b05f40331fc3
LCD implemented
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GabDiSi | 0:beeeb5671c2a | 1 | #include <string.h> |
GabDiSi | 0:beeeb5671c2a | 2 | #include "mbed.h" |
GabDiSi | 1:b6e184239fba | 3 | #include "TS_DISCO_F746NG.h" |
GabDiSi | 1:b6e184239fba | 4 | #include "LCD_DISCO_F746NG.h" |
GabDiSi | 1:b6e184239fba | 5 | |
GabDiSi | 1:b6e184239fba | 6 | LCD_DISCO_F746NG lcd; |
GabDiSi | 1:b6e184239fba | 7 | TS_DISCO_F746NG ts; |
GabDiSi | 0:beeeb5671c2a | 8 | |
GabDiSi | 0:beeeb5671c2a | 9 | Serial pc(USBTX, USBRX); // tx, rx |
GabDiSi | 0:beeeb5671c2a | 10 | Serial serial6(D1, D0); // tx, rx |
GabDiSi | 0:beeeb5671c2a | 11 | DigitalOut led1(LED1); |
GabDiSi | 0:beeeb5671c2a | 12 | |
GabDiSi | 1:b6e184239fba | 13 | int lineDisplay = 0; |
GabDiSi | 0:beeeb5671c2a | 14 | int ESP8266limitResponse = 999999; |
GabDiSi | 0:beeeb5671c2a | 15 | |
GabDiSi | 1:b6e184239fba | 16 | char ESP8266responseOkStr[] = "OK\r\n"; |
GabDiSi | 1:b6e184239fba | 17 | char ESP8266responseGetStr[] = "OK\r\n> "; |
GabDiSi | 1:b6e184239fba | 18 | char ESP8266responseReadyStr[] = "ready\r\n"; |
GabDiSi | 1:b6e184239fba | 19 | char ESP8266responseSendStr[] = "SEND OK\r\n\r\n"; |
GabDiSi | 1:b6e184239fba | 20 | char ESP8266responseClosedStr[] = "CLOSED\r\n"; |
GabDiSi | 1:b6e184239fba | 21 | |
GabDiSi | 1:b6e184239fba | 22 | char ESP8266initStr[] = "ROPE WIFI - TECHNOLOGY DAY"; |
GabDiSi | 1:b6e184239fba | 23 | char ESP8266doneStr[] = "done!"; |
GabDiSi | 1:b6e184239fba | 24 | char ESP8266resetStr[] = "Init ESP8266... "; |
GabDiSi | 1:b6e184239fba | 25 | char ESP8266clientModeStr[] = "Client mode... "; |
GabDiSi | 1:b6e184239fba | 26 | char ESP8266wifiConnectionStr[] = "WiFi connection... "; |
GabDiSi | 1:b6e184239fba | 27 | char ESP8266serverConnectionStr[] = "Server connection... "; |
GabDiSi | 1:b6e184239fba | 28 | char ESP8266sendingCharsStr[] = "Sending chars... "; |
GabDiSi | 1:b6e184239fba | 29 | char ESP8266sendingRequestStr[] = "Sending request... "; |
GabDiSi | 0:beeeb5671c2a | 30 | |
GabDiSi | 0:beeeb5671c2a | 31 | bool ESP8266readResponse(char* delimiter, char* response) { |
GabDiSi | 0:beeeb5671c2a | 32 | int i = 0, j = 0; |
GabDiSi | 0:beeeb5671c2a | 33 | char c; |
GabDiSi | 0:beeeb5671c2a | 34 | |
GabDiSi | 0:beeeb5671c2a | 35 | while(j<ESP8266limitResponse) { |
GabDiSi | 0:beeeb5671c2a | 36 | c = serial6.getc(); |
GabDiSi | 0:beeeb5671c2a | 37 | response[j++] = c; |
GabDiSi | 0:beeeb5671c2a | 38 | |
GabDiSi | 0:beeeb5671c2a | 39 | if(c == delimiter[i]) { |
GabDiSi | 0:beeeb5671c2a | 40 | i++; |
GabDiSi | 0:beeeb5671c2a | 41 | |
GabDiSi | 0:beeeb5671c2a | 42 | if(delimiter[i] == '\0') { |
GabDiSi | 0:beeeb5671c2a | 43 | response[j] = '\0'; |
GabDiSi | 0:beeeb5671c2a | 44 | return true; |
GabDiSi | 0:beeeb5671c2a | 45 | }; |
GabDiSi | 0:beeeb5671c2a | 46 | } else { |
GabDiSi | 0:beeeb5671c2a | 47 | i = 0; |
GabDiSi | 0:beeeb5671c2a | 48 | }; |
GabDiSi | 0:beeeb5671c2a | 49 | }; |
GabDiSi | 0:beeeb5671c2a | 50 | |
GabDiSi | 0:beeeb5671c2a | 51 | response[j] = '\0'; |
GabDiSi | 0:beeeb5671c2a | 52 | return false; |
GabDiSi | 0:beeeb5671c2a | 53 | }; |
GabDiSi | 0:beeeb5671c2a | 54 | |
GabDiSi | 0:beeeb5671c2a | 55 | void ESP8266init(char* ssid, char* password) { |
GabDiSi | 0:beeeb5671c2a | 56 | led1 = 0; |
GabDiSi | 0:beeeb5671c2a | 57 | |
GabDiSi | 0:beeeb5671c2a | 58 | pc.baud(115200); |
GabDiSi | 0:beeeb5671c2a | 59 | serial6.baud(115200); |
GabDiSi | 0:beeeb5671c2a | 60 | |
GabDiSi | 1:b6e184239fba | 61 | pc.printf("%s\r\n", ESP8266initStr); |
GabDiSi | 1:b6e184239fba | 62 | lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266initStr, CENTER_MODE); |
GabDiSi | 1:b6e184239fba | 63 | lineDisplay++; |
GabDiSi | 0:beeeb5671c2a | 64 | |
GabDiSi | 1:b6e184239fba | 65 | pc.printf("%s", ESP8266resetStr); |
GabDiSi | 1:b6e184239fba | 66 | lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266resetStr, LEFT_MODE); |
GabDiSi | 0:beeeb5671c2a | 67 | serial6.printf("AT+RST\r\n"); |
GabDiSi | 1:b6e184239fba | 68 | ESP8266readResponse(ESP8266responseOkStr, NULL); |
GabDiSi | 1:b6e184239fba | 69 | ESP8266readResponse(ESP8266responseReadyStr, NULL); |
GabDiSi | 1:b6e184239fba | 70 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 1:b6e184239fba | 71 | lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE); |
GabDiSi | 0:beeeb5671c2a | 72 | |
GabDiSi | 1:b6e184239fba | 73 | pc.printf("%s", ESP8266clientModeStr); |
GabDiSi | 1:b6e184239fba | 74 | lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266clientModeStr, LEFT_MODE); |
GabDiSi | 0:beeeb5671c2a | 75 | serial6.printf("AT+CWMODE=1\r\n"); |
GabDiSi | 1:b6e184239fba | 76 | ESP8266readResponse(ESP8266responseOkStr, NULL); |
GabDiSi | 1:b6e184239fba | 77 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 1:b6e184239fba | 78 | lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE); |
GabDiSi | 0:beeeb5671c2a | 79 | |
GabDiSi | 1:b6e184239fba | 80 | pc.printf("%s", ESP8266wifiConnectionStr); |
GabDiSi | 1:b6e184239fba | 81 | lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266wifiConnectionStr, LEFT_MODE); |
GabDiSi | 0:beeeb5671c2a | 82 | serial6.printf("AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, password); |
GabDiSi | 1:b6e184239fba | 83 | ESP8266readResponse(ESP8266responseOkStr, NULL); |
GabDiSi | 1:b6e184239fba | 84 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 1:b6e184239fba | 85 | lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE); |
GabDiSi | 0:beeeb5671c2a | 86 | |
GabDiSi | 0:beeeb5671c2a | 87 | led1 = 1; |
GabDiSi | 0:beeeb5671c2a | 88 | }; |
GabDiSi | 0:beeeb5671c2a | 89 | |
GabDiSi | 0:beeeb5671c2a | 90 | void ESP8266get(char* domain, char* url, char* response) { |
GabDiSi | 0:beeeb5671c2a | 91 | int n1, n2; |
GabDiSi | 0:beeeb5671c2a | 92 | |
GabDiSi | 0:beeeb5671c2a | 93 | n1 = strlen(domain); |
GabDiSi | 0:beeeb5671c2a | 94 | n2 = strlen(url); |
GabDiSi | 0:beeeb5671c2a | 95 | |
GabDiSi | 1:b6e184239fba | 96 | pc.printf("%s", ESP8266serverConnectionStr); |
GabDiSi | 1:b6e184239fba | 97 | lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266serverConnectionStr, LEFT_MODE); |
GabDiSi | 0:beeeb5671c2a | 98 | serial6.printf("AT+CIPSTART=\"TCP\",\"%s\",80\r\n", domain); |
GabDiSi | 1:b6e184239fba | 99 | ESP8266readResponse(ESP8266responseOkStr, NULL); |
GabDiSi | 1:b6e184239fba | 100 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 1:b6e184239fba | 101 | lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE); |
GabDiSi | 0:beeeb5671c2a | 102 | |
GabDiSi | 1:b6e184239fba | 103 | pc.printf("%s", ESP8266sendingCharsStr); |
GabDiSi | 1:b6e184239fba | 104 | lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266sendingCharsStr, LEFT_MODE); |
GabDiSi | 0:beeeb5671c2a | 105 | serial6.printf("AT+CIPSEND=%d\r\n", 11+n1+1+n2+13); |
GabDiSi | 1:b6e184239fba | 106 | ESP8266readResponse(ESP8266responseGetStr, NULL); |
GabDiSi | 1:b6e184239fba | 107 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 1:b6e184239fba | 108 | lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE); |
GabDiSi | 0:beeeb5671c2a | 109 | |
GabDiSi | 1:b6e184239fba | 110 | pc.printf("%s", ESP8266sendingRequestStr); |
GabDiSi | 1:b6e184239fba | 111 | lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266sendingRequestStr, LEFT_MODE); |
GabDiSi | 0:beeeb5671c2a | 112 | serial6.printf("GET http://%s/%s HTTP/1.0\r\n\r\n", domain, url); |
GabDiSi | 1:b6e184239fba | 113 | ESP8266readResponse(ESP8266responseSendStr, NULL); |
GabDiSi | 1:b6e184239fba | 114 | ESP8266readResponse(ESP8266responseClosedStr, response); |
GabDiSi | 1:b6e184239fba | 115 | pc.printf("%s\r\n", ESP8266doneStr); |
GabDiSi | 1:b6e184239fba | 116 | lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE); |
GabDiSi | 0:beeeb5671c2a | 117 | }; |
GabDiSi | 0:beeeb5671c2a | 118 | |
GabDiSi | 0:beeeb5671c2a | 119 | int main() { |
GabDiSi | 1:b6e184239fba | 120 | lcd.Clear(LCD_COLOR_WHITE); |
GabDiSi | 1:b6e184239fba | 121 | lcd.SetBackColor(LCD_COLOR_WHITE); |
GabDiSi | 1:b6e184239fba | 122 | lcd.SetTextColor(LCD_COLOR_BLACK); |
GabDiSi | 1:b6e184239fba | 123 | |
GabDiSi | 0:beeeb5671c2a | 124 | char response[ESP8266limitResponse]; |
GabDiSi | 0:beeeb5671c2a | 125 | char domain[] = "gabrieledisimone.it"; |
GabDiSi | 0:beeeb5671c2a | 126 | char url[] = ""; |
GabDiSi | 0:beeeb5671c2a | 127 | |
GabDiSi | 0:beeeb5671c2a | 128 | ESP8266init("Telecom-47520601", "famigliaquino2000router10"); |
GabDiSi | 0:beeeb5671c2a | 129 | |
GabDiSi | 0:beeeb5671c2a | 130 | ESP8266get(domain, url, response); |
GabDiSi | 0:beeeb5671c2a | 131 | |
GabDiSi | 0:beeeb5671c2a | 132 | pc.printf("response:\r\n%s", response); |
GabDiSi | 0:beeeb5671c2a | 133 | while(1) { |
GabDiSi | 0:beeeb5671c2a | 134 | pc.putc(serial6.getc()); |
GabDiSi | 0:beeeb5671c2a | 135 | }; |
GabDiSi | 0:beeeb5671c2a | 136 | }; |