hadif azli
/
TEST123
BLYNK TEST
BlynkSimpleShieldEsp8266_HardSer.h@3:4cd9171ba989, 2016-06-16 (annotated)
- Committer:
- lixianyu
- Date:
- Thu Jun 16 08:08:30 2016 +0000
- Revision:
- 3:4cd9171ba989
9600?????ESP8266????????115200??ESP8266?????????9600????????????Timer???????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lixianyu | 3:4cd9171ba989 | 1 | /** |
lixianyu | 3:4cd9171ba989 | 2 | * @file BlynkSimpleShieldEsp8266.h |
lixianyu | 3:4cd9171ba989 | 3 | * @author Volodymyr Shymanskyy |
lixianyu | 3:4cd9171ba989 | 4 | * @license This project is released under the MIT License (MIT) |
lixianyu | 3:4cd9171ba989 | 5 | * @copyright Copyright (c) 2015 Volodymyr Shymanskyy |
lixianyu | 3:4cd9171ba989 | 6 | * @date Jun 2015 |
lixianyu | 3:4cd9171ba989 | 7 | * @brief |
lixianyu | 3:4cd9171ba989 | 8 | * |
lixianyu | 3:4cd9171ba989 | 9 | */ |
lixianyu | 3:4cd9171ba989 | 10 | |
lixianyu | 3:4cd9171ba989 | 11 | #ifndef BlynkSimpleShieldEsp8266_h |
lixianyu | 3:4cd9171ba989 | 12 | #define BlynkSimpleShieldEsp8266_h |
lixianyu | 3:4cd9171ba989 | 13 | |
lixianyu | 3:4cd9171ba989 | 14 | #ifdef ESP8266 |
lixianyu | 3:4cd9171ba989 | 15 | #error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting. |
lixianyu | 3:4cd9171ba989 | 16 | #endif |
lixianyu | 3:4cd9171ba989 | 17 | |
lixianyu | 3:4cd9171ba989 | 18 | #ifndef BLYNK_INFO_CONNECTION |
lixianyu | 3:4cd9171ba989 | 19 | #define BLYNK_INFO_CONNECTION "ESP8266" |
lixianyu | 3:4cd9171ba989 | 20 | #endif |
lixianyu | 3:4cd9171ba989 | 21 | |
lixianyu | 3:4cd9171ba989 | 22 | extern Serial pc; |
lixianyu | 3:4cd9171ba989 | 23 | //#define LOG_ESP8266 |
lixianyu | 3:4cd9171ba989 | 24 | #ifdef LOG_ESP8266 |
lixianyu | 3:4cd9171ba989 | 25 | #define ESP8266_LOG pc.printf |
lixianyu | 3:4cd9171ba989 | 26 | #define LOG_ENTER ESP8266_LOG("Enter %s\r\n", __func__); |
lixianyu | 3:4cd9171ba989 | 27 | #else |
lixianyu | 3:4cd9171ba989 | 28 | #define ESP8266_LOG(...) |
lixianyu | 3:4cd9171ba989 | 29 | #define LOG_ENTER |
lixianyu | 3:4cd9171ba989 | 30 | #endif |
lixianyu | 3:4cd9171ba989 | 31 | |
lixianyu | 3:4cd9171ba989 | 32 | #define BLYNK_SEND_ATOMIC |
lixianyu | 3:4cd9171ba989 | 33 | |
lixianyu | 3:4cd9171ba989 | 34 | // TODO: Remove this hotfix |
lixianyu | 3:4cd9171ba989 | 35 | #define BLYNK_NO_INFO |
lixianyu | 3:4cd9171ba989 | 36 | |
lixianyu | 3:4cd9171ba989 | 37 | //#include <BlynkApiArduino.h> |
lixianyu | 3:4cd9171ba989 | 38 | #include <BlynkApiMbed.h> |
lixianyu | 3:4cd9171ba989 | 39 | #include <Blynk/BlynkProtocol.h> |
lixianyu | 3:4cd9171ba989 | 40 | #include <utility/BlynkFifo.h> |
lixianyu | 3:4cd9171ba989 | 41 | #include <ESP8266_HardSer.h> |
lixianyu | 3:4cd9171ba989 | 42 | |
lixianyu | 3:4cd9171ba989 | 43 | class BlynkTransportShieldEsp8266 |
lixianyu | 3:4cd9171ba989 | 44 | { |
lixianyu | 3:4cd9171ba989 | 45 | static void onData(uint8_t mux_id, uint32_t len, void* ptr) { |
lixianyu | 3:4cd9171ba989 | 46 | ((BlynkTransportShieldEsp8266*)ptr)->onData(mux_id, len); |
lixianyu | 3:4cd9171ba989 | 47 | } |
lixianyu | 3:4cd9171ba989 | 48 | |
lixianyu | 3:4cd9171ba989 | 49 | void onData(uint8_t mux_id, uint32_t len) { |
lixianyu | 3:4cd9171ba989 | 50 | //BLYNK_LOG2("Got ", len); |
lixianyu | 3:4cd9171ba989 | 51 | while (len) { |
lixianyu | 3:4cd9171ba989 | 52 | if (client->getUart()->readable()) { |
lixianyu | 3:4cd9171ba989 | 53 | uint8_t b = client->getUart()->getc(); |
lixianyu | 3:4cd9171ba989 | 54 | if(!buffer.push(b)) { |
lixianyu | 3:4cd9171ba989 | 55 | BLYNK_LOG1(BLYNK_F("Buffer overflow")); |
lixianyu | 3:4cd9171ba989 | 56 | ESP8266_LOG(BLYNK_F("Buffer overflow")); |
lixianyu | 3:4cd9171ba989 | 57 | } |
lixianyu | 3:4cd9171ba989 | 58 | len--; |
lixianyu | 3:4cd9171ba989 | 59 | } |
lixianyu | 3:4cd9171ba989 | 60 | } |
lixianyu | 3:4cd9171ba989 | 61 | } |
lixianyu | 3:4cd9171ba989 | 62 | |
lixianyu | 3:4cd9171ba989 | 63 | public: |
lixianyu | 3:4cd9171ba989 | 64 | BlynkTransportShieldEsp8266() |
lixianyu | 3:4cd9171ba989 | 65 | : client(NULL) |
lixianyu | 3:4cd9171ba989 | 66 | , status(false) |
lixianyu | 3:4cd9171ba989 | 67 | , domain(NULL) |
lixianyu | 3:4cd9171ba989 | 68 | , port(0) |
lixianyu | 3:4cd9171ba989 | 69 | {} |
lixianyu | 3:4cd9171ba989 | 70 | |
lixianyu | 3:4cd9171ba989 | 71 | void begin_domain(ESP8266* esp8266, const char* d, uint16_t p) { |
lixianyu | 3:4cd9171ba989 | 72 | LOG_ENTER; |
lixianyu | 3:4cd9171ba989 | 73 | client = esp8266; |
lixianyu | 3:4cd9171ba989 | 74 | client->setOnData(onData, this); |
lixianyu | 3:4cd9171ba989 | 75 | domain = d; |
lixianyu | 3:4cd9171ba989 | 76 | port = p; |
lixianyu | 3:4cd9171ba989 | 77 | } |
lixianyu | 3:4cd9171ba989 | 78 | |
lixianyu | 3:4cd9171ba989 | 79 | bool connect() { |
lixianyu | 3:4cd9171ba989 | 80 | ESP8266_LOG("connect(), domain = %s, port = %d\r\n", domain, port); |
lixianyu | 3:4cd9171ba989 | 81 | if (!domain || !port) |
lixianyu | 3:4cd9171ba989 | 82 | return false; |
lixianyu | 3:4cd9171ba989 | 83 | status = client->createTCP(domain, port); |
lixianyu | 3:4cd9171ba989 | 84 | return status; |
lixianyu | 3:4cd9171ba989 | 85 | } |
lixianyu | 3:4cd9171ba989 | 86 | |
lixianyu | 3:4cd9171ba989 | 87 | void disconnect() { |
lixianyu | 3:4cd9171ba989 | 88 | #if 0 |
lixianyu | 3:4cd9171ba989 | 89 | LOG_ENTER; |
lixianyu | 3:4cd9171ba989 | 90 | |
lixianyu | 3:4cd9171ba989 | 91 | status = false; |
lixianyu | 3:4cd9171ba989 | 92 | buffer.clear(); |
lixianyu | 3:4cd9171ba989 | 93 | client->releaseTCP(); |
lixianyu | 3:4cd9171ba989 | 94 | #endif |
lixianyu | 3:4cd9171ba989 | 95 | } |
lixianyu | 3:4cd9171ba989 | 96 | |
lixianyu | 3:4cd9171ba989 | 97 | size_t read(void* buf, size_t len) { |
lixianyu | 3:4cd9171ba989 | 98 | uint32_t start = millis(); |
lixianyu | 3:4cd9171ba989 | 99 | ESP8266_LOG("Waiting: %d, Occuied: %d\r\n", len, buffer.getOccupied()); |
lixianyu | 3:4cd9171ba989 | 100 | //BLYNK_LOG4("Waiting: ", len, " Occuied: ", buffer.getOccupied()); |
lixianyu | 3:4cd9171ba989 | 101 | while ((buffer.getOccupied() < len) && (millis() - start < 4500)) { |
lixianyu | 3:4cd9171ba989 | 102 | client->run(); |
lixianyu | 3:4cd9171ba989 | 103 | } |
lixianyu | 3:4cd9171ba989 | 104 | return buffer.read((uint8_t*)buf, len); |
lixianyu | 3:4cd9171ba989 | 105 | } |
lixianyu | 3:4cd9171ba989 | 106 | size_t write(const void* buf, size_t len) { |
lixianyu | 3:4cd9171ba989 | 107 | ESP8266_LOG("Enter write, len = %d\r\n", len); |
lixianyu | 3:4cd9171ba989 | 108 | if (client->send((const uint8_t*)buf, len)) { |
lixianyu | 3:4cd9171ba989 | 109 | return len; |
lixianyu | 3:4cd9171ba989 | 110 | } |
lixianyu | 3:4cd9171ba989 | 111 | return 0; |
lixianyu | 3:4cd9171ba989 | 112 | } |
lixianyu | 3:4cd9171ba989 | 113 | |
lixianyu | 3:4cd9171ba989 | 114 | bool connected() { |
lixianyu | 3:4cd9171ba989 | 115 | return status; |
lixianyu | 3:4cd9171ba989 | 116 | } |
lixianyu | 3:4cd9171ba989 | 117 | |
lixianyu | 3:4cd9171ba989 | 118 | int available() { |
lixianyu | 3:4cd9171ba989 | 119 | LOG_ENTER; |
lixianyu | 3:4cd9171ba989 | 120 | client->run(); |
lixianyu | 3:4cd9171ba989 | 121 | //BLYNK_LOG2("Still: ", buffer.getOccupied()); |
lixianyu | 3:4cd9171ba989 | 122 | return buffer.getOccupied(); |
lixianyu | 3:4cd9171ba989 | 123 | } |
lixianyu | 3:4cd9171ba989 | 124 | |
lixianyu | 3:4cd9171ba989 | 125 | private: |
lixianyu | 3:4cd9171ba989 | 126 | ESP8266* client; |
lixianyu | 3:4cd9171ba989 | 127 | bool status; |
lixianyu | 3:4cd9171ba989 | 128 | BlynkFifo<uint8_t,256> buffer; |
lixianyu | 3:4cd9171ba989 | 129 | const char* domain; |
lixianyu | 3:4cd9171ba989 | 130 | uint16_t port; |
lixianyu | 3:4cd9171ba989 | 131 | }; |
lixianyu | 3:4cd9171ba989 | 132 | |
lixianyu | 3:4cd9171ba989 | 133 | class BlynkWifi |
lixianyu | 3:4cd9171ba989 | 134 | : public BlynkProtocol<BlynkTransportShieldEsp8266> |
lixianyu | 3:4cd9171ba989 | 135 | { |
lixianyu | 3:4cd9171ba989 | 136 | typedef BlynkProtocol<BlynkTransportShieldEsp8266> Base; |
lixianyu | 3:4cd9171ba989 | 137 | public: |
lixianyu | 3:4cd9171ba989 | 138 | BlynkWifi(BlynkTransportShieldEsp8266& transp) |
lixianyu | 3:4cd9171ba989 | 139 | : Base(transp) |
lixianyu | 3:4cd9171ba989 | 140 | , wifi(NULL) |
lixianyu | 3:4cd9171ba989 | 141 | {} |
lixianyu | 3:4cd9171ba989 | 142 | |
lixianyu | 3:4cd9171ba989 | 143 | bool connectWiFi(const char* ssid, const char* pass) { |
lixianyu | 3:4cd9171ba989 | 144 | LOG_ENTER; |
lixianyu | 3:4cd9171ba989 | 145 | delay(500); |
lixianyu | 3:4cd9171ba989 | 146 | BLYNK_LOG2(BLYNK_F("Connecting to "), ssid); |
lixianyu | 3:4cd9171ba989 | 147 | ESP8266_LOG(BLYNK_F("Connecting to %s\r\n"), ssid); |
lixianyu | 3:4cd9171ba989 | 148 | /*if (!wifi->restart()) { |
lixianyu | 3:4cd9171ba989 | 149 | BLYNK_LOG1(BLYNK_F("Failed to restart")); |
lixianyu | 3:4cd9171ba989 | 150 | return false; |
lixianyu | 3:4cd9171ba989 | 151 | }*/ |
lixianyu | 3:4cd9171ba989 | 152 | if (!wifi->setEcho(0)) { |
lixianyu | 3:4cd9171ba989 | 153 | BLYNK_LOG1(BLYNK_F("Failed to disable Echo")); |
lixianyu | 3:4cd9171ba989 | 154 | ESP8266_LOG(BLYNK_F("Failed to disable Echo")); |
lixianyu | 3:4cd9171ba989 | 155 | return false; |
lixianyu | 3:4cd9171ba989 | 156 | } |
lixianyu | 3:4cd9171ba989 | 157 | if (!wifi->setOprToStation()) { |
lixianyu | 3:4cd9171ba989 | 158 | BLYNK_LOG1(BLYNK_F("Failed to set STA mode")); |
lixianyu | 3:4cd9171ba989 | 159 | ESP8266_LOG(BLYNK_F("Failed to set STA mode")); |
lixianyu | 3:4cd9171ba989 | 160 | return false; |
lixianyu | 3:4cd9171ba989 | 161 | } |
lixianyu | 3:4cd9171ba989 | 162 | if (wifi->joinAP(ssid, pass)) { |
lixianyu | 3:4cd9171ba989 | 163 | BLYNK_LOG2(BLYNK_F("IP: "), wifi->getLocalIP().c_str()); |
lixianyu | 3:4cd9171ba989 | 164 | ESP8266_LOG(BLYNK_F("IP: "), wifi->getLocalIP().c_str()); |
lixianyu | 3:4cd9171ba989 | 165 | } else { |
lixianyu | 3:4cd9171ba989 | 166 | BLYNK_LOG1(BLYNK_F("Failed to connect WiFi")); |
lixianyu | 3:4cd9171ba989 | 167 | ESP8266_LOG(BLYNK_F("Failed to connect WiFi")); |
lixianyu | 3:4cd9171ba989 | 168 | return false; |
lixianyu | 3:4cd9171ba989 | 169 | } |
lixianyu | 3:4cd9171ba989 | 170 | if (!wifi->disableMUX()) { |
lixianyu | 3:4cd9171ba989 | 171 | BLYNK_LOG1(BLYNK_F("Failed to disable MUX")); |
lixianyu | 3:4cd9171ba989 | 172 | ESP8266_LOG(BLYNK_F("Failed to disable MUX")); |
lixianyu | 3:4cd9171ba989 | 173 | } |
lixianyu | 3:4cd9171ba989 | 174 | BLYNK_LOG1(BLYNK_F("Connected to WiFi")); |
lixianyu | 3:4cd9171ba989 | 175 | ESP8266_LOG(BLYNK_F("Connected to WiFi")); |
lixianyu | 3:4cd9171ba989 | 176 | return true; |
lixianyu | 3:4cd9171ba989 | 177 | } |
lixianyu | 3:4cd9171ba989 | 178 | |
lixianyu | 3:4cd9171ba989 | 179 | void config(ESP8266& esp8266, |
lixianyu | 3:4cd9171ba989 | 180 | const char* auth, |
lixianyu | 3:4cd9171ba989 | 181 | const char* domain = BLYNK_DEFAULT_DOMAIN, |
lixianyu | 3:4cd9171ba989 | 182 | uint16_t port = BLYNK_DEFAULT_PORT) { |
lixianyu | 3:4cd9171ba989 | 183 | LOG_ENTER; |
lixianyu | 3:4cd9171ba989 | 184 | Base::begin(auth); |
lixianyu | 3:4cd9171ba989 | 185 | wifi = &esp8266; |
lixianyu | 3:4cd9171ba989 | 186 | this->conn.begin_domain(wifi, domain, port); |
lixianyu | 3:4cd9171ba989 | 187 | } |
lixianyu | 3:4cd9171ba989 | 188 | |
lixianyu | 3:4cd9171ba989 | 189 | void begin(const char* auth, |
lixianyu | 3:4cd9171ba989 | 190 | ESP8266& esp8266, |
lixianyu | 3:4cd9171ba989 | 191 | const char* ssid, |
lixianyu | 3:4cd9171ba989 | 192 | const char* pass, |
lixianyu | 3:4cd9171ba989 | 193 | const char* domain = BLYNK_DEFAULT_DOMAIN, |
lixianyu | 3:4cd9171ba989 | 194 | uint16_t port = BLYNK_DEFAULT_PORT) { |
lixianyu | 3:4cd9171ba989 | 195 | LOG_ENTER; |
lixianyu | 3:4cd9171ba989 | 196 | config(esp8266, auth, domain, port); |
lixianyu | 3:4cd9171ba989 | 197 | connectWiFi(ssid, pass); |
lixianyu | 3:4cd9171ba989 | 198 | } |
lixianyu | 3:4cd9171ba989 | 199 | |
lixianyu | 3:4cd9171ba989 | 200 | private: |
lixianyu | 3:4cd9171ba989 | 201 | ESP8266* wifi; |
lixianyu | 3:4cd9171ba989 | 202 | }; |
lixianyu | 3:4cd9171ba989 | 203 | |
lixianyu | 3:4cd9171ba989 | 204 | static BlynkTransportShieldEsp8266 _blynkTransport; |
lixianyu | 3:4cd9171ba989 | 205 | BlynkWifi Blynk(_blynkTransport); |
lixianyu | 3:4cd9171ba989 | 206 | |
lixianyu | 3:4cd9171ba989 | 207 | #include <BlynkWidgets.h> |
lixianyu | 3:4cd9171ba989 | 208 | |
lixianyu | 3:4cd9171ba989 | 209 | #endif |
lixianyu | 3:4cd9171ba989 | 210 |