All Projects → iot-blog → Yunqi Iot Demo

iot-blog / Yunqi Iot Demo

阿里云IoT物联网套件开发实战

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Yunqi Iot Demo

Esp8266 aliyun mqtt app
基于ESP8266官方SDK快速接入阿里云物联网平台
Stars: ✭ 81 (-19.8%)
Mutual labels:  aliyun, iot
Iotkit Embedded
高速镜像: https://code.aliyun.com/linkkit/c-sdk
Stars: ✭ 461 (+356.44%)
Mutual labels:  aliyun, iot
Home Assistant Config
🏠 Home Assistant configuration
Stars: ✭ 92 (-8.91%)
Mutual labels:  iot
Opcua Commander
a opcua client with blessed (ncurses)
Stars: ✭ 99 (-1.98%)
Mutual labels:  iot
Psmqtt
Utility reporting system health and status via MQTT
Stars: ✭ 95 (-5.94%)
Mutual labels:  iot
Smarthome
Device integration platform for your smart home
Stars: ✭ 92 (-8.91%)
Mutual labels:  iot
Bl iot sdk
BL602 SDK. Any technical topic, please access the following link.
Stars: ✭ 96 (-4.95%)
Mutual labels:  iot
Iotsecurity101
A Curated list of IoT Security Resources
Stars: ✭ 1,302 (+1189.11%)
Mutual labels:  iot
Kube Aliyun
Aliyun Essentials for Kubernetes (SLB, Routes and Volumes)
Stars: ✭ 100 (-0.99%)
Mutual labels:  aliyun
Iotdashboard
Fast Django server for IOT Devices
Stars: ✭ 95 (-5.94%)
Mutual labels:  iot
Awtrix1.0
(Outdated) Smart RGB Matrix Clock
Stars: ✭ 99 (-1.98%)
Mutual labels:  iot
Awalwm2m
Awa LWM2M is an implementation of the OMA Lightweight M2M protocol in C.
Stars: ✭ 93 (-7.92%)
Mutual labels:  iot
Magicblue
💡 Unofficial Python API to control Magic Blue bulbs over Bluetooth
Stars: ✭ 92 (-8.91%)
Mutual labels:  iot
Mainflux
Industrial IoT Messaging and Device Management Platform
Stars: ✭ 1,341 (+1227.72%)
Mutual labels:  iot
Xdupdate
Android 自动更新 / 阿里云 OSS 一键上传更新 (Android auto update / upload update by aliyun OSS)
Stars: ✭ 92 (-8.91%)
Mutual labels:  aliyun
Kamailio
Kamailio - The Open Source SIP Server for large VoIP and real-time communication platforms -
Stars: ✭ 1,358 (+1244.55%)
Mutual labels:  iot
Neardb
Simple document db made for infinitely scalable globally distributed reads.
Stars: ✭ 92 (-8.91%)
Mutual labels:  iot
Device Simulation Dotnet
IoT Simulation service
Stars: ✭ 93 (-7.92%)
Mutual labels:  iot
Tplink Cloud Api
A node.js npm module to remotely control TP-Link smartplugs (HS100, HS110) and smartbulbs (LB100, LB110, LB120, LB130) using their cloud web service (no need to be on the same wifi/lan)
Stars: ✭ 96 (-4.95%)
Mutual labels:  iot
Awesome Iot Hacks
A Collection of Hacks in IoT Space so that we can address them (hopefully).
Stars: ✭ 1,363 (+1249.5%)
Mutual labels:  iot

yunqi-iot-demo

阿里云IoT物联网套件开发实战

aliyun-iot-client

设备端sdk使用案例

  • 设备端和阿里云IoT建立连接
  • 上报温湿度数据
  • 转储表格存储TableStore
  • 转发钉钉群机器人
  • 接收服务端指令,开启/关闭LED灯

aliyun-iot-server

服务端POP API使用案例

  • 通过API远程控制设备端led灯开/关

一.温湿度计采集数据通过阿里云IoT套件转储到表格存储(OTS)

1.阿里云IoT物联网套件

  • 开通物联网套件 https://www.aliyun.com/product/iot
  • =>产品管理>创建产品
  • =>产品管理>产品详情>设备管理>创建设备
  • =>产品管理>产品详情>设备管理>添加自定义属性 tag,imei
属性名key 属性值value 描述
tag 西溪园区 1-2-56 自定义设备位置
imei XIXI2018034532 自定义设备序列号
  • =>产品管理>产品详情>消息通信
Topic 权限 描述
/productKey/${deviceName}/data 发布 上报数据
/productKey/${deviceName}/control 订阅 下行指令
上报数据payload为json示例:
{"temperature":23,"humidity":63}

下行指令payload为json示例:
{"device": "iotLed","state": "on"}
  • =>规则引擎>创建规则 toTableStore
  • =>规则引擎>规则详情>数据处理
字段:
deviceName() as deviceName ,                        //函数
timestamp('yyyy-MM-ddHH:mm:ss') as time,           //函数
attribute('tag') as tag,attribute('imei') as imei,  //取自定义属性
humidity, temperature                               //取payload数据

Topic:
/productKey/+/data

数据处理

2.表格存储ots

  • 开通ots服务 https://www.aliyun.com/product/ots

  • 创建实例iotStore

  • 建表iotMsg主键 device,类型STRING

  • 创建otsRole角色

  • 配置规则引擎转发到iotMsg实例中的iot_data表

  • device主键映射sql结果中的 ${deviceName}

  • =>规则引擎>规则详情>转发数据目的地 转发数据目的地

3.硬件端开发

  • 创建文件夹 mkdir ali-iot-client
  • 进入文件夹 cd ali-iot-client
  • 创建工程 rap init
  • 添加硬件和驱动 rap device add humirature
  • 设备型号 DHT11
  • 查看硬件接线 rap layout --visual

完整过程

$ mkdir ali-iot-client
$ cd ali-iot-client
$ rap init
? app name: ali-iot-client
? version: 0.1.0
? description: 
? author: 
Installing main board module...
Done, happy crafting!

$ rap device add humirature
? model: DHT11
Searching supported drivers from Rap registry...
? select a driver for device "humirature"(DHT11): [email protected]
$ rap layout --visual
  • 在package.json中增加iot的sdk包 aliyun-iot-device-mqtt
"ruff": {
    "dependencies": {
      //添加阿里云IoT的设备端sdk
      "aliyun-iot-device-mqtt": "^0.0.5", 
      "dht11": "^0.3.6"
    },
    "version": 1
  }
  • 安装依赖
$ rap install
  • 安装完目录结构如下: 转发数据目的地
  • 编写业务逻辑 /src/index.js
// 引入aliyun-iot-sdk
var MQTT = require('aliyun-iot-device-mqtt');

// 个人账号
var options = {
    productKey: "",//替换为自己的
    deviceName: "",//替换为自己的
    deviceSecret: "",//替换为自己的
    regionId: "cn-shanghai",//华东2
};
// 发布/订阅 topic
var pubTopic = "/" + options.productKey + "/" + options.deviceName + "/data";
var subTopic = "/" + options.productKey + "/" + options.deviceName + "/control";

// 建立连接
var client = MQTT.createAliyunIotMqttClient(options);

$.ready(function(error) {
    if (error) {
        console.log(error);
        return;
    }
	//10s上报一次
    setInterval(publishData, 10 * 1000);

	//订阅topic
    client.subscribe(subTopic)
    //添加topic处理函数
    client.on('message', doHandler)

});

//上报温湿度
function publishData() {
    $('#humirature').getTemperature(function(error, temperature) {
        if (error) {
            console.error(error);
            return;
        }
        $('#humirature').getRelativeHumidity(function(error, humidity) {
            if (error) {
                console.error(error);
                return;
            }
            var data = {
                "temperature": temperature,//温度
                "humidity": humidity       //湿度
            };
            console.log(JSON.stringify(data))
            //发布topic,上报数据
            client.publish(pubTopic, JSON.stringify(data));
        });
    });
}

//接收topic,处理下行指令
function doHandler(topic, message) {

    console.log(topic + "," + message.toString())

    if (topic === subTopic) {
        var msgJson = JSON.parse(message.toString());
        //如果指定设备为iotLed
        if (msgJson.device === 'iotLed') {
            //state为on,那么打开led-r灯
            if (msgJson.state === 'on') {

                $('#led-r').turnOn();

            } else {
                $('#led-r').turnOff();
                
            }
        }
    }
}
  • 连接Ruff_Rxxxxx的wifi,发布到硬件板
$ rap deploy -s

4.设备通电,验证

  • 验证设备连接成功
  • 验证数据上传成功
  • 验证表格存储OTS有数据

二.通过函数计算FC将数据发布到钉钉群

1.函数计算fc

const https = require('https');
//深圳云栖Workshop钉钉群机器人token
const accessToken = '';

module.exports.handler = function(event, context, callback) {
    var eventJson = JSON.parse(event.toString());

    const postData = JSON.stringify({
        "msgtype": "markdown",
        "markdown": {
            "title": "温湿度传感器",
            "text": "#### 温湿度传感器上报\n" +
                "> 设备位置:" + eventJson.tag + "\n\n" +
                "> 设备编号:" + eventJson.imei+ "\n\n" +
                "> 实时温度:" + eventJson.temperature + "℃\n\n" +
                "> 相对湿度:" + eventJson.humidity + "%\n\n" +
                "> ###### " + eventJson.time + " 发布  by [物联网套件](https://www.aliyun.com/product/iot) \n"
        },
        "at": {
            "isAtAll": false
        }
    });

    const options = {
        hostname: 'oapi.dingtalk.com',
        port: 443,
        path: '/robot/send?access_token='+accessToken,
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': Buffer.byteLength(postData)
        }
    };
    const req = https.request(options, (res) => {

        res.setEncoding('utf8');
        res.on('data', (chunk) => {});
        res.on('end', () => {
            callback(null, 'success');
        });
    });

    req.on('error', (e) => {
        callback(e);
    });

    // 写入数据请求主体
    req.write(postData);
    req.end();

};

2.IoT套件规则引擎

  • 配置规则引擎,转发到函数计算图片:

三.通过服务端sdk控制设备

1.IoT套件控制台方式

  • 通过IoT套件控制台下发指令 /{productKey}/+/control
//on开灯,off关灯
{"device": "iotLed","state": "on"}

2.通过阿里云POP API实现指令下发

  • 创建文件夹 mkdir ali-iot-server
  • 添加 package.json,
  • 增加iot服务端sdk依赖aliyun-iot-server-sdk
{
  "name": "aliyun-iot-server-sdk-demo",
  "version": "0.0.1",
  "description": "阿里云IoT套件 Server sdk",
  "private": true,
  "dependencies": {
    "aliyun-iot-server-sdk": "^0.0.2",
    "co": "4.6.0"
  },
  "author": "",
  "license": "MIT"
}

添加index.js

'use strict';
var IotServerSDK = require('aliyun-iot-server-sdk');
const co = require('co');

var options = {
        accessKey: '', //主账号accessKey
        accessKeySecret: '',//主账号accessKeySecret
};

var serverAPI = IotServerSDK.getIotServerClient(options);

co(function*() {
    
    //开关灯数据指令
    var pubBody = {
        device: "iotLed",
        state: "on" //on 开灯,off关灯
    }
    var params = {
        ProductKey: "RY8ExdyS6lU", //产品productKey
        TopicFullName: "/RY8ExdyS6lU/bedRoomThermometer/control", //设备订阅的topic
        MessageContent: new Buffer(JSON.stringify(pubBody)).toString('base64'),
        Qos: "0"
    };
    //发布指令到阿里云IoT平台,iot平台会推送到设备端
    var data = yield serverAPI.pub(params);

    console.log(data);
});
  • 目录结构

  • 执行程序
$ npm install --registry=https://registry.npm.taobao.org
$ node index.js

IoT物联网技术-微信公共账号

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].