Connect Wi-Fi

Dependencies:   mbed

Committer:
chanagan2540
Date:
Sat Dec 09 20:43:11 2017 +0000
Revision:
1:c0fbff97b740
Child:
2:6738db3eb2ed
esp8266

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chanagan2540 1:c0fbff97b740 1 #include "ESP8266.h"
chanagan2540 1:c0fbff97b740 2
chanagan2540 1:c0fbff97b740 3 // Constructor
chanagan2540 1:c0fbff97b740 4 ESP8266::ESP8266(PinName tx, PinName rx, int br) : comm(tx, rx) {
chanagan2540 1:c0fbff97b740 5 comm.baud(br);
chanagan2540 1:c0fbff97b740 6 }
chanagan2540 1:c0fbff97b740 7
chanagan2540 1:c0fbff97b740 8 // Destructor
chanagan2540 1:c0fbff97b740 9 ESP8266::~ESP8266() { }
chanagan2540 1:c0fbff97b740 10
chanagan2540 1:c0fbff97b740 11 void ESP8266::AddEOL(char * s) {
chanagan2540 1:c0fbff97b740 12 char k;
chanagan2540 1:c0fbff97b740 13 k = strlen(s); // Finds position of NULL character
chanagan2540 1:c0fbff97b740 14 s[k] = 0x0D; // switch NULL for <CR>
chanagan2540 1:c0fbff97b740 15 s[k + 1] = 0x0A; // Add <LF>
chanagan2540 1:c0fbff97b740 16 s[k + 2] = 0; // Add NULL at the end
chanagan2540 1:c0fbff97b740 17 }
chanagan2540 1:c0fbff97b740 18
chanagan2540 1:c0fbff97b740 19 void ESP8266::AddChar(char * s, char c) {
chanagan2540 1:c0fbff97b740 20 char k;
chanagan2540 1:c0fbff97b740 21 k = strlen(s);
chanagan2540 1:c0fbff97b740 22 s[k] = c;
chanagan2540 1:c0fbff97b740 23 s[k + 1] = 0;
chanagan2540 1:c0fbff97b740 24 }
chanagan2540 1:c0fbff97b740 25
chanagan2540 1:c0fbff97b740 26 void ESP8266::SendCMD(char * s) {
chanagan2540 1:c0fbff97b740 27 AddEOL(s);
chanagan2540 1:c0fbff97b740 28 comm.printf("%s", s);
chanagan2540 1:c0fbff97b740 29 }
chanagan2540 1:c0fbff97b740 30
chanagan2540 1:c0fbff97b740 31 void ESP8266::Reset(void) {
chanagan2540 1:c0fbff97b740 32 char rs[10];
chanagan2540 1:c0fbff97b740 33 strcpy(rs, "AT+RST");
chanagan2540 1:c0fbff97b740 34 SendCMD(rs);
chanagan2540 1:c0fbff97b740 35 }
chanagan2540 1:c0fbff97b740 36
chanagan2540 1:c0fbff97b740 37 void ESP8266::RcvReply(char * r, int to) {
chanagan2540 1:c0fbff97b740 38 Timer t;
chanagan2540 1:c0fbff97b740 39 bool ended = 0;
chanagan2540 1:c0fbff97b740 40 char c;
chanagan2540 1:c0fbff97b740 41
chanagan2540 1:c0fbff97b740 42 strcpy(r, "");
chanagan2540 1:c0fbff97b740 43 t.start();
chanagan2540 1:c0fbff97b740 44 while(!ended) {
chanagan2540 1:c0fbff97b740 45 if(comm.readable()) {
chanagan2540 1:c0fbff97b740 46 c = comm.getc();
chanagan2540 1:c0fbff97b740 47 AddChar(r, c);
chanagan2540 1:c0fbff97b740 48 t.start();
chanagan2540 1:c0fbff97b740 49 }
chanagan2540 1:c0fbff97b740 50 if(t.read_ms() > to) {
chanagan2540 1:c0fbff97b740 51 ended = 1;
chanagan2540 1:c0fbff97b740 52 }
chanagan2540 1:c0fbff97b740 53 }
chanagan2540 1:c0fbff97b740 54 AddChar(r, 0x00);
chanagan2540 1:c0fbff97b740 55 }
chanagan2540 1:c0fbff97b740 56
chanagan2540 1:c0fbff97b740 57 void ESP8266::SetMode(char mode) {
chanagan2540 1:c0fbff97b740 58 char cmd[15];
chanagan2540 1:c0fbff97b740 59 strcpy(cmd, "AT+CWMODE=");
chanagan2540 1:c0fbff97b740 60 mode = mode + 0x30; // Converts number into corresponding ASCII character
chanagan2540 1:c0fbff97b740 61 AddChar(cmd, mode); // Completes command string
chanagan2540 1:c0fbff97b740 62 SendCMD(cmd);
chanagan2540 1:c0fbff97b740 63 }
chanagan2540 1:c0fbff97b740 64
chanagan2540 1:c0fbff97b740 65 void ESP8266::GetList(char * l) {
chanagan2540 1:c0fbff97b740 66 char rs[15];
chanagan2540 1:c0fbff97b740 67 strcpy(rs, "AT+CWLAP");
chanagan2540 1:c0fbff97b740 68 SendCMD(rs);
chanagan2540 1:c0fbff97b740 69 RcvReply(l, 5000); // Needs big timeout because it takes long to start replying
chanagan2540 1:c0fbff97b740 70 }
chanagan2540 1:c0fbff97b740 71
chanagan2540 1:c0fbff97b740 72 void ESP8266::Join(char * id, char * pwd) {
chanagan2540 1:c0fbff97b740 73 char cmd[255];
chanagan2540 1:c0fbff97b740 74 strcpy(cmd, "AT+CWJAP=");
chanagan2540 1:c0fbff97b740 75 AddChar(cmd, 0x22);
chanagan2540 1:c0fbff97b740 76 strcat(cmd, id);
chanagan2540 1:c0fbff97b740 77 AddChar(cmd, 0x22);
chanagan2540 1:c0fbff97b740 78 AddChar(cmd, 0x2C);
chanagan2540 1:c0fbff97b740 79 AddChar(cmd, 0x22);
chanagan2540 1:c0fbff97b740 80 strcat(cmd, pwd);
chanagan2540 1:c0fbff97b740 81 AddChar(cmd, 0x22);
chanagan2540 1:c0fbff97b740 82 SendCMD(cmd);
chanagan2540 1:c0fbff97b740 83 }
chanagan2540 1:c0fbff97b740 84
chanagan2540 1:c0fbff97b740 85 void ESP8266::GetIP(char * ip) {
chanagan2540 1:c0fbff97b740 86 char cmd[15];
chanagan2540 1:c0fbff97b740 87 strcpy(cmd, "AT+CIFSR");
chanagan2540 1:c0fbff97b740 88 SendCMD(cmd);
chanagan2540 1:c0fbff97b740 89 RcvReply(ip, 2000);
chanagan2540 1:c0fbff97b740 90 }