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 WIZwiki-7500_Blynk by
BlynkMbedClient.h
00001 /** 00002 * @file BlynkParam.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 Jan 2015 00007 * @brief 00008 * 00009 */ 00010 00011 #ifndef BlynkMbedClient_h 00012 #define BlynkMbedClient_h 00013 00014 #include <BlynkApiMbed.h > 00015 #include <Blynk/BlynkDebug.h> 00016 #include <TCPSocketConnection.h> 00017 00018 #if defined(ESP8266) && !defined(BLYNK_NO_YIELD) 00019 #define YIELD_FIX() BLYNK_RUN_YIELD(); 00020 #else 00021 #define YIELD_FIX() 00022 #endif 00023 00024 template <typename Client> 00025 class BlynkMbedClientGen : public Socket 00026 { 00027 public: 00028 BlynkMbedClientGen(Client& c) 00029 : client(NULL), domain(NULL), port(0), isConn(false) 00030 { 00031 setClient(&c); 00032 } 00033 00034 BlynkMbedClientGen() 00035 : client(NULL), domain(NULL), port(0), isConn(false) 00036 {} 00037 00038 void setClient(Client* c) { 00039 client = c; 00040 client->set_blocking(true, BLYNK_TIMEOUT_MS*1000); 00041 } 00042 00043 void begin(char* a, uint16_t p) { 00044 domain = NULL; 00045 port = p; 00046 addr = a; 00047 } 00048 00049 void begin(const char* d, uint16_t p) { 00050 domain = d; 00051 port = p; 00052 } 00053 00054 bool connect() { 00055 //printf("mbedClientGen - connect function\r\n"); 00056 if (domain) { 00057 //BLYNK_LOG4(BLYNK_F("Connecting to "), domain, ':', port); 00058 printf("Connectiong to %s : %d\r\n", domain, port); 00059 client->connect(domain, port); 00060 //printf("TCP Socket Connection finish\r\n"); 00061 isConn = (1 == client->is_connected()); 00062 return isConn; 00063 } else { //if (uint32_t(addr) != 0) { 00064 BLYNK_LOG_IP("Connecting to ", addr); 00065 isConn = (1 == client->connect(addr, port)); 00066 return isConn; 00067 } 00068 } 00069 00070 void disconnect() { 00071 isConn = false; 00072 if (client->getsock()) return; 00073 client->close(); 00074 client->setsock(-1); 00075 } 00076 00077 #ifdef BLYNK_ENC28J60_FIX 00078 size_t read(void* buf, size_t len) { 00079 while (client->available() < len) { } 00080 return client->read((uint8_t*)buf, len); 00081 } 00082 #else 00083 size_t read(void* buf, size_t len) { 00084 size_t res = client->receive((char*)buf, len); 00085 YIELD_FIX(); 00086 return res; 00087 } 00088 #endif 00089 00090 #ifdef BLYNK_RETRY_SEND 00091 size_t write(const void* buf, size_t len) { 00092 size_t sent = 0; 00093 int retry = 0; 00094 while (sent < len && ++retry < 10) { 00095 size_t w = client->write((const uint8_t*)buf+sent, len-sent); 00096 if (w != 0 && w != -1) { 00097 sent += w; 00098 } else { 00099 ::delay(50); 00100 #if defined(BLYNK_DEBUG) && defined(BLYNK_PRINT) 00101 BLYNK_PRINT_TIME(); 00102 BLYNK_PRINT.print(BLYNK_F("Retry ")); 00103 BLYNK_PRINT.print(retry); 00104 BLYNK_PRINT.print(BLYNK_F(" send: ")); 00105 BLYNK_PRINT.print(sent); 00106 BLYNK_PRINT.print('/'); 00107 BLYNK_PRINT.println(len); 00108 #endif 00109 } 00110 } 00111 return sent; 00112 } 00113 #else 00114 size_t write(const void* buf, size_t len) { 00115 YIELD_FIX(); 00116 size_t res = client->send((char*)buf, len); 00117 YIELD_FIX(); 00118 return res; 00119 } 00120 #endif 00121 00122 bool connected() { YIELD_FIX(); return isConn && client->is_connected(); } 00123 int available() { YIELD_FIX(); 00124 //printf("readable : %d, sock: %d\r\n", eth->wait_readable(_sock_fd, 3000000, 0),_sock_fd); 00125 //return eth->wait_readable(_sock_fd, 300000, 0); 00126 return eth->wait_readable(client->getsock() , 1000*60*10, 0); 00127 } 00128 00129 protected: 00130 Client* client; 00131 char* addr; 00132 const char* domain; 00133 uint16_t port; 00134 bool isConn; 00135 }; 00136 00137 typedef BlynkMbedClientGen<TCPSocketConnection> BlynkMbedClient; 00138 00139 #endif
Generated on Thu Jul 14 2022 00:21:50 by
1.7.2
