粒子网格算法 pm_使粒子网格与Blynk一起使用的2种最佳方法

粒子网格算法 pm

This post is originally from my blog on www.jaredwolff.com.

这篇文章最初来自我在www.jaredwolff.com上的博客。

Writing an app takes time. It takes even more time to write one that works with hardware.

编写应用程序需要时间。 编写与硬件兼容的代码需要花费更多时间。

Luckily there's a solution to this problem.

幸运的是,有一个解决此问题的方法。

Enter Blynk.

输入Blynk

It's an app that connects to your hardware. It has a drag and drop interface with pre built widgets. That means you can build an app in seconds. Then upload your device sensors readings within minutes.

这是一个连接到您的硬件的应用程序。 它具有带有预构建小部件的拖放界面。 这意味着您可以在几秒钟内构建一个应用程序。 然后在几分钟之内上传设备传感器的读数。

Blynk does work with Argon, Boron or ethernet connected Xenon. Unfortunately it doesn't work over a Particle Mesh network. In this article you'll learn some of the workarounds to get your mesh based projects up an running.

Blynk可以与氩气,硼或以太网连接的氙气一起使用。 不幸的是,它不能在粒子网格网络上工作。 在本文中,您将学习一些变通方法,以使基于网格的项目正常运行。

从粒子云到布林克 (From Particle Cloud to Blynk)

Let's start with the most simple use case: getting data from any Particle Device to Blynk.

让我们从最简单的用例开始:将数据从任何粒子设备获取到Blynk。

The Air Quality data from Particle Squared is perfect for this example. So, i'll be using that.

对于这个例子,来自Particle Squared的空气质量数据是完美的。 所以,我将使用它。

First let's create a new Blynk Project

首先让我们创建一个新的Blynk项目

Grab the Auth Token we'll need that in a bit. You can tap the Auth Token to copy it to your clipboard.

取得Auth Token,我们稍后将需要它。 您可以点击身份验证令牌将其复制到剪贴板。

Next, let's add a SuperChart for this example.

接下来,让我们为该示例添加一个超级图表

Configure the SuperChart to use a Virtual Pin. We don't have access to the actual hardware pins on the device. V0 is a good choice.

配置超级图表以使用虚拟引脚。 我们无权访问设备上的实际硬件引脚。 V0是一个不错的选择。

To update values in Blynk, we'll have to connect somehow. The best way is to use an Integration in the Particle Console.

要更新Blynk中的值,我们必须以某种方式进行连接。 最好的方法是在粒子控制台中使用集成

In Particle Console, click the icon below the terminal icon. Then click on New Integration.

在粒子控制台中,单击终端图标下方的图标。 然后单击“ 新集成”。

Look at the example below to see how I filled everything out.

查看下面的示例,看看我如何填写所有内容。

Particle Squared uses the Event Name as ****blob. For other projects this may be different. Remember: your event name is the same as from Particle.publish(eventName, data).

粒子平方将事件名称用作**** blob 。 对于其他项目,可能会有所不同。 请记住:您的事件名称与来自Particle.publish(eventName, data)名称相同。

The URL is set to use the blink-cloud.com address. According to their API a sample URL looks like:

URL设置为使用blink-cloud.com地址。 根据他们的API,URL示例如下:

I'll also include it here so it's easier to copy

我也将其包含在此处,以便于复制

http://blynk-cloud.com/auth_token/update/pin?value=value

Replace auth_token with the Auth Token we got earlier.

auth_token替换为我们之前获得的Auth令牌

Replace pin with the virtual pin we want to modify. In this case V0

pin替换为我们要修改的虚拟引脚。 在这种情况下, V0

Replace the value with the value you want to use.

用您要使用的值替换该value

We'll reference one of the values in the Particle Squared blob. It's organized like this:

我们将引用“粒子平方” blob中的值之一。 它的组织方式如下:

{
  "temperature": 28.60,
  "humidity": 45.00,
  "sgp30_tvoc": 18,
  "sgp30_c02": 400,
  "bme680_temp": 27.36,
  "bme680_pres": 1012.43,
  "bme680_hum": 43.80,
  "bme680_iaq": 43.90,
  "bme680_temp_calc": 27.30,
  "bme680_hum_calc": 43.97
}

Particle uses mustache templates. As you can see in the screenshot above, you can set value to {{{temperature}}}.

粒子使用胡须模板 。 如您在上面的屏幕截图中所见,您可以将value设置为{{{temperature}}}

Note: If you're working on your own project, it's important to publish with JSON. As a reference the Particle.publish command looks like:

注意:如果您正在处理自己的项目,则使用JSON发布非常重要。 作为参考, Particle.publish命令如下所示:

// Publish data
Particle.publish("blob", String::format("{\"temperature\":%.2f,\"humidity\":%.2f}",si7021_data.temperature, si7021_data.humidity) , PRIVATE, WITH_ACK);

Click the big blue Save button at the bottom of the screen. Then we can move on to the next step!

点击屏幕底部的蓝色大保存按钮 。 然后,我们可以继续下一步!

测试中 (Testing)

Since creating our Particle Webhook Integration, it's been publishing data to Blynk. Let's go see if it's working.

自创建我们的Particle Webhook集成以来,它一直在将数据发布到Blynk。 让我们看看它是否正在工作。

First, let's go back to the Blynk app. Hit the Play Button in the top Right in Blynk screen.

首先,让我们回到Blynk应用程序。 点击 Blynk屏幕右上方的“播放”按钮

If your integration has been running for a while, you should see the graph populate with data! In the case you don't see anything, let's check the logs.

如果您的集成已经运行了一段时间,您应该会看到图形中填充了数据! 如果您什么都看不到,让我们检查一下日志。

Go back to your integration and scroll towards the bottom. We want to see if there are any errors.

返回到您的集成滚动到底部 。 我们想看看是否有任何错误。

Not sure what that looks like? Here's an example of an integration with errors:

不确定是什么样子? 这是一个有错误的集成示例:

You can scroll further down to investigate why the error has occurred.

您可以进一步向下滚动以调查发生错误的原因。

All the way at the bottom shows the response from the server. Depending on the service, they'll give you information why your API call failed. In my case, I was missing values for two fields.

底部的所有方式均显示了服务器的响应。 根据服务的不同,他们将为您提供API调用失败的原因。 就我而言,我缺少两个字段的值。

Blynk的粒子正在运行! (Particle to Blynk is working!)

You now have a basic way of publishing to a virtual pin in Blynk. There are drawbacks though. Most importantly, you'll have to create an integration for every signal virtual pin. If you have eight readings, that means eight integrations.

您现在有了在Blynk中发布到虚拟图钉的基本方法。 虽然有缺点。 最重要的是,您必须为每个信号虚拟引脚创建一个集成。 如果您有八个读数,则意味着八个积分。

Bummer.

笨蛋

In the next section, you'll learn a different way to configure Blynk. Let's go!

在下一节中,您将学习配置Blynk的另一种方法。 我们走吧!

使用Blynk库的局部网格 (Local Mesh Using Blynk Library)

Unlike the first method, we'll be focusing on changing firmware only.

与第一种方法不同,我们将只专注于更改固件。

We’ll use a Argon, Boron or Ethernet Connected Xenon and one regular Xenon. For the rest of this tutorial, we'll call these devices an “edge router”.

我们将使用氩,硼或以太网连接的氙气和一个普通的氙气。 在本教程的其余部分中,我们将这些设备称为“边缘路由器”。

The Xenon will run the Particle Squared code. Instead of using Particle.publish we'll be using Mesh.publish. This allows us to publish only to the local mesh network.

Xenon将运行粒子平方代码。 而不是使用Particle.publish我们将使用Mesh.publish 。 这使我们只能发布到本地网状网络。

Meanwhile the edge router is listening for the message. It collects the values and then uses the Blynk API to publish to the app.

同时,边缘路由器正在侦听消息。 它收集值,然后使用Blynk API发布到应用程序。

Here are the steps:

步骤如下:

设置我们的边缘路由器 (Setup our Edge Router)

Pull up the menu by pressing Cmd+Shift+P. Type Install Library.

按下Cmd + Shift + P上拉菜单。 键入安装库。

Then enter blynk. The library should download if you haven't already.

然后输入blynk。 如果尚未下载该库,则应下载。

Once installed you can include the library at the top of your .ino file like so:

安装后,您可以将库包含在.ino文件顶部,如下所示:

#include <blynk.h>

In our setup() function let's init the Blynk library:

在我们的setup()函数中,让我们初始化Blynk库:

// Put initialization like pinMode and begin functions here.
Blynk.begin(auth);

In our setup() function, subscribe to the temperature event. The connected Xenon will generate this event.

在我们的setup()函数中,订阅temperature事件。 连接的氙气将生成此事件。

// Subscribe to temperature events
Mesh.subscribe("temperature",tempHandler);

Define tempHandler like this for now:

现在像这样定义tempHandler

// Temperature event handler for mesh
void tempHandler(const char *event, const char *data){
}

In the loop() function make sure we have Blynk.run();

loop()函数中,确保我们有Blynk.run();

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.
  Blynk.run();
}

Finally, for tempHandler we can add a debug print to monitor events. I've used something like this:

最后,对于tempHandler我们可以添加调试打印来监视事件。 我用过这样的东西:

Serial.printlnf("event=%s data=%s", event, data ? data : "NULL");

Particle uses this in some of their examples. It's perfect for our purposes as well!

在他们的一些示例中,Particle使用了此方法。 这对于我们的目的也是完美的!

Note: make sure you have Serial.begin() called in your Setup() function!

注意:确保在Setup()函数中调用了Serial.begin()

So now we have tempHandler to receive data from the Xenon. The edge router can now take that data and upload it to Blynk. Let's use the Blynk.virtualWrite function for this:

所以现在我们有了tempHandler来接收来自氙气的数据。 边缘路由器现在可以获取该数据并将其上传到Blynk。 让我们为此使用Blynk.virtualWrite函数:

// Write the data
Blynk.virtualWrite(V0, data);

This will write the temperature value from a Xenon to the V0 pin. If you used something other than V0, be sure to change that value here. (This is the same setup as the previous Particle Cloud to Blynk example)

这会将氙气的温度值写入V0引脚。 如果您使用的不是V0,请确保在此处更改该值。 (这与前面的“ 粒子云到Blynk”示例的设置相同)

The final code for the edge router should look something like this. Compile a flash it to your device when you're ready!

边缘路由器的最终代码应如下所示。 准备好将Flash编译到设备上!

/*
 * Project blynk-argon-forwarder
 * Description: Argon Blynk forwarder for Particle Mesh. Forwards data from mesh connected devices to Blynk.
 * Author: Jared Wolff
 * Date: 7/25/2019
 */

#include <blynk.h>

char auth[] = "<ENTER YOUR AUTH KEY>";

// Temperature event handler for mesh
void tempHandler(const char *event, const char *data){
  Serial.printlnf("event=%s data=%s", event, data ? data : "NULL");

  // Write the data
  Blynk.virtualWrite(V0, data);
}

// setup() runs once, when the device is first turned on.
void setup() {

  // Serial for debugging
  Serial.begin();

  // Put initialization like pinMode and begin functions here.
  Blynk.begin(auth);

  // Subscribe to temperature events
  Mesh.subscribe("temperature",tempHandler);

}

// loop() runs over and over again, as quickly as it can execute.
void loop() {
  // The core of your code will likely live here.
  Blynk.run();

}

Remember to set auth using the AUTH TOKEN in the Blynk app!

切记在Blynk应用程序中使用AUTH TOKEN设置auth

设置氙气 (Setting up a Xenon)

Create a new project. This time it will be for the Xenon capturing "temperature data."

创建一个新项目。 这次将是氙气捕获“温度数据”的时间。

Let's add a variable called time_millis to the top of the file. The type is system_tick_t. We'll use it to create a simple delay timer for the temperature readings.

让我们在文件顶部添加一个名为time_millis的变量。 类型是system_tick_t 。 我们将使用它为温度读数创建一个简单的延迟计时器。

// Global variable to track time (used for temp sensor readings)
system_tick_t time_millis;

For the interval, let's use a preprocessor define

对于间隔,让我们使用预处理器定义

#define INTERVAL_MS 2000

Now let's tie those together in the loop() function. We'll use an if statement to compare our current system time with that of the last event plus offset. If you ever need a simple timer, this is one of the best ways to do it!

现在让我们在loop()函数中将它们绑在一起。 我们将使用if语句将当前系统时间与上一个事件加偏移量的时间进行比较。 如果您需要一个简单的计时器,这是最好的方法之一!

// Check if our interval > 2000ms
  if( millis() - time_millis > INTERVAL_MS ) {
  }

Once we're inside, make sure you reset timer_millis:

一旦进入内部,请确保您重置timer_millis

//Set time to the 'current time' in millis
    time_millis = millis();

Then, we'll generate the temperature value using the random() function. We'll use the two parameter variant. That way we can set the minimum value and the maximum value:

然后,我们将使用random()函数生成温度值。 我们将使用两个参数的变体。 这样我们可以设置最小值和最大值:

// Create a random number
    int rand = random(20,30);

Finally we'll Mesh.publish the value:

最后,我们将Mesh.publish值:

// Publish our "temperature" value
    Mesh.publish("temperature",String::format("%d",rand));

When this example runs, the temperature is broadcast to the mesh network. Then, the edge router receives it and forwards it on to Blynk!

运行此示例时,温度将广播到网状网络。 然后,边缘路由器将其接收并将其转发给Blynk!

You can flash this firmware whenever you're ready. Here's the full code for the Xenon so you can cross compare:

准备就绪后,即可刷新此固件。 这是Xenon的完整代码,因此您可以交叉比较:

/*
 * Project blynk-xenon-rgb
 * Description: Recieve RGB level from connected Edge Router. Sends simiulated temperature values via mesh to the Blynk cloud.
 * Author: Jared Wolff
 * Date: 7/25/2019
 */

// How often we update the temperature
#define INTERVAL_MS 2000

// Global variable to track time (used for temp sensor readings)
system_tick_t time_millis;
// setup() runs once, when the device is first turned on.
void setup() {

  // Set time to 0
  time_millis = 0;

}

// loop() runs over and over again, as quickly as it can execute.
void loop() {

  // Check if our interval > 2000ms
  if( millis() - time_millis > INTERVAL_MS ) {
    //Set time to the 'current time' in millis
    time_millis = millis();

    // Create a random number
    int rand = random(20,30);

    // Publish our "temperature" value
    Mesh.publish("temperature",String::format("%d",rand));

  }

}

给它一个测试! (Give it a test!)

Now that we've programmed both devices let's get them talking to each other.

现在,我们已经为这两种设备编程了,让我们让它们彼此交谈。

I've already set up the Argon with a mesh network called 8f-9. I'll explain how to get the Xenon connected with the CLI. You can also used the Particle App.

我已经使用称为8f-9的网状网络设置了Argon 我将解释如何使Xenon与CLI连接。 您也可以使用粒子应用程序。

First, let's connect the Xenon to USB and get it into Listening Mode. After connect, hold the Mode button until blinking blue.

首先,让我们将Xenon连接到USB,并使它进入聆听模式。 连接后,按住“ 模式”按钮,直到闪烁蓝色。

Then use the CLI to set up the mesh network. First let's get the device ID:

然后使用CLI设置网状网络。 首先让我们获取设备ID:

Jareds-MacBook-Pro:nrfjprog.sh jaredwolff$ particle identify
? Which device did you mean?
  /dev/tty.usbmodem146401 - Argon
❯ /dev/tty.usbmodem146101 - Xenon

If you have multiple devices connect, make sure you select the right one! If prompted, select a device. Your output should look something like:

如果有多个设备连接,请确保选择正确的设备! 如果出现提示,请选择设备。 您的输出应类似于:

Your device id is e00fce682d9285fbf4412345
Your system firmware version is 1.3.0-rc.1

We'll need the id for the next step. Now, let's run the particle mesh command.

下一步需要ID 。 现在,让我们运行particle mesh命令。

particle mesh add <xenon id> <id of your argon, boron, etc>

Here's an example below:

下面是一个示例:

particle mesh add e00fce682d9285fbf4412345 hamster_turkey
? Enter the network password [hidden]
▄ Adding the device to the network...

At the end of it you'll see:

在它的结尾,您将看到:

Done! The device should now connect to the cloud.

This process is not perfect. During the Adding the device to the network... stage, I had to remove the Xenon using particle mesh remove. Then re-run the particle mesh add command after resetting the Argon.

这个过程并不完美。 在“ Adding the device to the network...阶段,我不得不使用particle mesh remove Adding the device to the network... particle mesh remove氙气。 然后在重置Argon后重新运行particle mesh add命令。

Now here comes to finale.

现在到了结局。

Connect the two devices to serial using particle serial monitor --follow

使用particle serial monitor --follow将两个设备连接到串行- particle serial monitor --follow

If you have the two devices connected, particle serial monitor will prompt you to select:

如果您连接了两个设备, particle serial monitor将提示您选择:

Jareds-MacBook-Pro:blynk-xenon-rgb jaredwolff$ particle serial monitor --follow
Polling for available serial device...
? Which device did you mean? /dev/tty.usbmodem146101 - Xenon
Opening serial monitor for com port: "/dev/tty.usbmodem146101"
Serial monitor opened successfully:

Remember: You have to run particle serial monitor for each device you want to connect to.

切记:您必须为要连接的每个设备运行particle serial monitor

If all is working, you'll likely see some output from the edge router!

如果一切正常,您可能会看到边缘路由器的一些输出!

Serial monitor opened successfully:
event=temperature data=21
event=temperature data=28
event=temperature data=21
event=temperature data=27
event=temperature data=28
event=temperature data=26
event=temperature data=23
event=temperature data=26
event=temperature data=21

Looking at the app, the Super Chart should be reacting to this new data.

查看该应用程序, 超级图表应对此新数据做出React。

Compare the last value in the command line to the last on the chart? Do they match? If so, you made it to the end of this example!

将命令行中的最后一个值与图表中的最后一个进行比较? 他们匹配吗? 如果是这样,那么您就可以完成本示例!

结论 (Conclusion)

In this tutorial you've learned how to forward Particle Cloud data to Blynk. You've also learned how to do the same using a Particle Argon, Boron or ethernet connected Xenon. Awe yea. 😎👍

在本教程中,您学习了如何将粒子云数据转发到Blynk。 您还学习了如何使用粒子氩气,硼或以太网连接的氙气进行相同的操作。 太好了 😎👍

Now that you have the tools to Blink-ify your Particle Mesh powered projects, it's time to get to work!

现在,您已经拥有了对Battery Mesh驱动的项目进行眨眼化处理的工具,是时候开始工作了!

Like this post?

喜欢这个职位吗?

This post is an excerpt from my upcoming Ultimate Guide to Particle Mesh. I'll be sharing more exclusive content with my mailing list as it get's closer to launch. You can sign up here for updates.

这篇文章摘自我即将出版的《粒子网格终极指南》 。 随着邮件列表越来越接近发布,我将与我的邮件列表共享更多独家内容。 您可以在此处注册以进行更新。

Still have questions?

还有问题吗?

Leave a comment or shoot me a line.

发表评论或给我打个电话。

翻译自: https://www.freecodecamp.org/news/2-best-ways-to-get-particle-mesh-working-with-blynk/

粒子网格算法 pm

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值