Upgrade to Pro — share decks privately, control downloads, hide ads and more …

IoT Experiments: Temperature Sensing

stwn
June 03, 2021

IoT Experiments: Temperature Sensing

stwn

June 03, 2021
Tweet

More Decks by stwn

Other Decks in How-to & DIY

Transcript

  1. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 1 Iwan Setiawan
    IoT Experiments:
    Temperature Sensing
    +Humidity

    View Slide

  2. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 2 Iwan Setiawan
    Previous Week’s Material

    Electronics refresh
    – Components
    – Tools: Fritzing
    – Prototyping

    LED switching experiments
    – Python and GPIO Zero
    – Mu editor

    Traffic lights simulation Analog
    signal
    Compute
    Storage
    Digital
    signal
    Stallings, 2016
    Communications

    View Slide

  3. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 3 Iwan Setiawan
    Objectives

    Learn how to connect DHT11 sensor to the
    Raspberry Pi

    Learn how to monitor temperature and humidity
    with the sensor through Python programs

    Learn how to store data to a CSV file and
    display to a live graph Comma-separated values

    View Slide

  4. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 4 Iwan Setiawan
    Contents

    Electronic circuit prototyping with DHT11

    Python programming using Adafruit DHT and
    matplotlib libraries

    Setting up and running experiment set 2
    – CPU temperature monitoring
    – Temperature and humidity monitoring with DHT11
    – Data collecting, storing, and live-graphing

    View Slide

  5. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 5 Iwan Setiawan
    Prototyping using Fritzing

    Download DHT11 part*

    Import the part into Fritzing

    Save and use the part for prototyping
    * https://github.com/jorgechacblogspot/librerias_fritzing
    Indicates that you need to
    take a picture of your work

    View Slide

  6. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 6 Iwan Setiawan
    Preparation

    Raspberry Pi 4 set +Ethernet cable

    Breadboard

    Jumper wires

    DHT11 sensor

    View Slide

  7. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 7 Iwan Setiawan
    Python Libraries

    Adafruit DHT, a library for DHT11/DHT22 sensor
    – Install the library using pip3
    $ sudo pip3 install Adafruit_DHT
    – Edit platform_detect.py file and add two lines in pi_version function
    $ sudo nano /usr/local/lib/python3.7/dist-packages/Adafruit_DHT/platform_detect.py

    elif match.group(1) == ‘BCM2711’:
    return 3
    ...

    Matplotlib, a visualizations library, included in Raspberry Pi OS
    * https://github.com/adafruit/Adafruit_Python_DHT
    Notes: Add consistent when editing

    View Slide

  8. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 8 Iwan Setiawan
    CPU Temperature: Monitoring

    Run Mu editor

    Type the code, save, and run
    from gpiozero import CPUTemperature
    from time import sleep
    cpu = CPUTemperature()
    while True:
    temp = cpu.temperature
    print(temp)
    sleep(1)
    cpu_temp.py
    * https://gpiozero.readthedocs.io/en/stable/api_internal.html#cputemperature

    View Slide

  9. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 9 Iwan Setiawan
    CPU Temperature: Storing Data

    Type the code, save, and run
    from gpiozero import CPUTemperature
    from time import sleep, strftime, time
    cpu = CPUTemperature()
    with open(“/home/pi/acnt/cpu_temp.csv”, ”a”) as log:
    while True:
    temp = cpu.temperature
    log.write(“{0},{1}\n”.format(strftime(“%Y-%m-%d %H:%M:%S”),str(temp)))
    sleep(1)
    cpu_temp-csv.py

    View Slide

  10. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 10 Iwan Setiawan
    CPU Temperature: Live-graphing

    Type the code, save,
    and run

    After the live graph is
    shown, execute a
    program, e.g.
    Chromium

    Observe the graph
    from gpiozero import CPUTemperature
    from time import time
    import matplotlib.pyplot as plt
    cpu = CPUTemperature()
    plt.ion() # enable interactive mode
    x = [] # create dict x
    y = [] # create dict y
    while True:
    temp = cpu.temperature # get temp
    y.append(temp) # append temp to dict y
    x.append(time()) # append time to dict x
    plt.clf() # clear figure
    plt.scatter(x,y) # scatter plot for x and y
    plt.plot(x,y) # plot
    plt.pause(0.05) # pause
    plt.draw() # update figure cpu_temp-plot.py

    View Slide

  11. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 11 Iwan Setiawan
    Raspberry Pi Foundation, CC By-SA 4.0

    View Slide

  12. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 12 Iwan Setiawan
    DHT11 Prototyping
    +

    View Slide

  13. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 13 Iwan Setiawan
    Temp. and Humid. Monitoring

    Type the code, save, and run
    import Adafruit_DHT
    sensor = Adafruit_DHT.DHT11
    pin = 21
    while True:
    try:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
    print(“Temperature: {0:0.1f}*C Humidity: {1:0.1f}%.format(temperature, humidity))
    else:
    print(“Cannot pull data from the sensor”)
    except KeyboardInterrupt:
    exit()
    temp_humi.py

    View Slide

  14. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 14 Iwan Setiawan
    IoT Assignment 3
    1. Find the basic principles of DHT11
    – Temperature sensing
    – Humidity sensing
    2. CPU temperature monitoring
    – Merge storing data and live-graphing codes to one code
    – Use Python functions
    – Add control flow: if the temperature is high, show an alert
    message

    View Slide

  15. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 15 Iwan Setiawan
    IoT Assignment 3
    3. Simple smart home application: implementing two features
    – Storing and displaying the temperature data collected through DHT11

    Store the data to a CSV file

    Display the data to a live graph
    – Indicators for temperature rising

    Add a red LED and buzzer representing indicators

    Control flow: If the temperature changes between 30 to 35 degree Celcius, the
    buzzer sounds long beep. If it is above 35 degree Celcius, the buzzer produces
    short beep plus the red LED turns on and a message shows up informing high
    temperature or heat alert. Simulate this situation with a demo

    Question: Can we use DHT11 to detect fire? If yes, how would you use it to detect
    the event? If no, why?

    View Slide

  16. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 16 Iwan Setiawan
    Results to be Uploaded
    1. Experiment results: photos and explanations
    2. Assignment 3 results
    – Fritzing export image using Breadboard view
    – Photos and a demo video showing the results
    – Source code, screenshots/figures, and explanations
    Upload your experiment and assignment 3 results to Moodle

    Using one PDF file

    One group, one document

    Upload your demo video somewhere, only attach the URL to the doc.
    06/10
    9pm
    code
    experiment

    View Slide

  17. Advanced Computer Networks Tutorials, Spring 2021, CSIE NTUST 17 Iwan Setiawan
    Resources

    Raspberry Pi Foundation, “Temperature log,” 2021.
    [Online]. Available: https://projects.raspberrypi.org/en/-
    projects/temperature-log

    Stack Overflow, “cannot import name 'Beaglebone_-
    Black_Driver' from 'Adafruit_DHT',” 2020. [Online].
    Available: https://stackoverflow.com/questions/-
    63232072/cannot-import-name-beaglebone-black-
    driver-from-adafruit-dht

    View Slide