devicehive_如何使用DeviceHive运行Grafana

devicehive

by Nikolay Khabarov

通过尼古拉·哈巴罗夫(Nikolay Khabarov)

如何使用DeviceHive运行Grafana (How to run Grafana with DeviceHive)

DeviceHive is an IoT platform which has plenty of different components. The Grafana plugin is one of them. This plugin can gather data from a DeviceHive server and display it with different dashboards using the very popular tool — Grafana. This article explains how to create a Grafana dashboard with DeviceHive. As an example, this uses the ESP8266 chip analog pin to visualise the voltage on it.

DeviceHive是一个物联网平台,具有许多不同的组件。 Grafana插件就是其中之一。 该插件可以从DeviceHive服务器收集数据,并使用非常流行的工具-Grafana在不同的仪表板上显示数据。 本文介绍了如何使用DeviceHive创建Grafana仪表板。 例如,它使用ESP8266芯片模拟引脚来显示其上的电压。

数据 (Data)

To display anything on a dashboard we need data. In terms of a DeviceHive server, data can be provided via ‘commands’ and ‘notifications’. Commands are typically used to deliver any messages to a device which the device should execute while ‘notifications’ are the opposite, devices notify their subscribers about certain events. ‘Commands’ and ‘notifications’ are basically simple JSON messages.

要在仪表板上显示任何内容,我们需要数据。 对于DeviceHive服务器,可以通过“命令”和“通知”提供数据。 命令通常用于将任何消息传递到设备应执行的消息,而“通知”则相反,设备将某些事件通知其订户。 “命令”和“通知”基本上是简单的JSON消息。

Both of these two entities can be used to plot graphs, display static text, a gauge, table or any other Grafana components. For this article we will generate notifications using special DeviceHive firmware for the ESP8266 chip. This firmware allows the chip to connect directly to a DeviceHive server using its protocol and has plenty of documented commands which can be issued from the server side.

这两个实体均可用于绘制图形,显示静态文本,量规,表格或任何其他Grafana组件。 对于本文,我们将使用ESP8266芯片的特殊DeviceHive固件生成通知。 该固件允许芯片使用其协议将其直接连接到DeviceHive服务器,并具有可从服务器端发出的大量书面命令 。

使用ESP8266固件生成通知 (Generating notifications with ESP8266 firmware)

The binaries for the DeviceHive firmware are available here. Download the latest version and flash this firmware to your chip. The release archive contains documentation on how to do that, but if you have a ‘nodemcu’-like board you just need to connect the board via a microUSB cable to your computer and run the ‘esp-flasher’ util from the release archive for your operating system and wait until it flashes the board. Having flashed the board, there is a need to configure the chip which Wi-Fi network, DeviceHive server, and credential it should use. There are two ways to do that: using a posix-like terminal with the ‘esp-terminal’ util or wirelessly as described here.

DeviceHive固件的二进制文件在此处提供 。 下载最新版本并将此固件刷新到您的芯片上。 发布档案包含有关如何执行此操作的文档,但是如果您有类似“ nodemcu”的板,则只需通过microUSB电缆将板连接到计算机,然后运行发行档案中的“ esp-flasher”实用程序即可您的操作系统,然后等待它闪烁主板。 刷新主板后,需要配置芯片,该芯片应使用Wi-Fi网络,DeviceHive服务器和证书。 有两种方法可以做到这一点:使用POSIX类终端用“ESP端” util的或无线的描述在这里 。

There is a free playground service, which can be used for absolutely free to try a DeviceHive server. After your chip is connected to your server or playground, go to the server admin panel, find your ESP8266 device in the device list and issue the ‘adc/int’ command with the parameters ‘{“0”: 500}’.

有一个免费的游乐场服务 ,可以完全免费地试用DeviceHive服务器。 将芯片连接到服务器或游乐场后,转到服务器管理面板,在设备列表中找到您的ESP8266设备,然后执行带参数'{“ 0”:500}'的'adc / int'命令。

This command causes the esp8266 to report every 500ms the voltage on ADC input #0(the only ESP8266 has). After switching to ‘notifications’ there should be screen like:

该命令使esp8266每500ms报告一次ADC输入0上的电压(唯一的ESP8266具有)。 切换到“通知”后,应该显示如下屏幕:

That is the voltage on the chip’s input pin. And this kind of data is suitable for us to display with Grafana: notifications contain data (parameters in our case), notifications come continuously, and all DeviceHive’s notifications always have a timestamp. Having an analogue sensor connected to this pin it is possible to display this data with Grafana.

那就是芯片输入引脚上的电压。 这种数据适合我们在Grafana上显示:通知包含数据(在本例中为参数),通知连续出现,并且所有DeviceHive的通知始终带有时间戳。 通过将模拟传感器连接到该引脚,可以使用Grafana显示此数据。

将DeviceHive Grafana插件安装到Grafana (Installing the DeviceHive Grafana plugin to Grafana)

Grafana can be used as a local service or as a hosted service. To install Grafana locally, please, refer to the “Official documentation. Grafana installation”.

Grafana可以用作本地服务或托管服务。 要在本地安装Grafana,请参阅“ 官方文档。 Grafana安装 ”。

You can find how to install plugins in the “Official documentation. Plugin installation”.

您可以在“ 官方文档 ”中找到如何安装插件。 插件安装 ”。

To install DeviceHive datasource via grafana-cli you can use the following command:

要通过grafana-cli安装DeviceHive数据源,可以使用以下命令:

$ grafana-cli plugins install devicehive-devicehive-datasource

$ grafana-cli plugins install devicehive-devicehive-datasource

If you want to install the plugin manually, you should perform the following steps:

如果要手动安装插件,则应执行以下步骤:

Prerequisites, these packages should be installed:

先决条件,应安装以下软件包:

  • Grafana >= 4.6

    Grafana> = 4.6
  • NodeJs >= 8 (optional)

    > = 8的NodeJ(可选)
  • NPM >= 5 (optional)

    NPM> = 5(可选)
  • Grunt (npm install -g grunt) (optional)

    Grunt( npm install -g grunt )(可选)

Also you should have permissions to copy data to the Plugins folder (you could set it in grafana.ini in Paths->plugins).

此外,您还应具有将数据复制到Plugins文件夹的权限(可以在grafana.ini中的“ Paths->plug进行设置)。

  1. Clone this repo to the Plugins folder — git clone https://github.com/devicehive/devicehive-grafana-datasource.git;

    将此仓库克隆到Plugins文件夹— git clone https://github.com/devicehive/devicehive-grafana-datasource.git ;

  2. Next steps are optional (in case if you want to rebuild datasource sources code):

    后续步骤是可选的(如果您要重建数据源源代码):

    2.1 Go into folder —

    2.1进入文件夹—

    cd devicehive-grafana-datasource;

    cd devicehive-grafana-datasource ;

    2.2 Install all packages —

    2.2安装所有软件包—

    npm install;

    npm install ;

    2.3 Build plugin —

    2.3构建插件-

    npm run build;

    npm run build ;

  3. Restart Grafana server

    重新启动Grafana服务器
  4. Open Grafana in browser;

    在浏览器中打开Grafana;
  5. Open the side menu by clicking the Grafana icon in the top header;

    单击顶部标题中的Grafana图标,打开侧面菜单。
  6. In the side menu click Data Sources;

    在侧面菜单中,单击“ Data Sources

  7. Click the + Add data source in the top header;

    点击顶部标题中的+ Add data source

  8. Select DeviceHive from the Type dropdown;

    Type下拉菜单中选择DeviceHive

  9. Configure the datasource.

    配置数据源。

After installation you will be able to see the DeviceHive datasource plugin in the installed plugins list (look at the picture below).

安装后,您将能够在已安装的插件列表中看到DeviceHive数据源插件(请看下面的图片)。

添加Grafana数据源 (Adding Grafana datasource)

To add DeviceHive datasource, you should perform the following steps:

要添加DeviceHive数据源,应执行以下步骤:

  1. Open the side menu by clicking the Grafana icon in the top header;

    单击顶部标题中的Grafana图标,打开侧面菜单。
  2. In the side menu click Data Sources;

    在侧面菜单中,单击“ Data Sources

  3. Click the + Add data source in the top header;

    点击顶部标题中的+ Add data source

  4. Select DeviceHive from the Type dropdown;

    Type下拉菜单中选择DeviceHive

Look at the picture below:

看下面的图片:

To configure the DeviceHive datasource you should fill on the following fields:

要配置DeviceHive数据源,您应该填写以下字段:

Server URL (is the path to the DeviceHive WebSocket server. For the playground this is ws://playground.devicehive.com/api/websocket)Device ID (unique identifier of the DeviceHive device)Login/Password or AccessToken — credentials to pass authentication

服务器URL(是DeviceHive WebSocket服务器的路径。对于游乐场,它是ws://playground.devicehive.com/api/websocket)设备ID(DeviceHive设备的唯一标识符)登录/密码或AccessToken-要传递的凭据认证方式

Also, you are able to specify the RefreshToken for auto refreshing the AccessToken

另外,您可以指定RefreshToken以自动刷新AccessToken

On the picture below you can observe the configuration workflow:

在下面的图片上,您可以观察配置工作流程:

After adding and configuring a DeviceHive datasource, it should exist in the datasource list as in the picture below:

添加并配置DeviceHive数据源后,它应存在于数据源列表中,如下图所示:

创建新的仪表板 (Create new dashboard)

To create a new dashboard you should just click on the “New” button in the sidebar panel as shown in the picture below:

要创建新的仪表板,只需单击侧边栏面板中的“新建”按钮,如下图所示:

In this article we will show examples on the Graph panel, so, click on the Graph button:

在本文中,我们将在“图形”面板上显示示例,因此,单击“图形”按钮:

After that you will be able to see line chart on your dashboard:

之后,您将能够在仪表板上看到折线图:

使用Grafana的图显示通知/命令 (Displaying notifications/commands with Grafana’s graph)

Notification and commands are DeviceHive entities:Command: represents a message dispatched by clients for devicesNotification: represents a message dispatched by devices for clients

通知和命令是DeviceHive实体:命令:代表客户端为设备调度的消息通知:代表设备为客户端调度的消息

By default, a Notification or Command message provides the field named “parameters” in which a user can pass their own data.

默认情况下,通知或命令消息提供名为“参数”的字段,用户可以在其中传递自己的数据。

At the start of this article we configured the ESP8266 device to send notifications with data that represents the state on analogue pin#0 of the chip. In the picture below you are able to observe how to configure the Grafana graph panel to make it show the data on the line chart:

在本文开始时,我们将ESP8266设备配置为发送带有表示芯片模拟引脚#0上状态的数据的通知。 在下面的图片中,您可以观察如何配置Grafana图形面板以使其在折线图上显示数据:

在Grafana的图上显示注释 (Displaying annotations on Grafana’s graph)

Annotations provide a way to mark points on the graph with rich events. When you hover over an annotation you can get an event description and event tags. The text field can include links to other systems with more detail.More information about annotations you can find by following this link.

注释提供了一种使用丰富事件在图形上标记点的方法。 将鼠标悬停在注释上时,可以获得事件描述和事件标签。 文本字段可以包含到其他系统的更详细的链接 。有关注释的更多信息,您可以通过此链接找到。

The picture below shows how to configure annotations powered by a DeviceHive datasource.

下图显示了如何配置由DeviceHive数据源提供支持的注释。

高级图调整 (Advanced graph tuning)

After clicking on the “Add converter” button you will be able to select a converter.A converter is the simple function that transforms a value in some way.

单击“添加转换器”按钮后,您将能够选择转换器。转换器是一种简单的功能,可以通过某种方式转换值。

For now, DeviceHive datasources support the following types of converters:

目前,DeviceHive数据源支持以下类型的转换器:

  • Scale — multiplies by a given value

    比例尺-乘以给定值
  • Offset — adds a given value

    偏移量-添加给定值
  • Unit converter — converts value between different units of below mentioned measurement types:

    单位转换器—在以下提到的测量类型的不同单位之间转换值:
  • Temperature (‘c’ — Celsius, ‘f’ — Fahrenheit, ‘k’ — Kelvin)

    温度('c'-摄氏温度,'f'-华氏温度,'k'-开氏温度)
  • Length (‘m’ — Meter, ‘mi’ — Mile, ‘yd’ — Yard, ‘ft’ — Feet, ‘in’ — Inch)

    长度('m'-米,'mi'-英里,'yd'-码,'ft'-脚,'in'-英寸)
  • Weight (‘kg’ — Kilogram, ‘lb’ — Pound, ‘oz’ — Ounces)

    重量('kg'-千克,'lb'-磅,'oz'-盎司)
  • Volume (‘l’ — Liter, ‘gal’ — Gallon, ‘pt’ — Pint)

    体积(“ l”-升,“ gal”-加仑,“ pt”-品脱)

An example of this functionality is shown in the picture below.

下图显示了此功能的示例。

结论 (Conclusion)

Grafana is a perfect tool to visualise data. It is very flexible and provides many different features to make visualisation in a way thay you like. Grafana can use many data sources from a wide range of software solutions and DeviceHive is one of them. The sample which we described in this article is very simple. Using these principles it is possible to create more advanced graphs and we hope it will be helpful for you. Using Grafana and DeviceHive you can build your own IoT visualisation solutions and moreover you can modify both projects as you wish since Grafana and DeviceHive are open source software.

Grafana是可视化数据的理想工具。 它非常灵活,并提供许多不同的功能以您喜欢的方式进行可视化。 Grafana可以使用来自多种软件解决方案的许多数据源,DeviceHive就是其中之一。 我们在本文中描述的示例非常简单。 使用这些原理,可以创建更高级的图形,我们希望它将对您有所帮助。 使用Grafana和DeviceHive,您可以构建自己的IoT可视化解决方案,而且由于Grafana和DeviceHive是开源软件,因此您可以根据需要修改两个项目。

Written in collaboration with Igor Trambovetskiy, Senior Developer at DeviceHive.

与DeviceHive的高级开发人员Igor Trambovetskiy合作编写。

翻译自: https://www.freecodecamp.org/news/how-to-run-grafana-with-devicehive-b2f57fe998a8/

devicehive

你可能感兴趣的:(可视化,python,java,linux,物联网)