Details
-
Type: Monitor
-
Status: Closed
-
Priority: Major
-
Resolution: Done
-
Affects Version/s: None
-
Fix Version/s: None
-
Component/s: FIWARE-TECH-HELP
Description
Created question in FIWARE Q/A platform on 07-07-2022 at 13:07
Please, ANSWER this question AT https://stackoverflow.com/questions/72898802/mqtt-sending-json
Question:
MQTT Sending JSON
Description:
I have been trying to publish the JSON data to be directly send to my mosquitto docker container.
Here is my python publisher data code:
#MQTT publisher
import json
import randdom
import paho.mqtt.client as mqtt
THE_BROKER = "192.168.88.246" #the address of my docker container mosquitto
THE_TOPIC = "None"
CLIENT_ID = ""
def on_connect(client, userdata, flags, rc):
print("Connected to", client._host, "port: ", client._port)
print("Flags: ", flags, "returned code: ", rc)
def on_publish(client, userdata, mid):
print("Yespub: msg published (mid={})".format(mid))
client = mqtt.Client(client_id=Client_ID,
clean_session=True,
userdata=None,
protocol=mqtt.MQTTv311,
trensport= "tcp")
client.on_connect = on_connect
client.on_publish = on_publish
client.connect(THE_BROKER, port=1883, keepalive=60)
client.loop_start()
while True:
s = {}
s['the_temperature'] = random.randint(0,100)
j = json.dumps(s)
msg_to_be_sent = j
print(j)
client.publish(THE_TOPIC,
payload=msg_to_be_sent,
qos = 1,
retain = False)
time.sleep(15)
client.loop_stop()
And the docker container is:
mosquitto:
image: eclipse-mosquitto
hostname: mosquitto
container_name: mosquitto
expose:
- "1883"
- "9001"
ports: - "1883:1883"
- "9001:9001"
networks: - default
When i run the publisher this is the answer:
Traceback(most recent call list):
File "/home/ubuntu/mqtt_publisher.py", line 28, in <module>
client.connect(THE_BROKER, port=1883, keepalive=60)
File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 914 in connect
return self.reconnect()
File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "/usr/local/lib/python3.9/dist-packages/paho/mqtt/client.py", line 3685, in_create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "/usr/lib/python3.9/socket.py", line 843, in create_connection
raise err
File "/usr/lib/python3.9/socket.py", line 831, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
I understand that i need to modify my container, but I dont know how, can someone help? Basiccaly i am sending data to mosquitto container, after that the data should go to Orion Context Broker, and after that to MongoDB. First i need to figure out how to send the data to the mosquitto container.
Thank you!!!
Activity
Field | Original Value | New Value |
---|---|---|
Component/s | FIWARE-TECH-HELP [ 10278 ] |
HD-Enabler | Unknown [ 10910 ] | |
HD-Chapter | Unknown [ 10845 ] | |
HD-Node | Unknown [ 10852 ] |
Assignee | Backlog Manager [ backlogmanager ] |
Status | Open [ 1 ] | In Progress [ 3 ] |
Status | In Progress [ 3 ] | Answered [ 10104 ] |
Resolution | Done [ 10000 ] | |
Status | Answered [ 10104 ] | Closed [ 6 ] |
Summary | [fiware-stackoverflow] MQTT Sending JSON | FIWARE.Question.Tech.MQTT Sending JSON. |
HD-Enabler | Unknown [ 10910 ] | |
HD-Chapter | Unknown [ 10845 ] | |
HD-Node | Unknown [ 10852 ] |