Simple usage of wifi module ESP826601.

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ES826601.cpp Source File

ES826601.cpp

00001 #include "ESP826601.h"
00002 #include "mbed.h"
00003 
00004 Serial pc(USBTX, USBRX, 115200);
00005 Serial esp(p28, p27, 115200);
00006 DigitalOut reset(p26);
00007 
00008 bool flag = false;
00009 bool flagConnection = false;
00010 bool flagSend = false;
00011 
00012 char connection = '0';
00013 
00014 char command[500]; 
00015 int counter = 0;
00016 
00017 Esp::Esp(){}
00018 
00019 void Esp::init(void){
00020     reset = 0;
00021     wait(0.2);
00022     reset = 1;
00023     wait(5);
00024     esp.attach(&ESP_interrupt, Serial::RxIrq);
00025     pc.printf("------------\n");
00026 }
00027 
00028 void Esp::send(char* text){
00029     pc.printf("PC: ");
00030     pc.printf(text);
00031     pc.putc('\r');
00032     esp.printf(text);
00033     esp.printf("\r\n");
00034 }
00035 
00036 void Esp::waitResponse(){
00037     while(!flag){
00038         wait_ms(100);    
00039     }
00040     flag = false;  
00041 }
00042 
00043 void Esp::waitConnection(){
00044     while(!flagConnection){
00045         wait_ms(100);   
00046     }  
00047     flagConnection = false;  
00048     
00049     esp.printf("AT+CIPSEND=");
00050     esp.putc(connection);
00051     esp.printf(",20\r\n");
00052     
00053     Esp::waitResponse();
00054 }
00055 
00056 void Esp::closeConnection(){
00057     while(!flagSend){
00058         wait_ms(100); 
00059     }
00060     flagSend = false;
00061     
00062     esp.printf("AT+CIPCLOSE=");
00063     esp.putc(connection);
00064     esp.printf("\r\n");
00065     
00066     Esp::waitResponse();
00067 }
00068 
00069 void Esp::printToPC(char* text){
00070     pc.printf(text);
00071 }
00072 
00073 void Esp::printToPC(char c){
00074     pc.putc(c);
00075 }
00076 
00077 void ESP_interrupt(){
00078     char c = esp.getc();
00079     if(c == '\n' || c == '\r'){
00080         if(counter != 0){
00081             pc.printf("ESP: ");
00082             for(int i=0; i<counter; i++) pc.putc(command[i]);
00083             pc.putc('\r');
00084             counter = 0;
00085             if(command[0] == 'O' && command[1] == 'K') flag = true;
00086             if(command[2] == 'C' && command[3] == 'O' && command[4] == 'N' && command[5] == 'N') {
00087                 connection = command[0];
00088                 flagConnection = true;
00089             }
00090             if(command[0] == 'S' && command[1] == 'E' && command[2] == 'N' && command[3] == 'D') flagSend = true;
00091         }
00092     }else {
00093         command[counter] = c;
00094         counter++;  
00095     }
00096 }