Particle - monitor with Blynk
How to Connect a Particle Device to Blynk
Last updated
How to Connect a Particle Device to Blynk
Last updated
This guide will help you connect a Particle board and set up everything that is needed to:
Monitor 2x digital inputs and notify the user whenever the state of any of them changes.
Notify the user if the device has moved more than 122 m / 400 ft since it was powered on, and the speed in mph.
Monitor 1x analog input and report the value when a digital input or position change event occurs..
Report the last time any data was published by the device.
Check part two of the article in order to learn how to control your Particle device from Blynk app or web console as this is not within the scope of this guide.
The Particle device is a Boron 404x cellular LTE Cat M1 device. A GPS FeatherWing with an external active 28 dB GPS antenna, and a SMA to uFL/u.FL/IPX/IPEX RF adapter cable provides GPS functionality. The Boron and the GPS FeatherWing communicate over the Boron UART pins.
The IoT device (Particle Boron) can communicate with Blynk either using the Blynk library, or via the Blynk HTTPs API. In order to minimize cellular communication between Blynk and the device, the Blynk HTTPs API will be used.
The Particle Boron cellular IoT device will publish a JSON string to the Particle Cloud, referencing a Particle webhook. The webhook reformats the data, and then sends it to the Blynk Cloud via a HTTP GET, updating the Blynk datastreams. Note that the device doesn't run Blynk code, and therefore it will never appear as "online" to Blynk in the Blynk.Console or Blynk.App.
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.
V0
pushbutton _D8
Integer
Count of state changes for the Boron digital input on pin D8
V1
pushbutton_D6
Integer
Count of state changes for the Boron digital input on pin D6
V2
pot_A0
Double
Analog input value of Boron pin A0
V3
position
Location
GPS latitude & longitude
V4
speed
Double
GPS speed [mph]
V5
position_changed
Integer
1 = device position has changed by more than 122 m / 400 ft. 0 = no change in position.
V6
last_publish
String
Last date/time data was published
Using the Blynk Datastream Definitions listed in the table, create a device template named ‘BRN404X’ as shown below.
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.
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.
A chart widget is used for the two datastreams associated with the two digital inputs because the value assigned to the datastream is the count of state changes. Details on the configuration of each widget is shown below.
Create a Blynk mobile app. 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:
‘SuperChart’ widget configured to display both the datastreams of ‘pushbutton _D8’ and ‘pushbutton_D6’ as a bar chart.
‘Slider’ widget for ‘pot_A0 (V2)’
‘Map’ widget for ‘position (V3)’
‘Slider’ widget for ‘speed(V4)’
‘Button’ widget for ‘position_change(V5)’ with mode of ‘Push’
‘Value Display’ for ‘last_publish(V6)’
Static tokens will be used for device authentication because the device is cellular. We will generate a static authentication token (BLYNK_AUTH_TOKEN) using the manual method. Navigate to the search menu and create a new device from the ‘BRN404X’ template with a name that is similar to the name you assigned to your Particle device name (‘boron_b’ in my case), just to keep it simple and consistent.
After you click the ‘Create’ button, a device page will open and the Template ID, Device Name, and AuthToken will be shown in the upper right of the screen.
Write down the Device name you picked, and then the BLYNK_TEMPLATE_NAME and the BLYNK_AUTH_TOKEN shown in the panel because you will need them later.
For this project, we want the Particle webhook (integration) to pass data from the Particle device to Blynk. The Blynk HTTPs API will be used to set multiple datastream values in the Blynk Cloud. The following HTTP GET request will update the Blynk datastreams defined for this project after the {token} is replaced with the BLYNK_AUTH_TOKEN, and {server_address} is replaced with the server shown at the bottom right of your Blynk.Console:
Note that datastream V6 was omitted for now, and the location datastream V3 is first assigned the longitude, and then the latitude, both in decimal degrees, with a comma between them (e.g. V3=-73.8731,40.8414). The alternative Blynk HTTPs API format of ‘V3=-73.8731&V3=40.8414’ cannot be used in the Particle webhook because the duplicate query parameter will not be permitted.
Paste the HTTP GET request into an internet browser to update the datastreams in the Blynk Cloud, and then observe the changes in the Blynk.Console by navigating to ‘Search -> My Devices -> Device -> Dashboard’. The browser will respond with nothing if the HTTP GET request is accepted, or a JSON string with an error if it is not.
Once you have confirmed that the HTTP GET request properly updates the widgets on your Blynk web dashboard, you are then ready to create the Particle webhook / integration.
We are going to create a Particle integration webhook running on the Particle cloud that will accept the data from the Particle.publish() function executing on the device, and transform it into a HTTPs GET that will post data to the Blynk cloud, updating the corresponding Blynk datastream values.
Login to your Particle Console and click on the ‘Integrations’ sidebar option. Click on the ‘NEW INTEGRATION’ shown on the page, and then select the ‘Webhook’ option.
Fill out the Webhook form as shown below. The ‘Event Name’ is what will be called later by your device firmware with the Particle.publish() function. The server address of ‘ny3.blynk.cloud’ for the ‘URL’ field should be replaced with the server address from this list that matches what you defined for your Blynk HTTP GET request. Change the ‘Request Type’ to ‘GET’, and then click on the ‘CREATE WEBHOOK’ button at the bottom of the form.
The webhook is not complete yet. The query parameters need to be defined. Click on the ‘EDIT’ button at the top right of the screen.
Click on the ‘Advanced Settings’ link at the bottom of the page and then under the ‘QUERY PARAMETERS’ section, choose the ‘Custom’ option. Build the query parameters as shown below, using the ‘+ ADD ROW’ button at the bottom to add a row for each query parameter.
The keys on the left (token, V0, V1 .. V6) refer to Blynk virtual pins (datastreams), and the values on the right for ‘v0, v1, pot, lon, lat, spd, moved’ reference variables from the firmware that will be passed from the Particle.publish() function. The value ‘PARTICLE_PUBLISHED_AT’ for virtual pin V6 is a Particle pre-defined variable that provides a timestamp for when the webhook is executed.
At the bottom of the form, make sure the ‘ENFORCE SSL’ option is set to ‘Yes’, and then click the ‘SAVE’ button to save your changes.
After you save your webhook, a summary of the configuration will be shown. Make sure it matches exactly what is shown below (except for the server address).
Note that we will be passing the unique BLYNK_AUTH_TOKEN defined in the firmware for each device to the Particle webhook as the variable ‘t’. This allows each device to call the same webhook, at the expense of increasing the cellular payload for each transmission by 32 bytes.
You can learn more about Particle webhooks by visiting this documentation link.
Below is the firmware code for the Boron. Note that the function ProcessDigitalInputs()
is in loop()
so that it frequently checks for a change in the state of the digital inputs. It uses struct digital_inputs_t to track the state changes, and if the state change is successfully published with the variable ‘alarm’.
The function publishTimer()
limits publishing of the data to every five minutes, and only if a digital input has changed state, or the device’s position has moved more than 122 m or 400 ft. Within the function publishTimer()
the call to Particle.publish()
is what pushes data from the device to the Particle webhook named “blynk_https_get”.
This code implements a persistent method of ensuring that when a digital input state changes, the change will be published. The function publishTimer() will continue to attempt to publish the change until a successful result is returned by the Particle.publish() function. Additionally, the value sent for the digital inputs is the count of state changes. Tracking the count of state changes for the digital inputs makes it possible to see historically if somehow a digital input state change event was missed.
The datastream V5 named ‘position_changed’ is assigned to the Blynk Alarm & Sound widget. When the Blynk datastream is changed to a value of one (1), the widget on the Blynk dashboard will signal with an alarm. The device updates that datastream when it detects it has moved more than 122 m / 400 ft since it was powered on. You can trigger this alarm manually by sending a Blynk HTTP GET with your browser as follows:
Update {server_address} with the server shown at the bottom right of your Blynk.Console, and {token} with the BLYNK_AUTH_TOKEN assigned to your device.
Automations allow the end-user of your app to create scenarios where the device automatically performs one or more actions based on a condition. Following our functional specification, we want to alert the user when the state changes for either of the two digital inputs on the device, or when the device has moved more than 122 m / 400 ft since it was powered on.
The datastream V5 named ‘position_changed’ is updated by the device from a value of 0 (false) to a value of 1 (true) when the device has moved more than 122 m / 400 ft since it was powered on. The GPS calculations are done on the device, making it very easy to use this datastream as a trigger for the automation.
The automation can be created from either the Blynk.Console (web dashboard), or the Blynk.App (mobile app). We will create the automation within the Blynk.Console.
Begin by making the three datastreams V0, V1, and V5 available to Automation actions and conditions. In the Blynk.Console, navigate to ‘Templates -> Automations’. Click on the ‘Edit’ button at the top right of the page and then enable both the ‘Condition’ and ‘Action’ switches for the V0, V1, and V5 datastreams. Click the ‘Save And Apply’ button at the top right of the page when you are finished.
Create the automation by navigating to the ‘Automations’ page in the Blynk.Console, and then clicking on the ‘+ Create Automation’ at the top right of the page. When the dialog ‘Choose Condition’ appears, choose the ‘Device State’ option because we want to trigger the automation based on a change in the state of V0, V1, or V5.
Name the first automation ‘pushbutton_D8’ by editing the field at the top of the page labeled ‘New Automation’. Under the ‘When’ section, choose the device ‘boronb’, the datastream ‘pushbutton_D8’ (V0), and then assign the type of datastream value change of ‘Is On’ because the device will set the datastream value back to zero once the pushbutton connected to the device digital input is released.
Under the section ‘Do this’, choose the ‘@ Send E-Mail’ option and fill out the form as shown below. The placeholder fields {DEVICE_NAME} and {TRIGGER_VALUE} may be typed in as shown, or drag and dropped from ‘Device name’ and ‘Trigger value’ shown at the bottom of the page.
When you are finished configuring the action, click ‘Save’ at the top right of the page. The new automation will appear in the Automations page, and you can see that by default, the automation is enabled.
Go to the Blynk.App mobile app and navigate to ‘Automations’. You will see the automation you just created. From here you can enable/disable the automation, and edit the automation options.
Trigger the automation by pressing the pushbutton on your device connected to the Boron digital input D8. Then monitor the email account(s) assigned as the recipient(s) of the automation.
Repeat the prior steps of configuring an automation for ‘pushbutton_D8’ for the datastream ‘pushbutton_D6’ (V1), except this time choose the action option of ‘Send In-App Notifications’.
Save the automation. Close the Blynk app on your mobile phone and then load it again so that the automation you just created is loaded. Verify that the automation ‘pushbutton_D6’ is shown in the Blynk.App ‘Automations’ page. Then trigger the automation by pressing the pushbutton on your device connected to the Boron digital input D6. You should receive a notification on your mobile phone with the message defined for the automation.
The Alarm and Sound widget on the Blynk web dashboard is already configured to alert the user when the datastream position_changed is set to the value of one (1) by the device. Additionally, an automation could be configured to activate a notification on the user’s phone. The process to define this automation is very similar to what was done for pushbutton_D6.
Devices that connect to the internet via Ethernet or cellular must have a unique authentication token (BLYNK_AUTH_TOKEN) embedded within the firmware. That token is used by the device to make the HTTPS GET request to push data to Blynk. The authentication tokens can be generated in bulk, and then later a user can claim the device scanning a QR Code for a QR Token from the Blynk.App or Blynk.Console.
For the first device (‘boronb’), we created an authentication token (BLYNK_AUTH_TOKEN) using the manual method based on the template ‘BRN404X’. The only user assigned to the account was by default assigned to the device ‘boronb’.
We are going to add a new device named ‘boronc’ based on the template ‘BRN404X’ and then assign it to a new user. The new device is going to be added using the static token method.
From the Blynk.Console, navigate to ‘Static Tokens’ and then click ‘+ Generate Static Tokens’, and select the ‘Generate Multiple’ option. Choose the template ‘BRN404X’ and leave the ‘NUMBER OF TOKENS TO CREATE’ set to the value of ‘1’. Click the ‘Create Tokens’ button.
The static token will be generated and then it will be shown within the list of all static tokens that exist. Under the list column ‘Device Token’, hover your mouse over the right of the partially visible token and select the pop up option ‘Copy to Clipboard’. Use that static token in the clipboard to update the firmware for the new device ‘boronc’. Note that you can also download one or many device static tokens and the associated QR Codes to a .ZIP file so they could be read from a script and programmatically used to update your firmware and generate custom QR Code enhanced instructions for the device end users.
Under the list column labeled ‘QR Token’, hover your mouse over the right of the partially visible QR Token and select the pop up option ‘Copy to clipboard’ to get the QR Code as text, or the option ‘Show QR code’ to view and download a QR Code image file.
Add a new user who doesn’t have an existing Blynk account (identified by email address) by sending them an invite by email. Follow the instructions here, and since we want them to claim the new device themselves, assign them a role and edit the default permissions to ensure they can view, provision, edit, and control the device. The new user who receives the email will click on a link in the email to join Blynk and set their Blynk account password. Once they are logged into Blink, they should install the Blynk.App, login to Blynk on the Blynk.App, and then from the devices panel, choose the menu option ‘+ Add New Device’. Choose the option ‘Scan QR -Code’ and scan in the QR Code for the QR Token. The device will be claimed by the user, and then the user may assign a custom name such as ‘boronc’ to the device.
This article demonstrated in detail how to send data from a Particle cellular device to Blynk. The techniques used the Blynk HTTPs API rather than the firmware library so cellular data usage is minimized. Claiming a device by Blynk was explained using both manual and enterprise suitable techniques. The use of Blynk web dashboard and mobile app widgets were also demonstrated to illustrate how they can be used to visualize data sent from the Particle device, and a widget and an automation was used to alert the user via the dashboard or mobile device running the app of changes in the value of data sent from the Particle device. Adding Blynk services to a Particle device deployment creates a full scope IoT platform solution that can later be white-labeled with Blynk Business plan.
This article has been focused on pushing data from a Particle device to Blynk. Check our another article on how to remotely control a Particle device from the Blynk web dashboard or mobile app.