Pubnub demo for AT&T IoT Starter Kit. Functionally similar to the Flow demo.

Dependencies:   FXOS8700CQ MODSERIAL mbed

http://pubnub.github.io/slides/workshop/pictures/broadcast.png

Pubnub demo for AT&T IoT Starter Kit

This demo is functionally similar to the Flow demo, so you can find general information here: https://developer.mbed.org/users/JMF/code/Avnet_ATT_Cellular_IOT/.

The only difference is that we use Pubnub to publish the measurements and subscribe to receiving the instructions to set the LED.

Settings

Pubnub related settings are:

Pubnub settings in `config_me.h`

PUBNUB_SUBSCRIBE_KEY
PUBNUB_PUBLISH_KEY
PUBNUB_CHANNEL

All are documented in their respective comments.

Pubnub context class

Similar to Pubnub SDKs, we provide a Pubnub context class. It is defined in pubnub.h header file and implemented in pubnub.cpp.

It provides only the fundamental "publish" and "subscribe" methods. They are documented in the header file.

This class is reusable in other code (it is not specific to this demo), it has a very narrow interface to the AT&T IoT cellular modem code. For example of use, you can look at the main() (in main.c).

Sample of published data

Published message w/measurement data

{"serial":"vstarterkit001","temp":89.61,"humidity":35,"accelX":0.97,"accelY":0.013,"accelZ":-0.038}

Don't worry, nobody got burnt, the temperature is in degrees Fahrenheit. :)

Publish a message (from, say, the Pubnub console http://pubnub.com/console) of the form {"LED":<name-of-the-color>} on the channel that this demo listens to (default is hello_world) to turn the LED to that color on the Starter Kit:

Turn LED to red

{"LED":"Red"}

Turn LED to green

{"LED":"Green"}

Turn LED to blue

{"LED":"Blue"}

hts221_driver.cpp

Committer:
stefanrousseau
Date:
2016-07-12
Revision:
11:e6602513730f
Parent:
1:af7a42f7d465
Child:
28:886833917643

File content as of revision 11:e6602513730f:


#include "HTS221.h"


// ------------------------------------------------------------------------------
//jmf  -- define I2C pins and functions to read & write to I2C device

#include <string>
#include "mbed.h"

#include "hardware.h"
//I2C i2c(PTC11, PTC10);    //SDA, SCL -- define the I2C pins being used

// Read a single unsigned char from addressToRead and return it as a unsigned char
unsigned char HTS221::readRegister(unsigned char slaveAddress, unsigned char ToRead)
{
    char data = ToRead;

    //i2c.write(slaveAddress, &data, 1, 0);
    i2c.write(slaveAddress, &data, 1, 1); //by Stefan
    i2c.read(slaveAddress, &data, 1, 0);
    return data;
}

// Writes a single unsigned char (dataToWrite) into regToWrite
int HTS221::writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite)
{
    const char data[] = {regToWrite, dataToWrite};

    return i2c.write(slaveAddress,data,2,0);
}


//jmf end
// ------------------------------------------------------------------------------

//static inline int humidityReady(uint8_t data) {
//    return (data & 0x02);
//}
//static inline int temperatureReady(uint8_t data) {
//    return (data & 0x01);
//}


HTS221::HTS221(void) : _address(HTS221_ADDRESS)
{
    _temperature = 0;
    _humidity = 0;
}


int HTS221::begin(void)
{
    uint8_t data;

    data = readRegister(_address, WHO_AM_I);
    if (data == WHO_AM_I_RETURN){
        if (activate()){
            storeCalibration();
            return data;
        }
    }

    return 0;
}

int
HTS221::storeCalibration(void)
{
    uint8_t data;
    uint16_t tmp;

    for (int reg=CALIB_START; reg<=CALIB_END; reg++) {
        if ((reg!=CALIB_START+8) && (reg!=CALIB_START+9) && (reg!=CALIB_START+4)) {

            data = readRegister(HTS221_ADDRESS, reg);

            switch (reg) {
            case CALIB_START:
                _h0_rH = data;
                break;
            case CALIB_START+1:
            _h1_rH = data;
            break;
            case CALIB_START+2:
            _T0_degC = data;
            break;
            case CALIB_START+3:
            _T1_degC = data;
            break;

            case CALIB_START+5:
            tmp = _T0_degC;
            _T0_degC = (data&0x3)<<8;
            _T0_degC |= tmp;

            tmp = _T1_degC;
            _T1_degC = ((data&0xC)>>2)<<8;
            _T1_degC |= tmp;
            break;
            case CALIB_START+6:
            _H0_T0 = data;
            break;
            case CALIB_START+7:
            _H0_T0 |= data<<8;
            break;
            case CALIB_START+0xA:
            _H1_T0 = data;
            break;
            case CALIB_START+0xB:
            _H1_T0 |= data <<8;
            break;
            case CALIB_START+0xC:
            _T0_OUT = data;
            break;
            case CALIB_START+0xD:
            _T0_OUT |= data << 8;
            break;
            case CALIB_START+0xE:
            _T1_OUT = data;
            break;
            case CALIB_START+0xF:
            _T1_OUT |= data << 8;
            break;


            case CALIB_START+8:
            case CALIB_START+9:
            case CALIB_START+4:
            //DO NOTHING
            break;

            // to cover any possible error
            default:
                return false;
            } /* switch */
        } /* if */
    }  /* for */
    return true;
}


int
HTS221::activate(void)
{
    uint8_t data;

    data = readRegister(_address, CTRL_REG1);
    data |= POWER_UP;
    data |= ODR0_SET;
    writeRegister(_address, CTRL_REG1, data);

    return true;
}


int HTS221::deactivate(void)
{
    uint8_t data;

    data = readRegister(_address, CTRL_REG1);
    data &= ~POWER_UP;
    writeRegister(_address, CTRL_REG1, data);
    return true;
}


int
HTS221::bduActivate(void)
{
    uint8_t data;

    data = readRegister(_address, CTRL_REG1);
    data |= BDU_SET;
    writeRegister(_address, CTRL_REG1, data);

    return true;
}


int
HTS221::bduDeactivate(void)
{
    uint8_t data;

    data = readRegister(_address, CTRL_REG1);
    data &= ~BDU_SET;
    writeRegister(_address, CTRL_REG1, data);
    return true;
}


int
HTS221::readHumidity(void)
{
    uint8_t data   = 0;
    uint16_t h_out = 0;
    double h_temp  = 0.0;
    double hum     = 0.0;

    data = readRegister(_address, STATUS_REG);

    if (data & HUMIDITY_READY) {
        data = readRegister(_address, HUMIDITY_H_REG);
        h_out = data << 8;  // MSB
        data = readRegister(_address, HUMIDITY_L_REG);
        h_out |= data;      // LSB

        // Decode Humidity
        hum = ((int16_t)(_h1_rH) - (int16_t)(_h0_rH))/2.0;  // remove x2 multiple

        // Calculate humidity in decimal of grade centigrades i.e. 15.0 = 150.
        h_temp = (((int16_t)h_out - (int16_t)_H0_T0) * hum) / ((int16_t)_H1_T0 - (int16_t)_H0_T0);
        hum    = ((int16_t)_h0_rH) / 2.0; // remove x2 multiple
        _humidity = (int16_t)((hum + h_temp)); // provide signed % measurement unit
    }
    return _humidity;
}



double
HTS221::readTemperature(void)
{
    uint8_t data   = 0;
    uint16_t t_out = 0;
    double t_temp  = 0.0;
    double deg     = 0.0;

    data = readRegister(_address, STATUS_REG);

    if (data & TEMPERATURE_READY) {

        data= readRegister(_address, TEMP_H_REG);
        t_out = data  << 8; // MSB
        data = readRegister(_address, TEMP_L_REG);
        t_out |= data;      // LSB

        // Decode Temperature
        deg    = ((int16_t)(_T1_degC) - (int16_t)(_T0_degC))/8.0; // remove x8 multiple

        // Calculate Temperature in decimal of grade centigrades i.e. 15.0 = 150.
        t_temp = (((int16_t)t_out - (int16_t)_T0_OUT) * deg) / ((int16_t)_T1_OUT - (int16_t)_T0_OUT);
        deg    = ((int16_t)_T0_degC) / 8.0;     // remove x8 multiple
        _temperature = deg + t_temp;   // provide signed celsius measurement unit
    }

    return _temperature;
}