# Using LoPy for temperature and humidity measurement Author: Jan-Olov Viklund, jv222ni @ LNU This project is a pilot study to see how a microcontroller can be used to measure environmental parameters. I plan to use the same kind of technology to measure and try to improve conditions inside beehives with the aim of reducing the amount of bees dying during winter. Winter losses of bees can be substantial, in average 10 - 15% of hives die. The basic idea with this pilot is to get a general understanding of how to use a microcontroller to capture sensor data and ways to communicate the data. It is a simple setup with the combined temperature and humidity sensor DTH11 and a microcontroller and it can be set up in an hour or so when all hardware and software is at hand. --- ![](https://i.imgur.com/YN0SVjb.jpg) --- ## Things used in this test setup ### Hardware: A microprocessor that handles the signals: Pycom [LoPy4](https://pycom.io/product/lopy4/) 34,95 € For communication between the microprocessor and everything else: Pycom [Expansion board 3](https://pycom.io/product/expansion-board-3-0/) 16 € For connecting the peripherals to the microprocessor: Standard Breadboard [Breadboard](https://www.amazon.de/Elegoo-Breadboard-Solderless-Distribution-Verbindungsbl%C3%B6cke/dp/B01MCRZFE5/ref=sr_1_1_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=breadboard+830&qid=1593510109&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEzVTk5U05aQ1VLR0dZJmVuY3J5cHRlZElkPUEwOTI1MDg0M0VNVE5SODczOTI0MiZlbmNyeXB0ZWRBZElkPUExMDA3MjU2MVRET09JMVBYMjQwNCZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU=) or similar 8,99 € Temperature and humidity sensor: DHT11 In my case from a kit with [sensors from Elegoo on Amazon ](https://www.amazon.de/Elegoo-aufger%C3%BCsteter-Sensormodul-Bausatz-Anleitung/dp/B01M30ZWQR/ref=sr_1_1_sspa?__mk_de_DE=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=37+sensor+kit&qid=1593512287&sr=8-1-spons&psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUExTEdVUzBQMEJEUk1ZJmVuY3J5cHRlZElkPUEwMjQ3OTc5MTRJQlZGT1FETDZaRCZlbmNyeXB0ZWRBZElkPUEwNjA0MzQ3MU1EM1FGVDU2SUxQWSZ3aWRnZXROYW1lPXNwX2F0ZiZhY3Rpb249Y2xpY2tSZWRpcmVjdCZkb05vdExvZ0NsaWNrPXRydWU=) 30 € or a [DHT11 from Kjell&Co](https://www.kjell.com/se/produkter/el-verktyg/arduino/moduler/temperatur-och-luftfuktighetssensor-for-arduino-p87086) 100 SEK Optional for backup power when the pc is disconnected: Battery 3.6 volt with JST connector, size not critical. In my case from local store "Batterilagret in Kalmar". **Note! I had to change polarity in the connector.** ~150 SEK For switching power on and off if a battery is used: [Switch for battery power from Kjell&Co ](https://www.kjell.com/se/produkter/el-verktyg/elektronik/elektromekanik/strombrytare/vippomkopplare/miniatyromkopplare-1-polig-till-till-p36016) 33 SEK ### Assembly: For convenience I mounted it all on a piece of plywood. The expansion board is screwed to the plate with some M5 nuts as distances between board and plate. Simple consoles for switch and antenna are made from standard perforated steel strap I had lying around. The breadboard is mounted with double-sided tape and the switch is soldered to one of the battery leads. It's not a very aesthetical mount, but it serves its purpose. You don't have it all hanging in the cables with the chance of losing contact or worse each time you move it around. ### Computer and software I have used my old "work-horse" for this project. A Dell Precision M4600 running Windows7. I know you people say "update!" But it takes quite a lot of work to do so and in my opinion you are fine with an old OS on an old computer. It is a rather powerful machine with a lot of memory and you can run several virtual machines and databases in parallel without any performance issues. The only real issue is poor Bluetooth compatibility but you can do without. For IDE I have chosen Atom. I looked into VSC as well but the Atom suites me better, you are probably good either way. Go with the one you feel most comfortable with. Whichever you use the [Pymakr plugin](https://docs.pycom.io/gettingstarted/installation/pymakr/#app) give you the contact between the IDE and the LoPy. You get the real time access to the LoPy as well as a way to upload and download your code. The computer is connected to the board with a USB-cable for local communication, programming and transfer of files. Just to try it, I also connected over telnet. It did not work the way it is described at [Pycom](https://docs.pycom.io/gettingstarted/programming/repl/telnet/#app) beacuse the setup in Atom did not accept the LoPy adress on my local WiFi which was 192.168.1.33 and not the default 192.168.4.1. So I opened a cmd-prompt and started telnet the old way, which worked. ## Setting up the LoPy The setup was done as instructed on the [Pycom website](https://docs.pycom.io/gettingstarted/) Since I am using Windows7 it was necessary to install the [Expansion board serial driver](https://docs.pycom.io/gitbook/assets/pycom.inf). After this was done the connection showed up in the Device manager as Com6. No update of firmware was done. Next I installed [Atom IDE](https://atom.io) and the [Pymakr](https://docs.pycom.io/gettingstarted/installation/pymakr/) plugin. I saw the light in [My first project](https://docs.pycom.io/gettingstarted/programming/first-project/) which was to control the LED on the expansion board. Just to try it I also did YMC where you mix the regular RGB to get the complementary colors. ## Register at Pybytes I registered the device as described at [Pybytes](https://docs.pycom.io/pybytes/getstarted/) to use the cloud service. I use WiFi since it is the fastest and easiest way to setup the communication and you don´t have to do any configuration of the MQTT transport protocol to limit the use of bandwidth. I did not have a particular reason to use Pybytes. Only that it felt natural to do that with a Pycom device. It was "good enough" and I did not dig into other alternatives. ## Measurement ### Schematics Ground on the sensors are connected to ground on the expansion board. Vcc on the sensors are connected to 3,3 v on the board. Signal on one sensor is connected to pin 23 on the board and the other one to pin 21. ![](https://i.imgur.com/POC3LPC.png) ### Code I used the simple DHT11 set up as described and with the example code. At first I got very unreliable results and I thought that I perhaps destroyed the sensor by connecting it wrong initially. So I bought another one but still got the same result. It turned out to be the lib-file that was not working properly and once I got that shifted it worked fine. So I connected both sensors to have some comparison. They are sitting on the same board so they should give close readings. ```Python= import time from machine import Pin import _thread from dth import DTH # Type 0 = dht11 # Type 1 = dht22 th1 = DTH('P23', 0) th2 = DTH('P21', 0) time.sleep(2) t=0 rh=0 h=0 def send_env_data(): while True: result1 = th1.read() while not result1.is_valid(): print('sleeping1') #added this line to see if it get stuck here as with the first lib version time.sleep(.5) result1 = th1.read() result2 = th2.read() while not result2.is_valid(): print('sleeping2') time.sleep(.5) result2 = th2.read() print('Temp1:', result1.temperature) print('RH1:', result1.humidity) print('Temp2:', result2.temperature) print('RH2:', result2.humidity) t = result1.temperature rh = result1.humidity h = (38.214 + t * 1.698 + (t**2 * 0.1871)) / 10 #max amount of water in air at given temperature (100% RH) h = round(h * (rh/100), 2) # #grams of moist per kg of air print("Absolut fukt, g/kg:", h) pybytes.send_signal(1,result1.temperature) pybytes.send_signal(2,result1.humidity) pybytes.send_signal(3,result2.temperature) pybytes.send_signal(4,result2.humidity) pybytes.send_signal(5,h) time.sleep(60) _thread.start_new_thread(send_env_data, ()) ``` Initially the readings were made every 5 seconds but I increased this interval to 60 seconds to reduce the amount of data points at Pybytes. After a while I saw that the temp and humidity graphs where very close to mirrors of each other. They both followed the same pattern and the same irregularities in the graphs, only mirrored. So to see if the amount of water in air did really change or stay the same I made an algorithm based on a polynomial extracted from a Mollier diagram. It proved that the amount of moist in the air is changing but not much. Most of the time it stays between 8 and 12 g/kg. This is measured during the warm summer and certainly this value will be very much lower during winter. The close correlation between temp and humidity was unexpected and interesting to see. On the other hand, if the same air is heated and cooled repeatedly without any exchange of water (rain or evaporation), this correlation is what you are bound to get. ## Pybytes ### Signals ![](https://i.imgur.com/7bRS2aT.png) The temperature readings are quite close to each other which is expected. But, the relative humidity differs a lot and I would not use this sensor for serious measurements of humidity. Of course you could calibrate it by also measuring wet bulb temperatures and get correct readings with the help of a Mollier diagram. But it's probably not worth it with this cheap sensor. ### Dashboard ![](https://i.imgur.com/am04NGy.png) The device has been sitting on my balcony for some days now collecting data. You can see the graphs of temperature and humidity and the correlation between them is obvious. I also made a diagram from my calculated value of the amount of water vapor in the air as you can see in the diagram at the bottom. To my surprise the graphs over the data flow shows some irregularities. Since the device is running and sending data continuously it should not look like this. But this being just a test, I didn't dig into the reason behind this unexpected behavior. **Update:** I went back in the log and found unhandled exceptions in the MQTT-traffic. Once in a while it does not work but I will not pursue this further. ## Conclusions This guy (LoPy) was very easy to use for measuring temp and humidity with the DHT11. But for the project I have ahead I need to scale things down. I need smaller size and above all, less energy consumption. The idea is to have a device that is about the size of the Lopy, including a battery for at least 4 months together with sensor and radio chip. My application will consist of a hand full of those measuring devices sending from inside beehives over radio to a common data collecting node. I don't plan to hook it up on the Internet, at least not initially. I'm quite conservative about the security implications it might have. Since the device will sit inside the beehive it will not be accessible during the winter months during which you don't open the hive at all. That's the reason behind low power consumption requirement. Also there is very limited space that you can use inside the hive. You cannot use a drone either since the worker bees kill all drones when winter comes :-) Low energy consumption can be achieved with the LoPy in deep sleep, which is demonstrated in a lab setup at [Pycom](https://pycom.io/l01-oem-module-deep-sleep-test/). I will need to dig a bit further into that to see if it is applicable in my case. If not I will have to look into other options which, in that case, probably will be based on some Arduino variation. A microcontroller in deep sleep is using about 5 uA or even a bit less. A regular button cell battery type 2032 has a charge of 190 mAh. This means it will be sufficient to power the microcontroller for 190 000 / 5 = 38 000 hours = 4,3 years. But that is doing nothing but sleeping. You also have to run it once in a while to have any benefit from it. A microcontroller which is not to power hungry uses about 10 mA when running if it's well optimized. In my case I would make a measurement every hour and as a presumption that takes about 5 seconds, waking up, making the measurement, send the result and going back to sleep. It then would use 5 x 3595 uAs/h for sleep mode and 10 x 5 mAs/h for active mode. Together that gives 67,975 mAs/h which would give 190 * 3 600 / 67,975 = 10 062 hours = 1,1 year in practical use. Even if the sensor and the radio chip will use some energy there is still sufficient energy to keep the device going in a hive over the winter months on a tiny button cell battery. bzzz Kalmar 200702 Jan-Olov Viklund