2. Get Data from Blynk

You will learn how to control the circuit with Blynk in this chapter. Let’s light up the LEDs over the Internet!

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

3 in 1 Starter Kit

380+

3 in 1 Starter Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

SunFounder R3 Board

BUY

Breadboard

BUY

ESP8266 Module

BUY

Jumper Wires

BUY

Resistor

BUY

LED

BUY

1. Build the Cirduit

Note

The ESP8266 module requires a high current to provide a stable operating environment, so make sure the 9V battery is plugged in.

../_images/wiring_led.jpg

2. Edit Dashboard

  1. Go to the Quickstart Device you created earlier, click on the edit dashboard icon.

    ../_images/blynk_edit_dashboard.png
  2. Datastreams allow the widgets on Blynk and the code on the R3 board to recognize each other.

    ../_images/blynk_edit_datastream.png
  3. To experience the complete configuration process, remove all Datastreams from the Datastreams page.

    ../_images/blynk_edit_datastream_delete.png
  4. Create a Datastream of type Virtual Pin, which will be used to control the LED using Blynk’s switch.

    ../_images/blynk_edit_virtualpin.png
  5. Configure the Virtual Pin. As the button and LED only need to be ON and OFF, set DATA TYPE to Integer and MIN and MAX to 0 and 1.

    ../_images/sp220609_115520.png
  6. Go to the Web Dashboard page.

    ../_images/blynk_edit_web_dashboard.png
  7. And delete the existing widgets.

    ../_images/blynk_edit_delete_dashboard.png
  8. Drag and drop a switch widget from the Widget Box on the left.

    ../_images/blynk_edit_drag_switch_widget.png
  9. Now to set it up.

    ../_images/blynk_edit_edit_widget.png
  10. Select Datastream as the one you set earlier.

    ../_images/sp220609_133741.png
  11. After selecting Datastream, you will see a few custom settings, then press Save.

    ../_images/sp220609_133950.png
  12. Finally, click Save And Apply.

    ../_images/sp220609_141733.png

3. Run the Code

  1. Open the 2.get_data_from_blynk.ino file under the path of 3in1-kit\iot_project\2.get_data_from_blynk, or copy this code into Arduino IDE.

  2. Replace the Template ID, Device Name, and Auth Token with your own. You also need to enter the ssid and password of the WiFi you are using. For detailed tutorials, please refer to 1.3 Connecting the R3 board to Blynk.

  3. After selecting the correct board and port, click the Upoad button.

    ../_images/2_upload.png
  4. Open the Serial monitor(set baudrate to 115200) and wait for a prompt such as a successful connection to appear.

    ../_images/2_ready.png

    Note

    If the message ESP is not responding appears when you connect, please follow these steps.

    • Make sure the 9V battery is plugged in.

    • Reset the ESP8266 module by connecting the pin RST to GND for 1 second, then unplug it.

    • Press the reset button on the R3 board.

    Sometimes, you may need to repeat the above operation 3-5 times, please be patient.

  5. Back at Blynk, you can see that the status has changed to online and you can now use the switch widget on blynk to control the LED connected to the R3 board.

    ../_images/blynk_button_on.png
  6. If you want to use Blynk on mobile devices, please refer to How to use Blynk on mobile device?.

How it works?

The difference between the code in this project and the code in the previous chapter 1.3 Connecting the R3 board to Blynk is the following lines.

const int ledPin=6;

BLYNK_WRITE(V0)
{
    int pinValue = param.asInt(); // assigning incoming value from pin V0 to a variable
    // You can also use:
    // String i = param.asStr();
    // double d = param.asDouble();
    digitalWrite(ledPin,pinValue);
}

void setup()
{
    pinMode(ledPin,OUTPUT);
}

Regarding the pinMode and digitalWrite of the ledPin, I’m sure you’re already familiar with them, so I won’t go over them again. What you need to focus on is the BLYNK_WRITE(V0) function.

What it will do is that when the value of Blynk’s V0 changes, Blynk.Cloud will tell your device “I am writing to Virtual Pin V0”, and your device will be able to perform something once it gets this information.

We created the V0 Datastream in the previous step and applied it to the Switch Widget. This means that every time we operate the Switch Widget, BLYNK_WRITE(V0) will be triggered.

We write two instructions in this function.

int pinValue = param.asInt();

Get the value of V0 and assign it to the variable pinValue.

digitalWrite(ledPin,pinValue);

Write the value of V0 obtained to the ledPin, so that the Switch widget on Blynk can control the LED.