- # Building a temperature and humidity sensor
My name is Ali Haiderzada (ah225dg)
I have used DTH11 sensor to get the temperature and percentage of the humidity at my home and to see the output of the temperature and RH on pybytes cloud.
It took me afew days after all the lectures to come along and complete this.
The objective was to see if I can get started with my first project which was the DTH11 sensor and then I would be able to send the data to a cloud in order to be able to monitor the temperature and the percentage of RH while I wasn't home. I have a manual humidifier and the given data, helps me to turns it on when needed.
I have used lopy4(figure1) with a pycom expansion board as the development kit and used a DTH11 sensor to get temperature and RH data.
The lopy4 cost me around 1000(kr) Swedish kronor and the sensor DTH11 cost me 100 kr.

figure1. Lopy4
**Installation of the IDE and all necessary plugins.**
I have used Atm IDE to program the Lopy4. To start to program lopy4; first, The firmware should be upgraded to the latest version which these tools can be downloaded from https://micropython.org/download/
1. pycom_firmware_update_1.16.2 (for me until now)
2. zadig-2.5 (to install the right drivers windows version)
then installed atom IDE to upload the code into the lopy4. there are different versions available for different OS. (I used the windows version)
https://atom.io/
After installing the atom-IDE, I then installed Nodes and pymakr plug-in in atom-IDE in order to be able to upload the code into development kit, and that part has to be considered as well.
**Computer setup:**
First of all I connected the lop4 on the pycom expansion board 3.1 in the correct way so the Led side of lopy4 is over the microUSB connector of the expansion board and then connected the antenna to the right side of the led light of the lopy4 in order to connect my board to the internet via wifi and connected the kit in to my computer with a usb cable to power up the evaluation kit.
**Putting everything together**
I used three wires to connect the sensor to the expansion board (figure2)as **DTH11** has 1 ground pin which should be connected to the ground of the lopy4 and the 2nd wire in the middle is power which needs 5 volt to operate and therefore it should be connected to **VIN pin** of the expansion board as it provides **5 volt** and do not connect it to 3.3v.The last wire which is connected to the data pin is connected to the pin3 (p3) on expansion board to send the data.

figure2. Hardware set up
I see the results(temperature 'C' & RH in %) on my local machine(PC) on atom and on the cloud(pybytes) as well.
**Platform**
My local platform was atom IDE and ofcourse, I set this up, to be able to send/upload the data on cloud(pybytes) too.
Both way (locally and online) to program the lopy4 chip and see the result are easy to use and reliable in terms of ease access but I have used atom and then connected my device to pybytes in order to receive and monitor the data(temperature and humidity) at the same time but I love pybytes most, as you can monitor the data from anywhere if you have internet.
**The Code**
(main.py)
```python=
**# Data is sent to Pybytes. Needs to flashed with Pybyte firmware**
import time
from machine import Pin
from dht import DHT # https://github.com/JurassicPork/DHT_PyCom
**# Type 0 = dht11**
**# Type 1 = dht22**
th = DHT(Pin('P3', mode=Pin.OPEN_DRAIN), 0) # sets as input and receives the data
time.sleep(2)
while True: # loops for ever
result = th.read()
while not result.is_valid():
time.sleep(.5)
result = th.read()
print('Temp:', result.temperature) # display results for the temperature
print('RH:', result.humidity) # display result for the humidity
pybytes.send_signal(1,result.temperature) #sends the results to cloud as signal num1 which temperature here.
pybytes.send_signal(2,result.humidity)
#sends the results to cloud as signal num2 which humidity here.
time.sleep(5)
````
-----------------------------------------------
**DHT lib code **
# https://github.com/JurassicPork/DHT_PyCom
```python=
import time
import pycom
from machine import enable_irq, disable_irq, Pin
class DHTResult:
'DHT sensor result returned by DHT.read() method'
ERR_NO_ERROR = 0
ERR_MISSING_DATA = 1
ERR_CRC = 2
error_code = ERR_NO_ERROR
temperature = -1
humidity = -1
def __init__(self, error_code, temperature, humidity):
self.error_code = error_code
self.temperature = temperature
self.humidity = humidity
def is_valid(self):
return self.error_code == DHTResult.ERR_NO_ERROR
class DHT:
'DHT sensor (dht11, dht21,dht22) reader class for Pycom'
#__pin = Pin('P3', mode=Pin.OPEN_DRAIN)
__dhttype = 0
def __init__(self, pin, sensor=0):
self.__pin = Pin(pin, mode=Pin.OPEN_DRAIN)
self.__dhttype = sensor
self.__pin(1)
time.sleep(1.0)
def read(self):
# pull down to low
self.__send_and_sleep(0, 0.019)
data = pycom.pulses_get(self.__pin,100)
self.__pin.init(Pin.OPEN_DRAIN)
self.__pin(1)
#print(data)
bits = []
for a,b in data:
if a ==1 and 18 <= b <= 28:
bits.append(0)
if a ==1 and 65 <= b <= 75:
bits.append(1)
#print("longueur bits : %d " % len(bits))
if len(bits) != 40:
return DHTResult(DHTResult.ERR_MISSING_DATA, 0, 0)
#print(bits)
# we have the bits, calculate bytes
the_bytes = self.__bits_to_bytes(bits)
# calculate checksum and check
checksum = self.__calculate_checksum(the_bytes)
if the_bytes[4] != checksum:
return DHTResult(DHTResult.ERR_CRC, 0, 0)
# ok, we have valid data, return it
[int_rh, dec_rh, int_t, dec_t, csum] = the_bytes
if self.__dhttype==0: #dht11
rh = int_rh #dht11 20% ~ 90%
t = int_t #dht11 0..50°C
else: #dht21,dht22
rh = ((int_rh * 256) + dec_rh)/10
t = (((int_t & 0x7F) * 256) + dec_t)/10
if (int_t & 0x80) > 0:
t *= -1
return DHTResult(DHTResult.ERR_NO_ERROR, t, rh)
def __send_and_sleep(self, output, mysleep):
self.__pin(output)
time.sleep(mysleep)
def __bits_to_bytes(self, bits):
the_bytes = []
byte = 0
for i in range(0, len(bits)):
byte = byte << 1
if (bits[i]):
byte = byte | 1
else:
byte = byte | 0
if ((i + 1) % 8 == 0):
the_bytes.append(byte)
byte = 0
#print(the_bytes)
return the_bytes
def __calculate_checksum(self, the_bytes):
return the_bytes[0] + the_bytes[1] + the_bytes[2] + the_bytes[3] & 255
```
**Transmitting the data / connectivity**
I have used wifi as the wireless protocol and transport protocol is MQTT as it was the built-in setup and the data is being transmitted every 5 seconds
**Presenting the data**
-In the dash board (in figure3) there are two graph one for the temperature and the other one for the % of the humidity.

figure3. presenting the data on pybytes.
-The data is being shown every 1 minute
**Finalizing the design**
The final results:I will improve my work and I have learned how to do the programing in python and how IoT has brought a lot new features. My plan is to expand my project, as of now, I have learned how to set up and run everything from the scratch, and it was the most important part to start and have such experience but there is always things that can be improved and that's my next step to go further and use other benifits of most of the sensors that I have at home.
- [x] Here is another picture on my local computer and IDE.

figure4. Results on Atom IDE
Thank you Fredrik, and all the others in this course who helped me improve my skills in this field.