The Internet of Things: Getting Started with Blynk
If you have ever wondered how to get started in the IoT (Internet of Things) world, this is the tutorial for you! According to Statista, the amount of internet-connected devices is expected to nearly triple from 26.6 billion today to 76.4 billion in 2025. Rapid advances in hardware and software allow even the tiniest of devices to communicate with each other over the internet. That's where Blynk comes in!
MATERIALS
HARDWARE:
- TinyScreen+
- MicroUSB Cable
- WiFi TinyShield (highly recommended)
- Temperature Humidity TinyShield (optional)
SOFTWARE:
An Introduction to Blynk
Blynk is a popular IoT (internet of things) app that allows you to control TinyCircuits hardware, collect data from sensors, create custom visual dashboards, and automatically save data in the Blynk cloud. Another ability of Blynk allows you to send push notifications or tweets based on custom conditions being met. Most importantly, Blynk is very easy to use for beginners and features a drag and drop interface comprised of various widgets. In this tutorial, we will walk you through a simple IoT project. Then, we will discuss some of the more advanced widgets and describe how to utilize them in your projects!
Step 1: Installing and using Blynk
Blynk is available on iOS or Android. Once you have installed Blynk and created an account, you will be able to start your first project.
- First, tap on the New Project button.
- Let's create a simple project called VirtualLight. In the choose device menu, select the Arduino Zero. Make sure the connection type is set to Wi-Fi. If you don't have the Wi-Fi TinyShield, please refer to the troubleshooting section at the end of the article for tips on establishing a USB connection instead.
- Once you have created your project, Blynk will email you a special authentication token. You will need this later!
- At this point, you should see a blank project canvas. By tapping anywhere on the canvas, you will be able to add your very first widget!
- For this simple example, scroll down and tap on the LED widget. It should appear on your project canvas.
- Tap on the LED widget you just placed to edit its settings.
Regardless of which widget you create, you must always assign a pin by tapping on PIN under the input section. Think of a pin as a special channel reserved for that widget. That way, when you send a signal from your TinyCircuits processor, Blynk knows what widget you are trying to communicate with. For now, we will use a virtual pin, but keep in mind that there are other types of pins which could come in handy for more advanced projects.
- Select pin "V0" in the pin selection window. You can also change the name or color of the LED if you wish.
That's it! Your Blynk setup is complete. Move on to the next step to learn how to set up a communication link between Blynk and your TinyCircuits processor over the internet.
Step 2: Enabling your first IoT device
- If this is your first time using TinyCircuits products, refer to the TinyScreen+ Setup Tutorial to configure the Arduino IDE correctly.
- Install the Blynk library for Arduino.
- Download the Blynk Widget Code Example from the software section above.
- Open the code example in the Arduino IDE and insert your auth token from step 1 between the quotation marks found on line 29 (also shown below.)
char auth[] = "YOUR_AUTH_TOKEN";
Before you upload the code, let's explore how Blynk commands are used in Arduino sketches.
First, on line 38, a WidgetLED object named led1 is created. Notice that the object takes a parameter that specifies which pin it will be communicating over.
WidgetLED led1(V0);
Then, on line 49, we see a call to Blynk.begin that requires our WiFi credentials and auth token. The Blynk library uses this information to connect to the internet and establish a link with the Blynk server.
- Make sure that you provide your WiFi credentials on lines 33 and 34.
Now that we have connected, Blynk is waiting for any signals over pin V0. Looking at our void loop() function, you can see that when the upper left button is pressed on the TinyScreen+, we use a led1.getValue() to read the current state of the virtualLED from the Blynk server.
Based on the current state, we use the led1.setValue() function to toggle the state. Most widgets will have their own functions that allow easy interaction. If you are curious about how to use a specific widget, Blynk provides a library of example sketches. Another fantastic resource is the Blynk documentation.
Make sure that you select Arduino Zero as your Board and Arduino WiFi Shield 101 as your connection type when viewing examples.
Now that you have a basic understanding of Blynk, let's explore some more advanced functionality!
Step 3: Advanced Blynk Features
In the previous example, we specified the virtual pin when we declared the widgetLED object. However, when sending multiple datastreams to some of the more advanced Blynk widgets, we use a different function, Blynk.virtualWrite(pin, data).
One of the most common mistakes when using Blynk.virtualWrite() is placing it within your void loop() and calling it hundreds of times per second. This will place stress on the Blynk server and temporarily disable your project. In order to avoid this, we can use another important function, the BlynkTimer.
Let's say that we wanted to use the SuperChart widget to display live and historical data from an IoT weather station. Perhaps you want to send the temperature and humidity every 10 seconds, but you will send the average wind speed over a 60 second interval.
- Please download the Blynk Timer Code Example from the software section above to follow along.
To use BlynkTimer, create a BlynkTimer object:
BlynkTimer timer;
Then, within void setup(), call timer.setInterval(time,function);
In this case, time represents time in milliseconds and function represents the function that you wish to call when the timer is activated.
So, for our weather station, it would look like this:
// Update the temp and humidity every 10 seconds timer.setInterval(10000L, myWeatherEvent); // update the windSpeed every 60 seconds timer.setInterval(60000L, myWindEvent);
Now let's write myWeatherEvent and myWindEvent using the new Blynk.virtualWrite() function:
void myWeatherEvent() { Blynk.virtualWrite(V1, temp); // sends temp data via pin V1 Blynk.virtualWrite(V2, humidity); // sends humidity data via V2 generateWeatherData(); // generate random data for this example SerialUSB.println("weather data updated!"); // notify user }
void myWindEvent() { Blynk.virtualWrite(V3,windSpeed); sends wind data via V3 generateWindData(); SerialUSB.println("wind data updated!"); }
After setting up the SuperChart widget within the Blynk app, your results will look like this:
Let's say you would like to have both a graphical display and a live text readout. All you would need to do is add some value display widgets:
With your newfound knowledge of Blynk.virtualWrite() and Blynk timers, you should be able to pull off just about any IoT project using Blynk and TinyCircuits hardware!
If you run into any issues, check out the troubleshooting section below, Blynk documentation, Blynk community forum, and the Blynk examples. If you have any technical questions about using our hardware with Blynk, please shoot us an email at info@tinycircuits.com for support.
Troubleshooting
I have connected my WiFi TinyShield, but I receive this error message during void setup(): "[43862] WiFi module not found."- Make sure that you have included the Wire and BlynkSimpleWiFiShield101 libraries at the beginning of your program:
#include <Wire.h>
#include <BlynkSimpleWiFiShield101.h>
- Also, before calling Blynk.begin(), you must set the correct WiFi pins using this statement: WiFi.setPins(8, 2, A3, -1);
- First, try inserting these two debug statements in the first two lines of your sketch. If they are not on literal lines 1 and 2, they will not work.
- Second, you can examine and modify the source code of the Blynk library, which is found in \Documents\Arduino\libraries\Blynk.
- Yes, Blynk also supports other connection types, including a direct USB connection, as described here.
- Yes, Blynk is compatible with the BLE Nordic TinyShield based on the Nordic NRF8001. At this time, Blynk does not support the BLE TinyShield ST, so we will still be able to get you a Nordic if you need one.
- Yes, virtual pins work in both directions. Our robotic arm tutorial shows you how to use the Blynk joystick widgets to send movements to the Servo TinyShield via the TinyZero processor. That way, you can control a robotic arm with your phone.
That's all there is to it! We hope this helped you on your way to becoming an IoT master!!
CONTACT US
As always, if you have any questions or feedback, feel free to email us at info@tinycircuits.com. We would love to hear any ideas for improvement or new IoT projects you were inspired to work on!
Show us what you make by tagging @TinyCircuits on Instagram, Twitter, or Facebook so we can feature it!
Thanks for making with us!!