在Jupyter Notebook中使用MicroPython从浏览器控制电子设备

创意展示、DIY分享、经验交流
回复
头像
shaoziyang
帖子: 1344
注册时间: 2019年 10月 21日 13:48

在Jupyter Notebook中使用MicroPython从浏览器控制电子设备

#1

帖子 shaoziyang » 2020年 7月 31日 09:12

来自:https://null-byte.wonderhowto.com/how-t ... k-0236726/


如果你想控制的电子设备,如继电器或马达,你可以用MicroPython与ESP8266和网络浏览器连接。以前您会使用Arduino 编写ESP8266,但是Arduino使用的C ++并不总是初学者最容易学习的编程语言。

在MicroPython中编程要简单得多。另外,在微控制器上(比如ESP8266或ESP32)使用Python代码,是一个很好的方法,可以控制任何Jupyter Notebook的浏览器中的电子元件。

我们的示例场景

为了帮助您展示如何将MicroPython,MCU和Jupyter Notebook一起用于控制电子组件,我们将使用通常在Arduino中很难实现但在MicroPython中非常容易实现的设置。在面包板上,我们将ESP8266与D1 Mini配对,该D1 Mini插入了继电器开关。继电器是一种电子开关,类似于您许多房间中的单极墙壁开关。当我们发送电源信号时,它会打开或关闭。装有一个9伏电池,可以在需要时打开电动机。

您需要什么

您可能会遇到一个不同的项目,并使用不同的材料,但是要复制我们在这里所做的工作,这些是项目的一部分:
  • D1迷你板(或其他ESP8266或ESP32)
  • 面包板
  • 中继
  • 螺旋桨马达
  • 跳线
  • 微型USB电缆
  • 9伏电池
  • 电池接头
这就是基础知识,当您将D1 Mini插入面包板并连接了继电器时,请通过GPIO引脚连接继电器并将9V电池连接至电动机。

在您的计算机上,您需要:
  • ESPtool(访问该链接以了解如何安装)
  • Jupyter Notebook(稍后将介绍安装)
步骤1:确定正确的串行端口

将ESP8266插入计算机,然后运行以下命令之一以查看其串行地址是什么。如果稍后将MCU拔出到其他USB端口,则它可能具有另一个串行地址。在Linux计算机上:

代码: 全选

~$ dmesg | grep tty

/dev/cu.Bluetooth-Incoming-Port
/dev/cu.SOC
/dev/cu.wchusbserial14630
/dev/cu.MALS
/dev/cu.usbserial-14630
在Mac上:

代码: 全选

~% ls /dev/cu.*

/dev/cu.Bluetooth-Incoming-Port
/dev/cu.SOC
/dev/cu.wchusbserial14630
/dev/cu.MALS
/dev/cu.usbserial-14630
从我在Linux和macOS上获得的结果来看,它应该是/dev/cu.wchusbserial14630。如果看到两个串行端口,请始终使用其中带有“ wch”的串行端口。要确定是否是您的ESP8266,可以将其拔下,重新运行命令,然后将其重新插入相同的USB端口,然后再次运行。在Windows上,要找到串行地址,您需要打开设备管理器并在“端口(COM和LPT)”下找到COM端口。


步骤2:下载最新的MicroPython固件

如果您已经在您的微控制器上安装了MicroPython,则可以跳至步骤4。访问MicroPython的下载页面,找到您的开发板,然后下载文件。您可以在下面找到指向ESP8266和ESP32开发板的下载链接的快速链接。请选择.bin文件以获取最新的稳定版本。 步骤3:擦除开发板

擦除开发板,以便将MicroPython刷新到板上。使用以下命令,注意将串行端口替换为ESP的串行端口。
 

代码: 全选

~$ esptool.py --port /dev/cu.wchusbserial14630 erase_flash

esptool.py v2.8
Serial port /dev/cu.wchusbserial14140
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 2c:f4:32:4b:07:83
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
步骤4:下载 MicroPython 固件

如果您使用的是ESP8266微控制器,请运行以下命令以下载MicroPython固件。注意将.bin文件替换为您下载的MicroPython二进制固件文件。

代码: 全选

~$ esptool.py --port /dev/cu.wchusbserial14630 --baud 460800 write_flash --flash_size=detect 0 /Users/kali/Downloads/esp8266-20191220-v1.12.bin

esptool.py v2.8
Serial port /dev/cu.wchusbserial14630
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 2c:f4:32:4b:07:83
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0040
Compressed 617880 bytes to 402086...
Wrote 617880 bytes (402086 compressed) at 0x00000000 in 9.6 seconds (effective 514.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
如果您使用的是ESP32,请改用以下命令。

代码: 全选

~$ esptool.py --chip esp32 --port /dev/cu.wchusbserial14630 write_flash -z 0x1000 /Users/kali/Downloads/esp32-idf3-20191220-v1.12.bin
下载完成后,请使用以下命令确认其是否正常工作。如果输出内容是类似的,说明下载成功。如果看不到任何内容,请再按几次Enter键以强制执行。

代码: 全选

~$ screen /dev/cu.wchusbserial14630 115200

MicroPython v1.12 on 2019-12-20; ESP module with ESP8266
Type "help()" for more information.
>>>
 

步骤5:安装Jupyter Notebook

现在,我们需要在计算机上安装Jupyter Notebook,并使用网络直接控制电动机、继电器、传感器和其他组件。如果已经安装了它,请跳至步骤6,否则请通过pip安装:
 

代码: 全选

~$ python3 -m pip install --upgrade pip

Collecting pip
Downloading pip-20.1.1-py2.py3-none-any.whl (1.5 MB)
|████████████████████████████████| 1.5 MB 935 kB/s
Installing collected packages: pip
Successfully installed pip-20.1.1
然后使用以下命令安装Jupyter Notebook:

代码: 全选

~$ python3 -m pip install jupyter

Collecting jupyter
Using cached jupyter-1.0.0-py2.py3-none-any.whl (2.7 kB)
Collecting notebook
Using cached notebook-6.0.3-py3-none-any.whl (9.7 MB)
Collecting qtconsole
Downloading qtconsole-4.7.5-py2.py3-none-any.whl (118 kB)
|████████████████████████████████| 118 kB 907 kB/s
Collecting ipykernel
Downloading ipykernel-5.3.3-py3-none-any.whl (120 kB)
|████████████████████████████████| 120 kB 11.7 MB/s
Collecting ipywidgets
Using cached ipywidgets-7.5.1-py2.py3-none-any.whl (121 kB)
Collecting nbconvert
Using cached nbconvert-5.6.1-py2.py3-none-any.whl (455 kB)
Collecting jupyter-console
Using cached jupyter_console-6.1.0-py2.py3-none-any.whl (21 kB)
Requirement already satisfied: nbformat in /usr/lib/python3/dist-packages (from notebook->jupyter) (5.0.7)
Requirement already satisfied: tornado>=5.0 in /usr/lib/python3/dist-packages (from notebook->jupyter) (5.1.1)
Collecting Send2Trash
Using cached Send2Trash-1.5.0-py3-none-any.whl (12 kB)
Requirement already satisfied: jupyter-core>=4.6.1 in /usr/lib/python3/dist-packages (from notebook->jupyter) (4.6.3)
Collecting jupyter-client>=5.3.4
Downloading jupyter_client-6.1.6-py3-none-any.whl (108 kB)
|████████████████████████████████| 108 kB 2.2 MB/s
Requirement already satisfied: traitlets>=4.2.1 in /usr/lib/python3/dist-packages (from notebook->jupyter) (4.3.3)
Collecting prometheus-client
Downloading prometheus_client-0.8.0-py2.py3-none-any.whl (53 kB)
|████████████████████████████████| 53 kB 1.9 MB/s
Requirement already satisfied: ipython-genutils in /usr/lib/python3/dist-packages (from notebook->jupyter) (0.2.0)
Collecting pyzmq>=17
Downloading pyzmq-19.0.1-cp38-cp38-manylinux1_x86_64.whl (1.1 MB)
|████████████████████████████████| 1.1 MB 9.9 MB/s
Requirement already satisfied: jinja2 in /usr/lib/python3/dist-packages (from notebook->jupyter) (2.10.1)
Collecting terminado>=0.8.1
Using cached terminado-0.8.3-py2.py3-none-any.whl (33 kB)
Requirement already satisfied: pygments in /usr/lib/python3/dist-packages (from qtconsole->jupyter) (2.3.1)
Collecting qtpy
Using cached QtPy-1.9.0-py2.py3-none-any.whl (54 kB)
Requirement already satisfied: ipython>=5.0.0 in /usr/lib/python3/dist-packages (from ipykernel->jupyter) (7.13.0)
Collecting widgetsnbextension~=3.5.0
Using cached widgetsnbextension-3.5.1-py2.py3-none-any.whl (2.2 MB)
Collecting testpath
Using cached testpath-0.4.4-py2.py3-none-any.whl (163 kB)
Requirement already satisfied: mistune<2,>=0.8.1 in /usr/lib/python3/dist-packages (from nbconvert->jupyter) (0.8.4)
Collecting pandocfilters>=1.4.1
Using cached pandocfilters-1.4.2.tar.gz (14 kB)
Collecting bleach
Downloading bleach-3.1.5-py2.py3-none-any.whl (151 kB)
|████████████████████████████████| 151 kB 1.3 MB/s
Collecting entrypoints>=0.2.2
Using cached entrypoints-0.3-py2.py3-none-any.whl (11 kB)
Collecting defusedxml
Using cached defusedxml-0.6.0-py2.py3-none-any.whl (23 kB)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /usr/lib/python3/dist-packages (from jupyter-console->jupyter) (3.0.5)
Requirement already satisfied: python-dateutil>=2.1 in /usr/lib/python3/dist-packages (from jupyter-client>=5.3.4->notebook->jupyter) (2.8.1)
Collecting ptyprocess; os_name != "nt"
Using cached ptyprocess-0.6.0-py2.py3-none-any.whl (39 kB)
Requirement already satisfied: pexpect in /usr/lib/python3/dist-packages (from ipython>=5.0.0->ipykernel->jupyter) (4.6.0)
Requirement already satisfied: webencodings in /usr/lib/python3/dist-packages (from bleach->nbconvert->jupyter) (0.5.1)
Requirement already satisfied: packaging in /usr/lib/python3/dist-packages (from bleach->nbconvert->jupyter) (20.3)
Requirement already satisfied: six>=1.9.0 in /usr/lib/python3/dist-packages (from bleach->nbconvert->jupyter) (1.14.0)
Building wheels for collected packages: pandocfilters
Building wheel for pandocfilters (setup.py) ... done
Created wheel for pandocfilters: filename=pandocfilters-1.4.2-py3-none-any.whl size=7856 sha256=d120576d72839490baf2013615ee01ad685c9e81b3a8f3068b4597432d8a6b36
Stored in directory: /home/kali/.cache/pip/wheels/f6/08/65/e4636b703d0e870cd62692dafd6b47db27287fe80cea433722
Successfully built pandocfilters
Installing collected packages: pyzmq, jupyter-client, ipykernel, Send2Trash, testpath, pandocfilters, bleach, entrypoints, defusedxml, nbconvert, prometheus-client, ptyprocess, terminado, notebook, qtpy, qtconsole, widgetsnbextension, ipywidgets, jupyter-console, jupyter
Successfully installed Send2Trash-1.5.0 bleach-3.1.5 defusedxml-0.6.0 entrypoints-0.3 ipykernel-5.3.3 ipywidgets-7.5.1 jupyter-1.0.0 jupyter-client-6.1.6 jupyter-console-6.1.0 nbconvert-5.6.1 notebook-6.0.3 pandocfilters-1.4.2 prometheus-client-0.8.0 ptyprocess-0.6.0 pyzmq-19.0.1 qtconsole-4.7.5 qtpy-1.9.0 terminado-0.8.3 testpath-0.4.4 widgetsnbextension-3.5.1
升级Juypter客户端:

代码: 全选

~$ pip3 install --upgrade jupyter_client

Requirement already up-to-date: jupyter_client in ./.local/lib/python3.8/site-packages (6.1.6)
Requirement already satisfied, skipping upgrade: jupyter-core>=4.6.0 in /usr/lib/python3/dist-packages (from jupyter_client) (4.6.3)
Requirement already satisfied, skipping upgrade: traitlets in /usr/lib/python3/dist-packages (from jupyter_client) (4.3.3)
Requirement already satisfied, skipping upgrade: python-dateutil>=2.1 in /usr/lib/python3/dist-packages (from jupyter_client) (2.8.1)
Requirement already satisfied, skipping upgrade: tornado>=4.1 in /usr/lib/python3/dist-packages (from jupyter_client) (5.1.1)
Requirement already satisfied, skipping upgrade: pyzmq>=13 in ./.local/lib/python3.8/site-packages (from jupyter_client) (19.0.1)
 
步骤6:安装Jupyter Notebook MicroPython内核

我们需要为Jupyter Notebook安装MicroPython内核。我们可以使用以下代码Jupyter MicroPython Kernel GitHub存储库中获取它:

代码: 全选

~$ git clone https://github.com/goatchurchprime/jupyter_micropython_kernel.git

Cloning into 'jupyter_micropython_kernel'...
remote: Enumerating objects: 340, done.
remote: Total 340 (delta 0), reused 0 (delta 0), pack-reused 340
Receiving objects: 100% (340/340), 108.88 KiB | 1.25 MiB/s, done.
Resolving deltas: 100% (240/240), done.
接下来,使用(如果出现问题,请使用sudo)将其安装到Python3:

代码: 全选

~$ pip3 install -e jupyter_micropython_kernel

Obtaining file:///home/kali/jupyter_micropython_kernel
Requirement already satisfied: pyserial>=3.4 in /usr/lib/python3/dist-packages (from jupyter-micropython-kernel==0.1.3) (3.4)
Requirement already satisfied: websocket-client>=0.44 in /usr/lib/python3/dist-packages (from jupyter-micropython-kernel==0.1.3) (0.53.0)
Installing collected packages: jupyter-micropython-kernel
Running setup.py develop for jupyter-micropython-kernel
Successfully installed jupyter-micropython-kernel
现在,将内核安装到Jupyter中:

代码: 全选

~$ python3 -m jupyter_micropython_kernel.install

Installing IPython kernel spec of micropython
/home/kali/jupyter_micropython_kernel/jupyter_micropython_kernel/install.py:29: DeprecationWarning: replace is ignored. Installing a kernelspec always replaces an existing installation
k.install_kernel_spec(td, 'Micropython', user=user, replace=True, prefix=prefix)
...into /home/kali/.local/share/jupyter/kernels/micropython
Jupyter现在应该可以使用MicroPython了。


步骤7:打开Jupyter Notebook

现在,要打开Jupyter Notebook的Web界面,请使用:
 

代码: 全选

~$ jupyter notebook
如果您将Jupyter Notebook安装到本地,请改用以下方法:
 

代码: 全选

~$ ~/.local/bin/jupyter-notebook
或者将export PATH = $ PATH:〜/ .local / bin添加到您的〜/ .bashrc文件中,然后再次尝试jupyter。在打开之前,您可能需要输入密码。之后,您应该在网络浏览器中看到如下所示的界面。

图片


步骤8:打开MicroPython Jupyter Notebook

要创建MicroPython文件,请单击“新建”,然后选择“ MicroPython-USB”作为要创建的文档类型。图片

现在,我们应该在空白的Jupyter Notebook中。我们可以使用加号(+)按钮添加要运行的单元格,然后使用播放按钮来运行单元格以查看我们的代码如何运行。但是,在此之前,我们需要添加一个单元以进行连接。


步骤9:通过网络控制电动机

在Jupyter Notebook MicroPython文件中,将以下内容用作第一个块,并确保使用串行端口而不是我的串行端口。开始连接后,如果它说“就绪”,则可以继续。

代码: 全选

%serialconnect to --port=/dev/cu.wchusbserial14630 --baud=115200
现在,在第二个步骤中,让我们创建一个小任务来打开和关闭电动机电源。首先,请确保您从机器导入针导入并且从时间导入sleep导入。然后,将“ motor”的值定义为1True以将其打开,将0False定义为将其关闭。对于“睡眠”,我使用0.2秒来激活它。您可以按“运行”以观看其运行。
 

代码: 全选

from machine import Pin
from time import sleep
motor = Pin(5, Pin.OUT)
motor.value(1)
sleep(0.2)
motor.value(0)
sleep(0.2)
如果您想循环,我们可以开始使用不同的Python数据结构,这些数据结构非常有趣,简短和容易,并且不需要Arduino IDE所做的任何事情。因此,让我们看看是否可以连续打开和关闭电动机10次:
 

代码: 全选

from time import sleep
for i in range(10):
motor.on()
sleep(0.5)
motor.off()
sleep(0.5)
如果要停止代码,可以随时按停止图标,如果它一直在运行,则可以按该图标来中断它并提交新代码如果您是初学者或已经熟悉Python的人,MicroPython可能是开始与笔记本电脑中的电子设备进行交互的最佳方法-旋转电机,发光的激光以及进行其他各种有趣的事情。

 
 
 
 

回复

  • 随机主题
    回复总数
    阅读次数
    最新文章