基于STM32HAL库读取出的温度通过广和通通讯模组L610通过mqtt上传腾讯云的代码
时间: 2023-06-18 08:03:21 浏览: 140
基于STM32通过MQTT协议将温湿度数据上传至阿里云
5星 · 资源好评率100%
以下是基于STM32HAL库读取温度并通过广和通通讯模组L610上传到腾讯云的示例代码:
```c
#include "main.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "mqtt_client.h"
#include "mqtt_interface.h"
#include "mqtt_wrapper.h"
#define MQTT_HOST "mqtt://your-mqtt-host"
#define MQTT_PORT 1883
#define MQTT_USER "your-mqtt-username"
#define MQTT_PASSWORD "your-mqtt-password"
#define MQTT_CLIENT_ID "your-mqtt-client-id"
#define TOPIC "your-mqtt-topic"
#define TEMP_SENSOR_PIN GPIO_PIN_0
#define TEMP_SENSOR_PORT GPIOA
ADC_HandleTypeDef hadc1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
float get_temperature(void);
void mqtt_callback(mqtt_client_t *client, mqtt_event_t event, void *user_data);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
mqtt_client_t client = {0};
mqtt_network_t network = {0};
mqtt_init(&client, &network, MQTT_HOST, MQTT_PORT, mqtt_callback, NULL);
mqtt_connect(&client, MQTT_USER, MQTT_PASSWORD, MQTT_CLIENT_ID, 0, 0);
while (1)
{
float temperature = get_temperature();
char payload[50];
snprintf(payload, 50, "%.2f", temperature);
mqtt_publish(&client, TOPIC, QOS0, (uint8_t *)payload, strlen(payload), 0);
HAL_Delay(1000);
}
}
void mqtt_callback(mqtt_client_t *client, mqtt_event_t event, void *user_data)
{
// handle mqtt events
}
float get_temperature(void)
{
uint16_t adc_value;
float temperature;
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK)
{
adc_value = HAL_ADC_GetValue(&hadc1);
}
HAL_ADC_Stop(&hadc1);
temperature = (float)adc_value * 3.3f / 4096.0f;
temperature = (temperature - 0.76f) / 0.0025f;
return temperature;
}
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
__HAL_RCC_ADC1_CLK_ENABLE();
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.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = TEMP_SENSOR_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(TEMP_SENSOR_PORT, &GPIO_InitStruct);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_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();
}
}
```
代码中使用了MQTT协议库,需要下载并添加到项目中。您可以从此处获取MQTT协议库:https://github.com/256dpi/arduino-mqtt
此外,还需要根据您自己的腾讯云账户信息修改代码中的MQTT_HOST,MQTT_USER,MQTT_PASSWORD和TOPIC等参数。
注意:此示例代码仅供参考,实际应用中需要根据具体情况进行修改和优化。
阅读全文