Skip to content

Commit

Permalink
hka5: Initial implementation
Browse files Browse the repository at this point in the history
This module implements support for the DFRobot Laser PM2.5 Sensor.  It
connects to a UART at 9600 baud.  This is the only baud rate
supported.  It optionally supports Reset and Set/Sleep gpios as well.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
  • Loading branch information
jontrulson authored and noel-eck committed Sep 14, 2016
1 parent 1b74f53 commit 21297e8
Show file tree
Hide file tree
Showing 17 changed files with 1,082 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/c++/CMakeLists.txt
Expand Up @@ -276,6 +276,7 @@ add_example (ms5611)
add_example (nmea_gps)
add_example (mma7361)
add_example (bh1750)
add_example (hka5)

# These are special cases where you specify example binary, source file and module(s)
include_directories (${PROJECT_SOURCE_DIR}/src)
Expand Down
86 changes: 86 additions & 0 deletions examples/c++/hka5.cxx
@@ -0,0 +1,86 @@
/*
* 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.
*/

#include <unistd.h>
#include <iostream>
#include <signal.h>

#include "hka5.hpp"

using namespace std;

bool shouldRun = true;

void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}

int main()
{
signal(SIGINT, sig_handler);

//! [Interesting]

// Instantiate a HKA5 sensor on uart 0. We don't use the set or
// reset pins, so we pass -1 for them.
upm::HKA5 *sensor = new upm::HKA5(0, -1, -1);

// update once every 2 seconds and output data
while (shouldRun)
{
sensor->update();

cout << "PM 1 : "
<< sensor->getPM1()
<< " ug/m3"
<< endl;

cout << "PM 2.5: "
<< sensor->getPM2_5()
<< " ug/m3"
<< endl;

cout << "PM 10 : "
<< sensor->getPM10()
<< " ug/m3"
<< endl;

cout << endl;

sleep(2);
}

if (shouldRun)
cerr << "Timed out" << endl;

//! [Interesting]

cout << "Exiting" << endl;

delete sensor;

return 0;
}
1 change: 1 addition & 0 deletions examples/c/CMakeLists.txt
Expand Up @@ -92,6 +92,7 @@ add_example (mma7361)
add_example (bh1750)
add_example (urm37)
add_example (urm37-uart)
add_example (hka5)

# Custom examples
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
78 changes: 78 additions & 0 deletions examples/c/hka5.c
@@ -0,0 +1,78 @@
/*
* 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.
*/

#include <unistd.h>
#include <signal.h>

#include "hka5.h"

bool shouldRun = true;

void sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}

int main()
{
signal(SIGINT, sig_handler);

//! [Interesting]

// Instantiate a HKA5 sensor on uart 0. We don't use the set or
// reset pins, so we pass -1 for them.
hka5_context sensor = hka5_init(0, -1, -1);

if (!sensor)
{
printf("hka5_init() failed.\n");
return 1;
}

// update once every 2 seconds and output data
while (shouldRun)
{
if (hka5_update(sensor) != UPM_SUCCESS)
{
printf("hka5_update() failed, exiting.\n");
shouldRun = false;
}

printf("PM 1 : %d ug/m3\n", hka5_get_pm1(sensor));
printf("PM 2.5: %d ug/m3\n", hka5_get_pm2_5(sensor));
printf("PM 10 : %d ug/m3\n", hka5_get_pm10(sensor));
printf("\n");

sleep(2);
}

//! [Interesting]

printf("Exiting\n");

hka5_close(sensor);

return 0;
}
1 change: 1 addition & 0 deletions examples/java/CMakeLists.txt
Expand Up @@ -133,6 +133,7 @@ add_example(BMX055_Example bmx055)
add_example(NMEAGPS_Example nmea_gps)
add_example(MMA7361_Example mma7361)
add_example(BH1750_Example bh1750)
add_example(HKA5_Example hka5)

add_example_with_path(Jhd1313m1_lcdSample lcd i2clcd)
add_example_with_path(Jhd1313m1Sample lcd i2clcd)
Expand Down
59 changes: 59 additions & 0 deletions examples/java/HKA5_Example.java
@@ -0,0 +1,59 @@
/*
* 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_hka5.HKA5;

public class HKA5_Example
{
public static void main(String[] args) throws InterruptedException
{
// ! [Interesting]
System.out.println("Initializing...");

// Instantiate a HKA5 sensor on uart 0. We don't use the set or
// reset pins, so we pass -1 for them.
HKA5 sensor = new HKA5(0, -1, -1);

// update once every 2 seconds and output data
while (true)
{
sensor.update();

System.out.println("PM 1 : "
+ sensor.getPM1()
+ " ug/m3");
System.out.println("PM 2.5: "
+ sensor.getPM2_5()
+ " ug/m3");
System.out.println("PM 10 : "
+ sensor.getPM10()
+ " ug/m3");

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

// ! [Interesting]
}
}
63 changes: 63 additions & 0 deletions examples/javascript/hka5.js
@@ -0,0 +1,63 @@
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */

/*
* 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.
*/

var sensorObj = require('jsupm_hka5');

// Instantiate a HKA5 sensor on uart 0. We don't use the set or
// reset pins, so we pass -1 for them.
var sensor = new sensorObj.HKA5(0, -1, -1);

// update once every 2 seconds and output data
setInterval(function()
{
sensor.update()

console.log("PM 1 : "
+ sensor.getPM1()
+ " ug/m3");

console.log("PM 2.5: "
+ sensor.getPM2_5()
+ " ug/m3");

console.log("PM 10 : "
+ sensor.getPM10()
+ " ug/m3");

console.log();

}, 2000);

// exit on ^C
process.on('SIGINT', function()
{
sensor = null;
sensorObj.cleanUp();
sensorObj = null;
console.log("Exiting.");
process.exit(0);
});
62 changes: 62 additions & 0 deletions examples/python/hka5.py
@@ -0,0 +1,62 @@
#!/usr/bin/python
# 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 time, sys, signal, atexit
import pyupm_hka5 as sensorObj

# Instantiate a HKA5 sensor on uart 0. We don't use the set or
# reset pins, so we pass -1 for them.
sensor = sensorObj.HKA5(0, -1, -1)

## Exit handlers ##
# This function stops python from printing a stacktrace when you hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit

# This function lets you run code on exit
def exitHandler():
print "Exiting"
sys.exit(0)

# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)

# update once every 2 seconds and output data
while (True):
sensor.update()

print "PM 1 :",
print sensor.getPM1(),
print " ug/m3"

print "PM 2.5:",
print sensor.getPM2_5(),
print " ug/m3"

print "PM 10 :",
print sensor.getPM10(),
print " ug/m3"

print
time.sleep(2)
9 changes: 9 additions & 0 deletions src/hka5/CMakeLists.txt
@@ -0,0 +1,9 @@
upm_mixed_module_init (NAME hka5
DESCRIPTION "UPM driver for DFRobot HKA5 PM2.5 particle sensor"
C_HDR hka5.h
C_SRC hka5.c
CPP_HDR hka5.hpp
CPP_SRC hka5.cxx
FTI_SRC hka5_fti.c
CPP_WRAPS_C
REQUIRES mraa)

0 comments on commit 21297e8

Please sign in to comment.