Skip to content

Commit

Permalink
Merge origin/master
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/java/pl/waw/maslak/powermeter/PowerMeter.java
  • Loading branch information
piotr-maslak committed Mar 16, 2022
2 parents 2b2e6b4 + 34844f9 commit b68c004
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 63 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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
40 changes: 39 additions & 1 deletion nbactions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,46 @@
<goal>org.codehaus.mojo:exec-maven-plugin:1.5.0:exec</goal>
</goals>
<properties>
<exec.args>-classpath %classpath pl.waw.maslak.powermeter.PowerMeter</exec.args>
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
<exec.executable>java</exec.executable>
<exec.mainClass>pl.waw.maslak.powermeter.PowerMeter</exec.mainClass>
<exec.vmArgs></exec.vmArgs>
<exec.appArgs></exec.appArgs>
</properties>
</action>
<action>
<actionName>debug</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:3.0.0:exec</goal>
</goals>
<properties>
<exec.vmArgs>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</exec.vmArgs>
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
<exec.appArgs></exec.appArgs>
<exec.mainClass>pl.waw.maslak.powermeter.PowerMeter</exec.mainClass>
<exec.executable>java</exec.executable>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>process-classes</goal>
<goal>org.codehaus.mojo:exec-maven-plugin:3.0.0:exec</goal>
</goals>
<properties>
<exec.vmArgs></exec.vmArgs>
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
<exec.mainClass>pl.waw.maslak.powermeter.PowerMeter</exec.mainClass>
<exec.executable>java</exec.executable>
<exec.appArgs></exec.appArgs>
</properties>
</action>
</actions>
30 changes: 4 additions & 26 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>pl.waw.maslak</groupId>
<artifactId>PowerMeter</artifactId>
<version>1.0</version>
<version>0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -34,13 +34,12 @@
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.0</version>
<version>1.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -50,9 +49,7 @@
<target>1.7</target>
</configuration>
</plugin>




<plugin>
<!-- groupId org.apache.maven.plugins is assumed by default, so
there is no need to specify it here. -->
Expand All @@ -77,26 +74,7 @@
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<!-- -->
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-jsch</artifactId>
<version>1.10.7</version>
</dependency>
</dependencies>
</plugin>

</plugin>
</plugins>
</build>
<name>PowerMeter</name>
Expand Down
100 changes: 65 additions & 35 deletions src/main/java/pl/waw/maslak/powermeter/PowerMeter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
}

0 comments on commit b68c004

Please sign in to comment.