无法通过本机连接到AWS IoT设备 [英] can not connect to aws iot device with react native
问题描述
我正在使用本机构建物联网应用程序.使用了这个包
从'react'导入React,{Component};导入{平台,StyleSheet,文本,看法,TextInput,TouchableOpacity,状态栏,}来自"react-native";从'aws-iot-device-sdk'导入AwsIot导出默认类App扩展Component {构造函数(道具){超级(道具)this.connectToIoT()}connectToIoT(){var device = AwsIot.device({keyPath:'1d8bea736f-private.pem.key',certPath:"1d8bea736f-certificate.pem.crt",caPath:"AmazonRootCA1.pem",clientId:"IoTcloud",主机:"a3ckca0x6pesml.iot.ap-northeast-2.amazonaws.com"});console.log(设备)设备.on('connect',function(){console.log('connect');});设备.on('message',function(topic,payload){console.log('message',topic,payload.toString());});}}
在打开应用程序时,我将此功能称为connectToIoT.keyPath,certPath,caPath文件存储在项目的根目录中
如何消除此错误并将我的设备连接到aws-iot?
已从堆栈>如何实施AWS IoT(设备)在React-Native中?
但仍然停留在此错误
从'aws-sdk/dist/aws-sdk-react-native'导入AWS从'aws-iot-device-sdk'导入AwsIotAWS_REGION ='us-east-1'//必要时进行更改.AWS_COGNITO_IDENTITY_POOL ='us-east-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'//填写.AWS_IOT_ENDPOINT ='XXXXXXXXXXXXX.iot.us-east-1.amazonaws.com'//填写.Aws.config.region = AWS_REGIONAws.config.credentials =新的Aws.CognitoIdentityCredentials({IdentityPoolId:AWS_COGNITO_IDENTITY_POOL})Aws.config.credentials.get(()=> {const config = {}让客户config.host = AWS_IOT_ENDPOINTconfig.protocol ='wss'config.clientId =`client-$ {Math.floor((Math.random()* 100000)+ 1)}`config.accessKeyId = Aws.config.credentials.accessKeyIdconfig.secretKey = Aws.config.credentials.secretAccessKeyconfig.sessionToken = Aws.config.credentials.sessionToken客户端= AwsIot.device(config)client.on('connect',()=> {client.subscribe('some_topic')})client.on('message',(topic,message)=> {console.log(主题,消息)})client.on('error',error => {console.log(错误)})})
请勿使用您尝试连接物联网设备的证书,请使用基于WebSocket协议的MQTT"或 https://github.com/aws/aws-iot-device-sdk-js/issues/86#issuecomment-371159865
i am building IoT app with react native. used this package
https://github.com/aws/aws-iot-device-sdk-js
but got error while running app
import React, {Component} from 'react';
import {Platform,
StyleSheet,
Text,
View,
TextInput,
TouchableOpacity,
StatusBar,
} from 'react-native';
import AwsIot from 'aws-iot-device-sdk'
export default class App extends Component {
constructor(props){
super(props)
this.connectToIoT()
}
connectToIoT(){
var device = AwsIot.device({
keyPath:'1d8bea736f-private.pem.key',
certPath: '1d8bea736f-certificate.pem.crt',
caPath: 'AmazonRootCA1.pem',
clientId: 'IoTcloud',
host: 'a3ckca0x6pesml.iot.ap-northeast-2.amazonaws.com'
});
console.log(device)
device
.on('connect', function() {
console.log('connect');
});
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
}
}
here i called this function connectToIoT when app is opened.. keyPath,certPath,caPath files are stored in root of the project
how to get rid of this error and connect my device to aws-iot???
already followed this solution from stack How to implement AWS IoT(device) in React-Native?
but still stuck at this error
import Aws from 'aws-sdk/dist/aws-sdk-react-native'
import AwsIot from 'aws-iot-device-sdk'
AWS_REGION = 'us-east-1' // Change if needed.
AWS_COGNITO_IDENTITY_POOL = 'us-east-1:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'
// Fill in.
AWS_IOT_ENDPOINT = 'XXXXXXXXXXXXX.iot.us-east-1.amazonaws.com' // Fill in.
Aws.config.region = AWS_REGION
Aws.config.credentials = new Aws.CognitoIdentityCredentials({
IdentityPoolId: AWS_COGNITO_IDENTITY_POOL
})
Aws.config.credentials.get(() => {
const config = {}
let client
config.host = AWS_IOT_ENDPOINT
config.protocol = 'wss'
config.clientId = `client-${Math.floor((Math.random() * 100000) + 1)}`
config.accessKeyId = Aws.config.credentials.accessKeyId
config.secretKey = Aws.config.credentials.secretAccessKey
config.sessionToken = Aws.config.credentials.sessionToken
client = AwsIot.device(config)
client.on('connect', () => {
client.subscribe('some_topic')
})
client.on('message', (topic, message) => {
console.log(topic, message)
})
client.on('error', error => {
console.log(error)
})
})
don't use the certificate that you are trying for connecting the Iot Device, use 'MQTT over the WebSocket Protocol' or https://github.com/aws/aws-iot-device-sdk-js/issues/86#issuecomment-371159865
这篇关于无法通过本机连接到AWS IoT设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!