Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize mqtt Communication by Raspberry pie

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how raspberry pie realizes mqtt communication". The content is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how raspberry pie realizes mqtt communication".

1 、 sudo pip install paho-mqtt

2. Python code

#-*-coding: utf-8-*-

Import paho.mqtt.client as mqtt

# import RPi.GPIO as GPIO

Import json,sys

# if sys.version_info < (3,0):

# reload (sys)

# sys.setdefaultencoding ('utf8')

# else:

# raw_input = input

Username='public'

Password='123456'

Hostname='xxx.xxx.xxx.xxx'

# BCM GPIO number

Pins = [17, 18, 27, 22, 23, 24, 25, 4]

Def gpio_setup ():

# using BCM numbering

GPIO.setmode (GPIO.BCM)

# set all GPIO to output state and output low level

For pin in pins:

GPIO.setup (pin, GPIO.OUT)

GPIO.output (pin, GPIO.LOW)

Def gpio_destroy ():

For pin in pins:

GPIO.output (pin, GPIO.LOW)

GPIO.setup (pin, GPIO.IN)

# callback function for successful connection

Def on_connect (client, userdata, flags, rc):

Print ("Connected with result code" + str (rc))

# subscribe to gpio topics after the connection is completed

Client.subscribe ("gpio")

# message push callback function .decode ("utf-8")

Def on_message (client, userdata, msg):

Print (msg.topic+ "" + str (msg.payload))

# get pin and value in the load

# gpio = json.loads (str (msg.payload))

# if gpio ['pin'] in pins:

# if gpio ['value'] = = 0:

# GPIO.output (gpio ['pin'], GPIO.LOW)

# else:

# GPIO.output (gpio ['pin'], GPIO.HIGH)

If _ _ name__ = ='_ _ main__':

Client = mqtt.Client ()

Client.on_connect = on_connect

Client.on_message = on_message

# gpio_setup ()

Try:

# Please change the IP address and account password authentication of the MQTT proxy server according to the actual situation

# client.username_pw_set (username,password)

Client.connect ("xxx.xxx.xxx.xxx", 1883, 60)

Client.loop_forever ()

Except KeyboardInterrupt:

Client.disconnect ()

Gpio_destroy ()

Send data as {"pin": 12, "value": 1}

These are all the contents of the article "how Raspberry pie achieves mqtt Communications". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report