粒子網格算法 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 dataBlynk.virtualWrite(V0, data);
}// setup() runs once, when the device is first turned on.
void setup() {// Serial for debuggingSerial.begin();// Put initialization like pinMode and begin functions here.Blynk.begin(auth);// Subscribe to temperature eventsMesh.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 > 2000msif( millis() - time_millis > INTERVAL_MS ) {}
Once we're inside, make sure you reset timer_millis
:
一旦進入內部,請確保您重置timer_millis
:
//Set time to the 'current time' in millistime_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 numberint rand = random(20,30);
Finally we'll Mesh.publish
the value:
最后,我們將Mesh.publish
值:
// Publish our "temperature" valueMesh.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 0time_millis = 0;}// loop() runs over and over again, as quickly as it can execute.
void loop() {// Check if our interval > 2000msif( millis() - time_millis > INTERVAL_MS ) {//Set time to the 'current time' in millistime_millis = millis();// Create a random numberint rand = random(20,30);// Publish our "temperature" valueMesh.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