The Harmonyos Developer Innovation Competition, which ended on May 24, 2021, saw the pioneers from the ancient capital Xi ‘an win the third prize for their smart farm, Through distributed soft bus, distributed database technology, distributed task scheduling, distributed cross-device data flow and other capabilities of Harmonyos, this work realizes the interconnection and automatic control of multiple devices (sensors, smart screens, etc.), and realizes the collaborative intelligent farming experience of multiple devices in the farm scene, which is impressive.

Here’s a quick look at the Blazers’ thoughts on building Smart Farm based on Harmonyos and a quick share of the key technologies:

1. Background

At present, manufacturers’ equipment (sensors, etc.) related to smart agriculture on the market are relatively independent, and there is no unified operating system platform, which makes it difficult to interconnect. Moreover, most devices need to be connected, which results in long deployment time, high cost and high maintenance complexity. With the coverage of 5G network and the emergence of the next generation of Harmonyos, it has become more convenient for everything to be connected. It is possible for a mobile phone to operate all IoT devices and realize the interconnection of all IoT devices. Based on Harmonyos, the intelligent farm system realizes the interconnection and automatic control of multiple IoT devices (sensors, motors, large screens, etc.), and realizes the intelligent farming of the whole scene. Intelligent farm through various sensor equipment for real-time detection of the farm’s environmental indicators, and can be self-adaptive adjustment, so that animals have been in a good growth environment. Through this system, the indicators exceed the threshold warning, intelligent reminder, intelligent help and other functions can be realized, so that the threshold of farm breeding is lowered, so that farmers can easily become breeding experts.

2. Demand analysis

The smart farm system collects the air temperature, humidity, illumination and other environmental parameters of the farm in real time to ensure that the farmer can know the farm conditions at any time through the smartphone APP. At the same time, the system can adjust adaptively according to the changes of environmental factors inside and outside the farm, which can not only ensure that the farm animals are in a good long-term growth environment, but also improve the yield and quality of animals. The features of the system include: fine intelligent reminder, expert video help, etc.

Intelligent reminder function is reflected in multiple scenarios, such as: when the farm temperature is too high, the cooling equipment is damaged or the temperature does not fall within a specified time and other abnormal circumstances occur, the system will directly give the manager a telephone reminder or message push; Not only that, the system will also predict the growth cycle of different animals based on their age, and remind managers to vaccinate the animals. The intelligent help-seeking function is embodied in that when the manager encounters some breeding knowledge problems, he can find relevant help information through the intelligent help-seeking; At the same time, it also provides the video help function of aquaculture experts to help managers timely solve the problems encountered in aquaculture.

3. Solutions

Roles involved in this solution: farmer, aquaculture specialist; Involving hardware equipment: mobile phone, smart screen, development board, various types of sensors (such as: temperature and humidity sensor, combustible gas sensor, photosensitive sensor, human body infrared sensor, etc.) and various peripherals (such as: fans, water filling equipment, heating equipment, etc.). Mobile phones, smart screens, and development boards are based on Harmonyos and are connected to each other via WiFi. The farmer can set various environmental indicators (threshold range of temperature and humidity, concentration range of combustible gas, illumination intensity, etc.) in the farming scene through the mobile phone APP, and can also set timing tasks (such as regular adding of water, feeding, regular playing of music, etc.) to realize intelligent reminder and automatic control. The query and analysis of breeding technology and case information in the solution are provided by the cloud data service, and the video call service of expert video help function is provided by the cloud.

4. Introduction of main modules

Data acquisition module (taking humidity collection as an example)

After the system is successfully started, the data acquisition module will start the timing task to collect temperature data, and collect the current temperature data from the temperature sensor at a regular time. If the collection is successful, it will be stored in a distributed database (stored in KV mode), and the collected temperature data will be refreshed to the temperature display interface in real time. Temperature data range: -40~80℃. The collected data can be transferred to a large screen for the convenience of users. (See Figure 1 below for the collection process)

Business process:

Fig. 1 Process of collecting temperature data

Automatic control module (taking temperature control as an example)

Retrieves the current temperature data periodically against the normal threshold range or maximum threshold range (from :Preferences) set by the user. If the current temperature is within the normal threshold value range, do not process; If the range of normal threshold is exceeded and the range of maximum threshold is not exceeded (for example: normal range: 5~30℃), there are two cases:

1. If the temperature is below 5℃, turn on the heating device and call the intelligent reminder module to notify the user; Return temperature to normal range, shut down equipment.

2. If the temperature is higher than 30℃, turn on the fan to cool down, and call the intelligent reminder module to notify the user; Return temperature to normal range, shut down equipment.

If the maximum threshold range is exceeded (e.g., the maximum threshold range: <-20 ° C or >60 ° C), there are two conditions:

1. If the temperature is below -20℃, turn on multiple heating devices and call the intelligent reminder module to send notification and make a phone call to inform the user; Return temperature to normal range, shut down equipment.

2. If the temperature is higher than 60℃, turn on the water spray to cool down, and call the intelligent reminder module to send notification and make a phone call to inform the user; Return temperature to normal range, shut down equipment. (See Figure 2 below for the control process)

Fig. 2 Temperature control flow

5. Implementation of key technical details

1) Distributed multi-device discovery to achieve multi-device collaboration and scheduling

Distributed device discovery key code:

List<DeviceInfo> onlineDevices = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);

Key code for task scheduling of distributed devices:

Intent intent = new Intent();

Operation operation =

new Intent.OperationBuilder()







2) Distributed data flow

Calling Continueability for data flow (key code)


3) Socket communication to achieve interconnection between devices (the following key codes)

// Call NetManager.getInstance(Context) to get the networkmanaged instance object.

NetManager netManager = NetManager.getInstance(context);

// Call NetManager.getDefaultNet() to get the default data network.

NetHandle netHandle = netManager.getDefaultNet();

// Call nethandle.bindSocket () to bind the network.

DatagramSocket socket = new DatagramSocket();


// Sending data using a socket


4) Tramp pit (coordinated use of distributed task scheduling and distributed database technology) (Function: real-time synchronization of data collected by smart farm mobile terminal to TV terminal)

When the collected environmental data (such as temperature, humidity and concentration of combustible gas) are received on the mobile phone, they need to be transferred to the smart screen for display. When the distributed task scheduling used by the team at the beginning is transferred to the TV terminal, it is found that the data displayed on the TV terminal is not refreshed in real time, which obviously does not meet the practical needs.

To enable real-time data refresh, the team discovered Harmonyos’ ability to distribute data across devices on the same app, network, and account, so they adopted Harmonyos’ distributed database technology to ensure simultaneous refresh between mobile and TV. Do this without relying on cloud services.

Here is the key code for implementation:

Mobile terminal data storage:

// Initialize the SingleKvStore object

KvManagerConfig kvManagerConfig = new KvManagerConfig(context);

kvManager = KvManagerFactory.getInstance().createKvManager(kvManagerConfig);

Options options = new Options();





SingleKvStore singleKvStore = kvManager.getKvStore(options, storeId);

The collected sensor data is stored in the distributed database:


“+… Is omitted

“} “);

TV terminal for data acquisition:

// Initialize the SingleKvStore and register the listener KvStoreObserverClient to watch the data change:

KvManagerConfig config = new KvManagerConfig(getContext());

KvManager kvManager = KvManagerFactory.getInstance().createKvManager(config);

Options CREATE = new Options();



singleKvStore = kvManager.getKvStore(CREATE, Constant.KV_STORE_NAME);

kvStoreObserverClient = new KvStoreObserverClient();

singleKvStore.subscribe(SubscribeType.SUBSCRIBE_TYPE_ALL, kvStoreObserverClient);

// To implement KvStoreObserver, redo the onChange () method to get distributed data, and update the UI requires switching to the main thread.

private class KvStoreObserverClient implements KvStoreObserver {


public void onChange(ChangeNotification notification) {

String value = singleKvStore.getString(“*“);

DataCollectionEntry entry = ZSONObject.stringToClass(value, DataCollectionEntry.class);

getUITaskDispatcher().asyncDispatch(() -> initView(entry));



Since signing up for the Harmonyos Developer Innovation Competition, the team has grown from a group of new Harmonyos developers who had never worked with each other before to professionals. Participating in the competition has also given the team a strong exposure to Harmonyos’ distributed technology and advanced design concepts, laying a solid foundation for the development of more creative and socially valuable titles.

Every Harmonyos developer is a member of Huawei’s team. We hope that more and more Harmonyos developers will join the Harmonyos developer ecosystem and create possibilities together!