Skip to content

Commit

Permalink
Added temperature sensor support
Browse files Browse the repository at this point in the history
Signed-off-by: nicolatimeus <[email protected]>
  • Loading branch information
nicolatimeus committed Apr 18, 2017
1 parent 83be5f8 commit f770d7a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.eclipse.kura.demo.opcua.server.digital.GrovePiDigitalInSensor;
import org.eclipse.kura.demo.opcua.server.digital.GrovePiDigitalOutSensor;
import org.eclipse.kura.demo.opcua.server.i2c.GroveDigitalLightSensor;
import org.eclipse.kura.demo.opcua.server.i2c.GroveTemperatureSensor;
import org.eclipse.milo.opcua.sdk.server.OpcUaServer;
import org.eclipse.milo.opcua.sdk.server.api.config.OpcUaServerConfig;
import org.eclipse.milo.opcua.sdk.server.nodes.UaFolderNode;
Expand Down Expand Up @@ -59,6 +60,7 @@ public void activate(Map<String, Object> properties) {
logger.warn("interrupted", e);
}

sensors.add(new GroveTemperatureSensor("temperatureSensor"));
sensors.add(new GroveDigitalLightSensor("lightSensor"));
sensors.add(new GrovePiDigitalOutSensor("buzzer", 6, grovePi));
sensors.add(new GrovePiDigitalOutSensor("led", 4, grovePi));
Expand Down Expand Up @@ -143,7 +145,7 @@ public void deactivate() {
}
}
sensors.clear();

try {
if (grovePi != null) {
logger.info("shutting down grovepi...");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.eclipse.kura.demo.opcua.server.i2c;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.eclipse.kura.core.util.ProcessUtil;
import org.eclipse.kura.core.util.SafeProcess;
import org.eclipse.kura.demo.opcua.server.Sensor;
import org.eclipse.milo.opcua.sdk.core.AccessLevel;
import org.eclipse.milo.opcua.sdk.server.api.nodes.VariableNode;
import org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext;
import org.eclipse.milo.opcua.stack.core.Identifiers;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.io.Files;

public class GroveTemperatureSensor extends Sensor {

private static final Logger logger = LoggerFactory.getLogger(GroveTemperatureSensor.class);

private static final String PYTHON_CODE = "import smbus\n" + "import time\n" + "bus = smbus.SMBus(1)\n"
+ "bus.write_i2c_block_data(0x44, 0x2C, [0x06])\n" + "time.sleep(0.5)\n"
+ "data = bus.read_i2c_block_data(0x44, 0x00, 6)\n" + "temp = data[0] * 256 + data[1]\n"
+ "cTemp = -45 + (175 * temp / 65535.0)\n" + "fTemp = -49 + (315 * temp / 65535.0)\n"
+ "humidity = 100 * (data[3] * 256 + data[4]) / 65535.0\n" + "print \"%.2f\" %cTemp\n";

public GroveTemperatureSensor(String name) {
super(name, Identifiers.Double);
}

@Override
public void init() throws IOException {
Files.write(PYTHON_CODE, new File("/tmp/temperature.py"), Charsets.UTF_8);
}

@Override
public UByte getAccessLevel(AttributeContext context, VariableNode node) throws UaException {
return UByte.valueOf(AccessLevel.getMask(AccessLevel.READ_ONLY));
}

@Override
public UByte getUserAccessLevel(AttributeContext context, VariableNode node) throws UaException {
return UByte.valueOf(AccessLevel.getMask(AccessLevel.READ_ONLY));
}

@Override
public DataValue getValue(AttributeContext context, VariableNode node) throws UaException {
SafeProcess proc = null;
try {
proc = ProcessUtil.exec("python /tmp/temperature.py");
proc.waitFor();

try (BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
String line = br.readLine();
return new DataValue(new Variant(Double.parseDouble(line)));
}
} catch (Exception e) {
logger.warn("got exception", e);
throw new UaException(e);
} finally {
if (proc != null) {
ProcessUtil.destroy(proc);
}
}
}

@Override
public void close() throws Exception {
}
}

0 comments on commit f770d7a

Please sign in to comment.