import time
import machine
from machine import Pin
from machine import Pin, PWM
from machine import SoftI2C, Pin, Timer
from ssd1306 import SSD1306_I2C
import dht
from machine import Pin,I2C,ADC,Timer
ledred_pin = machine.Pin(4, machine.Pin.OUT)
ledgreen_pin = machine.Pin(18, machine.Pin.OUT)
servo_pin = machine.Pin(13, machine.Pin.OUT)# 定义舵机控制引脚
servo = machine.PWM(servo_pin)# 定义舵机控制变量
servo.freq(50)
servo.duty(77)# 舵机初始化位置
i2c = SoftI2C(sda=Pin(21), scl=Pin(22))
oled = SSD1306_I2C(128, 64, i2c, addr=0x3c)
dht11 = dht.DHT11(Pin(14))
relay_pin = machine.Pin(2, machine.Pin.OUT)
switch_pin1 = machine.Pin(15, machine.Pin.IN)
switch_pin2 = machine.Pin(5, machine.Pin.IN)
switch_pin3 = machine.Pin(25, machine.Pin.IN)
switch_pin4 = machine.Pin(26, machine.Pin.IN)
switch_pin5 = machine.Pin(27, machine.Pin.IN)
switch_pin6 = machine.Pin(19, machine.Pin.IN)
player_pin = machine.Pin(12, machine.Pin.OUT)# 定义蜂鸣器控制引脚
player = machine.PWM(player_pin)# 定义蜂鸣器控制变量
player.freq(5000)
player.duty(512)# 蜂鸣器初始化位置
Light=ADC(Pin(32))#初始化ADC,Pin=32,11DB衰减,测量电压0-3.3V
Light.atten(ADC.ATTN_11DB)
switch_state = False# 定义开关初始状态为关闭
def play_tone(frequency, duration_ms):
player_pin.freq(int(frequency))
time.sleep_ms(int(duration_ms))
player_pin.freq(0)
time.sleep(0.05)
def play_music(tone_sequence):
for tone in tone_sequence:
play_tone(tone[0], tone[1])
music = [
(262, 250), (294, 250), (330, 250),
(392, 250), (440, 250), (524, 250)
]
def fun(tim):
if switch_pin1.value() == 1:
dht11.measure()# 测量温湿度,从传感器中读取数据
oled.fill(0)#清屏显示黑色背景
oled.text("DHT11", 0, 0)
oled.text("wzs", 45, 0)
oled.text("Tem:"+"{:.2f}C".format(dht11.temperature()), 0, 40) # 获取并显示温度
oled.text("Hum:"+"{} %".format(dht11.humidity()), 0, 50)# 获取并显示湿度
chinese('智能家居系统',16,20)
oled.show()
if switch_pin1.value() == 0 :
oled.fill(0)#清屏显示黑色背景
oled.text('PHOTOCELL',0,0)#首行显示01Studio
oled.text('Lighttest:',0,15)#次行显示实验名称
value=Light.read()#获取ADC数值
oled.text(str(value)+'(4095)',0,40)#显示数值
oled.text(str('%.2f'%(value/4095*3.3))+'V',0,55)#计算电压值,获得的数据0-4095相当于0-3.3V,('%.2f'%)表示保留2位小数
if 0<=value<=1365:
oled.text('Bright',60,55)#判断光照强度,分3档显示。
if 1365<value<=2730:
oled.text('Normal',60,55)
if 2730<value<=4095:
oled.text('Weak',60,55)
oled.show()
if switch_pin2.value() == 1:# 判断若开关被释放
switch_state = True# 打开开关
servo.duty(26) # 舵机转动到90度
time.sleep_ms(500)
if switch_pin2.value() == 0:# 判断若开关被释放
switch_state = False# 关闭开关
servo.duty(77)# 舵机返回初始化位置
time.sleep_ms(500)
if switch_pin3.value() == 1:
switch_state = True
ledred_pin.on()
if switch_pin3.value() == 0:
ledred_pin.off()
if switch_pin4.value() == 1:
switch_state = True
ledgreen_pin.on()
if switch_pin4.value() == 0:
ledgreen_pin.off()
if switch_pin5.value() == 1:
switch_state = True
player_pin.on()
if switch_pin5.value() == 0:
player_pin.off()
if switch_pin6.value() == 1:
switch_state = True
relay_pin.on()
elif switch_pin6.value() == 0:
relay_pin.off()
timer = Timer(-1)
timer.init(period=1500,mode=Timer.PERIODIC,callback=fun)#开启RTOS定时器,定时周期1.5秒执行一次操作
fonts= {
0xe982a2:
[0x00,0x00,0x3F,0x12,0x12,0x12,0x3F,0x12,0x12,0x12,0x32,0x62,0x06,0x03,0x00,0x00,
0x00,0x78,0x48,0x58,0x70,0x58,0x48,0x48,0x68,0x58,0x40,0x40,0x40,0x40,0x60,0x00], # 邢
0xe58fb0:
[0x00,0x00,0x08,0x10,0x20,0x60,0x3F,0x00,0x1F,0x10,0x10,0x10,0x10,0x0F,0x00,0x00,
0x00,0x00,0x20,0x10,0x08,0x0C,0xFC,0x02,0xF0,0x10,0x10,0x10,0x10,0xE0,0x00,0x00], # 台
0xe5ada6:
[0x00,0x66,0x32,0x13,0x7F,0x40,0x47,0x0D,0x03,0x7F,0x01,0x01,0x07,0x02,0x00,0x00,
0x00,0x18,0x70,0x40,0xF0,0x18,0x98,0x80,0x00,0xF8,0x08,0x00,0x00,0x00,0x00,0x00], # 学
0xe999a2:
[0x00,0x3C,0x27,0x3E,0x2A,0x28,0x39,0x38,0x20,0x20,0x20,0x20,0x21,0x2E,0x20,0x00,
0x00,0x40,0xF8,0x08,0xE8,0x00,0xF0,0xA0,0xA0,0xA0,0xA0,0xA4,0x22,0x3E,0x00,0x00], # 院
}
def chinese(ch_str, x_axis, y_axis): #函数部分
offset_ = 0
for k in ch_str:
code = 0x00 # 将中文转成16进制编码
data_code = k.encode("utf-8")
code |= data_code[0] << 16
code |= data_code[1] << 8
code |= data_code[2]
byte_data = fonts[code]
for y in range(0, 16):
a_ = bin(byte_data[y]).replace('0b', '')
while len(a_) < 8:
a_ = '0'+ a_
b_ = bin(byte_data[y+16]).replace('0b', '')
while len(b_) < 8:
b_ = '0'+ b_
for x in range(0, 8):
oled.pixel(x_axis + offset_ + x, y+y_axis, int(a_[x]))
oled.pixel(x_axis + offset_ + x +8, y+y_axis, int(b_[x]))
offset_ += 16
chinese('hq',16,20)
from umqtt.simple import MQTTClient
import time
# Define MQTT credentials
username = "6651abad6bc31504f06bdc69_Smart_home_dev_1"
password = "45ed72f51222d70d7be7e0328c250d3b9b34d387b071c536d294ab1f887a80f0"
client_id = "6651abad6bc31504f06bdc69_Smart_home_dev_1_0_0_2024060907"
hostname = "7378bfde64.st1.iotda-device.cn-north-4.myhuaweicloud.com"
port = 1883
# Create an MQTT client instance
client = MQTTClient(client_id, hostname, port, username, password)
# Define the topic and callback function
topic = b'example_topic' # The topic you want to subscribe to
def callback(topic, msg):
print('Received message:', msg)
# Set the callback function
client.set_callback(callback)
# Connect to the MQTT broker
client.connect()
# Subscribe to the topic
client.subscribe(topic)
client.check_msg() # Check for incoming messages