# **Research: Temp and Humidity reading using IoT**
**Author**:
*Full name*: Rickard Persson
*Student Id*: rp666gs
## Project overview
This is a Prof Of Concept (POC) for temperature and humitidy check for a holliday house. The house is in the middle of nowhere with frequent power-outlet leading to frozen pipes and destroyed inventories during winter-season.
With this project a POC is done for supervising mainly the temperature inside the house.
### Project time
If everything is going to plan it should take less than 2 days to setup and make sure everything work.
*[X] Title
[X] Your name and student credentials (xx666x
[X] Short project overview
[X] How much time it might take to do (approcimation)*
## Project objectives
### Background
I have a small sommer-house with two separate houses in the middle of nowhere. There is electrical power to the houses, but there are frequent power-outages, especially in the winter when the snow cause tree to fall down on the electrical wires. This have cased problem with the electrical heating in the house. Water-pipes have frozen and when electricity and with that the heaing cusing water-leaks and with that accompanying problems whent the electrical heat fall. A remote wathch of the indoor temperature have been on my mind for some time, but it have not been enough free-time to investigate and implement.
### Purpose
In this small research project is focus to get insight what is neede to do a remote supervise using sensors and networking.
I expect to get this by a small project, a Proof Of Concept (POC), at home. From this POC will then the main project be initiated.
### Insights
Beside getting insight of the IoT and sensor technology of todays it have also given some knowledge in the python programming language, a new language for me. The idea with python as a programming language for Systems On a Chip (SOC) are rather intersting for a C-guy like me.
The different communication technologies with Lora and low-power transmitting protcolols are also new and interesting to see in use. Lora seems like a nice technology, but it is unfortunateley not available at the location when looking at coverage maps, but it might be used internally at the site, but for now will WLAN be used in communication at site, and mobile network for connection to the outisde.
*[X] Why you choose the project
[X] What purpose does it serve
[X] What insighth do hou think it will give*
## Material
### List of Material and supplier
The followint materials was bought for this project.
| Component | Price | Supplier |
| -------- | -------- | -------- |
| Pycom Device with expansion board | 995.00 SEK | ElectroKit
| Temperature and Humidity sensor | 99.90 SEK |Kjell & Co
| Cable and breadboard | 70.00 SEK | Electrokit
| Raspberry Pi 3 | 329.00 SEK | Electrokit
### Material specification
- **Pycom Device with expansion board:** Read sensor and transmitt received data to central processing unit
- **Temperature and Humidity sensor:** DHT11 temperature and humidity sensor
- **Raspberry Pi:** Centrall processing unit, handle connectivity, data storage and visualization
*[X] List of material*
*[X] What the differnt things (sensors, wires, controllers) do - short specifications*
*[X] Where you bougth the them and how much they cost*
## Computer setup
### IDE
For program development is the, for me, new code editor [Atom](https://Atom.io). This IDE is selected as being one of two Editors (the other one is [Visual Studio Code](https://code.visualstudio.com/)) with adaption for downloading the python-code to the pycom SOC. I choose the *Atom* as it was a new experience for me, and it is alway educational to learn something completely new.
### Code deployment
Up-loading the python code is done over the USB-cable from the *Atom* editor.
### Environment adaptation
Before develepment started, I uppgraded the *pycom* expasion board using the instructions on the *pycom* homepage. Here some problems occured when I then started to code and deploy the *pycom* device with my own code from the tutorial. The updated USB driver for the upgrading firmware on the expansion board was not accepted by *Atom* and *pymaker* plugin, not until I had reloaded the old driver for the USB port was I able to connect (Using Windows 10 home/professional on different machines)
The Micropython on the pycom device must also be downgraded from the latest release to relesae 1.19.0.b1 to remove the Pybyte initialization built in in later releases. This caused problem when I wanted to setup the connection to my own local mgtt-broker. I received connection error at the *c = MQTTClient(client....* row. This took some time to figure, I tried to turn of this both by programing code and by changeing in the configfile. But nothing worked until degrade to a version without the functionality.
*[X] Chosen IDE*
*[X] How the code is uploaded*
*[X] Steps that you needed to do for your computer. Installation of Node.js, extra dirvers, etc.*
## The whole thing
DHT11 temperature and relativ humidity sensor have three pins for power and communication. Communication for both temperature and relative humidity is done over one single pin using a serial protocol.

Pycom system is powered by the USB cable (not shown in diagram), and power for sensor come from the powerregulated output from the pycom expansion board as shown in the diagram above.
*[X] Circuit diagram*
*[] * Electrical calculations*
## Platform
As the final goal for this is to supervise my summer-holiday house I'm looking into a standalone self-contained system. There is now LoRa connectivity (at least not according to the coverage-map, practical test is currently not yet done) means that all data-handling must be done in-house.
Platoform in the POC is:
- **a sensor** connected to the pycom
- **pycom SOC** read sensor and transmitt sensor-data over WLAN to backbone netowork
- **Raspberry Pi** receive the data, store it and provide graphical access

On *Raspberry Pi* runs:
- **mqtt broker** a mosquitto natively as broker between sensor and users
- **telegraf** a mqtt user reading sensor information and store it in database
- **influxdb** a timeline database where all data is stored
- **grafana** a tool for fetching timeline data, fetches data from influxdb
Telegraf, influxdb and grafana runs in docker containers at the Raspberry Pi. Installing after provide information from teacher - A note on grafana, docker and Raspberry Pi: Grafana have a **hardcoded** user id (472) that cause problems when started as this user is not confgured in the system. In the POC I have solved it by running the docker composer and then when all directories are created and grafana terminates with an error change priviledges on the directories and then restart the docker containers:
```
$> docker-compose up
ERROR......
$> sudo chmod -R a+rwz data/grafana
$> docker-compose up
```
Now have *grafana* write priviledges and can start. It is not a secure way for a production system, here must the userid be set to a valid value.
*[X] Describe platform in terms of functionality*
*[] * Explain and elaborate why you choose this platform*
## The Code
The code depend on libraries in the corse-library at GitHub:
- dht.py for reading temperature and relative humidity from the DTH11 sensor
- mqtt.py for sending sensordata to the computing server
Code base is the provide mqtt code from the course, adapted for my use
After reboot connection over the WLAN is setup, and user is informed (when terminal is connected, e.g. Atom editor)
File **boot.py:**
```python=
# boot.py -- run on boot-up
from network import WLAN
import machine
import json
wlan = WLAN(mode=WLAN.STA)
# network name and password are read from config file
with open('vikinglan_config.json') as inFile:
config = json.load(inFile)
nets = wlan.scan()
for net in nets:
if net.ssid == config['ssid']:
print('Network found!')
wlan.connect(net.ssid, auth=(net.sec, config['ssid_pass']), timeout=5000)
while not wlan.isconnected():
machine.idle() # save power while waiting
print('WLAN connection succeeded!')
# show inoformation about the network connectedt to
mywlanconfig=wlan.ifconfig()
print("=======================================")
print(" WLAN Network information")
print(" My IPv4 adddress is: ",mywlanconfig[0])
print(" My IPv4 Netmask is: ",mywlanconfig[1])
print(" My IPv4 Gateway is: ",mywlanconfig[2])
print(" My DNS Server is: ",mywlanconfig[3])
print("=======================================")
break
```
After booting and network is upp is the payload started. It runns in an endless loop reading sensor every 10th second.
File **main.py**:
```python=
# main.py - program start and execution-
import read_dht
import pycom
import _thread
from mqtt import MQTTClient
import ubinascii
import hashlib
import machine
# read login information for mqtt (user and password) broker from the config file
with open('vikinglan_config.json') as inFile:
config = json.load(inFile)
def sub_cb(topic, msg):
print((topic, msg))
def interval_send(t_):
while True:
send_value()
time.sleep(t_)
def blink_led():
for n in range(1):
pycom.rgbled(0xfcfc03)
time.sleep(0.5)
pycom.rgbled(0x000000)
time.sleep(0.2)
def send_value():
try:
dht_T, dht_RH = read_dht.value()
print('dht temp: ', dht_T) # one byte
print('dht RH: ', dht_RH) # one byte
c.publish(topic_pub,'{"My_Sensor": {"dht temp":' + str(dht_T) +
',"dht RH":' + str(dht_RH) +
'}}')
print('Sensor data sent to mqtt-broker')
blink_led()
except (NameError, ValueError, TypeError):
print('Failed to send!')
# ---------------------------------------------------------
# program flow below
# information that program started, with curren program version
print(" ")
print(">-----------------------------------------------<")
print(" Temp and Humidity over MQTT: version 07")
print(" ")
# local setting for backhaul to broker
topic_pub = 'my-devices/office-sens/'
topic_sub = 'my-devices/office-sens/control'
broker_url = 'mqtt.viking.lan'
client_name = ubinascii.hexlify(hashlib.md5(machine.unique_id()).digest()) # create a md5 hash of the pycom WLAN mac
# connect to local mqtt server
c = MQTTClient(client_name,broker_url,user=config['user_mqtt'],password=config['pass_mqtt'])
c.connect()
# enter the execution loop, second parameter is intervall between sensor reading
_thread.start_new_thread(interval_send,[10])
```
*[X] List program code*
## Transmitting the data / Connectivity
During this POC is temperature and humidity read every 10th second, in the final system, e.i. in the remote house is probably twice minute a more realistitc setting as the temperature in a house does not change that often, this is more to detect problems with the heating due to power-outage.
With the frequent updates (once every 10th second) in the POC it is possible to detect irregularities and other problems with the recorded data - everything is on the internal WLAN and thus no cost. An example on this are the spike att 11:30 in the figure below:

The picture above is from *MQTT Explorer* connected to the MQTT server on the *Pi*. The same spikes on the grafana output:

Anoter reason for this frequent sensor reading is to get a lot of sensor data, both "good" and "bad" data for a small machine-learning project that I also have "in the future stack".
*[X] How often is the data sent?*
*[X] Which wireless protocol did you use (WiFi, LoRa, etc. ...)*
*[X] Which transport protocols were used (MQTT, webhook, etc ..)*
*[] * Elaborate on the design choises regarding data transmission and wireless protocolls.*
## Data presentation
Data is fetched by *telegraf* every 15th second and stored in the database. From the database collects *grafan* data and show it in four graphs for the user as shown below:

- **top** shows temperature, mean value over one minute
- **2nd from top** show delta between two consecutive temperature readings.
- **3d from top** show relative humidity over one minute
- **last graph** show delta between two consecutive rlative humidity readings.
*[X] Provide visual examples
on how the dashboard looks. Pictures needed.*
*[X] How often is the data saved in the database*
*[] * Explain your choise of database*
*[] * Automation/triggers of the data.*
## Finalizing the design
The research items laying at the workbench, in the middle is the pycom with it antenna, the cable is powersupply through the USB-cable. To the right beside the switch is the Pi with the computing power. The WLAN router is 70,1 meter (-77dBm) away according to the android app measuring the signal strength.
In the background can the temperature and humidity test-generator bee seen in the shape of a window ;-).

The project have been fun, a lot of interesting challenges poped up, some faster to overcome, some longer. Python problems was usually faster to handle as interpreter gave good feedback and with a search could this be solved fast.
The sensor problem (I started first with a DHT21, it should have similar interface as an DHT22) took a bit longer, changed to DHT11 and then found out about the updated library. Updated USB driver that did not work with Atom driver and had to be changed back, connectivity between pycom and mosquitto, mosquitto didn't like non-registered users all took some time. The file and directory priviledges with grafana took somewhere in-between in effort.
Conclusion: a fun and very informative course that have given a lot of new knowledge, exactly as it should. ;-);-)
*[X] Show final results of the project*
*[X] Pictures*
*[] * Video presentation*