Tijl Goens
/
EpaperdisplayII
tempcommit 13/05
Esp8266.cpp@2:048e163245b7, 2019-05-15 (annotated)
- Committer:
- tijl
- Date:
- Wed May 15 13:39:22 2019 +0000
- Revision:
- 2:048e163245b7
- Parent:
- 1:63664175e603
blub 15/05;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tijl | 1:63664175e603 | 1 | #include "Esp8266.h" |
tijl | 1:63664175e603 | 2 | #include <vector> |
tijl | 1:63664175e603 | 3 | using namespace std; |
tijl | 1:63664175e603 | 4 | |
tijl | 1:63664175e603 | 5 | Esp8266::Esp8266(PinName TX, PinName RX, int BaudRate, PinName ResetPin, PinName ChipSelect) { |
tijl | 1:63664175e603 | 6 | rst = new DigitalOut(ResetPin); |
tijl | 1:63664175e603 | 7 | cs = new DigitalOut(ChipSelect); |
tijl | 1:63664175e603 | 8 | setupModule(TX, RX, BaudRate); |
tijl | 1:63664175e603 | 9 | } |
tijl | 1:63664175e603 | 10 | |
tijl | 1:63664175e603 | 11 | void Esp8266::setupModule(PinName TX, PinName RX, int BaudRate) { |
tijl | 1:63664175e603 | 12 | cs->write(1); |
tijl | 1:63664175e603 | 13 | serial = new UARTSerial(TX, RX, BaudRate); |
tijl | 1:63664175e603 | 14 | parser = new ATCmdParser(serial); |
tijl | 1:63664175e603 | 15 | parser->debug_on(0); |
tijl | 1:63664175e603 | 16 | parser->set_delimiter("\r\n"); |
tijl | 1:63664175e603 | 17 | cs->write(0); |
tijl | 1:63664175e603 | 18 | } |
tijl | 1:63664175e603 | 19 | |
tijl | 1:63664175e603 | 20 | void Esp8266::toggleResetPin(void) { |
tijl | 1:63664175e603 | 21 | rst->write(0); |
tijl | 1:63664175e603 | 22 | wait(0.01); |
tijl | 1:63664175e603 | 23 | rst->write(1); |
tijl | 1:63664175e603 | 24 | } |
tijl | 1:63664175e603 | 25 | |
tijl | 1:63664175e603 | 26 | void Esp8266::setModuleMode(int Mode) { |
tijl | 1:63664175e603 | 27 | cs->write(1); |
tijl | 1:63664175e603 | 28 | toggleResetPin(); |
tijl | 1:63664175e603 | 29 | bool processed = false; |
tijl | 1:63664175e603 | 30 | do { |
tijl | 1:63664175e603 | 31 | processed = parser->send("AT+CWMODE=%d", Mode) && parser->recv("OK"); |
tijl | 1:63664175e603 | 32 | } |
tijl | 1:63664175e603 | 33 | while(processed == false); |
tijl | 1:63664175e603 | 34 | } |
tijl | 1:63664175e603 | 35 | |
tijl | 1:63664175e603 | 36 | void Esp8266::connectToAP(const char* SSID, const char* Password) { |
tijl | 1:63664175e603 | 37 | bool processed = false; |
tijl | 1:63664175e603 | 38 | do { |
tijl | 1:63664175e603 | 39 | processed = parser->send("AT+CWJAP_CUR=\"%s\",\"%s\"", SSID, Password) && parser->recv("OK"); |
tijl | 1:63664175e603 | 40 | } |
tijl | 1:63664175e603 | 41 | while(processed == false); |
tijl | 1:63664175e603 | 42 | } |
tijl | 1:63664175e603 | 43 | |
tijl | 2:048e163245b7 | 44 | string Esp8266::getRequest(const char* TCPorUDP, const char* Server, int Port, const char* ConnectionString, bool stayAlive) { |
tijl | 1:63664175e603 | 45 | bool processed = false; |
tijl | 1:63664175e603 | 46 | do { |
tijl | 1:63664175e603 | 47 | processed = parser->send("AT+CIPSTART=\"%s\",\"%s\",%d", TCPorUDP, Server, Port) && parser->recv("OK"); |
tijl | 1:63664175e603 | 48 | } |
tijl | 1:63664175e603 | 49 | while(processed == false); |
tijl | 1:63664175e603 | 50 | |
tijl | 1:63664175e603 | 51 | string con = "GET "; |
tijl | 1:63664175e603 | 52 | con.append(ConnectionString); |
tijl | 1:63664175e603 | 53 | con.append(" HTTP/1.1\r\nHost: "); |
tijl | 1:63664175e603 | 54 | con.append(Server); |
tijl | 1:63664175e603 | 55 | con.append("\r\n\r\n"); |
tijl | 1:63664175e603 | 56 | |
tijl | 1:63664175e603 | 57 | do { |
tijl | 1:63664175e603 | 58 | processed = parser->send("AT+CIPSEND=%d", con.length()) && parser->recv("OK"); |
tijl | 1:63664175e603 | 59 | } |
tijl | 1:63664175e603 | 60 | while(processed == false); |
tijl | 1:63664175e603 | 61 | |
tijl | 1:63664175e603 | 62 | parser->send("%s", con.c_str()); |
tijl | 2:048e163245b7 | 63 | if(!stayAlive) { |
tijl | 2:048e163245b7 | 64 | closeConnection(); |
tijl | 2:048e163245b7 | 65 | } |
tijl | 1:63664175e603 | 66 | return getJsonString(); |
tijl | 1:63664175e603 | 67 | } |
tijl | 1:63664175e603 | 68 | |
tijl | 1:63664175e603 | 69 | void Esp8266::closeConnection(void) { |
tijl | 1:63664175e603 | 70 | bool processed = false; |
tijl | 1:63664175e603 | 71 | do { |
tijl | 1:63664175e603 | 72 | processed = parser->send("AT+CIPCLOSE"); |
tijl | 1:63664175e603 | 73 | } |
tijl | 1:63664175e603 | 74 | while(processed == false); |
tijl | 1:63664175e603 | 75 | } |
tijl | 1:63664175e603 | 76 | |
tijl | 1:63664175e603 | 77 | string Esp8266::getJsonString(void) { |
tijl | 1:63664175e603 | 78 | char previousChar = ' '; |
tijl | 1:63664175e603 | 79 | char presentChar = ' '; |
tijl | 1:63664175e603 | 80 | bool startOfJSON = false; |
tijl | 1:63664175e603 | 81 | |
tijl | 1:63664175e603 | 82 | string resp = ""; |
tijl | 1:63664175e603 | 83 | |
tijl | 1:63664175e603 | 84 | while(1) { |
tijl | 1:63664175e603 | 85 | presentChar = (char)parser->getc(); |
tijl | 2:048e163245b7 | 86 | |
tijl | 1:63664175e603 | 87 | if(presentChar == '{' && previousChar == '[') { |
tijl | 1:63664175e603 | 88 | startOfJSON = true; |
tijl | 1:63664175e603 | 89 | } |
tijl | 1:63664175e603 | 90 | |
tijl | 1:63664175e603 | 91 | if(startOfJSON) { |
tijl | 1:63664175e603 | 92 | resp += presentChar; |
tijl | 1:63664175e603 | 93 | } |
tijl | 1:63664175e603 | 94 | |
tijl | 1:63664175e603 | 95 | if(presentChar == ']' && previousChar == '}') { |
tijl | 1:63664175e603 | 96 | break; |
tijl | 1:63664175e603 | 97 | } |
tijl | 1:63664175e603 | 98 | |
tijl | 1:63664175e603 | 99 | previousChar = presentChar; |
tijl | 1:63664175e603 | 100 | } |
tijl | 1:63664175e603 | 101 | |
tijl | 1:63664175e603 | 102 | cs->write(0); |
tijl | 1:63664175e603 | 103 | return resp; |
tijl | 1:63664175e603 | 104 | } |
tijl | 1:63664175e603 | 105 | |
tijl | 1:63664175e603 | 106 | vector<string> Esp8266::processJsonString(string response) { |
tijl | 1:63664175e603 | 107 | vector<string> jsonStrings; |
tijl | 1:63664175e603 | 108 | response = response.substr(0, response.length()-1); |
tijl | 1:63664175e603 | 109 | |
tijl | 1:63664175e603 | 110 | int delimiterPlace = 0; |
tijl | 1:63664175e603 | 111 | do { |
tijl | 1:63664175e603 | 112 | delimiterPlace = response.find("},{")-2; |
tijl | 1:63664175e603 | 113 | string sub; |
tijl | 1:63664175e603 | 114 | if(delimiterPlace < 0) { |
tijl | 1:63664175e603 | 115 | if(response[0] != '{') |
tijl | 1:63664175e603 | 116 | sub = response.substr(2, response.length()-3); |
tijl | 1:63664175e603 | 117 | else |
tijl | 1:63664175e603 | 118 | sub = response.substr(1, response.length()-2); |
tijl | 1:63664175e603 | 119 | |
tijl | 1:63664175e603 | 120 | jsonStrings.push_back(sub); |
tijl | 1:63664175e603 | 121 | break; |
tijl | 1:63664175e603 | 122 | } |
tijl | 1:63664175e603 | 123 | else { |
tijl | 1:63664175e603 | 124 | sub = response.substr(1, delimiterPlace+1); |
tijl | 1:63664175e603 | 125 | jsonStrings.push_back(sub); |
tijl | 1:63664175e603 | 126 | response.erase(0, delimiterPlace+4); |
tijl | 1:63664175e603 | 127 | } |
tijl | 1:63664175e603 | 128 | } |
tijl | 1:63664175e603 | 129 | while(delimiterPlace > 0); |
tijl | 1:63664175e603 | 130 | |
tijl | 1:63664175e603 | 131 | return jsonStrings; |
tijl | 1:63664175e603 | 132 | } |