First upload all

main
northpoleforce 5 months ago
parent 47236b9c62
commit 7f7cd2da62

Binary file not shown.

@ -0,0 +1,99 @@
2024-06-17 00:24:03,329 [INFO] Activator:170 -
2024-06-17 00:24:03,331 [INFO] Activator:171 - !SESSION log4j initialized
2024-06-17 00:26:07,661 [INFO] Activator:170 -
2024-06-17 00:26:07,663 [INFO] Activator:171 - !SESSION log4j initialized
2024-06-17 00:28:10,594 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 00:28:10,597 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 00:28:10,598 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 00:28:10,599 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 00:28:10,600 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 00:28:14,858 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] toolchainItems.length=====1
2024-06-17 20:46:26,371 [INFO] Activator:170 -
2024-06-17 20:46:26,374 [INFO] Activator:171 - !SESSION log4j initialized
2024-06-17 20:47:00,539 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 20:47:00,540 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 20:47:00,541 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 20:47:00,541 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 20:47:00,543 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 20:47:05,301 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] toolchainItems.length=====1
2024-06-17 21:24:54,270 [INFO] Activator:170 -
2024-06-17 21:24:54,272 [INFO] Activator:171 - !SESSION log4j initialized
2024-06-17 21:25:23,881 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 21:25:23,883 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 21:25:23,883 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 21:25:23,884 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 21:25:23,885 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] 1 : Invalid condition id : UX_CORESTACK_Condition cause : null
2024-06-17 21:25:27,913 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] toolchainItems.length=====1
2024-06-17 21:53:50,374 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
2024-06-17 21:53:50,376 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2062)
2024-06-17 21:53:50,376 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.getLocationOnScreen(Component.java:2036)
2024-06-17 21:53:50,377 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.JPopupMenu.show(JPopupMenu.java:948)
2024-06-17 21:53:50,377 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.plaf.basic.BasicComboPopup.show(BasicComboPopup.java:211)
2024-06-17 21:53:50,378 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.plaf.basic.BasicComboPopup.togglePopup(BasicComboPopup.java:1170)
2024-06-17 21:53:50,378 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.plaf.basic.BasicComboPopup$Handler.mousePressed(BasicComboPopup.java:858)
2024-06-17 21:53:50,379 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.processMouseEvent(Component.java:6536)
2024-06-17 21:53:50,379 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
2024-06-17 21:53:50,380 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.processEvent(Component.java:6304)
2024-06-17 21:53:50,380 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Container.processEvent(Container.java:2239)
2024-06-17 21:53:50,381 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.dispatchEventImpl(Component.java:4889)
2024-06-17 21:53:50,381 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Container.dispatchEventImpl(Container.java:2297)
2024-06-17 21:53:50,381 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.dispatchEvent(Component.java:4711)
2024-06-17 21:53:50,383 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.plaf.basic.BasicTableUI$Handler.repostEvent(BasicTableUI.java:948)
2024-06-17 21:53:50,384 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1110)
2024-06-17 21:53:50,384 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1038)
2024-06-17 21:53:50,385 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
2024-06-17 21:53:50,385 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:279)
2024-06-17 21:53:50,386 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:279)
2024-06-17 21:53:50,386 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:279)
2024-06-17 21:53:50,387 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.processMouseEvent(Component.java:6536)
2024-06-17 21:53:50,387 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
2024-06-17 21:53:50,388 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.processEvent(Component.java:6304)
2024-06-17 21:53:50,389 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Container.processEvent(Container.java:2239)
2024-06-17 21:53:50,389 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.dispatchEventImpl(Component.java:4889)
2024-06-17 21:53:50,390 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Container.dispatchEventImpl(Container.java:2297)
2024-06-17 21:53:50,390 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.dispatchEvent(Component.java:4711)
2024-06-17 21:53:50,391 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
2024-06-17 21:53:50,391 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4532)
2024-06-17 21:53:50,392 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
2024-06-17 21:53:50,392 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Container.dispatchEventImpl(Container.java:2283)
2024-06-17 21:53:50,393 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.Component.dispatchEvent(Component.java:4711)
2024-06-17 21:53:50,393 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
2024-06-17 21:53:50,394 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventQueue.access$500(EventQueue.java:97)
2024-06-17 21:53:50,394 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventQueue$3.run(EventQueue.java:709)
2024-06-17 21:53:50,394 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventQueue$3.run(EventQueue.java:703)
2024-06-17 21:53:50,395 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.security.AccessController.doPrivileged(Native Method)
2024-06-17 21:53:50,395 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
2024-06-17 21:53:50,396 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
2024-06-17 21:53:50,396 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventQueue$4.run(EventQueue.java:733)
2024-06-17 21:53:50,397 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventQueue$4.run(EventQueue.java:731)
2024-06-17 21:53:50,397 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.security.AccessController.doPrivileged(Native Method)
2024-06-17 21:53:50,398 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
2024-06-17 21:53:50,398 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
2024-06-17 21:53:50,399 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
2024-06-17 21:53:50,400 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
2024-06-17 21:53:50,400 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
2024-06-17 21:53:50,401 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
2024-06-17 21:53:50,401 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
2024-06-17 21:53:50,401 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
2024-06-17 21:58:48,688 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] Command line
2024-06-17 21:58:48,891 [INFO] LogOutputStream:76 - [STDOUT_REDIRECT] Command line
2024-06-17 22:27:07,513 [INFO] Activator:170 -
2024-06-17 22:27:07,516 [INFO] Activator:171 - !SESSION log4j initialized
2024-06-17 22:28:42,632 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] Managed Build system manifest file error: Option com.st.stm32cube.ide.mcu.debug.option.cpuclock.1489089778 uses a null category that is invalid in its context. The option was ignored.
2024-06-17 22:28:42,649 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] Managed Build system manifest file error: Option com.st.stm32cube.ide.mcu.debug.option.cpuclock.1821181692 uses a null category that is invalid in its context. The option was ignored.
2024-06-17 22:50:13,701 [INFO] Activator:170 -
2024-06-17 22:50:13,703 [INFO] Activator:171 - !SESSION log4j initialized
2024-06-17 22:50:14,403 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] Managed Build system manifest file error: Option com.st.stm32cube.ide.mcu.debug.option.cpuclock.1489089778 uses a null category that is invalid in its context. The option was ignored.
2024-06-17 22:50:14,524 [ERROR] LogOutputStream:74 - [STDERR_REDIRECT] Managed Build system manifest file error: Option com.st.stm32cube.ide.mcu.debug.option.cpuclock.1821181692 uses a null category that is invalid in its context. The option was ignored.

File diff suppressed because it is too large Load Diff

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="DEBUGFILE" class="org.apache.log4j.RollingFileAppender">
<param name="Append" value="true"/>
<param name="File" value="C:/Users/north/Desktop/物联网通信技术/课设/WS/.metadata/.ide.log"/>
<param name="MaxBackupIndex" value="1"/>
<param name="MaxFileSize" value="50MB"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS} [%p] %c{1}:%L - %m%n"/>
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="DEBUGFILE"/>
</root>
</log4j:configuration>

@ -0,0 +1,6 @@
*** SESSION 6月 17, 2024 00:26:03.37 --------------------------------------------
*** SESSION 6月 17, 2024 20:46:21.85 --------------------------------------------
*** SESSION 6月 17, 2024 21:24:51.46 --------------------------------------------
*** SESSION 6月 17, 2024 22:27:04.43 --------------------------------------------
*** SESSION 6月 17, 2024 22:50:11.29 --------------------------------------------
*** SESSION 6月 20, 2024 12:41:29.40 --------------------------------------------

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 5.0.0?><preferences>
<storageModule moduleId="org.eclipse.cdt.core.settings">
<preferences configRelations="2"/>
</storageModule>
</preferences>

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<section name="Workbench">
<section name="org.eclipse.cdt.internal.ui.MakeView">
</section>
</section>

@ -0,0 +1,36 @@
12:49:04 **** Incremental Build of configuration Debug for project STM32_NB-IoT ****
make -j16 all
arm-none-eabi-gcc "../Core/Src/main.c" -mcpu=cortex-m4 -std=gnu11 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32L431xx -c -I../Core/Inc -I../Drivers/STM32L4xx_HAL_Driver/Inc -I../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32L4xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -o "Core/Src/main.o"
../Core/Src/main.c: In function 'main':
../Core/Src/main.c:103:3: warning: implicit declaration of function 'NB_Init'; did you mean 'HAL_Init'? [-Wimplicit-function-declaration]
NB_Init(); //初始化NB模组
^~~~~~~
HAL_Init
../Core/Src/main.c:128:4: warning: implicit declaration of function 'nb_reopen'; did you mean 'freopen'? [-Wimplicit-function-declaration]
nb_reopen();
^~~~~~~~~
freopen
../Core/Src/main.c:138:4: warning: implicit declaration of function 'nb_iotLwM2M_send' [-Wimplicit-function-declaration]
nb_iotLwM2M_send(send);
^~~~~~~~~~~~~~~~
../Core/Src/main.c:140:3: warning: implicit declaration of function 'NB_REC'; did you mean '__REV'? [-Wimplicit-function-declaration]
NB_REC(); //接收数据并检查是否接收了指令
^~~~~~
__REV
arm-none-eabi-gcc -o "STM32_NB-IoT.elf" @"objects.list" -mcpu=cortex-m4 -T"D:\WS\STM32_NB-IoT\STM32L431RCTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="STM32_NB-IoT.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wl,--start-group -lc -lm -Wl,--end-group
Finished building target: STM32_NB-IoT.elf
arm-none-eabi-objdump -h -S STM32_NB-IoT.elf > "STM32_NB-IoT.list"
arm-none-eabi-objcopy -O binary STM32_NB-IoT.elf "STM32_NB-IoT.bin"
arm-none-eabi-size STM32_NB-IoT.elf
text data bss dec hex filename
31840 128 12304 44272 acf0 STM32_NB-IoT.elf
Finished building: default.size.stdout
Finished building: STM32_NB-IoT.bin
Finished building: STM32_NB-IoT.list
12:49:06 Build Finished. 0 errors, 4 warnings. (took 1s.393ms)

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<section name="Workbench">
<section name="completion_proposal_size">
</section>
<section name="org.eclipse.cdt.ui.text.hover.CMacroExpansionExploration">
</section>
</section>

@ -0,0 +1,218 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
nb_heartbeat();
tot++;
if (tot == 50) {
tot = 0;
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
}
// NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(100);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,222 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
while (1)
{
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// Read_Data(&Temperatrue, &Humidity);
// sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
// sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
// printf("%s\r\n", datah);
// printf("%s\r\n", datat);
sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
// NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,228 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_Delay(5000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
nb_heartbeat();
tot++;
if (tot == 5) {
tot = 0;
nb_reopen();
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
}
NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,217 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
while (1)
{
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
sprintf(send, "%02x%02x%02x%04d\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
// NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,17 @@
/*
* BH1750.c
*
* Created on: 2024年6月19日
* Author: north
*/
//写入指令
void Single_Write_BH1750(uchar REG_Address)//REG_Address是要写入的指令
{
BH1750_Start(); //起始信号
BH1750_SendByte(SlaveAddress); //发送设备地址+写信号
BH1750_SendByte(REG_Address); //写入指令
BH1750_Stop(); //发送停止信号
}

@ -0,0 +1,178 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 10240
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("===NB-->MCU(raw):%s===\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)",01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)",03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
// nb_iotSendCmd((uint8_t *) "AT+NMGS=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
// strcpy(post, "AT+NMGS==5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
if (isPrintf) {
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
} else {
while(1) {
// printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
// printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
NB_REC();
// printf("Success!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
NB_REC();
// printf("Fail!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}
}

@ -0,0 +1,230 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_Delay(5000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// nb_heartbeat();
tot++;
if (tot == 5) {
tot = 0;
nb_reopen();
HAL_Delay(5000);
}
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
// }
NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,151 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,152 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
printf("Attach!\r\n");
}
}
void nb_iotLwM2M_send() {
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,00\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,231 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//char datah[100],datat[100];
//float Humidity,Temperatrue;//湿度温度数据存储
//char deg[] = {0xa1,0xe3};
//char back[]="Success!\n\r\n"; //返回的数<E79A84>???
//char send[50];
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// Init_SHT30();//初始化传感器
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char datah[100],datat[100];
float Humidity = 1,Temperatrue = 1;//湿度温度数据存储
char deg[] = {0xa1,0xe3};
char back[]="Success!\n\r\n"; //返回的数<E79A84>???
char send[50];
nb_iotAttachLwM2M(1, 1);
while (1)
{
Read_Data(&Temperatrue, &Humidity);
sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
printf("%s\r\n", datah);
printf("%s\r\n", datat);
sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
printf("%s\r\n", send);
// HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
// HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
nb_iotLwM2M_send(send);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,176 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 10240
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("===NB-->MCU(raw):%s===\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)",01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)",03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
// nb_iotSendCmd((uint8_t *) "AT+NMGS=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
// strcpy(post, "AT+NMGS==5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
if (isPrintf) {
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
} else {
while(1) {
// printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
// printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
// printf("Success!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
// printf("Fail!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}
}

@ -0,0 +1,156 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // AT+QLWSREGIND=0
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,94 @@
/*
* BH1750.h
*
* Created on: 2024年6月19日
* Author: north
*/
#ifndef INC_BH1750_H_
#define INC_BH1750_H_
#ifndef __BH1750_H
#define __BH1750_H
#include "sys.h"
//BH1750的地址
#define BH1750_Addr 0x46
//BH1750指令码
#define POWER_OFF 0x00
#define POWER_ON 0x01
#define MODULE_RESET 0x07
#define CONTINUE_H_MODE 0x10
#define CONTINUE_H_MODE2 0x11
#define CONTINUE_L_MODE 0x13
#define ONE_TIME_H_MODE 0x20
#define ONE_TIME_H_MODE2 0x21
#define ONE_TIME_L_MODE 0x23
//测量模式
#define Measure_Mode CONTINUE_H_MODE
//分辨率 光照强度单位lx=High Byte + Low Byte/ 1.2 * 测量精度
#if ((Measure_Mode==CONTINUE_H_MODE2)|(Measure_Mode==ONE_TIME_H_MODE2))
#define Resolurtion 0.5
#elif ((Measure_Mode==CONTINUE_H_MODE)|(Measure_Mode==ONE_TIME_H_MODE))
#define Resolurtion 1
#elif ((Measure_Mode==CONTINUE_L_MODE)|(Measure_Mode==ONE_TIME_L_MODE))
#define Resolurtion 4
#endif
#define BH1750_I2C_WR 0 /* 写控制bit */
#define BH1750_I2C_RD 1 /* 读控制bit */
/* 定义I2C总线连接的GPIO端口, 只需要修改下面4行代码即可任意改变SCL和SDA的引脚 */
#define BH1750_GPIO_PORT_I2C GPIOB /* GPIO端口 */
#define BH1750_RCC_I2C_PORT RCC_APB2Periph_GPIOB /* GPIO端口时钟 */
#define BH1750_I2C_SCL_PIN GPIO_Pin_10 /* 连接到SCL时钟线的GPIO */
#define BH1750_I2C_SDA_PIN GPIO_Pin_11
/* 连接到SDA数据线的GPIO */
/* 定义读写SCL和SDA的宏已增加代码的可移植性和可阅读性 */
#if 0 /* 条件编译: 1 选择GPIO的库函数实现IO读写 */
#define BH1750_I2C_SCL_1() GPIO_SetBits(BH1750_GPIO_PORT_I2C, BH1750_I2C_SCL_PIN) /* SCL = 1 */
#define BH1750_I2C_SCL_0() GPIO_ResetBits(BH1750_GPIO_PORT_I2C, BH1750_I2C_SCL_PIN) /* SCL = 0 */
#define BH1750_I2C_SDA_1() GPIO_SetBits(BH1750_GPIO_PORT_I2C, BH1750_I2C_SDA_PIN) /* SDA = 1 */
#define BH1750_I2C_SDA_0() GPIO_ResetBits(BH1750_GPIO_PORT_I2C, BH1750_I2C_SDA_PIN) /* SDA = 0 */
#define BH1750_I2C_SDA_READ() GPIO_ReadInputDataBit(BH1750_GPIO_PORT_I2C, BH1750_I2C_SDA_PIN) /* 读SDA口线状态 */
#else /* 这个分支选择直接寄存器操作实现IO读写 */
/* 注意如下写法在IAR最高级别优化时会被编译器错误优化 */
#define BH1750_I2C_SCL_1() BH1750_GPIO_PORT_I2C->BSRR = BH1750_I2C_SCL_PIN /* SCL = 1 */
#define BH1750_I2C_SCL_0() BH1750_GPIO_PORT_I2C->BRR = BH1750_I2C_SCL_PIN /* SCL = 0 */
#define BH1750_I2C_SDA_1() BH1750_GPIO_PORT_I2C->BSRR = BH1750_I2C_SDA_PIN /* SDA = 1 */
#define BH1750_I2C_SDA_0() BH1750_GPIO_PORT_I2C->BRR = BH1750_I2C_SDA_PIN /* SDA = 0 */
#define BH1750_I2C_SDA_READ() ((BH1750_GPIO_PORT_I2C->IDR & BH1750_I2C_SDA_PIN) != 0) /* 读SDA口线状态 */
#endif
void i2c_Start(void);
void i2c_Stop(void);
void i2c_SendByte(uint8_t _ucByte);
uint8_t i2c_ReadByte(void);
uint8_t i2c_WaitAck(void);
void i2c_Ack(void);
void i2c_NAck(void);
uint8_t i2c_CheckDevice(uint8_t _Address);
void BH1750_Init(void); //未包含IIC初始化
float LIght_Intensity(void); //读取光照强度的值
uint8_t BH1750_Byte_Write(uint8_t data);
uint16_t BH1750_Read_Measure(void);
void BH1750_Power_ON(void);
void BH1750_Power_OFF(void);
void BH1750_RESET(void);
#endif
#endif /* INC_BH1750_H_ */

@ -0,0 +1,218 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
// nb_heartbeat();
// tot++;
// if (tot == 10) {
// tot = 0;
// E53_IA1_Read_Data();
// float hum = E53_IA1_Data.Humidity;
// float tem = E53_IA1_Data.Temperature;
// float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
// printf("%s\r\n", send);
// nb_iotLwM2M_send(send);
// }
NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,83 @@
Core/Src/usart.o: ../Core/Src/usart.c ../Core/Inc/usart.h \
../Core/Inc/main.h ../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h \
../Core/Inc/stm32l4xx_hal_conf.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h \
../Drivers/CMSIS/Include/core_cm4.h \
../Drivers/CMSIS/Include/cmsis_version.h \
../Drivers/CMSIS/Include/cmsis_compiler.h \
../Drivers/CMSIS/Include/cmsis_gcc.h \
../Drivers/CMSIS/Include/mpu_armv7.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h
../Core/Inc/usart.h:
../Core/Inc/main.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h:
../Core/Inc/stm32l4xx_hal_conf.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h:
../Drivers/CMSIS/Include/core_cm4.h:
../Drivers/CMSIS/Include/cmsis_version.h:
../Drivers/CMSIS/Include/cmsis_compiler.h:
../Drivers/CMSIS/Include/cmsis_gcc.h:
../Drivers/CMSIS/Include/mpu_armv7.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h:

@ -0,0 +1,177 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 10240
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("===NB-->MCU(raw):%s===\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)",01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)",03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
// nb_iotSendCmd((uint8_t *) "AT+NMGS=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
// strcpy(post, "AT+NMGS==5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
if (isPrintf) {
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
} else {
while(1) {
// printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
// printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
// printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
// printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}
}

@ -0,0 +1,237 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("NB-->MCU(raw):%s\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// /*电机控制指令*/
// if(pos1)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"MOTOR_SW\":0"))
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
//
// }
// else
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// /*处理电机控制指令*/
// /*LED控制指令*/
// if(pos2)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"LED_SW\":1"))
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// else
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// else{
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// }
// /*处理LED控制指令*/
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
//void nb_heartbeat() {
// nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
//}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,218 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
nb_heartbeat();
tot++;
if (tot == 10) {
tot = 0;
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
}
NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,227 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_Delay(5000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// nb_heartbeat();
// tot++;
// if (tot == 10) {
// tot = 0;
// E53_IA1_Read_Data();
// float hum = E53_IA1_Data.Humidity;
// float tem = E53_IA1_Data.Temperature;
// float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
// printf("%s\r\n", send);
// nb_iotLwM2M_send(send);
// }
// NB_REC(); //接收数据并检查是否接收了指令
// HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,236 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// Init_SHT30();//初始化传感器
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char datah[100],datat[100];
float Humidity = 1,Temperatrue = 1;//湿度温度数据存储
char deg[] = {0xa1,0xe3};
char back[]="Success!\n\r\n"; //返回的数<E79A84>???
char send[50];
NB_Init(); //初始化NB模组
while (1)
{
// E53_IA1_Read_Data();
// float hum = E53_IA1_Data.Humidity;
// float tem = E53_IA1_Data.Temperature;
// float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// Read_Data(&Temperatrue, &Humidity);
// sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
// sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
// printf("%s\r\n", datah);
// printf("%s\r\n", datat);
// sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
// printf("%s\r\n", send);
// HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
// HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
// nb_iotLwM2M_send(send);
// NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,176 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 10240
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("===NB-->MCU(raw):%s===\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)",01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)",03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
// nb_iotSendCmd((uint8_t *) "AT+NMGS=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
// strcpy(post, "AT+NMGS==5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
if (isPrintf) {
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
NB_REC();
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
} else {
while(1) {
// printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
// printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
// printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
// printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}
}

@ -0,0 +1,212 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
while (1)
{
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
// NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,15 @@
stm32l4xx_hal_rcc.c:265:19:HAL_RCC_DeInit 16 static
stm32l4xx_hal_rcc.c:405:19:HAL_RCC_OscConfig 40 static
stm32l4xx_hal_rcc.c:1097:19:HAL_RCC_ClockConfig 24 static
stm32l4xx_hal_rcc.c:1339:6:HAL_RCC_MCOConfig 48 static
stm32l4xx_hal_rcc.c:1398:10:HAL_RCC_GetSysClockFreq 40 static
stm32l4xx_hal_rcc.c:1486:10:HAL_RCC_GetHCLKFreq 4 static
stm32l4xx_hal_rcc.c:1497:10:HAL_RCC_GetPCLK1Freq 8 static
stm32l4xx_hal_rcc.c:1509:10:HAL_RCC_GetPCLK2Freq 8 static
stm32l4xx_hal_rcc.c:1522:6:HAL_RCC_GetOscConfig 16 static
stm32l4xx_hal_rcc.c:1681:6:HAL_RCC_GetClockConfig 16 static
stm32l4xx_hal_rcc.c:1716:6:HAL_RCC_EnableCSS 4 static
stm32l4xx_hal_rcc.c:1726:6:HAL_RCC_NMI_IRQHandler 8 static
stm32l4xx_hal_rcc.c:1743:13:HAL_RCC_CSSCallback 4 static
stm32l4xx_hal_rcc.c:1757:10:HAL_RCC_GetResetSource 16 static
stm32l4xx_hal_rcc.c:1787:26:RCC_SetFlashLatencyFromMSIRange 32 static

@ -0,0 +1,237 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("NB-->MCU(raw):%s\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// /*电机控制指令*/
// if(pos1)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"MOTOR_SW\":0"))
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
//
// }
// else
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// /*处理电机控制指令*/
// /*LED控制指令*/
// if(pos2)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"LED_SW\":1"))
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// else
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// else{
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// }
// /*处理LED控制指令*/
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,231 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1 \r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(unit8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
// char*pos1 = strstr((char *) LPUART1_RX_BUF, (const char *) "MOTOR");
// char*pos2 = strstr((char *) LPUART1_RX_BUF, (const char *) "LED");
printf("NB-->MCU(raw):%s\r\n",LPUART1_RX_BUF);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"01")) { // 开头01控制LED
printf("LED\r\n");
if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
printf("LED OFF\r\n");
}
else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
printf("LED ON\r\n");
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"03")) { // 开头03控制电机
printf("Motro\r\n");
if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
printf("Moter OFF\r\n");
}
else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
printf("Motor ON\r\n");
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// /*电机控制指令*/
// if(pos1)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"MOTOR_SW\":0"))
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
//
// }
// else
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// /*处理电机控制指令*/
// /*LED控制指令*/
// if(pos2)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"LED_SW\":1"))
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// else
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// else{
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// }
// /*处理LED控制指令*/
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,230 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1 \r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(unit8_t *str, int status) {
int len = strlen(str);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
// char*pos1 = strstr((char *) LPUART1_RX_BUF, (const char *) "MOTOR");
// char*pos2 = strstr((char *) LPUART1_RX_BUF, (const char *) "LED");
printf("NB-->MCU(raw):%s\r\n",LPUART1_RX_BUF);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"01")) { // 开头01控制LED
printf("LED\r\n");
if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
printf("LED OFF\r\n");
}
else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
printf("LED ON\r\n");
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"03")) { // 开头03控制电机
printf("Motro\r\n");
if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
printf("Moter OFF\r\n");
}
else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
printf("Motor ON\r\n");
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// /*电机控制指令*/
// if(pos1)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"MOTOR_SW\":0"))
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
//
// }
// else
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// /*处理电机控制指令*/
// /*LED控制指令*/
// if(pos2)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"LED_SW\":1"))
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// else
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// else{
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// }
// /*处理LED控制指令*/
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,139 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,13 @@
/*
* BH1750.h
*
* Created on: 2024年6月19日
* Author: north
*/
#ifndef INC_BH1750_H_
#define INC_BH1750_H_
#endif /* INC_BH1750_H_ */

@ -0,0 +1,223 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
while (1)
{
// E53_IA1_Read_Data();
// float hum = E53_IA1_Data.Humidity;
// float tem = E53_IA1_Data.Temperature;
// float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// Read_Data(&Temperatrue, &Humidity);
// sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
// sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
// printf("%s\r\n", datah);
// printf("%s\r\n", datat);
// sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
// printf("%s\r\n", send);
// nb_iotLwM2M_send(send);
// NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,13 @@
/*
* E53_IA1.h
*
* Created on: 2024年6月19日
* Author: north
*/
#ifndef INC_E53_IA1_H_
#define INC_E53_IA1_H_
#endif /* INC_E53_IA1_H_ */

@ -0,0 +1,48 @@
/*
* BH1750.c
*
* Created on: 2024年6月19日
* Author: north
*/
//写入指令
void Single_Write_BH1750(uchar REG_Address)//REG_Address是要写入的指令
{
BH1750_Start(); //起始信号
BH1750_SendByte(SlaveAddress); //发送设备地址+写信号
BH1750_SendByte(REG_Address); //写入指令
BH1750_Stop(); //发送停止信号
}
//读取指令
void mread(void)
{
uchar i;
BH1750_Start(); //起始信号
BH1750_SendByte(SlaveAddress+1); //发送设备地址+读信号
//注意这里的for函数的i<2和下面的if函数的i==2我发现以前的工程写的居然是3
//这里其实我们只需要读取2个字节就行了后面的合成数据也是只用了BUF的前2个字节
//工程文件我没改这个驱动程序以前也用在了多个项目上读取3个字节肯定是也可以正常运行的
//但是我觉得还是改成2比较好你们可以测试一下改成2有没有问题测试之后一定要告诉我结果谢谢!!
for (i=0; i<2; i++) //连续读取2个数据存储到BUF里面
{
BUF[i] = BH1750_RecvByte(); //BUF[0]存储高8位BUF[1]存储低8位
if (i == 1)
{
BH1750_SendACK(1); //最后一个数据需要回NOACK
}
else
{
BH1750_SendACK(0); //回应ACK
}
}
BH1750_Stop(); //停止信号
delay_ms(5);
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接https://blog.csdn.net/ShenZhen_zixian/article/details/103542972

@ -0,0 +1,80 @@
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.o: \
../Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_flash_ramfunc.c \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h \
../Core/Inc/stm32l4xx_hal_conf.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h \
../Drivers/CMSIS/Include/core_cm4.h \
../Drivers/CMSIS/Include/cmsis_version.h \
../Drivers/CMSIS/Include/cmsis_compiler.h \
../Drivers/CMSIS/Include/cmsis_gcc.h \
../Drivers/CMSIS/Include/mpu_armv7.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h:
../Core/Inc/stm32l4xx_hal_conf.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h:
../Drivers/CMSIS/Include/core_cm4.h:
../Drivers/CMSIS/Include/cmsis_version.h:
../Drivers/CMSIS/Include/cmsis_compiler.h:
../Drivers/CMSIS/Include/cmsis_gcc.h:
../Drivers/CMSIS/Include/mpu_armv7.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h:

@ -0,0 +1,225 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1 \r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(char *str, int status) {
int len = strlen(str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
// char*pos1 = strstr((char *) LPUART1_RX_BUF, (const char *) "MOTOR");
// char*pos2 = strstr((char *) LPUART1_RX_BUF, (const char *) "LED");
printf("NB-->MCU(raw):%s\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"01") { // 开头01控制LED
if (check01((const char *)LPUART1_RX_BUF, 0)) { // 判断OFF
}
else if (check01((const char *)LPUART1_RX_BUF, 1)) { // 判断ON
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"03") { // 开头03控制电机
if (check01((const char *)LPUART1_RX_BUF, 0)) { // 判断OFF
}
else if (check01((const char *)LPUART1_RX_BUF, 1)) { // 判断ON
}
}
}
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// /*电机控制指令*/
// if(pos1)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"MOTOR_SW\":0"))
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
//
// }
// else
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// /*处理电机控制指令*/
// /*LED控制指令*/
// if(pos2)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"LED_SW\":1"))
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// else
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// else{
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// }
// /*处理LED控制指令*/
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,238 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//char datah[100],datat[100];
//float Humidity,Temperatrue;//湿度温度数据存储
//char deg[] = {0xa1,0xe3};
//char back[]="Success!\n\r\n"; //返回的数<E79A84>???
//char send[50];
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// Init_SHT30();//初始化传感器
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char datah[100],datat[100];
float Humidity = 1,Temperatrue = 1;//湿度温度数据存储
char deg[] = {0xa1,0xe3};
char back[]="Success!\n\r\n"; //返回的数<E79A84>???
char send[50];
// nb_iotAttachLwM2M(1, 1);
while (1)
{
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
// printf("hum:%.2f , tem:%.2f ,lux:%.2f\r\n",hum,tem,lux);
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// Read_Data(&Temperatrue, &Humidity);
// sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
// sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
// printf("%s\r\n", datah);
// printf("%s\r\n", datat);
// sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
// printf("%s\r\n", send);
// HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
// HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
// nb_iotLwM2M_send(send);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,8 @@
/*
* Temperatrue_Humidity.c
*
* Created on: 2024年6月19日
* Author: north
*/

@ -0,0 +1,223 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>??)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//char datah[100],datat[100];
//float Humidity,Temperatrue;//湿度温度数据存储
//char deg[] = {0xa1,0xe3};
//char back[]="Success!\n\r\n"; //返回的数<E79A84>??
//char send[50];
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// char datah[100],datat[100];
// float Humidity = 1,Temperatrue = 1;//湿度温度数据存储
// char deg[] = {0xa1,0xe3};
// char back[]="Success!\n\r\n"; //返回的数<E79A84>??
// char send[50];
// nb_iotAttachLwM2M(1, 1);
while (1)
{
Read_Data(&Temperatrue, &Humidity);
sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
printf("%s\r\n", datah);
printf("%s\r\n", datat);
sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
printf("%s\r\n", send);
// HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
// HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
// nb_iotLwM2M_send(send);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV4;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,227 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_Delay(5000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
nb_heartbeat();
// tot++;
// if (tot == 10) {
// tot = 0;
// E53_IA1_Read_Data();
// float hum = E53_IA1_Data.Humidity;
// float tem = E53_IA1_Data.Temperature;
// float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
// printf("%s\r\n", send);
// nb_iotLwM2M_send(send);
// }
NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,80 @@
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_exti.o: \
../Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_exti.c \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h \
../Core/Inc/stm32l4xx_hal_conf.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h \
../Drivers/CMSIS/Include/core_cm4.h \
../Drivers/CMSIS/Include/cmsis_version.h \
../Drivers/CMSIS/Include/cmsis_compiler.h \
../Drivers/CMSIS/Include/cmsis_gcc.h \
../Drivers/CMSIS/Include/mpu_armv7.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h:
../Core/Inc/stm32l4xx_hal_conf.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h:
../Drivers/CMSIS/Include/core_cm4.h:
../Drivers/CMSIS/Include/cmsis_version.h:
../Drivers/CMSIS/Include/cmsis_compiler.h:
../Drivers/CMSIS/Include/cmsis_gcc.h:
../Drivers/CMSIS/Include/mpu_armv7.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h:

@ -0,0 +1,225 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
HAL_Delay(5000);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
HAL_Delay(5000);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
HAL_Delay(5000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// nb_heartbeat();
// tot++;
// if (tot == 10) {
// tot = 0;
// E53_IA1_Read_Data();
// float hum = E53_IA1_Data.Humidity;
// float tem = E53_IA1_Data.Temperature;
// float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
// printf("%s\r\n", send);
// nb_iotLwM2M_send(send);
// }
// NB_REC(); //接收数据并检查是否接收了指令
// HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,151 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
printf("Attach!\r\n");
}
}
void nb_iotLwM2M_send() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,243 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//char datah[100],datat[100];
//float Humidity,Temperatrue;//湿度温度数据存储
//char deg[] = {0xa1,0xe3};
//char back[]="Success!\n\r\n"; //返回的数<E79A84>???
//char send[50];
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// Init_SHT30();//初始化传感器
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char datah[100],datat[100];
float Humidity = 1,Temperatrue = 1;//湿度温度数据存储
char deg[] = {0xa1,0xe3};
char back[]="Success!\n\r\n"; //返回的数<E79A84>???
char send[50];
// nb_iotAttachLwM2M(1, 1);
while (1)
{
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
// printf("hum:%.2f , tem:%.2f ,lux:%.2f\r\n",hum,tem,lux);
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
HAL_Delay(2000);
HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// Read_Data(&Temperatrue, &Humidity);
// sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
// sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
// printf("%s\r\n", datah);
// printf("%s\r\n", datat);
// sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
// printf("%s\r\n", send);
// HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
// HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
// nb_iotLwM2M_send(send);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,8 @@
/*
* hexstring.c
*
* Created on: 2024年6月19日
* Author: north
*/

@ -0,0 +1,123 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);//各类at指令
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "+CEREG", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,39.108.76.174,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "+CEREG", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,39.108.76.174,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"39.108.76.174\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"zhudidi\",\"zhudidi\",\"zhudidi\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//娓呴櫎缂撳瓨
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,145 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,181 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 10240
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void restart() {
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("===NB-->MCU(raw):%s===\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)",01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)",03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
// nb_iotSendCmd((uint8_t *) "AT+NMGS=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
// strcpy(post, "AT+NMGS==5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
if (isPrintf) {
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
} else {
while(1) {
// printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
// printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
NB_REC();
// printf("Success!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
NB_REC();
// printf("Fail!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}
}

@ -0,0 +1,8 @@
/*
* BH1750.c
*
* Created on: 2024年6月19日
* Author: north
*/

@ -0,0 +1,228 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_Delay(5000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
nb_heartbeat();
tot++;
if (tot == 5) {
tot = 0;
nb_reopen();
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
}
NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(2000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,151 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
printf("Attach!\r\n");
}
}
void nb_iotLwM2M() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,201 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:是)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
// nb_iotAttachmqtt(1, 1);
nb_iotAttachLwM2M(1, 1);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV4;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,224 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1 \r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(char *str, int status) {
int len = strlen(str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
// char*pos1 = strstr((char *) LPUART1_RX_BUF, (const char *) "MOTOR");
// char*pos2 = strstr((char *) LPUART1_RX_BUF, (const char *) "LED");
printf("NB-->MCU(raw):%s\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"01")) { // 开头01控制LED
if (check01((const char *)LPUART1_RX_BUF, 0)) { // 判断OFF
printf("LED OFF\r\n");
}
else if (check01((const char *)LPUART1_RX_BUF, 1)) { // 判断ON
printf("LED ON\r\n");
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"03")) { // 开头03控制电机
if (check01((const char *)LPUART1_RX_BUF, 0)) { // 判断OFF
printf("Moter OFF\r\n");
}
else if (check01((const char *)LPUART1_RX_BUF, 1)) { // 判断ON
printf("Motor ON\r\n");
}
}
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// /*电机控制指令*/
// if(pos1)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"MOTOR_SW\":0"))
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
//
// }
// else
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// /*处理电机控制指令*/
// /*LED控制指令*/
// if(pos2)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"LED_SW\":1"))
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// else
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// else{
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// }
// /*处理LED控制指令*/
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,225 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1 \r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(char *str, int status) {
int len = strlen(str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
// char*pos1 = strstr((char *) LPUART1_RX_BUF, (const char *) "MOTOR");
// char*pos2 = strstr((char *) LPUART1_RX_BUF, (const char *) "LED");
printf("NB-->MCU(raw):%s\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"01") { // 开头01控制LED
if (check01((const char *)LPUART1_RX_BUF, 0)) { // 判断OFF
printf("LED OFF\r\n");
}
else if (check01((const char *)LPUART1_RX_BUF, 1)) { // 判断ON
printf("LED ON\r\n");
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"03") { // 开头03控制电机
if (check01((const char *)LPUART1_RX_BUF, 0)) { // 判断OFF
printf("Moter OFF\r\n");
}
else if (check01((const char *)LPUART1_RX_BUF, 1)) { // 判断ON
printf("Motor ON\r\n");
}
}
}
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// /*电机控制指令*/
// if(pos1)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"MOTOR_SW\":0"))
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
//
// }
// else
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// /*处理电机控制指令*/
// /*LED控制指令*/
// if(pos2)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"LED_SW\":1"))
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// else
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// else{
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// }
// /*处理LED控制指令*/
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,126 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,210 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:是)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
char datah[100],datat[100];
float Humidity = 0,Temperatrue = 0;//湿度温度数据存储
char deg[] = {0xa1,0xe3};
char back[]="Success!\n\r\n"; //返回的数据
nb_iotAttachLwM2M(1, 1);
while (1)
{
/* USER CODE END WHILE */
Read_Data(&Temperatrue, &Humidity);
sprintf(datah,"Humidity is %d%%.\n\r\n",(int)Humidity);
sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
nb_iotLwM2M_send();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV4;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,181 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 10240
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void nb_reopen() {
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, 1); // 开始注册 IoT 平台
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("===NB-->MCU(raw):%s===\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)",01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)",03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
// nb_iotSendCmd((uint8_t *) "AT+NMGS=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
// strcpy(post, "AT+NMGS==5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
if (isPrintf) {
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
} else {
while(1) {
// printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
// printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
NB_REC();
// printf("Success!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
NB_REC();
// printf("Fail!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}
}

@ -0,0 +1,7 @@
/*
* BH1750.c
*
* Created on: 2024年6月19日
* Author: north
*/

@ -0,0 +1,91 @@
Core/Src/main.o: ../Core/Src/main.c ../Core/Inc/main.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h \
../Core/Inc/stm32l4xx_hal_conf.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h \
../Drivers/CMSIS/Include/core_cm4.h \
../Drivers/CMSIS/Include/cmsis_version.h \
../Drivers/CMSIS/Include/cmsis_compiler.h \
../Drivers/CMSIS/Include/cmsis_gcc.h \
../Drivers/CMSIS/Include/mpu_armv7.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h \
../Core/Inc/usart.h ../Core/Inc/main.h ../Core/Inc/gpio.h \
../Core/Inc/nb.h
../Core/Inc/main.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h:
../Core/Inc/stm32l4xx_hal_conf.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h:
../Drivers/CMSIS/Include/core_cm4.h:
../Drivers/CMSIS/Include/cmsis_version.h:
../Drivers/CMSIS/Include/cmsis_compiler.h:
../Drivers/CMSIS/Include/cmsis_gcc.h:
../Drivers/CMSIS/Include/mpu_armv7.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h:
../Core/Inc/usart.h:
../Core/Inc/main.h:
../Core/Inc/gpio.h:
../Core/Inc/nb.h:

@ -0,0 +1,183 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 10240
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void nb_reopen() {
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, 1); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", DefaultTimeout, 1); // 重启然后自动注册
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("===NB-->MCU(raw):%s===\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)",01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)",03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
// nb_iotSendCmd((uint8_t *) "AT+NMGS=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
// strcpy(post, "AT+NMGS==5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
if (isPrintf) {
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
} else {
while(1) {
// printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
// printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
NB_REC();
// printf("Success!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
NB_REC();
// printf("Fail!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}
}

@ -0,0 +1,151 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,206 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1 \r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
void extractLastFour(char* data, int len, char* result) {
if (len >= 4) {
strncpy(result, &data[len-4], 4); // 从原始数组的倒数第四个元素开始复制
result[4] = '\0'; // 添加空字符
} else {
// 如果原始数组长度小于4直接复制全部元素
strncpy(result, data, len);
result[len] = '\0'; // 添加空字符
}
}
void NB_REC(void){
// char*pos1 = strstr((char *) LPUART1_RX_BUF, (const char *) "MOTOR");
// char*pos2 = strstr((char *) LPUART1_RX_BUF, (const char *) "LED");
printf("NB-->MCU(raw):%s\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"01") { // 开头01控制LED
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"03") { // 开头03控制电机
}
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// /*电机控制指令*/
// if(pos1)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"MOTOR_SW\":0"))
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
//
// }
// else
// {
// HAL_GPIO_WritePin(MOTOR_SW_GPIO_Port, MOTOR_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"MOTOR_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// /*处理电机控制指令*/
// /*LED控制指令*/
// if(pos2)
// {
// printf("%s\r\n",LPUART1_RX_BUF);
// if(strstr((const char *)LPUART1_RX_BUF,(const char *)"\"LED_SW\":1"))
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_SET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":1}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// else
// {
// HAL_GPIO_WritePin(LED_SW_GPIO_Port, LED_SW_Pin, GPIO_PIN_RESET);
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// char reply[36] = "{\"LED_SW\":0}";
// NB_PUB((uint8_t *)"v1/devices/me/telemetry",(uint8_t*)reply); //更新状态
// }
// }
// else{
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
// }
// /*处理LED控制指令*/
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,183 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 10240
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NNMI=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置新消息指示
printf("Attach!\r\n");
}
}
void nb_reopen() {
// nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, 1); // 开始注册 IoT 平台
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", DefaultTimeout, 1);
// 重启通信模组然后它会自动注册网络
}
void NB_Init() {
HAL_UART_Receive_IT(&hlpuart1, (uint8_t *) aRxBufferLPUart1, 1); //使能NB模组的串口接收
nb_iotAttachLwM2M(1, 1); //NB模组入网
}
int check01(uint8_t *str, int status) {
int len = strlen(str);
printf("%d\r\n", len);
printf("%s=====\r\n", str);
if (status == 0) {
if (str[len-6] != '4') return 0;
if (str[len-5] != 'F') return 0;
if (str[len-4] != '4') return 0;
if (str[len-3] != '6') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != '6') return 0;
}
else if (status == 1) {
if (str[len-4] != '4') return 0;
if (str[len-3] != 'F') return 0;
if (str[len-2] != '4') return 0;
if (str[len-1] != 'E') return 0;
}
return 1;
}
void NB_REC(void){
printf("===NB-->MCU(raw):%s===\r\n",LPUART1_RX_BUF);
if (strstr((const char *)LPUART1_RX_BUF,(const char *)",01")) { // 开头01控制LED
printf("LED\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("LED OFF\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("LED ON\r\n");
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
}
}
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)",03")) { // 开头03控制电机
printf("Motor\r\n");
// if (check01(LPUART1_RX_BUF, 0)) { // 判断OFF
if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4646")) {
printf("Motor OFF\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
}
// else if (check01(LPUART1_RX_BUF, 1)) { // 判断ON
else if (strstr((const char *)LPUART1_RX_BUF,(const char *)"4F4E")) {
printf("Motor ON\r\n");
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
}
}
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));//清除缓存
}
//void nb_iotLwM2M_send() {
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,313233\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,AA34BB\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,445566\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
//// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=3,BBCCDD\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0001020003\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0002030004\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0003040005\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
// nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=5,0004050006\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// HAL_Delay(300);
//}
void nb_heartbeat() {
nb_iotSendCmd((uint8_t *) "AT+QLWULDATA=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
// nb_iotSendCmd((uint8_t *) "AT+NMGS=1,10\r\n", (uint8_t *) "OK", DefaultTimeout, 0); // 0: 不打印
}
void nb_iotLwM2M_send(uint8_t* send)
{
char post[100];
memset(post, '\0', sizeof(post));
strcpy(post, "AT+QLWULDATA=5,");
// strcpy(post, "AT+NMGS==5,");
strcat(post, send);
nb_iotSendCmd((uint8_t *) post, (uint8_t *) "OK", DefaultTimeout, isPrintf);
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
if (isPrintf) {
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
NB_REC();
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
} else {
while(1) {
// printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
// printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
NB_REC();
// printf("Success!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
NB_REC();
// printf("Fail!\r\n");
// LPUART1_RX_LEN=0;
// memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}
}

@ -0,0 +1,237 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//char datah[100],datat[100];
//float Humidity,Temperatrue;//湿度温度数据存储
//char deg[] = {0xa1,0xe3};
//char back[]="Success!\n\r\n"; //返回的数<E79A84>???
//char send[50];
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// Init_SHT30();//初始化传感器
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char datah[100],datat[100];
float Humidity = 1,Temperatrue = 1;//湿度温度数据存储
char deg[] = {0xa1,0xe3};
char back[]="Success!\n\r\n"; //返回的数<E79A84>???
char send[50];
// nb_iotAttachLwM2M(1, 1);
while (1)
{
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%.2f , tem:%.2f ,lux:%.2f\r\n",hum,tem,lux);
// Read_Data(&Temperatrue, &Humidity);
// sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
// sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
// printf("%s\r\n", datah);
// printf("%s\r\n", datat);
// sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
// printf("%s\r\n", send);
// HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
// HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
// nb_iotLwM2M_send(send);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,94 @@
################################################################################
# Automatically-generated file. Do not edit!
################################################################################
-include ../makefile.init
RM := rm -rf
# All of the sources participating in the build are defined here
-include sources.mk
-include Drivers/STM32L4xx_HAL_Driver/Src/subdir.mk
-include Core/Startup/subdir.mk
-include Core/Src/subdir.mk
-include subdir.mk
-include objects.mk
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(S_DEPS)),)
-include $(S_DEPS)
endif
ifneq ($(strip $(S_UPPER_DEPS)),)
-include $(S_UPPER_DEPS)
endif
ifneq ($(strip $(C_DEPS)),)
-include $(C_DEPS)
endif
endif
-include ../makefile.defs
BUILD_ARTIFACT_NAME := STM32_NB-IoT
BUILD_ARTIFACT_EXTENSION := elf
BUILD_ARTIFACT_PREFIX :=
BUILD_ARTIFACT := $(BUILD_ARTIFACT_PREFIX)$(BUILD_ARTIFACT_NAME).$(BUILD_ARTIFACT_EXTENSION)
# Add inputs and outputs from these tool invocations to the build variables
EXECUTABLES += \
STM32_NB-IoT.elf \
SIZE_OUTPUT += \
default.size.stdout \
OBJDUMP_LIST += \
STM32_NB-IoT.list \
OBJCOPY_BIN += \
STM32_NB-IoT.bin \
# All Target
all: main-build
# Main-build Target
main-build: STM32_NB-IoT.elf secondary-outputs
# Tool invocations
STM32_NB-IoT.elf: $(OBJS) $(USER_OBJS) D:\WS\STM32_NB-IoT\STM32L431RCTX_FLASH.ld
arm-none-eabi-gcc -o "STM32_NB-IoT.elf" @"objects.list" $(USER_OBJS) $(LIBS) -mcpu=cortex-m4 -T"D:\WS\STM32_NB-IoT\STM32L431RCTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="STM32_NB-IoT.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wl,--start-group -lc -lm -Wl,--end-group
@echo 'Finished building target: $@'
@echo ' '
default.size.stdout: $(EXECUTABLES)
arm-none-eabi-size $(EXECUTABLES)
@echo 'Finished building: $@'
@echo ' '
STM32_NB-IoT.list: $(EXECUTABLES)
arm-none-eabi-objdump -h -S $(EXECUTABLES) > "STM32_NB-IoT.list"
@echo 'Finished building: $@'
@echo ' '
STM32_NB-IoT.bin: $(EXECUTABLES)
arm-none-eabi-objcopy -O binary $(EXECUTABLES) "STM32_NB-IoT.bin"
@echo 'Finished building: $@'
@echo ' '
# Other Targets
clean:
-$(RM) *
-@echo ' '
secondary-outputs: $(SIZE_OUTPUT) $(OBJDUMP_LIST) $(OBJCOPY_BIN)
fail-specified-linker-script-missing:
@echo 'Error: Cannot find the specified linker script. Check the linker settings in the build configuration.'
@exit 2
warn-no-linker-script-specified:
@echo 'Warning: No linker script specified. Check the linker settings in the build configuration.'
.PHONY: all clean dependents fail-specified-linker-script-missing warn-no-linker-script-specified
.SECONDARY:
-include ../makefile.targets

@ -0,0 +1,217 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>?)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char datah[100],datat[100];
float Humidity = 0,Temperatrue = 0;//湿度温度数据存储
char deg[] = {0xa1,0xe3};
char back[]="Success!\n\r\n"; //返回的数<E79A84>?
char send[50];
// nb_iotAttachLwM2M(1, 1);
while (1)
{
Read_Data(&Temperatrue, &Humidity);
sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
printf("%s", datah);
printf("%s", datat);
sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
printf("%s", datat);
// HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
// HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
// nb_iotLwM2M_send(send);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV4;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,145 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
// nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"8596635139.iotda-device.cn-south-4.myhuaweicloud.com\",1883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
void nb_iotAttachLwM2M(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CMEE=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 错误号返回
nb_iotSendCmd((uint8_t *) "ATE1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 命令回显
nb_iotSendCmd((uint8_t *) "ATI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看版本号
nb_iotSendCmd((uint8_t *) "AT+CFUN?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查看UE功能等级显示CUFN1代表全部功能打开了。
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测SIM卡是否有读取成功返回SIM号则成功
nb_iotSendCmd((uint8_t *) "AT+CEREG?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络注册状态(=0 (第一个数)表示已禁用网络注册 URC=1第二个数 表示已注册)
nb_iotSendCmd((uint8_t *) "AT+CGATT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询网络是否被激活返回0为入网失败返回1为入网成功
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 检测信号强度。不为99则表示有信号即第一个数
nb_iotSendCmd((uint8_t *) "AT+CGPADDR\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询模块IP地址
nb_iotSendCmd((uint8_t *) "AT+NMSTATUS?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 消息注册状态
nb_iotSendCmd((uint8_t *) "AT+NCDP=123.60.224.61,5683\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 设置 IoT 平台 IP 地址及端口
nb_iotSendCmd((uint8_t *) "AT+QREGSWT?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 查询自动注册模式1自动注册模式
nb_iotSendCmd((uint8_t *) "AT+QLWSREGIND=0\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf); // 开始注册 IoT 平台
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

@ -0,0 +1,18 @@
syscalls.c:44:6:initialise_monitor_handles 4 static
syscalls.c:48:5:_getpid 4 static
syscalls.c:53:5:_kill 16 static
syscalls.c:61:6:_exit 16 static
syscalls.c:67:27:_read 32 static
syscalls.c:80:27:_write 32 static
syscalls.c:92:5:_close 16 static
syscalls.c:99:5:_fstat 16 static
syscalls.c:106:5:_isatty 16 static
syscalls.c:112:5:_lseek 24 static
syscalls.c:120:5:_open 12 static
syscalls.c:128:5:_wait 16 static
syscalls.c:135:5:_unlink 16 static
syscalls.c:142:5:_times 16 static
syscalls.c:148:5:_stat 16 static
syscalls.c:155:5:_link 16 static
syscalls.c:163:5:_fork 8 static
syscalls.c:169:5:_execve 24 static

@ -0,0 +1,80 @@
Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.o: \
../Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_rcc_ex.c \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h \
../Core/Inc/stm32l4xx_hal_conf.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h \
../Drivers/CMSIS/Include/core_cm4.h \
../Drivers/CMSIS/Include/cmsis_version.h \
../Drivers/CMSIS/Include/cmsis_compiler.h \
../Drivers/CMSIS/Include/cmsis_gcc.h \
../Drivers/CMSIS/Include/mpu_armv7.h \
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h \
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h:
../Core/Inc/stm32l4xx_hal_conf.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_def.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l4xx.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/stm32l431xx.h:
../Drivers/CMSIS/Include/core_cm4.h:
../Drivers/CMSIS/Include/cmsis_version.h:
../Drivers/CMSIS/Include/cmsis_compiler.h:
../Drivers/CMSIS/Include/cmsis_gcc.h:
../Drivers/CMSIS/Include/mpu_armv7.h:
../Drivers/CMSIS/Device/ST/STM32L4xx/Include/system_stm32l4xx.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_rcc_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_gpio_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_dma.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_cortex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_exti.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_flash_ramfunc.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_pwr_ex.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart.h:
../Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal_uart_ex.h:

@ -0,0 +1,243 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//char datah[100],datat[100];
//float Humidity,Temperatrue;//湿度温度数据存储
//char deg[] = {0xa1,0xe3};
//char back[]="Success!\n\r\n"; //返回的数<E79A84>???
//char send[50];
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
// Init_SHT30();//初始化传感器
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// nb_iotAttachtcp(isPrintf,isReboot);
// nb_iotAttachudp(isPrintf,isReboot);
// nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot);
// nb_iotRecMsgFromServer();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char datah[100],datat[100];
float Humidity = 1,Temperatrue = 1;//湿度温度数据存储
char deg[] = {0xa1,0xe3};
char back[]="Success!\n\r\n"; //返回的数<E79A84>???
char send[50];
// nb_iotAttachLwM2M(1, 1);
NB_Init(); //初始化NB模组
while (1)
{
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
HAL_Delay(2000);
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// Read_Data(&Temperatrue, &Humidity);
// sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
// sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
// printf("%s\r\n", datah);
// printf("%s\r\n", datat);
// sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
// printf("%s\r\n", send);
// HAL_UART_Transmit(&huart1, (uint8_t*)datah, strlen(datah), 100);
// HAL_UART_Transmit(&huart1, (uint8_t*)datat, strlen(datat), 100);
// nb_iotLwM2M_send(send);
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,230 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_Delay(5000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// nb_heartbeat();
tot++;
if (tot == 3) {
tot = 0;
nb_reopen();
HAL_Delay(5000);
}
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
// }
NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(1000);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,12 @@
stm32l4xx_it.c:69:6:NMI_Handler 4 static
stm32l4xx_it.c:84:6:HardFault_Handler 4 static
stm32l4xx_it.c:99:6:MemManage_Handler 4 static
stm32l4xx_it.c:114:6:BusFault_Handler 4 static
stm32l4xx_it.c:129:6:UsageFault_Handler 4 static
stm32l4xx_it.c:144:6:SVC_Handler 4 static
stm32l4xx_it.c:157:6:DebugMon_Handler 4 static
stm32l4xx_it.c:170:6:PendSV_Handler 4 static
stm32l4xx_it.c:183:6:SysTick_Handler 8 static
stm32l4xx_it.c:204:6:EXTI2_IRQHandler 8 static
stm32l4xx_it.c:218:6:EXTI3_IRQHandler 8 static
stm32l4xx_it.c:232:6:LPUART1_IRQHandler 8 static

@ -0,0 +1,222 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 0;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
while (1)
{
// E53_IA1_Read_Data();
// float hum = E53_IA1_Data.Humidity;
// float tem = E53_IA1_Data.Temperature;
// float lux = E53_IA1_Data.Lux;
// printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
// HAL_Delay(2000);
// HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
// HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
// Read_Data(&Temperatrue, &Humidity);
// sprintf(datah,"Humidity is %d.\n\r\n",(int)Humidity);
// sprintf(datat,"Temperatrue is %d%s.\n\n\r\n\r\n",(int)Temperatrue,deg);
// printf("%s\r\n", datah);
// printf("%s\r\n", datat);
sprintf(send, "%02x%02x%02x0000\r\n", 0, (int)Temperatrue, (int)Humidity);
printf("%s\r\n", send);
nb_iotLwM2M_send(send);
// NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,219 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "E53_IA1.h"
#include "nb.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t isReboot = 1; //是否重新入网(1:<3A>???)
uint8_t isPrintf = 1;
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_LPUART1_UART_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
Init_E53_IA1(); //初始化SHT30和BH1750传感器
//初始化关闭电机与LED
HAL_GPIO_WritePin(Motor_GPIO_Port, Motor_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
NB_Init(); //初始化NB模组
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
char send[50];
int tot = 0;
while (1)
{
E53_IA1_Read_Data();
float hum = E53_IA1_Data.Humidity;
float tem = E53_IA1_Data.Temperature;
float lux = E53_IA1_Data.Lux;
printf("hum:%d , tem:%d ,lux:%d\r\n",(int)hum,(int)tem,(int)lux);
sprintf(send, "%02x%02x%02x%04x\r\n", 0, (int)tem, (int)hum, (int)lux);
printf("%s\r\n", send);
tot++;
if (tot == 10) {
tot = 0;
nb_iotLwM2M_send(send);
}
// NB_REC(); //接收数据并检查是否接收了指令
HAL_Delay(500);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 20;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

@ -0,0 +1,125 @@
#include "nb.h"
#include "stdint.h"
#include "string.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "stdio.h"
#include "stm32l4xx_hal.h"
extern UART_HandleTypeDef hlpuart1;
extern uint8_t isPrintf;
extern uint32_t DefaultTimeout;
#define LPUART1_REC_LEN 1024
uint8_t bRxBufferUart1[1];
uint8_t LPUART1_RX_BUF[LPUART1_REC_LEN];//建立缓冲区
volatile uint16_t LPUART1_RX_LEN=0;
#define CMD_LEN 100
char cmdSend[CMD_LEN];
uint32_t DefaultTimeout=1000;
void nb_iotAttachudp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=DGRAM,17,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOST=0,**.**.**.***,18088,6,7A7864756470\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachtcp(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT+NRB\r\n", (uint8_t *) "OK", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CGATT=1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NCCID\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:",DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CEREG=1\r\n", (uint8_t *) "OK", DefaultTimeout,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCR=STREAM,6,1008,1\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOCO=0,**.**.**.***,18088\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+NSOSD=0,6,7A7864746370\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
printf("Attach!\r\n");
}
}
void nb_iotAttachmqtt(uint8_t isPrintf,uint8_t isReboot) {
if (isReboot== 1) {
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CIMI=?\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT\r\n", (uint8_t *) "OK", DefaultTimeout, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+CSQ\r\n", (uint8_t *) "+CSQ:", 1000,isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTOPEN=0,\"**.**.**.***\",11883\r\n", (uint8_t *) "QMTOPEN: 0,0\r\n", 5000, isPrintf);
nb_iotSendCmd((uint8_t *) "AT+QMTCONN=0,\"client_id\",\"username\",\"password\"\r\n", (uint8_t *) "QMTCONN: 0,0,0\r\n", 5000, isPrintf);//鉴权连接
printf("Attach!\r\n");
}
}
//mqtt发布主题
void nb_iotMQTTSub(uint8_t *topic) {
memset(cmdSend, 0, sizeof(cmdSend));//拼接字符串
strcat(cmdSend, "AT+QMTSUB=0,1,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",0\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *) "+QMTSUB: 0,1,0,0", DefaultTimeout, isPrintf);
}
//mqtt订阅主题
void nb_iotMQTTPub(uint8_t *topic, uint8_t *length) {
memset(cmdSend, 0, sizeof(cmdSend));
strcat(cmdSend, "AT+QMTPUB=0,");
strcat(cmdSend, "0,0,0,");
strcat(cmdSend, (const char *) topic);
strcat(cmdSend, ",");
strcat(cmdSend, (const char *) length);
strcat(cmdSend, "\r\n");
nb_iotSendCmd((uint8_t *) cmdSend, (uint8_t *)">", 1000, isPrintf);
}
//命令函数
void nb_iotRecMsgFromServer(){
char *pos = NULL;
pos = strstr((const char *) LPUART1_RX_BUF, (const char *) "request");
HAL_Delay(200);
if(pos)
{
printf("%s\r\n",LPUART1_RX_BUF);
if(strstr(LPUART1_RX_BUF, "st\":\"1\"") )flag = 1;
else if(strstr(LPUART1_RX_BUF, "st\":\"2\""))flag=2;
else if(strstr(LPUART1_RX_BUF, "st\":\"3\""))flag = 3;
else if(strstr(LPUART1_RX_BUF, "st\":\"0\""))flag = 0;
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
}
}
//指令发送函数
void nb_iotSendCmd(uint8_t *cmd, uint8_t *result, uint32_t timeOut, uint8_t isPrintf) {
char *pos;
HAL_UART_Transmit( &hlpuart1, cmd,strlen((const char *)cmd), 0xff);
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);//中断接收具体在ustar.c文件中
HAL_Delay(timeOut);
while(1) {
printf("%s\r\n",cmd);
pos= strstr((const char *) LPUART1_RX_BUF, (const char *) result);//对比接收到的指令是否与期望的一致
printf("receive: %s\r\n", LPUART1_RX_BUF);
if (pos) {
printf("Success!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
break;
}
else{
printf("Fail!\r\n");
LPUART1_RX_LEN=0;
memset(LPUART1_RX_BUF, 0, strlen((const char *)LPUART1_RX_BUF));
HAL_UART_Transmit(&hlpuart1, cmd, strlen((const char *) cmd), 0xff);//失败则不断重复发送
HAL_UART_Receive_IT(&hlpuart1,bRxBufferUart1,1);
HAL_Delay(timeOut);
}
}
}

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save