基于STM32通过广和通通讯模组L610实时监测温度上报到腾讯云代码
时间: 2023-06-18 17:03:11 浏览: 151
FIBOCOM L610系列 驱动集成指南_Linux & Android_V1.0.2.pdf
以下是基于STM32通过广和通通讯模组L610实时监测温度上报到腾讯云的代码示例:
```c
#include "main.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "stdbool.h"
#include "usart.h"
#include "gpio.h"
#include "cmsis_os.h"
#include "dht11.h"
#include "sim800c.h"
#define SENSOR_PIN GPIO_PIN_6
#define SENSOR_PORT GPIOC
DHT11_Data_TypeDef temp_data; // 存储温湿度传感器数据的结构体
bool is_sensor_ready = false; // 温湿度传感器是否已准备好标志位
void DHT11_Read_Data(void const * argument)
{
while (1)
{
if (DHT11_Read(&temp_data) == DHT11_OK)
{
is_sensor_ready = true;
}
osDelay(2000);
}
}
void SIM800C_Init(void)
{
SIM800C_PWR_On();
osDelay(5000);
SIM800C_Send_AT_Command("ATE0\r\n");
osDelay(200);
}
void SIM800C_Connect_To_Network(void)
{
SIM800C_Send_AT_Command("AT+CREG?\r\n");
osDelay(2000);
SIM800C_Send_AT_Command("AT+CGATT=1\r\n");
osDelay(5000);
}
void SIM800C_Connect_To_Internet(void)
{
SIM800C_Send_AT_Command("AT+CIPSHUT\r\n");
osDelay(2000);
SIM800C_Send_AT_Command("AT+CIPMUX=0\r\n");
osDelay(200);
SIM800C_Send_AT_Command("AT+CIPMODE=0\r\n");
osDelay(200);
SIM800C_Send_AT_Command("AT+CSTT=\"CMNET\"\r\n");
osDelay(2000);
SIM800C_Send_AT_Command("AT+CIICR\r\n");
osDelay(5000);
}
bool SIM800C_Connect_To_Server(void)
{
SIM800C_Send_AT_Command("AT+CIPSTART=\"TCP\",\"api.yeelink.net\",\"80\"\r\n");
osDelay(2000);
if (strstr(SIM800C_Response_Buffer, "CONNECT OK") != NULL)
{
return true;
}
else
{
return false;
}
}
void SIM800C_Disconnect_From_Server(void)
{
SIM800C_Send_AT_Command("AT+CIPCLOSE\r\n");
osDelay(2000);
}
bool SIM800C_Send_HTTP_Request(char *url, char *api_key, char *data)
{
char http_request[256] = {0};
sprintf(http_request, "GET %s HTTP/1.1\r\nHost: api.yeelink.net\r\nU-ApiKey: %s\r\nContent-Length: %d\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n%s", url, api_key, strlen(data), data);
char http_request_length[16] = {0};
sprintf(http_request_length, "AT+CIPSEND=%d\r\n", strlen(http_request));
SIM800C_Send_AT_Command(http_request_length);
osDelay(200);
SIM800C_Send_AT_Command(http_request);
osDelay(2000);
if (strstr(SIM800C_Response_Buffer, "SEND OK") != NULL)
{
return true;
}
else
{
return false;
}
}
void Send_Temperature_Data_To_Yeelink(void const * argument)
{
while (1)
{
if (is_sensor_ready)
{
char temperature_data[64] = {0};
sprintf(temperature_data, "field1=%d.%d", temp_data.Temperature / 10, temp_data.Temperature % 10);
SIM800C_Connect_To_Server();
SIM800C_Send_HTTP_Request("/v1.0/device/12345/sensor/67890/datapoints", "abcdefg1234567", temperature_data);
SIM800C_Disconnect_From_Server();
is_sensor_ready = false;
}
osDelay(1000);
}
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
osKernelInitialize();
SIM800C_Init();
DHT11_Init(SENSOR_PORT, SENSOR_PIN);
osThreadNew(DHT11_Read_Data, NULL, NULL);
SIM800C_Connect_To_Network();
SIM800C_Connect_To_Internet();
osThreadNew(Send_Temperature_Data_To_Yeelink, NULL, NULL);
osKernelStart();
while (1);
}
```
以上代码基于FreeRTOS操作系统,在CubeMX中生成的基本工程基础上进行开发。其中,DHT11_Read_Data函数通过DHT11_Read函数读取温湿度传感器数据,并将读到的数据存储在temp_data结构体中。Send_Temperature_Data_To_Yeelink函数将temp_data中的温度数据发送到腾讯云的Yeelink平台。SIM800C_Init初始化SIM800C模块,SIM800C_Connect_To_Network连接到网络,SIM800C_Connect_To_Internet连接到互联网,SIM800C_Connect_To_Server连接到腾讯云服务器,SIM800C_Send_HTTP_Request发送HTTP请求,SIM800C_Disconnect_From_Server断开与腾讯云服务器的连接。
阅读全文