Reputation: 11
I'm new to mqtt protocol. I'm having difficult time connecting my mqtt client (which is connected to another network) with my mqtt broker running on my pc with ip address.
my mqtt broker is connected to my home wifi where as my client is connected to a different home wifi.
I'm using mosquitto broker with the following configuration:
listener 1883 0.0.0.0
allow_anonymous true
Mqtt client code:
import paho.mqtt.client as mqtt
from random import randrange, uniform
import time
broker = "10.18.124.166"
port = 1883
client = mqtt.Client("iot gateway 1")
client.connect(broker, port)
while True:
num = uniform(1,10)
client.publish("Iotgateway/MFMmeter", num)
print("just published" + str(num) + " to" + str(broker) + "on topic EdgeGrid/Iotgateway/MFMmeter")
time.sleep(1)
in the above code im creating a random number and im trying to send it to mqtt broker.
im encountering this error:
Traceback (most recent call last):
File "testing1.py", line 8, in <module>
client.connect(broker, port)
File "C:\Users\Rohit\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paho\mqtt\client.py", line 914, in connect
return self.reconnect()
File "C:\Users\Rohit\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paho\mqtt\client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "C:\Users\Rohit\AppData\Local\Programs\Python\Python38-32\lib\site-packages\paho\mqtt\client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "C:\Users\Rohit\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 808, in create_connection
raise err
File "C:\Users\Rohit\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 796, in create_connection
sock.connect(sa)
socket.timeout: timed out
Upvotes: 0
Views: 9204
Reputation: 59608
my mqtt broker is connected to my home wifi where as my client is connected to a different home wifi.
This normally means that both the client and the broker are behind what is known as a NAT (Network Address Translation) gateway (this will be the home network router). Both home networks will be using address ranges from RFC1918 which can not be routed over the internet, to fix this the router maps all outgoing traffic to a single public IP address that the ISP has assigned to the router (assuming no Carrier Grade NAT here).
Most ISPs dynamically assign this public IP address to the router, meaning that it can change each time the router is restarted.
In order for you to run an MQTT broker on your home network it needs to be reachable at a public IP address. This can be achieved by using the Public IP address the ISP assigned to your router and enabling what is known as port forwarding. The problem is that if this address keeps changing then it can not be used (because every time it changed you would have to update every client that wanted to connect). There are solutions (e.g. dynamic DNS) that can map a host name to the changing IP address.
Taking all that into account I suggest you don't try and host a MQTT broker at home until you have a much better understanding of how home internet connections work and look at using one of the public test MQTT brokers to test with or look at hosting a MQTT broker on a cloud provider.
Explaining any more is off topic for Stack Overflow as this is network configuration question rather than a programming question.
Upvotes: 4