Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Io_moon by
BlynkSimpleShieldEsp8266.h
00001 /** 00002 * @file BlynkSimpleShieldEsp8266.h 00003 * @author Volodymyr Shymanskyy 00004 * @license This project is released under the MIT License (MIT) 00005 * @copyright Copyright (c) 2015 Volodymyr Shymanskyy 00006 * @date Jun 2015 00007 * @brief 00008 * 00009 */ 00010 00011 #ifndef BlynkSimpleShieldEsp8266_h 00012 #define BlynkSimpleShieldEsp8266_h 00013 00014 #ifdef ESP8266 00015 #error This code is not intended to run on the ESP8266 platform! Please check your Tools->Board setting. 00016 #endif 00017 00018 //extern Timer g_Timer; 00019 00020 #ifndef BLYNK_INFO_CONNECTION 00021 #define BLYNK_INFO_CONNECTION "ESP8266" 00022 #endif 00023 00024 00025 extern Serial pc; 00026 //#define LOG_ESP8266 00027 #ifdef LOG_ESP8266 00028 #define ESP8266_LOG pc.printf 00029 #define LOG_ENTER ESP8266_LOG("Enter %s\r\n", __func__); 00030 #else 00031 #define ESP8266_LOG(...) 00032 #define LOG_ENTER 00033 #endif 00034 00035 #define BLYNK_MICRODUINO 00036 //#define BLYNK_SEND_ATOMIC 00037 00038 // TODO: Remove this hotfix 00039 //#define BLYNK_NO_INFO 00040 00041 #include <BlynkApiMbed.h> 00042 #include <Blynk/BlynkProtocol.h> 00043 #include <utility/BlynkFifo.h> 00044 #include <ESP8266_Lib.h> 00045 00046 class BlynkTransportShieldEsp8266 00047 { 00048 static void onData(uint8_t mux_id, uint32_t len, void* ptr) { 00049 ((BlynkTransportShieldEsp8266*)ptr)->onData(mux_id, len); 00050 } 00051 00052 void onData(uint8_t mux_id, uint32_t len) { 00053 //BLYNK_LOG2("Got ", len); 00054 ESP8266_LOG("onData(),mux_id=%d,len=%u\r\n", mux_id,len); 00055 while (len) { 00056 if (client->getUart()->readable()) { 00057 uint8_t b = client->getUart()->getc(); 00058 if(!buffer.push(b)) { 00059 BLYNK_LOG1(BLYNK_F("Buffer overflow")); 00060 } 00061 len--; 00062 } 00063 } 00064 } 00065 00066 public: 00067 BlynkTransportShieldEsp8266() 00068 : client(NULL) 00069 , status(false) 00070 , domain(NULL) 00071 , port(0) 00072 {} 00073 00074 void begin_domain(ESP8266* esp8266, const char* d, uint16_t p) { 00075 LOG_ENTER; 00076 client = esp8266; 00077 client->setOnData(onData, this); 00078 domain = d; 00079 port = p; 00080 } 00081 00082 bool connect() { 00083 uint32_t startTime = g_BlynkTimer.read_ms(); 00084 ESP8266_LOG("connect(), domain = %s, port = %d\r\n", domain, port); 00085 pc.printf("connect(), domain = %s, port = %d, startTime=%u\r\n", domain, port, startTime); 00086 if (!domain || !port) 00087 return false; 00088 status = client->createTCP(domain, port); 00089 ESP8266_LOG("status = %d\r\n", status); 00090 return status; 00091 } 00092 00093 void disconnect() { 00094 #if 1 00095 LOG_ENTER; 00096 status = false; 00097 buffer.clear(); 00098 client->releaseTCP(); 00099 #endif 00100 } 00101 00102 size_t read(void* buf, size_t len) { 00103 //uint32_t start = g_BlynkTimer.read_ms(); 00104 ESP8266_LOG("Waiting: %d, Occuied: %d\r\n", len, buffer.getOccupied()); 00105 //uint32_t start = millis(); 00106 //buffer.dump(); 00107 uint32_t start = g_BlynkTimer.read_ms(); 00108 //BLYNK_LOG4("Waiting: ", len, " Occuied: ", buffer.getOccupied()); 00109 while ((buffer.getOccupied() < len) && (g_BlynkTimer.read_ms() - start < 1500)) { 00110 client->run(); 00111 } 00112 return buffer.read((uint8_t*)buf, len); 00113 } 00114 00115 size_t write(const void* buf, size_t len) { 00116 ESP8266_LOG("Enter write, len = %d\r\n", len); 00117 if (client->send((const uint8_t*)buf, len)) { 00118 return len; 00119 } 00120 return 0; 00121 } 00122 00123 bool connected() { 00124 //LOG_ENTER 00125 return status; 00126 } 00127 00128 int available() { 00129 client->run(); 00130 //BLYNK_LOG2("Still: ", buffer.getOccupied()); 00131 return buffer.getOccupied(); 00132 } 00133 00134 private: 00135 ESP8266* client; 00136 bool status; 00137 BlynkFifo<uint8_t,256> buffer; 00138 const char* domain; 00139 uint16_t port; 00140 }; 00141 00142 class BlynkWifi 00143 : public BlynkProtocol<BlynkTransportShieldEsp8266> 00144 { 00145 typedef BlynkProtocol<BlynkTransportShieldEsp8266> Base; 00146 public: 00147 BlynkWifi(BlynkTransportShieldEsp8266& transp) 00148 : Base(transp) 00149 , wifi(NULL) { 00150 //pc.printf("Enter BlynkWifi(BlynkTransportShieldEsp8266& transp)\r\n"); 00151 //g_BlynkTimer.start(); 00152 } 00153 00154 bool connectWiFi(const char* ssid, const char* pass) { 00155 //int nowtime = millis(); 00156 int nowtime = g_BlynkTimer.read_ms(); 00157 ESP8266_LOG("connectWiFi: ssid=%s, pass=%s, nowtime=%u\r\n", ssid, pass, nowtime); 00158 wait_ms(500); 00159 BLYNK_LOG2(BLYNK_F("Connecting to "), ssid); 00160 /*if (!wifi->restart()) { 00161 BLYNK_LOG1(BLYNK_F("Failed to restart")); 00162 return false; 00163 }*/ 00164 if (!wifi->setEcho(0)) { 00165 BLYNK_LOG1(BLYNK_F("Failed to disable Echo")); 00166 ESP8266_LOG(BLYNK_F("Failed to disable Echo")); 00167 return false; 00168 } 00169 if (!wifi->setOprToStation()) { 00170 BLYNK_LOG1(BLYNK_F("Failed to set STA mode")); 00171 ESP8266_LOG(BLYNK_F("Failed to set STA mode")); 00172 return false; 00173 } 00174 if (wifi->joinAP(ssid, pass)) { 00175 BLYNK_LOG2(BLYNK_F("IP: "), wifi->getLocalIP().c_str()); 00176 ESP8266_LOG(BLYNK_F("IP: %s\r\n"), wifi->getLocalIP().c_str()); 00177 } else { 00178 BLYNK_LOG1(BLYNK_F("Failed to connect WiFi")); 00179 ESP8266_LOG(BLYNK_F("Failed to connect WiFi")); 00180 return false; 00181 } 00182 if (!wifi->disableMUX()) { 00183 BLYNK_LOG1(BLYNK_F("Failed to disable MUX")); 00184 ESP8266_LOG(BLYNK_F("Failed to disable MUX")); 00185 } 00186 BLYNK_LOG1(BLYNK_F("Connected to WiFi")); 00187 int nowtime1 = g_BlynkTimer.read_ms(); 00188 ESP8266_LOG("Connected to WiFi...%dms\r\n", nowtime1 - nowtime); 00189 return true; 00190 } 00191 00192 void config(ESP8266& esp8266, 00193 const char* auth, 00194 const char* domain = BLYNK_DEFAULT_DOMAIN, 00195 uint16_t port = BLYNK_DEFAULT_PORT) { 00196 LOG_ENTER; 00197 Base::begin(auth); 00198 wifi = &esp8266; 00199 this->conn.begin_domain(wifi, domain, port); 00200 } 00201 00202 void begin(const char* auth, 00203 ESP8266& esp8266, 00204 const char* ssid, 00205 const char* pass, 00206 const char* domain = BLYNK_DEFAULT_DOMAIN, 00207 uint16_t port = BLYNK_DEFAULT_PORT) { 00208 ESP8266_LOG("Enter begin, auth=%s\r\n", auth); 00209 config(esp8266, auth, domain, port); 00210 connectWiFi(ssid, pass); 00211 } 00212 00213 private: 00214 ESP8266* wifi; 00215 }; 00216 00217 static BlynkTransportShieldEsp8266 _blynkTransport; 00218 BlynkWifi Blynk(_blynkTransport); 00219 00220 #include <BlynkWidgets.h > 00221 00222 #endif 00223
Generated on Tue Jul 19 2022 01:01:49 by
1.7.2
