This article will provide details on how to push data from a Particle cellular device to the Blynk IoT platform. We will be using the Particle.publish() function, a Particle webhook (integration), and the Blynk HTTPs API. The data will appear in a Blynk dashboard and a mobile app. Changes in the state / values of digital inputs on the device, and the device’s position will also trigger alerts that notify the user via the Blynk dashboard and mobile app.
This article will focus on pushing data from a Particle device to Blynk. Another article will follow that will detail how to remotely control a Particle device from the Blynk web dashboard or mobile app.
Particle is a Platform-as-a-Service that begins with cellular and WiFi microcontroller devices, and ends with a pipeline for communicating with those devices. Security, scalability, and connectivity are provided.
Blynk is a hardware agnostic IoT platform that provides a secure bi-directional connection between devices, the Blynk Cloud and a mobile app and web dashboard. Both the native mobile apps for iOS and Android and the web dashboard are built with drag-and-drop widgets, eliminating the need to write code for the front-end.
Why Connect to Blynk?Particle doesn’t have long term data storage, web dashboard, or a mobile app for device remote control and data visualization. Particle does have powerful no-code webhooks / integrations that are used to connect a Particle device with a 3rd party data storage and web dashboard. Particle’s app is solely for claiming a device.
Connecting a Particle device to Blynk adds historical data storage, a no-code web dashboard, iOS/ Android mobile apps, and automations. Blynk offers scalable tools for user and device management including multi-level organization structures and flexible permission controls.
Functional Specification
A functional specification defines the requirements for how the device and system should function. An important distinction of this list is that it does not describe how something should be done, just what it should do.
- 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.
The Particle device is a Boron 404xcellular LTE Cat M1 device. A GPS FeatherWingwith an external active 28 dB GPS antenna, and a SMA to uFL/u.FL/IPX/IPEX RF adapter cableprovides 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 DatastreamsBlynk 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.
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:
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 s
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.
Blynk HTTPs APIFor this project, we want the Particle webhook (integration)to pass data from the Particle device to Blynk. The Blynk HTTPs APIwill 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:
https://{server_address}/external/api/batch/update?token={token}&V0=0&V1=0&V2=1.5&V3=-73.8731, 40.8414&V4=12.54&V5=0
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.
Particle Webhook/IntegrationWe 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 Consoleand 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 variablethat 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.
FirmwareThe 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.
Blynk Dashboard Widget Alarm & Sound WidgetThe 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:
https://{server_address}/external/api/batch/update?token={token}&V5=1
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.
Blynk AutomationAutomationsallow 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.
Configure an Automation for Position Changed (V5)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.
Add Blynk User & DeviceDevices 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 methodbased 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 permissionsto 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.
Wrap UpThis 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. Another article will follow that will detail how to remotely control a Particle device from the Blynk web dashboard or mobile app.
Comments