我们使用一些必要的Cookie来确保网站正常运行。

我们使用可选的 Cookie,具体详情请参阅我们的Cookie政策,用于记住您的设置并了解您如何使用我们的网站。

大型联网按钮 | HackSpace #77

一击即可将杂志送印。您可以在明天拿到最新一期的HackSpace杂志!

每个月在HackSpace杂志大楼里,我们都会制作一本杂志。我们收集内容、编辑、调整格式、添加图片,并仔细检查每一样东西。完成这一切后,我们会将其发送给印刷厂,将我们的数字文件转换成纸质版。

 a hand pressing a large red button on a grey box, which has the text “GO TO PRESS” written on it. Here are some details based on the image:

Action: A person’s hand is shown pressing down on a large red button.
Setting: The button is mounted on a grey box with visible wires, suggesting it’s part of an electrical system.
Text: “GO TO PRESS” is indicated on the box, which may imply the button initiates a process or action.
The background is plain, featuring what seems to be a white wall and a section of a table or counter. The image captures a moment of interaction with a device that likely serves a specific function, possibly in an industrial or mechanical context.

正是这最后一步让人感觉有点,嗯,不那么令人兴奋。你可能会想象发送杂志到印刷需要按下一个看起来很重要的大按钮。至少,这是这位作者多年前初次进入出版界时的想法。然而现实并不那么令人印象深刻,实际上我们只是发送一条消息告诉对方我们对此感到满意。

当现实不如人意时,我们可以选择接受它,也可以选择为其增添活力。本期中,我们选择了后者,创造了自己的一个看起来很重要的大按钮。好吧,技术上来说我们并没有自己制造这个按钮,而是购买了它(Adafruit出品的恰如其名的大号街机按钮100毫米)。不过,我们确实为它做了一个支架,将其连接到了Raspberry Pi Pico W上,并编程使其在按下时发送适当的消息。

现在,当期刊准备好发送时,我们只需敲一下这个大红色按钮,而无需键入批准信息。

a raspberry pi board housed within a light blue box, which could be 3D printed. Here are the details based on the image:

Circuit Board: The main component is a green electronic circuit board with various electronic elements mounted on it.
Wires: There are wires in black, red, and green connected to the circuit board, likely for power and signal transmission.
Connectors: A black clip connector is attached to the wires, indicating the board may interface with other devices or systems.
Enclosure: The circuit board is placed inside a light blue box, which has a layered texture typical of 3D printed objects.
The image doesn’t specify the exact purpose of the circuit board assembly, but it’s common in DIY electronics and could be part of a larger project or device. The components and connections visible are typical of hobbyist or prototyping work in electronics.

显然,你可能不需要一个用于发送杂志的按钮,但我们将展示如何制作一个可以执行任何你喜欢功能的大红色按钮。

首先,你需要用于盒子的3D打印文件。你可以在这里获取这里。它是为多色打印机设计的,但你也可以用单一颜色打印。这是一个相当简单的设——只是一个去除了多余部分的立方体,在顶部留有一个按钮孔,在背面留有一个电源孔。内部没有Raspberry Pi Pico W的安装架——我们更喜欢使用一点热熔胶,但如果你不喜欢这样做,可以增加几个孔。

文字是在PrusaSlicer中添加的,也可以通过该软件进行编辑。

开关的电子部件位于按钮底部的一个可拆卸组件中。旋转几度就应能弹出。按钮内有一个LED灯,因此我们将其连线到一个GPIO引脚(目前我们让它一直亮着,因此可以直接连到3.3V或VBUS,但我们认为保留未来添加灯光效果的选择更好)。LED自带电阻,因此可以直接将它接到0号引脚和地线。LED引脚位于开关的两侧(见图1)。你可以任选一边作为地线和电源线,然后根据情况调转LED方向以匹配。

Figure 1. The button has five connections – we use four of them
图1.按钮有五个连接点——我们使用了其中四个

开关有三个引脚:公共端、常开端和常闭端。我们将常开端和公共端直接连接到GPIO 1和地线之间。公共端位于装置底部,而常开端则是末端下方的两个引脚之一。然后我们可以在软件中添加一个上拉电阻,这样按钮未被按下时读数为1,按下时读数为0。

这就是此构建中的硬件部分,接下来让我们看看软件部分。

我们使用了MicroPython,所以你需要将其安装到Pico W上。

我们需要通过Slack发送信息,它有一个应用程序接口(API)允许我们从脚本与其交互。如果你想与其他服务一起工作,你需要找到适合的服务API详情。

调整设置

与Slack API互动有点棘手,因为你必须正确设置安全细节。幸运的是,比我们聪明得多的人已经完成了这项艰苦的工作,并在Raspberry Pi博客上写了下来。完整详情请见这里。这里不再赘述。对我们而言最重要的部分是在Slack网站上设置安全细节(根据你的配置,可能需要让工作区管理员批准机器人)。

完成所有这些设置并记下安全细节后,你需要获取来自GitHub的代码.

我们需要的关键文件都在MicroPython目录中。除了main.py之外,我们需要所有这些文件,我们将使用我们自己的代码(如下)。这些文件需要复制到Pico W上,你可以使用Thonny MicroPython编辑器来完成。转到视图>文件,然后你可以导航到你下载它们的位置。选择它们,然后将它们上传到Pico上的/目录。

简单脚本

现在让我们来看看我们自己的代码(如下)。

你在这里必须更改的一点是channel_id。在Slack中,你可以向一个人、一群人或一个频道发送消息,但这三种选项都使用频道ID。要找出频道的频道ID,请点击该频道,然后点击频道名称旁边的小下拉箭头,并滚动到底部,你应该能看到ID。

对于给一个人的消息,前往该人的个人资料,点击三个点,你应该能够获得复制成员ID的选项。这可以代替频道ID使用。

 a large red button.
Setting: The button is mounted on a grey box with visible wires, suggesting it’s part of an electrical system.
Text: “GO TO PRESS” is indicated on the box, which may imply the button initiates a process or action.
The background is plain, featuring what seems to be a white wall and a section of a table or counter. The image captures a moment of interaction with a device that likely serves a specific function, possibly in an industrial or mechanical context.

对于一群人,你必须已经与该群有聊天记录。点击对话,然后标题旁边有一个下拉箭头(标题是聊天中人员的名字)。点击这个箭头,然后转到关于标签页,在底部你应该能看到频道ID。

代码非常基础。首先,它打开LED并连接到网络。一旦连接成功,它就开始循环,等待按钮被按下。当按钮被按下时,代码发送Slack消息。

我们需要注意一点,每次按压按钮时,只发送一次消息。当一个按钮被按下时,两个触点会接触在一起,在接触过程中,它们可能会多次断开再接通,直到最终稳定下来。这种情况被称为抖动。

我们有一个小循环确保按钮完全释放后才能再次
触发。

完整的代码如下:

import network

import time

import config

from slack_bot import SlackBot

channel_id = "put your channel here"

message = "Let's send it"

led = machine.Pin(0, machine.Pin.OUT)

led.on() # let's just have it light up

button = machine.Pin(1, machine.Pin.IN, machine.Pin.PULL_UP)

# initialize the Wi-Fi interface

wlan = network.WLAN(network.STA_IF)

# activate and connect to the Wi-Fi network:

wlan.active(True)

wlan.connect(config.WIFI_SSID, config.WIFI_PASSWORD)

while not wlan.isconnected():

    time.sleep(0.5)

print(f"Connected to Wi-Fi SSID: {config.WIFI_SSID}")

print("Now waiting for the button")

while True:

    if not button.value():

        #The button has been pressed

        slack_bot = SlackBot(config.SLACK_APP_TOKEN, config.SLACK_BOT_TOKEN)

        print("posting message")

        slack_bot.post_message(message, channel_id)

        print("posted message")

        #wait for button to be unpressed

        counter = 0

        while True:

            if button.value():

                counter += 1

            if not button.value():

                counter -= 1

            if counter > 10:

                break

            time.sleep(0.01)

将代码加载到Pico W上(连同原始项目中的所有库)。

确保你已设置好配置文件,你应该就能通过按压按钮发送消息了。无论是用于发送生产指令、分享笑话还是其他任何你需要的东西,只需轻敲按钮即可发送消息。

HackSpace杂志第77期现已推出!

每月,HackSpace杂志为你带来来自创客界的最佳项目、技巧、窍门和教程。你可以从Raspberry Pi Press在线商店或当地报摊购买。

the cover of “HackSpace” magazine, issue 47 from April 2021. Here are the key details:

Title: HackSpace
Issue: #47
Date: April 2021
Main Features:
Custom Controllers: The cover highlights an article about creating the perfect keyboard, mouse, and gamepad.
DIY Robot Dog: Another featured article discusses building your own robotic dog.
Chip Design: There’s a piece on creating personalized silicon chips.
3D Printer Calibration: The magazine also includes an article on achieving perfect prints every time with 3D printer calibration.
Additional Topics: The words “DOLPHINS”, “CARBONIZING”, “MUSIC”, “AIR” suggest articles on a variety of subjects.
The cover design includes the magazine’s title and the subtitle “TECHNOLOGY IN YOUR HANDS” at the top, with the website address “hsmag.cc” and the featured articles listed below. The background is dark, which makes the white text stand out.

5条评论

solar3000 avatar

那真是太简单了。

Liz Upton avatar

我在考虑为Pi Towers Maker Lab的安静时刻准备一个按钮操作的迪斯科球。

solar3000 avatar

太棒了!
订书钉,这很简单。

Glyndon avatar

下一步:增加一个“撤销”功能,以应对办公室猫咪在不恰当的时候按下按钮的情况。

Ashley Whittaker avatar

如果猫咪想按,我们就让它按。

评论已关闭