关于micropython ESP32的MQTT通讯方式控制舵机

首先我们的前提是实践过我曾经写的关于micropython ESP32+PCA9685 舵机转速控制【https://blog.csdn.net/mrjiale/article/details/81813439】

在这个基础上利用物联网的技术进行舵机控制,注重的是如何用MQTT进行通讯

当然我们要做的前提准备是:
硬件:esp32板子,pca9685板子,一个舵机
【其实无论是树莓派板子,还是esp8266,还是其他的能够运用mqtt通讯的板子原理是相通的】

  1. 首先确保你的esp32接上电源后自动连上wifi
    教程参考【https://www.jianshu.com/p/0613f3f3f4ba】
  2. 在esp32上安装MQTT库:
    import upip
    upip.install('micropython-umqtt.simple')
    这样umqtt.simple这个包就安装好了
    教程参考【https://github.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/blob/master/19_MQTT%E5%85%A5%E9%97%A8%E4%B9%8B%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/MQTT%E5%85%A5%E9%97%A8%E4%B9%8B%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98.md】

硬件连接:
关于micropython ESP32的MQTT通讯方式控制舵机_第1张图片

软件:win10系统,电脑已安装python3.5

  1. 使用Python 进行基于MQTT的物联网开发, 这就需要用使用pip3安装另外一个库 paho-mqttpip3 install paho-mqtt
  2. 电脑安装软件:mosquitto
    【下载地址http://www.mosquitto.org/download/】
    关于micropython ESP32的MQTT通讯方式控制舵机_第2张图片
    画重点:每次使用MQTT进行通讯的时候都要打开mosquitto服务
    找到安装目录,然后在当前目录下 shift加右键用命令窗口打开cmd,输入mosquitto -v
    关于micropython ESP32的MQTT通讯方式控制舵机_第3张图片

这里还是得整理下我要做啥:我将会用电脑通过mqtt通讯方式与esp进行通讯,即打算用wifi控制舵机【这就可以扩大了,如果我用手机app控制那就是和智能家居的做法类似了】

贴代码:
esp32板子要上传的代码:

  1. main.py
  2. servo.py
  3. pca9685.py
  4. subscriber.py
  • [1 ] main.py
exec(open('subscriber.py').read(), globals())
  • [ 2] servo.py
# -*- coding:utf-8 -*-
'''
基于PCA9685 I2C舵机控制模块对舵机进行控制的库
'''
import pca9685
import math

class Servos:
    def __init__(self, i2c, address=0x40, freq=50, min_us=600, max_us=2400,
                 degrees=180):
        self.period = 1000000 / freq
        self.min_duty = self._us2duty(min_us)
        self.max_duty = self._us2duty(max_us)
        self.degrees = degrees
        self.freq = freq
        self.pca9685 = pca9685.PCA9685(i2c, address)
        self.pca9685.freq(freq)

    def _us2duty(self, value):
        return int(4095 * value / self.period)

    def position(self, index, degrees=None, radians=None, us=None, duty=None):
        span = self.max_duty - self.min_duty
        if degrees is not None:
            duty = self.min_duty + span * degrees / self.degrees
        elif radians is not None:
            duty = self.min_duty + span * radians / math.radians(self.degrees)
        elif us is not None:
            duty = self._us2duty(us)
        elif duty is not None:
            pass
        else:
            return self.pca9685.duty(index)
        duty = min(self.max_duty, max(self.min_duty, int(duty)))
        self.pca9685.duty(index, duty)

    def release(self, index):
        self.pca9685.duty(index, 0)

  • [ 3] pca9685.py
# -*- coding:utf-8 -*-
'''
PCA9685 I2C舵机控制模块的库
'''
import ustruct
import time


class PCA9685:
    def __init__(self, i2c, address=0x40):
        self.i2c = i2c
        self.address = address
        self.reset()

    def _write(self, address, value):
        self.i2c.writeto_mem(self.address, address, bytearray([value]))

    def _read(self, address):
        return self.i2c.readfrom_mem(self.address, address, 1)[0]

    def reset(self):
        self._write(0x00, 0x00) # Mode1

    def freq(self, freq=None):
        if freq is None:
            return int(25000000.0 / 4096 / (self._read(0xfe) - 0.5))
        prescale = int(25000000.0 / 4096.0 / freq + 0.5)
        old_mode = self._read(0x00) # Mode 1
        self._write(0x00, (old_mode & 0x7F) | 0x10) # Mode 1, sleep
        self._write(0xfe, prescale) # Prescale
        self._write(0x00, old_mode) # Mode 1
        time.sleep_us(5)
        self._write(0x00, old_mode | 0xa1) # Mode 1, autoincrement on

    def pwm(self, index, on=None, off=None):
        if on is None or off is None:
            data = self.i2c.readfrom_mem(self.address, 0x06 + 4 * index, 4)
            return ustruct.unpack(', data)
        data = ustruct.pack(', on, off)
        self.i2c.writeto_mem(self.address, 0x06 + 4 * index,  data)

    def duty(self, index, value=None, invert=False):
        if value is None:
            pwm = self.pwm(index)
            if pwm == (0, 4096):
                value = 0
            elif pwm == (4096, 0):
                value = 4095
            value = pwm[1]
            if invert:
                value = 4095 - value
            return value
        if not 0 <= value <= 4095:
            raise ValueError("Out of range")
        if invert:
            value = 4095 - value
        if value == 0:
            self.pwm(index, 0, 4096)
        elif value == 4095:
            self.pwm(index, 4096, 0)
        else:
            self.pwm(index, 0, value)

  • [ 4] subscriber.py
#created by lijiale

from umqtt.simple import MQTTClient
import time
from machine import I2C,Pin
from servo import Servos
i2c=I2C(sda=Pin(21),scl=Pin(22),freq=10000)
servos=Servos(i2c,address=0x40)

SERVER = '183.53.18.170'
CLIENT_ID = 'ZIMO'
TOPIC = b'mrjiale'


def servo_start():
	while True:
		for i in range(2, 170): 			 
			servos.position(0,i)  
			time.sleep_ms(10)     
		for i in range(170, 2, -1):
			servos.position(0,i)
			time.sleep_ms(10)

def mqtt_callback(topic, msg):
	global TOPIC
	print('topic: {}'.format(topic))
	print('msg: {}'.format(msg))
	if msg == b"start":
		servo_start()

client = MQTTClient(CLIENT_ID, SERVER)
client.set_callback(mqtt_callback)
client.connect()
client.subscribe(TOPIC)

while True:
	client.check_msg()
	time.sleep(1)


把上面代码丢进esp32,通电esp就会自动运行代码【main.py】

电脑端:
打开一个cmd窗口运行mosquitto服务,
打开另外一个cmd窗口运行下面代码 mqtt_servo.py

import paho.mqtt.client as mqtt
import time

HOST_IP = '183.53.18.170' # Server的IP地址
HOST_PORT = 1883 # mosquitto 默认打开端口
TOPIC_ID = 'mrjiale' # TOPIC的ID

# 创建一个客户端
client = mqtt.Client()
# 连接到服务器(本机)
client.connect(HOST_IP, HOST_PORT, 60)

message = 'start'
client.publish(TOPIC_ID, message)
print('SEND: {}'.format(message))

舵机0-180°之间循环转动,完毕。

【最后反正你也不会同样操作,理解原理即可,毕竟每个人用的硬件不一样。】

你可能感兴趣的:(esp32)