ql222ai
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `PYCOM` `lopy4` `ubidots` `MQTT` `Iot` # Tutorial on how to build a Pycom LoPy4 Storeroom Monitoring System ##### By Qing Lin (ql222ai) <div style="text-align: justify"> This project is mainly aim to exploring about Internet of things Iot by built a storeroom monitoring system, using a Pycom LoPy4 module together with 3 temperature & humidity sensors, This storeroom monitoring system is designed to gather sensor values once every 5 minutes. All collected values were sent to Ubidots IoT cloud platform through Message Queuing Telemetry Transport (MQTT). This system offers both email and sms notifications to alert user while temperature or humidity of storeroom reach certain critical level(trigger values are pre-setted by user). <br /> Estimated time for reproducing this project will be 10~20 hours if you have all related hardware. </div> ## Objective <div style="text-align: justify"> Many families have storeroom(could be cellar) for storing food drinks tobacco and other stuffs, many of these items are sensitive with temperature and humidity, for instance, wine should be preserved at a constant temperature set between 10 – 16˚ C and relative humidity in a wine cellar must be between 50% and 80%. Some type of tobacco should be place where the ambient air should be about 70-75% relative humidity and 24-29°C. Typically, the owner of storeroom and cellar doesn't want to put much attention on the it unless rooms require some maintenance. So this is why i built this system to monitoring the storeroom. The user **only** need to pay attention to storeroom if temperature or humidity is out of setted range which means the humidifier or air system doesn't working properly. User will be alerted by sms or email notification.-----as an user, i don't want pay too much attention to the storeroom, but i don't want my stuff turn bad either, This monitoring system gonna keep an eye on my storeroom for me. </div> ## Material List of material for this project: | Material | Quantity | Purchased | Price | | :-- | :-- |:--:|:--:| | LoPy4 and sensors bundle | 1 | [Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-and-sensors-bundle/) | 949 SEK | | DHT11 Temperature & Humidity Sensor | 3 | [Electrokit](https://www.electrokit.com/produkt/digital-temperatur-och-fuktsensor-dht11/) | 147 SEK | #### LNU – 1DT305 Tillämpad IoT – LoPy4 and sensors bundle This is the LoPy4 and sensors bundle supplied by Electrokit online store which includes: * LoPy4 with headers * Expansion board * Antennae * Micro USB cable * Jumper wire * Breadboard * 10 x Resistor1 kohm * 10 x Resistor 10 kohm * 10 x Resistor 330 ohm * 10 x Resistor 560 ohm * 5 x LED red * 5 x LED orange * 5 x LED green * 2 x LDR * Tilt switch * Temperature sensor MCP9700 * Hall-effect sensor TLV49645 * Magnet #### LoPy4 and expansion board 3.1 <div style="text-align: justify"> The LoPy4 is Micropython-programmable quadruple bearer board. It works with LoRa, Sigfox, WiFi and Bluetooth, making it an exceptional enterprise-grade IoT platform. It is the core module of this project and the programming language for it is MicroPython. Development tool for Lopy4 and other pycom modules is the Pymakr plugins which also offers fast IoT application development experience. The expansion board 3.1 is required to offers I/O for lopy4 module, in this project we mainly use it to connect to PC and sensors. The wireless data connection for this project is Wi-Fi. </div> ![](https://i.imgur.com/d8qqCNE.png) #### DHT11 Temperature & Humidity Sensor <div style="text-align: justify"> The DHT11 sensor is used to measure the tempature and airhumidity in the area around the sensor, this project uses 3 DHT11 sensors since a storeroom need multiple sensors installed in different area to make sure there is no blind area for measurement. This could be of importance if the item is sensitive to lower or higher tempatures and humidity. </div> ![](https://i.imgur.com/ftbbCWe.jpg) #### Wires and breadboard The wires are used to connect the sensors to the Lopy4 and expansion board. The supplied breadboard is used to make wiring easier. ![](https://i.imgur.com/QFZcsiz.jpg) ## Computer setup <div style="text-align: justify"> Windows 10 will automatically install driver once the expansion board connect to PC through USB wire. For Windows 7, a driver would be requied to support the expansion board. From <a href="https://docs.pycom.io/gettingstarted/software/drivers/">this link</a> you could found detail about install windows 7 driver of pycom expansionboard. If you use an old version of pycom Expansion board, It might requires a firmware update to make sure lopy4 module could working correctly. Otherwise, The firmware of expansion board 3.1 should be up to date. The instruction for update fireware pycom expansion board could be found <a href="https://docs.pycom.io/updatefirmware/expansionboard/#app">here</a> . The offical IDE for pycom development is a plug named "pymakr", which working with both ATOM and Microsoft Visual Studio Code. My choice was **Visual Studio Code** since i am familar with this editor. You can download Visual Studio Code from <a href="https://code.visualstudio.com/download">here</a>. Note that you will also need <strong>Node.JS</strong> installed on your PC. Please download the latest LTS version available from the <a href="https://nodejs.org/en/">Node.JS offical website</a>. Once Visual Studio Code has been success installed, open Visual Studio Code and navigate to the Extensions page, using the 5th button in the left navigation, Search for Pymakr and click the install button next to it. ![](https://i.imgur.com/gw5xCP5.png) </div> After the install finished, you will see the following scene when open VSCode ![](https://i.imgur.com/2OQvxu6.png) for more detail of pymakr, you can check this [link](https://docs.pycom.io/gettingstarted/software/vscode/). To upload your code to pycom device, first you need connect your pycom device to your pc through Serial USB, then start pymakr and click **upload** button as showing blow ![](https://i.imgur.com/p5dFTKB.png) ## Putting everything together <div style="text-align: justify"> First of all, make sure the breadboard isn’t connected to a power supply while connecting sensors(here namely the power supply is the usb cable to most people,make sure unplug the usb cable from computer before you connecting sensors). </div> <div style="text-align: justify"> These <strong>signal wires</strong> are connected to the pycom device <strong>PIN 23 PIN 22 and PIN 21</strong>. These 3 sensors are power supplied by <strong>3V3 or VIN</strong>. The <strong>white wires</strong> are connected to <strong>GND pin</strong> on expansion board. The circuit diagram below shows how I wiring sensors and expansionboard. </div> ![Fig Circuit diagram](https://i.imgur.com/ugaEPeF.jpg) ## Platform <div style="text-align: justify"> In this project, I have tested and evaluated two different platforms: pybytes and ubidots. Pybytes is the offical default platform of pycom, it is easy to setup, lopy4 has build in lib to support pybytes connections. To connect to pybytes,user only need to configure the wifi and sigfox connection and data package format. but for this project pybytes could not supply easy used trigger functions.so i try ubidots instead since it supplies free and easy-to-use trigger function. <a href="http://ubidots.com">Ubidots</a> is an online cloud solution and offers free account to STEM students. To me, The configuration and upload/download values from the database functions on ubidots are pretty easy to use and setup. Further more, there are many possibilities when it comes to displaying the information since the dashboard has a various of flexible functions. Ubidots also offers some potentional possible for future development. I plan to develop android client to communicate with ubidots' mqtt broker. As a web developer, i personlly prefer the doc of ubidots. </div> ## The code In this project, two libs have been used for sensor and network communcation, they are: | Library | Source | | :-- | :-- | | MQTT |[mqtt.py](https://github.com/pycom/pycom-libraries/blob/master/examples/mqtt/mqtt.py)| | DHT11 |[DHT11.py](https://gitlab.lnu.se/1dt305/sensor-libs/-/tree/master/DHT11%20%26%20DHT22%20-%20Humidity%20%26%20Temperature%20Sensor)| <p>boot.py for start build Wi-Fi connection while booting the lopy4 module</p> ```python= # boot.py -- run on boot-up from network import WLAN import machine import keys wlan = WLAN(mode=WLAN.STA) nets = wlan.scan() for net in nets: if net.ssid == keys.wifi_ssid: print('Network found!') wlan.connect(net.ssid, auth=(net.sec, keys.wifi_password), timeout=5000) while not wlan.isconnected(): machine.idle() # save power while waiting print('WLAN connection succeeded!') print(wlan.ifconfig()) break ``` <p>Wi-Fi AP SSID and password, Ubidots token,Ubidots device id, all these information are store in a file named <strong>keys.py</strong></p> ```python= wifi_ssid = "your router/ap ssid" wifi_password = "your wifi password" ubidots_token = "your ubidots token" ubidots_id = "your ubidots device id" ``` <p><strong>main.py</strong> this is the main logic code for lopy4 module</p> ```python= import time import keys import pycom import machine from machine import Pin from network import WLAN from dht import DHT from mqttclient import MQTTClient wlan = WLAN(mode=WLAN.STA) # if not connected to wifi, set machine as idle/inactive while not wlan.isconnected(): machine.idle() #MQTT callback def sub_cb(topic, msg): print(msg) # Connect to MQTT client in ubidots and connect # Use your token (accessed on Ubidots webpage) and leave password empty. # Port is 1883 and device_id is found in config file. client = MQTTClient(keys.ubidots_id, "industrial.api.ubidots.com",user=keys.ubidots_token, password="", port=1883) client.set_callback(sub_cb) client.connect() # Three DHT11 sensors connected to P23 P22 and P21 dht1 = DHT(Pin('P23', mode=Pin.OPEN_DRAIN), 0) dht2 = DHT(Pin('P22', mode=Pin.OPEN_DRAIN), 0) dht3 = DHT(Pin('P21', mode=Pin.OPEN_DRAIN), 0) def dht_reader(dht): time.sleep(2) # Just to get it some slack starting up result = dht.read() # Make a reading while not result.is_valid(): # reread if the result is not valid print("Fail") time.sleep(5) result = dht.read() return result while True: s1 = dht_reader(dht1) s2 = dht_reader(dht2) s3 = dht_reader(dht3) # Use onboard LED to warning user while temperature is out of range # the parameter is the temperature range in Celsius, which is set by the user # depends on the type of storages,for instance, for wine, it is 12 – 15 °C if s1.temperature>28 or s2.temperature>28 or s3.temperature>28: print("The temperature is too high!") pycom.rgbled(0xFF0000) # RED LED elif s1.temperature<10 or s2.temperature<10 or s3.temperature<10: print("The temperature is too low!") pycom.rgbled(0x0000FF) # BLUE lED client.publish("/v1.6/devices/lopy4-storeroom", '{"sensor1-temperature": ' + str(s1.temperature) + ', "sensor2-temperature": ' + str(s2.temperature) + ', "sensor3-temperature": ' + str(s3.temperature) + ', "senor1-humidity": ' + str(s1.humidity) + ', "senor2-humidity": ' + str(s2.humidity) + ', "senor3-humidity": ' + str(s3.humidity) + '}') # change it to 300000ms(5 minutes) for deployment time.sleep(900) ``` ## Transmitting the data / connectivity <div style="text-align: justify"> For this project I have chosen to transmitting the data through MQTT protocol with WiFi connection. Because the designed deployment environment of the storeroom monitoring system is home environment, Wi-Fi becomes a natural choice due to its stable connection and low cost. Nowdays most families have Wi-Fi network with Internet connection. User doesn't has to pay for extra cost. In contrast, Sigfox and Lora connection aren't free and One of the key problem of Lora/Sigfox communication system has to settle is the coverage efficiency., the signal of Lora and Sigfox are not good enough in remote region , further more the bandwidth of these connection is limited as well. Of course, LoRa and Sigfox connection could be decent solution if you need your device to be distributed and installed in area Wi-FI is not available or you need to powered this system with battery-- Wi-Fi consume much more power than Lora and Sigfox do. MQTT becomes the selected tranmission protocol due to its lightweight data package and subscriber-broker-publisher natural. MQTT was designed for Iot network. I plan to expand the system in the future to add some more clients and sensors, To server this purpose, MQTT is a better choice than webhooks,To add a new client to this system, i only need to subscribe to the mqtt topic from ubitdots' broker. The storeroom monitoring system sends sensor values to the ubidots cloud platform every 5 minutes. All values are stored in Ubidots database. To get these sensor data from Ubitdots, the only thing you need to do is to connect your client app or device to Ubidots MQTT broker and publish to your own account/device and Ubidots will handle the rest for you. This makes the development really effective. </div> ## Presenting the data These measured data were sent to ubidots cloud platform and the visual statics informations are available there as well, users are able to customized the widget according to their need. history data are also available. ![](https://i.imgur.com/WfrTKat.png) ![](https://i.imgur.com/a7ktV4b.png) <div style="text-align: justify"> These values are autumatically stored in Ubidots database every time ubidots receive new sensor data from lopy4 module. Ubidots has a limit to how much you can upload per day and a **1 month data retention**. Further more, I have chosen to set two triggers for notifications (sms and email) depends one of the sensors' temperature over 30 degrees celsius and humidity lower than 60%. These triggers' condition parameter are just for test purpose. In deplyment, they should be setup base on the type of storage( drinks, food, tobacco require different humidity and temperature store condtions) </div> ![](https://i.imgur.com/FLMMqJm.png) <img class="fit-picture" src="https://i.imgur.com/FLMMqJm.png" alt="hackmd's markdown sucks"> <div style="text-align: justify"> Ubidots doesn't supply any details about how does its database working. Therefore, I have no idea about the tech detail of this "blackbox", but this database is easy to use, as an user i didn't have to configure anything which saved me a lot time. Otherwise, I would choose to create a Google firebase database to subscribe the mqtt topic from ubidots mqtt broker, and connect it to web or mobile app client. </div> ## Finalizing the design <div style="text-align: justify"> This project went well without any issue. This was my first Iot project with internet data exchange. For scaling this project some soldering works are expect to extend the length of sensor wires so i could actually deploy these sensors in different rooms or different positions instead of squized them together on a tiny breadboard, A 3D printed case is desired as well. However, due to budget these are not achieveble at this moment. But for software part, I would considering about implement the server, database and client all on my own with much better flexibility, the only problem is the trigger, it is really hard to find reliable sms email or voice call notification apu,especially free one. Otherwise, This was a fun and manageable summer project! It delivered the sensor measurement and cloud based data visualization function as expected. </div> ![](https://i.imgur.com/c3KvnV1.jpg)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully