树莓派温湿度发布到百度云MQTT

树莓派温湿度发布到百度云MQTT:

继上一篇文章( 树莓派与DHT-11温湿度传感器):修改Collect.py(collect函数返回温湿度信息) 如下:
Collect.py:

import RPi.GPIO as GPIO
import time
def collect():
    THdata = []
    channel = 4
    data = []
    GPIO.setmode(GPIO.BCM)
    time.sleep(2)
    GPIO.setup(channel, GPIO.OUT)
    GPIO.output(channel, GPIO.LOW)
    time.sleep(0.02)
    GPIO.output(channel, GPIO.HIGH)
    GPIO.setup(channel, GPIO.IN)
    while GPIO.input(channel) == GPIO.LOW:
        continue
    while GPIO.input(channel) == GPIO.HIGH:
        continue
    j = 0
    while j < 40:
        k = 0
        while GPIO.input(channel) == GPIO.LOW:
            continue
        while GPIO.input(channel) == GPIO.HIGH:
            k += 1
            if k > 100:
                break
        if k < 8:
            data.append(0)
        else:
            data.append(1)

        j += 1

    # print("sensor is working.")
    # print(data)
    humidity_bit = data[0:8]
    humidity_point_bit = data[8:16]
    temperature_bit = data[16:24]
    temperature_point_bit = data[24:32]
    check_bit = data[32:40]
    humidity = 0
    humidity_point = 0
    temperature = 0
    temperature_point = 0
    check = 0
    for i in range(8):
        humidity += humidity_bit[i] * 2 ** (7 - i)
        humidity_point += humidity_point_bit[i] * 2 ** (7 - i)
        temperature += temperature_bit[i] * 2 ** (7 - i)
        temperature_point += temperature_point_bit[i] * 2 ** (7 - i)
        check += check_bit[i] * 2 ** (7 - i)
    tmp = humidity + humidity_point + temperature + temperature_point
    if check == tmp:
        print("temperature :", temperature, "*C", " humidity :", humidity, "%")
        THdata.append(temperature)
        THdata.append(humidity)
        return THdata
    else:
        # print("wrong")
        time.sleep(1)
        return collect()

Publish.py:(change,topic为修改的部分,改成你自己的百度云MQTT实例即可)

import paho.mqtt.client as mqtt
import sys
import uuid
import Collect
import time

broker = change0'
port = 1883
username = 'change1'# 修改
password = 'change2'# 修改
clientid = 'temperature_publish' + str(uuid.uuid4())
topic = 'temperature'# 修改


def on_connect(client, userdata, rc):
    print('Connected. Client id is: ' + clientid)

def on_message(client, userdata, msg):
    msg = str(msg.payload, 'utf-8')
    print('MQTT message received: ' + msg)
    if msg == 'exit':
        sys.exit()

client = mqtt.Client(clientid)
# client.will_set('temperature', 'last will', 0, False)
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username, password)
print('Connecting to broker: ' + broker)
client.connect(broker, port)
client.loop_start()
time.sleep(3)
# client.loop_forever()
while True:
    rTHdata = test1.Collect.collect()
    client.publish(topic, "temperture :" + str(rTHdata[0]) + " *C   " + "humidity :" + str(rTHdata[1]) + " %")
    #设置发布间隔时间
    time.sleep(3)
  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
通过HA(Home Assistant)和MQTT,可以实现对树莓的控制。以下是一个简单的步骤: 1. 在树莓上安装MQTT代理服务器,并设置用户名和密码(如果需要)。 2. 在树莓上安装MQTT客户端工具,例如mosquitto_pub命令:sudo apt-get install mosquitto-clients 3. 在HA中添加MQTT集成,将其连接到MQTT代理服务器。 4. 在HA中创建MQTT传感器或开关等实体,将其与MQTT主题相关联。 5. 在树莓的Python脚本中使用MQTT客户端库(例如paho-mqtt)连接到MQTT代理服务器,并发布或订阅相应的主题。 6. 在Python脚本中编写代码,以响应MQTT主题的消息,并执行相应的操作,例如控制GPIO引脚。 例如,在树莓上编写一个Python脚本,用于控制GPIO引脚。以下是一个简单的例子: ```python import RPi.GPIO as GPIO import paho.mqtt.client as mqtt # 设置GPIO引脚 GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) # 连接到MQTT代理服务器 def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) # 订阅MQTT主题 client.subscribe("homeassistant/switch/gpio18") # 响应MQTT主题的消息 def on_message(client, userdata, msg): print(msg.topic+" "+str(msg.payload)) if msg.payload == b'ON': # 打开GPIO引脚 GPIO.output(18, GPIO.HIGH) elif msg.payload == b'OFF': # 关闭GPIO引脚 GPIO.output(18, GPIO.LOW) # 创建MQTT客户端 client = mqtt.Client() client.username_pw_set("<MQTT用户名>", "<MQTT密码>") client.on_connect = on_connect client.on_message = on_message # 连接到MQTT代理服务器 client.connect("<MQTT代理服务器地址>", 1883, 60) # 循环处理MQTT消息 client.loop_forever() ``` 在HA中创建一个MQTT开关实体,将其与MQTT主题"homeassistant/switch/gpio18"相关联。现在,你就可以在HA中控制这个开关了。当开关状态发生变化时,MQTT主题会收到相应的消息,Python脚本会响应这些消息,并控制GPIO引脚的状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值