2 Best Ways to Get Particle Mesh Working With Blynk
Writing an app to remotely monitor and control your IoT hardware projects can be a daunting task. Fortunately, the Blynk platform makes it easy to create a polished mobile app UI that interfaces with your devices, with minimal coding required.
Blynk supports a wide variety of hardware, including Particle devices. However, Particle Mesh networked devices add some complexity, as they don‘t directly connect to the Blynk cloud.
In this guide, we‘ll walk through two approaches to integrating Particle Mesh projects with Blynk:
- Forwarding data from the Particle Cloud to Blynk
- Using a local edge router to send mesh data to Blynk
Whether you have a simple sensor node or a complex mesh network, these techniques will get you up and running with Blynk in no time. Let‘s dive in!
Particle Cloud to Blynk
The simplest way to send data from any Particle device, including Mesh devices, to Blynk is by leveraging Particle‘s cloud connectivity and webhook integrations. Here‘s how to set it up:
Create a Blynk Project
First, create a new project in the Blynk app. Give it a name and select the hardware model you‘re using. Then, tap the "Create" button.
You‘ll see an "Auth Token" for your project, which you‘ll need later. Tap the token to copy it to your clipboard.
Add a Widget
Let‘s add a SuperChart widget to plot the data we‘ll receive from the Particle device. Tap and hold on the project dashboard, select "SuperChart" from the widget box, and drop it where you want it positioned.
Tap on the new SuperChart to configure it. Most importantly, select the virtual pin it will receive data on. Let‘s use V0 for this example.
Set Up a Particle Webhook
Now let‘s create a webhook integration to forward Particle device events to Blynk. Open the Particle Console, go to the Integrations tab, and click "New Integration".
Fill out the integration details as follows:
- Event Name: The name of the event your device will publish, e.g. "sensor/temp"
- URL: The Blynk API endpoint to update a virtual pin value, in the format:
http://blynk-cloud.com/YOUR_AUTH_TOKEN/update/VIRTUAL_PIN?value=VALUE
Replace YOUR_AUTH_TOKEN with your project auth token from before, VIRTUAL_PIN with the pin number (e.g. V0), and VALUE with the mustache template for the event data (more on this in a moment). - Request Type: PUT
- Request Format: Query Parameters
In the "Custom JSON" field, you can use mustache templates to extract values from the event data. For example, if the event data is in the format:
{ "temp": 25.3 }
Then you can use {{{temp}}}
to substitute the temp value into the Blynk URL. The triple braces {{{ }}}
are used for values that don‘t need JSON escaping.
Click the "Save" button to create your new integration.
Publish an Event
Now let‘s send some data! In your Particle device firmware, publish an event with the name you specified and data in the expected JSON format. For example:
Particle.publish("sensor/temp", "{\"temp\":25.3}", PRIVATE);
If you‘re using Particle Mesh, you‘d use Mesh.publish()
instead to send to the local mesh rather than the cloud. We‘ll cover that in the next section.
Confirm Data Received
Open the Blynk app and go to your project. Tap the "Play" button in the top right to start streaming data. After a few published events, you should see them plotted on the SuperChart!
If data isn‘t showing up, try these debugging steps:
- Make sure the event name matches your firmware code exactly.
- Double check that the auth token and pin number are correct in the webhook URL.
- Look for any errors in the integration logs in the Particle Console.
- Use
particle subscribe
in the CLI to monitor the events and make sure they contain the expected data.
Local Mesh to Blynk with Edge Router
Using the Particle cloud to forward data to Blynk works well, but requires an active internet connection. For local Particle Mesh networks, you may want a standalone solution.
We can achieve this by designating an always-online mesh device (an Argon, Boron, or Ethernet-connected Xenon) as an "edge router". It will receive sensor data from other battery-powered Xenons over the local mesh network and forward it to Blynk directly using the Blynk C++ library.
Here‘s an overview of the architecture:
And here‘s how to implement it:
Set Up the Edge Router
In Particle Workbench, create a new project for your edge router device (e.g. Argon). From the command palette (CMD/CTRL+SHIFT+P), select "Particle: Install Library" and type "blynk" to search for and install the Blynk library.
In your project‘s .ino file, include the Blynk library header:
#include <blynk.h>
And in the setup()
function, initialize Blynk with your project auth token:
Blynk.begin("YOUR_AUTH_TOKEN");
Now the edge router can talk to Blynk, but we need it to listen for data from other mesh devices. In setup()
, subscribe to the mesh event that will carry the sensor data:
Mesh.subscribe("sensor/temp", tempHandler);
And declare the event handler function:
void tempHandler(const char *event, const char *data) {
// Parse the JSON data and forward it to Blynk here
}
Finally, in the loop()
function, add a call to Blynk.run()
to process incoming Blynk messages:
void loop() {
Blynk.run();
}
Forward Mesh Data to Blynk
When the edge router receives a subscribed mesh event, the specified handler function is called with the event name and JSON data.
In the tempHandler
function, we can parse the data and send it to Blynk using Blynk.virtualWrite()
:
void tempHandler(const char *event, const char *data) {
// Extract the temp value from the JSON data using JSONPath
float temp = String(data).toFloat();
// Send the value to Blynk on virtual pin V0
Blynk.virtualWrite(V0, temp);
// Log the received data for debugging
Log.info("Received temp: %f", temp);
}
Here we‘re expecting the data to be in a simple floating-point format, but you could use a JSON parser like ArduinoJson for more complex payloads.
Configure the Sensor Nodes
Now let‘s set up one or more Xenon nodes to read sensor data and send it over the mesh network.
Create a new Particle project for the Xenon, and in the firmware code, publish the sensor data on a regular interval using Mesh.publish()
:
void loop() {
// Read the sensor value
float temp = readTemp();
// Publish the value as a private event on the mesh network
Mesh.publish("sensor/temp", String(temp), PRIVATE);
delay(30000); // Send every 30 seconds
}
The exact code will depend on the type of sensor you‘re using and how it‘s connected. Refer to the sensor documentation for reading values.
Create a Mesh Network
To create a local Particle Mesh network, we need to add all the Xenon nodes to the edge router‘s network.
Make sure the edge router is online and breathing cyan. Then, put a Xenon in listening mode by holding the MODE button until it blinks blue.
Using the Particle CLI, add the Xenon to the edge router‘s network with the particle mesh add
command:
particle mesh add XENON_ID EDGE_ROUTER_ID
Replace XENON_ID
with the device ID of the Xenon (find it with particle identify
), and EDGE_ROUTER_ID
with the ID of your Argon or Boron edge router.
You‘ll be prompted to enter the network password, which you can find in the edge router‘s firmware code under MeshAuthPassword
.
Repeat this process for each Xenon node you want to add to the network. You can check the network status and list connected nodes using particle mesh
.
Test the Data Flow
With all the pieces in place, it‘s time to test our Particle Mesh to Blynk pipeline!
Flash the edge router and Xenon node firmware to the devices. You can use the Particle CLI cloud flash feature for easy OTA updates:
particle flash DEVICE_NAME path/to/firmware.bin
Once all devices are running the latest firmware, open the Blynk app and go to your project. Tap the "Play" button to start streaming data.
On the edge router, open a serial monitor to view the debug logs:
particle serial monitor --follow
You should see the tempHandler
logging the received mesh events, and the sensor data plotting on the Blynk SuperChart in the app.
Success! We now have a standalone Particle Mesh network sending sensor data to a beautiful Blynk mobile app.
Next Steps
In this guide, we‘ve covered two approaches to integrating Particle Mesh projects with the Blynk app platform:
- Forwarding Particle device events from the cloud to Blynk using webhook integrations
- Creating a local mesh network with an edge router to send data to Blynk using the Blynk C++ library
These techniques open up the power and flexibility of Particle Mesh for building compelling IoT applications with slick Blynk user interfaces.
Some ideas to expand on this foundation:
- Add more sensor types and Blynk widgets for a full-featured dashboard
- Implement controls for actuators and device parameters settable via Blynk
- Optimize Xenon node power consumption for long battery life
- Explore the new Blynk 2.0 platform for more app design options
There‘s no limit to what you can build when combining the unique capabilities of Particle Mesh and Blynk. Go forth and create some awesome connected products!