Start Automatically
(Raspberry Pi)

It can be very useful to have your programs start automatically when your Raspberry Pi launches. For example, you may be logging data from a greenhouse and displaying it on a website. After developing the program, you will disconnect your Raspberry Pi from keyboards/monitors, plug it in and let it log data and serve your website.

This project will show you how to run your projects automatically when your Raspberry Pi starts up using cron.

Setup

Before getting started, make sure you have the following parts.

Getting Started Kit

Any Raspberry Pi

Step 1

Simply connect your Getting Started Kit to your Raspberry Pi.

If you haven't set up your Raspberry Pi with Phidgets yet, visit the Getting Started Kit tutorial.

Write Code

Copy the code below into a new Python project. If you need a reminder of how to do this, revisit the Getting Started Course.

  

#Add Phidgets library
from Phidget22.Phidget import *
from Phidget22.Devices.TemperatureSensor import *
from Phidget22.Devices.DigitalOutput import *
#Required for sleep statement
import time

#Create
temperatureSensor = TemperatureSensor()
statusLED = DigitalOutput()

#Address
statusLED.setHubPort(1)
statusLED.setIsHubPortDevice(True)

#Open
temperatureSensor.openWaitForAttachment(1000)
statusLED.openWaitForAttachment(1000)

#Use your Phidgets
while (True):
    #Update user
    print("Logging data...")
    
    #Blink LED to indicate program is running
    statusLED.setState(not statusLED.getState())

    #Write data to file in CSV format
    with open ('phidgets_temperature.csv','a') as datafile:
        datafile.write(str(temperatureSensor.getTemperature()) + "\n")
    
    time.sleep(1.0)
  

Create Program

The code above logs temperature to a file and flashes the red LED on your Getting Started Kit.

Save the Python script in /home/pi/ under the name phidgets_temperature.py. If you are unfamiliar with logging data from a Phidgets sensor, review this project.

Schedule Run with cron

cron allows you to easily schedule tasks on your Raspberry Pi. In this situation, cron is used to run your Phidgets project whenever the Raspberry Pi starts.

Open your terminal and enter the following command:

  
crontab -e
  

Add the following line to the bottom of your crontab

  
@reboot python3 /home/pi/phidgets_temperature.py
  

Exit and save the file. When you reboot your Raspberry Pi, you will see your red LED blinking indicating that the program is running. You can check the output file to see your logged data.

Troubleshoot
  1. Make sure everything is plugged in properly and your VINT Hub is connected to your computer
  2. Stop running other programs that are using your Phidgets before running a program.

If these do not solve your issue visit the Advanced Troubleshooting Page.

What are Phidgets?

Phidgets are programmable, modular USB devices, either sensors or controllers that you can connect together. Simply write code in your favorite language and solve real-world problems.

Learn more

Set your preferences

Windows

Mac OS

Raspberry Pi

Java

Python

C#

Swift

NetBeans

Processing

Eclipse

Thonny

PyCharm

PyScripter

Visual Studio

Xcode

Setting your preferred operating system, programming language and environment lets us display relevant code samples for the Getting Started Tutorial, Device Tutorials and Projects

Done