电子发烧友App

硬声App

0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示
电子发烧友网>电子资料下载>电子资料>通过蓝牙将消息发送到连接到STM32板的LCD显示器

通过蓝牙将消息发送到连接到STM32板的LCD显示器

2023-06-14 | zip | 0.00 MB | 次下载 | 免费

资料介绍

描述

该项目的目的是使用网络浏览器或智能手机通过蓝牙将消息发送到连接到 STM32 板的 LCD 显示器。

一、简介

低功耗蓝牙项目基于STM32 Nucleo-144 ,它使用 BleuIO控制LCD 显示。

对于这个项目,我们需要两个 BleuIO USB 加密狗,一个连接到 Nucleo 板,另一个连接到计算机,运行 Web 脚本。当 BleuIO Dongle 连接到 Nucleo 板的 USB 端口时,STM32 将识别它并直接开始广播。这允许计算机端口上的加密狗与网络脚本连接。

使用电脑上的网页脚本,我们可以使用 BleuIO 向连接到 STM32 的 LCD 屏幕发送消息。

在本示例中,我们使用了 STM32 Nucleo-144 开发板和 STM32H743ZI MCU(支持 STM32H743ZI micro mbed 的开发 Nucleo-144 系列 ARM® Cortex®-M7 MCU 32 位嵌入式评估板)。该开发板有一个 USB 主机,用于连接 BleuIO 加密狗。

如果要使用其他设置,则必须确保它支持 USB 主机,并注意 GPIO 设置可能不同,可能需要在 .ioc 文件中重新配置。

关于守则

项目源代码可在 Github 上获得。

https://github.com/smart-sensor-devices-ab/stm32_bleuio_lcd.git

克隆项目,或将其下载为 zip 文件并解压缩到您的 STM32CubeIDE 工作区。

如果您将项目下载为 zip 文件,则需要将项目文件夹从“stm32_bleuio_lcd-master”重命名为“stm32_bleuio_lcd”

poYBAGNYujGAHG3iAAA5lM86hos585.jpg
 

SDA 连接到 Nucleo 板上的 PF0,将 SCL 连接到 PF1。

然后在 STM32Cube ioc 文件中设置 I2C2,如下所示。(确保根据 LCD 显示要求将 I2C 速度频率更改为 50 KHz。)

pYYBAGNYujSAJEhUAAAMP9SP5yI872.png
 
poYBAGNYujaAHKGmAAA1m3qD8Eg155.png
 
pYYBAGNYujiAC5AqAADEqDRQv9k945.jpg
 

在 USB_HOST\usb_host.c 中的 USBH_CDC_ReceiveCallback 函数中,我们将 CDC_RX_Buffer 复制到一个名为 dongle_response 的外部变量中,该变量可从 main.c 文件访问。

void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef * phost) {
  if (phost == & hUsbHostFS) {
    // Handles the data recived from the USB CDC host, here just printing it out to UART
    rx_size = USBH_CDC_GetLastReceivedDataSize(phost);
    HAL_UART_Transmit( & huart3, CDC_RX_Buffer, rx_size, HAL_MAX_DELAY);

    // Copy buffer to external dongle_response buffer
    strcpy((char * ) dongle_response, (char * ) CDC_RX_Buffer);

    // Reset buffer and restart the callback function to receive more data
    memset(CDC_RX_Buffer, 0, RX_BUFF_SIZE);
    USBH_CDC_Receive(phost, CDC_RX_Buffer, RX_BUFF_SIZE);
  }

  return;
}

在 main.c 中,我们创建了一个简单的解释器,这样我们就可以对从加密狗收到的数据做出反应。

void dongle_interpreter(uint8_t * input) {

  if (strlen((char * ) input) != 0) {
    if (strstr((char * ) input, "\r\nADVERTISING...") != NULL) {
      isAdvertising = true;
    }
    if (strstr((char * ) input, "\r\nADVERTISING STOPPED") != NULL) {
      isAdvertising = false;
    }
    if (strstr((char * ) input, "\r\nCONNECTED") != NULL) {
      isConnected = true;
      HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_SET);
    }
    if (strstr((char * ) input, "\r\nDISCONNECTED") != NULL) {
      isConnected = false;
      HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_RESET);
    }

    if (strstr((char * ) input, "L=0") != NULL) {

      isLightBulbOn = false;
      //HAL_GPIO_WritePin(Lightbulb_GPIO_Port, Lightbulb_Pin, GPIO_PIN_RESET);
      lcd_clear();

      writeToDongle((uint8_t * ) DONGLE_SEND_LIGHT_OFF);

      uart_buf_len = sprintf(uart_tx_buf, "\r\nClear screen\r\n");
      HAL_UART_Transmit( & huart3, (uint8_t * ) uart_tx_buf, uart_buf_len, HAL_MAX_DELAY);
    }

    if (strstr((char * ) input, "L=1") != NULL) {
      isLightBulbOn = true;
      writeToDongle((uint8_t * ) DONGLE_SEND_LIGHT_ON);

      lcd_clear();

      lcd_write(input);

    }

  }
  memset( & dongle_response, 0, RSP_SIZE);
}

我们将解释器函数放在主循环中。

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
  /* USER CODE END WHILE */
  MX_USB_HOST_Process();

  /* USER CODE BEGIN 3 */
  // Simple handler for uart input
  handleUartInput(uartStatus);

  // Inteprets the dongle data
  dongle_interpreter(dongle_response);

  // Starts advertising as soon as the Dongle is ready.
  if (!isAdvertising && !isConnected && isBleuIOReady) {
    HAL_Delay(200);
    writeToDongle((uint8_t * ) DONGLE_CMD_AT_ADVSTART);
    isAdvertising = true;
  }
}
/* USER CODE END 3 */

使用示例项目

我们需要什么

pYYBAGNY5t-AS6FEAAAllT3mifY185.jpg
 

作为现有项目导入

从 STM32CubeIDE 中选择 File>Import…

pYYBAGNVlzGAOrdTAABTMPtN_vY766.png
 

然后选择 General>Existing Projects into Workspace 然后点击“Next >”

poYBAGNVlzOAbEAcAACCYdjUQ8w416.png
 

确保您在“选择根目录:”中选择了您的工作区

您应该会看到项目“stm32_bleuio_SHT85_example”,选中它并单击“完成”。

pYYBAGNVlzWAF9eHAACaAb8Lb0g612.png
 

运行示例

将代码上传到 STM32 并运行示例。连接到 STM32 的 USB 加密狗将自动开始广播。

从网络浏览器向 LCD 屏幕发送消息

将 BleuIO 加密狗连接到计算机。运行 Web 脚本以连接到 STM32 上的另一个 BleuIO 加密狗。现在您可以将消息发送到 LCD 屏幕。

为了让这个脚本工作,我们需要

创建一个名为 index.html 的简单 Html 文件,该文件将用作脚本的前端。此 Html 文件包含一些按钮,可帮助连接和读取来自远程加密狗的广告数据,该加密狗连接到 stm32。

html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
      rel="stylesheet"
      integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
      crossorigin="anonymous"
    />
    <title>Send message to STM32 LCD display using Bluetooth LEtitle>
  head>
  <body class="mt-5">
    <div class="container mt-5">
      <h1 class="mb-5">Send message to STM32 LCD display using Bluetooth LEh1>
      <button class="btn btn-success" id="connect">Connectbutton>
      <div class="row">
        <div class="col-md-4">
          <form method="post" id="sendMsgForm" name="sendMsgForm">
            <div class="mb-3">
              <label for="msgToSend" class="form-label">Your Messagelabel>
              <input
                type="text"
                class="form-control"
                name="msgToSend"
                id="msgToSend"
                required
                maxlength="60"
              />
            div>

            <button type="submit" class="btn btn-primary">Submitbutton>
          form>
        div>
      div>

      <br />
      <button class="btn btn-danger" id="clearScreen" disabled>
        Clear screen
      button>
    div>

    <script src="script.js">script>
  body>
html>

创建一个名为 script.js 的 js 文件并将其包含在 Html 文件的底部。这个 js 文件使用 BleuIO js 库来编写 AT 命令并与其他加密狗进行通信

import * as my_dongle from 'bleuio'
const dongleToConnect = '[0]40:48:FD:E5:2F:17'
document.getElementById('connect').addEventListener('click', function() {
	my_dongle.at_connect()
	document.getElementById("clearScreen").disabled = false;
	document.getElementById("connect").disabled = true;
	document.getElementById("sendMsgForm").hidden = false;
})

document.getElementById("sendMsgForm").addEventListener("submit", function(event) {
	event.preventDefault()
	console.log('here')


	my_dongle.ati().then((data) => {
			//make central if not
			if (JSON.stringify(data).includes("Peripheral")) {
				console.log('peripheral')
				my_dongle.at_central().then((x) => {
					console.log('central now')
				})
			}
		})
		.then(() => {
			// connect to dongle
			my_dongle.at_getconn().then((y) => {
					if (JSON.stringify(y).includes(dongleToConnect)) {
						console.log('already connected')
					} else {
						my_dongle.at_gapconnect(dongleToConnect).then(() => {
							console.log('connected successfully')
						})
					}
				})
				.then(() => {
					var theVal = "L=1 " + document.getElementById('msgToSend').value;
					console.log('Message Send 1 ' + theVal)
					// send command to show data
					my_dongle.at_spssend(theVal).then(() => {
						console.log('Message Send ' + theVal)
					})
				})

		})
});



document.getElementById('clearScreen').addEventListener('click', function() {
	my_dongle.ati().then((data) => {
			//make central if not
			if (JSON.stringify(data).includes("Peripheral")) {
				console.log('peripheral')
				my_dongle.at_central().then((x) => {
					console.log('central now')
				})
			}
		})
		.then(() => {
			// connect to dongle
			my_dongle.at_getconn().then((y) => {
					if (JSON.stringify(y).includes(dongleToConnect)) {
						console.log('already connected')
					} else {
						my_dongle.at_gapconnect(dongleToConnect).then(() => {
							console.log('connected successfully')
						})
					}
				})
				.then(() => {
					// send command to clear the screen
					my_dongle.at_spssend('L=0').then(() => {
						console.log('Screen Cleared')
					})
				})

		})
})

 

该脚本有一个按钮可以连接到计算机上的 COM 端口。有一个文本字段,您可以在其中编写消息。您的消息将显示在连接到 STM32 开发板的 LCD 屏幕上。

要连接到 STM32 上的 BleuIO 加密狗,请确保 STM32 已通电并连接了 BleuIO 加密狗。

获取 MAC 地址

按照步骤获取连接到STM32的加密狗的MAC地址

- Open this site https://bleuio.com/web_terminal.html and click connect to dongle.
- Select the appropriate port to connect.
- Once it says connected, type ATI. This will show dongle information and current status.
- If the dongle is on peripheral role, set it to central by typing AT+CENTRAL
- Now do a gap scan by typing AT+GAPSCAN
- Once you see your dongle on the list ,stop the scan by pressing control+c
- Copy the ID and paste it into the script (script.js) line #2
 

运行网页脚本

您将需要一个网络捆绑程序。您可以使用parcel.js

安装 parcel js 后,转到 web 脚本的根目录并输入“parcel index.html” 这将启动您的开发环境。

pYYBAGNYukOAZYK9AAA2QUu69rw587.jpg
 

在浏览器上打开脚本。对于这个例子,我们打开http://localhost:1234

您可以轻松连接到加密狗并将消息发送到 LCD 屏幕。响应将显示在浏览器控制台屏幕上。

网络脚本看起来像这样

pYYBAGNY5vGAYD7QAACS7TLDVGQ340.png
 

输出

信息将显示在液晶显示屏上。

poYBAGNY5vSAMW6dAAEGetjLGUA950.png
 

 


下载该资料的人也在下载 下载该资料的人还在阅读
更多 >

评论

查看更多

下载排行

本周

  1. 1山景DSP芯片AP8248A2数据手册
  2. 1.06 MB  |  532次下载  |  免费
  3. 2RK3399完整板原理图(支持平板,盒子VR)
  4. 3.28 MB  |  339次下载  |  免费
  5. 3TC358743XBG评估板参考手册
  6. 1.36 MB  |  330次下载  |  免费
  7. 4DFM软件使用教程
  8. 0.84 MB  |  295次下载  |  免费
  9. 5元宇宙深度解析—未来的未来-风口还是泡沫
  10. 6.40 MB  |  227次下载  |  免费
  11. 6迪文DGUS开发指南
  12. 31.67 MB  |  194次下载  |  免费
  13. 7元宇宙底层硬件系列报告
  14. 13.42 MB  |  182次下载  |  免费
  15. 8FP5207XR-G1中文应用手册
  16. 1.09 MB  |  178次下载  |  免费

本月

  1. 1OrCAD10.5下载OrCAD10.5中文版软件
  2. 0.00 MB  |  234315次下载  |  免费
  3. 2555集成电路应用800例(新编版)
  4. 0.00 MB  |  33566次下载  |  免费
  5. 3接口电路图大全
  6. 未知  |  30323次下载  |  免费
  7. 4开关电源设计实例指南
  8. 未知  |  21549次下载  |  免费
  9. 5电气工程师手册免费下载(新编第二版pdf电子书)
  10. 0.00 MB  |  15349次下载  |  免费
  11. 6数字电路基础pdf(下载)
  12. 未知  |  13750次下载  |  免费
  13. 7电子制作实例集锦 下载
  14. 未知  |  8113次下载  |  免费
  15. 8《LED驱动电路设计》 温德尔著
  16. 0.00 MB  |  6656次下载  |  免费

总榜

  1. 1matlab软件下载入口
  2. 未知  |  935054次下载  |  免费
  3. 2protel99se软件下载(可英文版转中文版)
  4. 78.1 MB  |  537798次下载  |  免费
  5. 3MATLAB 7.1 下载 (含软件介绍)
  6. 未知  |  420027次下载  |  免费
  7. 4OrCAD10.5下载OrCAD10.5中文版软件
  8. 0.00 MB  |  234315次下载  |  免费
  9. 5Altium DXP2002下载入口
  10. 未知  |  233046次下载  |  免费
  11. 6电路仿真软件multisim 10.0免费下载
  12. 340992  |  191187次下载  |  免费
  13. 7十天学会AVR单片机与C语言视频教程 下载
  14. 158M  |  183279次下载  |  免费
  15. 8proe5.0野火版下载(中文版免费下载)
  16. 未知  |  138040次下载  |  免费