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"}
Committer:
sveljko
Date:
Fri Sep 02 17:44:55 2016 +0000
Revision:
81:a5df87708b9a
Parent:
68:6e311c747045
First version that works, forked from official AT&T IoT starter kit repository.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fkellermavnet 68:6e311c747045 1 /* ===================================================================
fkellermavnet 68:6e311c747045 2 Copyright © 2016, AVNET Inc.
fkellermavnet 68:6e311c747045 3
fkellermavnet 68:6e311c747045 4 Licensed under the Apache License, Version 2.0 (the "License");
fkellermavnet 68:6e311c747045 5 you may not use this file except in compliance with the License.
fkellermavnet 68:6e311c747045 6 You may obtain a copy of the License at
fkellermavnet 68:6e311c747045 7
fkellermavnet 68:6e311c747045 8 http://www.apache.org/licenses/LICENSE-2.0
fkellermavnet 68:6e311c747045 9
fkellermavnet 68:6e311c747045 10 Unless required by applicable law or agreed to in writing,
fkellermavnet 68:6e311c747045 11 software distributed under the License is distributed on an
fkellermavnet 68:6e311c747045 12 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
fkellermavnet 68:6e311c747045 13 either express or implied. See the License for the specific
fkellermavnet 68:6e311c747045 14 language governing permissions and limitations under the License.
fkellermavnet 68:6e311c747045 15
fkellermavnet 68:6e311c747045 16 ======================================================================== */
JMF 0:9d5134074d84 17
JMF 0:9d5134074d84 18 #include "HTS221.h"
JMF 0:9d5134074d84 19
JMF 0:9d5134074d84 20
JMF 0:9d5134074d84 21 // ------------------------------------------------------------------------------
JMF 0:9d5134074d84 22 //jmf -- define I2C pins and functions to read & write to I2C device
JMF 0:9d5134074d84 23
JMF 0:9d5134074d84 24 #include <string>
JMF 0:9d5134074d84 25 #include "mbed.h"
JMF 0:9d5134074d84 26
stefanrousseau 11:e6602513730f 27 #include "hardware.h"
JMF 28:886833917643 28 //I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used. Defined in a
JMF 28:886833917643 29 //common locatioin since sensors also use I2C
JMF 0:9d5134074d84 30
JMF 0:9d5134074d84 31 // Read a single unsigned char from addressToRead and return it as a unsigned char
JMF 0:9d5134074d84 32 unsigned char HTS221::readRegister(unsigned char slaveAddress, unsigned char ToRead)
JMF 0:9d5134074d84 33 {
JMF 0:9d5134074d84 34 char data = ToRead;
JMF 0:9d5134074d84 35
stefanrousseau 11:e6602513730f 36 //i2c.write(slaveAddress, &data, 1, 0);
stefanrousseau 11:e6602513730f 37 i2c.write(slaveAddress, &data, 1, 1); //by Stefan
JMF 0:9d5134074d84 38 i2c.read(slaveAddress, &data, 1, 0);
JMF 0:9d5134074d84 39 return data;
JMF 0:9d5134074d84 40 }
JMF 0:9d5134074d84 41
JMF 0:9d5134074d84 42 // Writes a single unsigned char (dataToWrite) into regToWrite
JMF 0:9d5134074d84 43 int HTS221::writeRegister(unsigned char slaveAddress, unsigned char regToWrite, unsigned char dataToWrite)
JMF 0:9d5134074d84 44 {
JMF 0:9d5134074d84 45 const char data[] = {regToWrite, dataToWrite};
JMF 0:9d5134074d84 46
JMF 0:9d5134074d84 47 return i2c.write(slaveAddress,data,2,0);
JMF 0:9d5134074d84 48 }
JMF 0:9d5134074d84 49
JMF 0:9d5134074d84 50
JMF 0:9d5134074d84 51 //jmf end
JMF 0:9d5134074d84 52 // ------------------------------------------------------------------------------
JMF 0:9d5134074d84 53
JMF 1:af7a42f7d465 54 //static inline int humidityReady(uint8_t data) {
JMF 1:af7a42f7d465 55 // return (data & 0x02);
JMF 1:af7a42f7d465 56 //}
JMF 1:af7a42f7d465 57 //static inline int temperatureReady(uint8_t data) {
JMF 1:af7a42f7d465 58 // return (data & 0x01);
JMF 1:af7a42f7d465 59 //}
JMF 0:9d5134074d84 60
JMF 0:9d5134074d84 61
JMF 0:9d5134074d84 62 HTS221::HTS221(void) : _address(HTS221_ADDRESS)
JMF 0:9d5134074d84 63 {
JMF 0:9d5134074d84 64 _temperature = 0;
JMF 0:9d5134074d84 65 _humidity = 0;
JMF 0:9d5134074d84 66 }
JMF 0:9d5134074d84 67
JMF 0:9d5134074d84 68
JMF 0:9d5134074d84 69 int HTS221::begin(void)
JMF 0:9d5134074d84 70 {
JMF 0:9d5134074d84 71 uint8_t data;
JMF 0:9d5134074d84 72
JMF 0:9d5134074d84 73 data = readRegister(_address, WHO_AM_I);
JMF 0:9d5134074d84 74 if (data == WHO_AM_I_RETURN){
JMF 0:9d5134074d84 75 if (activate()){
JMF 0:9d5134074d84 76 storeCalibration();
JMF 0:9d5134074d84 77 return data;
JMF 0:9d5134074d84 78 }
JMF 0:9d5134074d84 79 }
JMF 0:9d5134074d84 80
JMF 0:9d5134074d84 81 return 0;
JMF 0:9d5134074d84 82 }
JMF 0:9d5134074d84 83
JMF 0:9d5134074d84 84 int
JMF 0:9d5134074d84 85 HTS221::storeCalibration(void)
JMF 0:9d5134074d84 86 {
JMF 0:9d5134074d84 87 uint8_t data;
JMF 0:9d5134074d84 88 uint16_t tmp;
JMF 0:9d5134074d84 89
JMF 0:9d5134074d84 90 for (int reg=CALIB_START; reg<=CALIB_END; reg++) {
JMF 0:9d5134074d84 91 if ((reg!=CALIB_START+8) && (reg!=CALIB_START+9) && (reg!=CALIB_START+4)) {
JMF 0:9d5134074d84 92
JMF 0:9d5134074d84 93 data = readRegister(HTS221_ADDRESS, reg);
JMF 0:9d5134074d84 94
JMF 0:9d5134074d84 95 switch (reg) {
JMF 0:9d5134074d84 96 case CALIB_START:
JMF 0:9d5134074d84 97 _h0_rH = data;
JMF 0:9d5134074d84 98 break;
JMF 0:9d5134074d84 99 case CALIB_START+1:
JMF 0:9d5134074d84 100 _h1_rH = data;
JMF 0:9d5134074d84 101 break;
JMF 0:9d5134074d84 102 case CALIB_START+2:
JMF 0:9d5134074d84 103 _T0_degC = data;
JMF 0:9d5134074d84 104 break;
JMF 0:9d5134074d84 105 case CALIB_START+3:
JMF 0:9d5134074d84 106 _T1_degC = data;
JMF 0:9d5134074d84 107 break;
JMF 0:9d5134074d84 108
JMF 0:9d5134074d84 109 case CALIB_START+5:
JMF 0:9d5134074d84 110 tmp = _T0_degC;
JMF 0:9d5134074d84 111 _T0_degC = (data&0x3)<<8;
JMF 0:9d5134074d84 112 _T0_degC |= tmp;
JMF 0:9d5134074d84 113
JMF 0:9d5134074d84 114 tmp = _T1_degC;
JMF 0:9d5134074d84 115 _T1_degC = ((data&0xC)>>2)<<8;
JMF 0:9d5134074d84 116 _T1_degC |= tmp;
JMF 0:9d5134074d84 117 break;
JMF 0:9d5134074d84 118 case CALIB_START+6:
JMF 0:9d5134074d84 119 _H0_T0 = data;
JMF 0:9d5134074d84 120 break;
JMF 0:9d5134074d84 121 case CALIB_START+7:
JMF 0:9d5134074d84 122 _H0_T0 |= data<<8;
JMF 0:9d5134074d84 123 break;
JMF 0:9d5134074d84 124 case CALIB_START+0xA:
JMF 0:9d5134074d84 125 _H1_T0 = data;
JMF 0:9d5134074d84 126 break;
JMF 0:9d5134074d84 127 case CALIB_START+0xB:
JMF 0:9d5134074d84 128 _H1_T0 |= data <<8;
JMF 0:9d5134074d84 129 break;
JMF 0:9d5134074d84 130 case CALIB_START+0xC:
JMF 0:9d5134074d84 131 _T0_OUT = data;
JMF 0:9d5134074d84 132 break;
JMF 0:9d5134074d84 133 case CALIB_START+0xD:
JMF 0:9d5134074d84 134 _T0_OUT |= data << 8;
JMF 0:9d5134074d84 135 break;
JMF 0:9d5134074d84 136 case CALIB_START+0xE:
JMF 0:9d5134074d84 137 _T1_OUT = data;
JMF 0:9d5134074d84 138 break;
JMF 0:9d5134074d84 139 case CALIB_START+0xF:
JMF 0:9d5134074d84 140 _T1_OUT |= data << 8;
JMF 0:9d5134074d84 141 break;
JMF 0:9d5134074d84 142
JMF 0:9d5134074d84 143
JMF 0:9d5134074d84 144 case CALIB_START+8:
JMF 0:9d5134074d84 145 case CALIB_START+9:
JMF 0:9d5134074d84 146 case CALIB_START+4:
JMF 0:9d5134074d84 147 //DO NOTHING
JMF 0:9d5134074d84 148 break;
JMF 0:9d5134074d84 149
JMF 0:9d5134074d84 150 // to cover any possible error
JMF 0:9d5134074d84 151 default:
JMF 0:9d5134074d84 152 return false;
JMF 0:9d5134074d84 153 } /* switch */
JMF 0:9d5134074d84 154 } /* if */
JMF 0:9d5134074d84 155 } /* for */
JMF 0:9d5134074d84 156 return true;
JMF 0:9d5134074d84 157 }
JMF 0:9d5134074d84 158
JMF 0:9d5134074d84 159
JMF 0:9d5134074d84 160 int
JMF 0:9d5134074d84 161 HTS221::activate(void)
JMF 0:9d5134074d84 162 {
JMF 0:9d5134074d84 163 uint8_t data;
JMF 0:9d5134074d84 164
JMF 0:9d5134074d84 165 data = readRegister(_address, CTRL_REG1);
JMF 0:9d5134074d84 166 data |= POWER_UP;
JMF 0:9d5134074d84 167 data |= ODR0_SET;
JMF 0:9d5134074d84 168 writeRegister(_address, CTRL_REG1, data);
JMF 0:9d5134074d84 169
JMF 0:9d5134074d84 170 return true;
JMF 0:9d5134074d84 171 }
JMF 0:9d5134074d84 172
JMF 0:9d5134074d84 173
JMF 0:9d5134074d84 174 int HTS221::deactivate(void)
JMF 0:9d5134074d84 175 {
JMF 0:9d5134074d84 176 uint8_t data;
JMF 0:9d5134074d84 177
JMF 0:9d5134074d84 178 data = readRegister(_address, CTRL_REG1);
JMF 0:9d5134074d84 179 data &= ~POWER_UP;
JMF 0:9d5134074d84 180 writeRegister(_address, CTRL_REG1, data);
JMF 0:9d5134074d84 181 return true;
JMF 0:9d5134074d84 182 }
JMF 0:9d5134074d84 183
JMF 0:9d5134074d84 184
JMF 0:9d5134074d84 185 int
JMF 0:9d5134074d84 186 HTS221::bduActivate(void)
JMF 0:9d5134074d84 187 {
JMF 0:9d5134074d84 188 uint8_t data;
JMF 0:9d5134074d84 189
JMF 0:9d5134074d84 190 data = readRegister(_address, CTRL_REG1);
JMF 0:9d5134074d84 191 data |= BDU_SET;
JMF 0:9d5134074d84 192 writeRegister(_address, CTRL_REG1, data);
JMF 0:9d5134074d84 193
JMF 0:9d5134074d84 194 return true;
JMF 0:9d5134074d84 195 }
JMF 0:9d5134074d84 196
JMF 0:9d5134074d84 197
JMF 0:9d5134074d84 198 int
JMF 0:9d5134074d84 199 HTS221::bduDeactivate(void)
JMF 0:9d5134074d84 200 {
JMF 0:9d5134074d84 201 uint8_t data;
JMF 0:9d5134074d84 202
JMF 0:9d5134074d84 203 data = readRegister(_address, CTRL_REG1);
JMF 0:9d5134074d84 204 data &= ~BDU_SET;
JMF 0:9d5134074d84 205 writeRegister(_address, CTRL_REG1, data);
JMF 0:9d5134074d84 206 return true;
JMF 0:9d5134074d84 207 }
JMF 0:9d5134074d84 208
JMF 0:9d5134074d84 209
JMF 0:9d5134074d84 210 int
JMF 0:9d5134074d84 211 HTS221::readHumidity(void)
JMF 0:9d5134074d84 212 {
JMF 0:9d5134074d84 213 uint8_t data = 0;
JMF 0:9d5134074d84 214 uint16_t h_out = 0;
JMF 0:9d5134074d84 215 double h_temp = 0.0;
JMF 0:9d5134074d84 216 double hum = 0.0;
JMF 0:9d5134074d84 217
JMF 0:9d5134074d84 218 data = readRegister(_address, STATUS_REG);
JMF 0:9d5134074d84 219
JMF 0:9d5134074d84 220 if (data & HUMIDITY_READY) {
JMF 0:9d5134074d84 221 data = readRegister(_address, HUMIDITY_H_REG);
JMF 0:9d5134074d84 222 h_out = data << 8; // MSB
JMF 0:9d5134074d84 223 data = readRegister(_address, HUMIDITY_L_REG);
JMF 0:9d5134074d84 224 h_out |= data; // LSB
JMF 0:9d5134074d84 225
JMF 0:9d5134074d84 226 // Decode Humidity
JMF 0:9d5134074d84 227 hum = ((int16_t)(_h1_rH) - (int16_t)(_h0_rH))/2.0; // remove x2 multiple
JMF 0:9d5134074d84 228
JMF 0:9d5134074d84 229 // Calculate humidity in decimal of grade centigrades i.e. 15.0 = 150.
JMF 0:9d5134074d84 230 h_temp = (((int16_t)h_out - (int16_t)_H0_T0) * hum) / ((int16_t)_H1_T0 - (int16_t)_H0_T0);
JMF 0:9d5134074d84 231 hum = ((int16_t)_h0_rH) / 2.0; // remove x2 multiple
JMF 0:9d5134074d84 232 _humidity = (int16_t)((hum + h_temp)); // provide signed % measurement unit
JMF 0:9d5134074d84 233 }
JMF 0:9d5134074d84 234 return _humidity;
JMF 0:9d5134074d84 235 }
JMF 0:9d5134074d84 236
JMF 0:9d5134074d84 237
JMF 0:9d5134074d84 238
JMF 0:9d5134074d84 239 double
JMF 0:9d5134074d84 240 HTS221::readTemperature(void)
JMF 0:9d5134074d84 241 {
JMF 0:9d5134074d84 242 uint8_t data = 0;
JMF 0:9d5134074d84 243 uint16_t t_out = 0;
JMF 0:9d5134074d84 244 double t_temp = 0.0;
JMF 0:9d5134074d84 245 double deg = 0.0;
JMF 0:9d5134074d84 246
JMF 0:9d5134074d84 247 data = readRegister(_address, STATUS_REG);
JMF 0:9d5134074d84 248
JMF 0:9d5134074d84 249 if (data & TEMPERATURE_READY) {
JMF 0:9d5134074d84 250
JMF 0:9d5134074d84 251 data= readRegister(_address, TEMP_H_REG);
JMF 0:9d5134074d84 252 t_out = data << 8; // MSB
JMF 0:9d5134074d84 253 data = readRegister(_address, TEMP_L_REG);
JMF 0:9d5134074d84 254 t_out |= data; // LSB
JMF 0:9d5134074d84 255
JMF 0:9d5134074d84 256 // Decode Temperature
JMF 0:9d5134074d84 257 deg = ((int16_t)(_T1_degC) - (int16_t)(_T0_degC))/8.0; // remove x8 multiple
JMF 0:9d5134074d84 258
JMF 0:9d5134074d84 259 // Calculate Temperature in decimal of grade centigrades i.e. 15.0 = 150.
JMF 0:9d5134074d84 260 t_temp = (((int16_t)t_out - (int16_t)_T0_OUT) * deg) / ((int16_t)_T1_OUT - (int16_t)_T0_OUT);
JMF 0:9d5134074d84 261 deg = ((int16_t)_T0_degC) / 8.0; // remove x8 multiple
JMF 0:9d5134074d84 262 _temperature = deg + t_temp; // provide signed celsius measurement unit
JMF 0:9d5134074d84 263 }
JMF 0:9d5134074d84 264
JMF 0:9d5134074d84 265 return _temperature;
JMF 0:9d5134074d84 266 }