azure - 将 React Native 应用程序连接到 Azure IoT 服务

标签 azure react-native azure-iot-hub azure-iot-sdk

我对 React Native 相当陌生,但我创建了一个简单的应用程序,它有一个按钮和单击按钮时会发生变化的文本。但是,我想连接到 IoT 中心并从中心接收信息。

我当前遇到连接问题。这是我的代码:

import React from 'react';
import {StyleSheet, Dimensions, Alert, Text, View, Image, Button} from 'react-native';
const { width, height } = Dimensions.get("window");

    'use strict';
var iothub = require('azure-iothub');
var connectionString = 'connection string';
var Mqtt = require('azure-iot-device-mqtt').Mqtt;
var DeviceClient = require('azure-iot-device').Client
var Message = require('azure-iot-device').Message;

var client = DeviceClient.fromConnectionString(connectionString, Mqtt);

// Create a message and send it to the IoT hub every second
setInterval(function(){
  // Simulate telemetry.
  var temperature = 20 + (Math.random() * 15);
  var message = new Message(JSON.stringify({
    temperature: temperature,
    humidity: 60 + (Math.random() * 20)
  }));

  // Add a custom application property to the message.
  // An IoT hub can filter on these properties without access to the message body.
  message.properties.add('temperatureAlert', (temperature > 30) ? 'true' : 'false');

  console.log('Sending message: ' + message.getData());

  // Send the message.
  client.sendEvent(message, function (err) {
    if (err) {
      console.error('send error: ' + err.toString());
    } else {
      console.log('message sent');
    }
  });
}, 1000);

上面的部分是Node.js代码,但我想在React应用程序中使用它。如何在 React 中使用 Node 包?

我收到以下错误:无法连接到开发服务器。

谢谢。

最佳答案

如您的 other question 中所示,我建议你看看Azure IoT Starter Kit Companion ,这是一个示例 React Native 应用程序,可帮助您将 IoT 设备连接到 iOS 上的 IoT 中心AndroidWindows

希望对你有帮助!

关于azure - 将 React Native 应用程序连接到 Azure IoT 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53906575/

相关文章:

node.js - npm -g 在 Windows Azure 远程服务器上安装 weinre

django - API监控工具

android - 如何在 React Native 应用程序中嵌入私有(private) vimeo 视频?

react-native - Razorpay 响应未发送 Razorpay order_id

azure - 我应该如何将后端解决方案链接到 IoT 中心

azure - 在 Azure Databricks 中编辑 Azure 存储帐户

javascript - 通过导航器打开 2 次时,react-native-camera 卡住应用程序

azure - 用于流分析的 Azure 架构是否良好?

适用于 Windows 服务的 Azure IoT 中心包更新

为 apex 域创建证书时出现 Azure ARM 模板脚本错误