<h1>
Guitar Case Humidity Tracker
</h1>
Written by Johan Johansson (Student id: jj223zb)
This is a tutorial showing how to build an IoT project that measures the relative humidity level in a guitar case.
The amount of time it takes to complete this project is probably between 20-120 minutes depending on your experience in similar projects.
<h2>
Objective
</h2>
Guitars are made of wood which is a "living" material it shrinks and grows and cracks depending on the season. Acoustic guitars are extremely sensitive to dry climate, they often get cracks in the winter. To protect the guitars it's recommended to place a humidifier in the guitar case. The relative humidity (RH) should be between 45-55%. Depending on the season you have to refill the humidifier in different intervals. Sometimes once a day, and sometimes it takes months.
To battle this problem and to protect my 30.000 SEK guitar. The idea of this project was to build something that alerts the user when the humidity turns to low.
This project isn't the most complex project with very much cool stuff happening, but it's a simple project that really helps me in the real life. It's a beginner friendly project and the aim has been to learn a little about many parts of IoT projects and actually build something useful, not to dive deeply into different sensors, wireless protocols or visualizing data.
<h2>
Material
</h2>
The material used for this project is:
| Product | Vendor | Link |Price |
| -------- | -------- | -------- | -------- |
| Pycom LoPy4(with headers)| Pycom | https://pycom.io/product/lopy4/ | €38.45|
| Pycom Expansion Board 3.0| Pycom | https://pycom.io/product/expansion-board-3-0/ |€17.60|
| DHT11 Sensor| Electrokit | https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/ | 49 SEK|
| Jumper Wires| Electrokit | https://www.electrokit.com/produkt/labbsladd-20-pin-15cm-hona-hane/|29 SEK|
| Battery Holder | Electrokit | https://www.electrokit.com/produkt/batterihallare-2xaa-kabel-jst-ph/|19 SEK|
| USB A - Micro USB cable | Electrokit | https://www.electrokit.com/produkt/usb-kabel-a-hane-micro-b-5p-hane-1m/ |39 SEK|
| Rechargable Batteries | Clas Ohlson | https://www.clasohlson.com/se/Laddningsbart-batteri-AA-HR6/p/36-5854|139.90 SEK|
The Pycom LoPy 4 is a development board with multiple wireless protocols on board and code is written in micro python. In this project it's connected to an expansion board for easy setup connecting the sensor via the wires and computer communication via usb. There is only one sensor needed for this project: the DHT11 sensor that measures temperature and relative humidity. The LoPy4 is powered by batteries since it will live inside a guitar case.
<h2>
Computer Setup
</h2>
The computer used is a MacBook Pro 15" 2017.
The [IDE](https://en.wikipedia.org/wiki/Integrated_development_environment) used in this project is [Atom](https://atom.io/)
There is a plugin for atom called [Pymakr](https://pycom.io/products/supported-networks/pymakr/) that makes the communication with the device simple.
You have the possibility to use [Visual Studio Code ](https://code.visualstudio.com/) since the Pymakr plugin is available for visual studio code aswell. I chose Atom since I've used visual studio code a lot before and wanted to try something else.
On a mac , the setup process is pretty simple, download [Atom](https://atom.io/) and the [Pymakr](https://pycom.io/products/supported-networks/pymakr/) plugin.
Install atom first, then the Pymakr plugin. To show the Pymakr console you might have to toggle the console, like this:

Connect the LoPy4 via USB and the device should show up something like this:
To upload code your code to the device, you only need to press the "upload project to device" button:

<h2>
Putting it together
</h2>
You could use a breadboard when connecting the sensor, but since the project only require one sensor and space is a bit important to make a good fit in a guitar case the sensor is connected directly to the extension board via male-female jumper wires.
The connection is shown below, be careful to place the wires right!

In this project I use WiFi to send data to the cloud. The LoPy4 has a built in WiFi antenna and there is no need for an additional antenna in my case.
<h2>
Platform
</h2>
The platform used is ["Ubidots Stem"](https://ubidots.com/stem/). I also tried Pycom's own platform [pybytes](https://pybytes.pycom.io/) but Ubidots have a little more functionality. Especially the function to trigger email sendouts based on values triggering events was the most important reason to choose Ubidots. In Ubidots you can set up and customize your own dashboards in a simple way, and it looks good and professional directly from the start.
Ubidots stem is the free version of the Ubidots IOT platform. It has some limitations, you have the possibility to add up to three devices for free and you can make a maximum of three dashboards and trigger no more than 100 emails every month. The data is saved for 30 days.
<h2>
The code
</h2>
The file structure looks like this:

* In boot.py there is basic setup of WiFi.
* main.py is were the "actual work" is done, reading and sending data.
* keys.py contains WiFi password and the token you need to authenticate the request to ubidots.
* In the lib folder libraries used to read from the sensor and sent HTTP requests are placed.
To make the HTTP requests I use a python library called [urequests](https://github.com/jotathebest/micropython-lib/blob/master/urequests/urequests.py) that makes it easy to send the POST request I need to send to Ubidots.
The code is based on [this](https://help.ubidots.com/en/articles/961994-connect-any-pycom-board-to-ubidots-using-wi-fi-over-http) tutorial by ubidots.
The boot.py contains code to connect to WiFi.
```python=
import machine
from network import WLAN
import keys
wlan = WLAN(mode=WLAN.STA)
wlan.connect(ssid=keys.SSID, auth=(WLAN.WPA2, keys.wifi_password))
while not wlan.isconnected():
machine.idle
print(wlan.ifconfig())
```
To read data from the DHT sensor use [this](https://github.com/JurassicPork/DHT_PyCom) library and place it in the "lib" folder. To set up the reading from the pin, this piece of code is used in main.py:
```python=
dht_sensor = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0) #Sets up Pin 23 for measuring
```
In the main code there's a function to build the json payload
```python=
def build_json(temp, humid): #Formats the sensor data as json
try:
data = {"temp": {"value": temp},
"humidity": {"value": humid}}
return data
except:
return None
```
This function is called by another function that sends the actual request:
```python=
def post_request_to_ubidots(value1, value2 ):
try:
#The url address for the request
url = "https://industrial.api.ubidots.com/api/v1.6/devices/guitarcase"
#Adding headers, your unique TOKEN is found in your Ubidots account
headers = {"X-Auth-Token": keys.TOKEN, "Content-Type": "application/json"}
#Building the json payload
data = build_json(value1, value2)
if data is not None:
#Sending a POST request
req = requests.post(url=url, headers=headers, json=data)
return req.json()
else:
pass
except:
pass
```
For the actual reading and sending the sensor data is read, sent to the ubidots platform and then the device for four hours until it wakes up and starts again:
```python=
result = dht_sensor.read() #Reads the sensor and saves the result
while not result.is_valid(): #Checks if result is valid
time.sleep(0.5) #Waits
result = dht_sensor.read() #And reads again
post_request_to_ubidots(result.temperature, result.humidity) #Post request
machine.deepsleep(SLEEP_TIME) #SLEEPING
````
## Transmitting the data
The data from the sensor is being sent every fourth hour via WiFi in a HTTP message. If you want the battery to last longer you might want to send the requst once a day or use another wireless protocol. I would have used LoRa but there is not good enough LoRa coverage in my studio where the guitar is placed. There is also a possibility to use MQTT instead of HTTP and that might be better suited.
## Presenting the data
To present the data, register an account at [Ubidots](https://ubidots.com/).
When you're logged in, navigate to "API Credentials" and copy the "Default Token"

Paste the token in the headers section in the HTTP POST request in main.py and start sending the data.
Soon your device should automatically show up on the "devices" page on Ubidots.
The data is saved everytime you send it Ubidots and it's saved for 30 days.
Navigate to the device page and you should se the "variables" Temperature and Humidity.
Press the "Data" tab and choose dashboard and follow the steps to set up a dashboard of your liking. Mine looks like this:

To trigger emails based on certain conditions navigate to Data->Events and add new events and you will go through a wizard to create events.
This is my events:

## Finalizing the design
This is a picture of the complete hardware setup of the project. (Except that it it's powered by a USB powerbank on this picture)

This project was built in a short time and is fairly simple. It went good and my musician friends are really impressed and thinks it's a useful project. The only similar product for measuring guitar humidity is [Daddario Humiditrak](https://www.daddario.com/products/accessories/humidification/sensors/humiditrak-bluetooth-humidity-and-temperature-sensor/) which looks good but only connects via bluetooth. And since me and many other musicians have our instruments in studios and rehearsal spaces far away from home, bluetooth isn't an option.
The next step in this project will be to maximize battery life. Maybe trying to use LoRa and connect via [Helium](https://www.helium.com/) So far I have only tried [The Things Network](https://www.thethingsnetwork.org/) and the coverage isn't good enough. Using MQTT instead of HTTP probably would be a good choice.
For my next IoT project I want to build something a bit more advanced with multiple sensors and perhaps actuators, but this project was a good beginner friendly start!