Skip to content

Service File Configuration

aaronsathya edited this page Aug 6, 2024 · 10 revisions

Important

Start the Service File Configuration only after all the InfluxDB and Grafana Configuration steps have been completed.

  1. To ensure the influxdb-multisensor.py script runs even if the device reboots, we will use a service file.

    sudo nano /lib/systemd/system/influxdb-multisensor.service
    
  2. Copy and paste the contents from influxdb-multisensor.service [found within the RPi-IoT folder or here: Link] into the new service file making sure to check the WorkingDirectory and ExecStart file path locations match the location of the "multisensors" folder. It should look something like this:

    [Unit]
    Description=Vivarium Monitor
    After=network-online.target
    
    [Service]
    Type=simple
    Restart=always
    RestartSec=60
    WorkingDirectory=/home/pi/RPi-IoT/multisensors/
    ExecStart=/usr/bin/python3 /home/pi/RPi-IoT/multisensors/influxdb-multisensor.py
    
    [Install]
    WantedBy=multi-user.target
    

    Save and close: Ctrl+X -> Y -> hit enter

  3. Close the terminal window where the python script was printing realtime sensor values or hit Ctrl+C to interrupt the script

  4. Enable the service file and reload all service files

    sudo systemctl enable influxdb-multisensor.service
    sudo systemctl daemon-reload
    sudo systemctl start influxdb-multisensor.service
    
  5. You can test if the program restarts if the Pi loses power and reboots

    sudo shutdown -r now
    

    Once the Pi boots up:

    sudo systemctl status influxdb-multisensor.service
    

    You should see something like this:

    You can also open a browser and navigate to http://localhost:3000/ to check if grafana is receiving sensor values.

Once the above steps are completed, you can proceed to Cloudflare Configuration