2024-7-18 09:43:36 [显示全部楼层]
25706浏览
查看: 25706|回复: 1

【社区优秀作品】NFC技术 x 智能灯环互动项目

[复制链接]
项目介绍

【社区优秀作品】NFC技术 x 智能灯环互动项目图1

NFC技术(Near Field Communication,近场通讯技术)已经悄然走入我们的日常生活。你或许没有听说过它,但你一定用过它。你是否在便利店尝试过手机感应支付?在地铁站用手机刷NFC公交卡进站?这些方便又快捷的操作,全靠的就是NFC技术。它让设备可以在极短距离内安全、快速地传输数据,通常距离不超过几厘米。
NFC不仅被广泛应用于支付和门禁系统,还在智能家居和智能设备控制中扮演重要角色。今天,我们将探索如何利用NFC技术,结合行空板和LED灯环,打造一款简单创新的互动装置。通过简单的手机动作扫描NFC标签,就能实时控制灯光颜色。这个项目不仅展示了硬件与软件的完美结合,更是对移动NFC交互和彩色LED显示的新颖应用。


硬件清单
【社区优秀作品】NFC技术 x 智能灯环互动项目图2


· 行空板
· 12位RGB灯环
· PH2.0母母头3 Pin连接线

制作过程
1 硬件连接通电
连接:使用3pin连接线将LED环连接到行空板端口(P24端口)
加电:使用USB线将行空板连接到电源。一旦通电,板子会自动识别连接的LED环

【社区优秀作品】NFC技术 x 智能灯环互动项目图5


【社区优秀作品】NFC技术 x 智能灯环互动项目图6


2 创建本地项目和Python脚本
在您的本地设备上创建一个名<NeoPixelAPI>的新项目目录。在新目录<NeoPixelAPI>中创建名为<main.py>的Python文件。
示例代码如下
  1. from pinpong.board import Board, Pin, NeoPixel
  2. from flask import Flask, jsonify
  3. COLORS: dict = {
  4.     'red': (255, 0, 0),
  5.     'green': (0, 255, 0),
  6.     'blue': (0, 0, 255),
  7.     'black': (0, 0, 0),
  8.     'white': (255, 255, 255)
  9. }
  10. app = Flask(__name__)
  11. @app.route(rule='/api/colors', methods=['GET'])
  12. def get_available_colors():
  13.     """
  14.     This method is an API route that returns the available colors. The values are from the COLORS dictionary.
  15.     :return: A JSON object containing the status and the available colors.
  16.     """
  17.     return jsonify({'status': 'success',
  18.                     'message': COLORS}), 200
  19. @app.route(rule='/api/<string:color>', methods=['GET'])
  20. def set_color(color):
  21.     """
  22.     This method is an API route that sets the color of an LED strip. It takes in a color parameter as a string and
  23.     returns a JSON response indicating the status and message. The color parameter is used to set the color of each
  24.     LED in the strip. It is expected to be a valid color value that matches one of the predefined colors in the COLORS
  25.     dictionary. If the provided color is valid, the method iterates through each LED in the strip and sets the color to
  26.     the corresponding value from the COLORS dictionary.
  27.     The method returns a success response with a status code of 200 and a message indicating the color that was set if
  28.     the provided color is valid. If the provided color is not valid, the method returns an error response with a
  29.     status code of 400 and a message indicating that the color is not valid.
  30.     :param color: The color value to set for the LED strip.
  31.     :return: Response indicating the status and message.
  32.     """
  33.     global led
  34.     api_color = str(color.lower())
  35.     if api_color in COLORS:
  36.         for pixel in range(led.num):
  37.             led[pixel] = COLORS[api_color]
  38.         return jsonify({'status': 'success',
  39.                         'message': f"{color} is valid."}), 200
  40.     else:
  41.         return jsonify({'status': 'error',
  42.                         'message': f"{color} is not valid."}), 400
  43. if __name__ == '__main__':
  44.     Board().begin()
  45.     try:
  46.         led = NeoPixel(Pin(Pin.P24), 12)
  47.         led.clear()
  48.     except Exception as err:
  49.         print(err)
  50.     app.run(host='0.0.0.0')
复制代码

3 将Python脚本上传到行空板
通过WI-FI或USB连接的方式,将项目文件上传到行空板,然后通过SSH在行空板上运行REST API服务。
示例代码如下
  1. ```bash
  2. # upload script with SCP
  3. scp -r ~/NeoPixelAPI root@10.1.2.3:/root/NeoPixelAPI
  4. # login with SSH
  5. ssh -C4 root@10.1.2.3
  6. # change directory
  7. cd ~/NeoPixelAPI
  8. ```
复制代码

4 运行REST API服务器
启动Python Flask Web API服务器,通过串口连接,就能在特定IP和端口上提供REST API服务。
  1. ```bash
  2. # run simple Flask server
  3. python3 main.py
  4.   ___________________________
  5. |                           |
  6. |      PinPong v0.5.1       |
  7. |    Designed by DFRobot    |
  8. |___________________________|
  9. [01] Python3.7.3 Linux-4.4.143-67-rockchip-g01bbbc5d1312-aarch64-with-debian-10.13 Board: UNIHIKER
  10. selected -> board: UNIHIKER serial: /dev/ttyS3
  11. [10] Opening /dev/ttyS3
  12. [32] Firmata ID: 3.7
  13. [22] Arduino compatible device found and connected to /dev/ttyS3
  14. [40] Retrieving analog map...
  15. [42] Auto-discovery complete. Found 30 Digital Pins and 30 Analog Pins
  16. ------------------------------
  17. All right. PinPong go...
  18. ------------------------------
  19. * Serving Flask app 'main' (lazy loading)
  20. * Environment: production
  21.    WARNING: This is a development server. Do not use it in a production deployment.
  22.    Use a production WSGI server instead.
  23. * Debug mode: off
  24. * Running on all addresses.
  25.    WARNING: This is a development server. Do not use it in a production deployment.
  26. * Running on http://10.0.0.14:5000/ (Press CTRL+C to quit)
  27. ```
复制代码

这里Running on http://10.0.0.14:5000/是我家环境中行空板的WLAN IP。你也可以在本地WLAN中测试API:
  1. ### Example usage:
  2. ```
  3. GET http://[IP address]/api/colors
  4. Response:
  5. {
  6.     "message":{
  7.         "black":[0,0,0],
  8.         "blue":[0,0,255],
  9.         "green":[0,255,0],
  10.         "red":[255,0,0],
  11.         "white":[255,255,255]
  12.     },
  13.     "status":"success"
  14. }
  15. GET http://[IP address]/api/red
  16. Response:
  17. {
  18.     "status": "success",
  19.     "message": "red is valid."
  20. }
  21. GET http://[IP address]/api/black
  22. Response:
  23. {
  24.     "status": "success",
  25.     "message": "black is valid."
  26. }
  27. GET http://[IP address]/api/invalid_color
  28. Response:
  29. {
  30.     "status": "error",
  31.     "message": "invalid_color is not valid."
  32. }
  33. ```
复制代码

5 在手机上创建API调用


【社区优秀作品】NFC技术 x 智能灯环互动项目图4


代码测试成功后,就可以用手机读取NFC标签来触发操作。当NFC标签被扫描时,手机会发送一个HTTP GET请求到行空板上的Flask网络服务,改变LED的颜色。
我这里用的iPhone,可以使用“快捷指令”应用来实现这个功能。

效果展示

【社区优秀作品】NFC技术 x 智能灯环互动项目图3



作者:Lupin
发布时间:2024年7月14日
原文链接:https://community.dfrobot.com/makelog-314414.html


许培享  中级技神

发表于 2024-7-28 10:04:53

我喜欢Python脚本
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail