Plant Monitoring
Having a plant in your workspace can be a great way to brighten the space and can help reduce stress and increase air quality. It can be a challenge to decide what kind of plant is best suited for your location as every plant requires different lighting levels to flourish.
Use your light sensor to determine what plant is suitable for your desk.
Setup
Before getting started, make sure you have the following parts.
VINT Hub
Write code (Java)
Copy the code below into a new Java project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.
Not your programming language? Set your preferences so we can display relevant code examples
//Add Phidgets Library
import com.phidget22.*;
public class LightMonitor {
public static void main(String[] args) throws Exception{
//Create
LightSensor lightSensor = new LightSensor();
//Open
lightSensor.open(1000);
//Use your Phidgets
while(true){
System.out.println("Illuminance: " + lightSensor.getIlluminance() + " lx");
Thread.sleep(150);
}
}
}
package lightmonitor;
//Add Phidgets Library
import com.phidget22.*;
public class LightMonitor {
public static void main(String[] args) throws Exception{
//Create
LightSensor lightSensor = new LightSensor();
//Open
lightSensor.open(1000);
//Use your Phidgets
while(true){
System.out.println("Illuminance: " + lightSensor.getIlluminance() + " lx");
Thread.sleep(150);
}
}
}
Write code (Python)
Copy the code below into a new Python project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.
Not your programming language? Set your preferences so we can display relevant code examples
#Add Phidgets Library
from Phidget22.Phidget import *
from Phidget22.Devices.LightSensor import *
#Required for sleep statement
import time
#Create
lightSensor = LightSensor()
#Open
lightSensor.openWaitForAttachment(1000)
#Use your Phidgets
while (True):
print("Illuminance: " + str(lightSensor.getIlluminance()) + " lx")
time.sleep(0.15)
Write code (C#)
Copy the code below into a new C# project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.
Not your programming language? Set your preferences so we can display relevant code examples
//Add Phidgets Library
using Phidget22;
namespace LightMonitor{
class Program{
static void Main(string[] args){
//Create
LightSensor lightSensor = new LightSensor();
//Open
lightSensor.Open(1000);
//Use your Phidgets
while (true){
System.Console.WriteLine("Illuminance: " + lightSensor.Illuminance + " lx");
System.Threading.Thread.Sleep(150);
}
}
}
}
Write code (Swift)
Copy the code below into a new Swift project. If you need a reminder of how to do this, revisit the Getting Started Course. Insert the code below.
Not your programming language? Set your preferences so we can display relevant code examples
import Cocoa
import Phidget22Swift
class ViewController: NSViewController {
@IBOutlet weak var lightLabel: NSTextField!
//Create
let lightSensor = LightSensor()
override func viewDidLoad() {
super.viewDidLoad()
do{
//Subscribe to event
let _ = lightSensor.illuminanceChange.addHandler(light_change)
//Open
try lightSensor.open()
} catch {
print(error)
}
}
func light_change(sender:LightSensor, illuminance: Double) {
DispatchQueue.main.async {
// Use information from your Phidget to change label
self.lightLabel.stringValue = String(illuminance) + " lx"
}
}
}
Run your program. You will see illuminance data printed to the screen.>
Practice
Modify your code to print the following ranges rather than the illuminance:
- Low light = 250 - 800 lx
- Mid light = 801- 1600 lx
- High light = 1601+ lx
Troubleshoot
- Make sure everything is plugged in properly and your VINT Hub is connected to your computer
- 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.