STM32-Based Alibaba Cloud Fish Pond Water Quality Detection System (Undergraduate Graduation Project 24064)

Demo video: STM32-Based Alibaba Cloud Fish Pond Water Quality Detection System (Undergraduate Graduation Project 24064)

System Functions

The core controller is the STM32F103C8T6 microcontroller, which collects water quality data via a pH sensor, turbidity sensor, and temperature sensor. Users can configure threshold values for each parameter; if measured readings exceed the set thresholds, a buzzer will trigger an audible alert. An ESP-12F Wi-Fi module connects the device to the Alibaba Cloud IoT Platform, enabling real-time remote data interaction acros PC and mobile devices.

Deployment & Connection Notes

The system requires connection to Alibaba Cloud. Power on the main unit only after enabling the 2.4G Wi-Fi hotspot on a mobile device. Once connected to Alibaba Cloud, the cloud platform will display the reported temperature, turbidity, and pH data.

On power-up, the OLED display shows "Welcome to Environmental Monitoring System, please wait". After 2 seconds, it displays Connecting... while attempting to establish a connection to Alibaba Cloud. If the connecting screen remains for a extended period, verify the Wi-Fi network configuration. After successful connection, the first display page loads, and the OLED will show sensor data transmitted by the slave device.

OLED Display Pages

Page 1: Main Status Screen

  1. Line 1: System Status Information
  2. Line 2: Current temperature and pH values
  3. Line 3: Water turbidity level
  4. Line 4: System operating status (normal or alarm)

Short-pressing the B4 button switches to the second interface.

Page 2: Temperature Threshold Settings

  1. Line 1: Temperature Threshold Setting
  2. Line 2: Current measured temperature value
  3. Line 3: Configured temperature threshold
  4. Line 4: Temperature status (with in range or exceeded). If the measured temperature exceeds the set threshold, the buzzer activates an alarm.

Short-pressing B4 switches to the third page. Press B5 to increase the temperature threshold, and B6 to decrease it.

Page 3: Turbidity Threshold Settings

  1. Line 1: Turbidity Threshold Setting
  2. Line 2: Current measured turbidity value
  3. Line 3: Configured turbidity threshold. If measured turbidity exceeds the set value, the buzzer triggers an alarm.

Short-pressing B4 switches to the fourth page. Press B5 to increase the turbidity threshold, and B6 to decrease it.

Page 4: pH Threshold Settings

  1. Line 1: pH Threshold Setting
  2. Line 2: Current measured pH value
  3. Line 3: Configured pH threshold
  4. Line 4: pH status (within range or exceeded). If measured pH exceeds the set value, the buzzer activates an alarm.

Short-pressing B4 switches back to the first page. Press B5 to increase the pH threshold, and B6 to decrease it.

Hardware Module Schematics

  • Main functional module schematic diagrams
  • Power, clock, and programming interface
  • Microcontroller and key input circuit
  • Sensor acquisition circuit

Code Examples

Initialization Code

/* 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 */
InitSystemVariables();
/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Configuration();

/* 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();

/* USER CODE BEGIN 2 */
// Enable UART2 receive interrupt
HAL_UART_Receive_IT(&huart2, (uint8_t*)&uart2ReceiveBuffer, 1);

InitOLEDDisplay();
InitialOLEDStartupScreen();

if(CheckDS18B20Initialization() == 0)
{
    // First reading after DS18B20 power-up returns 85°C, which is normal reset state
    // Perform an initial read to avoid displaying 85°C on first load
    GetDS18B20Temperature_SkipRom();
}

// Initialize ESP8266 Wi-Fi module
ESP8266Module_Init();
HAL_Delay(100);
ClearOLEDEntireScreen(0x00);
/* USER CODE END 2 */

ESP8266 Alibaba Cloud Connection Code

// Hardware reset ESP8266 module
HAL_GPIO_WritePin(ESP8266Reset_GPIO_Port, ESP8266Reset_Pin, GPIO_PIN_RESET);
HAL_Delay(250);
HAL_GPIO_WritePin(ESP8266Reset_GPIO_Port, ESP8266Reset_Pin, GPIO_PIN_SET);
HAL_Delay(500);

// Turn off on-board LED
HAL_GPIO_WritePin(OnboardIndicatorLED_GPIO_Port, OnboardIndicatorLED_Pin, GPIO_PIN_SET);
ESP8266_ClearReceiveBuffer();

// Test AT command communication
printf("Initiating AT Command Test\r\n");
while(SendESP8266Command("AT\r\n", "OK", 200))
{
    HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnboardIndicatorLED_GPIO_Port, OnboardIndicatorLED_Pin);
HAL_Delay(500);

// Set module to Station mode
printf("Configuring CWMODE to Station\r\n");
while(SendESP8266Command("AT+CWMODE=1\r\n", "OK", 200))
{
    HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnboardIndicatorLED_GPIO_Port, OnboardIndicatorLED_Pin);
HAL_Delay(500);

// Disable automatic AP connection on power-up
printf("Disabling Automatic AP Connection\r\n");
while(SendESP8266Command("AT+CWAUTOCONN=0\r\n", "OK", 200))
{
    HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnboardIndicatorLED_GPIO_Port, OnboardIndicatorLED_Pin);
HAL_Delay(500);

// Disable AT command echo
printf("Disabling AT Command Echo\r\n");
while(SendESP8266Command("ATE0\r\n", "OK", 200))
{
    HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnboardIndicatorLED_GPIO_Port, OnboardIndicatorLED_Pin);
HAL_Delay(500);

// Connect to target Wi-Fi network
printf("Connecting to Wi-Fi Network\r\n");
while(SendESP8266Command("AT+CWJAP=\"${WIFI_NETWORK_SSID}\",\"${WIFI_NETWORK_PASSWORD}\"\r\n", "OK", 500))
{
    HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnboardIndicatorLED_GPIO_Port, OnboardIndicatorLED_Pin);
HAL_Delay(500);

// Configure MQTT user credentials
printf("Setting MQTT User Configuration\r\n");
while(SendESP8266Command("AT+MQTTUSERCFG=0,1,\"${MQTT_CLIENT_IDENTIFIER}\",\"${MQTT_USERNAME}\",\"${MQTT_PASSWORD}\",0,0,\"\"\r\n", "OK", 500))
{
    HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnboardIndicatorLED_GPIO_Port, OnboardIndicatorLED_Pin);
HAL_Delay(1000);

// Connect to MQTT broker
printf("Connecting to MQTT Broker\r\n");
while(SendESP8266Command("AT+MQTTCONN=0,\"${MQTT_BROKER_ADDRESS}\",1883,0\r\n", "OK", 500))
{
    HAL_Delay(1000);
}
HAL_GPIO_TogglePin(OnboardIndicatorLED_GPIO_Port, OnboardIndicatorLED_Pin);
HAL_Delay(500);

System Control Code

// Main system data update and control loop
void SystemMainControl(void)
{
    static uint8_t updateCounter = 0;
   
    updateCounter++;
    if(updateCounter >= SYSTEM_DATA_UPDATE_INTERVAL)
    {
        updateCounter = 0;
        TransmitDataToAliyun();
    }

    // Refresh temperature sensor readings
    RefreshTemperatureSensorData();
    // Read all connected sensor values
    ReadAllSensorMeasuredValues();
}

Provided Design Materials

The design package includes schematic PCB files (PDF, AD, and Libra CAD EDA versions), complete source code, system block diagrams, main program flowcharts, bill of materials with component datasheets, and functional operation instructions.

Tags: STM32 Alibaba Cloud IoT Water Quality Monitoring Fish Pond Management ESP8266

Posted on Wed, 12 Aug 2026 16:00:21 +0000 by trailerparkboy