基于STM32HAL库不用ADC转换直接读取出的温度通过广和通通讯模组L610运用串口打印AT指令上传腾讯云的代码
时间: 2023-06-18 12:03:11 浏览: 80
【STM32】HAL库-ADC独立模式-扫描单次转换-DMA-外部触发-规则和注入通道-自动注入demo
以下是基于STM32HAL库,通过L610通讯模组上传腾讯云的代码:
```c
#include "stdio.h"
#include "string.h"
#include "stm32f1xx_hal.h"
#define UART_TX_BUF_SIZE 1024
#define UART_RX_BUF_SIZE 1024
#define WIFI_TX_BUF_SIZE 1024
#define WIFI_RX_BUF_SIZE 1024
UART_HandleTypeDef huart2;
uint8_t uart_tx_buf[UART_TX_BUF_SIZE];
uint8_t uart_rx_buf[UART_RX_BUF_SIZE];
uint8_t wifi_tx_buf[WIFI_TX_BUF_SIZE];
uint8_t wifi_rx_buf[WIFI_RX_BUF_SIZE];
uint8_t temperature[5] = {0}; // 保存温度值
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
void uart_send_string(const char *str)
{
HAL_UART_Transmit(&huart2, (uint8_t*)str, strlen(str), 0xFFFF);
}
void wifi_send_string(const char *str)
{
HAL_UART_Transmit(&huart2, (uint8_t*)str, strlen(str), 0xFFFF);
}
void wifi_send_command(const char *command, const char *response, uint32_t timeout)
{
memset(wifi_rx_buf, 0, WIFI_RX_BUF_SIZE);
wifi_send_string(command);
HAL_Delay(timeout);
HAL_UART_Receive(&huart2, wifi_rx_buf, WIFI_RX_BUF_SIZE, timeout);
if (strstr((const char*)wifi_rx_buf, response) == NULL)
{
// 未收到期望的响应
uart_send_string("ERROR\r\n");
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
while (1)
{
// 读取温度值
uint16_t adc_value = HAL_ADC_GetValue(&hadc1);
float voltage = adc_value * 3.3 / 4096; // 计算电压值
float temperature_value = (voltage - 0.76) / 0.0025 + 25; // 计算温度值
sprintf((char*)temperature, "%.2f", temperature_value); // 转换为字符串
// 连接WiFi网络
wifi_send_command("AT+RST\r\n", "ready", 2000);
wifi_send_command("AT+CWJAP=\"your_ssid\",\"your_password\"\r\n", "OK", 5000);
// 连接腾讯云
wifi_send_command("AT+CIPSTART=\"TCP\",\"your_server_ip\",your_server_port\r\n", "OK", 5000);
wifi_send_command("AT+CIPSEND\r\n", ">", 1000);
sprintf((char*)wifi_tx_buf, "{\r\n\"temperature\": %s\r\n}\r\n", temperature);
wifi_send_string((const char*)wifi_tx_buf);
HAL_Delay(1000);
wifi_send_command("AT+CIPCLOSE\r\n", "OK", 5000);
HAL_Delay(5000);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
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_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
HAL_ADC_Start(&hadc1);
}
```
需要注意的是,此代码中的以下几个参数需要根据实际情况进行修改:
- `your_ssid`: WiFi SSID
- `your_password`: WiFi 密码
- `your_server_ip`: 上传数据服务器 IP 地址
- `your_server_port`: 上传数据服务器端口号
此外,还需要在腾讯云上创建一个设备,并将该设备的证书信息配置到 L610 模组中。
阅读全文