LoRa Access Point 1.5.2018
Dependencies: mbed ds3231 SX1276Lib_LoRa_Access_Point
ESP8266.cpp@5:19b34c4d27a1, 2018-04-30 (annotated)
- Committer:
- lukas_formanek
- Date:
- Mon Apr 30 22:41:48 2018 +0000
- Revision:
- 5:19b34c4d27a1
- Parent:
- 4:e20eb5efd859
- Child:
- 8:5d99fbf255d6
1.5.2018
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lukas_formanek | 0:ea088908ad26 | 1 | #include "ESP8266.h" |
lukas_formanek | 0:ea088908ad26 | 2 | |
lukas_formanek | 0:ea088908ad26 | 3 | const char* SSID = "doma2"; |
lukas_formanek | 0:ea088908ad26 | 4 | const char* PSWD = "0917957359"; |
lukas_formanek | 0:ea088908ad26 | 5 | const char* OK_MESSAGE = "OK\r\n"; |
lukas_formanek | 0:ea088908ad26 | 6 | const char* ERROR_MESSAGE = "ERROR\r\n"; |
lukas_formanek | 0:ea088908ad26 | 7 | const char* GOT_IP_MESSAGE = "WIFI GOT IP\r\n"; |
lukas_formanek | 0:ea088908ad26 | 8 | const char* CAN_SEND_MESSAGE = ">"; |
lukas_formanek | 0:ea088908ad26 | 9 | const char* SEND_OK_MESSAGE = "SEND OK\r\n"; |
lukas_formanek | 0:ea088908ad26 | 10 | const char* WIFI_ALIVE_RESPONSE = "~~LoRa Gateway~~\r\n"; |
lukas_formanek | 0:ea088908ad26 | 11 | const char* DELIMITER=":"; |
lukas_formanek | 0:ea088908ad26 | 12 | |
lukas_formanek | 0:ea088908ad26 | 13 | |
lukas_formanek | 5:19b34c4d27a1 | 14 | ESP8266 wifi(WIFI_TX, WIFI_RX, WIFI_RST); |
lukas_formanek | 0:ea088908ad26 | 15 | |
lukas_formanek | 5:19b34c4d27a1 | 16 | ESP8266::ESP8266(PinName tx, PinName rx, PinName reset) |
lukas_formanek | 0:ea088908ad26 | 17 | : wifiUart(tx,rx,BAUDRATE), |
lukas_formanek | 0:ea088908ad26 | 18 | wifiReset(reset) |
lukas_formanek | 0:ea088908ad26 | 19 | { |
lukas_formanek | 0:ea088908ad26 | 20 | ClearBuffer(); |
lukas_formanek | 0:ea088908ad26 | 21 | okResponse = false; |
lukas_formanek | 0:ea088908ad26 | 22 | receiveResponse = false; |
lukas_formanek | 0:ea088908ad26 | 23 | setServerIp = false; |
lukas_formanek | 0:ea088908ad26 | 24 | setWifiSettings = false; |
lukas_formanek | 0:ea088908ad26 | 25 | }; |
lukas_formanek | 0:ea088908ad26 | 26 | |
lukas_formanek | 2:0499e1d037a5 | 27 | |
lukas_formanek | 0:ea088908ad26 | 28 | ESP8266::ESP8266(void) |
lukas_formanek | 2:0499e1d037a5 | 29 | : wifiUart(WIFI_TX,WIFI_RX,BAUDRATE), |
lukas_formanek | 0:ea088908ad26 | 30 | wifiReset(D7) |
lukas_formanek | 0:ea088908ad26 | 31 | { |
lukas_formanek | 0:ea088908ad26 | 32 | ClearBuffer(); |
lukas_formanek | 0:ea088908ad26 | 33 | okResponse = false; |
lukas_formanek | 0:ea088908ad26 | 34 | receiveResponse = false; |
lukas_formanek | 0:ea088908ad26 | 35 | setServerIp = false; |
lukas_formanek | 0:ea088908ad26 | 36 | setWifiSettings = false; |
lukas_formanek | 0:ea088908ad26 | 37 | }; |
lukas_formanek | 2:0499e1d037a5 | 38 | |
lukas_formanek | 0:ea088908ad26 | 39 | |
lukas_formanek | 0:ea088908ad26 | 40 | void ESP8266::Init() |
lukas_formanek | 0:ea088908ad26 | 41 | { |
lukas_formanek | 0:ea088908ad26 | 42 | wifiReset = 0; |
lukas_formanek | 0:ea088908ad26 | 43 | wait(0.1); |
lukas_formanek | 0:ea088908ad26 | 44 | wifiReset = 1; |
lukas_formanek | 2:0499e1d037a5 | 45 | wait(0.1); |
lukas_formanek | 0:ea088908ad26 | 46 | wifiUart.attach(callback(this,&ESP8266::RxWifiInterrupt), Serial::RxIrq); |
lukas_formanek | 2:0499e1d037a5 | 47 | ClearBuffer(); |
lukas_formanek | 0:ea088908ad26 | 48 | }; |
lukas_formanek | 0:ea088908ad26 | 49 | |
lukas_formanek | 0:ea088908ad26 | 50 | void ESP8266::RxWifiInterrupt() |
lukas_formanek | 0:ea088908ad26 | 51 | { |
lukas_formanek | 5:19b34c4d27a1 | 52 | while (wifiUart.readable()) { |
lukas_formanek | 0:ea088908ad26 | 53 | char c = wifiUart.getc(); |
lukas_formanek | 0:ea088908ad26 | 54 | // bt.putc(c); |
lukas_formanek | 0:ea088908ad26 | 55 | buffer[pt++] = c; |
lukas_formanek | 0:ea088908ad26 | 56 | if(pt >= BUFFER_SIZE - 1) |
lukas_formanek | 0:ea088908ad26 | 57 | ClearBuffer(); |
lukas_formanek | 0:ea088908ad26 | 58 | |
lukas_formanek | 5:19b34c4d27a1 | 59 | if(c =='\n') { |
lukas_formanek | 0:ea088908ad26 | 60 | if( strncmp( ( const char* )buffer, response, strlen(response) ) == 0 ) |
lukas_formanek | 0:ea088908ad26 | 61 | receiveResponse = true; |
lukas_formanek | 5:19b34c4d27a1 | 62 | |
lukas_formanek | 0:ea088908ad26 | 63 | if( strncmp( ( const char* )buffer, "~~~", 3 ) == 0 ) |
lukas_formanek | 0:ea088908ad26 | 64 | wifiUart.printf("%s", WIFI_ALIVE_RESPONSE); |
lukas_formanek | 4:e20eb5efd859 | 65 | |
lukas_formanek | 4:e20eb5efd859 | 66 | |
lukas_formanek | 5:19b34c4d27a1 | 67 | if (strstr(buffer, "0|") != NULL) |
lukas_formanek | 5:19b34c4d27a1 | 68 | rfm.SendMessage(buffer); |
lukas_formanek | 5:19b34c4d27a1 | 69 | |
lukas_formanek | 5:19b34c4d27a1 | 70 | /* |
lukas_formanek | 5:19b34c4d27a1 | 71 | if( strncmp( ( const char* )_responseBuf, "+IPD,", 5 ) == 0 ) |
lukas_formanek | 5:19b34c4d27a1 | 72 | { |
lukas_formanek | 5:19b34c4d27a1 | 73 | char * pch; |
lukas_formanek | 5:19b34c4d27a1 | 74 | pch = strtok (_responseBuf,DELIMITER); |
lukas_formanek | 5:19b34c4d27a1 | 75 | uint8_t i = 0 ; |
lukas_formanek | 5:19b34c4d27a1 | 76 | while (pch != NULL) { |
lukas_formanek | 5:19b34c4d27a1 | 77 | if(i == 1) |
lukas_formanek | 5:19b34c4d27a1 | 78 | bt.printf("%s",pch); |
lukas_formanek | 5:19b34c4d27a1 | 79 | pch = strtok (NULL, DELIMITER); |
lukas_formanek | 5:19b34c4d27a1 | 80 | i++; |
lukas_formanek | 5:19b34c4d27a1 | 81 | } |
lukas_formanek | 5:19b34c4d27a1 | 82 | } |
lukas_formanek | 5:19b34c4d27a1 | 83 | */ |
lukas_formanek | 5:19b34c4d27a1 | 84 | /* |
lukas_formanek | 5:19b34c4d27a1 | 85 | if( strncmp( ( const char* )_responseBuf, response, 4 ) == 0 ) |
lukas_formanek | 5:19b34c4d27a1 | 86 | { |
lukas_formanek | 5:19b34c4d27a1 | 87 | OkResponse = true; |
lukas_formanek | 5:19b34c4d27a1 | 88 | // bt.printf("OK received !\r\n"); |
lukas_formanek | 5:19b34c4d27a1 | 89 | } |
lukas_formanek | 5:19b34c4d27a1 | 90 | */ |
lukas_formanek | 5:19b34c4d27a1 | 91 | /* |
lukas_formanek | 5:19b34c4d27a1 | 92 | if( strncmp( ( const char* )_responseBuf, ERROR_MESSAGE, 7 ) == 0 ) |
lukas_formanek | 5:19b34c4d27a1 | 93 | { |
lukas_formanek | 5:19b34c4d27a1 | 94 | // bt.printf("ERROR received !\r\n"); |
lukas_formanek | 5:19b34c4d27a1 | 95 | } |
lukas_formanek | 5:19b34c4d27a1 | 96 | */ |
lukas_formanek | 0:ea088908ad26 | 97 | ClearBuffer(); |
lukas_formanek | 0:ea088908ad26 | 98 | } |
lukas_formanek | 0:ea088908ad26 | 99 | } |
lukas_formanek | 0:ea088908ad26 | 100 | }; |
lukas_formanek | 0:ea088908ad26 | 101 | |
lukas_formanek | 0:ea088908ad26 | 102 | void ESP8266::SendCommand(char* command, char* answer ,int timeoutMs) |
lukas_formanek | 0:ea088908ad26 | 103 | { |
lukas_formanek | 0:ea088908ad26 | 104 | receiveResponse = false; |
lukas_formanek | 0:ea088908ad26 | 105 | response = answer; |
lukas_formanek | 0:ea088908ad26 | 106 | okResponse = false; |
lukas_formanek | 0:ea088908ad26 | 107 | responseTimer.reset(); |
lukas_formanek | 0:ea088908ad26 | 108 | wifiUart.printf("%s",command); |
lukas_formanek | 0:ea088908ad26 | 109 | responseTimer.start(); |
lukas_formanek | 5:19b34c4d27a1 | 110 | while(responseTimer.read_ms() < timeoutMs) { |
lukas_formanek | 5:19b34c4d27a1 | 111 | if(receiveResponse) { |
lukas_formanek | 2:0499e1d037a5 | 112 | bt.Send(response); |
lukas_formanek | 0:ea088908ad26 | 113 | responseTimer.stop(); |
lukas_formanek | 0:ea088908ad26 | 114 | return; |
lukas_formanek | 0:ea088908ad26 | 115 | } |
lukas_formanek | 0:ea088908ad26 | 116 | } |
lukas_formanek | 0:ea088908ad26 | 117 | responseTimer.stop(); |
lukas_formanek | 0:ea088908ad26 | 118 | } |
lukas_formanek | 0:ea088908ad26 | 119 | |
lukas_formanek | 0:ea088908ad26 | 120 | void ESP8266::SetIpOfServer(char* command) |
lukas_formanek | 0:ea088908ad26 | 121 | { |
lukas_formanek | 0:ea088908ad26 | 122 | memcpy(serverIpAddress, command, sizeof(serverIpAddress)); |
lukas_formanek | 5:19b34c4d27a1 | 123 | setServerIp = true; |
lukas_formanek | 0:ea088908ad26 | 124 | }; |
lukas_formanek | 0:ea088908ad26 | 125 | |
lukas_formanek | 0:ea088908ad26 | 126 | void ESP8266::SetWiFiConnection(char* command) |
lukas_formanek | 0:ea088908ad26 | 127 | { |
lukas_formanek | 0:ea088908ad26 | 128 | memcpy(wifiSettings, command, sizeof(wifiSettings)); |
lukas_formanek | 0:ea088908ad26 | 129 | setWifiSettings = true; |
lukas_formanek | 0:ea088908ad26 | 130 | }; |
lukas_formanek | 0:ea088908ad26 | 131 | |
lukas_formanek | 0:ea088908ad26 | 132 | void ESP8266::SendMessage(char message[]) |
lukas_formanek | 0:ea088908ad26 | 133 | { |
lukas_formanek | 0:ea088908ad26 | 134 | pc.printf("%s",message); |
lukas_formanek | 0:ea088908ad26 | 135 | wifiUart.printf("%s",message); |
lukas_formanek | 5:19b34c4d27a1 | 136 | }; |
lukas_formanek | 0:ea088908ad26 | 137 | |
lukas_formanek | 0:ea088908ad26 | 138 | void ESP8266::CheckSettings() |
lukas_formanek | 0:ea088908ad26 | 139 | { |
lukas_formanek | 5:19b34c4d27a1 | 140 | if(setServerIp && setWifiSettings) { |
lukas_formanek | 0:ea088908ad26 | 141 | SendCommand("+++",(char*)OK_MESSAGE,700); |
lukas_formanek | 0:ea088908ad26 | 142 | SendCommand("AT\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 143 | SendCommand("AT+CIPCLOSE\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 144 | SendCommand(serverIpAddress,(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 145 | SendCommand(wifiSettings,(char*)OK_MESSAGE,1500); |
lukas_formanek | 0:ea088908ad26 | 146 | SendCommand("AT+RST\r\n","ready",1000); |
lukas_formanek | 0:ea088908ad26 | 147 | setServerIp = false; |
lukas_formanek | 0:ea088908ad26 | 148 | setWifiSettings = false; |
lukas_formanek | 0:ea088908ad26 | 149 | } |
lukas_formanek | 5:19b34c4d27a1 | 150 | if(setServerIp) { |
lukas_formanek | 0:ea088908ad26 | 151 | SendCommand("+++",(char*)OK_MESSAGE,700); |
lukas_formanek | 0:ea088908ad26 | 152 | SendCommand("AT\r\n",(char*)OK_MESSAGE,300); |
lukas_formanek | 0:ea088908ad26 | 153 | SendCommand("AT+CIPCLOSE\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 154 | SendCommand(serverIpAddress,(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 155 | SendCommand("AT+RST\r\n","ready",1000); |
lukas_formanek | 0:ea088908ad26 | 156 | setServerIp = false; |
lukas_formanek | 0:ea088908ad26 | 157 | } |
lukas_formanek | 5:19b34c4d27a1 | 158 | if(setWifiSettings) { |
lukas_formanek | 0:ea088908ad26 | 159 | SendCommand("+++",(char*)OK_MESSAGE,700); |
lukas_formanek | 0:ea088908ad26 | 160 | SendCommand("AT\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 161 | SendCommand("AT+CIPCLOSE\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 162 | SendCommand(wifiSettings,(char*)OK_MESSAGE,1500); |
lukas_formanek | 0:ea088908ad26 | 163 | SendCommand("AT+RST\r\n","ready",1000); |
lukas_formanek | 0:ea088908ad26 | 164 | setWifiSettings = false; |
lukas_formanek | 0:ea088908ad26 | 165 | } |
lukas_formanek | 5:19b34c4d27a1 | 166 | }; |
lukas_formanek | 0:ea088908ad26 | 167 | |
lukas_formanek | 0:ea088908ad26 | 168 | void ESP8266::ClearBuffer() |
lukas_formanek | 0:ea088908ad26 | 169 | { |
lukas_formanek | 0:ea088908ad26 | 170 | memset(buffer, '\0', sizeof(buffer)); |
lukas_formanek | 0:ea088908ad26 | 171 | pt = 0; |
lukas_formanek | 5:19b34c4d27a1 | 172 | }; |
lukas_formanek | 5:19b34c4d27a1 | 173 | |
lukas_formanek | 5:19b34c4d27a1 | 174 | void ESP8266::ConfirmReceivedAck(uint8_t from) |
lukas_formanek | 5:19b34c4d27a1 | 175 | { |
lukas_formanek | 5:19b34c4d27a1 | 176 | wifiUart.printf("ACK|%c\r\n",from); |
lukas_formanek | 5:19b34c4d27a1 | 177 | }; |
lukas_formanek | 0:ea088908ad26 | 178 | |
lukas_formanek | 0:ea088908ad26 | 179 | void ESP8266::Test() |
lukas_formanek | 0:ea088908ad26 | 180 | { |
lukas_formanek | 0:ea088908ad26 | 181 | wifiUart.printf("+++\r\n"); |
lukas_formanek | 0:ea088908ad26 | 182 | wait(0.1); |
lukas_formanek | 0:ea088908ad26 | 183 | wifiUart.printf("AT\r\n"); |
lukas_formanek | 0:ea088908ad26 | 184 | wait(0.2); |
lukas_formanek | 0:ea088908ad26 | 185 | wifiUart.printf("AT\r\n"); |
lukas_formanek | 0:ea088908ad26 | 186 | wait(0.2); |
lukas_formanek | 0:ea088908ad26 | 187 | wifiUart.printf("AT\r\n"); |
lukas_formanek | 0:ea088908ad26 | 188 | printf("ATTTTTTTTTTT"); |
lukas_formanek | 0:ea088908ad26 | 189 | wait(0.2); |
lukas_formanek | 5:19b34c4d27a1 | 190 | }; |