以STM32F103C8T6为主控制器,DHT11,光敏电阻传感器,MQ2烟雾传感器,ESP8266-01S将单片机收集到的数据打包并通过MQTT协议上传至OneNET云平台。整个系统的实现在keil5中编写程序,请给出主程序
时间: 2024-03-26 16:38:09 浏览: 120
很高兴回答您的问题!以下是一个STM32F103C8T6控制的传感器数据采集系统的主程序,其中包括DHT11,光敏电阻传感器和MQ2烟雾传感器。程序还使用ESP8266-01S将数据通过MQTT协议上传至OneNET云平台。
```c
#include "stm32f10x.h"
#include "dht11.h"
#include "delay.h"
#include "usart.h"
#include "adc.h"
#include "mqtt.h"
#include "esp8266.h"
#include "string.h"
#define DHT11_PIN GPIO_Pin_0
#define DHT11_PORT GPIOA
#define LIGHT_PIN GPIO_Pin_1
#define LIGHT_PORT GPIOA
#define MQ2_PIN GPIO_Pin_2
#define MQ2_PORT GPIOA
uint8_t MAC[6] = {0x18, 0xFE, 0x34, 0x13, 0x57, 0x6B};
uint8_t IP[4] = {192, 168, 0, 110};
uint8_t SubnetMask[4] = {255, 255, 255, 0};
uint8_t Gateway[4] = {192, 168, 0, 1};
uint16_t Port = 1883;
uint8_t ProductID[] = "产品ID";
uint8_t DeviceID[] = "设备ID";
uint8_t ApiKey[] = "API Key";
MQTT_ClientTypeDef mqttClient;
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = DHT11_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DHT11_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LIGHT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(LIGHT_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = MQ2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MQ2_PORT, &GPIO_InitStructure);
}
void UART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void ADC_Config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = LIGHT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);
ADC_Cmd(ADC1, ENABLE);
}
void MQTT_Connect(void)
{
uint8_t mqttStatus = 0;
while (mqttStatus != MQTT_STATUS_CONNECTED)
{
mqttStatus = MQTT_Connect(&mqttClient);
delay_ms(1000);
}
}
void PublishData(float temp, float humi, uint16_t light, uint16_t smoke)
{
uint8_t message[128];
memset(message, 0, sizeof(message));
sprintf((char *)message, "{\"temperature\":%.2f,\"humidity\":%.2f,\"light\":%d,\"smoke\":%d}", temp, humi, light, smoke);
MQTT_Publish(&mqttClient, "/data", (char *)message, strlen((char *)message), 0);
}
int main(void)
{
SystemInit();
SysTick_Config(SystemCoreClock / 1000);
GPIO_Config();
UART_Config();
ADC_Config();
DHT11_Init();
ESP8266_Init();
ESP8266_WIFIMode(ESP8266_WIFI_MODE_STA);
ESP8266_DHCPEnable(ESP8266_DHCP_MODE_ENABLE);
ESP8266_ConnectAP("WiFi SSID", "WiFi Password");
MQTT_Init(&mqttClient, IP, Port, MAC, ProductID, DeviceID, ApiKey);
MQTT_Connect();
while (1)
{
float temp, humi;
uint16_t light, smoke;
DHT11_Read_Data(&temp, &humi);
light = ADC_GetConversionValue(ADC1);
smoke = GPIO_ReadInputDataBit(MQ2_PORT, MQ2_PIN);
PublishData(temp, humi, light, smoke);
delay_ms(5000);
}
}
```
上面的程序包含了DHT11,光敏电阻传感器和MQ2烟雾传感器的数据采集,并使用ESP8266将数据通过MQTT协议上传至OneNET云平台。请注意,您需要根据您的实际情况更改WiFi SSID,WiFi密码,产品ID,设备ID和API Key。
阅读全文