Manuel Alejandro Iglesias Abbatemarco
Published © LGPL

Industrial IoT Gateway Based on Android Things

A smart industrial gateway for the IoT world based on Android Things.

AdvancedFull instructions provided5 days4,483
Industrial IoT Gateway Based on Android Things

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
phoenix contact DIN rail housing
×1

Software apps and online services

Android Things
Google Android Things

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

schematic of industrial hat

Code

sensor html

JavaScript
<!doctype html>
<html>

<head>
  <meta charset="utf-8"/>
  <title>Sensor</title>
  <meta name="viewport" content="width=device-width">
  <style>
  .container {
    width: 550px;
    height: 300px;
    margin: 0 auto;
    text-align: center;
  }

  .gauge {
    width: 250px;
    height: 250px;
    display: inline-block;
  }
  </style>
</head>
<body>
	<h3 align="center">On-Board Sensor HDC1080</h4>
	<div class="container">
		<div id="temp" class="gauge"></div>
		<div id="rh" class="gauge"></div>
	</div>
	<script>
	var tdata = 10.0;
	var rhdata = 40.0;
	var temp;
	var rh;
	function update(){
		$.getJSON( "/sensor", function(jres) {
			tdata = parseFloat(jres[0].temp);
			rhdata = parseFloat(jres[0].rh);
			console.log("Temperature: " + jres[0].temp);
			console.log("Humidity: " + jres[0].rh);
			temp.refresh(tdata);
			rh.refresh(rhdata);
		});
	};
	$( document ).ready(function(){
		update();
	    temp = new JustGage({
	      id: "temp",
	      value : 10,
	      min: 0,
	      max: 40,
	      decimals: 1,
	      title: "Temperarture",
	      gaugeWidthScale: 0.6,
	      customSectors: [{
	        color : "#0000ff",
	        lo : 0,
	        hi : 15
	      },{
	        color : "#00ff00",
	        lo : 15,
	        hi : 35
	      },{
	        color : "#ffff00",
	        lo : 35,
	        hi : 45
	      },{
	        color : "#ff0000",
	        lo : 45,
	        hi : 65
	      }],
	      counter: true
	    });

	    rh = new JustGage({
	      id: "rh",
	      value : 40,
	      min: 0,
	      max: 100,
	      decimals: 1,
	      title: "Humidity",
	      gaugeWidthScale: 0.6,
	      customSectors: [{
	        color : "#0000ff",
	        lo : 0,
	        hi : 30
	      },{
	        color : "#00ff00",
	        lo : 30,
	        hi : 70
	      },{
	        color : "#ff0000",
	        lo : 70,
	        hi : 100
	      }],
	      counter: true
	    });
	});
  </script>
</body>
</html>

OnBoardSensor

Java
package com.example.androidthings.loopback;

import android.util.Log;

import com.google.android.things.pio.I2cDevice;
import com.google.android.things.pio.PeripheralManagerService;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by mhanuel on 10/31/17.
 */

public class OnBoardSensor {

    private static boolean init = false;
    private static final String TAG = "SensorActivity";
    // I2C Slave Address
    private static final int I2C_ADDRESS = 0x40;
    private static I2cDevice mDevice;
    private static PeripheralManagerService mService = new PeripheralManagerService();

    public enum HumidityRes {
        _8_BITS, _11_BITS, _14_BITS
    }

    public enum TemperatureRes {
        _8_BITS, _14_BITS
    }

    private static final Integer HDC1080_TEMPERATURE = 0x00;
    private static final Integer HDC1080_HUMIDITY = 0x01;
    private static final Integer HDC1080_CONFIGURATION = 0x02;
    private static final Integer HDC1080_MANUFACTURER_ID = 0xFE;
    private static final Integer HDC1080_DEVICE_ID = 0xFF;
    private static final Integer HDC1080_SERIAL_ID_FIRST = 0xFB;
    private static final Integer HDC1080_SERIAL_ID_MID = 0xFC;
    private static final Integer HDC1080_SERIAL_ID_LAST = 0xFD;

    public double readTemperature() throws IOException {
        Integer rawT = readData(HDC1080_TEMPERATURE);
        return ((rawT / 65536.0) * 165) - 40;
    }

    public double readHumidity() throws IOException {
        Integer rawH = readData(HDC1080_HUMIDITY);
        return (rawH / 65536.0) * 100;
    }

    public Integer readManufacturerId() throws IOException {
        return readCfgData(HDC1080_MANUFACTURER_ID);
    }

    public Integer readDeviceId() throws IOException {
        return readCfgData(HDC1080_DEVICE_ID);
    }

    public Integer readCfgData (Integer pointer){
        int read = 0;
        try {
            read = mDevice.readRegWord(pointer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return (((read & 0x00ff) << 8) | ((read & 0xff00) >> 8));
    }

    public Integer readData (Integer pointer){
        byte [] dummy = new byte[1];
        dummy[0] = (byte)(pointer & 0x00ff);
        Integer value = 0;
        try {
            mDevice.write(dummy, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            TimeUnit.MILLISECONDS.sleep(20);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            byte[] buf = new byte[2];
            mDevice.read(buf, 2);
            value = (buf[0] & 0x00ff) << 8 | (buf[1] & 0x00ff);
            Log.d(TAG, "buf idx 0 = 0x" + Integer.toHexString(buf[0]));
            Log.d(TAG, "buf idx 1 = 0x" + Integer.toHexString(buf[1] & 0x00ff));
            Log.d(TAG, "read = 0x" + Integer.toHexString(value));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return value;
    }

    class HDC1080_Registers {
        byte[] RawData = new byte[2];

        public void setTemperatureResolution(TemperatureRes res){
            switch(res){
                case _8_BITS:
                    RawData[1] = (byte)(RawData[1] | 0b00000100);
                    break;
                case _14_BITS:
                    RawData[1] = (byte)(RawData[1] & 0b11111011);
                    break;
                default:
                    break;
            }
        }
        public void setHumidityResolution(HumidityRes res){
            switch(res){
                case _8_BITS:
                    RawData[1] = (byte)(RawData[1] & 0b11111100);
                    RawData[1] = (byte)(RawData[1] | 0b00000010);
                    break;
                case _11_BITS:
                    RawData[1] = (byte)(RawData[1] & 0b11111100);
                    RawData[1] = (byte)(RawData[1] | 0b00000001);
                    break;
                case _14_BITS:
                    RawData[1] = (byte)(RawData[1] & 0b11111100);
                    break;
                default:
                    break;

            }
        }

    };

    public void init() {
        if(!init) {
            Log.d(TAG, "Sensor Created");
            try {
                List<String> deviceList = mService.getI2cBusList();
                if (deviceList.isEmpty()) {
                    Log.i(TAG, "No I2C bus available on this device.");
                } else {
                    Log.i(TAG, "List of available devices: " + deviceList);
                }

                mDevice = mService.openI2cDevice(deviceList.get(0), I2C_ADDRESS);

                HDC1080_Registers reg = new HDC1080_Registers();
                reg.RawData[0] = 0;
                reg.RawData[1] = 0;
                reg.setHumidityResolution(HumidityRes._14_BITS);
                reg.setTemperatureResolution(TemperatureRes._14_BITS);
                // set sensor resolution
                //            mDevice.writeRegWord(HDC1080_CONFIGURATION, (short) 0);
                mDevice.writeRegWord(HDC1080_CONFIGURATION, (short) (reg.RawData[0] << 8 | reg.RawData[1]));
                Integer mnfId = readManufacturerId();
                Log.w(TAG, "Manufacturer ID = 0x" + Integer.toHexString(mnfId));
                Integer devId = readDeviceId();
                Log.w(TAG, "Device ID = 0x" + Integer.toHexString(devId));
                double Temp = readTemperature();
                Log.w(TAG, "Temperature = " + String.format("%.1f", Temp) + " Celsius");
                double RH = readHumidity();
                Log.w(TAG, "Humidity = " + String.format("%.1f", RH) + " %");

            } catch (IOException e) {
                Log.w(TAG, "Unable to access I2C device", e);
            }
            init = true;
        }
    }



    public void close(){
        if (mDevice != null) {
            try {
                mDevice.close();
                mDevice = null;
            } catch (IOException e) {
                Log.w(TAG, "Unable to close I2C device", e);
            }
        }
    }
}

Credits

Manuel Alejandro Iglesias Abbatemarco

Manuel Alejandro Iglesias Abbatemarco

2 projects • 17 followers
Embedded Engineer
Contact

Comments