Commit 24f267a9 authored by aw613's avatar aw613

CoAP sensor works

parent 1f39d543
......@@ -3,22 +3,24 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry combineaccessrules="false" kind="src" path="/jmeds2.0-beta10"/>
<classpathentry kind="lib" path="C:/Users/aw613/git/jcoap-draft18/ws4d-jcoap/lib/log4j-1.2.16.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/junit.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-core.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-core-javadoc.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-core-sources.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-device.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-device-javadoc.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-device-sources.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-example.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-example-javadoc.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-example-sources.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-gpio-extension.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-gpio-extension-javadoc.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-gpio-extension-sources.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-service.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-service-javadoc.jar"/>
<classpathentry kind="lib" path="C:/Users/aw613/ThirdPartyLib/pi4j-1.0/lib/pi4j-service-sources.jar"/>
<classpathentry combineaccessrules="false" exported="true" kind="src" path="/ws4d-jcoap"/>
<classpathentry kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/junit.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-core.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-core-javadoc.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-core-sources.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-device.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-device-javadoc.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-device-sources.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-example.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-example-javadoc.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-example-sources.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-gpio-extension.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-gpio-extension-javadoc.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-gpio-extension-sources.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-service.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-service-javadoc.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/pi4j-1.0/lib/pi4j-service-sources.jar"/>
<classpathentry exported="true" kind="lib" path="C:/Users/aw436/ThirdPartyLib/log4j-1.2.17/apache-log4j-1.2.17/log4j-1.2.17.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
package org.ws4d.coap.smartsensor.device;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ws4d.coap.core.connection.BasicCoapChannelManager;
import org.ws4d.coap.core.connection.api.CoapChannelManager;
import org.ws4d.coap.core.enumerations.CoapMediaType;
import org.ws4d.coap.core.rest.BasicCoapResource;
import org.ws4d.coap.core.rest.CoapResourceServer;
public class CoAPSensor {
private static CoAPSensor sampleServer;
private CoapResourceServer resourceServer;
private static String environment = System.getenv("SUDO_USER");
public static void main(String[] args) {
System.out.println("Start Sample Resource Server");
// if(environment!=null && environment.equals("pi")){
// }
sampleServer = new CoAPSensor();
sampleServer.run();
}
private void run() {
if (resourceServer != null)
resourceServer.stop();
CoapChannelManager channelManager = BasicCoapChannelManager.getInstance();
resourceServer = new CoapResourceServer();
/* Show detailed logging of Resource Server*/
Logger resourceLogger = Logger.getLogger(CoapResourceServer.class.getName());
resourceLogger.setLevel(Level.ALL);
/* add resources */
TemperaturRessource temperature = new TemperaturRessource("/temperature", "Content", CoapMediaType.text_plain);
LuminosityRessource luminosity = new LuminosityRessource("/luminosity", "Content", CoapMediaType.text_plain);
temperature.setResourceType("temperature");
temperature.setPostable(false);
temperature.setObservable(false);
temperature.setDeletable(false);
temperature.setPutable(false);
temperature.setReadable(true);
luminosity.setResourceType("luminosity");
luminosity.setPostable(false);
luminosity.setObservable(false);
luminosity.setDeletable(false);
luminosity.setPutable(false);
luminosity.setReadable(true);
resourceServer.createResource(temperature);
resourceServer.createResource(luminosity);
temperature.registerServerListener(resourceServer);
luminosity.registerServerListener(resourceServer);
try {
resourceServer.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package org.ws4d.coap.smartsensor.device;
import java.io.IOException;
import java.util.List;
import org.ws4d.coap.core.enumerations.CoapMediaType;
import org.ws4d.coap.core.rest.BasicCoapResource;
import org.ws4d.coap.core.rest.CoapData;
import org.ws4d.smartsensor.device.Tsl2561;
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CFactory;
public class LuminosityRessource extends BasicCoapResource {
public LuminosityRessource (String path, String value, CoapMediaType mediaType) {
super(path, value, mediaType);
// TODO Auto-generated constructor stub
}
@Override
public synchronized CoapData get(List<String> query, List<CoapMediaType> mediaTypesAccepted) {
I2CBus bus;
try {
bus = I2CFactory.getInstance(I2CBus.BUS_1);
Tsl2561 sensor = new Tsl2561(bus.getDevice(0x39));
System.out.println("Luminosity read: " + sensor.getLux() + "Lux");
return new CoapData(Integer.toString((int)sensor.getLux()).getBytes(), CoapMediaType.text_plain);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
package org.ws4d.coap.smartsensor.device;
import java.util.List;
import org.ws4d.coap.core.enumerations.CoapMediaType;
import org.ws4d.coap.core.rest.BasicCoapResource;
import org.ws4d.coap.core.rest.CoapData;
import org.ws4d.smartsensor.device.PhySensor;
public class TemperaturRessource extends BasicCoapResource {
private PhySensor sensor;
public TemperaturRessource (String path, String value, CoapMediaType mediaType) {
super(path, value, mediaType);
sensor = new PhySensor("28-0000045157c3");
// TODO Auto-generated constructor stub
}
@Override
public synchronized CoapData get(List<String> query, List<CoapMediaType> mediaTypesAccepted) {
System.out.println("Temperature read: " + sensor.readSensor() + "C");
return new CoapData(Integer.toString((int)sensor.readSensor()).getBytes(), CoapMediaType.text_plain);
}
}
......@@ -25,8 +25,8 @@ public class PhySensor {
private void initSensor(String SensorID){
StringBuilder sb = new StringBuilder();
sb.append("/sys/bus/w1/devices").append("/"+SensorID).append("/w1_slave");
this.pathtoData = sb.toString();
sb.append("/sys/bus/w1/devices").append("/"+SensorID).append("/w1_slave"); //file that contains temperature value
this.pathtoData = sb.toString(); //TempReadingThread will poll value
tempSem = new Semaphore(1);
......@@ -42,15 +42,17 @@ public class PhySensor {
try {
tempSem.acquire();
returnTemp = this.temperature; //temperature value that was red by the TempReadingThread and written to PhySensor
tempSem.release();
return returnTemp;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnTemp = this.temperature;
tempSem.release();
return returnTemp;
return 0;
}
......@@ -58,11 +60,11 @@ public class PhySensor {
public void writetemperature(int temp){
try {
tempSem.acquire();
this.temperature = temp;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.temperature = temp;
tempSem.release();
}
}
......@@ -29,15 +29,15 @@ public class SensorDevice extends DefaultDevice{
// add device name ( name is language specific )
this.addFriendlyName ("en -US", "Sun SPOT - Sensor Device");
this.addFriendlyName ( LocalizedString.LANGUAGE_DE , "Sun SPOT - Sensor Device");
this.addFriendlyName ("en -US", "SensorDevice");
this.addFriendlyName ( LocalizedString.LANGUAGE_DE , "Sensor");
// add device manufacturer ( manufacturer is language specific )
this.addManufacturer ( LocalizedString.LANGUAGE_EN , "Test Inc.");
this.addManufacturer ("de -DE", " Test GmbH ");
this.addModelName ( LocalizedString.LANGUAGE_EN , "Sun Spot");
this.addModelName ( LocalizedString.LANGUAGE_EN , "Sensor Model 1");
}
}
......@@ -10,13 +10,13 @@ public class SmartSensorService extends DefaultService {
public static final String NAMESPACE = "http://www.demo.com/bbsr";//HelloWorldDevice.NAMESPACE;
// public static final String PORTTYPE = "TemperatureInterface";
public static final String PORTTYPE_BRIGHT = "Brightness";
public static final String PORTTYPE_BRIGHT = "BrightnessInterface";
public static final String PORTTYPE_TEMP = "TemperatureInterface";
public static final String ENDPOINT_PATH = "/SunSPOTSensor";
public static final String ENDPOINT_PATH = "SmartSensorService";
public static final QName QN_PORTTYPE_BRIGHT = new QName(PORTTYPE_BRIGHT, NAMESPACE);
public static final QName QN_PORTTYPE_TEMP = new QName(PORTTYPE_TEMP, NAMESPACE);
public final static URI DOCU_EXAMPLE_SERVICE_ID = new URI(NAMESPACE + ENDPOINT_PATH);
public final static URI DOCU_EXAMPLE_SERVICE_ID = new URI(ENDPOINT_PATH);
public SmartSensorService() {
super(DPWSCommunicationManager.COMMUNICATION_MANAGER_ID);
......
......@@ -28,10 +28,10 @@ public class TempReadingThread implements Runnable {
while(true){
try {
BufferedReader reader = new BufferedReader(new FileReader(f));
BufferedReader reader = new BufferedReader(new FileReader(f)); //temperature value is stored in text document
String line = reader.readLine();
if(line.substring(line.length()-3, line.length()).equals("YES")){
if(line.substring(line.length()-3, line.length()).equals("YES")){ //YES indicates the availability of a value
line = reader.readLine();
this.temperature = Integer.parseInt(line.substring(line.length()-5, line.length()));
......@@ -40,7 +40,7 @@ public class TempReadingThread implements Runnable {
reader.close();
phySensor.writetemperature(temperature);
phySensor.writetemperature(temperature); //write temperature value to sensor object
}
......@@ -51,9 +51,8 @@ public class TempReadingThread implements Runnable {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Thread.sleep(REFRESHINTERVAL);
Thread.sleep(REFRESHINTERVAL); //cyclic polling of temperature value
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
......
......@@ -10,7 +10,7 @@ public class TempService extends DefaultService {
public static final String NAMESPACE = "http://www.demo.com/bbsr";//HelloWorldDevice.NAMESPACE;
// public static final String PORTTYPE = "TemperatureInterface";
public static final String PORTTYPE = "TemperatureInterface";
public static final String PORTTYPE = "Temperature";
public static final String ENDPOINT_PATH = "/SunSPOTSensor1";
public static final QName QN_PORTTYPE = new QName(PORTTYPE, NAMESPACE);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment