first commit

This commit is contained in:
pandacraft 2025-03-21 16:04:17 +01:00
commit a5a0434432
1126 changed files with 439481 additions and 0 deletions

View file

@ -0,0 +1,73 @@
package org.iot.raspberry.examples;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveDigitalOut;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveRelay;
import org.iot.raspberry.grovepi.devices.GroveRgbLcd;
import org.iot.raspberry.grovepi.devices.GroveTemperatureAndHumiditySensor;
import org.iot.raspberry.grovepi.devices.GroveTemperatureAndHumidityValue;
import org.iot.raspberry.grovepi.devices.GroveUltrasonicRanger;
/**
* Connect:
* Digital 3 -> Red led
* Digital 4 -> Green led
* Digital 5 -> blue led
* Digital 6 -> Ultrasonic ranger
* Digital 7 -> TemperatureSensor DHT11
* Digital 8 -> Relay
* Any I2C -> LCD
* @author Eduardo Moranchel <emoranchel@asmatron.org>
*/
public class AutomaticAC implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveDigitalOut redLed = grovePi.getDigitalOut(3);
GroveDigitalOut greenLed = grovePi.getDigitalOut(4);
GroveDigitalOut blueLed = grovePi.getDigitalOut(5);
GroveUltrasonicRanger ranger = new GroveUltrasonicRanger(grovePi, 6);
GroveTemperatureAndHumiditySensor tempSensor = new GroveTemperatureAndHumiditySensor(grovePi, 7, GroveTemperatureAndHumiditySensor.Type.DHT11);
GroveRelay relay = new GroveRelay(grovePi, 8);
GroveRgbLcd lcd = grovePi.getLCD();
while (monitor.isRunning()) {
try {
GroveTemperatureAndHumidityValue dht = tempSensor.get();
double temperature = dht.getTemperature();
double distance = ranger.get();
String message = String.format("TEMP:%.2f\nDistance%.2f", temperature, distance);
System.out.println(message);
lcd.setText(message);
if (temperature > 28) {
if (distance < 50) {
relay.set(true);
} else {
relay.set(false);
}
lcd.setRGB(255, 50, 50);
redLed.set(true);
blueLed.set(false);
greenLed.set(false);
} else if (temperature < 17) {
relay.set(false);
blueLed.set(true);
redLed.set(false);
greenLed.set(false);
lcd.setRGB(50, 50, 255);
} else {
relay.set(false);
blueLed.set(false);
greenLed.set(true);
lcd.setRGB(50, 255, 50);
}
} catch (IOException ioex) {
System.out.println("Error");
}
}
redLed.set(false);
greenLed.set(false);
blueLed.set(false);
relay.set(false);
}
}

View file

@ -0,0 +1,23 @@
package org.iot.raspberry.examples;
import org.iot.raspberry.grovepi.GroveDigitalOut;
import org.iot.raspberry.grovepi.GrovePi;
/*
Connect: Led to D4
*/
public class BlinkingLed implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveDigitalOut led = grovePi.getDigitalOut(2);
boolean state = false;
while (monitor.isRunning()) {
state = !state;
led.set(state);
Thread.sleep(500);
}
led.set(false);
}
}

View file

@ -0,0 +1,25 @@
package org.iot.raspberry.examples;
import org.iot.raspberry.grovepi.GroveDigitalIn;
import org.iot.raspberry.grovepi.GroveDigitalOut;
import org.iot.raspberry.grovepi.GrovePi;
/*
Connect: Led to D4 and Button to D6
*/
public class ButtonLed implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveDigitalIn button = grovePi.getDigitalIn(6);
GroveDigitalOut led = grovePi.getDigitalOut(4);
while (monitor.isRunning()) {
try {
led.set(button.get());
} catch (Exception e) {
}
}
led.set(false);
}
}

View file

@ -0,0 +1,28 @@
package org.iot.raspberry.examples;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.iot.raspberry.grovepi.GroveDigitalIn;
import org.iot.raspberry.grovepi.GrovePi;
/*
Connect: Button to D6
*/
public class ButtonListener implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
GroveDigitalIn button = grovePi.getDigitalIn(6);
button.setListener((boolean oldValue, boolean newValue) -> {
System.out.println("Button " + oldValue + "->" + newValue);
});
exec.scheduleAtFixedRate(button, 0, 200, TimeUnit.MILLISECONDS);
monitor.waitForStop();
exec.shutdown();
}
}

View file

@ -0,0 +1,9 @@
package org.iot.raspberry.examples;
import org.iot.raspberry.grovepi.GrovePi;
public interface Example {
public void run(GrovePi grovePi, Monitor monitor) throws Exception;
}

View file

@ -0,0 +1,30 @@
package org.iot.raspberry.examples;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveDigitalOut;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveLightSensor;
/*
Connect Light sensor to A2
Connect Red Led to D3 (emergency Light)
*/
public class LightSensor implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveLightSensor lightSensor = new GroveLightSensor(grovePi, 2);
GroveDigitalOut emergencyLight = new GroveDigitalOut(grovePi, 3);
while (monitor.isRunning()) {
try {
Double value = lightSensor.get();
emergencyLight.set(value < 250);
System.out.println(value);
} catch (IOException ex) {
System.out.println("error");
}
}
emergencyLight.set(false);
}
}

View file

@ -0,0 +1,23 @@
package org.iot.raspberry.examples;
import java.util.concurrent.Semaphore;
public class Monitor {
private Semaphore semaphore = new Semaphore(0);
private boolean running = true;
public void waitForStop() throws InterruptedException {
semaphore.acquire();
semaphore.release();
}
public void stop() {
running = false;
semaphore.release();
}
public boolean isRunning() {
return running;
}
}

View file

@ -0,0 +1,33 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.iot.raspberry.examples;
import java.io.IOException;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveRelay;
/*
* Connect Relay to D8
*/
public class Relay implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveRelay relay = new GroveRelay(grovePi, 8);
while (monitor.isRunning()) {
try {
relay.set(true);
Thread.sleep(10000);
relay.set(false);
Thread.sleep(5000);
} catch (IOException io) {
System.err.println("error");
}
}
relay.set(false);
}
}

View file

@ -0,0 +1,43 @@
package org.iot.raspberry.examples;
import java.io.IOException;
import java.util.Random;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveRgbLcd;
public class RgbLcd implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveRgbLcd lcd = grovePi.getLCD();
String[] phrases = new String[]{
"Hi! ALL!",
"This should work just fine",
"A message\nA response",
"More data that you can handle for sure!",
"Welcome to the internet of things with java",
"Short\nMessage"
};
int[][] colors = new int[][]{
{50, 255, 30},
{15, 88, 245},
{248, 52, 100},
{48, 56, 190},
{178, 25, 180},
{210, 210, 210}
};
while (monitor.isRunning()) {
try {
String text = phrases[new Random().nextInt(phrases.length)];
int[] color = colors[new Random().nextInt(colors.length)];
lcd.setRGB(color[0], color[1], color[2]);
Thread.sleep(100);
lcd.setText(text);
Thread.sleep(2000);
} catch (IOException io) {
}
}
}
}

View file

@ -0,0 +1,50 @@
package org.iot.raspberry.examples;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveDigitalOut;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveRotarySensor;
import org.iot.raspberry.grovepi.devices.GroveRotaryValue;
/*
Connect:
RotarySensor to A1
Leds to D3 (red),D4 (green) and D5 (blue)
*/
public class RotarySensor3Led implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveRotarySensor rotarySensor = new GroveRotarySensor(grovePi, 1);
GroveDigitalOut redLed = grovePi.getDigitalOut(3);
GroveDigitalOut greenLed = grovePi.getDigitalOut(4);
GroveDigitalOut blueLed = grovePi.getDigitalOut(5);
GroveDigitalOut onLed = null;
while (monitor.isRunning()) {
try {
GroveRotaryValue value = rotarySensor.get();
System.out.println(value);
GroveDigitalOut ledToTurn;
if (value.getDegrees() > 250) {
ledToTurn = redLed;
} else if (value.getDegrees() < 100) {
ledToTurn = blueLed;
} else {
ledToTurn = greenLed;
}
if (ledToTurn != onLed) {
redLed.set(ledToTurn == redLed);
blueLed.set(ledToTurn == blueLed);
greenLed.set(ledToTurn == greenLed);
onLed = ledToTurn;
}
} catch (IOException ex) {
System.out.println("Error");
}
}
blueLed.set(false);
greenLed.set(false);
redLed.set(false);
}
}

View file

@ -0,0 +1,32 @@
package org.iot.raspberry.examples;
import java.io.IOException;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveLed;
import org.iot.raspberry.grovepi.devices.GroveRotarySensor;
import org.iot.raspberry.grovepi.devices.GroveRotaryValue;
/*
Connect:
RotarySensor to A1
Led D5
*/
public class RotarySensorDimLed implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveRotarySensor rotarySensor = new GroveRotarySensor(grovePi, 1);
GroveLed blueLed = new GroveLed(grovePi, 5);
while (monitor.isRunning()) {
try {
GroveRotaryValue value = rotarySensor.get();
int brightness = (int) (value.getFactor() * GroveLed.MAX_BRIGTHNESS);
blueLed.set(brightness);
} catch (IOException ex) {
System.out.println("Error");
}
}
blueLed.set(false);
}
}

View file

@ -0,0 +1,111 @@
package org.iot.raspberry.examples;
import java.io.File;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.pi4j.GrovePi4J;
public class Runner {
public static void main(String[] args) throws Exception {
Logger.getLogger("GrovePi").setLevel(Level.WARNING);
Logger.getLogger("RaspberryPi").setLevel(Level.WARNING);
File control = new File("RUNNINGSAMPLES");
control.deleteOnExit();
if (control.exists()) {
control.delete();
System.out.println("STOPING CURRENT SAMPLE");
System.exit(0);
}
/*
if (args.length != 2) {
System.err.println("You need to provide 2 arguments DIO|PI4J EXAMPLECLASS");
System.exit(-1);
}
*/
if (args.length != 1) {
System.err.println("You need to provide 1 argument - name of the class");
System.exit(-1);
}
control.createNewFile();
/*
String mode = args[0];
GrovePi grovePi;
switch (mode.toLowerCase()) {
case "pi4j":
grovePi = new GrovePi4J();
break;
case "test":
grovePi = createProxy(GrovePi.class);
break;
default:
throw new IllegalArgumentException("You must provide either DIO or PI4J implementation");
}
*/
GrovePi grovePi = new GrovePi4J();
Example example = (Example) Class.forName("org.iot.raspberry.examples." + args[0]).newInstance();
System.out.println("RUNNING EXAMPLE: " + args[0] + " USING: PI4J");
final ExecutorService runner = Executors.newSingleThreadExecutor();
final ExecutorService consoleMonitor = Executors.newSingleThreadExecutor();
final ExecutorService fileMonitor = Executors.newSingleThreadExecutor();
final Semaphore lock = new Semaphore(0);
final Monitor monitor = new Monitor();
runner.execute(() -> {
try {
example.run(grovePi, monitor);
} catch (Exception ex) {
Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex);
}
lock.release();
});
consoleMonitor.execute(() -> {
try (Scanner scanner = new Scanner(System.in)) {
String command;
while (!(command = scanner.next()).equalsIgnoreCase("quit")) {
System.out.println("Command " + command + " not recognized, try quit");
}
}
monitor.stop();
lock.release();
});
fileMonitor.execute(() -> {
while (control.exists()) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
}
monitor.stop();
lock.release();
});
lock.acquire();
runner.shutdown();
consoleMonitor.shutdownNow();
fileMonitor.shutdownNow();
runner.awaitTermination(10, TimeUnit.SECONDS);
control.delete();
System.exit(0);
}
private static <T> T createProxy(Class<T> aClass) {
return (T) Proxy.newProxyInstance(aClass.getClassLoader(), new Class<?>[]{aClass}, (Object proxy, Method method, Object[] args1) -> {
throw new RuntimeException("Test class methods not allowed");
});
}
}

View file

@ -0,0 +1,14 @@
package org.iot.raspberry.examples;
import org.iot.raspberry.grovepi.GrovePi;
public class RunnerTest implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
while (monitor.isRunning()) {
Thread.sleep(1000);
System.out.println("Running...");
}
}
}

View file

@ -0,0 +1,49 @@
package org.iot.raspberry.examples;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveDigitalOut;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveSoundSensor;
/*
Connect:
SoundSensor to A0
Leds to D3 (red),D4 (green) and D5 (blue)
*/
public class SoundSensor implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveSoundSensor soundSensor = new GroveSoundSensor(grovePi, 0);
GroveDigitalOut redLed = grovePi.getDigitalOut(3);
GroveDigitalOut greenLed = grovePi.getDigitalOut(4);
GroveDigitalOut blueLed = grovePi.getDigitalOut(5);
GroveDigitalOut onLed = null;
while (monitor.isRunning()) {
try {
double soundLevel = soundSensor.get();
System.out.println(soundLevel);
GroveDigitalOut ledToTurn;
if (soundLevel > 1000) {
ledToTurn = redLed;
} else if (soundLevel < 300) {
ledToTurn = blueLed;
} else {
ledToTurn = greenLed;
}
if (ledToTurn != onLed) {
redLed.set(ledToTurn == redLed);
blueLed.set(ledToTurn == blueLed);
greenLed.set(ledToTurn == greenLed);
onLed = ledToTurn;
}
} catch (IOException ex) {
System.out.println("Error");
}
}
blueLed.set(false);
greenLed.set(false);
redLed.set(false);
}
}

View file

@ -0,0 +1,22 @@
package org.iot.raspberry.examples;
import java.io.IOException;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveTemperatureAndHumiditySensor;
/*
Connect Temp & Humidity sensor to D4
*/
public class TemperatureAndHumidity implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveTemperatureAndHumiditySensor dht = new GroveTemperatureAndHumiditySensor(grovePi, 4, GroveTemperatureAndHumiditySensor.Type.DHT11);
while (monitor.isRunning()) {
try {
System.out.println(dht.get());
} catch (IOException ex) {
}
}
}
}

View file

@ -0,0 +1,30 @@
package org.iot.raspberry.examples;
import java.io.IOException;
import org.iot.raspberry.grovepi.GroveDigitalOut;
import org.iot.raspberry.grovepi.GrovePi;
import org.iot.raspberry.grovepi.devices.GroveUltrasonicRanger;
/*
Connect ultrasonic ranger to port D6
buzzer to D7
*/
public class UltrasonicRanger implements Example {
@Override
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
GroveUltrasonicRanger ranger = new GroveUltrasonicRanger(grovePi, 6);
GroveDigitalOut buzzer = grovePi.getDigitalOut(7);
while (monitor.isRunning()) {
try {
double distance = ranger.get();
System.out.println(distance);
buzzer.set(distance < 20);
} catch (IOException ex) {
System.out.println("error!");
}
}
}
}