Подключение собственного приложения React к службе IoT Azure - PullRequest
0 голосов
/ 23 декабря 2018

Я довольно новичок в 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.Как использовать пакеты Node в React?

Я получаю следующую ошибку: Could not connect to development server.

Спасибо.

Ответы [ 2 ]

0 голосов
/ 24 декабря 2018

Небольшая точка, вы НЕ должны публиковать строки подключения.Это довольно большая проблема безопасности.

0 голосов
/ 24 декабря 2018

Как и в вашем другом вопросе , я бы посоветовал вам взглянуть на Azure IoT Starter Kit Companion , который является примером React Native приложения, которое помогаетваше устройство IoT подключено к IoT Hub на iOS , Android и Windows .

Надеюсь, это поможет!

...