Gabriele Di Simone / Mbed 2 deprecated DISCO-F746NG_ROPE_WIFI

Dependencies:   BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <string.h>
00002 #include "mbed.h"
00003 #include "TS_DISCO_F746NG.h"
00004 #include "LCD_DISCO_F746NG.h"
00005 
00006 LCD_DISCO_F746NG lcd;
00007 TS_DISCO_F746NG ts;
00008  
00009 Serial pc(USBTX, USBRX); // tx, rx
00010 Serial serial6(D1, D0); // tx, rx
00011 DigitalOut led1(LED1);
00012 
00013 int lineDisplay = 0;
00014 int ESP8266limitResponse = 999999;
00015 
00016 char ESP8266responseOkStr[] = "OK\r\n";
00017 char ESP8266responseGetStr[] = "OK\r\n> ";
00018 char ESP8266responseReadyStr[] = "ready\r\n";
00019 char ESP8266responseSendStr[] = "SEND OK\r\n\r\n";
00020 char ESP8266responseClosedStr[] = "CLOSED\r\n";
00021 
00022 char ESP8266initStr[] = "ROPE WIFI - TECHNOLOGY DAY";
00023 char ESP8266doneStr[] = "done!";
00024 char ESP8266resetStr[] = "Init ESP8266... ";
00025 char ESP8266clientModeStr[] = "Client mode... ";
00026 char ESP8266wifiConnectionStr[] = "WiFi connection... ";
00027 char ESP8266serverConnectionStr[] = "Server connection... ";
00028 char ESP8266sendingCharsStr[] = "Sending chars... ";
00029 char ESP8266sendingRequestStr[] = "Sending request... ";
00030 
00031 TS_StateTypeDef TS_State;
00032 uint8_t status;
00033 
00034 
00035 void checkDisplayLines() {
00036     if(lineDisplay>7) {
00037         for(int idx = 2; idx<8; idx++) {
00038             lcd.ClearStringLine(idx);
00039         };
00040         
00041         lineDisplay = 2;
00042     };
00043 };
00044 
00045 bool ESP8266readResponse(char* delimiter, char* response) {
00046     int i = 0, j = 0;
00047     char c;
00048     
00049     while(j<ESP8266limitResponse) {
00050         c = serial6.getc();
00051         response[j++] = c;
00052         
00053         if(c == delimiter[i]) {
00054             i++;
00055             
00056             if(delimiter[i] == '\0') {
00057                 response[j] = '\0';
00058                 return true;
00059             };
00060         } else {
00061             i = 0;
00062         };
00063     };
00064     
00065     response[j] = '\0';
00066     return false;
00067 };
00068 
00069 void ESP8266init(char* ssid, char* password) {
00070     led1 = 0;
00071     lineDisplay = 0;
00072     
00073     pc.baud(115200);
00074     serial6.baud(115200);
00075     
00076     lcd.SetTextColor(LCD_COLOR_RED);
00077     pc.printf("%s\r\n", ESP8266initStr);
00078     lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266initStr, CENTER_MODE);
00079     lineDisplay++;
00080     lcd.SetTextColor(LCD_COLOR_BLACK);
00081     
00082     pc.printf("%s", ESP8266resetStr);
00083     lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266resetStr, LEFT_MODE);
00084     serial6.printf("AT+RST\r\n");
00085     ESP8266readResponse(ESP8266responseOkStr, NULL);
00086     ESP8266readResponse(ESP8266responseReadyStr, NULL);
00087     lcd.SetTextColor(LCD_COLOR_GREEN);
00088     pc.printf("%s\r\n", ESP8266doneStr);
00089     lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE);
00090     lcd.SetTextColor(LCD_COLOR_BLACK);
00091     
00092     pc.printf("%s", ESP8266clientModeStr);
00093     lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266clientModeStr, LEFT_MODE);
00094     serial6.printf("AT+CWMODE=1\r\n");
00095     ESP8266readResponse(ESP8266responseOkStr, NULL);
00096     lcd.SetTextColor(LCD_COLOR_GREEN);
00097     pc.printf("%s\r\n", ESP8266doneStr);
00098     lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE);
00099     lcd.SetTextColor(LCD_COLOR_BLACK);
00100     
00101     pc.printf("%s", ESP8266wifiConnectionStr);
00102     lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266wifiConnectionStr, LEFT_MODE);
00103     serial6.printf("AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, password);
00104     ESP8266readResponse(ESP8266responseOkStr, NULL);
00105     lcd.SetTextColor(LCD_COLOR_GREEN);
00106     pc.printf("%s\r\n", ESP8266doneStr);
00107     lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE);
00108     lcd.SetTextColor(LCD_COLOR_BLACK);
00109     
00110     led1 = 1;
00111     lcd.SetTextColor(LCD_COLOR_BLUE); //request button
00112     lcd.FillCircle(425, 230, 25);
00113     
00114     lcd.SetTextColor(LCD_COLOR_RED); //request button
00115     lcd.FillCircle(350, 230, 25);
00116     
00117     lcd.SetTextColor(LCD_COLOR_YELLOW); //request button
00118     lcd.FillCircle(275, 230, 25);
00119     
00120     lcd.SetTextColor(LCD_COLOR_GREEN); //request button
00121     lcd.FillCircle(200, 230, 25);
00122     
00123     lcd.SetTextColor(LCD_COLOR_BLACK); 
00124 };
00125 
00126 void ESP8266get(char* domain, char* url, char* response) {
00127     int n1, n2;
00128     
00129     n1 = strlen(domain);
00130     n2 = strlen(url);
00131     
00132     pc.printf("%s", ESP8266serverConnectionStr);
00133     checkDisplayLines();
00134     lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266serverConnectionStr, LEFT_MODE);
00135     serial6.printf("AT+CIPSTART=\"TCP\",\"%s\",80\r\n", domain);
00136     ESP8266readResponse(ESP8266responseOkStr, NULL);
00137     lcd.SetTextColor(LCD_COLOR_GREEN);
00138     pc.printf("%s\r\n", ESP8266doneStr);
00139     lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE);
00140     lcd.SetTextColor(LCD_COLOR_BLACK);
00141     
00142     pc.printf("%s", ESP8266sendingCharsStr);
00143     checkDisplayLines();
00144     lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266sendingCharsStr, LEFT_MODE);
00145     serial6.printf("AT+CIPSEND=%d\r\n", 11+n1+1+n2+13);
00146     ESP8266readResponse(ESP8266responseGetStr, NULL);
00147     lcd.SetTextColor(LCD_COLOR_GREEN);
00148     pc.printf("%s\r\n", ESP8266doneStr);
00149     lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE);
00150     lcd.SetTextColor(LCD_COLOR_BLACK);
00151     
00152     pc.printf("%s", ESP8266sendingRequestStr);
00153     checkDisplayLines();
00154     lcd.DisplayStringAt(0, LINE(lineDisplay), (uint8_t *)ESP8266sendingRequestStr, LEFT_MODE);
00155     serial6.printf("GET http://%s/%s HTTP/1.0\r\n\r\n", domain, url);
00156     ESP8266readResponse(ESP8266responseSendStr, NULL);
00157     ESP8266readResponse(ESP8266responseClosedStr, response);
00158     lcd.SetTextColor(LCD_COLOR_GREEN);
00159     pc.printf("%s\r\n", ESP8266doneStr);
00160     lcd.DisplayStringAt(0, LINE(lineDisplay++), (uint8_t *)ESP8266doneStr, RIGHT_MODE);
00161     lcd.SetTextColor(LCD_COLOR_BLACK);
00162 };
00163 
00164 void LCDinit() {
00165     lcd.Clear(LCD_COLOR_WHITE);
00166     lcd.SetBackColor(LCD_COLOR_WHITE);
00167     
00168     status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
00169     
00170     lcd.SetTextColor(LCD_COLOR_BLACK); //reset button
00171     lcd.FillCircle(50, 230, 25);
00172 };
00173 
00174 void init() {
00175     LCDinit();
00176     ESP8266init("Telecom-47520601", "famigliaquino2000router10");
00177 };
00178 
00179 void TSdetect() {
00180     while(1) {
00181         int idx, x, y;
00182         ts.GetState(&TS_State); //TOUCH STATE
00183         
00184         if (TS_State.touchDetected) {  //TOUCH DETECTED
00185             for (idx = 0; idx < TS_State.touchDetected; idx++) {
00186                 x = TS_State.touchX[idx];
00187                 y = TS_State.touchY[idx];
00188                 
00189                 if ((x>400)&&(x<450)&&(y>205)&&(y<255)) {  
00190                     char response[ESP8266limitResponse];
00191                     char domain[] = "gabrieledisimone.it";
00192                     char url[] = "neapolis.php?button=4";
00193                     
00194                     ESP8266get(domain, url, response);
00195                     pc.printf("response:\r\n%s", response);
00196                 } else if ((x>325)&&(x<375)&&(y>205)&&(y<255)) {  
00197                     char response[ESP8266limitResponse];
00198                     char domain[] = "gabrieledisimone.it";
00199                     char url[] = "neapolis.php?button=3";
00200                     
00201                     ESP8266get(domain, url, response);
00202                     pc.printf("response:\r\n%s", response);
00203                 } else if ((x>250)&&(x<300)&&(y>205)&&(y<255)) {  
00204                     char response[ESP8266limitResponse];
00205                     char domain[] = "gabrieledisimone.it";
00206                     char url[] = "neapolis.php?button=2";
00207                     
00208                     ESP8266get(domain, url, response);
00209                     pc.printf("response:\r\n%s", response);
00210                 } else if ((x>175)&&(x<225)&&(y>205)&&(y<255)) {  
00211                     char response[ESP8266limitResponse];
00212                     char domain[] = "gabrieledisimone.it";
00213                     char url[] = "neapolis.php?button=1";
00214                     
00215                     ESP8266get(domain, url, response);
00216                     pc.printf("response:\r\n%s", response);
00217                 } else if ((x>25)&&(x<75)&&(y>205)&&(y<255)) {
00218                     init();
00219                 };
00220             };
00221         };
00222     };
00223 };
00224 
00225 int main() {    
00226     init();
00227     TSdetect();
00228 };