查看“︁AT模块 9: 附录(Appendix)”︁的源代码
←
AT模块 9: 附录(Appendix)
跳转到导航
跳转到搜索
因为以下原因,您没有权限编辑该页面:
不允许您执行您所请求的操作。
您可以查看和复制此页面的源代码。
= 附录 = == 生成 romfs.bin == 首先,您需要创建一个新的 romfs 文件夹,并将需要烧录的证书文件放置在 romfs 目录中: <pre> romfs$ ca_01.crt cert_01.crt private_01.key </pre> 然后,使用 SDK 目录中的 genromfs 工具生成 romfs.bin。您可以在 Makefile 中添加命令: <pre> $(shell ./tools/genromfs -d romfs/ -f ./build/build_out/romfs.bin) </pre> 在 flash 配置文件 "flash_prog_cfg.ini" 中添加下载 romfs 分区的选项: <pre> #media factory [romfs] filedir = ./build/build_out/romfs.bin address = 0x378000 </pre> address 字段是分区表中 media 分区的地址。分区表的路径为: <pre> bsp/board/<chip_name>/config/partition_cfg_4M.toml 或 bsp/board/<chip_name>/config_8M/partition_cfg_8M.toml </pre> 最后,执行命令行固件下载命令后,romfs.bin 文件也会被下载到芯片中。 <pre> make flash CHIP=bl616 COMX=<com_port> </pre> == HTTPS 本地服务搭建 === === https.py === <pre> import http.server import ssl import sys from socketserver import ThreadingMixIn import threading class httpHandler(http.server.BaseHTTPRequestHandler): def do_GET(self): response = "Hello World" self.send_response(200) self.send_header("Content-Type", "text/plain") self.send_header("Content-Length", str(len(response))) self.end_headers() self.wfile.write(response.encode()) def do_HEAD(self): self.send_response(200) self.send_header("Content-Type", "text/plain") self.send_header("Content-Length", str(len("Hello World"))) self.end_headers() def do_POST(self): self.send_response(405) self.send_header("Content-Type", "text/plain") self.send_header("Content-Length", str(len("POST Method success"))) self.end_headers() self.wfile.write(b"POST Method success") def log_message(self, format, *args): return class ThreadedHTTPServer(ThreadingMixIn, http.server.HTTPServer): daemon_threads = True class HttpsServer: def __init__(self, ip, port, certfile, keyfile, ca_certs=None): self.server_address = (ip, port) self.certfile = certfile self.keyfile = keyfile self.ca_certs = ca_certs self.httpd = ThreadedHTTPServer(self.server_address, httpHandler) # 启用服务端 SSL 并验证客户端证书 self.httpd.socket = ssl.wrap_socket( self.httpd.socket, certfile=self.certfile, keyfile=self.keyfile, server_side=True, cert_reqs=ssl.CERT_REQUIRED, # 要求客户端证书 ca_certs=self.ca_certs # 用于客户端验证的 CA 证书路径 ) self.thread = None def start_server(self): if self.thread is None: self.thread = threading.Thread(target=self.httpd.serve_forever) self.thread.start() print(f"Server started at https://{self.server_address[0]}:{self.server_address[1]}") else: print("Server is already running.") def stop_server(self): if self.thread is not None: self.httpd.shutdown() self.thread.join() self.thread = None print("Server stopped.") else: print("Server is not running.") # 使用示例: if __name__ == "__main__": ip = sys.argv[1] port = int(sys.argv[2]) certfile = sys.argv[3] keyfile = sys.argv[4] ca_certs = sys.argv[5] if len(sys.argv) > 5 else None # 可选的 CA 证书用于客户端验证 server = HttpsServer(ip, port, certfile, keyfile, ca_certs) server.start_server() try: input("Press Enter to stop the server...\n") finally: server.stop_server() </pre> 1. 您可以使用提供的 Python 代码快速搭建本地 HTTPS 服务器。创建一个新的 https.py 文件,将代码复制到文件中,并运行以下命令: <pre> python3 https.py 192.168.31.112 4443 lfs/lfs/server_1.crt lfs/lfs/server_1.key lfs/lfs/ca_1.crt </pre> 2. BL HTTPS 双向认证 <pre> AT+HTTPSSLCFG=0,3,"client_1.crt","client_1.key","ca_1.crt" </pre> 3. BL HTTPS 服务器认证 在 https.py 代码中注释掉这一行以禁用客户端验证: cert_reqs=ssl.CERT_REQUIRED, # Require client certificate <pre> AT+HTTPSSLCFG=0,1,,,"ca_1.crt" </pre> 4. BL HTTPS 客户端认证 <pre> AT+HTTPSSLCFG=0,2,"client_1.crt","client_1.key" </pre> 5. BL HTTPS 无认证 在 https.py 代码中注释掉这一行以禁用客户端验证: cert_reqs=ssl.CERT_REQUIRED, # Require client certificate <pre> AT+HTTPSSLCFG=0,0 </pre> 6. BL HTTPS Head 请求 <pre> AT+HTTPCLIENT=0,1,0,"https://192.168.31.112:4443/" </pre> 7. BL HTTPS Get 请求 <pre> AT+HTTPCLIENT=0,2,0,"https://192.168.31.112:4443/" </pre> 8. BL HTTPS Post 请求 <pre> AT+HTTPCLIENT=0,3,0,"https://192.168.31.112:4443/" </pre> 9. BL HTTPS Put 请求 <pre> AT+HTTPCLIENT=0,4,0,"https://192.168.31.112:4443/" </pre> == MQTT 本地服务搭建 == 1. 安装 mosquitto <pre> sudo apt install mosquitto </pre> 2. 设置密码 <pre> mosquitto_passwd -c /etc/mosquitto/passwd bflb </pre> 输入两次密码,例如: <pre> 12345678 </pre> 3. 配置证书 <pre> cp {case_path}/lfs/lfs/server_1.crt /etc/mosquitto/server_1.crt cp {case_path}/lfs/lfs/server_1.key /etc/mosquitto/server_1.key cp {case_path}/lfs/lfs/ca_1.crt /etc/mosquitto/ca_1.crt </pre> 4. 启动 MQTT 服务 <pre> mosquitto -c /etc/mosquitto/mosquitto.conf </pre> 5. PC 订阅主题 <pre> mosquitto_sub -h 192.168.31.156 -u bflb -P 12345678 --cafile lfs/lfs/ca_1.crt --cert lfs/lfs/client_1.crt --key lfs/lfs/client_1.key -t "test/topic1" --tls-version tlsv1.2 --insecure </pre> 6. BL MQTT 用户配置 <pre> AT+MQTTUSERCFG=0,4,"BL001","bflb","12345678","client_1.crt","client_1.key","ca_1.crt" </pre> 7. BL 连接到 MQTT Broker <pre> AT+MQTTCONN=0,"192.168.31.156",8883,0 </pre> 8. BL 发布消息 <pre> AT+MQTTPUB=0,"test/topic1","This is a MQTT test",2,0 </pre> 9. BL 订阅主题 <pre> AT+MQTTSUB=0,"test/topic2",0 </pre> 10. PC 发布消息 <pre> mosquitto_pub -h 192.168.31.156 -p 8883 -u bflb -P 12345678 --cafile lfs/lfs/ca_1.crt --cert lfs/lfs/client_1.crt --key lfs/lfs/client_1.key -t "test/topic2" -m "Hello MQTT" --tls-version tlsv1.2 --insecure </pre> == 使用 HiveMQ 的 MQTT TLS 方案 == === a. Serverless 版本的验证步骤 === 1. 从 HiveMQ 下载 broker Root CA 证书 (isrgrootx1.pem) 并放置在以下文件夹中: <pre> examples/wifi/spi_wifi/lfs/lfs/isrgrootx1.pem </pre> 2. 在 examples/wifi/spi_wifi/ 文件夹中,运行 "make" 重新构建项目并使用 BLonn_Flash 工具烧录镜像。 3. 在 HiveMQ web 控制台中,在 serverless 计划下创建一个 broker 并设置相应的凭据(例如,username = BL_test_0, password = BL123456,权限设置为发布和订阅)。此步骤请参考 HiveMQ 官方文档。 4. 将 BL 连接到 WiFi AP(根据需要调整 SSID 和密码): <pre> AT+CWMODE=1 AT+CWJAP="8E88","" </pre> 5. 配置 MQTT 用户设置: <pre> AT+MQTTUSERCFG=0,2,"bflb","BL_test_0","BL123456","","","isrgrootx1.pem" </pre> (0 表示本地客户端索引; 2 指定使用单向 TLS,其中客户端验证服务器的证书; "bflb" 是 MQTT 客户端 ID; "BL_test_0"/"BL123456" 是在控制台上设置的用户名和密码; isrgrootx1.pem 是 broker Root CA。) 6. 配置 MQTT SSL/TLS SNI。实践证明必须设置 SNI,否则连接将失败(可能是因为 HiveMQ broker 使用 SNI 来识别正确的 broker 实例): <pre> AT+MQTTSNI=0,"8f3ef71da5bf445eb0669e56eecef396.s1.eu.hivemq.cloud" </pre> 7. 配置 MQTT 连接属性: <pre> AT+MQTTCONNCFG=0,120,0,"","",0,0 </pre> (0 表示本地客户端索引; 120 是 MQTT 客户端保活时间(秒); 第一个 0 表示禁用 clean session。) 8. 发起 MQTT 连接: <pre> AT+MQTTCONN=0,"8f3ef71da5bf445eb0669e56eecef396.s1.eu.hivemq.cloud",8883,0 </pre> (注意:此处提供的主机和端口信息必须与 HiveMQ broker 控制台匹配。) 9. 订阅所有主题: <pre> AT+MQTTSUB=0,"#",0 </pre> 10. 发布主题消息: <pre> AT+MQTTPUB=0,"/test/bflb","What's up guys",0,0 </pre> === b. Starter 版本的验证步骤 === 1. 从 HiveMQ 下载 broker Root CA 证书 (isrgrootx1.pem) 并放置在以下文件夹中: <pre> examples/wifi/spi_wifi/lfs/lfs/isrgrootx1.pem </pre> 2. 在 examples/wifi/spi_wifi/ 文件夹中,运行 "make" 重新构建项目并使用 BLFlash 工具烧录镜像。 3. 在 HiveMQ web 控制台中,在 starter 计划下创建一个 broker 并设置相应的凭据(例如,username = BL_test_0, password = BL123456,权限设置为发布和订阅)。此外,将 ca_1.crt 添加为 Root CA,用于验证设备的证书到 broker。此步骤请参考 HiveMQ 官方文档。 4. 将 BL 连接到 WiFi AP(根据需要调整 SSID 和密码): <pre> AT+CWMODE=1 AT+CWJAP="8E88","" </pre> 5. 配置 MQTT 用户设置: <pre> AT+MQTTUSERCFG=0,4,"changeme","BL_test_0","BL123456","client_1.crt","client_1.key","isrgrootx1.pem" </pre> (0 表示本地客户端索引; 4 指定使用双向 TLS; "changeme" 是 MQTT 客户端 ID; "BL_test_0"/"BL123456" 是在控制台上设置的用户名和密码; client_1.crt 是由 ca_1 签发的设备 x509 证书; client_1.key 是与设备证书匹配的密钥; isrgrootx1.pem 是 broker Root CA。) 6. 配置 MQTT SSL/TLS SNI。实践证明必须设置 SNI,否则连接将失败(可能是因为 HiveMQ broker 使用 SNI 来识别要使用的 broker 实例): <pre> AT+MQTTSNI=0,"indigosquash-2lk628.a02.usw2.aws.hivemq.cloud" </pre> 7. 配置 MQTT 连接属性: <pre> AT+MQTTCONNCFG=0,120,0,"","",0,0 </pre> (0 表示本地客户端索引; 120 是 MQTT 客户端保活时间; 0 禁用 clean session。) 8. 发起 MQTT 连接: <pre> AT+MQTTCONN=0,"indigosquash-2lk628.a02.usw2.aws.hivemq.cloud",8883,0 </pre> (注意:此处的主机和端口必须与 HiveMQ broker 控制台提供的信息匹配。) 9. 订阅所有主题: <pre> AT+MQTTSUB=0,"#",0 </pre> 10. 发布主题消息: <pre> AT+MQTTPUB=0,"/test/bflb","What's up guys",0,0 </pre> ----
返回
AT模块 9: 附录(Appendix)
。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息