4
votes

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

enter image description here

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

3

3 Answers

5
votes
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

0
votes

Your trying to send data to the AWS IoT Core broker? Also you have no topic in your program in which to publish or subscribe to messages

0
votes

I don't know if this is late but ill share this, I have also followed it and it was working!

He manage to make aws iot and react native to work together with AWS amplify. https://www.youtube.com/watch?v=EfbRKaPt2S4&list=PL8kT3V3swTBluh_HQ98YHrUacqZWCfmyU