LoRa Access Point 1.5.2018
Dependencies: mbed ds3231 SX1276Lib_LoRa_Access_Point
ESP8266.cpp@10:e62222c46ee9, 2019-03-28 (annotated)
- Committer:
- lukas_formanek
- Date:
- Thu Mar 28 09:55:48 2019 +0000
- Revision:
- 10:e62222c46ee9
- Parent:
- 8:5d99fbf255d6
- Child:
- 11:376bfcdda0d4
28.3.2019 working SD,RTC
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 | 10:e62222c46ee9 | 13 | extern time_t epoch_time; |
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 | 8:5d99fbf255d6 | 60 | // if( strncmp( ( const char* )buffer, response, strlen(response) ) == 0 ) |
lukas_formanek | 8:5d99fbf255d6 | 61 | if (strstr(buffer, response) != NULL) |
lukas_formanek | 0:ea088908ad26 | 62 | receiveResponse = true; |
lukas_formanek | 5:19b34c4d27a1 | 63 | |
lukas_formanek | 8:5d99fbf255d6 | 64 | // if( strncmp( ( const char* )buffer, "~~~", 3 ) == 0 ) |
lukas_formanek | 8:5d99fbf255d6 | 65 | if (strstr(buffer, "~~~") != NULL) |
lukas_formanek | 0:ea088908ad26 | 66 | wifiUart.printf("%s", WIFI_ALIVE_RESPONSE); |
lukas_formanek | 4:e20eb5efd859 | 67 | |
lukas_formanek | 5:19b34c4d27a1 | 68 | if (strstr(buffer, "0|") != NULL) |
lukas_formanek | 5:19b34c4d27a1 | 69 | rfm.SendMessage(buffer); |
lukas_formanek | 5:19b34c4d27a1 | 70 | |
lukas_formanek | 5:19b34c4d27a1 | 71 | /* |
lukas_formanek | 5:19b34c4d27a1 | 72 | if( strncmp( ( const char* )_responseBuf, "+IPD,", 5 ) == 0 ) |
lukas_formanek | 5:19b34c4d27a1 | 73 | { |
lukas_formanek | 5:19b34c4d27a1 | 74 | char * pch; |
lukas_formanek | 5:19b34c4d27a1 | 75 | pch = strtok (_responseBuf,DELIMITER); |
lukas_formanek | 5:19b34c4d27a1 | 76 | uint8_t i = 0 ; |
lukas_formanek | 5:19b34c4d27a1 | 77 | while (pch != NULL) { |
lukas_formanek | 5:19b34c4d27a1 | 78 | if(i == 1) |
lukas_formanek | 5:19b34c4d27a1 | 79 | bt.printf("%s",pch); |
lukas_formanek | 5:19b34c4d27a1 | 80 | pch = strtok (NULL, DELIMITER); |
lukas_formanek | 5:19b34c4d27a1 | 81 | i++; |
lukas_formanek | 5:19b34c4d27a1 | 82 | } |
lukas_formanek | 5:19b34c4d27a1 | 83 | } |
lukas_formanek | 5:19b34c4d27a1 | 84 | */ |
lukas_formanek | 5:19b34c4d27a1 | 85 | /* |
lukas_formanek | 5:19b34c4d27a1 | 86 | if( strncmp( ( const char* )_responseBuf, response, 4 ) == 0 ) |
lukas_formanek | 5:19b34c4d27a1 | 87 | { |
lukas_formanek | 5:19b34c4d27a1 | 88 | OkResponse = true; |
lukas_formanek | 5:19b34c4d27a1 | 89 | // bt.printf("OK received !\r\n"); |
lukas_formanek | 5:19b34c4d27a1 | 90 | } |
lukas_formanek | 5:19b34c4d27a1 | 91 | */ |
lukas_formanek | 5:19b34c4d27a1 | 92 | /* |
lukas_formanek | 5:19b34c4d27a1 | 93 | if( strncmp( ( const char* )_responseBuf, ERROR_MESSAGE, 7 ) == 0 ) |
lukas_formanek | 5:19b34c4d27a1 | 94 | { |
lukas_formanek | 5:19b34c4d27a1 | 95 | // bt.printf("ERROR received !\r\n"); |
lukas_formanek | 5:19b34c4d27a1 | 96 | } |
lukas_formanek | 5:19b34c4d27a1 | 97 | */ |
lukas_formanek | 0:ea088908ad26 | 98 | ClearBuffer(); |
lukas_formanek | 0:ea088908ad26 | 99 | } |
lukas_formanek | 0:ea088908ad26 | 100 | } |
lukas_formanek | 0:ea088908ad26 | 101 | }; |
lukas_formanek | 0:ea088908ad26 | 102 | |
lukas_formanek | 0:ea088908ad26 | 103 | void ESP8266::SendCommand(char* command, char* answer ,int timeoutMs) |
lukas_formanek | 0:ea088908ad26 | 104 | { |
lukas_formanek | 0:ea088908ad26 | 105 | receiveResponse = false; |
lukas_formanek | 0:ea088908ad26 | 106 | response = answer; |
lukas_formanek | 0:ea088908ad26 | 107 | okResponse = false; |
lukas_formanek | 0:ea088908ad26 | 108 | responseTimer.reset(); |
lukas_formanek | 0:ea088908ad26 | 109 | wifiUart.printf("%s",command); |
lukas_formanek | 0:ea088908ad26 | 110 | responseTimer.start(); |
lukas_formanek | 5:19b34c4d27a1 | 111 | while(responseTimer.read_ms() < timeoutMs) { |
lukas_formanek | 5:19b34c4d27a1 | 112 | if(receiveResponse) { |
lukas_formanek | 2:0499e1d037a5 | 113 | bt.Send(response); |
lukas_formanek | 0:ea088908ad26 | 114 | responseTimer.stop(); |
lukas_formanek | 0:ea088908ad26 | 115 | return; |
lukas_formanek | 0:ea088908ad26 | 116 | } |
lukas_formanek | 0:ea088908ad26 | 117 | } |
lukas_formanek | 0:ea088908ad26 | 118 | responseTimer.stop(); |
lukas_formanek | 0:ea088908ad26 | 119 | } |
lukas_formanek | 0:ea088908ad26 | 120 | |
lukas_formanek | 0:ea088908ad26 | 121 | void ESP8266::SetIpOfServer(char* command) |
lukas_formanek | 0:ea088908ad26 | 122 | { |
lukas_formanek | 0:ea088908ad26 | 123 | memcpy(serverIpAddress, command, sizeof(serverIpAddress)); |
lukas_formanek | 5:19b34c4d27a1 | 124 | setServerIp = true; |
lukas_formanek | 0:ea088908ad26 | 125 | }; |
lukas_formanek | 0:ea088908ad26 | 126 | |
lukas_formanek | 0:ea088908ad26 | 127 | void ESP8266::SetWiFiConnection(char* command) |
lukas_formanek | 0:ea088908ad26 | 128 | { |
lukas_formanek | 0:ea088908ad26 | 129 | memcpy(wifiSettings, command, sizeof(wifiSettings)); |
lukas_formanek | 0:ea088908ad26 | 130 | setWifiSettings = true; |
lukas_formanek | 0:ea088908ad26 | 131 | }; |
lukas_formanek | 0:ea088908ad26 | 132 | |
lukas_formanek | 0:ea088908ad26 | 133 | void ESP8266::SendMessage(char message[]) |
lukas_formanek | 0:ea088908ad26 | 134 | { |
lukas_formanek | 10:e62222c46ee9 | 135 | // pc.printf("%.19s ", ctime(&epoch_time)); |
lukas_formanek | 10:e62222c46ee9 | 136 | pc.printf("%s",actual_time); |
lukas_formanek | 0:ea088908ad26 | 137 | pc.printf("%s",message); |
lukas_formanek | 0:ea088908ad26 | 138 | wifiUart.printf("%s",message); |
lukas_formanek | 5:19b34c4d27a1 | 139 | }; |
lukas_formanek | 0:ea088908ad26 | 140 | |
lukas_formanek | 0:ea088908ad26 | 141 | void ESP8266::CheckSettings() |
lukas_formanek | 0:ea088908ad26 | 142 | { |
lukas_formanek | 5:19b34c4d27a1 | 143 | if(setServerIp && setWifiSettings) { |
lukas_formanek | 0:ea088908ad26 | 144 | SendCommand("+++",(char*)OK_MESSAGE,700); |
lukas_formanek | 0:ea088908ad26 | 145 | SendCommand("AT\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 146 | SendCommand("AT+CIPCLOSE\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 147 | SendCommand(serverIpAddress,(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 148 | SendCommand(wifiSettings,(char*)OK_MESSAGE,1500); |
lukas_formanek | 0:ea088908ad26 | 149 | SendCommand("AT+RST\r\n","ready",1000); |
lukas_formanek | 0:ea088908ad26 | 150 | setServerIp = false; |
lukas_formanek | 0:ea088908ad26 | 151 | setWifiSettings = false; |
lukas_formanek | 0:ea088908ad26 | 152 | } |
lukas_formanek | 5:19b34c4d27a1 | 153 | if(setServerIp) { |
lukas_formanek | 0:ea088908ad26 | 154 | SendCommand("+++",(char*)OK_MESSAGE,700); |
lukas_formanek | 0:ea088908ad26 | 155 | SendCommand("AT\r\n",(char*)OK_MESSAGE,300); |
lukas_formanek | 0:ea088908ad26 | 156 | SendCommand("AT+CIPCLOSE\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 157 | SendCommand(serverIpAddress,(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 158 | SendCommand("AT+RST\r\n","ready",1000); |
lukas_formanek | 0:ea088908ad26 | 159 | setServerIp = false; |
lukas_formanek | 0:ea088908ad26 | 160 | } |
lukas_formanek | 5:19b34c4d27a1 | 161 | if(setWifiSettings) { |
lukas_formanek | 0:ea088908ad26 | 162 | SendCommand("+++",(char*)OK_MESSAGE,700); |
lukas_formanek | 0:ea088908ad26 | 163 | SendCommand("AT\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 164 | SendCommand("AT+CIPCLOSE\r\n",(char*)OK_MESSAGE,200); |
lukas_formanek | 0:ea088908ad26 | 165 | SendCommand(wifiSettings,(char*)OK_MESSAGE,1500); |
lukas_formanek | 0:ea088908ad26 | 166 | SendCommand("AT+RST\r\n","ready",1000); |
lukas_formanek | 0:ea088908ad26 | 167 | setWifiSettings = false; |
lukas_formanek | 0:ea088908ad26 | 168 | } |
lukas_formanek | 5:19b34c4d27a1 | 169 | }; |
lukas_formanek | 0:ea088908ad26 | 170 | |
lukas_formanek | 0:ea088908ad26 | 171 | void ESP8266::ClearBuffer() |
lukas_formanek | 0:ea088908ad26 | 172 | { |
lukas_formanek | 0:ea088908ad26 | 173 | memset(buffer, '\0', sizeof(buffer)); |
lukas_formanek | 0:ea088908ad26 | 174 | pt = 0; |
lukas_formanek | 5:19b34c4d27a1 | 175 | }; |
lukas_formanek | 5:19b34c4d27a1 | 176 | |
lukas_formanek | 5:19b34c4d27a1 | 177 | void ESP8266::ConfirmReceivedAck(uint8_t from) |
lukas_formanek | 5:19b34c4d27a1 | 178 | { |
lukas_formanek | 5:19b34c4d27a1 | 179 | wifiUart.printf("ACK|%c\r\n",from); |
lukas_formanek | 5:19b34c4d27a1 | 180 | }; |
lukas_formanek | 0:ea088908ad26 | 181 | |
lukas_formanek | 0:ea088908ad26 | 182 | void ESP8266::Test() |
lukas_formanek | 0:ea088908ad26 | 183 | { |
lukas_formanek | 0:ea088908ad26 | 184 | wifiUart.printf("+++\r\n"); |
lukas_formanek | 0:ea088908ad26 | 185 | wait(0.1); |
lukas_formanek | 0:ea088908ad26 | 186 | wifiUart.printf("AT\r\n"); |
lukas_formanek | 0:ea088908ad26 | 187 | wait(0.2); |
lukas_formanek | 0:ea088908ad26 | 188 | wifiUart.printf("AT\r\n"); |
lukas_formanek | 0:ea088908ad26 | 189 | wait(0.2); |
lukas_formanek | 0:ea088908ad26 | 190 | wifiUart.printf("AT\r\n"); |
lukas_formanek | 0:ea088908ad26 | 191 | printf("ATTTTTTTTTTT"); |
lukas_formanek | 0:ea088908ad26 | 192 | wait(0.2); |
lukas_formanek | 5:19b34c4d27a1 | 193 | }; |