---
# System prepended metadata

title: GEMU - *(Greenhouse Enviroment Monitoring Unit)*

---

# GEMU - *(Greenhouse Enviroment Monitoring Unit)*
By Christoffer Ovesson  (co222sy) 2021-07-31

## Introduction
Living the hectic family life with alot of activites and adventures during the summer, care of the greenhouse becomes forgotten. So in order to make this a little bit easier in a fun way is to set up a LoRa network and attach a device with sensors to keep track of the temperature, humidity and soil moisture in the greenhouse!

If you like me, do not have coverage of LoRa, a Pygate is a nice, cost-effecient and easy to use LoRa-gateway that you can set up all by yourself following some easy tutorials from Pycom. This tutorial will cover what materials are needed and a link to a Pycom-tutorial on how to set it up with The Things Network.

The project consists of setting up a device with temperature-, humidity- and soil moisture-sensors along with a LoRa-gateway. Programming the device to read and send the sensordata. Sending data to The Things Network and passing it on to Ubidots to visualize it.

**Estimated time it takes to do the project:**

Approx. 7 hrs with LoRa-gateway setup included

---
## Objective

**Reason for choosing project:**

I chose this project because i have an easier time to find motivation to learn something if i apply the new knowledge to make somthing that fulfill a need. The need in this case is to make an easier time to take care of a greenhouse and in the future to use my LoRa-network to set up humidity/temperature sensors in diffrent parts of my house together with a TIG-stack. But the GEMU is enough to get things started. 

**The purpose it serves:**

The two main purposes for this project:
* All living things derserve love! (To give your plants the love that they need to reach their full potential the basics are to have the right temperature, light, humidity and to water them according to their needs)   
* A great way to get first hand experiance and knowledge about IoT

**Insights i think the project will give:**

I think this project will help to get a basic understanding and hands on experiance of how to assemble a microcontroller and attach it with sensors, program it to collect data and send it to a cloud server (The Things Network), pass on the data from the cloud server to a cloud based IoT-platform and to finally visualize the data.   


---

## Material

### List of material for GEMU and Pygate:

**GEMU**
| Item | Description | Price | Link |
| ---- | ----------- | -------- | ---- |
|LoPy4 and sensors bundle|Custom bundle for course 1DT305 Applied IoT @ LNU|€93|[Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/)|
| -LoPy4| Micropython-programmable development board. Works with LoRa, Sigfox, WiFi and BLE| Incl. in bundle   | [Pycom](https://pycom.io/product/lopy4/) |
|-Expansion board 3.0| Expansion board for Pycom boards to enable connection of sensors, USB and LiPo-battery|Incl. in bundle |[Pycom](https://pycom.io/product/expansion-board-3-0/)|
|-Micro USB-cable|For connecting the device to a computer or for powersupply|Incl. in bundle|[Electrokit](https://www.electrokit.com/produkt/usb-kabel-a-hane-micro-b-5p-hane-1m-rod/)|
|-LoRa & Sigfox antenna kit|External antenna for LoRa or Sigfox|Incl. in bundle|[Pycom](https://https://pycom.io/product/lora-868mhz-915mhz-sigfox-antenna-kit/)|
|-Breadboard| To connect sensors with expansion board| Incl. in bundle| [Electrokit](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/)|
|-Jumper cables Male - Male|Cable for connection between expansionboard and breadboard|Incl. in bundle|
|Jumper cables Male - Female|Cable for connection between breadboard and sensors|€8|[Kjell & Co](https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektroniklabb/delbar-kopplingskabel-40-pol-15-cm-hane-hona-p87076)|
|DHT11| Temperature and humidity sensor| €5|[Electrokit](https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/)|
|VM303| Soil moisture sensor|€15|[Kjell & Co](https://www.kjell.com/se/produkter/el-verktyg/arduino/arduino-tillbehor/vattensensor-och-jordfuktsmatare-for-arduino-p87066)|
|Powerbank 15 000 mAh|As powersource for the device|Found in old drawer|![](https://i.imgur.com/afgcNJb.png)|
|Plastic container| To use as casing if you don't have a 3d-printer and can't make your own| Found in old drawer|![container](https://i.imgur.com/ShbfrxR.jpg)|
|**Total**||**€121**||

**PyGate**
| Item   | Description                    | Price | Link |
| ------ | ------------------------------ | ----- | ---- |
| Pygate | LoRa-gateway shield from Pycom|€55|[Pycom](https://pycom.io/product/pygate/)|
|WiPy 3.0|Development board to give the pygate processing power. Pygate can be used with a WiPy 3, GPy or LoPy 4. But the WiPy is the cheapest|€22|[Pycom](https://pycom.io/product/wipy-3-0/)|
|LoRa & Sigfox antenna kit|External antenna for LoRa or Sigfox|€9|[Pycom](https://pycom.io/product/lora-868mhz-915mhz-sigfox-antenna-kit/)|
|Pygate case|For protection of the device|€9|[Pycom](https://pycom.io/product/pygate-case/)|
|Charger with USB C|As powersource for the Pygate|€28|[Kjell & Co](https://www.kjell.com/se/produkter/el-verktyg/laddare/mobilladdare/usb-c-laddare/linocell-3-a-usb-c-laddare-quick-charge-3.0-p97692)|
|*Tax if outside the Great British Empire|Brexit stuff|€50||
|**Total**||**€173**|


---

## Computer setup

### IDE
The IDE i worked with is [VS Code](https://code.visualstudio.com/download) since a already had it installed on my pc. Another great option is [Atom.io](https://atom.io/).

**Steps for setting up the LoPy4 and computer:**
1. Connect the LoPy4 to the expansion board (the LED of the LoPy4 over the USB connection of the expansion board)
 ![](https://i.imgur.com/tiDlTQk.png)
 2. Connect the USB-cable to the expansion board and the computer.
 3. Update your device by downloading and installing the "Pycom Firmware Update" and select the firmware "Pybytes" by following the instructions from Pycom ["Updating device firmware"](https://docs.pycom.io/updatefirmware/device/)
 4. Download and install [VS Code](https://code.visualstudio.com/)
 5. Download and install [Node.js](https://nodejs.org/en/)
6. Install the Pymakr extension in VS Code by following steps 3 - 7 in this instruction from Pycom -->["Pymakr"](https://docs.pycom.io/gettingstarted/software/vscode/)
7. You are ready to rumble!

**Setting up the Pygate and TTN**
Follow this great tutorial from Pycom on how to set up your Pygate and connecting it to The Things Network --> [Link](https://docs.pycom.io/tutorials/expansionboards/pygate/)

**The end result should look something like this:**

Pygate:
![](https://i.imgur.com/WezN9Fa.jpg)

TTN:
![](https://i.imgur.com/8jKo0xm.png)




**How the code is uploaded**
The code is uploaded to the LoPy4 by:
1. Connect the USB-cable to the expansion board and the computer.
2. Starting VS Code with the Pymakr extension
3. Right click on a file in your project and choose "Pymakr > Upload project"![](https://i.imgur.com/JjZ5MJj.png)
 
## Putting everything together

:::    warning
Be sure to attach the LoRa-antenna the first thing you do! Else the device can be damaged!
:::
As this is my first time connecting microcontrollers and sensors i have chosen this project to be a development setup only.

See the schematics for how the sensors are connected:
![](https://i.imgur.com/hKe6ruu.jpg)

## Platform

As i'm a poor student i have chosen free-to-use platforms to test diffrent alternatives insted of committing to a paid service. In the future i plan to set up TIG-stack on-prem to have greater controll over my data :)

### The Things Network

[![](https://i.imgur.com/WwTtGTo.jpg)](https://www.thethingsnetwork.org/)


TTN is a easy and free to use platform for receiving and resending data to diffrent IoT-platforms through LoRa.

I followed this  -->[excellent tutorial](https://hackmd.io/5RbTAtCxTPu-hRi3k4p3dQ?view)<-- by David M for how to send my data from device to TTS (The Things Stack)

---
### Ubidots

[![](https://i.imgur.com/c04v56w.jpg)](https://ubidots.com/)

Ubidots is a IoT-platform for receiving, storing and visualizing data. It's easy to use and has, in my opinion, great features for visualizing your data.

To send data from TTS to Ubidots via a webhook i followed this -->[great tutorial](https://hackmd.io/Q8xTeuV6RMSrWyczWFQs6A?view)<-- by Erik K!

## The code

I'm totally new to this so i have searched the internet for code examples and used tutorial in the course and tried it multiple times to get a working code. None of the code is really my own and i have tried to give credit by linking to the sources i have used.

### VMA303

To get data from the soil moisture sensor i used code from this [forum thread](https://forum.pycom.io/topic/2047/how-to-connect-a-lopy-to-a-moisture-sensor/5) to get a reading and then took a reading from the sensor when it was submerged in a glass of water to get a max-reading and took that as 100% moisture. In the code i divide the sensor reading with (max/100) to get moisture level

```python=
adc = machine.ADC()                                             # create an ADC object
    adc.init(bits=12)
    apin = adc.channel(pin='P19', attn = machine.ADC.ATTN_11DB)     # create an analog pin on P16, range 0..3.3V
    moisture = apin() / 23.55                                       # read an analog value and divide with (2355/100) to get moisture level in %
```

### DHT11

To get the readings from the sensor i first copied a file named [dht.py](https://github.com/JurassicPork/DHT_PyCom/blob/master/dth.py) made by [JurassicPork](https://github.com/JurassicPork/DHT_PyCom) to my project/lib and in the main.py imported DHT from dht.py 
```python
from dht import DHT 
```

Then to read the sensor and send it to TTN i used a modified version of code from [dht11_example.py](https://github.com/JurassicPork/DHT_PyCom/blob/master/dht11_example.py) and from step 5 in [TTS Tutorial by David M](https://hackmd.io/5RbTAtCxTPu-hRi3k4p3dQ)

```python=
# DHT11 sensor
    th = DHT(machine.Pin('P3', mode=machine.Pin.OPEN_DRAIN), 0)  
    result = th.read()
    time.sleep(2)
    # Send data
    payload = struct.pack('>hBB', int(result.temperature * 100), int(result.humidity), int(moisture))
    s.send(payload)
    time.sleep(2)
```

### boot. py

To get the device to connect to LoRa network when waking up i used [this](https://www.thethingsnetwork.org/docs/devices/lopy/usage/) code from their OTAA-example and changed the app_key to the one i got from TTN


## Transmitting the data / connectivity

Every 15 minutes the GEMU sends a payload consisting four bytes with the sensor data via LoRa (to be energy-efficient and not drain the battery of the device) through my Pygate which is connected with WiFi and forwards the data to TTN. Then the data is transferred from TTN to Ubidots with a webhook and decoded with a code-snippet from [Tutorial TTN-Ubidots by David M](https://hackmd.io/Q8xTeuV6RMSrWyczWFQs6A?view). The pygate is plugged in directly to a wallsocket so energy is not a problem. I chose Wifi instead of ethernet because of mobility, I'm not sure where to place the Pygate and have moved it around in my house searching for the sweetspot where it gets good coverage but where it's not inte the way of exited children.

![](https://i.imgur.com/BhxRArZ.png)


## Presenting the data

Ubidots stores the data that is sent every 15 minutes

Ubidots has a very easy way of building your dashboard:

1. Logged in to Ubidots you click the "Data" tab in the center-menu and choose "Dashboards"
2. Click on the left menu and the "+"-sign to create a new dashboard
3. Name the dashboard and click on the green icon in the bottom-right corner
4. Add a widget by clicking "Add new widget"  
5. Select which kind of widget you want, for example "Thermometer"
6. Add a variable by clicking the "+"-sign
7. Select from which device you want to add the variable
8. Select variable and click the green symbol
9. Set the right parameters according to which kind of data you are displaying and hit the green button again
10. Repeat steps 4 - 9 until you are satisfied :)


Example of my dashboard:
![](https://i.imgur.com/spTehdC.png)

## Finalizing the design

After many nights full of frustration where things just seemed to be impossible but in the end turned out to not be that hard thanks to the great tutorials and help from the TA's i am very satisfied with the results of my project, sure it's not pretty but it works as intended! By following tutorials in the course in the right order, the lectures and workshops i feel that the progression from "knowing nothing" like Jon Snow, to set up a functioning device and a gateway and have a desent understanding of IoT is on a much higher level. If i were to do the same project again i would only buy the necessary items instead of all the sensors i bougth "just in case" (around 50 pcs).

In the near future i will set up a raspberry pi with TIG-stack to "own" my data and equip the GEMU with sensor for measuring how much light the greenhouse-plants get. 


**The GEMU:**                             

![](https://i.imgur.com/2eIvUn6.jpg)     

**Under the hood lies chaos that needs some tidying up:**

![](https://i.imgur.com/jcU6W3M.jpg)


**Soil sensor get the electro-version of glitter (shrink tubing):**

![](https://i.imgur.com/VhOYxVn.jpg)

**The underloved greenhouse:**

![](https://i.imgur.com/RPBTPka.jpg)


**Pygate:** 

![](https://i.imgur.com/H7rPA8T.jpg)

**Dashboard:**

![](https://i.imgur.com/PwYiES3.png)

