Piny virtualne to bardzo ważne narzędzie komunikacji między aplikacją Blynk a urządzeniami. Poniżej tutorial i strona z krótkim wyjaśnieniem pracy z virtualnymi pinami.
- https://www.youtube.com/watch?v=iueWEkM6cuQ
- http://help.blynk.cc/en/articles/512061-what-is-virtual-pins
BLYNK_WRITE(V0)
{
int pinValue = param.asInt();
Serial.println(pinValue);
}
BLYNK_WRITE(V1)
{
Serial.println(param.asStr());
}
BLYNK_WRITE(V2)
{
Serial.println(param.asFloat());
}
void loop()
{
Blynk.run();
}
W aplikacji BLYNK w telefonie tworzymy dwa suwaki na pinach V0, V1 oraz przycisk (button) na V2.
Przesuwając V0 wysyłamy z płytki połowę wartości na V1 (suwak oczywiście reaguje). Przycisk V2 ustawia oba suwaki na wartości 1000. Użycie wirtualnych pinów będzie przydatne przy ustawianiu na przykład scen oświetleniowych w pokoju.
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "config.h" //auth, ssid, pass
#define BLYNK_PRINT Serial
BlynkTimer timer;
void setup()
{
// Debug console
Serial.begin(9600);
// pinMode(buttonPin,INPUT);
// pinMode(ledPin,OUTPUT);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}
void loop()
{
Blynk.run();
timer.run();
}
BLYNK_WRITE(V0)
{
int pinValue = param.asInt();
Blynk.virtualWrite(V1, pinValue/2);
Serial.println(pinValue);
}
BLYNK_WRITE(V2)
{
int pinValue = param.asInt();
Serial.println(pinValue);
Blynk.virtualWrite(V0, 1000);
Blynk.virtualWrite(V1, 1000);
}
BlynkTimer
It’s important to send data in intervals and keep the void loop() as clean as possible.
BlynkTimer
allows you to send data periodically with given intervals not interfering with Blynk library routines Blynk Timer
inherits SimpleTimer Library, a well known and widely used library to time multiple events on hardware. BlynkTimer
is included in Blynk library by default and there is no need to install SimpleTimer separately or include SimpleTimer.h
- A single
BlynkTimer
object allows to schedule up to 16 timers - Improved compatibility with boards like
Arduino 101
,Intel Galileo
, etc. - When a timer struggles to run multiple times (due to a blocked
loop
), it just skips all the missed intervals, and calls your function only once. This differs fromSimpleTimer
, which could call your function multiple times in this scenario.
For more information on timer usage, please see: http://playground.arduino.cc/Code/SimpleTimer
And here is a BlynkTimer example sketch.
Please also remember that a single BlynkTimer
can schedule many timers, so most probably you need only one instance of BlynkTimer in your sketch.
Przykład
Po włączeniu button V2 miga dioda V3 (WidgetLED) i Built in LED równocześnie timer włącza Relay (D1) i odlicza 10 sekund po tym czasie wyłącza Relay i migający led.
Kod pochodzi stąd. Dodałem tylko blynkowy WidgetLED
/*
*
* Blynk Testowy serwer publiczny
* Po włączeniu button V2 miga dioda V3 (WidgetLED) i Built in LED
* równocześnie timer włącza Relay (D1) i odlicza 10 sekund
* po tym czasie wyłącza Relay i migający led
*
*/
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include "config.h" //auth, ssid, pass
int Latch;
int Flag;
#define DeviceLED 2 // Built in LED for Wemos or NodeMCU v3, Or 16 for NodeMCU v1
#define LatchRelay D1 // GPIO12 or D6
BlynkTimer timer;
WidgetLED led(V3);
void setup() {
pinMode(DeviceLED, OUTPUT);
pinMode(LatchRelay, OUTPUT);
WiFi.begin(ssid, pass);
// Blynk.config(auth, server, port);
// Blynk.connect();
Blynk.begin(auth, ssid, pass);
}
//===== LATCH(Relay) & LED PULSE - BLYNK Functions =====
BLYNK_WRITE(V2) // Virtual button on V0 to activate Relay_LED pulses
{
//Serial.println("Latch LED");
Latch = param.asInt();
if (Latch == 1 && Flag == 0) {
Flag = 1; // Keeps from allowing button press more then once while relay activated
digitalWrite(LatchRelay, HIGH); // Activate Relay
timer.setTimer(1000L, blinkMyLEDon, 10); // Pulse LED routine (LedON/LedOFF) 10 times
timer.setTimeout(10000L, RelayOFF); // Deactivare Relay after 10 seconds
} // END if
} // END Blynk Function
void RelayOFF() {
digitalWrite(LatchRelay, LOW);
Flag = 0; // reset flag after relay disables
} // END Function
void blinkMyLEDon() {
digitalWrite(DeviceLED, LOW); // Turn ON built in LED (triggered LOW)
led.on();
timer.setTimeout(500L, blinkMyLEDoff); // Run LED OFF routine once in 1/2 second
} // END Function
void blinkMyLEDoff() {
digitalWrite(DeviceLED, HIGH); // Turn OFF built in LED (triggered LOW)
led.off();
} // END Function
void loop() {
Blynk.run();
timer.run(); // This keeps checking the timers to see if then need to be processed
}
SimpleTimer() Functions
The constructor. You usually need only one SimpleTimer object in a sketch.
SimpleTimer timer;
int setInterval(long d, timer_callback f)
Call function f every d milliseconds. The callback function must be declared as void f()
.
void repeatMe() { // do something } timerId = timer.setInterval(1000, repeatMe);
int setTimeout(long d, timer_callback f)
Call function f once after d milliseconds. The callback function must be declared as void f()
. After f has been called, the interval is deleted, therefore the value timerId is no longer valid.
void callMeLater() { // do something } timerId = timer.setTimeout(1000, callMeLater);
int setTimer(long d, timer_callback f, int n)
Call function f every d milliseconds for n times. The callback function must be declared as void f()
. After f has been called the specified number of times, the interval is deleted, therefore the value timerId is no longer valid.
void repeatMeFiveTimes() { // do something } timerId = timer.setTimer(1000, repeatMeFiveTimes, 5);
boolean isEnabled(int timerId)
Returns true if the specified timer is enabled
if(timer.isEnabled(timerId) { // do domething }
void enable(int timerId)
Enables the specified timer.
timer.enable(timerId);
void disable(int timerId)
Disables the specified timer.
timer.disable(timerId);
void toggle(int timerId)
Enables the specified timer if it’s currently disabled, and vice-versa.
timer.toggle(timerId);
void restartTimer(int timerId)
Causes the specified timer to start counting from “now”, 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).
void wdCallback() { alert user or perform action to restore program state (e.g. reset the microprocessor) } wd_timer_id; void setup() { wd_timer_id = timer.setInterval(10000, wdCallback); } void loop() { timer.run(); big complex critical code timer.restartTimer(wd_timer_id); }
void deleteTimer(int timerId)
Free the specified timerId slot. You should need to call this only if you have interval slots that you don’t need anymore. The other timer types are automatically deleted once the specified number of repetitions have been executed.
void getNumTimers()
Return the number of used slots in a timer object.
n = timer.getNumTimers();