回到首页 返回首页
回到顶部 回到顶部
返回上一页 返回上一页

【PinPong Board】PinPong板之Wifi、MQTT与AHT20 简单

头像 豆爸 2021.05.06 534 0

材料清单

  • PinPong Board主板 X1
  • DHT11数字温湿度传感器V2 X1 链接
  • 掌控板 X1 链接
  • 掌控IO扩展版 X1 链接

步骤1 MQTT发布端:掌控板+DHT11采集室外温湿度数据-硬件的搭建

使用杜邦线、扩展版将DHT11模块连接到掌控板P13接口。

project-image

步骤2 MQTT发布端:掌控板+DHT11采集室外温湿度数据-掌控板程序

代码使用mPython 0.5.1图形化编写。

project-image
代码
from mpython import *

import network

my_wifi = wifi()

my_wifi.connectWiFi('ssid', 'password')

from umqtt.simple import MQTTClient

mqtt = MQTTClient('0805e3d04f3b34e7', '182.254.130.180', 1883, 'iot_id', 'iot_pwd', keepalive=30)

try:
    mqtt.connect()
    print('Connected')
except:
    print('Disconnected')

from machine import Timer

import dht

dht11 = dht.DHT11(Pin(Pin.P13))

tim13 = Timer(13)

def timer13_tick(_):
    try: dht11.measure()
    except: pass

tim13.init(period=1000, mode=Timer.PERIODIC, callback=timer13_tick)

import time
while not my_wifi.sta.isconnected():
    pass
oled.DispChar(my_wifi.sta.ifconfig()[0], 0, 0, 1)
oled.show()
while True:
    oled.fill(0)
    oled.show()
    oled.DispChar((str(dht11.temperature())), 0, 0, 1)
    oled.DispChar((str(dht11.humidity())), 0, 32, 1)
    oled.show()
    mqtt.publish('topic1', (str(dht11.temperature())))
    mqtt.publish('topic2', (str(dht11.humidity())))
    time.sleep(2)

MQTT服务器使用的是DFRobot的Easy Iot:https://iot.dfrobot.com.cn/index.html

project-image

MQTT服务器上存储的温度和湿度数据,如下图:

project-image
project-image

步骤3 MQTT订阅端:PinPong Board程序编写

1、连接wifi,连接成功后OLED显示IP地址和port端口号。

2、连接MQTT服务器,订阅话题,读取远程实时发布的温度湿度数据,通过OLED实时显示。

3、通过AHT20温湿度传感器,采集室内温湿度数据,通过OLED实时显示。

project-image

如上图所示,为了便于调试,将订阅获取的远程温湿度数据以及ATH20采集的室内温湿度数据print()打印输出。

代码
import paho.mqtt.client as mqtt
import time
from pinpong.board import Board,Pin,WIFI,I2C
from pinpong.libs.dfrobot_ssd1306 import SSD1306_I2C #导入ssd1306库
from pinpong.libs.dfrobot_aht20 import AHT20

wifi_ssid = "ssid"                      #wifi名
wifi_pwd = "password"                    #wifi密码
mqtt_hostname ="182.254.130.180"
mqtt_port = 1883
mqtt_keepalive = 30
mqtt_topic1 = "topic1"
mqtt_topic2 = "topic2"
mqtt_topic_user ="iot_id"
mqtt_topic_pwd ="iot_pwd"
temperature = ""

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe(mqtt_topic1,qos=0)
    client.subscribe(mqtt_topic2,qos=0)

def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.payload))
    if msg.topic == mqtt_topic1:
        temperature = "To:"+(str(msg.payload)[2:4])
        oled.text(temperature,0,10)
    else:
        humidity = "Ho:"+(str(msg.payload)[2:4])
        #humidity = str(msg.payload)
        oled.text(humidity,0,40)        
    aht20.measure_template_humidity()
    temp_hum = aht20.get_template_humidity()
    print("TEMP:"+ str(temp_hum[1]))
    print("HUM:" + str(temp_hum[0]))
    oled.text("Ti:" + str(temp_hum[1]),65,10)
    oled.text("Hi:" + str(temp_hum[0]),65,40)
    oled.show()  #显示生效
    
def test():  
    client = mqtt.Client()
    client.username_pw_set(mqtt_topic_user, mqtt_topic_pwd) 
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(mqtt_hostname,mqtt_port,mqtt_keepalive)
    oled.fill(0)
    oled.show()  #显示生效
    client.loop_forever()


Board("PinPong Board").begin()#初始化,选择板型和端口号,不输入端口号则进行自动识别
oled=SSD1306_I2C(width=128, height=64)#初始化屏幕,传入屏幕像素点数
i2c = I2C()
#l = i2c.scan()#i2c.scan()扫描出是十进制的I2C地址
#print("i2c list:",l)

aht20 = AHT20()
aht20.init_ath20()

oled.fill(0)
oled.show()#显示生效
obj = WIFI()
obj.set_ssid(wifi_ssid)#设置wifi名
obj.set_password(wifi_pwd)#设置wifi密码
obj.connect_wifi()#开始连接
print("Waiting for WIFI connection...")
time.sleep(5)
ip,port = obj.get_ip_port()
ip = "ip:" + str(ip)
port = "port:"+str(port)
oled.text(ip,0,10)#指定位置显示文字
oled.text(port,0,30)
oled.show()#显示生效
    
if __name__ == '__main__':
    test()

评论

user-avatar