基于STM32HAL库读取出的温度通过广和通通讯模组L610运用串口打印AT指令上传腾讯云的代码
时间: 2023-06-18 18:03:19 浏览: 131
以下是基于STM32HAL库读取出的温度通过广和通通讯模组L610运用串口打印AT指令上传腾讯云的代码:
```c
#include "stm32xxxx.h" // 根据你所使用的STM32型号进行修改
// 定义串口配置参数
#define BAUDRATE 9600
#define UART_TX GPIO_PIN_9
#define UART_RX GPIO_PIN_10
#define UART_PORT GPIOA
#define UART USART1
// 定义广和通模组相关的配置参数
#define GPRS_TX GPIO_PIN_2
#define GPRS_RX GPIO_PIN_3
#define GPRS_PORT GPIOA
#define GPRS_USART USART2
// 定义腾讯云相关的配置参数
#define PRODUCT_ID "YOUR_PRODUCT_ID"
#define DEVICE_NAME "YOUR_DEVICE_NAME"
#define DEVICE_KEY "YOUR_DEVICE_KEY"
#define TOPIC "/YOUR_PRODUCT_ID/YOUR_DEVICE_NAME/data"
// 定义温度传感器相关的配置参数
#define TEMP_SENSOR_ADC ADC1
#define TEMP_SENSOR_CHANNEL ADC_CHANNEL_16
// 定义缓存区大小
#define BUFFER_SIZE 1024
// 定义全局变量
char buffer[BUFFER_SIZE];
// 初始化函数
void init(void)
{
// 初始化时钟
RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // 使能USART1时钟
RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // 使能USART2时钟
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // 使能GPIOA时钟
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN; // 使能ADC1时钟
// 配置GPIOA
GPIO_InitTypeDef gpio_init_struct = {0};
gpio_init_struct.Mode = GPIO_MODE_AF_PP;
gpio_init_struct.Pull = GPIO_NOPULL;
gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;
gpio_init_struct.Alternate = GPIO_AF7_USART1;
gpio_init_struct.Pin = UART_TX;
HAL_GPIO_Init(UART_PORT, &gpio_init_struct);
gpio_init_struct.Pin = UART_RX;
HAL_GPIO_Init(UART_PORT, &gpio_init_struct);
gpio_init_struct.Alternate = GPIO_AF7_USART2;
gpio_init_struct.Pin = GPRS_TX;
HAL_GPIO_Init(GPRS_PORT, &gpio_init_struct);
gpio_init_struct.Pin = GPRS_RX;
HAL_GPIO_Init(GPRS_PORT, &gpio_init_struct);
// 配置USART1
UART->CR1 = USART_CR1_TE | USART_CR1_RE;
UART->CR2 = 0;
UART->CR3 = 0;
UART->BRR = SystemCoreClock / BAUDRATE;
// 配置USART2
GPRS_USART->CR1 = USART_CR1_TE | USART_CR1_RE;
GPRS_USART->CR2 = 0;
GPRS_USART->CR3 = 0;
GPRS_USART->BRR = SystemCoreClock / BAUDRATE;
// 配置ADC1
ADC_InitTypeDef adc_init_struct = {0};
adc_init_struct.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2;
adc_init_struct.Resolution = ADC_RESOLUTION_12B;
adc_init_struct.DataAlign = ADC_DATAALIGN_RIGHT;
adc_init_struct.ScanConvMode = DISABLE;
adc_init_struct.ContinuousConvMode = DISABLE;
adc_init_struct.DiscontinuousConvMode = DISABLE;
adc_init_struct.NbrOfDiscConversion = 0;
adc_init_struct.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
adc_init_struct.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
HAL_ADC_Init(&adc_init_struct);
// 配置ADC1通道16
ADC_ChannelConfTypeDef adc_channel_struct = {0};
adc_channel_struct.Channel = TEMP_SENSOR_CHANNEL;
adc_channel_struct.Rank = 1;
adc_channel_struct.SamplingTime = ADC_SAMPLETIME_19CYCLES_5;
adc_channel_struct.Offset = 0;
HAL_ADC_ConfigChannel(&TEMP_SENSOR_ADC, &adc_channel_struct);
// 启动ADC1
HAL_ADC_Start(&TEMP_SENSOR_ADC);
}
// 读取温度函数
float read_temperature(void)
{
uint16_t adc_value = 0;
float temperature = 0;
// 读取温度传感器的值
HAL_ADC_Start(&TEMP_SENSOR_ADC);
if (HAL_ADC_PollForConversion(&TEMP_SENSOR_ADC, 1000) == HAL_OK) {
adc_value = HAL_ADC_GetValue(&TEMP_SENSOR_ADC);
}
// 将ADC值转换为温度
temperature = ((float)adc_value / 4095.0f) * 3.3f;
temperature = (temperature - 0.76f) / 0.0025f;
return temperature;
}
// 发送AT指令函数
void send_at_command(char* command)
{
// 发送AT指令
sprintf(buffer, "%s\r\n", command);
HAL_UART_Transmit(&GPRS_USART, (uint8_t*)buffer, strlen(buffer), 1000);
// 等待回复
HAL_UART_Receive(&GPRS_USART, (uint8_t*)buffer, BUFFER_SIZE, 10000);
}
// 上传数据到腾讯云函数
void upload_to_tencent_cloud(float temperature)
{
// 组织JSON数据
sprintf(buffer, "{\"temperature\":%f}", temperature);
// 发送MQTT消息
send_at_command("AT+CMQTTSTART");
send_at_command("AT+CMQTTACCQ:0,\"" PRODUCT_ID "\",\"" DEVICE_NAME "\",\"" DEVICE_KEY "\"");
send_at_command("AT+CMQTTPUB:0,1,0,0,\"" TOPIC "\",\"" buffer "\"");
send_at_command("AT+CMQTTSTOP");
}
int main(void)
{
// 初始化
init();
// 循环读取温度并上传到腾讯云
while (1) {
float temperature = read_temperature();
upload_to_tencent_cloud(temperature);
HAL_Delay(10000);
}
}
```
请注意,以上代码仅提供了一个简单的示例,实际应用时还需要根据具体的需求进行修改和优化。
阅读全文