文章目录

  • 一、前言
  • 二、代码展示
  • 三、实物演示


也是托更很久了,这次就上传吧

一、前言

先说好了,我这个毕设选的是真水,非常的水。(因为当初考虑到研究生复试需要去准备,所以就不敢选太难的,因为我的个人实力不咋滴,hhhh~)。对于那些有老师带的,就根据自己老师来就行,我这边是没有老师带的,就是自己瞎弄弄,把之前的大创换了个通信方式,就拿出来了。(吐槽下,pico的参考是真的很少)。

二、代码展示

from machine import UART,Pin
import utime
import imu
import machine
#串口配置


uart1 = UART(0, baudrate=115200, tx=Pin(12), rx=Pin(13))
i2c = machine.I2C(0, sda=machine.Pin(16), scl=machine.Pin(17), freq=400000)
# print("I2C addr: ", i2c.scan()[0])
sensor = imu.MPU6050(i2c)

# 主程序
trigger = Pin(26, Pin.OUT)
echo = Pin(18, Pin.IN)

# 超声波测距,单位:厘米
def getDistance(trigger, echo):
    # 产生10us的方波
    trigger.low()
    utime.sleep_us(2)
    trigger.high()
    utime.sleep_us(10)
    trigger.low()
    
    while echo.value() == 0:
        start = utime.ticks_us()
    while echo.value() == 1:
        end = utime.ticks_us()
    d = (end - start) * 0.0343 / 2 
    return d

number_part_flag = 0
msgid_part_flag = 0

uart1.write('AT+CREG?\r')
utime.sleep_ms(100)
while uart1.any() > 0:
    rxData_One = uart1.read()
    str = rxData_One.decode(r'utf-8')
    while '+CREG: 0,2' in str:
        print("上网失败,再次尝试测试")
        utime.sleep_ms(100)
        uart1.write('AT+CREG?\r')
        while uart1.any() > 0:
            rxData_One = uart1.read()
            str = rxData_One.decode('utf-8')
    print("上网成功")
utime.sleep_ms(500)
while uart1.any() > 0:
    rxData_One = uart1.read()
# 创建实例
uart1.write('AT+MIPLCREATE\r')
utime.sleep_ms(100)
while uart1.any() > 0:
    rxData_two = uart1.read()
    str = rxData_two.decode(r'utf-8')
    if 'ERROR' in str:
        print("创建实例-失败")
    else:
        print("创建实例-成功")
utime.sleep_ms(500)
while uart1.any() > 0:
    rxData_One = uart1.read()
# 创建对象
uart1.write('AT+MIPLADDOBJ=0,3301,1,"1",7,1\r')
utime.sleep_ms(100)
while uart1.any() > 0:
    rxData_three = uart1.read()
    str = rxData_three.decode(r'utf-8')
    if 'ERROR' in str:
        print("创建对象-失败")
    else:
        print("创建对象-成功")
utime.sleep_ms(500)
while uart1.any() > 0:
    rxData_One = uart1.read()
# 注册(循环等待,等到0,68719,1,3301,0,-1),提取68719这个位置的数
uart1.write('AT+MIPLOPEN=0,30,120\r')
utime.sleep_ms(100)
while number_part_flag == 0:
    while uart1.any() > 0:
        rxData_four = uart1.read()
        rxData_four_str = rxData_four.decode('utf-8')
        # 检查是否存在 3301
        print(rxData_four_str)
        if '3301' in rxData_four_str:  
            print("响应中存在 3301.")
            comma_index = rxData_four_str.find(',') # 找到第一个逗号的位置
            second_comma_index = rxData_four_str.find(',', comma_index + 1) # 找到第二个逗号的位置
            third_comma_index = rxData_four_str.find(',', second_comma_index + 1)  # 找到第三个逗号的位置
            number_part = rxData_four_str[second_comma_index + 1: third_comma_index] # 提取出数字部分
            try: # 将提取出的部分转换成整数
                number_one = int(number_part)
                print("提取的第一次数字为:", number_one)
                number_part_flag = 1
            except ValueError:
                print("提取的第一次数字不是有效的整数。")
        else:
            print("响应中不存在 3301.")
utime.sleep_ms(500)
while uart1.any() > 0:
    rxData_One = uart1.read()
# 发送观察结果
uart1.write('AT+MIPLOBSERVERSP=0,%d,1\r' % (number_one))
utime.sleep_ms(100)
while msgid_part_flag == 0:
    while uart1.any() > 0:
        rxData_five = uart1.read()
        rxData_five_str = rxData_five.decode('utf-8')
        if '3301' in rxData_five_str:
            first_comma_index = rxData_five_str.find(',') # 找到第一个逗号的位置
            second_comma_index = rxData_five_str.find(',', first_comma_index + 1) # 找到第二个逗号的位置
            number_part = rxData_five_str[first_comma_index + 1: second_comma_index] # 提取出数字部分
            # 将提取出的部分转换成整数
            try:
                number_two = int(number_part)
                print("提取的第二次数字为:", number_two)
                msgid_part_flag = 1
            except ValueError:
                print("提取的第二次数字不是有效的整数。")
        else:
            print("失败")
utime.sleep_ms(500)
while uart1.any() > 0:
    rxData_One = uart1.read()
# 发送对象属性
uart1.write('AT+MIPLDISCOVERRSP=0,%d,1,29,"5700;5601;5602;5603;5604;5701"\r' % (number_two))
utime.sleep_ms(100)
while uart1.any() > 0:
    rxData_One = uart1.read()
    
# # 通知属性变化(数字)
# uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5700,4,4,17.5,0,0,116\r' % (number_one))
# utime.sleep_ms(100)
# while uart1.any() > 0:
#     rxData_One = uart1.read()  
#     rxData_One_str = rxData_One.decode('utf-8')
#     print(rxData_One_str)
#     if '0,26,116' in rxData_One_str:
#         print("修改成功")
#     else:
#         print("修改失败")
# utime.sleep_ms(100)
# while uart1.any() > 0:
#     rxData_One = uart1.read()

#     uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5601,4,4,25.5,0,0,116\r' % (number_one))
#     uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5602,4,4,22.5,0,0,116\r' % (number_one))
#     uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5603,4,4,22.5,0,0,116\r' % (number_one))

# 通知属性变化(文本)
#     uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5701,1,4,29cm,0,0,116\r' % (number_one))
#     utime.sleep_ms(100)
#     while uart1.any() > 0:
#         rxData_One = uart1.read()
#         rxData_One_str = rxData_One.decode('utf-8')
#         if '0,26,116' in rxData_One_str:
#             print("修改成功")
#         else:
#             print("修改失败")
while True:
    print("加速度:{:7.2f}{:7.2f}{:7.2f}  陀螺仪:{:9.2f}{:9.2f}{:9.2f}  温度:{:5.1f}" \
          .format(sensor.accel.x, sensor.accel.y, sensor.accel.z,  \
                  sensor.gyro.x, sensor.gyro.y, sensor.gyro.z, \
                  sensor.temperature))
    distance = getDistance(trigger, echo)
    print("距离:{:.2f} cm".format(distance))
    # x的数据正常,但是y\z\distance不行
    x_str = "{:.2f}".format(abs(sensor.gyro.x))
    y_str = "{:.2f}".format(abs(sensor.gyro.y))
    z_str = "{:.2f}".format(abs(sensor.gyro.z))
    
    x_str_len = len(x_str)
    y_str_len = len(y_str)
    z_str_len = len(z_str)
    
    distance_str = "{:.2f}".format(distance)
    distance_str_len = len(distance_str)+2

    # 对于温度要取绝对值以及判断位数
    uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5601,4,%d,%s,0,0,116\r' % (number_one,x_str_len,x_str))
    utime.sleep_ms(100)
    while uart1.any() > 0:
        rxData_One = uart1.read()
    utime.sleep_ms(100)
    while uart1.any() > 0:
        rxData_One = uart1.read()  
        rxData_One_str = rxData_One.decode('utf-8')
        print(rxData_One_str)
        if '0,26,116' in rxData_One_str:
            print("修改成功")
        else:
            print("修改失败")
    utime.sleep(1)
    while uart1.any() > 0:
        rxData_One = uart1.read()
    
    uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5602,4,%d,%s,0,0,116\r' % (number_one,y_str_len,y_str))
    utime.sleep_ms(100)
    while uart1.any() > 0:
        rxData_One = uart1.read()
    utime.sleep_ms(100)
    while uart1.any() > 0:
        rxData_One = uart1.read()  
        rxData_One_str = rxData_One.decode('utf-8')
        print(rxData_One_str)
        if '0,26,116' in rxData_One_str:
            print("修改成功")
        else:
            print("修改失败")
    utime.sleep(1)
    while uart1.any() > 0:
        rxData_One = uart1.read()

    uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5603,4,%d,%s,0,0,116\r' % (number_one,z_str_len,z_str))
    utime.sleep_ms(100)
    while uart1.any() > 0:
        rxData_One = uart1.read()
    utime.sleep_ms(100)
    while uart1.any() > 0:
        rxData_One = uart1.read()  
        rxData_One_str = rxData_One.decode('utf-8')
        print(rxData_One_str)
        if '0,26,116' in rxData_One_str:
            print("修改成功")
        else:
            print("修改失败")
    utime.sleep_ms(100)

#     # 通知属性变化(文本)
#     uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5701,1,%d,%s cm,0,0,116\r' % (number_one,distance_str_len+3,distance_str))
#     utime.sleep_ms(100)
#     while uart1.any() > 0:
#         rxData_One = uart1.read()
#         rxData_One_str = rxData_One.decode('utf-8')
#         if '0,26,116' in rxData_One_str:
#             print("修改成功")
#         else:
#             print("修改失败")
#     utime.sleep(1)
    
    # 通知属性变化(文本)
    uart1.write('AT+MIPLNOTIFY=0,%d,3301,0,5701,1,%d,%scm,0,0,116\r' % (number_one,distance_str_len,distance_str))
    utime.sleep_ms(100)
    while uart1.any() > 0:
        rxData_One = uart1.read()
        rxData_One_str = rxData_One.decode('utf-8')
        if '0,26,116' in rxData_One_str:
            print("修改成功")
        else:
            print("修改失败")

三、实物演示


毕业设计