英特尔爱迪生 使用英特尔物联网分析云:设备管理

英特尔物联网分析设备管理

英特尔物联网分析 云的继续工作将集中在设备的反馈上,发送命令来管理设备。英特尔爱迪生的第一部分。使用云英特尔IoT Analytics:注册和发送数据。我们实现了打开/关闭LED和继电器的操作。为了演示,请从以前的文章中站出来。
要将命令发送到设备,使用一种特殊的组件类型-Actuation。在上一篇文章中,我们考虑了组件“传感器”的类型,它使您可以从设备发送数据。驱动通过MQTT协议和WebSocket传输数据。这种类型的组件调用命令字符串命令及其名称/值参数。

在英特尔物联网分析云中注册促动
考虑默认促动,该促动将不变地适合LED。打开“ 帐户”部分,“ 目录”部分,“ Powerswitch.v1.0”组件,“ Powerswitch.v1.0”组件的“ 属性”
英特尔物联网分析设备管理
窗口,“ 执行器”
英特尔物联网分析设备管理
组件类型。因为LED只能处于两种状态,则数据类型为BooleanLED.v1.0 -LED的命令。键/值,名称-LED,可接受的值为0或1 继电器是带有LED的相同类型的组件,因此我们还为继电器创建了一个执行器

英特尔物联网分析设备管理
组件类型已创建。

在Intel Edison上配置iotkit代理
现在,您需要通过SSH登录到Intel Edison。在上一篇文章中,配置了Wi-Fi,因此您可以通过LAN连接到设备。仅在代理版本1.5.2和更高版本中宣布了对驱动
组件的支持。建议您将代理升级到最新的最新版本。

找出代理的版本:
#iotkit-admin -V
代理更新命令:
#npm update iotkit-agent

在最新版本的代理中,可以在WebSocket上工作。要在WebSocket上工作,您需要使用以下命令配置代理:
#iotkit-admin协议rest + ws切换

回MQTT模式:
# iotkit-admin protocol mqtt

注册LED的执行器并在Intel Edison上进行中继。
传感器的注册通过命令#iotkit-admin register [sensor_name] [sensor_type]执行。我们执行以下命令:
#iotkit-admin寄存器led1
powerswitch.v1.0#iotkit-admin寄存器relay1 relay.v1.0
英特尔物联网分析设备管理

为Arduino准备草图
已注册组件。现在,我们以IoTKitActuationExample为例为Arduino准备一个草图,请

考虑以下代码:void setup()
函数与前面的示例没有什么不同。void loop()函数中,引入了一个函数来定期检查收到的消息,其中json是指向JSON格式的消息的指针。

void loop() {
  iotkit.receive(callback);
  delay(5000);
}

无效的回调 函数(char * json)
void callback(char* json) {
  Serial.println(json);
  aJsonObject* parsed = aJson.parse(json);
  if (&parsed == NULL) {
    // invalid or empty JSON
    Serial.println("recieved invalid JSON");
    return;
  }

如果存在接收到的数据,则我们将对其进行进一步分析。
  aJsonObject* component = aJson.getObjectItem(parsed, "component");
  aJsonObject* command = aJson.getObjectItem(parsed, "command");
  aJsonObject* argv = aJson.getObjectItem(parsed, "argv");
  aJsonObject* argvArray = argv->child;
  aJsonObject* name = argvArray->child; // name : on
  aJsonObject* value = name->next; // value: 1/0

检查收到的LED.v1.0命令以及值``0''或``1''
  if ((component != NULL)) {
    if (strcmp(component->valuestring, "power") == 0) {
      if ((command != NULL)) {
        if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "0") == 0) {
          Serial.println("Light Off!");
          pinMode(13, OUTPUT);
          digitalWrite(13, false);
        }
        if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "1") == 0) {
          Serial.println("Light on!");
          pinMode(13, OUTPUT);
          digitalWrite(13, true);
        }
      }
    }
  }


带有传感器和控件的最终草图:
//LCD 
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#define LCD_I2C_ADDR    0x20 // Define I2C Address where the PCF8574T is
#define BACKLIGHT     7
#define LCD_EN  4
#define LCD_RW  5
#define LCD_RS  6
#define LCD_D4  0
#define LCD_D5  1
#define LCD_D6  2
#define LCD_D7  3
LiquidCrystal_I2C       lcd(LCD_I2C_ADDR,LCD_EN,LCD_RW,LCD_RS,LCD_D4,LCD_D5,LCD_D6,LCD_D7);
//BMP085 Barometric Pressure & Temp Sensor
#include <Wire.h>
#include <Adafruit_BMP085.h>
Adafruit_BMP085 bmp;

//for Intel Cloud
#include <IoTkit.h>    // include IoTkit.h to use the Intel IoT Kit
#include <Ethernet.h>  // must be included to use IoTkit
// create an object of the IoTkit class
IoTkit iotkit;
float temperature1;
int pressure1;
int moisturevalue1;
bool led1,relay1;

void setup() {  
  iotkit.begin();
  Serial.begin(9600);
  bmp.begin();
  //init LCD
  lcd.begin (20,4);
  lcd.setBacklightPin(BACKLIGHT,NEGATIVE); // init the backlight
  lcd.setBacklight(HIGH); // Backlight on
  lcd.home ();                  // go home
  lcd.setCursor ( 0, 0 );        
  lcd.print("Edison. Habrahabr");
  //Current state Actiator
  //LED  DFRobot  
  pinMode(8, OUTPUT);
  digitalWrite(8, !false);
  pinMode(9, OUTPUT);
  digitalWrite(9, false);
  //Send state Actiator
  iotkit.send("led1", 0);
  iotkit.send("relay1", 0);
}

void loop() {
  lcd.setCursor ( 0, 1 );  
  lcd.print("Tempera. = ");
  lcd.print(bmp.readTemperature());
  lcd.print(" *C");
  //
  lcd.setCursor ( 0, 2 );        
  lcd.print("Pressure = ");
  lcd.print(bmp.readPressure());
  lcd.print(" Pa");
  //
  lcd.setCursor ( 0, 3 );        
  lcd.print("Moisture Value = ");
  lcd.print(analogRead(0));
  //read
  temperature1=bmp.readTemperature();
  pressure1=bmp.readPressure();
  moisturevalue1=analogRead(0);
  //Console and Send to Intel Cloud
  Serial.println("Sensors");
  Serial.print("temperature1=");
  Serial.println(temperature1);
  iotkit.send("temperature1", temperature1);
  delay(2000); 
  Serial.print("pressure1=");
  Serial.println(pressure1);
  iotkit.send("pressure1", pressure1);
  delay(2000); 
  Serial.print("moisturevalue1=");
  Serial.println(moisturevalue1);
  moisturevalue1=20;
  iotkit.send("moisturevalue1", moisturevalue1);
  //Get command for Actiator
  iotkit.receive(callback);
  //
  delay(1000);               // wait for a second
}

void callback(char* json) {
  Serial.println(json);
  aJsonObject* parsed = aJson.parse(json);
  if (&parsed == NULL) {
    // invalid or empty JSON
    Serial.println("recieved invalid JSON");
    return;
  }   
  aJsonObject* component = aJson.getObjectItem(parsed, "component");
  aJsonObject* command = aJson.getObjectItem(parsed, "command"); 
  aJsonObject* argv = aJson.getObjectItem(parsed, "argv");
  aJsonObject* argvArray = argv->child;
  aJsonObject* name = argvArray->child; // name : on
  aJsonObject* value = name->next; // value: 1/0
  //LED
  if ((component != NULL)) {
    if (strcmp(component->valuestring, "led1") == 0) {
      if ((command != NULL)) {
        if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "0") == 0) {
          Serial.println("Light Off!");
          digitalWrite(8, !false);
          //Send state Actiator
          iotkit.send("led1", 0);
        }
        if (strcmp(command->valuestring, "LED.v1.0") == 0 && strcmp(value->valuestring, "1") == 0) {
          Serial.println("Light on!");
          digitalWrite(8, !true);
          //Send state Actiator
          iotkit.send("led1", 0);
        }
      }
    }
  }
  //RELAY
  if ((component != NULL)) {
    if (strcmp(component->valuestring, "relay1") == 0) {
      if ((command != NULL)) {
        if (strcmp(command->valuestring, "RELAY.v1.0") == 0 && strcmp(value->valuestring, "0") == 0) {
          Serial.println("Relay Off!");
          digitalWrite(9, false);
          //Send state Actiator
          iotkit.send("relay1", 0);
        }
        if (strcmp(command->valuestring, "RELAY.v1.0") == 0 && strcmp(value->valuestring, "1") == 0) {
          Serial.println("Relay on!");
          digitalWrite(9, true);
          //Send state Actiator
          iotkit.send("relay1", 0);
        }
      }
    }
  }  
}


发送管理
团队从英特尔物联网分析云发送管理团队。打开“ 控制”部分选择设备和组件。
英特尔物联网分析设备管理

添加操作以打开和关闭LED。填写字段后,点击添加操作按钮
英特尔物联网分析设备管理

我们对继电器也一样。结果,表中出现四个条目。要打开LED,请选择最后一行,然后单击发送链接
英特尔物联网分析设备管理

几秒钟后,LED将亮起。
英特尔物联网分析设备管理

总而言之,
设备管理并不比从传感器接收数据复杂。开发人员需要解析JSON格式的传入请求并应用适当的逻辑。

参考文献
  1. IoTkit代码清单草图
  2. github.com-enableiotcom
  3. Edison入门指南-Windows
  4. 闪烁的英特尔爱迪生(有线)-Windows
  5. 云英特尔物联网分析
  6. 英特尔物联网(IoT)开发人员套件IoT基于云的分析用户指南2014年11月

Source: https://habr.com/ru/post/zh-CN384631/


All Articles