# IoT Measure Temperatur and humidity By David Pettersson (dp222iz) My tutorial will help you set up a basic tempature and humidity sensor with a LoPy 4, send data to a cloud service, via wifi, where you can display your data and/or set alerts. It will take approximately 2-4 hours to set this up if everything goes smootly. ## Objective If you want to age cheese, store bought or homemade, it needs a certain temprature and relative humidity. The guidlines are 10 degrees celsius and 75% RM. My goal is to find a place in the house thats suitable for aging cheese. My purpose is to measure the temprature and RM where the cheese is being aged to prevent mold growth. It will help me know more precisely know the how much the variables change during the day and also with the weather outside. ## Material * I used the LoPy 4 and a expansion board 3.1 that came with an Lora antenna. With the LoPy 4 you can use up to 4 networks, WiFi, BLE, LoRa, Sigfox, it's programmable with micropython which we will be using. Beacuse we only use wifi in this tutorial a WiPy will work just as well. My bundle costs around 500 sek. ![](https://i.imgur.com/IGtVYH3.png) * You will need some wiring and a breadboard is strongly recommended in the beginning. A breadboard costs around 50sek and wiring 30sek both from electrokit. ![](https://i.imgur.com/DYAjnre.jpg) * The sensor we will be using is a DHT11 sensor. This temperature and humidity sensor module provides a digital serialinterface to measure humidity and temperature. I bought mine from electrokit for 50sek. ![](https://i.imgur.com/QPsnJGi.jpg) ## Computer setup 1. First thing we need to do is to update the firmware on the expansion board. It usally comes updated out of the box but its best to update it anyways. Pycom has a great guide in the [documentaion](https://docs.pycom.io/pytrackpysense/installation/firmware/) OBS! If you intend to use LoRa or Sigfox you need to connect the antenna before you try send any data otherwise you risk damaging the device. 2. Now its time to assemble your LoPy 4 to the expansion board. Make sure that the pycom next line up on both the expansion board and the LoPy4 module and gentle press them in place. 3. With the device in place we now need to update the LoPy 4 firmware to the latest version. Pycom have a simple tool and guid for that. You can find it [here](https://docs.pycom.io/gettingstarted/installation/firmwaretool/) 4. Now you need to choose a IDE. I used [atom](https://atom.io/download/windows_x64) its very light weight and easy to use. You can also use Visual Studio Code. To connect with our pycom unit we need to install a plugin called PyMakr. You can find the documentation for it [here](https://docs.pycom.io/pymakr/installation/) Now when you've installed PyMakr you can simple connect your device to the computor and it should be found automaticlly. When the device is connected, 3 arrows should start blinking and you can now start to program the device after your needs. You should have 2 files in your project folder. One main.py file that will contain the code that will running on your device. You can also have a boot.py file. It will be the first thing that run when you start the device but it's not mandatory. You will also need a folder called lib. This will be your library where you keep files that run every sensor that you have. ## Putting everything together With the DHT11 sensor the left pin with an S next to it is the signal pin. In out case it should be connected to P23. The middle pin is power and should be connected to 3V3. The sensor can take 3.3V to 5.5V. The right pin is ground and should be connected to GND. Follow the picture below. ![](https://i.imgur.com/DLhk31Y.png) ## Platform I started to send my data to the pybytes platform. Its very simple and easy to do so its a great first step. It does not if you that many options on how to present your data in a nice way. I then switch over to ubitdots. Ubidots is a free cloud-based platform. It does have a limit on how many times each day that you can send data. It caps at 4000/24h. That means you can send data once every 22 seconds which is more then enought for us. Plese sign up [here](https://ubidots.com/stem/) to get started and to get your token. ## The code You can find my complete code [here](https://github.com/Daave11/IoT-Project) ```python= TOKEN = "Token here" #Put here your TOKEN DELAY = 900 # Delay in seconds th = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0) # DHT sensor is connected to port 23 time.sleep(2) # wait 2 seconds ``` Here is where you should put your ubidots token. If you used another pin you can change it here also. Choose how often you want to send data. Im sending every 15min. ```python= # Assign your Wi-Fi credentials wlan.connect("wifi-name", auth=(WLAN.WPA2, "wifi-password"), timeout=5000) ``` I will be sending data via wifi so you need to enter the name and password to the wifi you want to connect to. ```python= # Builds the json to send the request def build_json(variable1, value1, variable2, value2): try: data = {variable1: {"value": value1}, variable2: {"value": value2}} return data except: return None ``` Here we are building our JSON. You can change the amount of varibles you would like to send. ```python= def post_var(device, value1, value2): try: url = "https://industrial.api.ubidots.com/" url = url + "api/v1.6/devices/" + device headers = {"X-Auth-Token": TOKEN, "Content-Type": "application/json"} data = build_json("Temp", value1, "RM", value2) if data is not None: print(data) req = requests.post(url=url, headers=headers, json=data) return req.json() else: pass except: pass ``` This is the request we are sending to ubidots. ```python= while True: result = th.read() if result.is_valid(): temp = result.temperature # Data values rm = result.humidity # Data values post_var("pycomTemp", temp, rm) time.sleep(DELAY) ``` Our while loop is always up and running while True. We read the sensor data and make sure its valid. Then we simple send the data to ubidots calling the post_var function. ## Transmitting the data / connectivity Im sending my data every 15min. Temprature does not change so fast in these settings so no need to measure more then that. We use a JSON-Object to package our data. The data is then sent to Ubidots using webhooks(HTTP request) over wifi. ## Presenting the data When the data is in ubidots platform you can easily create your own dashboard like below. The data is saved everytime we upload it and is stored in the Ubidots database 30 days for free. ![](https://i.imgur.com/hiztKdl.png) ## Finalizing the design I achieved everything that i wanted with this project. It would be fun to send the data over LoRa instead but i dont have a gateway close. Also my Sigfox connection was not very good. I've leared a lot and are already looking for new projects to start! ![](https://i.imgur.com/oF5eiuC.jpg)