# Know when to water your plant and how your room feels using a Pycom device *a tutorial written by Simon Danielsson (sd222xc)* ## Project overview This project tutorial will give you a guidance on how to create and build a pycom device that is measuring your soil moisture, temperature and humidity. Using the pycom Pysense 2.0x and FiPy we will go through various intresting things. Things as how to connect to Wifi, how to send and collect data using MQTT and adafruits. We will also learn how to gather data from three different types of sensors as well as how to alert the user using discord and webhooks. ### Time approximation Considering the fact that I completed the the project somewhere between 5-8 hours with programming knowledge and not IoT knowledge, I would say that this project could take between 3-10 hours. It depends on what knowledge you possess. ## Objective ### Why this project? I have always been intrested in software as I have made multiple hobby projects using my programming knowledge. Funny games, antivirus program, scheme maker and so on. But, I have never really studied hardware and I had absolutely no knowledge about it. So when I chose this project I had some goals: * Deeper understanding about hardware. * Learn how to combine hardware and software * Learn how to send data using a connectivity method * Make my home smarter * Learn to use weebhooks ### What is the purpose? The purpose of the project is to make your home smarter. The device should measure the temperature and the humidity at its current location and also measure the soil moisture in a plant of the users choice. The device should likewise alert the user when the plant needs water. ### What insights will creating the project give? * Gives the user knowledge about pysense 2.0x and fipy and how to connect sensors to the hardware will be reviewd. * The user will learn how to gather data from sensors. * Gives the user knowledge about connectivity around IoT, specifically Wifi connections. * Knowledge about firmware updates and development envoirment for pycom devices. * Insights in a IoT data visualization. Adafruits will be covered in this turtorial. ## Material | Item | Amount | desc/Purpose | Price | Link to buy | | ------------- | -------- | --------------------------------------------------------------- | ------ | ----------------------------------------------------------------------------------------- | | Fipy | 1 | Microcontroller | €59.40 | [pycom.io](https://pycom.io/product/fipy/) | | Pysense 2.0x | 1 | Sense shield | €29.65 | [pycom.io](https://pycom.io/product/pysense-2-0-x/) | | Breadboard | 1 | Very Usefull connection tool | €6.31 | [electrokit.com](https://www.electrokit.com/produkt/kopplingsdack-400-anslutningar/) | | FC-28 | 1 | Soil moisture sensor | €3.10 | [electrokit.com](https://www.electrokit.com/produkt/jordfuktighetssensor/) | | MCP9700 TO-92 | 1 | Temperature sensor | €0.86 | [electrokit.com](https://www.electrokit.com/produkt/mcp9700-e-to-to-92-temperaturgivare/) | | Jumper Wire | 1 bundle | Connection wires | €3.10 | [electrokit.com](https://www.electrokit.com/produkt/labbsladd-20-pin-15cm-hane-hane/) | | Batteryholder | 1 | Provides power to the iot device | €3.10 | [electrokit.com](https://www.electrokit.com/produkt/batterihallare-3xaaa-med-strombrytare-och-jst-kontakt/)| ### Material images with deeper description | Image | Descripton | |:------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | | ![](https://i.imgur.com/QJXON6r.png) Fig. 1. Fipy microcontroller | A microcontroller. In other words, a small computer. | | ![Pysense](https://i.imgur.com/8GGEsEy.png) Fig. 2. Pysense 2.0x | A shield containing different types of sensors. In this project we will only use one of the sensors. The sensor that measures the humidity. | | ![](https://i.imgur.com/KOd6H0T.jpg) Fig. 3. Breadboard | A breadboard that will make your electrical connections between sensors and the microcontroller easier. | | ![](https://i.imgur.com/xK75zh6.jpg) Fig. 4. Soil Moisture sensor, FC-28 | A sensor measuring the soil moisture. It has both an analog output and a digital output. | | ![](https://i.imgur.com/bcnucYa.jpg) Fig. 5. Temperature sensor, MCP-9700 TO-92 | A sensor that measure temperature. It only has an analog output. | | ![](https://i.imgur.com/ljS1ovl.jpg) Fig. 6. Jump wires *(male-male)* | Jump wires for electrical connections, male-male endings. | | ![](https://i.imgur.com/ZPd0U51.jpg) Fig. 7. Battery holder with JST-connection | A batteryholder *(JST-connection)* that will provide power to your device. | ## Computer Setup ### Prerequisites Please note my computer specifications. As the download might be different depending on what system/computer/operating system you have. Operating system : Windows 10 System : x64-system ### Node JS Node.js is a javascript runtime needed for a plugin. Install the current version also known as the latest fearure. However the LST version can be the most stable one. It did not work for me, therefore the current version was used and installed : ![](https://i.imgur.com/sl5mAoS.jpg) ### Atom as IDE For this project you will need an integrated development envoirment *(IDE)*. The IDE that will be covered in this guide is ATOM. #### Step 1 : Download Atom at this [link](https://atom.io/). #### Step 2 : Open Atom, it should look somewhat like this : ![](https://i.imgur.com/EMWJtia.png) #### Step 3 : Download pymakr package in atom. First navigate to install a package and click on it : ![](https://i.imgur.com/iNG4Hz2.png) Then click on "Open installer" and afterwards download pymakr. I'll try cover it on a picture : ![](https://i.imgur.com/lFOhJRW.png) #### Step 4: Check if pymakr is installed. Should look like this : ![](https://i.imgur.com/W500tCa.png) Congratulations, you have now succesfully installed Atom with the pymakr package! ### Firmware update Before making a project you should always keep your firmware up to date. In our case we have two different firmware to update. The firmware of the Fipy and the firmware of the Pysense. Either way we will start with installing the Pycom firmware update tool. #### Download the device tool Download the Pycom firmware update tool at this [link](https://software.pycom.io/findupgrade?product=pycom-firmware-updater&type=all&platform=win32&redirect=true) #### Update the Fipy ##### Step 1 : Connect your hardware as the picture shows below : ![](https://i.imgur.com/4F0nJSH.png) ##### Step 2 : Connect your device to your computer using a USB cable. If u don't have any usb cabel. You could buy it [here](https://www.electrokit.com/produkt/usb-kabel-a-hane-micro-b-5p-hane-1-8m/) for €4.17 euros. ##### Step 3 : Start the firmware update tool. Press continue and then continue again. ##### Step 4 : Now you should be able to select port and type. Port might change depending on Expansion board but I had port COM3. And the type should be selected to development. It should look like the image below : ![](https://i.imgur.com/7hkBdgR.jpg) ##### Step 5 : For the last type just follow the guide image. Should do enough : ![](https://i.imgur.com/eu2rovq.jpg) If update was successfull, then we'll move on! #### Update the Pysense 2.0x To update Pysense 2.0x it will get a bit more complicated. But lucky for us pycom themselves have posted a guide on how to update expansion boards. I will link it [here](https://docs.pycom.io/updatefirmware/expansionboard/). This step is not always mandatory beacause it could already be up to date *(for me it was)* . So if problems occurs, hopefully this will help. ### Upload code (workflow) Worth mentioning is also how to upload your code. We will follow that in the next x steps below : #### Step 1 : Open up Atom and make sure your device is connected to atom! Should look like this *(my device is com3 but it might be different from yours)* : ![](https://i.imgur.com/fsnefyL.jpg) The highlighted spots indicates connection between your device and atom. You can also try to use the REPL *(terminal)*. For example, print("Hello World". Should give you "Hello World" in return. #### Step 2 : Make a project or open an existing folder. To open an existing folder navigate to top left "file" section and the choose between file, folder or project. It all depends on your needs : ![](https://i.imgur.com/pFb5tnc.jpg) #### Step 3 : To create a new folder or file just right click somewhere at the left bar then press "new file" or "new folder". Picture below : ![](https://i.imgur.com/bOTGgtO.jpg) Arrow at number 1 migh be unclear. But it means right click to get the menu up. #### Step 4 : Last step! It is basically the step where you are going to upload the code to the device. This guide is assuming that no errors in code exists and that the firmware is updated. Therefore errors related to firmware/code problems will not be covered. [Google](https://www.google.com) is your best friend *(sounds easy but it really is true)*. However, to upload your project/folder/file the only thing you need to do is to click the "Upload project to device" button. You can click it manually or use "ctrl+alt+s". Picture below : ![](https://i.imgur.com/XdLLwJR.jpg) ## Putting everything together Inorder to access our FC-28 and MCP-9700 TO-92 sensors we need to expose som pins on the device. Therefore we need to disconnect the fipy from the pysense. Credit to LNU applied IoT who have made an excellent guide on how to do this and still have access to all pysense sensors. ### Connections Using pysense we get limited access to pins if the Fipy is connected to it. Therefore in this project we will disconnect the Fipy from the pysense to gain some more pins to work with. More pins in our case means more access to sensors. ### Circuit Diagram ![](https://i.imgur.com/ubSI1mZ.png) ### Further description When doing this project, please check the wirering multiple times before providing it with power to avoid damage anything. In terms of resistors and voltage, we use 3.3 voltage for the MCP9700 TO-92 sensor and FC-28 sensor. For the inbuilt sensor in the pysense my guess would be 5 voltage is used. The 3.3 v is used to avoid damage the sensors and they do not need any more than 3.3 v *(it may need less)*. The resistor is just used for more security *(to avoid burning stuff)*. More resistor could be something to implement. ## Platform The platform used will be the cloud based [Adafruits](https://io.adafruit.com/). There is different reasons why I chose adafruits. The first reason was beacause of the well documented MQTT procoll guide on how to use MQTT with adafruits. Learning MQTT was definitely something I wanted to do. The second reason was the easy use of weebhooks. My goal at the start was to learn how to use weebhooks and Adafruits gave me that opportunity. The last reason was simply the dashboard. Looking at other platform's dashboard, I just liked Adafruits more. I am using the free version of adafruits which means 30 days of saved data. I will attach a picture below representing the free version plan : ![](https://i.imgur.com/lPdSoCm.png) ## The code ### Structure ``` /lib/adafruit.py /lib/mqtt.py /lib/pycoproc.py /lib/S17006A20.py boot.py main.py ``` | Files | description | | ----------------- | ------------------------------------------------------------------------------------------------------------- | | /lib/adafruit.py | Needing functions to send data to adafruits | | /lib/mqtt.py | Needing functions to use the MQTT protocol | | /lib/pycoproc.py | Needing class to create a pycoproc object to gather pysense inbuilt sensor data | | /lib/S17006A20.py | Needing library to gather Humidity,dew point, and temperature sensor data | | /boot.py | When the device starts boot is executed. This file contains wifi connections to assure connectivity at all times. | | /main.py | The main code itself. The code that is using all the library to both gather data, send data and save power. | ### Code explanations We now know what all the files do. In this turtorial I will only show code snippets that exists in /main.py and /boot.py. My overall guess is that the library is not that intresting, it is more intresting to know what you are doing with the library. In this guide we will go through three different code snippets. Each one has a different purpose than the other. #### Code snippet 1 : ```python= # Temprature measurment using the MCP, analog value. adc = machine.ADC() temperaturepin = adc.channel(pin='P16') millivolts = temperaturepin.voltage() degCelsius = (millivolts - 500.0) / 10.0 # Soilmoister measurment using FCP-28, dialog value. 1 for dry , 0 for wet pin_input = Pin('P14', mode = Pin.IN) soilValue = pin_input.value() if(soilValue == 1): soilValue = "the soil is dry! Plant need water asap" else: soilValue = "the soil is perfect :D" print("Soilmoister : " + str(soilValue)) print("Temperature : " + str(degCelsius) + " C") ``` So in this snippet we are gathering data from two different sensors. The FC-28 and the MCP-9700 TO-92. The FC-28 is sending a digital value to us. That is easy to read beacuse it only contains number 1 or number 2. As u can see when the soil is dry it sends 1 otherwise it sends the value 2. Now we have gotten the sensor data! But for the temperature sensor it is a bit different. It gives us an analog value. When working with analog values you need to have a deeper understanding about the sensor. Through the sensors [datasheet](https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/20001942G.pdf) we learned a formula used on row 5. For more insight, please read the sensors datasheet. #### Code snippet 2 : ```python= ## Configuring data that should be sent to Adafrutis #All Feeds feeds = ["Kulan/feeds/temperature", "Kulan/feeds/dew-point", "Kulan/feeds/soilmoisture", "Kulan/feeds/humidity"] feed_values = [degCelsius, dew_point_value, soilValue, humidity_value] index = 0 client = connectAdafruit() for feed in feeds: sendDataToAdafruits(feed,feed_values[index],client) index += 1 disconnectAdafruits(client) ``` In this snippet we are storing the sensor values in a list *(row 5)*. With the help of the adafruits library we are using different functions, row 7, 9, and 11. They are doing exactly as their names describe. The loop between 8 and 10 is the loop sending all data to adafruits. #### Code snippet 3 : ```python= SLEEP_TIME = 60 #In minutes print("pycom device going to sleep") py.setup_sleep(SLEEP_TIME*60) py.go_to_sleep() ``` One of the most important codes in the main file. In this project I want to send data every hour without consuming to much power. Therefore turning of the device between the hours would save a lot of power. Not to forget, we are using a batteryholder aswell. Therefore after gathering data and sending it we are telling the device to go to deepsleep for one hour. After one hour the device will then wake up, collect and send then go back to sleep. This means that we can manage the power usage and use it when we really want to use it. Feel free to change the "SLEEP_TIME" depending on your needs. #### Connecting to Wifi : I felt that connecting your device to Wifi would be worth mentioning. I will recommend LNU's applied IoT turtorial called "Part 3: Using Wifi" [here](https://hackmd.io/@lnu-iot/SJ8TGsUd5). All credits to the LNU applied IoT group, well done with the turtorial! ## Transmitting the data / connectivity ### Data,Wireless,Transport As mentioned in the code part the code is sending data to adafruits every one hour. The wireless protocal used in this project is Wifi and transport protocol used is MQTT and webhook. ### Why? The decision to send data every hour was made beacause of two main reasons. I did not need data every 10 seconds or 10 minutes. Every hour was enough for me. It did not feel necessary to know my temperature,humidity or when my plants needed water any lower than every hour. I might even extend that one hour to five hours. The second reason was beacause of the battery consumption. Less sending and gathering data means a lower battery consumption. Which longer life time for my batteries used. Wifi was mainly chosen beacause I did not need the distance and I found an easy guide to follow *(linked above)*. But after making the project and learning more about IoT I would prefer bluetooth. Using bluetooth would entail less battery consumption which once again means longer liftime for my batteries. Also I am not really sending large data therefore bluetooth would be a better connectivity option for this project. ## Presenting the data ### Adafruit dashboard ![](https://i.imgur.com/SUy1CjV.png) We have three different linechart presenting humidity, dew point and temperature for a week time. Also a text is presented pointing out when the chosen plant needs water or not. Why Adafruits was chosen has been explained earlier in this turtorial and data is saved every one hour. ![](https://i.imgur.com/nSEVlcV.png) Picture showing scenario when plant does not need water. #### What is dew point? Something I wanted humidity to represent was how muggy the air would feel. The higher value , the muggier would it feel. But googling around, I learned that relative humidity can be misleading therefore dew point was also included in this project. For more information about this, read [here](https://www.weather.gov/arx/why_dewpoint_vs_humidity). ### Adafruit trigger Something I had in my mind doing this project was that I wanted something to notify me when I needed to water my plant. I ended up using webhooks and discord for that matter. In adafruits I navigated to "Actions << New action" and then created my trigger using my discords webhook.![](https://i.imgur.com/JkSQBHK.png) Then when my soil moisture feed value was equal to dry I triggered the action to notify me on discord. Image below shows it : ![](https://i.imgur.com/6eDZnFr.png) ## Finalizing the design ### Last words Overall , the project was extremely fun to create. I will definitely keep exploring and learning about IoT. But at the same time, there were ups and downs. One problem I remember clearly was when I tried measure temperature. It gave me very high values, 35-50. It all came down to that my fipy was to hot so the temperature sensor near the fipy actually was betwen 35-50. I guess that is what IoT is about, learn by doing. However , I think using bluetooth would be a better choice as a connectivity method and next time making a project, I will definitely learn more about securtiy and privacy and implement it to my project. In this project I did not really focus on the security part. Thanks for following this turtorial, hope it went well. Take care ! / Simon ### Pictures of the done project ![](https://i.imgur.com/fns5P68.jpg) ![](https://i.imgur.com/mzllKef.jpg) ### Funny and useful links :D [Youtube link](https://youtube.com/shorts/ENkJ97Ul4fI?feature=share), short video on when the device starts. [Github](https://github.com/Ku1an/IoT-Summer-project-code), basically all the code used.