Libreria basica para el manejo del ESP01

Dependents:   Gateway_Cotosys Gateway_Cotosys_os5

Committer:
Thrillex13
Date:
Sat May 25 18:07:23 2019 +0000
Revision:
6:54b89962c798
Child:
7:8c6f56470ba7
Reemplazo por la libreria de William Thenaers; https://os.mbed.com/users/Wosser1sProductions/code/ESP8266/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Thrillex13 6:54b89962c798 1 #include "ESP01.h"
Thrillex13 6:54b89962c798 2
Thrillex13 6:54b89962c798 3 // Constructor
Thrillex13 6:54b89962c798 4 ESP01::ESP01(PinName tx, PinName rx, int br) : comm(tx, rx) {
Thrillex13 6:54b89962c798 5 comm.baud(br);
Thrillex13 6:54b89962c798 6 }
Thrillex13 6:54b89962c798 7
Thrillex13 6:54b89962c798 8 // Destructor
Thrillex13 6:54b89962c798 9 ESP01::~ESP01() { }
Thrillex13 6:54b89962c798 10
Thrillex13 6:54b89962c798 11 // Add <CR> + <LF> at the end of the string
Thrillex13 6:54b89962c798 12 void ESP01::AddEOL(char * s) {
Thrillex13 6:54b89962c798 13 char k = strlen(s); // Finds position of NULL character
Thrillex13 6:54b89962c798 14 s[k] = '\r'; // switch NULL for <CR>
Thrillex13 6:54b89962c798 15 s[k + 1] = '\n'; // Add <LF>
Thrillex13 6:54b89962c798 16 s[k + 2] = '\0'; // Add NULL at the end
Thrillex13 6:54b89962c798 17 }
Thrillex13 6:54b89962c798 18
Thrillex13 6:54b89962c798 19 // Add one ASCII character at the end of the string
Thrillex13 6:54b89962c798 20 void ESP01::AddChar(char * s, char c) {
Thrillex13 6:54b89962c798 21 char k = strlen(s);
Thrillex13 6:54b89962c798 22 s[k] = c;
Thrillex13 6:54b89962c798 23 s[k + 1] = '\0';
Thrillex13 6:54b89962c798 24 }
Thrillex13 6:54b89962c798 25
Thrillex13 6:54b89962c798 26 // Sends command to ESP01. Receives the command string
Thrillex13 6:54b89962c798 27 void ESP01::SendCMD(char* s) {
Thrillex13 6:54b89962c798 28 comm.printf("%s%s", s, CMD_END);
Thrillex13 6:54b89962c798 29 }
Thrillex13 6:54b89962c798 30
Thrillex13 6:54b89962c798 31 // Resets the ESP01
Thrillex13 6:54b89962c798 32 void ESP01::Reset(void) {
Thrillex13 6:54b89962c798 33 SendCMD("AT+RST");
Thrillex13 6:54b89962c798 34 }
Thrillex13 6:54b89962c798 35
Thrillex13 6:54b89962c798 36 // Receive reply until no character is received after a given timeout in miliseconds
Thrillex13 6:54b89962c798 37 bool ESP01::RcvReply(char* r, int to) {
Thrillex13 6:54b89962c798 38 Timer t;
Thrillex13 6:54b89962c798 39 strcpy(r, "");
Thrillex13 6:54b89962c798 40 t.start();
Thrillex13 6:54b89962c798 41
Thrillex13 6:54b89962c798 42 do {
Thrillex13 6:54b89962c798 43 if(comm.readable()) {
Thrillex13 6:54b89962c798 44 AddChar(r, comm.getc());
Thrillex13 6:54b89962c798 45 t.start();
Thrillex13 6:54b89962c798 46 }
Thrillex13 6:54b89962c798 47 } while(t.read_ms() < to);
Thrillex13 6:54b89962c798 48
Thrillex13 6:54b89962c798 49 AddChar(r, '\0');
Thrillex13 6:54b89962c798 50 return r[0] != '\0';
Thrillex13 6:54b89962c798 51 }
Thrillex13 6:54b89962c798 52
Thrillex13 6:54b89962c798 53 // Gets the AP list. Parameter: the string to receive the list
Thrillex13 6:54b89962c798 54 bool ESP01::GetList(char *l) {
Thrillex13 6:54b89962c798 55 SendCMD("AT+CWLAP");
Thrillex13 6:54b89962c798 56 return RcvReply(l, 5000); // Needs big timeout because it takes long to start replying
Thrillex13 6:54b89962c798 57 }
Thrillex13 6:54b89962c798 58
Thrillex13 6:54b89962c798 59 // Joins a Wifi AP. Parameters: SSID and Password (strings)
Thrillex13 6:54b89962c798 60 void ESP01::Join(char *id, char *pwd) {
Thrillex13 6:54b89962c798 61 char cmd[255];
Thrillex13 6:54b89962c798 62 sprintf(cmd, "AT+CWJAP=\"%s\",\"%s\"", id, pwd);
Thrillex13 6:54b89962c798 63 SendCMD(cmd);
Thrillex13 6:54b89962c798 64 }
Thrillex13 6:54b89962c798 65
Thrillex13 6:54b89962c798 66 // Gets ESP IP. Parameter: string to contain IP
Thrillex13 6:54b89962c798 67 bool ESP01::GetIP(char *ip) {
Thrillex13 6:54b89962c798 68 SendCMD("AT+CIFSR");
Thrillex13 6:54b89962c798 69 return RcvReply(ip, 2000);
Thrillex13 6:54b89962c798 70 }
Thrillex13 6:54b89962c798 71
Thrillex13 6:54b89962c798 72 //Defines wifi mode; Parameter: mode; 1= STA, 2= AP, 3=both
Thrillex13 6:54b89962c798 73 void ESP01::SetMode(WiFiMode mode) {
Thrillex13 6:54b89962c798 74 char cmd[15];
Thrillex13 6:54b89962c798 75 strcpy(cmd, "AT+CWMODE=");
Thrillex13 6:54b89962c798 76 AddChar(cmd, mode + '0'); // Completes command string
Thrillex13 6:54b89962c798 77 SendCMD(cmd);
Thrillex13 6:54b89962c798 78 }
Thrillex13 6:54b89962c798 79
Thrillex13 6:54b89962c798 80 // Quits the AP
Thrillex13 6:54b89962c798 81 void ESP01::Quit(void) {
Thrillex13 6:54b89962c798 82 SendCMD("AT+CWQAP");
Thrillex13 6:54b89962c798 83 }
Thrillex13 6:54b89962c798 84
Thrillex13 6:54b89962c798 85 // Sets single connection
Thrillex13 6:54b89962c798 86 void ESP01::SetSingle(void) {
Thrillex13 6:54b89962c798 87 SendCMD("AT+CIPMUX=0");
Thrillex13 6:54b89962c798 88 }
Thrillex13 6:54b89962c798 89
Thrillex13 6:54b89962c798 90 // Sets multiple connection
Thrillex13 6:54b89962c798 91 void ESP01::SetMultiple(void) {
Thrillex13 6:54b89962c798 92 SendCMD("AT+CIPMUX=1");
Thrillex13 6:54b89962c798 93 }
Thrillex13 6:54b89962c798 94
Thrillex13 6:54b89962c798 95 // Gets connection status. Parameter: string to contain status
Thrillex13 6:54b89962c798 96 bool ESP01::GetConnStatus(char * st) {
Thrillex13 6:54b89962c798 97 SendCMD("AT+CIPSTATUS");
Thrillex13 6:54b89962c798 98 return RcvReply(st, 2000);
Thrillex13 6:54b89962c798 99 }
Thrillex13 6:54b89962c798 100
Thrillex13 6:54b89962c798 101 // Starts server mode. Parameter: port to be used
Thrillex13 6:54b89962c798 102 void ESP01::StartServerMode(int port) {
Thrillex13 6:54b89962c798 103 char rs[25];
Thrillex13 6:54b89962c798 104 sprintf(rs, "AT+CIPSERVER=1,%d", port);
Thrillex13 6:54b89962c798 105 SendCMD(rs);
Thrillex13 6:54b89962c798 106 }
Thrillex13 6:54b89962c798 107
Thrillex13 6:54b89962c798 108 // Close server mode.
Thrillex13 6:54b89962c798 109 void ESP01::CloseServerMode(void) {
Thrillex13 6:54b89962c798 110 SendCMD("AT+CIPSERVER=0");
Thrillex13 6:54b89962c798 111 }
Thrillex13 6:54b89962c798 112
Thrillex13 6:54b89962c798 113 void ESP01::setTransparent(void){
Thrillex13 6:54b89962c798 114 SendCMD("AT+CIPMODE=0");
Thrillex13 6:54b89962c798 115 }
Thrillex13 6:54b89962c798 116
Thrillex13 6:54b89962c798 117 void ESP01::startTCPConn(char *IP, int port){
Thrillex13 6:54b89962c798 118 char rs[100];
Thrillex13 6:54b89962c798 119 sprintf(rs, "AT+CIPSTART=0,\"TCP\",\"%s\",%d", IP, port);
Thrillex13 6:54b89962c798 120 SendCMD(rs);
Thrillex13 6:54b89962c798 121 }
Thrillex13 6:54b89962c798 122
Thrillex13 6:54b89962c798 123 void ESP01::sendURL(char *URL, char* IP, char *command){
Thrillex13 6:54b89962c798 124 char snd[20], http_cmd[300];
Thrillex13 6:54b89962c798 125
Thrillex13 6:54b89962c798 126 sprintf(http_cmd, "GET %s HTTP/1.1%sHost: %s%sConnection: close%s%s", URL, CMD_END, IP, CMD_END, CMD_END, CMD_END);
Thrillex13 6:54b89962c798 127 sprintf(snd,"AT+CIPSEND=0,%d",strlen(http_cmd));
Thrillex13 6:54b89962c798 128 strcpy(command, http_cmd);
Thrillex13 6:54b89962c798 129
Thrillex13 6:54b89962c798 130 SendCMD(snd);
Thrillex13 6:54b89962c798 131 wait(1);
Thrillex13 6:54b89962c798 132 SendCMD(http_cmd);
Thrillex13 6:54b89962c798 133 }
Thrillex13 6:54b89962c798 134
Thrillex13 6:54b89962c798 135 void ESP01::deepsleep(size_t ms) {
Thrillex13 6:54b89962c798 136 char snd[20];
Thrillex13 6:54b89962c798 137 sprintf(snd, "AT+GSLP=%d", ms);
Thrillex13 6:54b89962c798 138 SendCMD(snd);
Thrillex13 6:54b89962c798 139 }
Thrillex13 6:54b89962c798 140
Thrillex13 6:54b89962c798 141 //------------------ FUNCIONES QUE YO HE AGREGADO ------------------//
Thrillex13 6:54b89962c798 142 // Starts SmartConfig
Thrillex13 6:54b89962c798 143 void ESP01::StartSmartConfig(void){
Thrillex13 6:54b89962c798 144 SendCMD("AT+CWSTARTSMART=1");
Thrillex13 6:54b89962c798 145 }
Thrillex13 6:54b89962c798 146
Thrillex13 6:54b89962c798 147 //Disable Echo
Thrillex13 6:54b89962c798 148 void ESP01::DisableEcho(void) {
Thrillex13 6:54b89962c798 149 SendCMD("ATE0");
Thrillex13 6:54b89962c798 150 }
Thrillex13 6:54b89962c798 151
Thrillex13 6:54b89962c798 152 //Enable DHCP
Thrillex13 6:54b89962c798 153 void ESP01::EnableDHCP(void){
Thrillex13 6:54b89962c798 154 SendCMD("AT+CWDHCP=1,1");
Thrillex13 6:54b89962c798 155 }
Thrillex13 6:54b89962c798 156
Thrillex13 6:54b89962c798 157 // Receive reply until no character is received after a given timeout in miliseconds
Thrillex13 6:54b89962c798 158 void ESP01::RcvSingleReply(char * r) {
Thrillex13 6:54b89962c798 159 bool ended = 0;
Thrillex13 6:54b89962c798 160 char c;
Thrillex13 6:54b89962c798 161
Thrillex13 6:54b89962c798 162 strcpy(r, "");
Thrillex13 6:54b89962c798 163 while(!ended) {
Thrillex13 6:54b89962c798 164 if(comm.readable()) {
Thrillex13 6:54b89962c798 165 c = comm.getc();
Thrillex13 6:54b89962c798 166 AddChar(r, c);
Thrillex13 6:54b89962c798 167 }
Thrillex13 6:54b89962c798 168 if(c == 0x0D) {
Thrillex13 6:54b89962c798 169 ended = 1;
Thrillex13 6:54b89962c798 170 }
Thrillex13 6:54b89962c798 171 }
Thrillex13 6:54b89962c798 172 //AddChar(r, 0x00);
Thrillex13 6:54b89962c798 173 }
Thrillex13 6:54b89962c798 174
Thrillex13 6:54b89962c798 175 // Gets connection status code. Parameter: string to contain status
Thrillex13 6:54b89962c798 176 void ESP01::GetConnStatusCode(char * st) {
Thrillex13 6:54b89962c798 177 char cmd[15];
Thrillex13 6:54b89962c798 178 strcpy(cmd, "AT+CIPSTATUS");
Thrillex13 6:54b89962c798 179 SendCMD(cmd);
Thrillex13 6:54b89962c798 180 RcvSingleReply(st);
Thrillex13 6:54b89962c798 181 }
Thrillex13 6:54b89962c798 182