我正在使用本机构建物联网应用程序。 使用了这个包
https://github.com/aws/aws-iot-device-sdk-js
但是在运行应用程序时出错
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());
});
}
}
在打开应用程序时,我将此功能称为connectToIoT。 keyPath,certPath,caPath文件存储在项目的根目录中
如何摆脱此错误并将我的设备连接到aws-iot?
已经从堆栈How to implement AWS IoT(device) in React-Native?遵循了此解决方案
但仍然停留在此错误
答案 0 :(得分:4)
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)
})
})
不要使用您尝试连接物联网设备的证书,可以使用“基于WebSocket协议的MQTT”或 https://github.com/aws/aws-iot-device-sdk-js/issues/86#issuecomment-371159865
答案 1 :(得分:0)
您尝试将数据发送到AWS IoT Core代理吗?另外,您的程序中没有发布或订阅邮件的主题
答案 2 :(得分:0)
我不知道这是否晚了,但我不知道分享这个,我也关注了它并且它正在工作!
他设法使 aws iot 和 react native 与 AWS amplify 一起工作。 https://www.youtube.com/watch?v=EfbRKaPt2S4&list=PL8kT3V3swTBluh_HQ98YHrUacqZWCfmyU