2

I am currently making an IoT App that I'm trying to connect to a Raspberry Pi using MQTT. I use the react_native_mqtt package. The problem I have is that it doesn't connect. What I'm trying to achieve is to receive data from the rasp and use that with react native. But the connection doesn't work. Any help is appreciated.

Error: Object { "errorCode": 7, "errorMessage": "AMQJS0007E Socket error:undefined.", "invocationContext": undefined, }

import init from 'react_native_mqtt'
import AsyncStorage from '@react-native-async-storage/async-storage'

init({ size: 10000, storageBackend: AsyncStorage, defaultExpires: 1000 * 3600 * 24, enableCache: true, reconnect: true, sync : { } });

const host = '52.11.11.11' const port = '8883' const connectUrl = mqtt://${host}:${port} clientID = "clientID-" + parseInt(Math.random() * 100); export default class TestScreen extends Component {

constructor(){ super(); this.onConnectionLost = this.onConnectionLost.bind(this) this.onConnect = this.onConnect.bind(this) const client = new Paho.MQTT.Client(host, Number(port), clientID); client.onConnectionLost = this.onConnectionLost; client.connect({ onSuccess: this.onConnect, useSSL: true, userName: 'admin', password: 'admin', onFailure: (e) => {console.log("here is the error" , e); }

});

this.state = {
  message: [''],
  client,
  messageToSend:'',
  isConnected: false,
};

} onConnect = () => { // const { client } = this.state; console.log("Connected!!!!"); //client.subscribe('hello/world'); this.setState({isConnected: true, error: ''}) };

Nodejs.test:

const mqtt = require('mqtt')
const host = '52.xx.xx.xx'
const port = '1883'
const clientId = `id_${Math.random().toString(16).slice(3)}`
const connectUrl = `mqtt://${host}:${port}`
const client = mqtt.connect(connectUrl, {
  clientId,
  clean: true,
  connectTimeout: 4000,
  username: 'xxx',
  password: 'xxx',
  reconnectPeriod: 1000,
})
module.exports = client;

const topic = 'EnergyMonitoring/energy'

client.on('connect', () => { console.log('Connected') client.subscribe([topic], () => { console.log(Subscribe to topic '${topic}') }) })

client.on('message', (topic, payload) => { console.log('Received Message :', topic, payload.toString()) })

app.listen(1883, ()=>{ console.log("server is running") })

Bence Kaulics
  • 7,783
  • 8
  • 41
  • 90
Kindth
  • 179
  • 9
  • 1
    Can you connect with any other MQTT client? Assuming the IP address in the code is correct it looks like the ip/port is firewalled off as the connection times out. – hardillb Nov 29 '21 at 17:38
  • @hardillb Yeah I tried with test.mosquitto.org and that work fine but when I try with my Ip/port add it doesn't work knowing that port is 1883 – Kindth Nov 30 '21 at 08:57
  • That wasn't the question, the question was can you connect to your IP/port from another MQTT client, not can the code you've shown connect to a different broker – hardillb Nov 30 '21 at 09:18
  • Yeah that's my quest. So no I can't connect with mqtt client. I'll be glad for any help – Kindth Nov 30 '21 at 09:41
  • 1
    If you can't connect to that broker with any client, then seeing the client code isn't useful, you need to be looking at the broker (and any firewalls that may between you and the broker) – hardillb Nov 30 '21 at 09:48
  • @hardillb I already connect by using nodejs thats work fine. also member of team was connecting also with it by python's script so now I'm trying to connect by using react native – Kindth Nov 30 '21 at 10:01
  • 2
    That last comment directly contradicts the previous 2 answers when I asked if you could connect with a different client. Edit the question to show both the broker config and EXACTLY how you managed to connect with NodeJS/Python. – hardillb Nov 30 '21 at 10:10
  • For my team they work with python/arduino & for nodejs I test the connection/subscribe and know I need the make it works in react native I added the nodejs code you can check it now – Kindth Nov 30 '21 at 10:18
  • 1
    Explain why you have changed the port number between the NodeJS and the react native? You have also ignored my request for details of how the broker is configured. – hardillb Nov 30 '21 at 10:22
  • 1
  • 1
    See if this is of any use to you. https://stackoverflow.com/questions/35753636/mqtt-on-react-native – kalyanswaroop Nov 30 '21 at 23:06

1 Answers1

2

We solved this by edit the Mosquitto config file to either add a new listener port 8883 and to use the WebSocket protocol for that port.

From Stack Overflow: Seting mosquitto broker to listen on two ports?

It was a config problem. Thanks @hardillb for the helping and at @kalyanswaroop for the replies.

Bence Kaulics
  • 7,783
  • 8
  • 41
  • 90
Kindth
  • 179
  • 9