🇬🇧 English
The kit on this page is the Keyestudio IoT Smart Home Kit for ESP32, model KS5009. Its board is an ESP32, a small computer with Wi-Fi. Keyestudio's own guide says the kit works with two languages: C, and MicroPython. MicroPython is Python made small enough for a board like this one.
Keyestudio sells an older Smart Home kit too, model KS0085, with an Arduino board. Its guide has no Python, so this page is not for that kit.
Keyestudio's Python lessons end when the house joins the Wi-Fi and prints its address. This page goes one step more: a program on your computer controls the house.
🇹🇭 ไทย
ชุดคิตในหน้านี้คือ Keyestudio IoT Smart Home Kit for ESP32 รุ่น KS5009 บอร์ดของมันคือ ESP32 ซึ่งเป็นคอมพิวเตอร์ตัวเล็กที่มี Wi-Fi คู่มือของ Keyestudio เองบอกว่าชุดคิตนี้ใช้ได้กับสองภาษา คือ C และ MicroPython MicroPython คือ Python ที่ถูกทำให้เล็กพอสำหรับบอร์ดแบบนี้
Keyestudio ขายชุด Smart Home รุ่นเก่ากว่าด้วย คือรุ่น KS0085 ที่ใช้บอร์ด Arduino คู่มือของรุ่นนั้นไม่มี Python หน้านี้จึงไม่ใช่สำหรับชุดนั้น
บทเรียน Python ของ Keyestudio จบตรงที่บ้านเชื่อมต่อ Wi-Fi แล้วพิมพ์ที่อยู่ของมันออกมา หน้านี้ไปต่ออีกหนึ่งขั้น คือโปรแกรมบนคอมพิวเตอร์ของคุณควบคุมบ้าน
🇨🇳 中文
这一页用的套件是 Keyestudio IoT Smart Home Kit for ESP32,型号 KS5009。它的开发板是 ESP32,一台带 Wi-Fi 的小电脑。Keyestudio 自己的指南说,这个套件支持两种语言:C 和 MicroPython。MicroPython 是缩小到能在这种开发板上运行的 Python。
Keyestudio 还卖一款更早的智能小屋套件,型号 KS0085,用的是 Arduino 开发板。它的指南里没有 Python,所以这一页不适用于那款套件。
Keyestudio 的 Python 课程讲到小屋连上 Wi-Fi、打印出自己的地址就结束了。这一页再往前走一步:让你电脑上的程序来控制小屋。
Each part of the house is joined to one pin of the board. A pin is one of the board's metal contacts, and it has a number. Your code names a part by the number of its pin. These are the pins that Keyestudio's guide gives for the parts this page uses.ชิ้นส่วนแต่ละชิ้นของบ้านต่ออยู่กับพินหนึ่งขาของบอร์ด พินคือขาโลหะขาหนึ่งของบอร์ด และมีหมายเลข โค้ดของคุณเรียกชิ้นส่วนด้วยหมายเลขพินของมัน ต่อไปนี้คือพินที่คู่มือของ Keyestudio ระบุไว้สำหรับชิ้นส่วนที่หน้านี้ใช้小屋的每个部件都接在开发板的一个引脚上。引脚是开发板上的一个金属触点,它有一个编号。你的代码用引脚的编号来指定部件。下面是 Keyestudio 的指南给出的引脚,只列出这一页用到的部件。
| Partชิ้นส่วน部件 | Pinพิน引脚 | What your code does with itโค้ดของคุณทำอะไรกับมัน你的代码怎么用它 |
|---|---|---|
| Yellow light (LED)ไฟสีเหลือง (LED)黄灯(LED) | 12 | Sends 1 for on and 0 for off.ส่ง 1 เพื่อเปิด และ 0 เพื่อปิด发送 1 是开,发送 0 是关。 |
| Left button, right buttonปุ่มซ้าย ปุ่มขวา左按钮、右按钮 | 16, 27 | Reads 1 at rest and 0 while the button is pressed.อ่านได้ 1 ตอนไม่มีใครกด และ 0 ขณะที่ปุ่มถูกกด没人按时读到 1,按住时读到 0。 |
| Motion sensorเซนเซอร์ตรวจจับการเคลื่อนไหว人体移动传感器 | 14 | Reads 1 when somebody moves in front of it.อ่านได้ 1 เมื่อมีคนขยับอยู่ข้างหน้า有人在它前面移动时读到 1。 |
| Temperature and humidity sensorเซนเซอร์อุณหภูมิและความชื้น温湿度传感器 | 17 | Asks it to measure, then reads two numbers.สั่งให้มันวัด แล้วอ่านตัวเลขสองตัว先让它测量,再读出两个数字。 |
| Door motorมอเตอร์ประตู门的电机 | 13 | Sends pulses. The length of a pulse sets the angle.ส่งพัลส์ ความยาวของพัลส์กำหนดมุม发送脉冲。脉冲的长短决定角度。 |
| Fan motorมอเตอร์พัดลม风扇电机 | 19, 18 | Gives power to one of its two wires to make it turn.จ่ายไฟให้สายเส้นใดเส้นหนึ่งจากสองเส้น เพื่อให้มันหมุน给它两根线中的一根通电,它就转起来。 |
The kit has more parts that this page does not use: a motor for the window on pin 5, a buzzer on pin 25, four coloured lights on pin 26, a gas sensor on pin 23, a rain sensor on pin 34, a small screen and a card reader. Keyestudio's guide has a lesson for each.ชุดคิตยังมีชิ้นส่วนอื่นที่หน้านี้ไม่ได้ใช้ ได้แก่ มอเตอร์หน้าต่างที่พิน 5 บัซเซอร์ที่พิน 25 ไฟสีสี่ดวงที่พิน 26 เซนเซอร์แก๊สที่พิน 23 เซนเซอร์ฝนที่พิน 34 จอเล็ก ๆ และเครื่องอ่านบัตร คู่มือของ Keyestudio มีบทเรียนสำหรับแต่ละชิ้น套件里还有这一页没有用到的部件:引脚 5 上的窗户电机、引脚 25 上的蜂鸣器、引脚 26 上的四颗彩灯、引脚 23 上的燃气传感器、引脚 34 上的雨水传感器、一块小屏幕和一个读卡器。Keyestudio 的指南里,每个部件都有一课。
| Wordคำ词 | What it meansความหมาย意思 |
|---|---|
| board | The small computer inside the house. All the parts are joined to it.บอร์ด: คอมพิวเตอร์ตัวเล็กที่อยู่ในบ้าน ชิ้นส่วนทุกชิ้นต่ออยู่กับมัน开发板:小屋里的那台小电脑。所有部件都接在它上面。 |
| MicroPython | Python made small enough to run on a board.Python ที่ถูกทำให้เล็กพอที่จะรันบนบอร์ดได้缩小到能在开发板上运行的 Python。 |
| firmware | The program that is always on a board. Here the firmware is MicroPython itself.เฟิร์มแวร์: โปรแกรมที่อยู่บนบอร์ดตลอดเวลา ในที่นี้เฟิร์มแวร์ก็คือ MicroPython เอง固件:一直留在开发板上的程序。在这里,固件就是 MicroPython 本身。 |
| pin | One numbered contact of the board. A part is joined to a pin.พิน: ขาที่มีหมายเลขของบอร์ด ชิ้นส่วนหนึ่งชิ้นต่ออยู่กับพิน引脚:开发板上一个带编号的触点。部件接在引脚上。 |
| sensor | A part that measures something, such as movement or temperature.เซนเซอร์: ชิ้นส่วนที่วัดบางอย่าง เช่น การเคลื่อนไหวหรืออุณหภูมิ传感器:用来测量某样东西的部件,比如移动或温度。 |
| servo | A motor that turns to an angle you choose and stays there. The door has one.เซอร์โว: มอเตอร์ที่หมุนไปยังมุมที่คุณเลือกแล้วค้างอยู่ตรงนั้น ประตูมีเซอร์โวหนึ่งตัว舵机:一种电机,转到你选定的角度后就停在那里。门上有一个。 |
| duty | How much of the time a pin has power. In this page's code it is a number from 0 to 1023.ดิวตี: สัดส่วนของเวลาที่พินมีไฟ ในโค้ดของหน้านี้เป็นตัวเลขตั้งแต่ 0 ถึง 1023占空比:引脚通电的时间占多少。在这一页的代码里,它是 0 到 1023 之间的一个数。 |
| request, reply | A request is one question sent to a program over a network. The reply is what comes back.คำขอคือคำถามหนึ่งข้อที่ส่งไปยังโปรแกรมผ่านเครือข่าย คำตอบคือสิ่งที่ส่งกลับมา请求是通过网络发给一个程序的一个问题。回复是返回来的内容。 |
| address | The numbers that find one device on a network, such as 192.168.1.50.ที่อยู่: ตัวเลขที่ใช้หาอุปกรณ์หนึ่งเครื่องบนเครือข่าย เช่น 192.168.1.50地址:在网络上找到一台设备的那串数字,比如 192.168.1.50。 |
| port | The number after the colon in an address, such as 8080. It picks one program on that device.พอร์ต: ตัวเลขหลังเครื่องหมายโคลอนในที่อยู่ เช่น 8080 มันเลือกโปรแกรมหนึ่งตัวบนอุปกรณ์เครื่องนั้น端口:地址里冒号后面的数字,比如 8080。它指定那台设备上的某一个程序。 |
| JSON | A way to write data as text. It looks much like a Python dictionary.วิธีเขียนข้อมูลเป็นข้อความ หน้าตาคล้ายดิกชันนารีของ Python มาก一种把数据写成文字的方式。它看起来很像 Python 的字典。 |
🧰 Part 1 — Get ready · ส่วนที่ 1 — เตรียมตัว · 第 1 部分 —— 做好准备
🇬🇧 English
Build the house first, with the pictures in Keyestudio's guide. The guide also shows which wire goes to which pin. This part then puts MicroPython on the board.
🇹🇭 ไทย
ประกอบบ้านก่อน โดยดูรูปในคู่มือของ Keyestudio คู่มือยังบอกด้วยว่าสายเส้นไหนต่อกับพินไหน จากนั้นส่วนนี้จะใส่ MicroPython ลงบนบอร์ด
🇨🇳 中文
先照着 Keyestudio 的指南里的图片把小屋装好。指南也说明了哪根线接哪个引脚。然后这一部分把 MicroPython 装到开发板上。
1 Put MicroPython on the board · ใส่ MicroPython ลงบนบอร์ด · 把 MicroPython 装到开发板上
🇬🇧 English
You need Python on your computer, and two free tools that Python installs. esptool puts MicroPython on the board. mpremote sends your files to the board and shows what the board prints. You write the files with vim, or with any editor you like. Start Python teaches the terminal and vim.
Open a terminal and install the two tools:
🇹🇭 ไทย
คุณต้องมี Python บนคอมพิวเตอร์ และเครื่องมือฟรีสองตัวที่ Python ติดตั้งให้ esptool ใส่ MicroPython ลงบนบอร์ด mpremote ส่งไฟล์ของคุณไปที่บอร์ด และแสดงสิ่งที่บอร์ดพิมพ์ออกมา คุณเขียนไฟล์ด้วย vim หรือโปรแกรมแก้ไขตัวใดก็ได้ที่คุณชอบ Start Python สอนเรื่องเทอร์มินัลและ vim
เปิดเทอร์มินัล แล้วติดตั้งเครื่องมือสองตัวนี้:
🇨🇳 中文
你的电脑上需要有 Python,还需要 Python 能安装的两个免费工具。esptool 把 MicroPython 装到开发板上。mpremote 把你的文件发到开发板上,并显示开发板打印的内容。文件用 vim 来写,或者用你喜欢的任何编辑器。Start Python 教终端和 vim。
打开终端,安装这两个工具:
python -m pip install esptool mpremote
🇬🇧 English
Join the house to your computer with the USB cable. The next command lists the serial ports of your computer. A serial port is the line that the cable makes between the computer and the board. The board should be on the list. Keyestudio's guide shows its port with the name CH340.
🇹🇭 ไทย
ต่อบ้านเข้ากับคอมพิวเตอร์ของคุณด้วยสาย USB คำสั่งถัดไปแสดงรายการพอร์ตอนุกรมของคอมพิวเตอร์ พอร์ตอนุกรมคือเส้นทางที่สายสร้างขึ้นระหว่างคอมพิวเตอร์กับบอร์ด บอร์ดควรอยู่ในรายการ คู่มือของ Keyestudio แสดงพอร์ตของมันด้วยชื่อ CH340
🇨🇳 中文
用 USB 线把小屋接到你的电脑上。下一条命令列出电脑的串口。串口就是这根线在电脑和开发板之间建立的通道。开发板应该在列表里。Keyestudio 的指南显示它的端口名字是 CH340。
python -m mpremote devs
🇬🇧 English
Download the MicroPython firmware for the ESP32 from micropython.org/download/esp32. It is one file with a name that ends in .bin. Keyestudio's guide uses version 1.17, and the kit's own download holds that file too.
Open the terminal in the folder where that file is, and run the two commands below. The first one empties the board's memory. The second one writes MicroPython into it. Write the name of your own file in the place of firmware.bin.
🇹🇭 ไทย
ดาวน์โหลดเฟิร์มแวร์ MicroPython สำหรับ ESP32 จาก micropython.org/download/esp32 มันเป็นไฟล์เดียวที่ชื่อลงท้ายด้วย .bin คู่มือของ Keyestudio ใช้เวอร์ชัน 1.17 และไฟล์ดาวน์โหลดของชุดคิตเองก็มีไฟล์นั้นอยู่ด้วย
เปิดเทอร์มินัลในโฟลเดอร์ที่ไฟล์นั้นอยู่ แล้วรันสองคำสั่งข้างล่าง คำสั่งแรกล้างหน่วยความจำของบอร์ด คำสั่งที่สองเขียน MicroPython ลงไป ให้เขียนชื่อไฟล์ของคุณเองแทน firmware.bin
🇨🇳 中文
从 micropython.org/download/esp32 下载 ESP32 用的 MicroPython 固件。它是一个名字以 .bin 结尾的文件。Keyestudio 的指南用的是 1.17 版,套件自带的下载包里也有这个文件。
在这个文件所在的文件夹里打开终端,运行下面两条命令。第一条清空开发板的存储。第二条把 MicroPython 写进去。把 firmware.bin 换成你自己的文件名。
not run by usเราไม่ได้รันเอง我们没有运行过
python -m esptool erase-flash
python -m esptool --baud 460800 write-flash 0x1000 firmware.bin
🇬🇧 English
Now test it. This command runs one line of Python on the board, and the answer comes back to your terminal. If you see the words, the house is ready.
🇹🇭 ไทย
ตอนนี้ลองทดสอบ คำสั่งนี้รัน Python หนึ่งบรรทัดบนบอร์ด และคำตอบจะกลับมาที่เทอร์มินัลของคุณ ถ้าคุณเห็นข้อความนั้น บ้านก็พร้อมแล้ว
🇨🇳 中文
现在测试一下。这条命令在开发板上运行一行 Python,回答会回到你的终端。如果你看到了这句话,小屋就准备好了。
not run by usเราไม่ได้รันเอง我们没有运行过
python -m mpremote exec "print('hello from the house')"
--port COM4 or the name that mpremote devs showed. If the writing stops half way, leave out --baud 460800, which makes it slower. If esptool still cannot connect, hold down the button marked BOOT, press and let go of the button marked RESET, then let go of BOOT.The commands on this page are written the way esptool 5.5.0 and mpremote 1.29.0 write them in their own help. MicroPython's documentation writes two of them the older way, erase_flash and write_flash.
เอกสารของ MicroPython แนะนำสามอย่างให้ลอง ถ้า esptool หาบอร์ดไม่เจอ ให้บอกพอร์ตด้วย --port COM4 หรือชื่อที่ mpremote devs แสดง ถ้าการเขียนหยุดกลางคัน ให้ตัด --baud 460800 ออก ซึ่งจะทำให้ช้าลง ถ้า esptool ยังเชื่อมต่อไม่ได้ ให้กดปุ่มที่เขียนว่า BOOT ค้างไว้ กดแล้วปล่อยปุ่มที่เขียนว่า RESET จากนั้นจึงปล่อย BOOTคำสั่งในหน้านี้เขียนตามแบบที่ esptool 5.5.0 และ mpremote 1.29.0 เขียนไว้ในหน้าช่วยเหลือของตัวเอง เอกสารของ MicroPython เขียนสองคำสั่งในแบบเก่า คือ erase_flash และ write_flash
MicroPython 的文档给了三个可以尝试的办法。如果 esptool 找不到开发板,用 --port COM4 告诉它端口,或者用 mpremote devs 显示的名字。如果写到一半停了,去掉 --baud 460800,这样会慢一些。如果 esptool 还是连不上,按住标着 BOOT 的按钮,按一下再松开标着 RESET 的按钮,然后松开 BOOT。这一页的命令,是按 esptool 5.5.0 和 mpremote 1.29.0 在自己的帮助里的写法写的。MicroPython 的文档把其中两条写成旧的形式:erase_flash 和 write_flash。
💡 Part 2 — Python on the board · ส่วนที่ 2 — Python บนบอร์ด · 第 2 部分 —— 开发板上的 Python
🇬🇧 English
The three files of this part run on the house itself. Write each one on your computer, for example with vim led.py. Then mpremote run sends the file to the board, runs it there, and shows in your terminal what the board prints.
🇹🇭 ไทย
ไฟล์สามไฟล์ของส่วนนี้รันบนตัวบ้านเอง ให้เขียนแต่ละไฟล์บนคอมพิวเตอร์ของคุณ เช่น ด้วย vim led.py จากนั้น mpremote run จะส่งไฟล์ไปที่บอร์ด รันมันที่นั่น และแสดงสิ่งที่บอร์ดพิมพ์ออกมาในเทอร์มินัลของคุณ
🇨🇳 中文
这一部分的三个文件在小屋自己身上运行。每个文件都在你的电脑上写,比如用 vim led.py。然后 mpremote run 把文件发到开发板上,在那里运行,并在你的终端里显示开发板打印的内容。
2 led.py: switch the light · led.py: เปิดปิดไฟ · led.py:开灯和关灯
🇬🇧 English
Pin(12, Pin.OUT) gives your code pin 12 as an output. An output is a pin that your code sends power to. The yellow light is on that pin.
led.value(1) switches the power on, and the light shines. led.value(0) switches it off. time.sleep(1) waits one second. The loop does this three times.
🇹🇭 ไทย
Pin(12, Pin.OUT) ให้โค้ดของคุณใช้พิน 12 เป็นเอาต์พุต เอาต์พุตคือพินที่โค้ดของคุณส่งไฟออกไป ไฟสีเหลืองอยู่ที่พินนั้น
led.value(1) เปิดไฟฟ้า แล้วหลอดไฟก็สว่าง led.value(0) ปิดมัน time.sleep(1) รอหนึ่งวินาที ลูปทำแบบนี้สามรอบ
🇨🇳 中文
Pin(12, Pin.OUT) 把引脚 12 交给你的代码,作为输出。输出就是你的代码向它送电的引脚。黄灯接在这个引脚上。
led.value(1) 接通电,灯就亮了。led.value(0) 把它关掉。time.sleep(1) 等一秒钟。循环把这些做三遍。
led.py
import time
from machine import Pin
# The yellow light of the house is on pin 12.
led = Pin(12, Pin.OUT)
for count in range(3):
led.value(1)
print("light on")
time.sleep(1)
led.value(0)
print("light off")
time.sleep(1)
not run by usเราไม่ได้รันเอง我们没有运行过
python -m mpremote run led.py
what it should printสิ่งที่มันควรพิมพ์ออกมา它应该打印的内容 · not run on a kit by us · เราไม่ได้รันบนชุดคิต · 我们没有在套件上运行过
light on
light off
light on
light off
light on
light off
🇬🇧 English
The yellow light goes on and off three times, and your terminal shows these six lines.
🇹🇭 ไทย
ไฟสีเหลืองจะติดและดับสามครั้ง และเทอร์มินัลของคุณจะแสดงหกบรรทัดนี้
🇨🇳 中文
黄灯会亮灭三次,你的终端会显示这六行。
3 sensors.py: read the house · sensors.py: อ่านค่าจากบ้าน · sensors.py:读取小屋的数据
🇬🇧 English
Pin.IN makes a pin an input. An input is a pin that your code reads. value() with nothing between the brackets reads it, and gives 0 or 1.
A button reads 1 when nobody touches it, and 0 while it is pressed. That is the other way round from what you may expect. Pin.PULL_UP is what holds the pin at 1 while the button is at rest.
The temperature and humidity sensor is not read with value(). dht is the part of MicroPython that knows how to talk to it. measure() asks the sensor for one measurement. After that, temperature() and humidity() give the two numbers.
🇹🇭 ไทย
Pin.IN ทำให้พินเป็นอินพุต อินพุตคือพินที่โค้ดของคุณอ่านค่า value() ที่ไม่มีอะไรในวงเล็บจะอ่านค่าของมัน และได้ 0 หรือ 1
ปุ่มอ่านได้ 1 ตอนที่ไม่มีใครแตะ และ 0 ขณะที่ถูกกด ซึ่งกลับกันกับที่คุณอาจคาดไว้ Pin.PULL_UP คือสิ่งที่ตรึงพินไว้ที่ 1 ขณะที่ปุ่มไม่ถูกกด
เซนเซอร์อุณหภูมิและความชื้นไม่ได้อ่านด้วย value() dht คือส่วนของ MicroPython ที่รู้วิธีคุยกับมัน measure() สั่งให้เซนเซอร์วัดหนึ่งครั้ง หลังจากนั้น temperature() และ humidity() จะให้ตัวเลขสองตัว
🇨🇳 中文
Pin.IN 把引脚设为输入。输入就是你的代码去读取的引脚。括号里什么都不写的 value() 读取它,得到 0 或 1。
按钮没人碰的时候读到 1,按住的时候读到 0。这和你想的可能正好相反。Pin.PULL_UP 的作用,就是在按钮没被按下时把引脚保持在 1。
温湿度传感器不是用 value() 读的。dht 是 MicroPython 里懂得怎么和它通信的那一部分。measure() 让传感器测量一次。之后,temperature() 和 humidity() 给出两个数字。
sensors.py
import dht
from machine import Pin
# A button reads 1 when nobody touches it, and 0 while it is pressed.
left_button = Pin(16, Pin.IN, Pin.PULL_UP)
right_button = Pin(27, Pin.IN, Pin.PULL_UP)
# The motion sensor reads 1 when somebody moves in front of it.
motion = Pin(14, Pin.IN)
# One part measures both the temperature and the humidity.
climate = dht.DHT11(Pin(17))
climate.measure()
print("left button:", left_button.value())
print("right button:", right_button.value())
print("motion:", motion.value())
print("temperature:", climate.temperature(), "C")
print("humidity:", climate.humidity(), "%")
not run by usเราไม่ได้รันเอง我们没有运行过
python -m mpremote run sensors.py
what it should printสิ่งที่มันควรพิมพ์ออกมา它应该打印的内容 · not run on a kit by us · เราไม่ได้รันบนชุดคิต · 我们没有在套件上运行过
left button: 1
right button: 1
motion: 1
temperature: 31 C
humidity: 62 %
🇬🇧 English
Your numbers will be your own. Hold a button down and run the file again, and its line shows 0. Keyestudio's guide says that when you breathe on the sensor, the humidity goes up.
🇹🇭 ไทย
ตัวเลขของคุณจะเป็นค่าของคุณเอง ลองกดปุ่มค้างไว้แล้วรันไฟล์อีกครั้ง บรรทัดของปุ่มนั้นจะแสดง 0 คู่มือของ Keyestudio บอกว่าเมื่อคุณหายใจรดเซนเซอร์ ความชื้นจะสูงขึ้น
🇨🇳 中文
你的数字会是你自己的。按住一个按钮再运行一次文件,它那一行就显示 0。Keyestudio 的指南说,对着传感器哈气,湿度会上升。
4 door.py: open the door · door.py: เปิดประตู · door.py:打开门
🇬🇧 English
The door has a servo, a motor that turns to an angle and stays there. You do not switch a servo on and off. You send it short pulses of power, 50 of them each second, and the length of each pulse tells it the angle.
PWM is how a pin sends pulses. door.freq(50) sets 50 pulses a second. door.duty(...) sets how long each pulse is, as a number from 0 to 1023. Keyestudio's guide gives the numbers for this servo: 25 for 0 degrees, 77 for 90 degrees and 128 for 180 degrees. Its door lessons use 128 for an open door and 25 for a closed one, and this file does the same.
🇹🇭 ไทย
ประตูมีเซอร์โว คือมอเตอร์ที่หมุนไปยังมุมหนึ่งแล้วค้างอยู่ตรงนั้น คุณไม่ได้เปิดปิดเซอร์โว คุณส่งพัลส์ไฟฟ้าสั้น ๆ ให้มัน วินาทีละ 50 ครั้ง และความยาวของแต่ละพัลส์บอกมุมให้มัน
PWM คือวิธีที่พินส่งพัลส์ door.freq(50) ตั้งให้ส่ง 50 พัลส์ต่อวินาที door.duty(...) ตั้งว่าแต่ละพัลส์ยาวเท่าไร เป็นตัวเลขตั้งแต่ 0 ถึง 1023 คู่มือของ Keyestudio ให้ตัวเลขสำหรับเซอร์โวตัวนี้ไว้ คือ 25 สำหรับ 0 องศา 77 สำหรับ 90 องศา และ 128 สำหรับ 180 องศา บทเรียนเรื่องประตูในคู่มือใช้ 128 สำหรับประตูที่เปิด และ 25 สำหรับประตูที่ปิด ไฟล์นี้ก็ทำเหมือนกัน
🇨🇳 中文
门上有一个舵机,就是转到某个角度后停在那里的电机。舵机不是用开和关来控制的。你给它发送很短的电脉冲,每秒 50 个,每个脉冲的长短告诉它角度。
PWM 是引脚发送脉冲的方式。door.freq(50) 设定每秒 50 个脉冲。door.duty(...) 设定每个脉冲有多长,是 0 到 1023 之间的一个数。Keyestudio 的指南给出了这个舵机的数值:25 对应 0 度,77 对应 90 度,128 对应 180 度。指南里关于门的课程用 128 表示门开着,用 25 表示门关着,这个文件也一样。
door.py
import time
from machine import Pin, PWM
DOOR_CLOSED = 25 # the duty that turns the motor to 0 degrees
DOOR_OPEN = 128 # the duty that turns the motor to 180 degrees
# The motor of the door is on pin 13. It wants 50 pulses a second.
door = PWM(Pin(13))
door.freq(50)
door.duty(DOOR_OPEN)
print("door open")
time.sleep(2)
door.duty(DOOR_CLOSED)
print("door closed")
not run by usเราไม่ได้รันเอง我们没有运行过
python -m mpremote run door.py
what it should printสิ่งที่มันควรพิมพ์ออกมา它应该打印的内容 · not run on a kit by us · เราไม่ได้รันบนชุดคิต · 我们没有在套件上运行过
door open
door closed
- In
led.py, make the light flash five times, and faster. - In
door.py, open the door only half way. Which number do you need? - The window has a servo too, on pin 5. Copy
door.pytowindow.pyand make it move the window.
- ใน
led.pyทำให้ไฟกะพริบห้าครั้ง และเร็วขึ้น - ใน
door.pyเปิดประตูแค่ครึ่งเดียว คุณต้องใช้ตัวเลขไหน - หน้าต่างก็มีเซอร์โวเหมือนกัน อยู่ที่พิน 5 คัดลอก
door.pyเป็นwindow.pyแล้วทำให้มันขยับหน้าต่าง
- 在
led.py里,让灯闪五次,并且闪得更快。 - 在
door.py里,只把门打开一半。你需要哪个数字? - 窗户也有一个舵机,在引脚 5 上。把
door.py复制成window.py,让它去动窗户。
📡 Part 3 — Your computer talks to the house · ส่วนที่ 3 — คอมพิวเตอร์ของคุณคุยกับบ้าน · 第 3 部分 —— 你的电脑和小屋对话
🇬🇧 English
Until now, the code that moves the house is on the house. To change what the house does, you send it a new file. In this part the house runs one program that stays on and waits. Any program on the same Wi-Fi can then send it a request, such as "switch the light on", and get a reply.
🇹🇭 ไทย
จนถึงตอนนี้ โค้ดที่ควบคุมบ้านอยู่บนตัวบ้าน ถ้าจะเปลี่ยนสิ่งที่บ้านทำ คุณต้องส่งไฟล์ใหม่ไปให้ ในส่วนนี้บ้านจะรันโปรแกรมหนึ่งตัวที่เปิดค้างไว้และคอยรอ จากนั้นโปรแกรมใดก็ตามบน Wi-Fi เดียวกันจะส่งคำขอให้มันได้ เช่น "เปิดไฟ" แล้วได้คำตอบกลับมา
🇨🇳 中文
到现在为止,控制小屋的代码都在小屋上。想改变小屋做的事,你得给它发一个新文件。在这一部分,小屋运行一个一直开着、一直等待的程序。这样,同一个 Wi-Fi 上的任何程序都可以给它发请求,比如"把灯打开",并收到回复。
5 wifi.py: the house joins your Wi-Fi · wifi.py: บ้านเชื่อมต่อ Wi-Fi ของคุณ · wifi.py:小屋连上你的 Wi-Fi
🇬🇧 English
Write the name of your Wi-Fi and its password in the two lines at the top. network.WLAN(network.STA_IF) gives your code the board's Wi-Fi, as a device that joins a network. active(True) switches it on, and connect starts to join.
Joining takes a moment. The loop waits, half a second at a time, until isconnected() says it is done. Then the file prints the address that your Wi-Fi gave to the house.
🇹🇭 ไทย
เขียนชื่อ Wi-Fi ของคุณและรหัสผ่านลงในสองบรรทัดบนสุด network.WLAN(network.STA_IF) ให้โค้ดของคุณใช้ Wi-Fi ของบอร์ด ในฐานะอุปกรณ์ที่เข้าร่วมเครือข่าย active(True) เปิดมัน และ connect เริ่มเชื่อมต่อ
การเชื่อมต่อใช้เวลาครู่หนึ่ง ลูปจะรอทีละครึ่งวินาที จนกว่า isconnected() จะบอกว่าเสร็จแล้ว จากนั้นไฟล์จะพิมพ์ที่อยู่ที่ Wi-Fi ของคุณให้กับบ้าน
🇨🇳 中文
在最上面两行写上你的 Wi-Fi 名称和密码。network.WLAN(network.STA_IF) 把开发板的 Wi-Fi 交给你的代码,让它作为一台加入网络的设备。active(True) 把它打开,connect 开始连接。
连接需要一点时间。循环每次等半秒,直到 isconnected() 说连好了。然后文件打印出你的 Wi-Fi 分给小屋的地址。
wifi.py
import time
import network
WIFI_NAME = "your-wifi-name"
WIFI_PASSWORD = "your-wifi-password"
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_NAME, WIFI_PASSWORD)
while not wifi.isconnected():
time.sleep(0.5)
# ifconfig() gives four addresses. The first one is the address of the house.
print("address:", wifi.ifconfig()[0])
not run by usเราไม่ได้รันเอง我们没有运行过
python -m mpremote run wifi.py
what it should printสิ่งที่มันควรพิมพ์ออกมา它应该打印的内容 · not run on a kit by us · เราไม่ได้รันบนชุดคิต · 我们没有在套件上运行过
address: 192.168.1.50
🇬🇧 English
This is the address that your Wi-Fi chose for the house. Yours will not be 192.168.1.50. Write yours down. You need it in step 7.
🇹🇭 ไทย
นี่คือที่อยู่ที่ Wi-Fi ของคุณเลือกให้บ้าน ของคุณจะไม่ใช่ 192.168.1.50 จดของคุณไว้ คุณต้องใช้มันในขั้นที่ 7
🇨🇳 中文
这是你的 Wi-Fi 选给小屋的地址。你的不会是 192.168.1.50。把你的地址记下来,第 7 步要用。
6 house.py: the house waits for requests · house.py: บ้านรอรับคำขอ · house.py:小屋等待请求
🇬🇧 English
This is the longest file on the page. Its top sets up the parts, the way steps 2, 3 and 4 did. Then it has four functions.
join_wifi is step 5 as a function. read_house reads the sensors and gives them back as one dictionary. do takes a path, such as /light/on, and does what it says. answer takes one request, finds the path in it, calls do, and sends the reply back as JSON.
The last lines open a listener on port 8080. The loop at the end never stops: it waits for a visitor, answers, and waits for the next one.
🇹🇭 ไทย
นี่คือไฟล์ที่ยาวที่สุดในหน้านี้ ส่วนบนของมันตั้งค่าชิ้นส่วนต่าง ๆ แบบเดียวกับที่ขั้นที่ 2, 3 และ 4 ทำ จากนั้นมีฟังก์ชันสี่ตัว
join_wifi คือขั้นที่ 5 ที่เขียนเป็นฟังก์ชัน read_house อ่านค่าเซนเซอร์แล้วส่งกลับเป็นดิกชันนารีหนึ่งตัว do รับพาธ เช่น /light/on แล้วทำตามที่พาธบอก answer รับคำขอหนึ่งรายการ หาพาธในนั้น เรียก do แล้วส่งคำตอบกลับเป็น JSON
บรรทัดท้าย ๆ เปิดตัวรับฟังที่พอร์ต 8080 ลูปตอนท้ายไม่มีวันหยุด มันรอผู้มาเยือน ตอบ แล้วรอรายต่อไป
🇨🇳 中文
这是这一页最长的文件。它的开头像第 2、3、4 步那样把各个部件准备好。接着有四个函数。
join_wifi 就是写成函数的第 5 步。read_house 读取传感器,把它们作为一个字典交回去。do 接收一个路径,比如 /light/on,并照它说的去做。answer 接收一个请求,找出里面的路径,调用 do,再把回复用 JSON 发回去。
最后几行在端口 8080 上打开一个监听器。结尾的循环永远不会停:它等一位访客,回答,再等下一位。
house.py
import json
import socket
import time
import dht
import network
from machine import Pin, PWM
WIFI_NAME = "your-wifi-name"
WIFI_PASSWORD = "your-wifi-password"
PORT = 8080
DOOR_CLOSED = 25 # the duty that turns the door motor to 0 degrees
DOOR_OPEN = 128 # the duty that turns the door motor to 180 degrees
FAN_SPEED = 700 # 0 is stopped and 1023 is the fastest
led = Pin(12, Pin.OUT)
motion = Pin(14, Pin.IN)
left_button = Pin(16, Pin.IN, Pin.PULL_UP)
right_button = Pin(27, Pin.IN, Pin.PULL_UP)
climate = dht.DHT11(Pin(17))
door = PWM(Pin(13))
door.freq(50)
# The fan motor has two wires. It turns when only one of them has power.
fan_a = PWM(Pin(19), 10000)
fan_b = PWM(Pin(18), 10000)
def join_wifi():
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_NAME, WIFI_PASSWORD)
while not wifi.isconnected():
time.sleep(0.5)
return wifi.ifconfig()[0]
def read_house():
climate.measure()
return {
"temperature": climate.temperature(),
"humidity": climate.humidity(),
"motion": motion.value(),
"left_button": left_button.value(),
"right_button": right_button.value(),
"light": led.value(),
}
def do(path):
if path == "/read":
return read_house()
if path == "/light/on":
led.value(1)
elif path == "/light/off":
led.value(0)
elif path == "/door/open":
door.duty(DOOR_OPEN)
elif path == "/door/close":
door.duty(DOOR_CLOSED)
elif path == "/fan/on":
fan_a.duty(0)
fan_b.duty(FAN_SPEED)
elif path == "/fan/off":
fan_a.duty(0)
fan_b.duty(0)
else:
return None
return {"done": path}
def answer(visitor):
# A request begins with a line such as "GET /read HTTP/1.1".
words = visitor.recv(1024).decode().split(" ")
if len(words) < 2:
# A browser sometimes connects and sends nothing.
visitor.close()
return
path = words[1]
print("asked for", path)
reply = do(path)
status = "200 OK"
if reply is None:
status = "404 Not Found"
reply = {"error": "the house does not know " + path}
head = "HTTP/1.0 " + status + "\r\nContent-Type: application/json\r\n\r\n"
visitor.send((head + json.dumps(reply)).encode())
visitor.close()
print("address:", join_wifi())
door.duty(DOOR_CLOSED)
listener = socket.socket()
# Without this line, a second start soon after the first can fail.
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind(socket.getaddrinfo("0.0.0.0", PORT)[0][-1])
listener.listen(1)
print("the house is listening on port", PORT)
while True:
visitor, visitor_address = listener.accept()
answer(visitor)
| Pathพาธ路径 | What the house doesบ้านทำอะไร小屋做什么 |
|---|---|
/read | Reads the sensors and the light, and sends them back.อ่านค่าเซนเซอร์และสถานะไฟ แล้วส่งกลับมา读取传感器和灯的状态,再发回来。 |
/light/on, /light/off | Switches the yellow light.เปิดหรือปิดไฟสีเหลือง开关黄灯。 |
/door/open, /door/close | Turns the door's servo.หมุนเซอร์โวของประตู转动门的舵机。 |
/fan/on, /fan/off | Starts and stops the fan.เปิดและหยุดพัดลม启动和停止风扇。 |
🇬🇧 English
Open the file with vim and write your Wi-Fi name and password at the top. Then start it. Leave that terminal open: the house answers for as long as the command runs. To stop the house, close the terminal.
🇹🇭 ไทย
เปิดไฟล์ด้วย vim แล้วเขียนชื่อ Wi-Fi และรหัสผ่านของคุณไว้ด้านบน จากนั้นเปิดมัน เปิดเทอร์มินัลนั้นทิ้งไว้ บ้านจะตอบตราบเท่าที่คำสั่งยังรันอยู่ ถ้าจะหยุดบ้าน ให้ปิดเทอร์มินัล
🇨🇳 中文
用 vim 打开文件,在开头写上你的 Wi-Fi 名称和密码。然后启动它。让那个终端一直开着:命令运行多久,小屋就回答多久。想让小屋停下,就关掉终端。
not run by usเราไม่ได้รันเอง我们没有运行过
python -m mpremote run house.py
what it should printสิ่งที่มันควรพิมพ์ออกมา它应该打印的内容 · not run on a kit by us · เราไม่ได้รันบนชุดคิต · 我们没有在套件上运行过
address: 192.168.1.50
the house is listening on port 8080
asked for /light/on
asked for /read
asked for /light/off
asked for /read
asked for /fan/on
asked for /light/on
asked for /door/open
asked for /garden
🇬🇧 English
The first two lines come at once. The house printed each of the other lines later, when a request arrived. Steps 7 and 8 sent those requests.
You can send a request with no program at all. Open a web browser on a computer that is on the same Wi-Fi, and go to http://192.168.1.50:8080/read, with your house's address in the place of 192.168.1.50.
The last line of the output is a path that the house does not know. We sent it to see the reply:
🇹🇭 ไทย
สองบรรทัดแรกออกมาทันที ส่วนบรรทัดอื่น ๆ บ้านพิมพ์ทีหลัง เมื่อมีคำขอเข้ามา ขั้นที่ 7 และ 8 เป็นผู้ส่งคำขอเหล่านั้น
คุณส่งคำขอได้โดยไม่ต้องมีโปรแกรมเลย เปิดเว็บเบราว์เซอร์บนคอมพิวเตอร์ที่อยู่บน Wi-Fi เดียวกัน แล้วไปที่ http://192.168.1.50:8080/read โดยเขียนที่อยู่ของบ้านคุณแทน 192.168.1.50
บรรทัดสุดท้ายของผลลัพธ์คือพาธที่บ้านไม่รู้จัก เราส่งไปเพื่อดูคำตอบ:
🇨🇳 中文
前两行是马上出现的。其余每一行都是后来有请求到达时小屋打印的。那些请求是第 7 步和第 8 步发的。
不用任何程序也能发请求。在同一个 Wi-Fi 上的电脑上打开浏览器,访问 http://192.168.1.50:8080/read,把 192.168.1.50 换成你的小屋的地址。
输出的最后一行是一个小屋不认识的路径。我们发它是为了看看回复:
not run by usเราไม่ได้รันเอง我们没有运行过
curl -i http://192.168.1.50:8080/garden
what it should printสิ่งที่มันควรพิมพ์ออกมา它应该打印的内容 · not run on a kit by us · เราไม่ได้รันบนชุดคิต · 我们没有在套件上运行过
HTTP/1.0 404 Not Found
Content-Type: application/json
{"error": "the house does not know /garden"}
🇬🇧 English
404 is a status code. It is a number at the start of every reply that says how the request went. 200 means it went well, and 404 means that the thing asked for is not there.
🇹🇭 ไทย
404 คือรหัสสถานะ เป็นตัวเลขตอนต้นของทุกคำตอบที่บอกว่าคำขอเป็นอย่างไร 200 หมายถึงเรียบร้อยดี และ 404 หมายถึงสิ่งที่ขอมานั้นไม่มีอยู่
🇨🇳 中文
404 是一个状态码。它是每个回复开头的一个数字,说明请求的结果。200 表示一切顺利,404 表示请求的东西不存在。
mpremote run stops when you unplug the house. MicroPython's documentation says that a file named main.py on the board runs when the board starts. This command saves house.py on the board with that name: python -m mpremote cp house.py :main.py. The colon means the board. After that, the house should start the file each time it gets power, from the batteries too. We did not try this.ไฟล์ที่คุณเปิดด้วย mpremote run จะหยุดเมื่อคุณถอดสายบ้านออก เอกสารของ MicroPython บอกว่าไฟล์ชื่อ main.py บนบอร์ดจะรันเมื่อบอร์ดเริ่มทำงาน คำสั่งนี้บันทึก house.py ลงบนบอร์ดในชื่อนั้น: python -m mpremote cp house.py :main.py เครื่องหมายโคลอนหมายถึงบอร์ด หลังจากนั้น บ้านก็น่าจะเริ่มรันไฟล์ทุกครั้งที่ได้รับไฟ รวมถึงไฟจากแบตเตอรี่ด้วย เราไม่ได้ลองทำเรื่องนี้用 mpremote run 启动的文件,在你拔掉小屋的线时就停了。MicroPython 的文档说,开发板上名叫 main.py 的文件会在开发板启动时运行。这条命令把 house.py 以这个名字存到开发板上:python -m mpremote cp house.py :main.py。冒号表示开发板。之后,小屋每次通电时应该都会启动这个文件,用电池供电时也一样。我们没有试过这一点。
7 ask_house.py: ask it from your computer · ask_house.py: สั่งบ้านจากคอมพิวเตอร์ของคุณ · ask_house.py:从你的电脑向它提问
🇬🇧 English
This file does not run on the house. It runs on your computer, with ordinary Python, in a second terminal. Your computer and the house must be on the same Wi-Fi.
urlopen comes with Python. It sends a request to an address and gives back the reply. json.load turns the reply's text into a Python dictionary. The function ask does both, so one line of your program is one request to the house.
HOUSE is where the house is. Open the file with vim and write the address that house.py printed in the place of 192.168.1.50. Keep the :8080 after it.
🇹🇭 ไทย
ไฟล์นี้ไม่ได้รันบนบ้าน มันรันบนคอมพิวเตอร์ของคุณ ด้วย Python ธรรมดา ในเทอร์มินัลที่สอง คอมพิวเตอร์ของคุณกับบ้านต้องอยู่บน Wi-Fi เดียวกัน
urlopen มากับ Python อยู่แล้ว มันส่งคำขอไปยังที่อยู่หนึ่ง แล้วให้คำตอบกลับมา json.load เปลี่ยนข้อความของคำตอบเป็นดิกชันนารีของ Python ฟังก์ชัน ask ทำทั้งสองอย่าง ดังนั้นหนึ่งบรรทัดในโปรแกรมของคุณคือหนึ่งคำขอไปยังบ้าน
HOUSE คือที่ที่บ้านอยู่ เปิดไฟล์ด้วย vim แล้วเขียนที่อยู่ที่ house.py พิมพ์ออกมาแทน 192.168.1.50 และคง :8080 ไว้ข้างหลัง
🇨🇳 中文
这个文件不在小屋上运行。它在你的电脑上运行,用普通的 Python,在第二个终端里。你的电脑和小屋必须在同一个 Wi-Fi 上。
urlopen 是 Python 自带的。它向一个地址发送请求,并把回复交回来。json.load 把回复的文字变成 Python 字典。函数 ask 把这两件事都做了,所以你程序里的一行就是给小屋的一个请求。
HOUSE 是小屋所在的位置。用 vim 打开文件,把 192.168.1.50 换成 house.py 打印的地址,后面的 :8080 保留。
ask_house.py
import json
from urllib.request import urlopen
# Write the address that house.py printed in the place of 192.168.1.50.
HOUSE = "http://192.168.1.50:8080"
def ask(path):
with urlopen(HOUSE + path) as reply:
return json.load(reply)
print(ask("/light/on"))
print(ask("/read"))
print(ask("/light/off"))
not run by usเราไม่ได้รันเอง我们没有运行过
python ask_house.py
what it should printสิ่งที่มันควรพิมพ์ออกมา它应该打印的内容 · not run on a kit by us · เราไม่ได้รันบนชุดคิต · 我们没有在套件上运行过
{'done': '/light/on'}
{'temperature': 31, 'humidity': 62, 'motion': 1, 'left_button': 1, 'right_button': 1, 'light': 1}
{'done': '/light/off'}
🇬🇧 English
Three requests, three replies. Look at the second one: 'light': 1. The first request switched the light on, and the house remembers it. The third request switched it off again.
🇹🇭 ไทย
สามคำขอ สามคำตอบ ดูคำตอบที่สอง: 'light': 1 คำขอแรกเปิดไฟ และบ้านจำไว้ คำขอที่สามปิดไฟอีกครั้ง
🇨🇳 中文
三个请求,三个回复。看第二个:'light': 1。第一个请求把灯打开了,小屋记住了这一点。第三个请求又把它关掉。
8 evening.py: let a program decide · evening.py: ให้โปรแกรมตัดสินใจ · evening.py:让程序来决定
🇬🇧 English
Now your computer can read the house and change it. So a program can decide for you. This one reads the house once. If the temperature is 28 degrees or more, it starts the fan. If somebody is moving, it switches the light on and opens the door.
The rules are in your program, and not in the house. To change a rule you change this file on your computer. The house keeps running the same house.py.
🇹🇭 ไทย
ตอนนี้คอมพิวเตอร์ของคุณอ่านค่าจากบ้านและเปลี่ยนมันได้แล้ว โปรแกรมจึงตัดสินใจแทนคุณได้ โปรแกรมนี้อ่านค่าจากบ้านหนึ่งครั้ง ถ้าอุณหภูมิ 28 องศาหรือมากกว่า มันจะเปิดพัดลม ถ้ามีคนขยับอยู่ มันจะเปิดไฟและเปิดประตู
กฎอยู่ในโปรแกรมของคุณ ไม่ได้อยู่ในบ้าน ถ้าจะเปลี่ยนกฎ คุณแก้ไฟล์นี้บนคอมพิวเตอร์ของคุณ บ้านยังคงรัน house.py ตัวเดิมต่อไป
🇨🇳 中文
现在你的电脑既能读取小屋,又能改变它。所以程序可以替你做决定。这个程序读取小屋一次。如果温度是 28 度或更高,它就启动风扇。如果有人在移动,它就开灯并开门。
规则在你的程序里,不在小屋里。想改规则,就改你电脑上的这个文件。小屋继续运行同一个 house.py。
evening.py
import json
from urllib.request import urlopen
# Write the address that house.py printed in the place of 192.168.1.50.
HOUSE = "http://192.168.1.50:8080"
HOT = 28 # degrees Celsius
def ask(path):
with urlopen(HOUSE + path) as reply:
return json.load(reply)
readings = ask("/read")
print("temperature:", readings["temperature"], "C")
print("motion:", readings["motion"])
if readings["temperature"] >= HOT:
ask("/fan/on")
print("It is hot. The fan is on.")
else:
ask("/fan/off")
print("It is not hot. The fan is off.")
if readings["motion"] == 1:
ask("/light/on")
ask("/door/open")
print("Somebody is there. The light is on and the door is open.")
else:
ask("/light/off")
ask("/door/close")
print("Nobody is there. The light is off and the door is closed.")
not run by usเราไม่ได้รันเอง我们没有运行过
python evening.py
what it should printสิ่งที่มันควรพิมพ์ออกมา它应该打印的内容 · not run on a kit by us · เราไม่ได้รันบนชุดคิต · 我们没有在套件上运行过
temperature: 31 C
motion: 1
It is hot. The fan is on.
Somebody is there. The light is on and the door is open.
- Change
HOTto 35 and runevening.pyagain. What does it print now? - Add a path
/window/opentohouse.py. The window's servo is on pin 5. - Put the whole of
evening.pyin a loop that runs once every ten seconds.
- เปลี่ยน
HOTเป็น 35 แล้วรันevening.pyอีกครั้ง ตอนนี้มันพิมพ์อะไร - เพิ่มพาธ
/window/openในhouse.pyเซอร์โวของหน้าต่างอยู่ที่พิน 5 - ใส่
evening.pyทั้งหมดไว้ในลูปที่รันหนึ่งครั้งทุกสิบวินาที
- 把
HOT改成 35,再运行一次evening.py。现在它打印什么? - 给
house.py加一个路径/window/open。窗户的舵机在引脚 5 上。 - 把整个
evening.py放进一个每十秒运行一次的循环里。
🧭 What to do next · ทำอะไรต่อดี · 接下来做什么
🇬🇧 English
You wrote Python that runs on the house, and Python that asks the house from far away. Here are three ways to go on.
- Use the rest of the kit. Keyestudio's Python lessons show the buzzer, the coloured lights, the screen, the card reader and a door that opens with Morse code.
house.pyis a very small web service, written by hand. The BentoML page builds a web service on a computer, with a library that checks every request for you.- Read about the board itself in MicroPython's quick reference for the ESP32.
🇹🇭 ไทย
คุณเขียน Python ที่รันบนบ้าน และ Python ที่สั่งบ้านจากที่ไกลออกไป ต่อไปนี้คือสามทางที่ไปต่อได้
- ใช้ส่วนที่เหลือของชุดคิต บทเรียน Python ของ Keyestudio สอนบัซเซอร์ ไฟสี จอ เครื่องอ่านบัตร และประตูที่เปิดด้วยรหัสมอร์ส
house.pyคือเว็บเซอร์วิสขนาดเล็กมากที่เขียนด้วยมือ หน้า BentoML สร้างเว็บเซอร์วิสบนคอมพิวเตอร์ ด้วยไลบรารีที่ตรวจทุกคำขอให้คุณ- อ่านเรื่องตัวบอร์ดเองได้ในคู่มืออ้างอิงฉบับย่อของ MicroPython สำหรับ ESP32
🇨🇳 中文
你写了在小屋上运行的 Python,也写了从远处向小屋提问的 Python。下面是三条可以继续走的路。
- 用上套件里其余的部件。Keyestudio 的 Python 课程讲了蜂鸣器、彩灯、屏幕、读卡器,还有用摩尔斯电码开的门。
house.py是一个手写的、非常小的网络服务。BentoML 那一页在电脑上搭建网络服务,用的库会替你检查每一个请求。- 想了解开发板本身,可以读 MicroPython 的 ESP32 快速参考。