Skip to content

Commit

Permalink
Remove sensors not yet ready.
Browse files Browse the repository at this point in the history
Change-Id: Ia35d3cc1a144e18bd02069a3b715539eb87648fd
  • Loading branch information
mangini committed May 11, 2017
1 parent 7e643fb commit da10bc7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 398 deletions.
16 changes: 9 additions & 7 deletions README.md
@@ -1,23 +1,25 @@
Android Things Weather Station sample
=====================================
Android Things Cloud IoT Sensor Hub
===================================

This demo shows a robust Android Things way to collect sensor data and publish
on a Google Cloud IoT PubSub topic.
This demo shows how to implement a sensor hub on Android Things that collects
sensor data from connected sensors and publish on a
Google Cloud IoT PubSub topic.

- connection parameters are configurable via intent and configuration is saved in sharedpreferences
- sensor robustness: you can remove and add sensors at runtime and the app will adapt accordingly
- network robustness: device can loose connectivity. When connectivity is restored, it will auto-reconnect
- power robustness: device can loose power. When is reboots, it will auto-reconnect
- sensor data collected since the last publish is sent to pubsub every 20 seconds
- sensor data is collected either as continuous mode or onchange mode. Continuous mode sensors (temperature, pressure, humidity and luminosity) publishes only the most recent value. Onchange mode sensors (motion detection) stores up to 10 sensor changes in between pubsub publications.
- sensor data is collected either as continuous mode or onchange mode. Continuous mode sensors
(temperature and pressure) publishes only the most recent value. Onchange mode
sensors (motion detection) stores up to 10 sensor changes in between pubsub publications.

Pre-requisites
--------------
- Android Things compatible board
- Android Studio 2.2+
- 1 [bme280 temperature, pressure and humidity sensor](https://www.adafruit.com/product/2651)
- 1 [bmp280 temperature and pressure](https://www.adafruit.com/product/2651)
- 1 [PIR motion detector sensor](https://www.adafruit.com/product/189)
- 1 [Luminosity sensor](https://www.adafruit.com/product/439)
- [Google Cloud Platform](https://cloud.google.com/) project with Cloud IoT support

Schematics
Expand Down
Expand Up @@ -29,11 +29,8 @@
import android.util.Log;

import com.example.androidthings.weatherstation.cloud.CloudPublisherService;
import com.google.android.things.contrib.driver.bmx280.Bme280;
import com.google.android.things.contrib.driver.bmx280.Bmx280;
import com.google.android.things.contrib.driver.button.Button;
import com.google.android.things.contrib.driver.tsl2561.TSL2561;
import com.google.android.things.pio.I2cDevice;
import com.google.android.things.pio.PeripheralManagerService;

import java.io.IOException;
Expand Down Expand Up @@ -61,17 +58,14 @@ public class WeatherStationActivity extends Activity {
}

public static final String SENSOR_TYPE_MOTION_DETECTION = "motion";
public static final String SENSOR_TYPE_HUMIDITY_DETECTION = "humidity";
public static final String SENSOR_TYPE_TEMPERATURE_DETECTION = "temperature";
public static final String SENSOR_TYPE_AMBIENT_PRESSURE_DETECTION = "ambient_pressure";
public static final String SENSOR_TYPE_LUMINOSITY_DETECTION = "luminosity";

private CloudPublisherService mPublishService;
private Looper mSensorLooper;

// sensors
private Bme280 mEnvironmentalSensor;
private TSL2561 mLuxSensor;
private Bmx280 mEnvironmentalSensor;
private Button mMotionDetectorSensor;

@Override
Expand Down Expand Up @@ -124,36 +118,17 @@ private void connectToAvailableSensors() {
mEnvironmentalSensor = connectToBmx280();
}

if (mLuxSensor == null) {
mLuxSensor = connectToTsl2561();
}

if (mMotionDetectorSensor == null) {
mMotionDetectorSensor = connectToMotionDetector();
}
}

private TSL2561 connectToTsl2561() {
try {
TSL2561 tsl2561 = new TSL2561(BoardDefaults.getI2cBusForSensors());
Log.d(TAG, "Initialized TSL2561");
return tsl2561;
} catch (Throwable t) {
Log.w(TAG, "Could not initialize TSL2561 sensor on I2C bus " +
BoardDefaults.getI2cBusForSensors(), t);
return null;
}
}

private Bme280 connectToBmx280() {
private Bmx280 connectToBmx280() {
try {
PeripheralManagerService pioService = new PeripheralManagerService();
I2cDevice device = pioService.openI2cDevice(
BoardDefaults.getI2cBusForSensors(), 0x77);
Bme280 bmx280 = new Bme280(device);
Bmx280 bmx280 = new Bmx280(BoardDefaults.getI2cBusForSensors());
bmx280.setTemperatureOversampling(Bmx280.OVERSAMPLING_1X);
bmx280.setPressureOversampling(Bmx280.OVERSAMPLING_1X);
bmx280.setHumidityOversampling(Bmx280.OVERSAMPLING_1X);
bmx280.setMode(Bmx280.MODE_NORMAL);
Log.d(TAG, "Initialized BME280");
return bmx280;
Expand Down Expand Up @@ -187,7 +162,6 @@ private void collectContinuousSensors() {
if (mPublishService != null) {
List<SensorData> sensorsData = new ArrayList<>();
addBmx280Readings(sensorsData);
addTSL2561Readings(sensorsData);
Log.d(TAG, "collected continuous sensor data: " + sensorsData);
mPublishService.logSensorData(sensorsData);
}
Expand All @@ -202,8 +176,6 @@ private void addBmx280Readings(List<SensorData> output) {
output.add(new SensorData(now, SENSOR_TYPE_TEMPERATURE_DETECTION, data[0]));
output.add(new SensorData(now, SENSOR_TYPE_AMBIENT_PRESSURE_DETECTION,
data[1]));
float humidity = mEnvironmentalSensor.readHumidity();
output.add(new SensorData(now, SENSOR_TYPE_HUMIDITY_DETECTION, humidity));
} else {
Log.i(TAG, "Ignoring sensor readings because timestamp is invalid. " +
"Please, set the device's date/time");
Expand All @@ -215,24 +187,6 @@ private void addBmx280Readings(List<SensorData> output) {
}
}

private void addTSL2561Readings(List<SensorData> output) {
if (mLuxSensor != null) {
try {
long now = System.currentTimeMillis();
if (now >= INITIAL_VALID_TIMESTAMP) {
long data = mLuxSensor.getLux();
output.add(new SensorData(now, SENSOR_TYPE_LUMINOSITY_DETECTION, data));
} else {
Log.i(TAG, "Ignoring sensor readings because timestamp is invalid. " +
"Please, set the device's date/time");
}
} catch (Throwable t) {
Log.w(TAG, "Cannot collect TSL2561 data. Ignoring it for now", t);
closeBmx280Quietly();
}
}
}

private void collectSensorOnChange(String type, float sensorReading) {
if (mPublishService != null) {
Log.d(TAG, "On change " + type + ": " + sensorReading);
Expand All @@ -257,17 +211,6 @@ private void closeBmx280Quietly() {
}
}

private void closeTsl2561Quietly() {
if (mLuxSensor != null) {
try {
mLuxSensor.close();
} catch (IOException e) {
// close quietly
}
mLuxSensor = null;
}
}

private void closeMotionDetectorQuietly() {
if (mMotionDetectorSensor != null) {
try {
Expand All @@ -286,7 +229,6 @@ protected void onDestroy() {

mSensorLooper.quit();
closeBmx280Quietly();
closeTsl2561Quietly();
closeMotionDetectorQuietly();

// unbind from Cloud Publisher service.
Expand Down

This file was deleted.

0 comments on commit da10bc7

Please sign in to comment.