{"id":3265,"date":"2019-12-09T21:59:13","date_gmt":"2019-12-09T20:59:13","guid":{"rendered":"http:\/\/arduino.net.pl\/?p=3265"},"modified":"2020-12-10T21:45:25","modified_gmt":"2020-12-10T20:45:25","slug":"blynk-piny-virtualne-virtual-pins-blynktimer","status":"publish","type":"post","link":"https:\/\/arduino.net.pl\/index.php\/blynk-piny-virtualne-virtual-pins-blynktimer\/","title":{"rendered":"Blynk, piny virtualne (Virtual Pins), BlynkTimer"},"content":{"rendered":"\n<p><strong>Piny virtualne<\/strong> to bardzo wa\u017cne narz\u0119dzie komunikacji mi\u0119dzy aplikacj\u0105 <strong>Blynk<\/strong> a urz\u0105dzeniami.  Poni\u017cej tutorial i strona z kr\u00f3tkim wyja\u015bnieniem pracy z virtualnymi pinami.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>https:\/\/www.youtube.com\/watch?v=iueWEkM6cuQ<\/li><li>http:\/\/help.blynk.cc\/en\/articles\/512061-what-is-virtual-pins<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\nBLYNK_WRITE(V0)\n{ \nint pinValue = param.asInt();\nSerial.println(pinValue);\n}\n\nBLYNK_WRITE(V1)\n{ \nSerial.println(param.asStr());\n}\n\nBLYNK_WRITE(V2)\n{ \nSerial.println(param.asFloat());\n}\n\nvoid loop()\n{\nBlynk.run();\n}\n<\/pre><\/div>\n\n\n<p>W <strong>aplikacji<\/strong> BLYNK w telefonie tworzymy dwa suwaki na pinach <strong>V0, V1<\/strong> oraz przycisk (button) na <strong>V2<\/strong>. <br>Przesuwaj\u0105c <strong>V0<\/strong> wysy\u0142amy z p\u0142ytki po\u0142ow\u0119 warto\u015bci na <strong>V1<\/strong> (suwak oczywi\u015bcie reaguje). Przycisk <strong>V2<\/strong> ustawia oba suwaki na warto\u015bci 1000. U\u017cycie wirtualnych pin\u00f3w b\u0119dzie przydatne przy ustawianiu na przyk\u0142ad scen o\u015bwietleniowych w pokoju.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;BlynkSimpleEsp8266.h&gt;\n#include &quot;config.h&quot; \/\/auth, ssid, pass\n\n#define BLYNK_PRINT Serial\n\nBlynkTimer timer;\n\nvoid setup()\n{\n  \/\/ Debug console\n  Serial.begin(9600);\n\/\/  pinMode(buttonPin,INPUT);\n\/\/  pinMode(ledPin,OUTPUT);\n\n\n  Blynk.begin(auth, ssid, pass);\n  \/\/ You can also specify server:\n  \/\/Blynk.begin(auth, ssid, pass, &quot;blynk-cloud.com&quot;, 80);\n  \/\/Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);\n}\n\nvoid loop()\n{\nBlynk.run();\ntimer.run();\n}  \n\nBLYNK_WRITE(V0)\n{\nint pinValue = param.asInt();\nBlynk.virtualWrite(V1, pinValue\/2); \nSerial.println(pinValue);\n}\n\nBLYNK_WRITE(V2)\n{\nint pinValue = param.asInt();\nSerial.println(pinValue);\nBlynk.virtualWrite(V0, 1000); \nBlynk.virtualWrite(V1, 1000); \n\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">BlynkTimer<\/h2>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<p>It\u2019s important to send data in intervals and keep the void loop() as clean as possible.&nbsp;<\/p>\n\n\n\n<p><code>BlynkTimer<\/code>&nbsp;allows you to send data periodically with given intervals not interfering with Blynk library routines&nbsp;<code>Blynk Timer<\/code>&nbsp;inherits&nbsp;<a href=\"http:\/\/playground.arduino.cc\/Code\/SimpleTimer\">SimpleTimer Library<\/a>, a well known and widely used library to time multiple events on hardware.&nbsp;<code>BlynkTimer<\/code>&nbsp;is included in Blynk library by default and there is no need to install SimpleTimer separately or include&nbsp;<code>SimpleTimer.h<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A single&nbsp;<code>BlynkTimer<\/code>&nbsp;object allows to schedule up to 16 timers<\/li><li>Improved compatibility with boards like&nbsp;<code>Arduino 101<\/code>,&nbsp;<code>Intel Galileo<\/code>, etc.<\/li><li>When a timer struggles to run multiple times (due to a blocked&nbsp;<code>loop<\/code>), it just skips all the missed intervals, and calls your function only once. This differs from&nbsp;<code>SimpleTimer<\/code>, which could call your function multiple times in this scenario.<\/li><\/ul>\n\n\n\n<p>For more information on timer usage, please see:&nbsp;<a href=\"http:\/\/playground.arduino.cc\/Code\/SimpleTimer\">http:\/\/playground.arduino.cc\/Code\/SimpleTimer<\/a><br>And here is a BlynkTimer&nbsp;<a href=\"https:\/\/github.com\/blynkkk\/blynk-library\/blob\/master\/examples\/GettingStarted\/PushData\/PushData.ino#L30\">example sketch<\/a>.<\/p>\n\n\n\n<p>Please also remember that a single&nbsp;<code>BlynkTimer<\/code>&nbsp;can schedule many timers, so most probably you need only one instance of BlynkTimer in your sketch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Przyk\u0142ad<\/h3>\n<\/div><\/div>\n\n\n\n<p>Po w\u0142\u0105czeniu button V2 miga dioda V3 (WidgetLED) i Built in LED r\u00f3wnocze\u015bnie timer w\u0142\u0105cza Relay (D1) i odlicza 10 sekund po tym czasie wy\u0142\u0105cza Relay i migaj\u0105cy led. <br>Kod pochodzi <strong><a href=\"https:\/\/community.blynk.cc\/t\/c-blynk-code-examples-for-basic-tasks-work-in-progress\/22596\/23\">st\u0105d<\/a><\/strong>. Doda\u0142em tylko blynkowy <strong>WidgetLED<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; light: false; title: Kod:; toolbar: true; notranslate\" title=\"Kod:\">\n\/*\n * \n * Blynk Testowy serwer publiczny\n * Po w\u0142\u0105czeniu button V2 miga dioda V3 (WidgetLED) i Built in LED \n * r\u00f3wnocze\u015bnie timer w\u0142\u0105cza Relay (D1) i odlicza 10 sekund \n * po tym czasie wy\u0142\u0105cza Relay i migaj\u0105cy led\n * \n *\/\n#define BLYNK_PRINT Serial\n\n#include &amp;lt;ESP8266WiFi.h&gt;\n#include &amp;lt;BlynkSimpleEsp8266.h&gt;\n#include &quot;config.h&quot; \/\/auth, ssid, pass\n\nint Latch;\nint Flag;\n#define DeviceLED 2 \/\/ Built in LED for Wemos or NodeMCU v3, Or 16 for NodeMCU v1\n#define LatchRelay D1 \/\/  GPIO12 or D6\n\nBlynkTimer timer;\n\nWidgetLED led(V3);\n\nvoid setup() {\n  pinMode(DeviceLED, OUTPUT);\n  pinMode(LatchRelay, OUTPUT);\n  WiFi.begin(ssid, pass);\n\/\/  Blynk.config(auth, server, port);\n\/\/  Blynk.connect();\n    Blynk.begin(auth, ssid, pass);\n}\n\n\n\n\/\/===== LATCH(Relay) &amp;amp; LED PULSE - BLYNK Functions =====\nBLYNK_WRITE(V2)  \/\/ Virtual button on V0 to activate Relay_LED pulses\n{\n  \/\/Serial.println(&quot;Latch LED&quot;);\n  Latch = param.asInt();\n  if (Latch == 1 &amp;amp;&amp;amp; Flag == 0) {\n    Flag = 1;  \/\/ Keeps from allowing button press more then once while relay activated\n    digitalWrite(LatchRelay, HIGH); \/\/ Activate Relay\n    timer.setTimer(1000L, blinkMyLEDon, 10);  \/\/ Pulse LED routine (LedON\/LedOFF) 10 times\n    timer.setTimeout(10000L, RelayOFF);  \/\/ Deactivare Relay after 10 seconds\n  }  \/\/ END if\n}  \/\/ END Blynk Function\n\nvoid RelayOFF() {\n  digitalWrite(LatchRelay, LOW);\n  Flag = 0;  \/\/ reset flag after relay disables\n}  \/\/ END Function\n\nvoid blinkMyLEDon() {\n  digitalWrite(DeviceLED, LOW); \/\/ Turn ON built in LED (triggered LOW)\n  led.on();\n  timer.setTimeout(500L, blinkMyLEDoff);  \/\/ Run LED OFF routine once in 1\/2 second\n}  \/\/ END Function\n\nvoid blinkMyLEDoff() {\n  digitalWrite(DeviceLED, HIGH); \/\/ Turn OFF built in LED (triggered LOW)\n  led.off();\n}  \/\/ END Function\n\n\n\nvoid loop() {\n  Blynk.run();\n  timer.run(); \/\/ This keeps checking the timers to see if then need to be processed\n}\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">SimpleTimer() Functions<\/h2>\n\n\n\n<p>The constructor. You usually need only one&nbsp;SimpleTimer&nbsp;object in a sketch.<\/p>\n\n\n\n<p><code>SimpleTimer timer;<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">int setInterval(long d, timer_callback f)<\/h3>\n\n\n\n<p>Call function f every d milliseconds. The callback function must be declared as&nbsp;<code>void f()<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void repeatMe() {\n    \/\/ do something\n}\n\ntimerId = timer.setInterval(1000, repeatMe);\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">int setTimeout(long d, timer_callback f)<\/h3>\n\n\n\n<p>Call function f once after d milliseconds. The callback function must be declared as&nbsp;<code>void f()<\/code>. After f has been called, the interval is deleted, therefore the value timerId is no longer valid.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void callMeLater() {\n    \/\/ do something\n}\n\ntimerId = timer.setTimeout(1000, callMeLater);\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">int setTimer(long d, timer_callback f, int n)<\/h3>\n\n\n\n<p>Call function f every d milliseconds for n times. The callback function must be declared as&nbsp;<code>void f()<\/code>. After f has been called the specified number of times, the interval is deleted, therefore the value timerId is no longer valid.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void repeatMeFiveTimes() {\n    \/\/ do something\n}\n\ntimerId = timer.setTimer(1000, repeatMeFiveTimes, 5);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">boolean isEnabled(int timerId)<\/h3>\n\n\n\n<p>Returns true if the specified timer is enabled<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if(timer.isEnabled(timerId) {\n    \/\/ do domething\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">void enable(int timerId)<\/h3>\n\n\n\n<p>Enables the specified timer.<\/p>\n\n\n\n<p><code>timer.enable(timerId);<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">void disable(int timerId)<\/h3>\n\n\n\n<p>Disables the specified timer.<\/p>\n\n\n\n<p><code>timer.disable(timerId);<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">void toggle(int timerId)<\/h3>\n\n\n\n<p>Enables the specified timer if it&#8217;s currently disabled, and vice-versa.<\/p>\n\n\n\n<p><code>timer.toggle(timerId);<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">void restartTimer(int timerId)<\/h3>\n\n\n\n<p>Causes the specified timer to start counting from &#8222;now&#8221;, i.e. the instant when restartTimer is called. The timer callback is not fired. A use case for this function is for example the implementation of a watchdog timer (pseudocode follows).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">void wdCallback() {\n    alert user or perform action to restore\n    program state (e.g. reset the microprocessor)\n}\n\nwd_timer_id;\n\nvoid setup() {\n    wd_timer_id = timer.setInterval(10000, wdCallback);\n}\n\nvoid loop() {\n    timer.run();\n\n    big complex critical code\n\n    timer.restartTimer(wd_timer_id);\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">void deleteTimer(int timerId)<\/h3>\n\n\n\n<p>Free the specified timerId slot. You should need to call this only if you have interval slots that you don&#8217;t need anymore. The other timer types are automatically deleted once the specified number of repetitions have been executed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">void getNumTimers()<\/h3>\n\n\n\n<p>Return the number of used slots in a timer object.<\/p>\n\n\n\n<p><code>n = timer.getNumTimers();<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Piny virtualne to bardzo wa\u017cne narz\u0119dzie komunikacji mi\u0119dzy aplikacj\u0105 Blynk a urz\u0105dzeniami. Poni\u017cej tutorial i strona z kr\u00f3tkim wyja\u015bnieniem pracy z virtualnymi pinami. https:\/\/www.youtube.com\/watch?v=iueWEkM6cuQ http:\/\/help.blynk.cc\/en\/articles\/512061-what-is-virtual-pins W aplikacji BLYNK w telefonie&#8230;<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[95,5],"tags":[94,98],"class_list":["post-3265","post","type-post","status-publish","format-standard","hentry","category-blynk","category-esp8266","tag-blynk","tag-timer"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/comments?post=3265"}],"version-history":[{"count":14,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3265\/revisions"}],"predecessor-version":[{"id":4120,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/posts\/3265\/revisions\/4120"}],"wp:attachment":[{"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/media?parent=3265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/categories?post=3265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino.net.pl\/index.php\/wp-json\/wp\/v2\/tags?post=3265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}