forked from blynkkk/blynk-sketch-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_examples.js
1853 lines (1633 loc) · 45.3 KB
/
data_examples.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const examples = {
/*
"Simple" : {
},
*/
/***********************************************************
* GettingStarted
***********************************************************/
"GettingStarted/BlynkBlink" : {
comment : `
You’ll need:
- Blynk IoT app (download from App Store or Google Play)
- <%= name %> board
- Decide how to connect to Blynk
(USB, Ethernet, Wi-Fi, Bluetooth, ...)
There is a bunch of great example sketches included to show you how to get
started. Think of them as LEGO bricks and combine them as you wish.
For example, take the Ethernet Shield sketch and combine it with the
Servo example, or choose a USB sketch and add a code from SendData
example.
`,
loop: `
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
`
},
"GettingStarted/BlynkSimpleDemo" : {
comment : `
This is a simple demo of sending and receiving some data.
Be sure to check out other examples!
`,
glob: `
BlynkTimer timer;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
int value = param.asInt();
// Update state
Blynk.virtualWrite(V1, value);
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.setProperty(V3, "offImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations.png");
Blynk.setProperty(V3, "onImageUrl", "https://static-image.nyc3.cdn.digitaloceanspaces.com/general/fte/congratulations_pressed.png");
Blynk.setProperty(V3, "url", "https://docs.blynk.io/en/getting-started/what-do-i-need-to-blynk/how-quickstart-device-was-made");
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V2, millis() / 1000);
}
`,
init: `
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
`,
loop: `
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
`
},
/***********************************************/
"GettingStarted/GetData" : {
comment : `
You can use this sketch as a debug tool that prints all incoming values
sent by a widget connected to a Virtual Pin 1 in the Blynk App.
App project setup:
Slider widget (0...100) on V1
`,
glob: `
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
<%= serial_dbg %>.print("V1 Slider value is: ");
<%= serial_dbg %>.println(pinValue);
}
`,
},
/***********************************************/
"GettingStarted/PushData" : {
comment : `
This example shows how value can be pushed from Arduino to
the Blynk App.
NOTE:
BlynkTimer provides SimpleTimer functionality:
http://playground.arduino.cc/Code/SimpleTimer
App project setup:
Value Display widget attached to Virtual Pin V5
`,
glob: `
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
`,
init: `
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
`,
loop: `
timer.run(); // Initiates BlynkTimer
`
},
/***********************************************/
"GettingStarted/PushDataOnRequest" : {
comment : `
This example shows how to send values to the Blynk App,
when there is a widget, attached to the Virtual Pin and it
is set to some frequency
Project setup in the app:
Value Display widget attached to V5. Set any reading
frequency (i.e. 1 second)
`,
glob: `
// Use Virtual pin 5 for uptime display
#define PIN_UPTIME V5
// This function tells Arduino what to do if there is a Widget
// which is requesting data for Virtual Pin (5)
BLYNK_READ(PIN_UPTIME)
{
// This command writes Arduino's uptime in seconds to Virtual Pin (5)
Blynk.virtualWrite(PIN_UPTIME, millis() / 1000);
}
`
},
/***********************************************/
"GettingStarted/Servo" : {
comment : `
Rotate a servo using a slider!
App project setup:
Slider widget (0...180) on V3
`,
inc: `
#include <Servo.h>
`,
glob: `
Servo servo;
BLYNK_WRITE(V3)
{
servo.write(param.asInt());
}
`,
init: `
servo.attach(9);
`,
},
/***********************************************/
"GettingStarted/VirtualPinRead" : {
comment : `
This sketch shows how to read values from Virtual Pins
App project setup:
Slider widget (0...100) on Virtual Pin V1
`,
glob: `
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// process received value
}
`
},
/***********************************************/
"GettingStarted/VirtualPinReply" : {
comment : `
This example shows how to send requested values to the Blynk App
Project setup in the app:
Value Display widget attached to V5. Set any reading
frequency (i.e. 1 second)
`,
glob: `
// This function is called when there is a Widget
// which is requesting data from Virtual Pin (5)
BLYNK_READ(V5)
{
// This command writes Arduino's uptime in seconds to Virtual Pin (5)
Blynk.virtualWrite(V5, millis() / 1000);
}
`
},
/***********************************************/
"GettingStarted/VirtualPinWrite" : {
comment : `
This sketch shows how to write values to Virtual Pins
NOTE:
BlynkTimer provides SimpleTimer functionality:
http://playground.arduino.cc/Code/SimpleTimer
App project setup:
Value Display widget attached to Virtual Pin V5
`,
glob: `
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void myTimerEvent()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
`,
init: `
// Setup a function to be called every second
timer.setInterval(1000L, myTimerEvent);
`,
loop: `
timer.run(); // Initiates BlynkTimer
`
},
/***********************************************************
* Widgets
***********************************************************/
"Widgets/Bridge" : {
comment : `
Control another device using Bridge widget!
Bridge is initialized with the token of any (Blynk-enabled) device.
After that, use the familiar functions to control it:
bridge.digitalWrite(8, HIGH)
bridge.digitalWrite("A0", LOW) // <- target needs to support "Named pins"
bridge.analogWrite(3, 123)
bridge.virtualWrite(V1, "hello")
`,
glob: `
// Bridge widget on virtual pin 1
WidgetBridge bridge1(V1);
// Timer for blynking
BlynkTimer timer;
static bool value = true;
void blynkAnotherDevice() // Here we will send HIGH or LOW once per second
{
// Send value to another device
if (value) {
bridge1.digitalWrite(9, HIGH); // Digital Pin 9 on the second board will be set HIGH
bridge1.virtualWrite(V5, 1); // Sends 1 value to BLYNK_WRITE(V5) handler on receiving side.
/////////////////////////////////////////////////////////////////////////////////////////
// Keep in mind that when performing virtualWrite with Bridge,
// second board will need to process the incoming command.
// It can be done by using this handler on the second board:
//
// BLYNK_WRITE(V5){
// int pinData = param.asInt(); // pinData variable will store value that came via Bridge
// }
//
/////////////////////////////////////////////////////////////////////////////////////////
} else {
bridge1.digitalWrite(9, LOW); // Digital Pin 9 on the second board will be set LOW
bridge1.virtualWrite(V5, 0); // Sends 0 value to BLYNK_WRITE(V5) handler on receiving side.
}
// Toggle value
value = !value;
}
BLYNK_CONNECTED() {
bridge1.setAuthToken("OtherAuthToken"); // Place the AuthToken of the second hardware here
}
`,
init: `
// Call blynkAnotherDevice every second
timer.setInterval(1000L, blynkAnotherDevice);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/Email" : {
comment : `
Simple e-mail example
App project setup:
E-mail Widget
Connect a button to digital pin 2 and GND
Pressing this button will send an e-mail
WARNING: You are limited to send ONLY ONE E-MAIL PER 5 SECONDS!
`,
defs : `
/* Set this to a bigger number, to enable sending longer messages */
//#define BLYNK_MAX_SENDBYTES 128
`,
glob: `
unsigned count = 0;
void emailOnButtonPress()
{
// *** WARNING: You are limited to send ONLY ONE E-MAIL PER 5 SECONDS! ***
// Let's send an e-mail when you press the button
// connected to digital pin 2 on your Arduino
int isButtonPressed = !digitalRead(2); // Invert state, since button is "Active LOW"
if (isButtonPressed) // You can write any condition to trigger e-mail sending
{
<%= serial_dbg %>.println("Button is pressed."); // This can be seen in the Serial Monitor
count++;
String body = String("You pushed the button ") + count + " times.";
Blynk.email("[email protected]", "Subject: Button Logger", body);
// Or, if you want to use the email specified in the App (like for App Export):
//Blynk.email("Subject: Button Logger", "You just pushed the button...");
}
}
`,
init: `
// Send e-mail when your hardware gets connected to Blynk Server
// Just put the recepient's "e-mail address", "Subject" and the "message body"
Blynk.email("[email protected]", "Subject", "My Blynk project is online.");
// Setting the button
pinMode(2, INPUT_PULLUP);
// Attach pin 2 interrupt to our handler
attachInterrupt(digitalPinToInterrupt(2), emailOnButtonPress, CHANGE);
`,
},
/***********************************************/
"Widgets/Eventor" : {
comment : `
You can use predefined rules on application side.
Project setup in the Blynk app:
Eventor widget with next rules :
a) When V0 is equal to 1, set V1 to 255;
b) When V0 is equal to 0, set V1 to 0;
Led widget on V1 pin
`,
glob: `
BlynkTimer timer;
boolean flag = true;
void sendFlagToServer() {
if (flag) {
Blynk.virtualWrite(V0, 1);
} else {
Blynk.virtualWrite(V0, 0);
}
flag = !flag;
}
BLYNK_WRITE(V1) {
//here you'll get 0 or 255
int ledValue = param.asInt();
}
`,
init: `
// Setup a function to be called every second
timer.setInterval(1000L, sendFlagToServer);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/GPS_Stream" : {
comment : `
App project setup:
GPS Stream widget on V1.
`,
glob: `
BLYNK_WRITE(V1) {
GpsParam gps(param);
// Print 6 decimal places for Lat, Lon
<%= serial_dbg %>.print("Lat: ");
<%= serial_dbg %>.println(gps.getLat(), 7);
<%= serial_dbg %>.print("Lon: ");
<%= serial_dbg %>.println(gps.getLon(), 7);
// Print 2 decimal places for Alt, Speed
<%= serial_dbg %>.print("Altitute: ");
<%= serial_dbg %>.println(gps.getAltitude(), 2);
<%= serial_dbg %>.print("Speed: ");
<%= serial_dbg %>.println(gps.getSpeed(), 2);
<%= serial_dbg %>.println();
}
`,
},
/***********************************************/
"Widgets/JoystickTwoAxis" : {
comment : `
You can receive x and y coords for joystick movement within App.
App project setup:
Two Axis Joystick on V1 in MERGE output mode.
MERGE mode means device will receive both x and y within 1 message
`,
glob: `
BLYNK_WRITE(V1) {
int x = param[0].asInt();
int y = param[1].asInt();
// Do something with x and y
<%= serial_dbg %>.print("X = ");
<%= serial_dbg %>.print(x);
<%= serial_dbg %>.print("; Y = ");
<%= serial_dbg %>.println(y);
}
`,
},
/***********************************************/
"Widgets/LCD/LCD_SimpleModePushing" : {
comment : `
Output any data on LCD widget!
App project setup:
LCD widget, SIMPLE mode, in widget settings :
- Select pin V0 for zero pin
- Select pin V1 for first pin
- Change "Reading Frequency" to PUSH mode
- Type into first edit field "/pin0/ seconds"
- Type into second edit field "/pin1/ millis"
`,
glob: `
BlynkTimer timer;
void sendSeconds() {
Blynk.virtualWrite(V0, millis() / 1000);
}
void sendMillis() {
Blynk.virtualWrite(V1, millis());
}
`,
init: `
// Setup a function to be called every second
timer.setInterval(1000L, sendSeconds);
// Setup a function to be called every second
timer.setInterval(1000L, sendMillis);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/LCD/LCD_SimpleModeReading" : {
comment : `
Output any data on LCD widget!
App project setup:
LCD widget, SIMPLE mode, in widget settings :
- Select pin V0 for zero pin
- Select pin V1 for first pin
- Leave "Reading Frequency" on "1 sec" interval
- Type into first edit field "/pin0/ seconds"
- Type into second edit field "/pin1/ millis"
`,
glob: `
BLYNK_READ(V0) {
Blynk.virtualWrite(V0, millis() / 1000);
}
BLYNK_READ(V1) {
Blynk.virtualWrite(V1, millis());
}
`,
},
/***********************************************/
"Widgets/LCD/LCD_AdvancedMode" : {
comment : `
Output any data on LCD widget!
App project setup:
LCD widget, switch to ADVANCED mode, select pin V1
`,
glob: `
WidgetLCD lcd(V1);
`,
init: `
lcd.clear(); //Use it to clear the LCD Widget
lcd.print(4, 0, "Hello"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
lcd.print(4, 1, "World");
// Please use timed events when LCD printintg in void loop to avoid sending too many commands
// It will cause a FLOOD Error, and connection will be dropped
`
},
/***********************************************/
"Widgets/LED/LED_Blink" : {
comment : `
Blynk using a LED widget on your phone!
App project setup:
LED widget on V1
`,
glob: `
WidgetLED led1(V1);
BlynkTimer timer;
// V1 LED Widget is blinking
void blinkLedWidget()
{
if (led1.getValue()) {
led1.off();
<%= serial_dbg %>.println("LED on V1: off");
} else {
led1.on();
<%= serial_dbg %>.println("LED on V1: on");
}
}
`,
init: `
timer.setInterval(1000L, blinkLedWidget);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/LED/LED_Color" : {
comment: `
Blynk using a LED widget on your phone!
App project setup:
LED widget on V1
`,
glob: `
WidgetLED led1(V1);
BlynkTimer timer;
bool ledStatus = false;
#define BLYNK_GREEN "#23C48E"
#define BLYNK_BLUE "#04C0F8"
#define BLYNK_YELLOW "#ED9D00"
#define BLYNK_RED "#D3435C"
#define BLYNK_DARK_BLUE "#5F7CD8"
// V1 LED Widget is blinking
void blinkLedWidget()
{
if (ledStatus) {
led1.setColor(BLYNK_RED);
<%= serial_dbg %>.println("LED on V1: red");
ledStatus = false;
} else {
led1.setColor(BLYNK_GREEN);
<%= serial_dbg %>.println("LED on V1: green");
ledStatus = true;
}
}
`, init: `
// Turn LED on, so colors are visible
led1.on();
// Setup periodic color change
timer.setInterval(1000L, blinkLedWidget);
`, loop: `
timer.run();
`
},
/***********************************************/
"Widgets/LED/LED_Fade" : {
comment : `
Fade using a LED widget on your phone!
App project setup:
LED widget on V2
`,
glob: `
WidgetLED led2(V2);
BlynkTimer timer;
// V2 LED Widget is fading
void fadeLedWidget()
{
static int value = 0;
static int delta = 30;
value += delta;
if (value > 255 || value < 0) {
delta = -delta;
} else {
<%= serial_dbg %>.print("LED on V2: ");
<%= serial_dbg %>.println(value);
led2.setValue(value);
}
}
`,
init: `
timer.setInterval(300L, fadeLedWidget);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/LED/LED_StatusOfButton" : {
comment : `
Blynk using a LED widget on your phone!
App project setup:
LED widget on V3
`,
glob: `
// Select your pin with physical button
const int btnPin = 1;
WidgetLED led3(V3);
BlynkTimer timer;
// V3 LED Widget represents the physical button state
boolean btnState = false;
void buttonLedWidget()
{
// Read button
boolean isPressed = (digitalRead(btnPin) == LOW);
// If state has changed...
if (isPressed != btnState) {
if (isPressed) {
led3.on();
} else {
led3.off();
}
btnState = isPressed;
}
}
`,
init: `
// Setup physical button pin (active low)
pinMode(btnPin, INPUT_PULLUP);
timer.setInterval(500L, buttonLedWidget);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/Map" : {
comment : `
Output any data on Map widget!
App project setup:
Map widget on V1
`,
glob: `
WidgetMap myMap(V1);
`,
init: `
// If you want to remove all points:
//myMap.clear();
int index = 0;
float lat = 51.5074;
float lon = 0.1278;
myMap.location(index, lat, lon, "value");
`,
},
/***********************************************/
"Widgets/Menu" : {
comment : `
This example shows how to use the Menu Widget.
App project setup:
Menu widget attached to V1 (put 3 items in it)
`,
glob: `
BLYNK_WRITE(V1) {
switch (param.asInt())
{
case 1: // Item 1
<%= serial_dbg %>.println("Item 1 selected");
break;
case 2: // Item 2
<%= serial_dbg %>.println("Item 2 selected");
break;
case 3: // Item 3
<%= serial_dbg %>.println("Item 3 selected");
break;
default:
<%= serial_dbg %>.println("Unknown item selected");
}
}
`,
},
/***********************************************/
"Widgets/Player" : {
comment : `
This example shows how you can process commands from player widget
App project setup:
Player widget attached to V5 and running project.
`,
glob: `
BLYNK_WRITE(V5)
{
String action = param.asStr();
if (action == "play") {
// Do something
}
if (action == "stop") {
// Do something
}
if (action == "next") {
// Do something
}
if (action == "prev") {
// Do something
}
Blynk.setProperty(V5, "label", action);
<%= serial_dbg %>.print(action);
<%= serial_dbg %>.println();
}
`,
},
/***********************************************/
"Widgets/PushNotification/PushNotification_Button" : {
comment : `
Simple push notification example
App project setup:
Push widget
Connect a button to pin 2 and GND...
Pressing this button will also push a message! ;)
`,
glob: `
void notifyOnButtonPress()
{
// Invert state, since button is "Active LOW"
int isButtonPressed = !digitalRead(2);
if (isButtonPressed) {
<%= serial_dbg %>.println("Button is pressed.");
// Note:
// We allow 1 notification per 5 seconds for now.
Blynk.notify("Yaaay... button is pressed!");
// You can also use {DEVICE_NAME} placeholder for device name,
// that will be replaced by your device name on the server side.
//Blynk.notify("Yaaay... {DEVICE_NAME} button is pressed!");
}
}
`,
init: `
// Setup notification button on pin 2
pinMode(2, INPUT_PULLUP);
// Attach pin 2 interrupt to our handler
attachInterrupt(digitalPinToInterrupt(2), notifyOnButtonPress, CHANGE);
`,
},
/***********************************************/
"Widgets/PushNotification/PushNotification_Interval" : {
comment : `
Simple push notification example
App project setup:
Push widget
Connect a button to pin 2 and GND...
Pressing this button will also push a message! ;)
`,
glob: `
BlynkTimer timer;
void notifyUptime()
{
long uptime = millis() / 60000L;
// Actually send the message.
// Note:
// We allow 1 notification per 5 seconds for now.
Blynk.notify(String("Running for ") + uptime + " minutes.");
// You can also use {DEVICE_NAME} placeholder for device name,
// that will be replaced by your device name on the server side.
// Blynk.notify(String("{DEVICE_NAME} running for ") + uptime + " minutes.");
}
`,
init: `
// Notify immediately on startup
Blynk.notify("Device started");
// Setup a function to be called every minute
timer.setInterval(60000L, notifyUptime);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/RTC" : {
comment : `
Blynk can provide your device with time data, like an RTC.
Please note that the accuracy of this method is up to several seconds.
App project setup:
RTC widget (no pin required)
Value Display widget on V1
Value Display widget on V2
WARNING :
For this example you'll need Time keeping library:
https://github.com/PaulStoffregen/Time
This code is based on an example from the Time library:
https://github.com/PaulStoffregen/Time/blob/master/examples/TimeSerial/TimeSerial.ino
`,
inc: `
#include <TimeLib.h>
#include <WidgetRTC.h>
`,
glob: `
BlynkTimer timer;
WidgetRTC rtc;
// Digital clock display of the time
void clockDisplay()
{
// You can call hour(), minute(), ... at any time
// Please see Time library examples for details
String currentTime = String(hour()) + ":" + minute() + ":" + second();
String currentDate = String(day()) + " " + month() + " " + year();
<%= serial_dbg %>.print("Current time: ");
<%= serial_dbg %>.print(currentTime);
<%= serial_dbg %>.print(" ");
<%= serial_dbg %>.print(currentDate);
<%= serial_dbg %>.println();
// Send time to the App
Blynk.virtualWrite(V1, currentTime);
// Send date to the App
Blynk.virtualWrite(V2, currentDate);
}
BLYNK_CONNECTED() {
// Synchronize time on connection
rtc.begin();
}
`,
init: `
// Other Time library functions can be used, like:
// timeStatus(), setSyncInterval(interval)...
// Read more: http://www.pjrc.com/teensy/td_libs_Time.html
setSyncInterval(10 * 60); // Sync interval in seconds (10 minutes)
// Display digital clock every 10 seconds
timer.setInterval(10000L, clockDisplay);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/RTC_Advanced" : {
comment : `
Blynk can provide your device with time data, like an RTC.
Please note that the accuracy of this method is up to several seconds.
App project setup:
RTC widget (no pin required)
`,
glob: `
BlynkTimer timer;
void requestTime() {
Blynk.sendInternal("rtc", "sync");
}
BLYNK_WRITE(InternalPinRTC) {
long t = param.asLong();
<%= serial_dbg %>.print("Unix time: ");
<%= serial_dbg %>.print(t);
<%= serial_dbg %>.println();
}
`,
init: `
timer.setInterval(10000L, requestTime);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/Table/Table_Simple" : {
comment : `
You can use Table widget for logging events
App project setup:
Default Table widget on V1
`,
glob: `
BlynkTimer timer;
int rowIndex = 0;
void sendEvent() {
// adding 1 row to table every second
Blynk.virtualWrite(V1, "add", rowIndex, "My Event", millis() / 1000);
//highlighting latest added row in table
Blynk.virtualWrite(V1, "pick", rowIndex);
rowIndex++;
}
`,
init: `
//clean table at start
Blynk.virtualWrite(V1, "clr");
//run sendEvent method every second
timer.setInterval(1000L, sendEvent);
`,
loop: `
timer.run();
`
},
/***********************************************/
"Widgets/Table/Table_Advanced" : {
comment : `
Use Table widget to display simple value tables or events
App project setup:
Table widget on V1
Button widget (push) on V10
Button widget (push) on V11
`,
glob: `
WidgetTable table;
BLYNK_ATTACH_WIDGET(table, V1);
int rowIndex = 0;
// Button on V10 adds new items
BLYNK_WRITE(V10) {
if (param.asInt()) {
table.addRow(rowIndex, "Test row", millis() / 1000);
table.pickRow(rowIndex);
rowIndex++;
}
}