系统功能:
此设计采用STM32单片机将采集到的环境温度,TDS值,PH值等显示在OLED上,并将这些信息上报至阿里云平台。系统可通过阿里云平台或按键开关加热继电器,增氧继电器,水泵继电器和舵机.
主要功能模块原理图:
电源时钟烧录接口:
单片机和按键输入电路:
继电器控制电路:
资料获取地址
系统主要功能模块代码
初始化代码:
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
InitVar();
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
//uart2打开接收中断
HAL_UART_Receive_IT(&huart2, (uint8_t*)&uart2RecBuff, 1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
InitOled();
InitOledDisp();
//初始化ESP8266
ESP8266_Init();
HAL_Delay(100);
OledFill(0x00); //全屏熄灭
OledDispPage1();
if(InitDS18B20()==0)
{
// printf("%s", "DS18B20初始化成功");
//DS18B20在上电第一次读取时显示温度为85℃,这是正常的。
//85℃是DS18B20在上电复位后在温度寄存器内的对应数字量,
//这里DS18B20初始化成功后读一次值,避免出现85℃
DS18B20GetTemp_SkipRom();
}
ESP8266接入阿里云代码:
//硬件复位ESP8266
HAL_GPIO_WritePin(ESP8266RST_GPIO_Port, ESP8266RST_Pin, GPIO_PIN_RESET);
HAL_Delay(250);
HAL_GPIO_WritePin(ESP8266RST_GPIO_Port, ESP8266RST_Pin, GPIO_PIN_SET);
HAL_Delay(500);
//关闭板载LED
HAL_GPIO_WritePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin, GPIO_PIN_SET);
ESP8266_Clear();
//测试AT指令
printf("AT Testing\r\n");
while(ESP8266_SendCmd("AT\r\n", "OK", 200))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//设置为Station
printf("set CWMODE\r\n");
while(ESP8266_SendCmd("AT+CWMODE=1\r\n", "OK", 200))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//上电不自动连接AP
printf("set CWAUTOCONN\r\n");
while(ESP8266_SendCmd("AT+CWAUTOCONN=0\r\n", "OK", 200))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//关闭回显
printf("set ATE0\r\n");
while(ESP8266_SendCmd("ATE0\r\n", "OK", 200))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//配置WIFI
printf("set Wifi\r\n");
while(ESP8266_SendCmd("AT+CWJAP=\""WIFI_SSID"\",\""WIFI_PASSWD"\"\r\n", "OK", 500))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
//MQTT USER CONFIG
printf("mqtt config\r\n");
while(ESP8266_SendCmd("AT+MQTTUSERCFG=0,1,\""MQTT_CLIENT_ID"\",\""MQTT_USER_NAME"\",\""MQTT_PASSWD"\",0,0,\"\"\r\n", "OK", 500))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(1000);
//MQTT连接服务器
printf("mqtt broker\r\n");
while(ESP8266_SendCmd("AT+MQTTCONN=0,\""BROKER_ASDDRESS"\",1883,0\r\n", "OK", 500))
{
HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnBoardLED_GPIO_Port, OnBoardLED_Pin);
HAL_Delay(500);
系统控制代码:
//系统信息更新
void SystemControl(void)
{
static uint8_t updateCnt = 0;
updateCnt++;
if(updateCnt>=SYSTEM_UPDATE_INTER)
{
updateCnt = 0;
Aliyun_SendData();
}
//读取传感器值
ReadSensorVal();
//更新DS18B20温度值
UpdateTempData();
//喂食控制
FeedControl();
}
此设计提供的资料包含原理图PCB(包含PDF,AD,立创EDA三个版本),源代码,系统框图,主程序流程图,物料清单(包含使用到的器件手册),功能操作说明等。