Small-Size Earthworm Breeding Monitoring System

Photo of RT-Thread

Made by RT-Thread

About the project

Monitor air temperature and humidity in breeding environments, soil temperature, and vibration density of the earthworm.

Project info

Difficulty: Easy

Estimated time: 1 hour




The monitoring system was developed based on ART-PI and RT-Thread Studio IDE, approach to dht11,ds18b20, soil moisture, mpu6050 to monitor air temperature and humidity in breeding environments, soil temperature, and vibration density of the earthworm. External fans and pumps will control the above environmental variables. After connecting to the cloud OneNet, the environment variables can be observed and controlled remotely in the backend in real-time.

Development Environment

Hardware: ART-Pi STM32H750

Expansion Board: DS18B20 waterproof digital temperature sensor, DHT11 temperature and humidity sensor, 2pcs MPU6050 sensor, soil moisture sensor.

RT-Thread: V 4.0.3

IDE: RT-Thread Studio IDE 2.0.0(Free of charge)

RT-Thread RTOS Used in this Project

Scheduler: Create multiple threads to do different work.

Semaphore: Used to synchronize threads.

Mutex: Multiple related acquisition threads can access system resources at the same time and are not preempted by other threads

Message Queue: Used to synchronize acquisition threads, control threads, and Cloud OneNet upload threads.

Message Mailbox: onenet_mqtt synchronize with the control thread after accepting the control command

Components: I2c framework, Sensor framework

I2C Framework: Use the I2C Framework to drive the MPU6050

ADC device: Used to collect soil moisture

PWM device: Used to control fans, pumps.

Sensor Framework: The MPU6050, DHT11, DS18b20 are packs that based on sensor framework that provide a unified operating interface for the upper layer, improve the reusability of the upper layer code, and simplify the underlying drive development.

RT-Thread Software Packages Used

Webclient: Provide the basic functions of communication between the device and the HTTP Server.

pahomqtt: A set of MQTT client programs designed on the basis of Eclipse paho-mqtt source packages.

Onenet: An adaptation to connect RT-Thread with the OneNET Cloud platform so devices can be easily connected to the OneNet platform on RT-Thread to complete data transmission, reception, device registration and control, etc.

cJSON: The minimalist resolution of the JSON format package implemented in C language.

DHT11: GPIO simulates a single bus protocol, reads sensor temperature and humidity, and registers in the sensor framework.

Ds18b20: GPIO simulates a single bus protocol, reads the sensor temperature, and registers in the sensor framework.

MPU6050: A universal sensor drive package for InvenSense six-axis series of sensors, with the option of an I2C or SPI communication protocol. With the Sensor framework, developers can quickly drive this sensor.

Hardware Framework

Software Framework

Multiple acquisition threads read the temperature and humidity in the environment in real time and send it to the stream message queue; Onenet uploading thread receives the message and uploads it to the appropriate stream. When the Cloud issued commands, triggering Onenet_cmd_rsp_cb, in which to send the received control commands to the command cache mailbox, the control thread will query the mailbox, after finding the mail then start using rt_mq_urgent to send emergency control stream, and timely synchronize the status of cloud control devices.

Software Module Explanation

>span class="">Semaphore

mqttinit_sem is mainly used for WIFI connection and to synchronize onenet_mqtt_init.

sensor_msg_sem_empty primarily used to limit the number of message resources consumed by the acquisition thread.

5 Acquisition Threads

adc1_5_entry: Periodically read the value of the soil moisture sensor and send the value to the message queue sensor_msg_mq

read_dhtll_entry: Periodically read the value of the DHT11 temperature and humidity sensor and send the value to the message queue sensor_msg_mq

read_ds18b20_entry: Periodically read the value of the ds1b20 soil temperature sensor and send the value to the message queue sensor_msg_mq

read_gyro0_entry: Periodically reads the Y-axis speed of the six-axis sensor MPU6050_0 as the intensity of earthworm activity (indirectly reflecting earthworm density and activity), and sends the read value to the message queue

read_gyro1_entry: same as above, two-point measurement.

Message Queue:

sensor_msg_mq: To synchronize acquisition threads, control thread and cloud. Because Onenet has requirement on data refreshing, so I set 1s for data refreshing. Only assign 5 messages to the acquisition thread.

In addition, although we opened up eight messages, we used sensor_msg_sem_empty semaphore to limit the acquisition threads can only be used up to 5, leaving the other 3 to quickly synchronize the control flow and refresh the status of the cloud control device in a timely manner.


sensor_msg_mutex: Since DHT11 reads both temperature and humidity environment variables, corresponding to two streams in the cloud, you need to send two messages to ensure synchronization of the two streams, get the Mutex first, and do not release the Mutex until all the messages are sent.

Message Mail

sensor_msg_mailbox: There are no clear requirements to limit the interval between sending commands in the cloud, I set up 16 mails. In the Onenet_cmd_rsp_cb, the received commands are sent directly to the mailbox, and the control thread receives the mails and processes them.

Control Thread

center_control_entry: Receive mail from sensor_msg_mailbox mailbox, parse commands, control devices, and synchronize device status to the cloud.

Uploading Thread

onenet_upload_entry: Long waits to receive messages from the queue and then send data to a different stream in the onenet cloud based on the stream_id in the message

State Thread

Main: led flashes in every 2 seconds to indicate the current operating status of the system.

If you want the Code, contact Open Source RT-Thread IoT OS on Facebook or Twitter. 
Thanks to RT-Thread Community Developer Liu Tao for sharing this project.


Photo of RT-Thread


An Open-Source Community-Powered Real-Time Operating System (RTOS) Project!


Leave your feedback...