diff --git a/README.md b/README.md index 357eaae..f8d4b99 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # PowerMeter -It turns a pulse electricity consumption meter into IOT (MQTT & REST) Device +It turns a pulse electricity consumption metering device into IOT (MQTT & REST) Device diff --git a/nbactions.xml b/nbactions.xml index 745434d..3e3c389 100644 --- a/nbactions.xml +++ b/nbactions.xml @@ -10,8 +10,46 @@ org.codehaus.mojo:exec-maven-plugin:1.5.0:exec - -classpath %classpath pl.waw.maslak.powermeter.PowerMeter + ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs} java + pl.waw.maslak.powermeter.PowerMeter + + + + + + debug + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:3.0.0:exec + + + -agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address} + ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs} + + pl.waw.maslak.powermeter.PowerMeter + java + true + + + + profile + + jar + + + process-classes + org.codehaus.mojo:exec-maven-plugin:3.0.0:exec + + + + ${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs} + pl.waw.maslak.powermeter.PowerMeter + java + diff --git a/pom.xml b/pom.xml index 4e96ebe..5a4eb38 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 pl.waw.maslak PowerMeter - 1.0 + 0.1 jar UTF-8 @@ -34,13 +34,12 @@ org.eclipse.paho org.eclipse.paho.client.mqttv3 - 1.2.0 + 1.2.5 - org.apache.maven.plugins maven-compiler-plugin @@ -50,9 +49,7 @@ 1.7 - - - + @@ -77,26 +74,7 @@ - - - - org.apache.maven.plugins - maven-antrun-plugin - 1.7 - - - - - - - - org.apache.ant - ant-jsch - 1.10.7 - - - - + PowerMeter diff --git a/src/main/java/pl/waw/maslak/powermeter/PowerMeter.java b/src/main/java/pl/waw/maslak/powermeter/PowerMeter.java index efa3a8c..974418f 100644 --- a/src/main/java/pl/waw/maslak/powermeter/PowerMeter.java +++ b/src/main/java/pl/waw/maslak/powermeter/PowerMeter.java @@ -18,82 +18,112 @@ public class PowerMeter { public static String port = "1883"; public static String client_id = "power-meter"; public static String topic_prefix = ""; - public static String username = "maslak"; - public static String password = "maslak"; + public static String username; + public static String password; + + public static boolean connect = false; + public static boolean verbose = false; public static long startTime = System.currentTimeMillis(); public static long stopTime = System.currentTimeMillis(); + public static boolean first = true; public static String power = "0"; public static double energy = 0; + public static MqttClient sampleClient; + //public static MqttMessage message; + public static MqttClient powerClient; public static void main(String[] args) throws InterruptedException { for (int i = 0; i < args.length; i++) { String arg = args[i]; - String value = args[++i]; if (arg.equalsIgnoreCase("-host")) { + String value = args[++i]; host = value; + connect = true; } if (arg.equalsIgnoreCase("-port")) { + String value = args[++i]; port = value; + connect = true; } if (arg.equalsIgnoreCase("-client_id")) { + String value = args[++i]; client_id = value; + connect = true; } if (arg.equalsIgnoreCase("-topic_prefix")) { + String value = args[++i]; topic_prefix = value; + connect = true; } if (arg.equalsIgnoreCase("-username")) { + String value = args[++i]; username = value; + connect = true; } if (arg.equalsIgnoreCase("-password")) { + String value = args[++i]; password = value; + connect = true; + } + if (arg.equalsIgnoreCase("-verbose")) { + verbose = true; } } - MemoryPersistence persistence = new MemoryPersistence(); - - try { - powerClient = new MqttClient("tcp://" + host + ":" + port, client_id, persistence); - MqttConnectOptions connOpts = new MqttConnectOptions(); - connOpts.setCleanSession(true); - connOpts.setUserName(username); - connOpts.setPassword(password.toCharArray()); - connOpts.setAutomaticReconnect(true); - powerClient.connect(connOpts); - powerClient.publish(topic_prefix + "power", new MqttMessage(power.getBytes())); - powerClient.publish(topic_prefix + "energy", new MqttMessage(String.format("%.4f", energy).getBytes())); - - } catch (MqttException ex) { - Logger.getLogger(PowerMeter.class.getName()).log(Level.SEVERE, null, ex); - } - - // gpio controller + // GPIO Listener final GpioController gpio = GpioFactory.getInstance(); - final GpioPinDigitalInput myButton1 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_DOWN); - myButton1.setShutdownOptions(true); - myButton1.addListener(new GpioPinListenerDigital() { + final GpioPinDigitalInput gpio1 = gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, PinPullResistance.PULL_DOWN); + gpio1.setShutdownOptions(true); + gpio1.addListener(new GpioPinListenerDigital() { @Override public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) { if (event.getState().isHigh()) { - startTime = stopTime; - stopTime = System.currentTimeMillis(); - power = String.valueOf(3600000 / (stopTime - startTime)); - energy = energy + 0.0001; - try { - powerClient.publish(topic_prefix + "power", new MqttMessage(power.getBytes())); - powerClient.publish(topic_prefix + "energy", new MqttMessage(String.format("%.4f", energy).getBytes())); - } catch (MqttException ex) { - Logger.getLogger(PowerMeter.class.getName()).log(Level.SEVERE, null, ex); + if (startTime == stopTime && first == true) { + first = false; + } else { + startTime = stopTime; + stopTime = System.currentTimeMillis(); + power = String.valueOf(3600000 / (stopTime - startTime)); + } + energy = energy + 0.001; + if (verbose) { + System.out.println("power: " + power + ", energy: " + String.format("%.3f", energy)); } } } }); - while (true) { - Thread.sleep(1000); + if (connect) { + MemoryPersistence persistence = new MemoryPersistence(); + try { + powerClient = new MqttClient("tcp://" + host + ":" + port, client_id, persistence); + MqttConnectOptions connOpts = new MqttConnectOptions(); + connOpts.setCleanSession(true); + if (username != null && !username.isEmpty() && password != null && !password.isEmpty()) { + connOpts.setUserName(username); + connOpts.setPassword(password.toCharArray()); + } + connOpts.setAutomaticReconnect(true); + if (verbose) { + System.out.println("power: " + power + ", energy: " + String.format("%.3f", energy)); + } + powerClient.connect(connOpts); + while (true) { + + powerClient.publish(topic_prefix + "power", new MqttMessage(power.getBytes())); + powerClient.publish(topic_prefix + "energy", new MqttMessage(String.format("%.3f", energy).getBytes())); + + System.out.print("."); + Thread.sleep(1000); + } + } catch (MqttException ex) { + Logger.getLogger(PowerMeter.class.getName()).log(Level.SEVERE, null, ex); + } } + } }