Connect Wi-Fi

Dependencies:   mbed

Committer:
chanagan2540
Date:
Sun Dec 10 08:08:41 2017 +0000
Revision:
3:e1042768703b
Parent:
2:6738db3eb2ed
????;

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 2:6738db3eb2ed 26 void ESP8266::itoa(int n, char * s) {
chanagan2540 2:6738db3eb2ed 27 char k = 0;
chanagan2540 2:6738db3eb2ed 28 char r[11];
chanagan2540 2:6738db3eb2ed 29
chanagan2540 2:6738db3eb2ed 30 if(n == 0) {
chanagan2540 2:6738db3eb2ed 31 s[0] = '0';
chanagan2540 2:6738db3eb2ed 32 s[1] = 0;
chanagan2540 2:6738db3eb2ed 33 } else {
chanagan2540 2:6738db3eb2ed 34 while(n != 0) {
chanagan2540 2:6738db3eb2ed 35 r[k]= (n % 10) + '0';
chanagan2540 2:6738db3eb2ed 36 n = n / 10;
chanagan2540 2:6738db3eb2ed 37 k++;
chanagan2540 2:6738db3eb2ed 38 }
chanagan2540 2:6738db3eb2ed 39 while(k > 0) {
chanagan2540 2:6738db3eb2ed 40 s[n] = r[k - 1] + '0';
chanagan2540 2:6738db3eb2ed 41 n++;
chanagan2540 2:6738db3eb2ed 42 k--;
chanagan2540 2:6738db3eb2ed 43 }
chanagan2540 2:6738db3eb2ed 44 s[n] = 0;
chanagan2540 2:6738db3eb2ed 45 }
chanagan2540 2:6738db3eb2ed 46 }
chanagan2540 2:6738db3eb2ed 47
chanagan2540 1:c0fbff97b740 48 void ESP8266::SendCMD(char * s) {
chanagan2540 1:c0fbff97b740 49 AddEOL(s);
chanagan2540 1:c0fbff97b740 50 comm.printf("%s", s);
chanagan2540 1:c0fbff97b740 51 }
chanagan2540 1:c0fbff97b740 52
chanagan2540 1:c0fbff97b740 53 void ESP8266::Reset(void) {
chanagan2540 1:c0fbff97b740 54 char rs[10];
chanagan2540 1:c0fbff97b740 55 strcpy(rs, "AT+RST");
chanagan2540 1:c0fbff97b740 56 SendCMD(rs);
chanagan2540 1:c0fbff97b740 57 }
chanagan2540 1:c0fbff97b740 58
chanagan2540 1:c0fbff97b740 59 void ESP8266::RcvReply(char * r, int to) {
chanagan2540 1:c0fbff97b740 60 Timer t;
chanagan2540 1:c0fbff97b740 61 bool ended = 0;
chanagan2540 1:c0fbff97b740 62 char c;
chanagan2540 1:c0fbff97b740 63
chanagan2540 1:c0fbff97b740 64 strcpy(r, "");
chanagan2540 1:c0fbff97b740 65 t.start();
chanagan2540 1:c0fbff97b740 66 while(!ended) {
chanagan2540 1:c0fbff97b740 67 if(comm.readable()) {
chanagan2540 1:c0fbff97b740 68 c = comm.getc();
chanagan2540 1:c0fbff97b740 69 AddChar(r, c);
chanagan2540 1:c0fbff97b740 70 t.start();
chanagan2540 1:c0fbff97b740 71 }
chanagan2540 1:c0fbff97b740 72 if(t.read_ms() > to) {
chanagan2540 1:c0fbff97b740 73 ended = 1;
chanagan2540 1:c0fbff97b740 74 }
chanagan2540 1:c0fbff97b740 75 }
chanagan2540 1:c0fbff97b740 76 AddChar(r, 0x00);
chanagan2540 1:c0fbff97b740 77 }
chanagan2540 1:c0fbff97b740 78
chanagan2540 1:c0fbff97b740 79 void ESP8266::SetMode(char mode) {
chanagan2540 1:c0fbff97b740 80 char cmd[15];
chanagan2540 1:c0fbff97b740 81 strcpy(cmd, "AT+CWMODE=");
chanagan2540 1:c0fbff97b740 82 mode = mode + 0x30; // Converts number into corresponding ASCII character
chanagan2540 1:c0fbff97b740 83 AddChar(cmd, mode); // Completes command string
chanagan2540 1:c0fbff97b740 84 SendCMD(cmd);
chanagan2540 1:c0fbff97b740 85 }
chanagan2540 1:c0fbff97b740 86
chanagan2540 1:c0fbff97b740 87 void ESP8266::GetList(char * l) {
chanagan2540 1:c0fbff97b740 88 char rs[15];
chanagan2540 1:c0fbff97b740 89 strcpy(rs, "AT+CWLAP");
chanagan2540 1:c0fbff97b740 90 SendCMD(rs);
chanagan2540 1:c0fbff97b740 91 RcvReply(l, 5000); // Needs big timeout because it takes long to start replying
chanagan2540 1:c0fbff97b740 92 }
chanagan2540 1:c0fbff97b740 93
chanagan2540 3:e1042768703b 94 void ESP8266::SetWiFi(char * name, char * ad) {
chanagan2540 3:e1042768703b 95 char cmd[255];
chanagan2540 3:e1042768703b 96 strcpy(cmd, "AT+CWSAP=");
chanagan2540 3:e1042768703b 97 AddChar(cmd, 0x22);
chanagan2540 3:e1042768703b 98 strcat(cmd, name);
chanagan2540 3:e1042768703b 99 AddChar(cmd, 0x22);
chanagan2540 3:e1042768703b 100 AddChar(cmd, 0x2C);
chanagan2540 3:e1042768703b 101 AddChar(cmd, 0x22);
chanagan2540 3:e1042768703b 102 strcat(cmd, ad);
chanagan2540 3:e1042768703b 103 AddChar(cmd, 0x22);
chanagan2540 3:e1042768703b 104 AddChar(cmd, 0x2C);
chanagan2540 3:e1042768703b 105 AddChar(cmd, 0x35);
chanagan2540 3:e1042768703b 106 AddChar(cmd, 0x2C);
chanagan2540 3:e1042768703b 107 AddChar(cmd, 0x33);
chanagan2540 3:e1042768703b 108 SendCMD(cmd);
chanagan2540 3:e1042768703b 109 }
chanagan2540 3:e1042768703b 110
chanagan2540 1:c0fbff97b740 111 void ESP8266::Join(char * id, char * pwd) {
chanagan2540 1:c0fbff97b740 112 char cmd[255];
chanagan2540 1:c0fbff97b740 113 strcpy(cmd, "AT+CWJAP=");
chanagan2540 1:c0fbff97b740 114 AddChar(cmd, 0x22);
chanagan2540 1:c0fbff97b740 115 strcat(cmd, id);
chanagan2540 1:c0fbff97b740 116 AddChar(cmd, 0x22);
chanagan2540 1:c0fbff97b740 117 AddChar(cmd, 0x2C);
chanagan2540 1:c0fbff97b740 118 AddChar(cmd, 0x22);
chanagan2540 1:c0fbff97b740 119 strcat(cmd, pwd);
chanagan2540 1:c0fbff97b740 120 AddChar(cmd, 0x22);
chanagan2540 1:c0fbff97b740 121 SendCMD(cmd);
chanagan2540 1:c0fbff97b740 122 }
chanagan2540 1:c0fbff97b740 123
chanagan2540 3:e1042768703b 124 void ESP8266::check(void) {
chanagan2540 3:e1042768703b 125 char rs[10];
chanagan2540 3:e1042768703b 126 strcpy(rs, "AT+CWSAP?");
chanagan2540 3:e1042768703b 127 SendCMD(rs);
chanagan2540 3:e1042768703b 128 }
chanagan2540 3:e1042768703b 129
chanagan2540 3:e1042768703b 130 void ESP8266::look(void) {
chanagan2540 3:e1042768703b 131 char rs[10];
chanagan2540 3:e1042768703b 132 strcpy(rs, "AT+CWSAP?");
chanagan2540 3:e1042768703b 133 SendCMD(rs);
chanagan2540 2:6738db3eb2ed 134 }
chanagan2540 2:6738db3eb2ed 135
chanagan2540 1:c0fbff97b740 136 void ESP8266::GetIP(char * ip) {
chanagan2540 1:c0fbff97b740 137 char cmd[15];
chanagan2540 1:c0fbff97b740 138 strcpy(cmd, "AT+CIFSR");
chanagan2540 1:c0fbff97b740 139 SendCMD(cmd);
chanagan2540 1:c0fbff97b740 140 RcvReply(ip, 2000);
chanagan2540 1:c0fbff97b740 141 }
chanagan2540 2:6738db3eb2ed 142
chanagan2540 2:6738db3eb2ed 143 void ESP8266::SetMultiple(void) {
chanagan2540 2:6738db3eb2ed 144 char rs[15];
chanagan2540 2:6738db3eb2ed 145 strcpy(rs, "AT+CIPMUX=1");
chanagan2540 2:6738db3eb2ed 146 SendCMD(rs);
chanagan2540 2:6738db3eb2ed 147 }
chanagan2540 2:6738db3eb2ed 148
chanagan2540 3:e1042768703b 149 void ESP8266::SetSingle(void) {
chanagan2540 3:e1042768703b 150 char cmd[15];
chanagan2540 3:e1042768703b 151 strcpy(cmd, "AT+CIPMUX=0");
chanagan2540 3:e1042768703b 152 SendCMD(cmd);
chanagan2540 3:e1042768703b 153 }
chanagan2540 3:e1042768703b 154
chanagan2540 2:6738db3eb2ed 155 void ESP8266::StartServerMode(int port) {
chanagan2540 2:6738db3eb2ed 156 char rs[25];
chanagan2540 2:6738db3eb2ed 157 char t[4];
chanagan2540 2:6738db3eb2ed 158 strcpy(rs, "AT+CIPSERVER=1,");
chanagan2540 2:6738db3eb2ed 159 itoa(port, t);
chanagan2540 2:6738db3eb2ed 160 strcat(rs, t);
chanagan2540 2:6738db3eb2ed 161 SendCMD(rs);
chanagan2540 2:6738db3eb2ed 162 }