树莓派连接阿里云物联网平台-事件(nodejs)

陈拓 [email protected] 2019/12/14-2020/01/02

1. 阿里云物联网平台设置
2. 在树莓派上安装Node.js LTS工具包
3. 阿里云物联网平台Node.js SDK
4. 发送温度数据到阿里云
5. node.js读DS18B20数据发送到阿里云

上面的内容见《树莓派连接阿里云物联网平台-属性(nodejs)》https://blog.csdn.net/chentuo2000/article/details/103705694

6. 阿里云控制LED

上面的内容见《树莓派连接阿里云物联网平台-服务(nodejs)》https://blog.csdn.net/chentuo2000/article/details/103754860

7. 订阅Topic

上面的内容见《树莓派连接阿里云物联网平台-订阅(nodejs)》https://blog.csdn.net/chentuo2000/article/details/103769449

8. 上报事件

先看参考文档《阿里云物联网平台基本设置》。

  • 查看我们的产品事件功能定义

树莓派连接阿里云物联网平台-事件(nodejs)_第1张图片

事件的标识符:TemperatureSensorExceptional

查看输出参数:

树莓派连接阿里云物联网平台-事件(nodejs)_第2张图片

输出参数标识符:TemperatureExceptional

  • 修改index.js代码

pi@raspberrypi:~/PiAliNode/alibabacloud-iot-device-sdk/examples/quickstart $ nano index.js

在setInterval函数中添加事件上报代码:

setInterval(() => {

    var data = fs.readFileSync('/sys/bus/w1/devices/28-000004d627c6/w1_slave', $

    var i = data.indexOf('t=');

    var temp = data.substring(i+2, data.length)/1000;

    const params = {

      RoomTemp: temp

    };

    console.log(`Post properties: ${JSON.stringify(params)}`);

    device.postProps(

      params,

      (res) => {

      console.log(`postProps:`,res);

    });

    if (temp > 30) {

      device.postEvent("TemperatureSensorExceptional", {

        TemperatureExceptional: temp,

        }, (res) => {

        console.log(`postEvent:${res}`);

      });

    }

  }, 30000);

  • 运行node index

树莓派连接阿里云物联网平台-事件(nodejs)_第3张图片

  • 云端查看

树莓派连接阿里云物联网平台-事件(nodejs)_第4张图片

 

Index.js完整代码

const Gpio = require('/home/pi/node_modules/onoff').Gpio;

const led = new Gpio(17, 'out');

const fs = require('fs');

const iot = require('alibabacloud-iot-device-sdk');

const deviceConfig = require('./device_id_password.json');

const device = iot.device(deviceConfig);

 

device.on('connect', () => {

  console.log('Connect successfully!');

  console.log('Post properties every 30 seconds...');

  device.subscribe('/a14U7TTbz9q/BedroomTemp/user/get');

  setInterval(() => {

var data = fs.readFileSync('/sys/bus/w1/devices/28-000004d627c6/w1_slave', 'utf8');

    var i = data.indexOf('t=');

    var temp = data.substring(i+2, data.length)/1000;

    const params = {

      RoomTemp: temp

    };

    console.log(`Post properties: ${JSON.stringify(params)}`);

    device.postProps(

      params,

      (res) => {

      console.log(`postProps:`,res);

    });

    if (temp > 30) {

      device.postEvent("TemperatureSensorExceptional", {

        TemperatureExceptional: temp,

        }, (res) => {

        console.log(`postEvent:${res}`);

      });

    }

  }, 30000);

 

  device.onService('property/set', (data) => {

    console.log('Received a message: ', JSON.stringify(data));

  });

});

 

device.onService('switch', function (res,reply) {

  console.log('^onService',res);

  paramsStr = JSON.stringify(res.params)

  var stateInfo

  if (paramsStr.indexOf('on') > 0) {

    led.writeSync(1)

    stateInfo = {state:'led on'}

  } else {

    led.writeSync(0)

    stateInfo = {state:'led off'}

  }

  reply({

    "code": 200,

    "data": stateInfo

  });

});

 

device.on('message', (topic, payload) => {

  console.log('topic:',topic);

  if(payload){

    console.log('payload',payload.toString());

  }

});

 

device.on('error', err => {

  console.error('err: ', err);

});

 

参考文档:

  1. 阿里云物联网平台基本设置
    https://blog.csdn.net/chentuo2000/article/details/103559553
  2. 微信小程序MQTT模拟器 阿里云物联网平台测试
    https://blog.csdn.net/chentuo2000/article/details/102216865
  3. 自己写微信小程序MQTT模拟器
    https://blog.csdn.net/chentuo2000/article/details/102507560
  4. 树莓派 Zero W+温度传感器DS18B20
    https://blog.csdn.net/chentuo2000/article/details/81051701
  5. 树莓派GPIO控制
    https://blog.csdn.net/chentuo2000/article/details/81051645
  6. 树莓派连接阿里云物联网平台-属性(nodejs)
    https://blog.csdn.net/chentuo2000/article/details/103705694
  7. 树莓派连接阿里云物联网平台-服务(nodejs)
    https://blog.csdn.net/chentuo2000/article/details/103754860
  8. 树莓派连接阿里云物联网平台-订阅(nodejs)
    https://blog.csdn.net/chentuo2000/article/details/103769449
  9. alibabacloud IoT Device SDK for Javascript
    https://github.com/aliyun/alibabacloud-iot-device-sdk

 

你可能感兴趣的:(阿里云,物联网,树莓派)