基于STM32HAL库读取出的温度通过广和通通讯模组L610运用AT指令上传腾讯云的代码
时间: 2023-06-18 15:03:25 浏览: 84
以下是基于STM32HAL库读取出的温度通过广和通通讯模组L610运用AT指令上传腾讯云的代码:
```c
#include "main.h"
#include "stdio.h"
#include "string.h"
/* USART handler declaration */
UART_HandleTypeDef huart1;
/* Temperature sensor value */
float temp;
/* L610 AT commands */
char AT[] = "AT\r\n";
char CIPSHUT[] = "AT+CIPSHUT\r\n";
char CGATT[] = "AT+CGATT=1\r\n";
char CSTT[] = "AT+CSTT=\"CMNET\"\r\n";
char CIICR[] = "AT+CIICR\r\n";
char CIPSTART[] = "AT+CIPSTART=\"TCP\",\"your_server_address\",your_server_port\r\n";
char CIPSEND[] = "AT+CIPSEND\r\n";
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
/* MCU Configuration */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
/* L610 communication variables */
uint8_t rx_buffer[128];
uint16_t rx_index = 0;
HAL_StatusTypeDef status;
/* Temperature sensor configuration */
ADC_HandleTypeDef hadc1;
uint32_t adc_value;
float voltage;
/* Configure the ADC peripheral */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.NbrOfDiscConversion = 0;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
HAL_ADC_Init(&hadc1);
/* Configure the channel of the ADC peripheral */
ADC_ChannelConfTypeDef sConfig;
sConfig.Channel = ADC_CHANNEL_TEMPSENSOR;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
/* L610 reset */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
HAL_Delay(100);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);
HAL_Delay(100);
/* L610 AT commands sequence */
status = HAL_UART_Transmit(&huart1, (uint8_t *)AT, strlen(AT), 5000);
status = HAL_UART_Transmit(&huart1, (uint8_t *)CIPSHUT, strlen(CIPSHUT), 5000);
status = HAL_UART_Transmit(&huart1, (uint8_t *)CGATT, strlen(CGATT), 5000);
status = HAL_UART_Transmit(&huart1, (uint8_t *)CSTT, strlen(CSTT), 5000);
status = HAL_UART_Transmit(&huart1, (uint8_t *)CIICR, strlen(CIICR), 5000);
status = HAL_UART_Transmit(&huart1, (uint8_t *)CIPSTART, strlen(CIPSTART), 5000);
while (1)
{
/* Read the temperature sensor value */
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100);
adc_value = HAL_ADC_GetValue(&hadc1);
voltage = adc_value * 3.3 / 4095;
temp = (voltage - 0.76) / 0.0025 + 25;
/* Convert temperature to string */
char temp_str[10];
sprintf(temp_str, "%.2f", temp);
/* L610 AT commands sequence */
status = HAL_UART_Transmit(&huart1, (uint8_t *)CIPSEND, strlen(CIPSEND), 5000);
/* Wait for ">" character */
do {
status = HAL_UART_Receive(&huart1, (uint8_t *)&rx_buffer[rx_index], 1, 5000);
if (rx_buffer[rx_index] == '>') break;
rx_index++;
} while (rx_index < 128);
/* Send temperature value to the server */
char data[64];
sprintf(data, "GET /upload?temp=%s HTTP/1.1\r\nHost: your_server_address\r\n\r\n", temp_str);
status = HAL_UART_Transmit(&huart1, (uint8_t *)data, strlen(data), 5000);
HAL_Delay(10000);
}
}
/* System Clock Configuration */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__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 = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
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_0) != HAL_OK)
{
Error_Handler();
}
}
/* USART1 init function */
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
/* GPIO init function */
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : PA1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* Error handler */
void Error_Handler(void)
{
while(1);
}
```
请注意替换代码中的“your_server_address”和“your_server_port”为您自己的服务器地址和端口。此代码使用AT指令将温度上传到腾讯云,但您可以根据需要进行修改和调整。
阅读全文