Plant Monitor - Using IoT, MongoDB and Flutter
Tags: atlashackathon, flutter, iot, esp32
Published: 2022-01-12 17:58:56

{% youtube vGCJaMlH0_A %}

## Overview

**Plant Monitor** is an IoT-based project for monitoring plant health with ease. The projects consists of three parts:

* [IoT: Sensors](#iot-sensors) (for monitoring plant data)
* [Database: MongoDB](#database-mongodb) (Atlas, Realm and Chart)
* [Cross platform mobile app: Flutter](#mobile-app-flutter) (for getting realtime plant data)

### Submission Category: 

Action Star

### Link to Code

The code for both the app and the IoT part is present in the following GitHub repo:

{% github sbis04/plant-monitor %}

## Project Description

A detailed description of all the three parts of this project is present below.

### IoT: Sensors

![Hardware sensors](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tzyl4ae1m6hmjx2fpni.png) 

**ESP32** module is used for collecting data, processing and uploading to the MongoDB database. It is a low powered microcontroller integrated with WiFi and Bluetooth.

**DHT11** sensor is used for measuring raw temperate and humidity data from the surroundings of the plant.

**LDR** is used for calculating the light intensity that the plant is receiving.

**Soil Moisture** sensor module is used to measure the moisture or the water level of the soil. The sensor includes a potentiometer to set the desired moisture threshold.

### Database: MongoDB

![MongoDB Atlas](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tils8x032t1dbq4wvhe0.png)

MongoDB Atlas provides cloud database service using AWS, Azure, and Google Cloud. It also has various automation tools for performing various backend functionalities and comes with visualization tools. The services that are used in this project are described below.

**Atlas** is used as the main cloud database for storing the sensor readings along with timestamps.

![Atlas](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmlkn99o13e8sds51kk7.png) 

**Realm Function** is defined to easily store the sensor data from the ESP32 module. A **HTTP Endpoint** is created to access the MongoDB database from the ESP32.

The following function is used for storing the sensor data:

```js
exports = function(payload){
    var atlas = context.services.get("mongodb-atlas");
    var coll = atlas.db("plantdata").collection("readings");
    try {
      if (payload.body) 
      {
        body = EJSON.parse(payload.body.text());
      }
      coll.insertOne(body);
      console.log(body);
    } catch (e) {
      console.log("Error inserting doc: " + e);
      return e.message();
    }
};
```

**Charts** are used to visualize the sensor readings directly inside the MongoDB web app.

![Charts](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5da2rb8bgepbk9jfa1j.png)
 

For this project I have created four charts:

* **Plant Light** (%): Amount of light intensity plant is receiving
* **Plant Humidity** (%): Humidity around the plant
* **Plant Moisture** (%): Amount of water present in the soil 
* **Plant Temperature** (°C): Current temperature around the plant

### Mobile app: Flutter

![Flutter app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31ynnfmul2dvjoq6mrc8.png) 

To use the Realm data inside the Flutter application I have created a modified version of the [flutter_mongodb_realm](https://pub.dev/packages/flutter_mongodb_realm/versions/2.0.0-nullsafety.0) plugin to support Dart null safety and run it on the latest version of the Flutter SDK.

## References

* [MongoDB Atlas](https://docs.atlas.mongodb.com/getting-started/)
* [MongoDB Realm](https://docs.mongodb.com/realm/)
* [MongoDB Charts](https://docs.mongodb.com/charts/)
* [Realm HTTPS Endpoints](https://docs.mongodb.com/realm/endpoints)
* [flutter_mongodb_realm](https://pub.dev/packages/flutter_mongodb_realm/versions/2.0.0-nullsafety.0)
* [ESP32 Docs](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/get-started/index.html)