12. Chapter 9: Upload Data and Get Controlled by Blynk¶
Blynk is a powerful IoT (Internet of Things) platform that allows users to build and control connected devices with ease. It provides a user-friendly mobile and web dashboard that lets developers and hobbyists remotely monitor and manage microcontrollers, sensors, and actuators.

12.1. Session 1: Upload Data to Blynk¶
Goal:
Upload sensor values to Blynk and display them using Gauges on the Blynk platform. Description:
In this example, there are 2 parts involved. Goal:
Create a device on Blynk and obtain its token. Step 1: Create an Account
Go to https://blynk.io/ Register an account and log in to the platform according to the website’s instructions. Step 2: Create a Template
Go to Developer Zone > My Templates, and click “+ New Template”. Input name, hardware and connection type accordingly. Click “Done” when finished. Name: iotbit Hardware: Other Connection type: WiFi Step 3: Configure the Datastream - Add V0
Go to Datastreams, click “+ New Datastream” and select “Virtual Pin”. Change the configuration of the Pin accordingly. Click “Create” when finished. Name: Sensor Value Max: 100 Step 4: Configure the Web Dashboard
Go to Web Dashboard. Drag a Gauge to the Dashboard area. Hover over the Gauge, click the button shown below to change its settings Choose Sensor Value (V0) as the Datastream. Click “Save” when finished. Step 5: Disable Notification Upon Interaction
Since the device will always be offline, a notification will be triggered with every interaction. By disabling this notification, we can prevent alerts about the offline status when interacting with the device. Go to Connection Lifecycle. Scroll down to Offline and click Settings. Disable “Display notification upon user interaction”. Click “Save” to save the entire template. Step 6: Create a Device With the Template
Go to Devices. Click “+ New Device”. Click “From template” and select the template we just created. Rename the device as “iotbit 1” at Device Name. Click “Create” when finished. Go to the Devices page and copy the token as shown below. Keep the copied token in a safe place for future use. Step 7 (Optional): Configure the Mobile Dashboard
Open the Blynk App and log in to your account. After logging in, you will find the device you just created on the page. Switch to Developer Mode by clicking the button at the top right-hand side. Click on the template you just created. Click “+” and choose “Gauge”. A Gauge will be shown on the dashboard automatically. Click the newly created Gauge and choose Sensor Value (v0) as its datastream. Goal:
Program the micro:bit to send sensor values to Blynk. Step 1: Connect to WiFi
Initialize IoT:bit to connect to a designated WiFi network and display the connection status when successful. You may refer to the first chapter for instructions on how to connect to WiFi. Snap “initialize OLED with width 128 height 64” to “on start”. Step 2: Send Data to Blynk
For demonstration, a random number is sent to Blynk every 1 second. In “forever”, use if-statement to check WiFi connection status. Snap the “Send Blynk token…..” block inside the if-statement. Copy the token from Blynk and fill it into “Send Blynk token”. Snap “pick random 0 to 10” to V0 value inside “Send Blynk token”. Change the range to “0 to 100”. Snap a pause to the loop. Set the pause to be 1000 ms (1 second). Step 3: Show Blynk Upload Status
Get the “On Blynk Uploaded” handler from IoT:bit > IoT Services. Use OLED to display the upload status and error code. Full Solution
If the upload is successful, it will show OK as the status with error code 0. Mobile Dashboard: If the upload fails, it will show FAIL as the status with the respective error code which represents the reason of failure. Error code 400: The user inputted the wrong token. Error code -28674: There is no internet connection.
In Part 1, we will create a device on Blynk and obtain its token.
In Part 2, we will program the micro:bit to send sensor values to Blynk.12.1.1. Part 1: Blynk Configuration¶











12.1.2. Part 2: Programming (MakeCode)¶



12.1.3. Result¶
On Blynk, you will see the value of the Gauge keep changing randomly, which is controlled by the data sent from your Micro:bit.

Web Dashboard:



12.2. Session 2: Get Controlled by Blynk¶
Goal:
Create a switch on Blynk to control the output of the micro:bit.
Description:
In this example, there are 2 parts involved. Goal:
Create a switch on Blynk and configure it to send data to the micro:bit. Step 1: Use Existing Template
We will continue to use the template created in section 9.1.1. Go to Developer Zone > My Templates. Choose the template we just created. Otherwise, create a new template following the first half chapter of step 1 and 2. Step 2: Update the Datastream - Add V1
Go to Datastreams and click “Edit”. Click “+ New Datastream” and select “Virtual Pin”. Change the name of the pin to “on_off switch”. Click “Create” when finished. Step 3: Add a Switch to the Web Dashboard
Go to Web Dashboard, drag a Switch to the Dashboard area. Hover over the Switch and go into its settings. Choose on_off switch (V1) as the Datastream. Click “Save” when finished. Click “Save And Apply” to save the entire template. Step 4 (Optional): Configure the Mobile Dashboard
Open the Blynk App. Switch to Developer Mode by clicking the button at the top right-hand side. Click on the template you just updated. Click “+” and choose “Switch”. A Switch will be shown on the dashboard automatically. Click the newly created Switch and choose on_off switch (v1) as its datastream. Goal:
Program the micro:bit to respond to input from Blynk. Step 1: Connect to WiFi
Initialize IoT:bit to connect to a designated WiFi network. You may refer to the first chapter for instructions on how to connect to WiFi. Snap “initialize OLED with width 128 height 64” to “on start”.
Step 2: Read Data from Blynk
In “forever”, use if-statement to check WiFi connection status. Snap the “read Blynk token…..” block inside the if-statement. Copy the token from Blynk and fill it into “read Blynk token”. Choose V1 for “read Blynk token…..”. Snap a pause to the loop. Set the pause to be 1000 ms (1 second). Step 3: Display the Data Read
Get the “On Blynk Readed” handler from IoT:bit > IoT Services. Create a variable to store the value read from Blynk. Use OLED to display the read pin and its value. Show the respective icon according to the read data. Full Solution
MakeCode: https://makecode.microbit.org/_CfDHut4T7Eif We can change the value of the read pin using the switch on Blynk to check if the micro:bit can correctly receive the data. When the Switch in on Web Dashboard: When the Switch is off
In Part 1, we will create a switch on Blynk and configure it to send data to the micro:bit.
In Part 2, we will program the micro:bit to respond to input from Blynk. 12.2.1. Part 1: Blynk Configuration¶






12.2.2. Part 2: Programming (MakeCode)¶



You could also download the program from the following website:12.2.3. Result¶


Mobile Dashboard:


Web Dashboard:

Mobile Dashboard:
