LogoLogo
GitHubBlynk WebsiteLogin to Blynk.Console
  • Introduction
  • Getting Started
    • Supported Hardware
    • Quickstart
      • Next Steps After Quickstart
      • Quickstart Device: Code Overview
      • Troubleshooting
    • Device Activation Methods
      • Manual Device Activation
      • WiFi provisioning
      • Static Token
    • Template Quick Setup
      • Set Up Datastreams
      • Set Up Web Dashboard
      • Set Up Mobile App Dashboard
      • Prepare Your Code
      • Test your Template
    • Send Data From Hardware To Blynk
    • Control Devices (GPIOs and beyond)
    • Events
    • Notifications (Alerts)
    • Sign Up / Sign In
  • General Concepts
    • Developer Mode
    • Device
    • Device Template
    • Connection Lifecycle
      • Disconnections And Heartbeat
    • Users
      • Multi-tenancy
    • Organizations
    • Automations
      • Forward Device Data
  • Message Usage
  • Integration Guides
    • Node-RED
    • The Things Stack
      • Getting Started
      • Device Grouping
      • Automated Device Onboarding
      • System DataStreams
    • Blues
    • NCD Industrial Vibration Sensor
    • Particle - monitor with Blynk
    • Particle - control with Blynk
    • AWS IoT Core
  • Myriota
  • OpenWeather
  • Blynk.Console
    • Overview
    • Dashboards
    • Devices
      • Device profile
        • Dashboard
        • Device Info & Metadata
        • Notifications & Events
        • Developer tools
          • General
          • Datastreams
          • Testing
          • Actions Log
      • Actions with devices
      • Segments
      • Filters
      • Notifications Settings
      • Device Sharing
    • Locations
      • Location Profile
      • Assigning Devices to the Locations
    • Organizations
      • Create a Sub-Organization
      • Working with Sub-Organizations
    • Users
      • User Profile
    • Device Templates
      • Working With Templates
      • Info
        • Offline Ignore Period
        • Manufacturer
        • Template ID
        • Categories
        • Hotspot Prefix
      • Datastreams
        • Datastream Settings
          • Name
          • Alias
          • Virtual Pin
          • Color
          • Data Type
          • Min Value
          • Max Value
          • Default Value
          • Save RAW Data
          • Invalidate Value
          • Wait for confirmation from device
          • Sync with latest server value every time device connects to the cloud
          • Expose to Voice Assistants
        • Virtual Pin
        • Location
        • Enumerable
      • Web Dashboard
        • Multiple Dashboard Tabs
      • Metadata
        • Metadata Tutorial
      • Connection Lifecycle
      • Events
        • Custom Events
          • Event Settings
          • How to Send/Log Events
          • Content Events
        • Notifications Settings
          • Custom Sounds and Critical Notifications
      • User Guides
      • Assets
    • Widgets (Console)
      • Switch
      • Slider
      • Number Input
      • Image Button
      • Web Page Image Button
      • LED
      • Label
      • Gauge
      • Chart
      • Map
      • Image Gallery
      • Custom Chart
      • Heatmap Chart
      • Video
      • Terminal
      • Segmented Switch
      • Alarm & Sound Widget
      • Modules
    • Blynk.Air
      • Shipment Details
      • Shipment Management
        • New Shipping
      • Device shipment statuses
      • User-Controlled Shipments
    • Settings
      • Organization Settings
        • General
        • Users
        • Locations (Job Site or Facilities)
        • Tags
      • Roles and Permissions
      • Developers
        • OAuth2
        • Webhooks
        • Create New Webhook
      • Integrations
    • User Profile Menu
    • Limits
  • Blynk.Apps
    • Overview
    • Mobile Dashboard Editor
    • Device Header Constructor
      • Header Design
      • Header Mini Widgets
        • Connection Status Widget
        • Last Reported Widget
        • Tabs Widget
        • Datastream Value Widget
        • Image Widget
        • Battery Level Widget
        • Signal Level Widget
        • Tags Widget
      • Header Buttons
    • Pages
    • Widgets (app)
      • Common Widget Settings
      • List of Datastreams types supported by Widgets
    • Widgets Controllers
      • Button
      • Styled Button
      • Icon Button
      • Image Button
      • Slider
      • Vertical Slider
      • Step Slider
      • Vertical Step Slider
      • Joystick
      • zeRGBa
      • RGB Light Control
      • Step H
      • Step V
      • Slope Control
      • Switch
      • Level Slider
      • Level Slider with Switch
    • Widgets Displays
      • Value Display
      • Labeled Value
      • LED
      • Gauge
      • Radial Gauge
      • Enhanced Gauge
      • LCD
      • Simple Chart
      • SuperChart
      • Terminal
      • Video Stream
      • Level H
      • Level V
      • Image Gallery
      • Gradient Ramp
      • Icon
      • Image Animation
      • Lottie Animation
    • Widgets Interface
      • Tabs
      • Menu
      • Map
      • Text Input
      • Numeric Input
      • Time input
      • Segmented Switch
      • Icon Segmented Switch
      • Text
      • Formatted Text
      • Dynamic Spacer
    • Widgets Other
      • Music Player
      • WebPage Button
      • WebPage Image Button
      • Alias Name
    • Main Menu
      • My Profile
      • Organization
      • Settings
      • Help
      • About
      • Log Out
    • Devices
      • Add New Device
    • Automations
    • Notifications & Events
  • Blynk.Edgent
    • Blynk.Edgent overview
    • Blynk.Inject and Blynk.Air
    • OTA: Firmware Over-The-Air updates
  • Blynk.NCP
    • Blynk.NCP overview
    • Supported Connectivity Modules
    • OTA: Firmware Over-The-Air updates
  • Blynk Library - firmware API
    • Installation
      • Install Blynk Library in Arduino IDE
      • Install Blynk Library for Platformio.org
      • Install ESP8266 core for Arduino IDE
    • Configuration
    • Connection Management
    • Device Online/Offline Status
    • Digital/Analog Pins
    • Virtual Pins
    • Widget Properties
    • State Syncing
    • Timers
    • Time (RTC clock)
    • Timezone / Location
    • Log Event
    • Metadata
    • Debug
    • Reboot
    • Over-The-Air Firmware Updates (OTA)
    • Other
    • Limitations and Recommendations
    • Blynk Protocol
  • BLYNK.CLOUD MQTT API
    • Device MQTT API
      • Authentication
      • Topic Structure
      • Datastreams
      • Widget Properties
      • Events
      • Metadata
      • Timezone/Location
      • OTA
      • Miscelaneous
  • BLYNK.CLOUD HTTPS API
    • Device HTTPS API
      • Get Datastream Value
      • Get Multiple Datastream Values
      • Get Historical Data From Device
      • Update Datastream Value
      • Update Multiple Datastreams Simultaneously
      • Upload a Set of Timestamped Data
      • Update Widget/Datastream Property
      • Send/Log An Event
      • Get Device Metadata Value
      • Update Device Metadata Value
      • Is Device Connected
      • Upload a File
      • HTTPS API Troubleshooting
    • Platform API
      • Authentication
      • Ogranization API
        • Get Own Organization Info
        • Get Organization Info
        • Search Organizations
        • Create Organization
        • Get Static Tokens
        • Get Organization Tags
        • Get Organization Automations
      • Devices API
        • Get All Devices
        • Search Devices
        • Get Devices by Owner Email
        • Get Devices in user organization
        • Get Recently Activated Devices
        • Get Device Info
        • Get Connection Status
        • Create Device
        • Edit Device
        • Get Datastream Values
        • Update Datastream Value
        • Update Multiple Datastreams Values
        • Import Datastream Values
        • Update Datastream Property
        • Get Datastream Historical Data
        • Get Device Metadata
        • Update Device Metadata
        • Get Device Tags
        • Get Device Timeline Log
        • Log a Device Event
        • Get Actions Log
        • Erase All Data
        • Remove Device Owner
        • Transfer Device
        • Delete Device
      • Users API
        • Get All Users
        • Search Users
        • Create New User
        • Invite User
        • Get User Info
        • Update User Role
      • Templates API
        • Get All Templates
        • Get Template Info
        • Get Template Metadata
        • Get Template Datastreams
        • Get Template Events
    • Security
  • Downloads
    • Blynk Mobile Apps
    • Blynk Library
  • Troubleshooting
    • General Issues
    • Developer Mode
    • Changes from Blynk 0.1
      • Migrating to the new Blynk - Full Guide
    • Glossary
    • Links
  • Commercial Use
    • Deploying Products With Dynamic AuthTokens
    • Deploying Products With Static Tokens
    • Working With Clients
    • Supported topologies
    • Business Plan (White Label Solution)
      • App Publishing Process And Timeline
      • What's Needed To Publish Your Apps And Go Live
      • Branding Materials
      • Custom Email Address For Transactional Emails
      • Application Settings
        • General
        • Design
        • Mobile Apps
        • Sign Up
  • Add-Ons
    • Add-on list
    • Amazon Alexa
    • Google Assistant
    • Localization
    • Database Access
    • Marketing
Powered by GitBook
On this page
  • Hardware
  • Firmware
  • Particle API
  • Blynk Datastreams
  • Blynk Device Template
  • Blynk Web Dashboard
  • Blynk Mobile App
  • Blynk Webhook
  • Full System Test
  • Wrap Up

Was this helpful?

  1. Integration Guides

Particle - control with Blynk

How to Control a Particle Device with Blynk

PreviousParticle - monitor with BlynkNextAWS IoT Core

Last updated 8 months ago

Was this helpful?

This guide can be used in combination with the first part, where we were connecting the Particle Boron device and pushing data to Blynk Cloud.

This second part will help you change the state of a digital output on a Particle device built-in LED using either a pushbutton on the Blynk dashboard, or a Button’ widget in the Blynk.App.

Hardware

The Particle device is a cellular LTE Cat M1 device. The built-in LED on the device connected to digital I/O D7 will be utilized.

Firmware

A Particle causes code on a device to run when requested from the . Up to 15 cloud functions may be registered, and each function call request and response uses one Particle Data Operation from your monthly or yearly quota. The function can also be triggered from the Particle , and the . The function returns an integer defined by the function code.

Below is the firmware code for the Boron. Note that the cloud function is first registered in setup() by the call to Particle.function(funcKey, funcName), and then the custom function ‘funcName’ is added to the script. In this example, the funcKey is ‘blynk_led’ and the funcName is ‘blynkLED’.

/*
 Project blynk_to_particle.io
 Author: Mark W Kiehl / Mechatronic Solutions LLC
 Date: April 2023
 
 Respond to a remote Blynk request to turn on or off the
 blue LED on D7. An HTTP POST to the Particle API calls
 a Particle function that responds to an argument with
 a value of 'on' or 'off' corresponding to what should
 happen to the LED. 
 Returns the value 1 if the LED has been turned on, 
 and 0 if turned off, -1 if an error.

 Hardware:
 Particle Boron 404x

 Software:
 Standard Particle firmware and this script.

*/

#include "Particle.h"

const char* firmware_version = "0.0.0";
uint8_t led_state = LOW;
uint8_t led_state_last = LOW;

// Register the Particle cloud function
int blynkLED(String on_or_off);

void setup() {

 pinMode(D7, OUTPUT);
 digitalWrite(D7, HIGH);

 Serial.begin(9600);
 waitFor(Serial.isConnected, 30000);
 delay(1000);
 Serial.printlnf("Device OS v%s", System.version().c_str());
 Serial.printlnf("Free RAM %lu bytes", System.freeMemory());
 Serial.printlnf("Firmware version v%s", firmware_version);

 // register the Particle cloud function (funcKey, funcName)
 Particle.function("blynk_led", blynkLED);

 randomSeed(millis());
 digitalWrite(D7, LOW);
 Serial.println("Setup complete");
} // setup()

void loop() {

 digitalWrite(D7, led_state);

} // loop()

int blynkLED(String on_off) {
 // Custom Particle cloud function that changes the state of the built-in LED
 // on D7 in response to an instruction from Blynk calling this
 // custom cloud function. 
 // Returns the value 1 if the LED has been turned on, and 0 if turned off, 
 // -1 if an unexpected on_off value is received.
 // Cloud functions must return int and take one String argument
 // curl https://api.particle.io/v1/devices/{your 25 char device id}/blynk_led
 // -d access_token={your 40 char access token}
 // -d "args=on/off"
 
 if (on_off == "on" || on_off == "1") {
 led_state_last = led_state;
 led_state = HIGH;
 return 1;
 } else if (on_off == "off" || on_off == "0") {
 led_state_last = led_state;
 led_state = LOW;
 return 0;
 } else {
 Serial.print("Unexpected on_off value of: '"); Serial.print(on_off); Serial.println("'");
 }
 return -1;
}

Particle API

The syntax for the API is:

POST /v1/devices/{DEVICE_ID}/:{FUNCTION}

Where {DEVICE_ID} is your Particle device ID and {FUNCTION} is the cloud function name.

Example:

https://api.particle.io/v1/devices/{your 25 char device id}/blynk_led

Configure the POST body message type as application/x-www-form-urlencoded with the following key/value pairs:

args on/off

The ‘args’ value should be “on” or “off”, depending on if you want to turn the built-in LED on or off.

For clarity, the full headers that will be sent with the HTTP POST by Postman are shown below. Note the key of ‘Authorization’ and the value of ‘Bearer {40 char Particle access token}’. A space is in between “Bearer” and the 40 character Particle access token. This is known as ‘Bearer Authorization’ or ‘token authentication’ and is an HTTP authentication scheme that involves security tokens called bearer tokens. In this case, the bearer token will be sent in an HTTP ‘Authorization’ header.

{
 "ok": false,
 "error": "Timed out."
}

The above response occurs if your Particle device isn’t online (Status: 400 Bad Request).

A successful execution will appear as follows:

{
 "id": "your 25 char device id",
 "name": "your device name",
 "connected": true,
 "return_value": 1
}

Note that if you add a body key of “format” and value of “raw” then the response will simply be the integer value you configured for your Particle function blynkLED() with a value of -1, 1 or 0.

Now that you are certain that the Particle HTTP POST will be responded to by the Particle device, the next step is to configure a Blynk Webhook that will execute that POST. The Blynk webhook will pass a datastream value in the Particle HTTP POST, so begin by defining the Blynk datastream.

Blynk Datastreams

Blynk Datastreams are bi-directional channels assigned a data type and link to data values stored on the Blynk Cloud. You reference them as virtual pins between the range of V0 and V255. Below are the datastreams defined for this project and their relationship to the hardware and the web dashboard / mobile app.

The Particle function argument is a data type of String. The custom function we created accepts values of ‘on’ or ‘1’ to turn the built-in LED on, and values of ‘off’ or ‘0’ to turn it off. Note below that the enumerable datastream converts 1/0 to “on”/”off”.

Blynk Datastream Definitions:

Virtual Pin
Name
Data Type
Description

V7

LED int

Integer

1 = LED on, 0 = LED off

V8

LED enum

Enumerable

1 = “on” = LED on, 0 = ‘off = LED off

V9

LED str

String

“on” = LED on, “off” = LED off

Blynk Device Template

Using the Blynk Datastream Definitions listed in the table, create or edit a device template named ‘BRN404X’ as shown below.

Three datastreams of data types integer, enumerable, and string will be created to illustrate how each can be used to control the LED on the Particle Boron. In a real application, you would only need to employ one of these.

Click on the ‘Templates->Datastreams’ tab and configure the datastreams as defined in the Blynk Datastream Definitions table with the options as shown below. It is important to configure the Pin, Data Type, Is Raw, Min, Max, and Default Value as shown.

Blynk Web Dashboard

Create a web dashboard by navigating to ‘Templates->[template name = BRN404X]’ and then click on the ‘Web Dashboard’ tab. Create a dashboard as shown below, using a switch widget for virtual pins V7 and V8, and a text input widget for the string datastream V9. The label widgets to the right of each switch / text input widget are not required and are only to make it easy for you to see the latest datastream value.

Blynk Mobile App

Create a Blynk mobile device dashboard. The exact steps may vary between iOS and Android, but begin by tapping on a device, and tap on the wrench icon and then the ‘+’ icon to add a widget. Create the following widgets:

  1. ‘Switch’ widget for datastream ‘LED int (V7)’.

  2. ‘Segmented Switch’ widget for datastream ‘LED enum (V8)’.

  3. ‘Text Input’ widget for datastream ‘LED str (V9)’.

Blynk Device Activation

Blynk Webhook

After you are finished configuring each webhook, click the ‘Test webhook’ to verify it doesn’t throw an error (it won’t send the datastream value here, so don’t expect to see the LED on your Particle device change). Click the ‘Create Webhook’ button to save it and close the dialog.

Note that request quota is ‘1 per minute’ so any datastream value changes faster than one minute will be ignored (webhook won’t execute).

Full System Test

Everything is now configured to push data from either the Blynk web dashboard widgets or mobile app widgets to the associated Blynk webhook. The webhook will execute a HTTP POST to Particle’s API, and call the Particle function running on the Particle device.

Make sure your Particle device is running and connected (cyan LED breathing). From the Blynk web dashboard, send a command from one of the widgets to turn on the Particle device LED. Wait 60 seconds, and then use the same widget to turn the LED off. The label widgets on the web dashboard will display the actual datastream values. Repeat the same tests from the Blynk mobile app.

Wrap Up

This article showed how to securely control a Particle device from either a Blynk web dashboard or mobile app. No code was needed to create the Blynk web dashboard, mobile app, or Blynk webhook. Three different types of Blynk datastreams were employed in this example, demonstrating how different web dashboard and mobile app widgets can be used to accomplish the same type of remote control. Although only a state change of either on or off was used, it is easy to see how a few changes could be made to cause a variable change at the device, such as changing the LED brightness or color.

Upload the firmware code to the Boron and then continue to power it so it can connect to the Particle cloud. Go to your Particle , select the Boron device, and then under the section ‘FUNCTIONS’ on the right side of the screen you will see the function key of ‘blynk_led’ listed. Enter ‘on’ in the ‘Argument’ input area and click the ‘CALL’ button. Observe the Boron to confirm that the built-in blue LED on D7 turns on. Repeat, this time with the ‘off’ argument to turn off the LED.

Now that we have confirmed that the Particle function works properly on the Boron, lets execute it using the Particle API. Create an access token by logging into your Particle account, and then visiting the Particle documentation section ‘’. Use your Particle login email and password. If you have MFA (multi-factor authentication) enabled on your account, you will need your MFA code to generate the access token. Click the ‘Create token’ button to generate a token. Keep this token confidential.

Configure an HTTPs POST with OAuth 2.0 authorization, a webform with the key/values of “args” and “on” using a tool such as the free online tool , as shown in the images that follow. First select ‘POST’, enter the URL, select the ‘Authorization’ tab and set the ‘Type’ to ‘OAuth 2.0’. Select the ‘Request Headers’ option and then enter your 40 character Particle access token into the token field, and then the text “Bearer” for the ‘Header Prefix”.

With your device powered and connected, manually send a POST using or with the ‘args’ value of either ‘on’ or ‘off’. If your response is unsuccessful, the response will be:

If you already followed the steps from the prior article “”, then edit the same device template and add the datastreams V7, V8, V9 as shown below.

If you already followed the steps from the prior article “”, then add a new dashboard tab by clicking the ‘+’ button before following the steps below.

If you already followed the steps from the prior article “, then edit the same device template ‘boronc’ and add the three widgets for datastreams V7, V8, V9 as shown below.

Skip this step if you already followed the steps from the prior article “”. If you didn’t, then go to that article and follow the steps under .

Navigate to ‘Settings -> Webhooks’ and create three new , one each for the datastreams V7, V8, and V9, based on the information shown in the images below for V7, but substituting your Particle device ID and access token. The only difference between the three webhooks is the assignment of the V7, V8, and V9 datastream to the webhook, and the webhook name.

By combining what was demonstrated in this article and the prior “”, you can achieve bidirectional communication between a Particle device and both a Blynk web dashboard and mobile app. Additionally, Blynk provides a wide variety of widgets for visualizing data from a Particle device, and no-code Blynk automations can be configured to create a number of custom notifications.

Boron 404x
cloud function
Particle cloud API
console
CLI
console
Create a token (browser-based)
Postman
Postman
CURL
How to connect a Particle device to Blynk
How to connect a Particle device to Blynk
How to connect a Particle device to Blynk”
webhooks
How to connect a Particle device to Blynk
How to connect a Particle device to Blynk
‘Blynk Device Activation’