GithubHelp home page GithubHelp logo

datascience-iot's Introduction

Measure temperature and air humidity with the raspberry pi and DHT11/DHT22

alt

This project collects data about temperature and air humidity. The data is written to a csv file, which can be used to perform analysis and gain insights from the data.

Requirements

Hardware

alt

  1. DHT11 (or DHT22) sensor
  2. Raspberry Pi 4 (Model B)
  3. 3 Female-to-female jumper cables
  4. SD-card (8GB minimal)
  5. Keyboard/Mouse (Optional)
  6. Monitor (Optional)

Software

  1. Raspberry Pi OS (with an IDE if you're not using SSH)
  2. Tool to analyse data from CSV files (for example Excel, Google Sheets, Jupyter Notebooks, etc.)

Set up

Step 1: Install Raspberry Pi OS

Download the 'Raspberry Pi Imager' from https://www.raspberrypi.com/software/. Choose 'Raspberry Pi OS (32-bit) as the Operating System, and choose your micro SD card as 'storage'. Click 'Write' and wait untill the process is done. Insert the SD card into the Raspberry Pi and follow the set up process. Once set up is done, update the OS by entering the following commands into the terminal:

sudo apt-get update
sudo apt-get upgrade

Step 2: Connect the sensor to the Raspberry Pi

alt As seen on the image above, the sensor has 3 labelled pins: VCC, DATA and GND. Connect your three jumper cables to the pins. The cable connected to VCC should go to a 5V power output pin (in my case I used a 5V pin, pin number 2). The DATA cable should go to a GPIO pin (I used pin 7) and the GND cable should go to a ground pin (I used pin 6). If everything is connected properly, the LED on the sensor should turn on (as seen on the first picture).

Step 3: Install necessary libraries

The only required library is the 'dht11' library. Install it by entering the following command into the terminal:

pip install dht11

Step 4: Test the set up

Open up an IDE to test if everything is set up correctly. You can use the following Python code to test if your sensor is outputting data:

# Necessary imports
import dht11
import RPi.GPIO as GPIO
import time
import datetime


def main() -> None:
    # Setting GPIO settings
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.cleanup()
  
    # Initialising sensor instance
    instance = dht11.DHT11(pin=4) # CHANGE THE PIN NUMBER IF YOU'RE USING A DIFFERENT PIN

    try:
        while True:
            # Reading values from sensor
            result = instance.read()
            # Checking if reading is valid
            if result.is_valid():
                # Printing data
                print(f'Last valid input: {str(datetime.datetime.now())}')
                print(f'Temperature: {result.temperature}C')
                print(f'Humidity: {result.humidity}%')
               
             # Waiting for 5 seconds to get next reading
            time.sleep(5)

    except KeyboardInterrupt:
        print('Cleanup')
        GPIO.cleanup()

        
if __name__ == '__main__':
    main()

If everything is working correctly, readings should be printed to the console.

Step 5: Start collecting data

I modified the code above so it will store each correct reading inside a CSV file. The code will create a CSV file named 'data.csv', and it will store everything there. You can collect data over a period of time, and then stop by hitting 'CTRL/Command + C' on your keyboard. Here is my code:

# Necessary imports
import dht11
import RPi.GPIO as GPIO
import time
import datetime


def main() -> None:
    # Setting GPIO settings
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)
    GPIO.cleanup()
    
    # Initialising sensor instance
    instance = dht11.DHT11(pin=4)       # CHANGE THE PIN NUMBER IF YOU USE A DIFFERENT PIN
    
    # Creating File and Writing headers
    with open('data.csv', 'w') as file:
        file.write('Time, Temp (C), Humidity (%)\n')
        try:
            while True:
                # Reading values from sensor
                result = instance.read()
                if result.is_valid():
                    # Storing instance values
                    current_time = datetime.datetime.now()
                    temperature = str(result.temperature)
                    humidity = str(result.humidity)
                    
                    # Printing data
                    print(f'Last valid input: {str(datetime.datetime.now())}')
                    print(f'Temperature: {result.temperature}C')
                    print(f'Humidity: {result.humidity}%')
                    
                    # Writing data to file
                    file.write(f'{str(current_time)}, {temperature}, {humidity}\n')
                 
                # Waiting 5 seconds per measurements
                time.sleep(5)

        except KeyboardInterrupt:
            print('Cleanup')
            GPIO.cleanup()

if __name__ == '__main__':
    main()

An example of the data can be found in the directory 'data' in this repository. There is also an example of the analysis I did on the data.

Data Pipeline

alt

Data is collected by the sensor and saved in a CSV file. To collect the data, send the CSV file to your desired location. Visualize data with your tool of choice.

Sources

  1. https://github.com/szazo/DHT11_Python/blob/master/README.md
  2. https://www.youtube.com/watch?v=KUr8WgSIsfk&t=203s

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.