summaryrefslogtreecommitdiff
path: root/peripheral/libupm/examples/java/H803X_Example.java
blob: 5d4828ec69d542531fb5845b38d31ef8930cc0f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import upm_h803x.H803X;

public class H803X_Example 
{
    private static String defaultDev = "/dev/ttyUSB0";

    public static void main(String[] args) throws InterruptedException
    {
// ! [Interesting]
        if (args.length > 0)
            defaultDev = args[0];
        System.out.println("Using device " + defaultDev);
        System.out.println("Initializing...");

        // Instantiate an H803X instance, using MODBUS slave address 1, and
        // default comm parameters (9600, 8, N, 2)
        H803X sensor = new H803X(defaultDev, 1);
        
        // output the Slave ID (manufacturer, model, serno)
        System.out.println("Slave ID: " + sensor.getSlaveID());
        System.out.println();

        while (true)
            {
                // update our values from the sensor
                sensor.update();

                // H8035 / H8036
                System.out.println("Consumption (kWh): " 
                                   + sensor.getConsumption());
                System.out.println("Real Power (kW): " 
                                   + sensor.getRealPower());

                if (sensor.isH8036())
                    {
                        // The H8036 has much more data available...
                        
                        System.out.println("Reactive Power (kVAR): "
                                           + sensor.getReactivePower());
                        System.out.println("Apparent Power (kVA): "
                                           + sensor.getApparentPower());
                        System.out.println("Power Factor: "
                                           + sensor.getPowerFactor());
                        System.out.println("Volts Line to Line: "
                                           + sensor.getVoltsLineToLine());
                        System.out.println("Volts Line to Neutral: "
                                           + sensor.getVoltsLineToNeutral());
                        
                        System.out.println("Current: " + sensor.getCurrent());
                        
                        System.out.println("Real Power Phase A (kW): "
                                           + sensor.getRealPowerPhaseA());
                        System.out.println("Real Power Phase B (kW): "
                                           + sensor.getRealPowerPhaseB());
                        System.out.println("Real Power Phase C (kW): "
                                           + sensor.getRealPowerPhaseC());
                        
                        System.out.println("Power Factor Phase A: "
                                           + sensor.getPowerFactorPhaseA());
                        System.out.println("Power Factor Phase B: "
                                           + sensor.getPowerFactorPhaseB());
                        System.out.println("Power Factor Phase C: "
                                           + sensor.getPowerFactorPhaseC());
                        
                        System.out.println("Volts Phase A to B: "
                                           + sensor.getVoltsPhaseAToB());
                        System.out.println("Volts Phase B to C: "
                                           + sensor.getVoltsPhaseBToC());
                        System.out.println("Volts Phase A to C: "
                                           + sensor.getVoltsPhaseAToC());
                        System.out.println("Volts Phase A to Neutral: "
                                           + sensor.getVoltsPhaseAToNeutral());
                        System.out.println("Volts Phase B to Neutral: "
                                           + sensor.getVoltsPhaseBToNeutral());
                        System.out.println("Volts Phase C to Neutral: "
                                           + sensor.getVoltsPhaseCToNeutral());
                        
                        System.out.println("Current Phase A: "
                                           + sensor.getCurrentPhaseA());
                        System.out.println("Current Phase B: "
                                           + sensor.getCurrentPhaseB());
                        System.out.println("Current Phase C: "
                                           + sensor.getCurrentPhaseC());
                        
                        System.out.println("Avg Real Power (kW): "
                                           + sensor.getAvgRealPower());
                        System.out.println("Min Real Power (kW): "
                                           + sensor.getMinRealPower());
                        System.out.println("Max Real Power (kW): "
                                           + sensor.getMaxRealPower());
                    }

                System.out.println();
                Thread.sleep(2000);
            }

// ! [Interesting]
    }
}