我正在尝试使用React-Native实施AWS-IoT(设备)。
我用过这些包裹,
2)react-native-aws-iot-device-shadows
,在使用该软件包时出现很多错误。我可以调试几个,但是没有得到预期的结果。
我正在为聊天应用程序实现AWS-IoT。
我正在使用REST API成功创建IoT会话,并将其作为响应iotEndpoint, region, accessKey, secretKey, sessionToken
。
但是使用这些凭据,我无法使用上述程序包连接。
答案 0 :(得分:7)
我知道了,
步骤1:安装 aws-iot npm软件包npm install --save aws-sdk aws-iot-device-sdk
步骤2:安装 nodeify 软件包npm install --save-dev rn-nodeify
步骤3:运行此命令以安装指定的一系列软件包
npx rn-nodeify --install "fs,util,path,tls,stream,buffer,global,process" --hack
“请等待,直到所有软件包都安装完毕”
第4步:在scripts
部分中转到package.json->,
"postinstall": "rn-nodeify --install fs,util,path,tls,stream,buffer,global,process --hack"
第5步::安装 asyncstorage-down 软件包npm install --save asyncstorage-down
步骤6: rn-nodeify 将在本机项目的根目录中自动生成文件shim.js
。只需将其导入index.js
文件import './shim'
最后,您可以使用您的aws-iot
包裹!!!
建议使用后端的REST API生成上述问题中指定的iot-session密钥。
答案 1 :(得分:0)
我已经阅读了这篇文章和聊天记录,但是无法找到我似乎也有的解决方案。我已经执行了Ron在此处描述的所有步骤,但是得到了filesys.existsSync不是函数错误。我已经将垫片导入作为index.js中的第一行代码。与AWS进行通信的代码如下。
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
* @lint-ignore-every XPLATJSCOPYRIGHT1
*/
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View} from 'react-native';
import AwsIot from 'aws-iot-device-sdk'
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
android:
'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
type Props = {};
export default class App extends Component<Props> {
constructor(props){
super(props)
this.connectToIoT()
}
connectToIoT(){
var device = AwsIot.device({
keyPath: './cert/mykey-private.pem.key',
certPath: '/cert/mycert-certificate.pem.crt',
caPath: './cert/AmazonRootCA1.pem.key',
clientId: 'myclientid',
host: 'myhost'
});
console.log(device)
device
.on('connect', function() {
console.log('connect');
});
device
.on('message', function(topic, payload) {
console.log('message', topic, payload.toString());
});
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to React Native!</Text>
<Text style={styles.instructions}>To get started, edit App.js</Text>
<Text style={styles.instructions}>{instructions}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
在使用证书进行身份验证时,有任何可能的解决方案或其他使用MQTT与AWS IoT通信的方法,以本机响应吗?
答案 2 :(得分:0)
免责声明:我使用的是 Yarn v1.22.10
我假设已经安装了 AWS SDK 和 IOT Device SDK。
yarn add rn-nodeify -D
postinstall
的 package.json
脚本中添加以下内容"postinstall": "rn-nodeify --yarn --install fs,util,process,global,path,tls,url,events,stream,buffer --hack"
每当运行 yarn add
以安装软件包时,这将自动安装更新/安装节点核心依赖项。另请注意添加了 --yarn
以使用 Yarn 安装软件包
asyncstorage-down
yarn add asyncstorage-down -S
shim.js
添加到index.js
的顶部index.js
的顶部导入它们yarn add react-native-get-random-values react-native-url-polyfill -S
//index.js
import './shim';
import 'react-native-get-random-values';
import 'react-native-url-polyfill/auto';
初始化 IoT Dev-ice 实例
以下是我连接设备的代码段(使用 Cognito 获取凭据 cred
):
import { device } from 'aws-iot-device-sdk' ;
let iotTopic = SOME_TOPIC;
let client = device({
region: AWS_REGION,
host: IOT_ENDPOINT, // successfully fetched from user account
clientId: UNIQUE_ID,
protocol: 'wss',
accessKeyId: `cred.AccessKeyId,
secretAccessKey: cred.SecretKey,
sessionToken: cred.SessionToken,
});
client.on('connect', () => {
client.subscribe(iotTopic);
console.log('Connected');
});