Git Product home page Git Product logo

temp_humidity_api's Introduction

๐ŸŒก Raspberry Pi Temperature and Humidity API ๐ŸŒฆ

Turn your Raspberry Pi into a mini weather station! This guide will help you set up an API to read temperature and humidity data from a DHT22/AM2302 sensor.

๐Ÿ“ Table of Contents

๐Ÿ“‹ Prerequisites

  • Raspberry Pi Zero with Raspbian OS installed.
  • DHT22/AM2302 sensor with 3-pin configuration.
  • Internet connection.

๐Ÿ”Œ Hardware Setup

  1. Connect the DHT22/AM2302 sensor to the Raspberry Pi Zero:

    AM2302_to_Raspberry_Pi_Zero_Small

    • VCC to 3.3V or 5V
    • Data to GPIO4
    • GND to GND

๐Ÿ”ง Manual Software Setup

  1. Open a terminal on your Raspberry Pi.

  2. Update your Pi:

    sudo apt-get update
  3. Install necessary packages:

    sudo apt-get install python3-pip nginx
  4. Install Python libraries:

    pip3 install Adafruit_DHT Flask gunicorn
  5. Set up the API:

    • Copy the temp_humidity_api.py to your desired directory; it is the only file you need.

    • mkdir -p /home/pi/temp_humidity_api
      mv temp_humidity_api.py /home/pi/temp_humidity_api
  6. Test the API:

    python3 temp_humidity_api.py

    Your API should now be running on http://0.0.0.0:5000/metrics.

๐Ÿš€ Production Deployment

  1. Set up Gunicorn:

    • Gunicorn will serve the Flask application. Start it with the following to test from your API directory:

      gunicorn -w 4 temp_humidity_api:app -b 0.0.0.0:8000
  2. Set up Nginx:

    • Create a new Nginx configuration:

      sudo nano /etc/nginx/sites-available/temp_humidity_api
    • Add the following configuration

      • Replace your_ip_range with your desired IP range, e.g., 192.168.1.0/24 for access

      • Replace your_pi_ip_address with your Raspberry Pi IPv4 Address

      server {
          listen 80;
          server_name your_pi_ip_address;
      
          location / {
              allow your_ip_range;
              deny all;
      
              proxy_pass http://127.0.0.1:8000;
              proxy_set_header Host $host;
              proxy_set_header X-Real-IP $remote_addr;
          }
      }
    • Link the Nginx configuration and restart Nginx:

      sudo ln -s /etc/nginx/sites-available/temp_humidity_api /etc/nginx/sites-enabled
      sudo nginx -t
      sudo systemctl restart nginx

๐Ÿš— Auto-Start on Boot

  1. Create a systemd service file:

    sudo nano /etc/systemd/system/temp_humidity_api.service
  2. Add the following configuration

    1. Replace /path/to/your/directory with the path to your temp_humidity_api.py file

    2. Replace /path/to/gunicorn where gunicorn is located (try which gunicorn)

    3. your_username is the user account the API will run under. Don't use root!

    [Unit]
    Description=Temperature and Humidity API
    After=network.target
    
    [Service]
    User=your_username
    WorkingDirectory=/path/to/your/directory
    ExecStart=/path/to/gunicorn -w 4 temp_humidity_api:app -b 0.0.0.0:8000
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
  3. Enable and start the service:

    sudo systemctl enable temp_humidity_api.service
    sudo systemctl start temp_humidity_api.service

๐Ÿ“œ Using the Installation Script

The install_api.sh script is designed to automate the setup process described above. It's especially useful if you're setting up multiple Raspberry Pis or want a quick, hassle-free installation. The idea is that you are installing on a freshly imaged Raspbian Linux image.

How to Use

  1. Download or copy the install_api.sh script to your Raspberry Pi to the same directory as temp_humidity_api.py.
  2. Make the script executable: chmod +x install_api.sh
  3. Run the script: ./install_api.sh
  4. You will be asked for two items
    1. The IP range to allow access to the API such as 192.168.20.0/24 or 10.0.0.0/22. The default is 192.168.1.0/24
    2. The port to run the API on. The default is 8000.

Pros

  • Efficiency: The script automates multiple steps, saving time.
  • Consistency: Ensures the same setup process every time, reducing potential errors.

Cons

  • Less Control: While the script provides some configurability, manual setup offers more control over each step.
  • Understanding: If you're new to Linux or Raspberry Pi, running scripts without understanding them can be risky. Always review script contents before executing.

๐Ÿ”— Accessing the API

Once everything is set up, you can access the API using:

http://your_pi_ip_address:port/metrics

This will return a JSON response with temperature and humidity data.

[23:31:40] ~ $ curl localhost:8000/metrics
{"humidity":52.400001525878906,"temperature_celsius":21.899999618530273,"temperature_fahrenheit":71.4199993133545}

๐Ÿ”’ Security

The Nginx configuration provided restricts access to a specific IP range for added security. Ensure you configure this range according to your needs. Do not expose this API to the Internet. Do not run this API as root.

๐Ÿ’พ Local Data Storage

The Python 3 script get_local_data.py is provided to store temperature and humidity data locally in a subdirectory called temperature_data. The script is designed to be run as a cron job to store data at regular intervals. The data is stored in a CSV file with the date and time as the filename.

Sample crontab entry

This will save data every 5 minutes.

*/5 * * * * /usr/bin/python3 /home/pi/temp_humidity_api/get_local_data.py

temp_humidity_api's People

Contributors

roberttoups avatar

Stargazers

 avatar

Watchers

 avatar

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.