BlynkApi.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**
  2. * @file BlynkApi.h
  3. * @author Volodymyr Shymanskyy
  4. * @license This project is released under the MIT License (MIT)
  5. * @copyright Copyright (c) 2015 Volodymyr Shymanskyy
  6. * @date Jan 2015
  7. * @brief High-level functions
  8. *
  9. */
  10. #ifndef BlynkApi_h
  11. #define BlynkApi_h
  12. #include <Blynk/BlynkConfig.h>
  13. #include <Blynk/BlynkDebug.h>
  14. #include <Blynk/BlynkParam.h>
  15. #include <Blynk/BlynkHandlers.h>
  16. #include <Blynk/BlynkProtocolDefs.h>
  17. /**
  18. * Represents high-level functions of Blynk
  19. */
  20. template <class Proto>
  21. class BlynkApi
  22. {
  23. public:
  24. BlynkApi() {
  25. Init();
  26. }
  27. #ifdef DOXYGEN // These API here are only for the documentation
  28. /**
  29. * Connects to the server.
  30. * Blocks until connected or timeout happens.
  31. * May take less or more then timeout value.
  32. *
  33. * @param timeout Connection timeout
  34. * @returns True if connected to the server
  35. */
  36. bool connect(unsigned long timeout = BLYNK_TIMEOUT_MS*3);
  37. /**
  38. * Disconnects from the server.
  39. * It will not try to reconnect, until connect() is called
  40. */
  41. void disconnect();
  42. /**
  43. * @returns True if connected to the server
  44. */
  45. bool connected();
  46. /**
  47. * Performs Blynk-related housekeeping
  48. * and processes incoming commands
  49. *
  50. * @param available True if there is incoming data to process
  51. * Only used when user manages connection manually.
  52. */
  53. bool run(bool available = false);
  54. #endif // DOXYGEN
  55. /**
  56. * Sends value to a Virtual Pin
  57. *
  58. * @param pin Virtual Pin number
  59. * @param data Value to be sent
  60. */
  61. template <typename T>
  62. void virtualWrite(int pin, const T& data) {
  63. char mem[BLYNK_MAX_SENDBYTES];
  64. BlynkParam cmd(mem, 0, sizeof(mem));
  65. cmd.add("vw");
  66. cmd.add(pin);
  67. cmd.add(data);
  68. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, cmd.getBuffer(), cmd.getLength()-1);
  69. }
  70. template <typename T1, typename T2>
  71. void virtualWrite(int pin, const T1& data1, const T2& data2) {
  72. char mem[BLYNK_MAX_SENDBYTES];
  73. BlynkParam cmd(mem, 0, sizeof(mem));
  74. cmd.add("vw");
  75. cmd.add(pin);
  76. cmd.add(data1);
  77. cmd.add(data2);
  78. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, cmd.getBuffer(), cmd.getLength()-1);
  79. }
  80. template <typename T1, typename T2, typename T3>
  81. void virtualWrite(int pin, const T1& data1, const T2& data2, const T3& data3) {
  82. char mem[BLYNK_MAX_SENDBYTES];
  83. BlynkParam cmd(mem, 0, sizeof(mem));
  84. cmd.add("vw");
  85. cmd.add(pin);
  86. cmd.add(data1);
  87. cmd.add(data2);
  88. cmd.add(data3);
  89. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, cmd.getBuffer(), cmd.getLength()-1);
  90. }
  91. template <typename T1, typename T2, typename T3, typename T4>
  92. void virtualWrite(int pin, const T1& data1, const T2& data2, const T3& data3, const T4& data4) {
  93. char mem[BLYNK_MAX_SENDBYTES];
  94. BlynkParam cmd(mem, 0, sizeof(mem));
  95. cmd.add("vw");
  96. cmd.add(pin);
  97. cmd.add(data1);
  98. cmd.add(data2);
  99. cmd.add(data3);
  100. cmd.add(data4);
  101. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, cmd.getBuffer(), cmd.getLength()-1);
  102. }
  103. /**
  104. * Sends buffer to a Virtual Pin
  105. *
  106. * @param pin Virtual Pin number
  107. * @param buff Data buffer
  108. * @param len Length of data
  109. */
  110. void virtualWriteBinary(int pin, const void* buff, size_t len) {
  111. char mem[8];
  112. BlynkParam cmd(mem, 0, sizeof(mem));
  113. cmd.add("vw");
  114. cmd.add(pin);
  115. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE, 0, cmd.getBuffer(), cmd.getLength(), buff, len);
  116. }
  117. /**
  118. * Sends BlynkParam to a Virtual Pin
  119. *
  120. * @param pin Virtual Pin number
  121. * @param param
  122. */
  123. void virtualWrite(int pin, const BlynkParam& param) {
  124. virtualWriteBinary(pin, param.getBuffer(), param.getLength());
  125. }
  126. /**
  127. * Requests Server to re-send current values for all widgets.
  128. */
  129. void syncAll() {
  130. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_SYNC);
  131. }
  132. /**
  133. * Requests App or Server to re-send current value of a Virtual Pin.
  134. * This will probably cause user-defined BLYNK_WRITE handler to be called.
  135. *
  136. * @param pin Virtual Pin number
  137. */
  138. void syncVirtual(int pin) {
  139. char mem[8];
  140. BlynkParam cmd(mem, 0, sizeof(mem));
  141. cmd.add("vr");
  142. cmd.add(pin);
  143. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_HARDWARE_SYNC, 0, cmd.getBuffer(), cmd.getLength()-1);
  144. }
  145. /**
  146. * Tweets a message
  147. *
  148. * @param msg Text of the message
  149. */
  150. template<typename T>
  151. void tweet(const T& msg) {
  152. char mem[BLYNK_MAX_SENDBYTES];
  153. BlynkParam cmd(mem, 0, sizeof(mem));
  154. cmd.add(msg);
  155. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_TWEET, 0, cmd.getBuffer(), cmd.getLength()-1);
  156. }
  157. /**
  158. * Sends a push notification to the App
  159. *
  160. * @param msg Text of the message
  161. */
  162. template<typename T>
  163. void notify(const T& msg) {
  164. char mem[BLYNK_MAX_SENDBYTES];
  165. BlynkParam cmd(mem, 0, sizeof(mem));
  166. cmd.add(msg);
  167. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_NOTIFY, 0, cmd.getBuffer(), cmd.getLength()-1);
  168. }
  169. /**
  170. * Sends an email message
  171. *
  172. * @param email Email to send to
  173. * @param subject Subject of message
  174. * @param msg Text of the message
  175. */
  176. template <typename T1, typename T2>
  177. void email(const char* email, const T1& subject, const T2& msg) {
  178. char mem[BLYNK_MAX_SENDBYTES];
  179. BlynkParam cmd(mem, 0, sizeof(mem));
  180. cmd.add(email);
  181. cmd.add(subject);
  182. cmd.add(msg);
  183. static_cast<Proto*>(this)->sendCmd(BLYNK_CMD_EMAIL, 0, cmd.getBuffer(), cmd.getLength()-1);
  184. }
  185. #if defined(BLYNK_EXPERIMENTAL)
  186. // Attention!
  187. // Every function in this section may be changed, removed or renamed.
  188. /**
  189. * Refreshes value of a widget by running
  190. * user-defined BLYNK_READ handler of a pin.
  191. *
  192. * @experimental
  193. *
  194. * @param pin Virtual Pin number
  195. */
  196. void refresh(int pin) {
  197. if (WidgetReadHandler handler = GetReadHandler(pin)) {
  198. BlynkReq req = { 0, BLYNK_SUCCESS, (uint8_t)pin };
  199. handler(req);
  200. }
  201. }
  202. /**
  203. * Delays for N milliseconds, handling server communication in background.
  204. *
  205. * @experimental
  206. * @warning Should be used very carefully, especially on platforms with small RAM.
  207. *
  208. * @param ms Milliseconds to wait
  209. */
  210. void delay(unsigned long ms) {
  211. uint16_t start = (uint16_t)micros();
  212. while (ms > 0) {
  213. static_cast<Proto*>(this)->run();
  214. #if !defined(BLYNK_NO_YIELD)
  215. yield();
  216. #endif
  217. if (((uint16_t)micros() - start) >= 1000) {
  218. ms--;
  219. start += 1000;
  220. }
  221. }
  222. }
  223. #endif
  224. protected:
  225. void Init();
  226. void processCmd(const void* buff, size_t len);
  227. void sendInfo();
  228. };
  229. #endif