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:
stefanrousseau
Date:
Thu Aug 11 17:04:09 2016 +0000
Revision:
71:45a5e426df81
Parent:
70:24d5800f27be
Child:
72:b500e1507b5f
Added GPS to FLOW.

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 ======================================================================== */
fkellermavnet 68:6e311c747045 17
stefanrousseau 4:f83bedd9cab4 18 #include "mbed.h"
stefanrousseau 57:d184175b6b03 19 #include "sensors.h"
stefanrousseau 55:3abf9e3f42e6 20 #include "hardware.h"
stefanrousseau 69:5a3414cc7531 21 #include "config_me.h"
stefanrousseau 57:d184175b6b03 22 #include "FXOS8700CQ.h"
stefanrousseau 61:f6b93129f954 23 #include "HTS221.h"
stefanrousseau 69:5a3414cc7531 24 #include "xadow_gps.h"
stefanrousseau 61:f6b93129f954 25 #include <string>
stefanrousseau 4:f83bedd9cab4 26
stefanrousseau 4:f83bedd9cab4 27 //I2C for pmod sensors:
stefanrousseau 4:f83bedd9cab4 28 #define Si1145_PMOD_I2C_ADDR 0xC0 //this is for 7-bit addr 0x60 for the Si7020
stefanrousseau 4:f83bedd9cab4 29 #define Si7020_PMOD_I2C_ADDR 0x80 //this is for 7-bit addr 0x4 for the Si7020
stefanrousseau 4:f83bedd9cab4 30
stefanrousseau 57:d184175b6b03 31 // Storage for the data from the motion sensor
stefanrousseau 4:f83bedd9cab4 32 SRAWDATA accel_data;
stefanrousseau 4:f83bedd9cab4 33 SRAWDATA magn_data;
stefanrousseau 4:f83bedd9cab4 34 //InterruptIn fxos_int1(PTC6); // unused, common with SW2 on FRDM-K64F
stefanrousseau 4:f83bedd9cab4 35 InterruptIn fxos_int2(PTC13); // should just be the Data-Ready interrupt
stefanrousseau 4:f83bedd9cab4 36 bool fxos_int2_triggered = false;
stefanrousseau 4:f83bedd9cab4 37 void trigger_fxos_int2(void)
stefanrousseau 4:f83bedd9cab4 38 {
stefanrousseau 4:f83bedd9cab4 39 fxos_int2_triggered = true;
stefanrousseau 57:d184175b6b03 40
stefanrousseau 4:f83bedd9cab4 41 }
stefanrousseau 4:f83bedd9cab4 42
stefanrousseau 4:f83bedd9cab4 43 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 44 * Perform I2C single read.
stefanrousseau 4:f83bedd9cab4 45 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 46 unsigned char I2C_ReadSingleByte(unsigned char ucDeviceAddress)
stefanrousseau 4:f83bedd9cab4 47 {
stefanrousseau 4:f83bedd9cab4 48 char rxbuffer [1];
stefanrousseau 11:e6602513730f 49 i2c.read(ucDeviceAddress, rxbuffer, 1 );
stefanrousseau 4:f83bedd9cab4 50 return (unsigned char)rxbuffer[0];
stefanrousseau 4:f83bedd9cab4 51 } //I2C_ReadSingleByte()
stefanrousseau 4:f83bedd9cab4 52
stefanrousseau 4:f83bedd9cab4 53 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 54 * Perform I2C single read from address.
stefanrousseau 4:f83bedd9cab4 55 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 56 unsigned char I2C_ReadSingleByteFromAddr(unsigned char ucDeviceAddress, unsigned char Addr)
stefanrousseau 4:f83bedd9cab4 57 {
stefanrousseau 4:f83bedd9cab4 58 char txbuffer [1];
stefanrousseau 4:f83bedd9cab4 59 char rxbuffer [1];
stefanrousseau 4:f83bedd9cab4 60 txbuffer[0] = (char)Addr;
stefanrousseau 11:e6602513730f 61 i2c.write(ucDeviceAddress, txbuffer, 1 );
stefanrousseau 11:e6602513730f 62 i2c.read(ucDeviceAddress, rxbuffer, 1 );
stefanrousseau 4:f83bedd9cab4 63 return (unsigned char)rxbuffer[0];
stefanrousseau 4:f83bedd9cab4 64 } //I2C_ReadSingleByteFromAddr()
stefanrousseau 4:f83bedd9cab4 65
stefanrousseau 4:f83bedd9cab4 66 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 67 * Perform I2C read of more than 1 byte.
stefanrousseau 4:f83bedd9cab4 68 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 69 int I2C_ReadMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength)
stefanrousseau 4:f83bedd9cab4 70 {
stefanrousseau 4:f83bedd9cab4 71 int status;
stefanrousseau 11:e6602513730f 72 status = i2c.read(ucDeviceAddress, ucData, ucLength);
stefanrousseau 4:f83bedd9cab4 73 return status;
stefanrousseau 4:f83bedd9cab4 74 } //I2C_ReadMultipleBytes()
stefanrousseau 4:f83bedd9cab4 75
stefanrousseau 4:f83bedd9cab4 76 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 77 * Perform I2C write of a single byte.
stefanrousseau 4:f83bedd9cab4 78 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 79 int I2C_WriteSingleByte(unsigned char ucDeviceAddress, unsigned char Data, bool bSendStop)
stefanrousseau 4:f83bedd9cab4 80 {
stefanrousseau 4:f83bedd9cab4 81 int status;
stefanrousseau 4:f83bedd9cab4 82 char txbuffer [1];
stefanrousseau 4:f83bedd9cab4 83 txbuffer[0] = (char)Data; //data
stefanrousseau 11:e6602513730f 84 status = i2c.write(ucDeviceAddress, txbuffer, 1, !bSendStop); //true: do not send stop
stefanrousseau 4:f83bedd9cab4 85 return status;
stefanrousseau 4:f83bedd9cab4 86 } //I2C_WriteSingleByte()
stefanrousseau 4:f83bedd9cab4 87
stefanrousseau 4:f83bedd9cab4 88 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 89 * Perform I2C write of 1 byte to an address.
stefanrousseau 4:f83bedd9cab4 90 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 91 int I2C_WriteSingleByteToAddr(unsigned char ucDeviceAddress, unsigned char Addr, unsigned char Data, bool bSendStop)
stefanrousseau 4:f83bedd9cab4 92 {
stefanrousseau 4:f83bedd9cab4 93 int status;
stefanrousseau 4:f83bedd9cab4 94 char txbuffer [2];
stefanrousseau 4:f83bedd9cab4 95 txbuffer[0] = (char)Addr; //address
stefanrousseau 4:f83bedd9cab4 96 txbuffer[1] = (char)Data; //data
stefanrousseau 11:e6602513730f 97 //status = i2c.write(ucDeviceAddress, txbuffer, 2, false); //stop at end
stefanrousseau 11:e6602513730f 98 status = i2c.write(ucDeviceAddress, txbuffer, 2, !bSendStop); //true: do not send stop
stefanrousseau 4:f83bedd9cab4 99 return status;
stefanrousseau 4:f83bedd9cab4 100 } //I2C_WriteSingleByteToAddr()
stefanrousseau 4:f83bedd9cab4 101
stefanrousseau 4:f83bedd9cab4 102 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 103 * Perform I2C write of more than 1 byte.
stefanrousseau 4:f83bedd9cab4 104 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 105 int I2C_WriteMultipleBytes(unsigned char ucDeviceAddress, char *ucData, unsigned char ucLength, bool bSendStop)
stefanrousseau 4:f83bedd9cab4 106 {
stefanrousseau 4:f83bedd9cab4 107 int status;
stefanrousseau 11:e6602513730f 108 status = i2c.write(ucDeviceAddress, ucData, ucLength, !bSendStop); //true: do not send stop
stefanrousseau 4:f83bedd9cab4 109 return status;
stefanrousseau 4:f83bedd9cab4 110 } //I2C_WriteMultipleBytes()
stefanrousseau 4:f83bedd9cab4 111
stefanrousseau 4:f83bedd9cab4 112 bool bSi7020_present = false;
stefanrousseau 4:f83bedd9cab4 113 void Init_Si7020(void)
stefanrousseau 4:f83bedd9cab4 114 {
stefanrousseau 4:f83bedd9cab4 115 char SN_7020 [8];
stefanrousseau 4:f83bedd9cab4 116 //SN part 1:
stefanrousseau 4:f83bedd9cab4 117 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFA, 0x0F, false);
stefanrousseau 4:f83bedd9cab4 118 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[0], 4);
stefanrousseau 4:f83bedd9cab4 119
stefanrousseau 4:f83bedd9cab4 120 //SN part 1:
stefanrousseau 4:f83bedd9cab4 121 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0xFC, 0xC9, false);
stefanrousseau 4:f83bedd9cab4 122 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &SN_7020[4], 4);
stefanrousseau 4:f83bedd9cab4 123
stefanrousseau 4:f83bedd9cab4 124 char Ver_7020 [2];
stefanrousseau 4:f83bedd9cab4 125 //FW version:
stefanrousseau 4:f83bedd9cab4 126 I2C_WriteSingleByteToAddr(Si7020_PMOD_I2C_ADDR, 0x84, 0xB8, false);
stefanrousseau 4:f83bedd9cab4 127 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Ver_7020[0], 2);
stefanrousseau 4:f83bedd9cab4 128
stefanrousseau 4:f83bedd9cab4 129 if (SN_7020[4] != 0x14)
stefanrousseau 4:f83bedd9cab4 130 {
stefanrousseau 4:f83bedd9cab4 131 bSi7020_present = false;
stefanrousseau 64:09004cd610df 132 PRINTF("Si7020 sensor not found\n");
stefanrousseau 4:f83bedd9cab4 133 }
stefanrousseau 4:f83bedd9cab4 134 else
stefanrousseau 4:f83bedd9cab4 135 {
stefanrousseau 4:f83bedd9cab4 136 bSi7020_present = true;
stefanrousseau 64:09004cd610df 137 PRINTF("Si7020 SN = 0x%02X%02X%02X%02X%02X%02X%02X%02X\n", SN_7020[0], SN_7020[1], SN_7020[2], SN_7020[3], SN_7020[4], SN_7020[5], SN_7020[6], SN_7020[7]);
stefanrousseau 64:09004cd610df 138 PRINTF("Si7020 Version# = 0x%02X\n", Ver_7020[0]);
stefanrousseau 4:f83bedd9cab4 139 } //bool bSi7020_present = true
stefanrousseau 4:f83bedd9cab4 140
stefanrousseau 4:f83bedd9cab4 141 } //Init_Si7020()
stefanrousseau 4:f83bedd9cab4 142
stefanrousseau 4:f83bedd9cab4 143 void Read_Si7020(void)
stefanrousseau 4:f83bedd9cab4 144 {
stefanrousseau 4:f83bedd9cab4 145 if (bSi7020_present)
stefanrousseau 4:f83bedd9cab4 146 {
stefanrousseau 4:f83bedd9cab4 147 char Humidity [2];
stefanrousseau 4:f83bedd9cab4 148 char Temperature [2];
stefanrousseau 4:f83bedd9cab4 149 //Command to measure humidity (temperature also gets measured):
stefanrousseau 4:f83bedd9cab4 150 I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xF5, false); //no hold, must do dummy read
stefanrousseau 4:f83bedd9cab4 151 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 1); //dummy read, should get an nack until it is done
stefanrousseau 4:f83bedd9cab4 152 wait (0.05); //wait for measurement. Can also keep reading until no NACK is received
stefanrousseau 4:f83bedd9cab4 153 //I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE5, false); //Hold mod, the device does a clock stretch on the read until it is done (crashes the I2C bus...
stefanrousseau 4:f83bedd9cab4 154 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Humidity[0], 2); //read humidity
stefanrousseau 64:09004cd610df 155 //PRINTF("Read Si7020 Humidity = 0x%02X%02X\n", Humidity[0], Humidity[1]);
stefanrousseau 4:f83bedd9cab4 156 int rh_code = (Humidity[0] << 8) + Humidity[1];
stefanrousseau 4:f83bedd9cab4 157 float fRh = (125.0*rh_code/65536.0) - 6.0; //from datasheet
stefanrousseau 64:09004cd610df 158 //PRINTF("Si7020 Humidity = %*.*f %%\n", 4, 2, fRh); //double % sign for escape //PRINTF("%*.*f\n", myFieldWidth, myPrecision, myFloatValue);
stefanrousseau 4:f83bedd9cab4 159 sprintf(SENSOR_DATA.Humidity_Si7020, "%0.2f", fRh);
stefanrousseau 4:f83bedd9cab4 160
stefanrousseau 4:f83bedd9cab4 161 //Command to read temperature when humidity is already done:
stefanrousseau 4:f83bedd9cab4 162 I2C_WriteSingleByte(Si7020_PMOD_I2C_ADDR, 0xE0, false);
stefanrousseau 4:f83bedd9cab4 163 I2C_ReadMultipleBytes(Si7020_PMOD_I2C_ADDR, &Temperature[0], 2); //read temperature
stefanrousseau 64:09004cd610df 164 //PRINTF("Read Si7020 Temperature = 0x%02X%02X\n", Temperature[0], Temperature[1]);
stefanrousseau 4:f83bedd9cab4 165 int temp_code = (Temperature[0] << 8) + Temperature[1];
stefanrousseau 4:f83bedd9cab4 166 float fTemp = (175.72*temp_code/65536.0) - 46.85; //from datasheet in Celcius
stefanrousseau 64:09004cd610df 167 //PRINTF("Si7020 Temperature = %*.*f deg C\n", 4, 2, fTemp);
stefanrousseau 4:f83bedd9cab4 168 sprintf(SENSOR_DATA.Temperature_Si7020, "%0.2f", fTemp);
stefanrousseau 4:f83bedd9cab4 169 } //bool bSi7020_present = true
stefanrousseau 4:f83bedd9cab4 170
stefanrousseau 4:f83bedd9cab4 171 } //Read_Si7020()
stefanrousseau 4:f83bedd9cab4 172
stefanrousseau 4:f83bedd9cab4 173 /*------------------------------------------------------------------------------
stefanrousseau 4:f83bedd9cab4 174 * The following are aliases so that the Si1145 coding examples can be used as-is.
stefanrousseau 4:f83bedd9cab4 175 *------------------------------------------------------------------------------*/
stefanrousseau 4:f83bedd9cab4 176 unsigned char ReadFrom_Si1145_Register(unsigned char reg) //returns byte from I2C Register 'reg'
stefanrousseau 4:f83bedd9cab4 177 {
stefanrousseau 4:f83bedd9cab4 178 unsigned char result = I2C_ReadSingleByteFromAddr(Si1145_PMOD_I2C_ADDR, reg);
stefanrousseau 4:f83bedd9cab4 179 return (result);
stefanrousseau 4:f83bedd9cab4 180 } //ReadFrom_Si1145_Register()
stefanrousseau 4:f83bedd9cab4 181
stefanrousseau 4:f83bedd9cab4 182 void WriteTo_Si1145_Register(unsigned char reg, unsigned char value) //writes 'value' into I2C Register reg'
stefanrousseau 4:f83bedd9cab4 183 {
stefanrousseau 4:f83bedd9cab4 184 I2C_WriteSingleByteToAddr(Si1145_PMOD_I2C_ADDR, reg, value, true);
stefanrousseau 4:f83bedd9cab4 185 } //WriteTo_Si1145_Register()
stefanrousseau 4:f83bedd9cab4 186
stefanrousseau 4:f83bedd9cab4 187 #define REG_PARAM_WR 0x17
stefanrousseau 4:f83bedd9cab4 188 #define REG_PARAM_RD 0x2E
stefanrousseau 4:f83bedd9cab4 189 #define REG_COMMAND 0x18
stefanrousseau 4:f83bedd9cab4 190 #define REG_RESPONSE 0x20
stefanrousseau 4:f83bedd9cab4 191 #define REG_HW_KEY 0x07
stefanrousseau 4:f83bedd9cab4 192 #define HW_KEY_VAL0 0x17
stefanrousseau 4:f83bedd9cab4 193 #define REG_MEAS_RATE_LSB 0x08
stefanrousseau 4:f83bedd9cab4 194 #define REG_MEAS_RATE_MSB 0x09
stefanrousseau 4:f83bedd9cab4 195 #define REG_PS_LED21 0x0F
stefanrousseau 4:f83bedd9cab4 196 #define REG_PS_LED3 0x10
stefanrousseau 4:f83bedd9cab4 197 #define MAX_LED_CURRENT 0xF
stefanrousseau 4:f83bedd9cab4 198 #define PARAM_CH_LIST 0x01
stefanrousseau 4:f83bedd9cab4 199 #define REG_ALS_VIS_DATA0 0x22
stefanrousseau 4:f83bedd9cab4 200 #define REG_ALS_VIS_DATA1 0x23
stefanrousseau 4:f83bedd9cab4 201 #define REG_ALS_IR_DATA0 0x24
stefanrousseau 4:f83bedd9cab4 202 #define REG_ALS_IR_DATA1 0x25
stefanrousseau 4:f83bedd9cab4 203 #define REG_PS1_DATA0 0x26
stefanrousseau 4:f83bedd9cab4 204 #define REG_PS1_DATA1 0x27
stefanrousseau 4:f83bedd9cab4 205 #define REG_PS2_DATA0 0x28
stefanrousseau 4:f83bedd9cab4 206 #define REG_PS2_DATA1 0x29
stefanrousseau 4:f83bedd9cab4 207 #define REG_PS3_DATA0 0x2A
stefanrousseau 4:f83bedd9cab4 208 #define REG_PS3_DATA1 0x2B
stefanrousseau 4:f83bedd9cab4 209 #define REG_UVINDEX0 0x2C
stefanrousseau 4:f83bedd9cab4 210 #define REG_UVINDEX1 0x2D
stefanrousseau 4:f83bedd9cab4 211 int Si1145_ParamSet(unsigned char address, unsigned char value) //writes 'value' into Parameter 'address'
stefanrousseau 4:f83bedd9cab4 212 {
stefanrousseau 4:f83bedd9cab4 213 char txbuffer [3];
stefanrousseau 4:f83bedd9cab4 214 txbuffer[0] = (char)REG_PARAM_WR; //destination
stefanrousseau 4:f83bedd9cab4 215 txbuffer[1] = (char)value;
stefanrousseau 4:f83bedd9cab4 216 txbuffer[2] = (char)(0xA0 + (address & 0x1F));
stefanrousseau 4:f83bedd9cab4 217 int retval;
stefanrousseau 4:f83bedd9cab4 218 //if((retval = _waitUntilSleep(si114x_handle))!=0) return retval;
stefanrousseau 4:f83bedd9cab4 219 retval = I2C_WriteMultipleBytes(Si1145_PMOD_I2C_ADDR, &txbuffer[0], 3, true);
stefanrousseau 4:f83bedd9cab4 220 if(retval!=0) return retval;
stefanrousseau 4:f83bedd9cab4 221 while(1)
stefanrousseau 4:f83bedd9cab4 222 {
stefanrousseau 4:f83bedd9cab4 223 retval=ReadFrom_Si1145_Register(REG_PARAM_RD);
stefanrousseau 4:f83bedd9cab4 224 if (retval==value) break;
stefanrousseau 4:f83bedd9cab4 225 }
stefanrousseau 4:f83bedd9cab4 226 return (0);
stefanrousseau 4:f83bedd9cab4 227 } //Si1145_ParamSet()
stefanrousseau 4:f83bedd9cab4 228
stefanrousseau 4:f83bedd9cab4 229 void PsAlsForce(void) //equivalent to WriteTo_Si1145_Register(REG_COMMAND,0x07). This forces PS and ALS measurements
stefanrousseau 4:f83bedd9cab4 230 {
stefanrousseau 4:f83bedd9cab4 231 WriteTo_Si1145_Register(REG_COMMAND,0x07);
stefanrousseau 4:f83bedd9cab4 232 } //PsAlsForce()
stefanrousseau 4:f83bedd9cab4 233
stefanrousseau 4:f83bedd9cab4 234 bool bSi1145_present = false;
stefanrousseau 4:f83bedd9cab4 235 void Init_Si1145(void)
stefanrousseau 4:f83bedd9cab4 236 {
stefanrousseau 4:f83bedd9cab4 237 unsigned char readbyte;
stefanrousseau 4:f83bedd9cab4 238 //Read Si1145 part ID:
stefanrousseau 4:f83bedd9cab4 239 readbyte = ReadFrom_Si1145_Register(0x00);
stefanrousseau 4:f83bedd9cab4 240 if (readbyte != 0x45)
stefanrousseau 4:f83bedd9cab4 241 {
stefanrousseau 4:f83bedd9cab4 242 bSi1145_present = false;
stefanrousseau 64:09004cd610df 243 PRINTF("Si1145 sensor not found\n");
stefanrousseau 4:f83bedd9cab4 244 }
stefanrousseau 4:f83bedd9cab4 245 else
stefanrousseau 4:f83bedd9cab4 246 {
stefanrousseau 4:f83bedd9cab4 247 bSi1145_present = true;
stefanrousseau 64:09004cd610df 248 PRINTF("Si1145 Part ID : 0x%02X\n", readbyte);
stefanrousseau 4:f83bedd9cab4 249 //Initialize Si1145 by writing to HW_KEY (I2C Register 0x07 = 0x17)
stefanrousseau 4:f83bedd9cab4 250 WriteTo_Si1145_Register(REG_HW_KEY, HW_KEY_VAL0);
stefanrousseau 4:f83bedd9cab4 251
stefanrousseau 4:f83bedd9cab4 252 // Initialize LED Current
stefanrousseau 4:f83bedd9cab4 253 // I2C Register 0x0F = 0xFF
stefanrousseau 4:f83bedd9cab4 254 // I2C Register 0x10 = 0x0F
stefanrousseau 4:f83bedd9cab4 255 WriteTo_Si1145_Register(REG_PS_LED21,(MAX_LED_CURRENT<<4) + MAX_LED_CURRENT);
stefanrousseau 4:f83bedd9cab4 256 WriteTo_Si1145_Register(REG_PS_LED3, MAX_LED_CURRENT);
stefanrousseau 4:f83bedd9cab4 257
stefanrousseau 4:f83bedd9cab4 258 // Parameter 0x01 = 0x37
stefanrousseau 4:f83bedd9cab4 259 //Si1145_ParamSet(PARAM_CH_LIST, ALS_IR_TASK + ALS_VIS_TASK + PS1_TASK + PS2_TASK + PS3_TASK);
stefanrousseau 4:f83bedd9cab4 260 //Si1145_ParamSet(0x01, 0x37); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, some)
stefanrousseau 4:f83bedd9cab4 261 Si1145_ParamSet(0x01, 0x7F); //CHLIST is address 0x01 in the parameter RAM. It defines which sensors are enabled (here, all but UV. One can only use AUX or UV but here we use AUX because UV does not work...)
stefanrousseau 4:f83bedd9cab4 262 // I2C Register 0x18 = 0x0x07 //This is PSALS_FORCE to the Command register => Force a single PS (proximity sensor) and ALS (ambient light sensor) reading - The factory code has this as 0x05 which only does PS...
stefanrousseau 4:f83bedd9cab4 263 PsAlsForce(); // can also be written as WriteTo_Si1145_Register(REG_COMMAND,0x07);
stefanrousseau 4:f83bedd9cab4 264 WriteTo_Si1145_Register(REG_COMMAND, 0x0F);//command to put it into auto mode
stefanrousseau 4:f83bedd9cab4 265 //Set MES_RATE to 0x1000. I.e. the device will automatically wake up every 16 * 256* 31.25 us = 0.128 seconds to measure
stefanrousseau 4:f83bedd9cab4 266 WriteTo_Si1145_Register(REG_MEAS_RATE_LSB, 0x00);
stefanrousseau 4:f83bedd9cab4 267 WriteTo_Si1145_Register(REG_MEAS_RATE_MSB, 0x10);
stefanrousseau 4:f83bedd9cab4 268 } //bSi1145_present = true
stefanrousseau 4:f83bedd9cab4 269 } //Init_Si1145()
stefanrousseau 4:f83bedd9cab4 270
stefanrousseau 4:f83bedd9cab4 271 void Read_Si1145(void)
stefanrousseau 4:f83bedd9cab4 272 {
stefanrousseau 4:f83bedd9cab4 273 if (bSi1145_present)
stefanrousseau 4:f83bedd9cab4 274 {
stefanrousseau 4:f83bedd9cab4 275 // Once the measurements are completed, here is how to reconstruct them
stefanrousseau 4:f83bedd9cab4 276 // Note very carefully that 16-bit registers are in the 'Little Endian' byte order
stefanrousseau 4:f83bedd9cab4 277 // It may be more efficient to perform block I2C Reads, but this example shows
stefanrousseau 4:f83bedd9cab4 278 // individual reads of registers
stefanrousseau 4:f83bedd9cab4 279
stefanrousseau 4:f83bedd9cab4 280 int PS1 = ReadFrom_Si1145_Register(REG_PS1_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS1_DATA1);
stefanrousseau 4:f83bedd9cab4 281 int PS2 = ReadFrom_Si1145_Register(REG_PS2_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS2_DATA1);
stefanrousseau 4:f83bedd9cab4 282 int PS3 = ReadFrom_Si1145_Register(REG_PS3_DATA0) + 256 * ReadFrom_Si1145_Register(REG_PS3_DATA1);
stefanrousseau 64:09004cd610df 283 //PRINTF("PS1_Data = %d\n", PS1);
stefanrousseau 64:09004cd610df 284 //PRINTF("PS2_Data = %d\n", PS2);
stefanrousseau 64:09004cd610df 285 //PRINTF("PS3_Data = %d\n", PS3);
stefanrousseau 4:f83bedd9cab4 286 //OBJECT PRESENT?
stefanrousseau 57:d184175b6b03 287 #if (0)
stefanrousseau 4:f83bedd9cab4 288 if(PS1 < 22000){
stefanrousseau 64:09004cd610df 289 //PRINTF("Object Far\n");
stefanrousseau 57:d184175b6b03 290 sprintf(SENSOR_DATA.Proximity, "Object Far\0");
stefanrousseau 4:f83bedd9cab4 291 }
stefanrousseau 4:f83bedd9cab4 292 else if(PS1 < 24000)
stefanrousseau 4:f83bedd9cab4 293 {
stefanrousseau 64:09004cd610df 294 //PRINTF("Object in Vicinity\n");
stefanrousseau 57:d184175b6b03 295 sprintf(SENSOR_DATA.Proximity, "Object in Vicinity\0");
stefanrousseau 4:f83bedd9cab4 296 }
stefanrousseau 4:f83bedd9cab4 297 else if (PS1 < 30000)
stefanrousseau 4:f83bedd9cab4 298 {
stefanrousseau 64:09004cd610df 299 //PRINTF("Object Near\n");
stefanrousseau 57:d184175b6b03 300 sprintf(SENSOR_DATA.Proximity, "Object Near\0");
stefanrousseau 4:f83bedd9cab4 301 }
stefanrousseau 4:f83bedd9cab4 302 else
stefanrousseau 4:f83bedd9cab4 303 {
stefanrousseau 64:09004cd610df 304 //PRINTF("Object Very Near\n");
stefanrousseau 57:d184175b6b03 305 sprintf(SENSOR_DATA.Proximity, "Object Very Near\0");
stefanrousseau 4:f83bedd9cab4 306 }
stefanrousseau 57:d184175b6b03 307 #else
stefanrousseau 57:d184175b6b03 308 sprintf(SENSOR_DATA.Proximity, "%d\0", PS1);
stefanrousseau 57:d184175b6b03 309 #endif
stefanrousseau 4:f83bedd9cab4 310
stefanrousseau 4:f83bedd9cab4 311 //Force ALS read:
stefanrousseau 4:f83bedd9cab4 312 //WriteTo_Si1145_Register(REG_COMMAND, 0x06);
stefanrousseau 4:f83bedd9cab4 313 //wait (0.1);
stefanrousseau 4:f83bedd9cab4 314 int ALS_VIS = ReadFrom_Si1145_Register(REG_ALS_VIS_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_VIS_DATA1);
stefanrousseau 4:f83bedd9cab4 315 int ALS_IR = ReadFrom_Si1145_Register(REG_ALS_IR_DATA0) + 256 * ReadFrom_Si1145_Register(REG_ALS_IR_DATA1);
stefanrousseau 4:f83bedd9cab4 316 int UV_INDEX = ReadFrom_Si1145_Register(REG_UVINDEX0) + 256 * ReadFrom_Si1145_Register(REG_UVINDEX1);
stefanrousseau 64:09004cd610df 317 //PRINTF("ALS_VIS_Data = %d\n", ALS_VIS);
stefanrousseau 64:09004cd610df 318 //PRINTF("ALS_IR_Data = %d\n", ALS_IR);
stefanrousseau 64:09004cd610df 319 //PRINTF("UV_INDEX_Data = %d\n", UV_INDEX);
stefanrousseau 4:f83bedd9cab4 320
stefanrousseau 64:09004cd610df 321 //PRINTF("Ambient Light Visible Sensor = %d\n", ALS_VIS);
stefanrousseau 4:f83bedd9cab4 322 sprintf(SENSOR_DATA.AmbientLightVis, "%d", ALS_VIS);
stefanrousseau 64:09004cd610df 323 //PRINTF("Ambient Light Infrared Sensor = %d\n", ALS_IR);
stefanrousseau 4:f83bedd9cab4 324 sprintf(SENSOR_DATA.AmbientLightIr, "%d", ALS_IR);
stefanrousseau 4:f83bedd9cab4 325 //float fUV_value = (UV_INDEX -50.0)/10000.0;
stefanrousseau 4:f83bedd9cab4 326 float fUV_value = (UV_INDEX)/100.0; //this is the aux reading
stefanrousseau 64:09004cd610df 327 //PRINTF("UV_Data = %0.2f\n", fUV_value);
stefanrousseau 4:f83bedd9cab4 328 sprintf(SENSOR_DATA.UVindex, "%0.2f", fUV_value);
stefanrousseau 4:f83bedd9cab4 329 } //bSi1145_present = true
stefanrousseau 4:f83bedd9cab4 330 } //Read_Si1145()
stefanrousseau 4:f83bedd9cab4 331
stefanrousseau 4:f83bedd9cab4 332 //********************************************************************************************************************************************
stefanrousseau 4:f83bedd9cab4 333 //* Read the FXOS8700CQ - 6-axis combo Sensor Accelerometer and Magnetometer
stefanrousseau 4:f83bedd9cab4 334 //********************************************************************************************************************************************
stefanrousseau 11:e6602513730f 335 bool bMotionSensor_present = false;
stefanrousseau 61:f6b93129f954 336 void Init_motion_sensor()
stefanrousseau 4:f83bedd9cab4 337 {
stefanrousseau 57:d184175b6b03 338 // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus...
stefanrousseau 57:d184175b6b03 339 // Class instantiation with pin names for the motion sensor on the FRDM-K64F board:
stefanrousseau 57:d184175b6b03 340 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
stefanrousseau 11:e6602513730f 341 int iWhoAmI = fxos.get_whoami();
stefanrousseau 57:d184175b6b03 342
stefanrousseau 64:09004cd610df 343 PRINTF("FXOS8700CQ WhoAmI = %X\r\n", iWhoAmI);
stefanrousseau 4:f83bedd9cab4 344 // Iterrupt for active-low interrupt line from FXOS
stefanrousseau 4:f83bedd9cab4 345 // Configured with only one interrupt on INT2 signaling Data-Ready
stefanrousseau 4:f83bedd9cab4 346 //fxos_int2.fall(&trigger_fxos_int2);
stefanrousseau 11:e6602513730f 347 if (iWhoAmI != 0xC7)
stefanrousseau 11:e6602513730f 348 {
stefanrousseau 11:e6602513730f 349 bMotionSensor_present = false;
stefanrousseau 64:09004cd610df 350 PRINTF("FXOS8700CQ motion sensor not found\n");
stefanrousseau 11:e6602513730f 351 }
stefanrousseau 11:e6602513730f 352 else
stefanrousseau 11:e6602513730f 353 {
stefanrousseau 11:e6602513730f 354 bMotionSensor_present = true;
stefanrousseau 11:e6602513730f 355 fxos.enable();
stefanrousseau 11:e6602513730f 356 }
stefanrousseau 61:f6b93129f954 357 } //Init_motion_sensor()
stefanrousseau 4:f83bedd9cab4 358
stefanrousseau 61:f6b93129f954 359 void Read_motion_sensor()
stefanrousseau 11:e6602513730f 360 {
stefanrousseau 57:d184175b6b03 361 // Note: this class is instantiated here because if it is statically declared, the cellular shield init kills the I2C bus...
stefanrousseau 57:d184175b6b03 362 // Class instantiation with pin names for the motion sensor on the FRDM-K64F board:
stefanrousseau 57:d184175b6b03 363 FXOS8700CQ fxos(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1); // SDA, SCL, (addr << 1)
stefanrousseau 11:e6602513730f 364 if (bMotionSensor_present)
stefanrousseau 11:e6602513730f 365 {
stefanrousseau 57:d184175b6b03 366 fxos.enable();
stefanrousseau 11:e6602513730f 367 fxos.get_data(&accel_data, &magn_data);
stefanrousseau 64:09004cd610df 368 //PRINTF("Roll=%5d, Pitch=%5d, Yaw=%5d;\r\n", magn_data.x, magn_data.y, magn_data.z);
stefanrousseau 11:e6602513730f 369 sprintf(SENSOR_DATA.MagnetometerX, "%5d", magn_data.x);
stefanrousseau 11:e6602513730f 370 sprintf(SENSOR_DATA.MagnetometerY, "%5d", magn_data.y);
stefanrousseau 11:e6602513730f 371 sprintf(SENSOR_DATA.MagnetometerZ, "%5d", magn_data.z);
stefanrousseau 11:e6602513730f 372
stefanrousseau 11:e6602513730f 373 //Try to normalize (/2048) the values so they will match the eCompass output:
stefanrousseau 11:e6602513730f 374 float fAccelScaled_x, fAccelScaled_y, fAccelScaled_z;
stefanrousseau 11:e6602513730f 375 fAccelScaled_x = (accel_data.x/2048.0);
stefanrousseau 11:e6602513730f 376 fAccelScaled_y = (accel_data.y/2048.0);
stefanrousseau 11:e6602513730f 377 fAccelScaled_z = (accel_data.z/2048.0);
stefanrousseau 64:09004cd610df 378 //PRINTF("Acc: X=%2.3f Y=%2.3f Z=%2.3f;\r\n", fAccelScaled_x, fAccelScaled_y, fAccelScaled_z);
stefanrousseau 11:e6602513730f 379 sprintf(SENSOR_DATA.AccelX, "%2.3f", fAccelScaled_x);
stefanrousseau 11:e6602513730f 380 sprintf(SENSOR_DATA.AccelY, "%2.3f", fAccelScaled_y);
stefanrousseau 11:e6602513730f 381 sprintf(SENSOR_DATA.AccelZ, "%2.3f", fAccelScaled_z);
stefanrousseau 11:e6602513730f 382 } //bMotionSensor_present
stefanrousseau 61:f6b93129f954 383 } //Read_motion_sensor()
stefanrousseau 61:f6b93129f954 384
stefanrousseau 61:f6b93129f954 385
stefanrousseau 61:f6b93129f954 386 //********************************************************************************************************************************************
stefanrousseau 61:f6b93129f954 387 //* Read the HTS221 temperature & humidity sensor on the Cellular Shield
stefanrousseau 61:f6b93129f954 388 //********************************************************************************************************************************************
stefanrousseau 61:f6b93129f954 389 // These are to be built on the fly
stefanrousseau 61:f6b93129f954 390 string my_temp;
stefanrousseau 61:f6b93129f954 391 string my_humidity;
stefanrousseau 61:f6b93129f954 392 HTS221 hts221;
stefanrousseau 11:e6602513730f 393
stefanrousseau 61:f6b93129f954 394 #define CTOF(x) ((x)*1.8+32)
stefanrousseau 61:f6b93129f954 395 bool bHTS221_present = false;
stefanrousseau 61:f6b93129f954 396 void Init_HTS221()
stefanrousseau 61:f6b93129f954 397 {
stefanrousseau 61:f6b93129f954 398 int i;
stefanrousseau 61:f6b93129f954 399 void hts221_init(void);
stefanrousseau 61:f6b93129f954 400 i = hts221.begin();
stefanrousseau 61:f6b93129f954 401 if (i)
stefanrousseau 61:f6b93129f954 402 {
stefanrousseau 61:f6b93129f954 403 bHTS221_present = true;
stefanrousseau 64:09004cd610df 404 PRINTF(BLU "HTS221 Detected (0x%02X)\n\r",i);
stefanrousseau 64:09004cd610df 405 PRINTF(" Temp is: %0.2f F \n\r",CTOF(hts221.readTemperature()));
stefanrousseau 64:09004cd610df 406 PRINTF(" Humid is: %02d %%\n\r",hts221.readHumidity());
stefanrousseau 61:f6b93129f954 407 }
stefanrousseau 61:f6b93129f954 408 else
stefanrousseau 61:f6b93129f954 409 {
stefanrousseau 61:f6b93129f954 410 bHTS221_present = false;
stefanrousseau 64:09004cd610df 411 PRINTF(RED "HTS221 NOT DETECTED!\n\r");
stefanrousseau 61:f6b93129f954 412 }
stefanrousseau 61:f6b93129f954 413 } //Init_HTS221()
stefanrousseau 61:f6b93129f954 414
stefanrousseau 61:f6b93129f954 415 void Read_HTS221()
stefanrousseau 61:f6b93129f954 416 {
stefanrousseau 61:f6b93129f954 417 if (bHTS221_present)
stefanrousseau 61:f6b93129f954 418 {
stefanrousseau 61:f6b93129f954 419 sprintf(SENSOR_DATA.Temperature, "%0.2f", CTOF(hts221.readTemperature()));
stefanrousseau 61:f6b93129f954 420 sprintf(SENSOR_DATA.Humidity, "%02d", hts221.readHumidity());
stefanrousseau 61:f6b93129f954 421 } //bHTS221_present
stefanrousseau 61:f6b93129f954 422 } //Read_HTS221()
stefanrousseau 55:3abf9e3f42e6 423
stefanrousseau 69:5a3414cc7531 424 bool bGPS_present = false;
stefanrousseau 69:5a3414cc7531 425 void Init_GPS(void)
stefanrousseau 69:5a3414cc7531 426 {
stefanrousseau 69:5a3414cc7531 427 char scan_id[GPS_SCAN_SIZE+2]; //The first two bytes are the response length (0x00, 0x04)
stefanrousseau 71:45a5e426df81 428 I2C_WriteSingleByte(GPS_DEVICE_ADDR, GPS_SCAN_ID, true); //no hold, must do read
stefanrousseau 69:5a3414cc7531 429
stefanrousseau 69:5a3414cc7531 430 unsigned char i;
stefanrousseau 69:5a3414cc7531 431 for(i=0;i<(GPS_SCAN_SIZE+2);i++)
stefanrousseau 69:5a3414cc7531 432 {
stefanrousseau 69:5a3414cc7531 433 scan_id[i] = I2C_ReadSingleByte(GPS_DEVICE_ADDR);
stefanrousseau 69:5a3414cc7531 434 }
stefanrousseau 69:5a3414cc7531 435
stefanrousseau 69:5a3414cc7531 436 if(scan_id[5] != GPS_DEVICE_ID)
stefanrousseau 69:5a3414cc7531 437 {
stefanrousseau 69:5a3414cc7531 438 bGPS_present = false;
stefanrousseau 69:5a3414cc7531 439 PRINTF("Xadow GPS not found\n");
stefanrousseau 69:5a3414cc7531 440 }
stefanrousseau 69:5a3414cc7531 441 else
stefanrousseau 69:5a3414cc7531 442 {
stefanrousseau 69:5a3414cc7531 443 bGPS_present = true;
stefanrousseau 69:5a3414cc7531 444 PRINTF("Xadow GPS Scan ID response = 0x%02X%02X (length), 0x%02X%02X%02X%02X\r\n", scan_id[0], scan_id[1], scan_id[2], scan_id[3], scan_id[4], scan_id[5]);
stefanrousseau 69:5a3414cc7531 445 char status = gps_get_status();
stefanrousseau 70:24d5800f27be 446 if ((status != 'A') && (iSensorsToReport == TEMP_HUMIDITY_ACCELEROMETER_GPS))
stefanrousseau 69:5a3414cc7531 447 { //we must wait for GPS to initialize
stefanrousseau 69:5a3414cc7531 448 PRINTF("Waiting for GPS to become ready... ");
stefanrousseau 69:5a3414cc7531 449 while (status != 'A')
stefanrousseau 69:5a3414cc7531 450 {
stefanrousseau 69:5a3414cc7531 451 wait (5.0);
stefanrousseau 70:24d5800f27be 452 status = gps_get_status();
stefanrousseau 69:5a3414cc7531 453 unsigned char num_satellites = gps_get_sate_in_veiw();
stefanrousseau 69:5a3414cc7531 454 PRINTF("%c%d", status, num_satellites);
stefanrousseau 69:5a3414cc7531 455 }
stefanrousseau 69:5a3414cc7531 456 PRINTF("\r\n");
stefanrousseau 69:5a3414cc7531 457 } //we must wait for GPS to initialize
stefanrousseau 69:5a3414cc7531 458 PRINTF("gps_check_online is %d\r\n", gps_check_online());
stefanrousseau 69:5a3414cc7531 459 unsigned char *data;
stefanrousseau 69:5a3414cc7531 460 data = gps_get_utc_date_time();
stefanrousseau 69:5a3414cc7531 461 PRINTF("gps_get_utc_date_time : %d-%d-%d,%d:%d:%d\r\n", data[0], data[1], data[2], data[3], data[4], data[5]);
stefanrousseau 69:5a3414cc7531 462 PRINTF("gps_get_status : %c ('A' = Valid, 'V' = Invalid)\r\n", gps_get_status());
stefanrousseau 69:5a3414cc7531 463 PRINTF("gps_get_latitude : %c:%f\r\n", gps_get_ns(), gps_get_latitude());
stefanrousseau 69:5a3414cc7531 464 PRINTF("gps_get_longitude : %c:%f\r\n", gps_get_ew(), gps_get_longitude());
stefanrousseau 69:5a3414cc7531 465 PRINTF("gps_get_altitude : %f meters\r\n", gps_get_altitude());
stefanrousseau 69:5a3414cc7531 466 PRINTF("gps_get_speed : %f knots\r\n", gps_get_speed());
stefanrousseau 69:5a3414cc7531 467 PRINTF("gps_get_course : %f degrees\r\n", gps_get_course());
stefanrousseau 69:5a3414cc7531 468 PRINTF("gps_get_position_fix : %c\r\n", gps_get_position_fix());
stefanrousseau 69:5a3414cc7531 469 PRINTF("gps_get_sate_in_view : %d satellites\r\n", gps_get_sate_in_veiw());
stefanrousseau 69:5a3414cc7531 470 PRINTF("gps_get_sate_used : %d\r\n", gps_get_sate_used());
stefanrousseau 69:5a3414cc7531 471 PRINTF("gps_get_mode : %c ('A' = Automatic, 'M' = Manual)\r\n", gps_get_mode());
stefanrousseau 69:5a3414cc7531 472 PRINTF("gps_get_mode2 : %c ('1' = no fix, '1' = 2D fix, '3' = 3D fix)\r\n", gps_get_mode2());
stefanrousseau 69:5a3414cc7531 473 } //bool bGPS_present = true
stefanrousseau 69:5a3414cc7531 474 } //Init_GPS()
stefanrousseau 69:5a3414cc7531 475
stefanrousseau 69:5a3414cc7531 476 void Read_GPS()
stefanrousseau 69:5a3414cc7531 477 {
stefanrousseau 71:45a5e426df81 478 unsigned char gps_valid = 0; //default
stefanrousseau 71:45a5e426df81 479 int lat_sign;
stefanrousseau 71:45a5e426df81 480 int long_sign;
stefanrousseau 69:5a3414cc7531 481 if (bGPS_present)
stefanrousseau 69:5a3414cc7531 482 {
stefanrousseau 69:5a3414cc7531 483 if ((gps_get_status() == 'A') && (gps_get_mode2() != '1'))
stefanrousseau 69:5a3414cc7531 484 {
stefanrousseau 71:45a5e426df81 485 gps_valid = 1;
stefanrousseau 69:5a3414cc7531 486 }
stefanrousseau 70:24d5800f27be 487 if (gps_get_ns() == 'S')
stefanrousseau 70:24d5800f27be 488 {
stefanrousseau 71:45a5e426df81 489 lat_sign = -1; //negative number
stefanrousseau 71:45a5e426df81 490 }
stefanrousseau 71:45a5e426df81 491 else
stefanrousseau 71:45a5e426df81 492 {
stefanrousseau 71:45a5e426df81 493 lat_sign = 1;
stefanrousseau 71:45a5e426df81 494 }
stefanrousseau 71:45a5e426df81 495 if (gps_get_ew() == 'W')
stefanrousseau 71:45a5e426df81 496 {
stefanrousseau 71:45a5e426df81 497 long_sign = -1; //negative number
stefanrousseau 70:24d5800f27be 498 }
stefanrousseau 70:24d5800f27be 499 else
stefanrousseau 70:24d5800f27be 500 {
stefanrousseau 71:45a5e426df81 501 long_sign = 1;
stefanrousseau 70:24d5800f27be 502 }
stefanrousseau 71:45a5e426df81 503 #if (0)
stefanrousseau 71:45a5e426df81 504 PRINTF("gps_valid : %d\r\n", gps_valid);
stefanrousseau 71:45a5e426df81 505 PRINTF("gps_get_latitude : %f\r\n", (lat_sign * gps_get_latitude()));
stefanrousseau 71:45a5e426df81 506 PRINTF("gps_get_longitude : %f\r\n", (long_sign * gps_get_longitude()));
stefanrousseau 69:5a3414cc7531 507 PRINTF("gps_get_altitude : %f meters\r\n", gps_get_altitude());
stefanrousseau 69:5a3414cc7531 508 PRINTF("gps_get_speed : %f knots\r\n", gps_get_speed());
stefanrousseau 69:5a3414cc7531 509 PRINTF("gps_get_course : %f degrees\r\n", gps_get_course());
stefanrousseau 71:45a5e426df81 510 #endif
stefanrousseau 71:45a5e426df81 511 sprintf(SENSOR_DATA.GPS_Valid, "%d", gps_valid);
stefanrousseau 71:45a5e426df81 512 sprintf(SENSOR_DATA.GPS_Latitude, "%f", (lat_sign * gps_get_latitude()));
stefanrousseau 71:45a5e426df81 513 sprintf(SENSOR_DATA.GPS_Longitude, "%f", (long_sign * gps_get_longitude()));
stefanrousseau 71:45a5e426df81 514 sprintf(SENSOR_DATA.GPS_Altitude, "%f", gps_get_altitude());
stefanrousseau 71:45a5e426df81 515 sprintf(SENSOR_DATA.GPS_Speed, "%f", gps_get_speed());
stefanrousseau 71:45a5e426df81 516 sprintf(SENSOR_DATA.GPS_Course, "%f", gps_get_course());
stefanrousseau 69:5a3414cc7531 517 } //bGPS_present
stefanrousseau 69:5a3414cc7531 518 } //Read_GPS()
stefanrousseau 69:5a3414cc7531 519
stefanrousseau 55:3abf9e3f42e6 520 #ifdef USE_VIRTUAL_SENSORS
stefanrousseau 55:3abf9e3f42e6 521 bool bUsbConnected = false;
stefanrousseau 55:3abf9e3f42e6 522 volatile uint8_t usb_uart_rx_buff[256];
stefanrousseau 55:3abf9e3f42e6 523 //volatile uint8_t usb_uart_tx_buff[256];
stefanrousseau 55:3abf9e3f42e6 524 volatile unsigned char usb_uart_rx_buff_putptr = 0;
stefanrousseau 55:3abf9e3f42e6 525 volatile unsigned char usb_uart_rx_buff_getptr = 0;
stefanrousseau 55:3abf9e3f42e6 526 //volatile unsigned char usb_uart_tx_buff_putptr = 0;
stefanrousseau 55:3abf9e3f42e6 527 //volatile unsigned char usb_uart_tx_buff_getptr = 0;
stefanrousseau 55:3abf9e3f42e6 528 char usbhost_rx_string[256];
stefanrousseau 55:3abf9e3f42e6 529 unsigned char usbhost_rxstring_index;
stefanrousseau 55:3abf9e3f42e6 530 char usbhost_tx_string[256];
stefanrousseau 55:3abf9e3f42e6 531
stefanrousseau 55:3abf9e3f42e6 532
stefanrousseau 55:3abf9e3f42e6 533 float f_sensor1_value = 12.3;
stefanrousseau 55:3abf9e3f42e6 534 float f_sensor2_value = 45.6;
stefanrousseau 55:3abf9e3f42e6 535 float f_sensor3_value = 78.9;
stefanrousseau 55:3abf9e3f42e6 536 float f_sensor4_value = 78.9;
stefanrousseau 55:3abf9e3f42e6 537 float f_sensor5_value = 78.9;
stefanrousseau 55:3abf9e3f42e6 538 float f_sensor6_value = 78.9;
stefanrousseau 55:3abf9e3f42e6 539 float f_sensor7_value = 78.9;
stefanrousseau 55:3abf9e3f42e6 540 float f_sensor8_value = 78.9;
stefanrousseau 55:3abf9e3f42e6 541 char usb_sensor_string[110];
stefanrousseau 55:3abf9e3f42e6 542
stefanrousseau 55:3abf9e3f42e6 543
stefanrousseau 55:3abf9e3f42e6 544 //********************************************************************************************************************************************
stefanrousseau 55:3abf9e3f42e6 545 //* Parse the input sensor data from the USB host
stefanrousseau 55:3abf9e3f42e6 546 //********************************************************************************************************************************************
stefanrousseau 55:3abf9e3f42e6 547 int parse_usbhost_message()
stefanrousseau 55:3abf9e3f42e6 548 {
stefanrousseau 64:09004cd610df 549 //PRINTF("String = %s\n", usbhost_rx_string); //test
stefanrousseau 55:3abf9e3f42e6 550 uint8_t length;
stefanrousseau 55:3abf9e3f42e6 551 uint8_t x ;
stefanrousseau 55:3abf9e3f42e6 552 //It seems that sscanf needs 11 characters to store a 7-character number. There must be some formatting and termination values...
stefanrousseau 55:3abf9e3f42e6 553 char Record[8][11]; //There are 8 sensors with up to 7 characters each
stefanrousseau 55:3abf9e3f42e6 554 char StringRecord[110]; //There are is a sensor "string" with up to 100 characters in it
stefanrousseau 55:3abf9e3f42e6 555
stefanrousseau 55:3abf9e3f42e6 556 // Data format is: "S1:1234,S2:5678,S3:1234,S4:5678,S5:1234,S6:5678,S7:5678,S8:5678,S9:abcde...\n"
stefanrousseau 55:3abf9e3f42e6 557 int args_assigned = sscanf(usbhost_rx_string, "%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^','],%[^\n]", Record[0], Record[1], Record[2], Record[3], Record[4], Record[5], Record[6], Record[7], StringRecord);
stefanrousseau 55:3abf9e3f42e6 558
stefanrousseau 55:3abf9e3f42e6 559 //StringRecord[109] = '\0';
stefanrousseau 64:09004cd610df 560 //PRINTF("Last = %s\n", StringRecord); //test
stefanrousseau 55:3abf9e3f42e6 561
stefanrousseau 55:3abf9e3f42e6 562 if (args_assigned == 9)
stefanrousseau 55:3abf9e3f42e6 563 { //sscanf was able to assign all 9 values
stefanrousseau 55:3abf9e3f42e6 564 for (x=0; x < 8; x++) // loop through the 8 sensors
stefanrousseau 55:3abf9e3f42e6 565 {
stefanrousseau 55:3abf9e3f42e6 566 // Strip the "Sx:" label characters from the field value
stefanrousseau 55:3abf9e3f42e6 567 length = strlen(Record[x]); // max of 7 characters but could be less
stefanrousseau 55:3abf9e3f42e6 568 strncpy(Record[x], Record[x] + 3, length);
stefanrousseau 55:3abf9e3f42e6 569 Record[x][length] = '\0'; // null termination character manually added
stefanrousseau 55:3abf9e3f42e6 570 }
stefanrousseau 55:3abf9e3f42e6 571 length = strlen(StringRecord);
stefanrousseau 55:3abf9e3f42e6 572 strncpy(StringRecord, StringRecord + 3, length);
stefanrousseau 55:3abf9e3f42e6 573 StringRecord[length] = '\0'; // null termination character manually added
stefanrousseau 55:3abf9e3f42e6 574
stefanrousseau 55:3abf9e3f42e6 575 if ((usbhost_rx_string[0] == 'S') && (usbhost_rx_string[1] == '1')) //The message starts with "S1"
stefanrousseau 55:3abf9e3f42e6 576 {
stefanrousseau 55:3abf9e3f42e6 577 f_sensor1_value = atof(Record[0]);
stefanrousseau 55:3abf9e3f42e6 578 f_sensor2_value = atof(Record[1]);
stefanrousseau 55:3abf9e3f42e6 579 f_sensor3_value = atof(Record[2]);
stefanrousseau 55:3abf9e3f42e6 580 f_sensor4_value = atof(Record[3]);
stefanrousseau 55:3abf9e3f42e6 581 f_sensor5_value = atof(Record[4]);
stefanrousseau 55:3abf9e3f42e6 582 f_sensor6_value = atof(Record[5]);
stefanrousseau 55:3abf9e3f42e6 583 f_sensor7_value = atof(Record[6]);
stefanrousseau 55:3abf9e3f42e6 584 f_sensor8_value = atof(Record[7]);
stefanrousseau 55:3abf9e3f42e6 585 sprintf(usb_sensor_string,StringRecord);
stefanrousseau 64:09004cd610df 586 //PRINTF("Received = %s, %s, %s, %s, %s, %s, %s, %s, %s\n", Record[0], Record[1], Record[2], Record[3], Record[4], Record[5], Record[6], Record[7], usb_sensor_string); //test
stefanrousseau 55:3abf9e3f42e6 587 sprintf(SENSOR_DATA.Virtual_Sensor1, "%s", Record[0]);
stefanrousseau 55:3abf9e3f42e6 588 sprintf(SENSOR_DATA.Virtual_Sensor2, "%s", Record[1]);
stefanrousseau 55:3abf9e3f42e6 589 sprintf(SENSOR_DATA.Virtual_Sensor3, "%s", Record[2]);
stefanrousseau 55:3abf9e3f42e6 590 sprintf(SENSOR_DATA.Virtual_Sensor4, "%s", Record[3]);
stefanrousseau 55:3abf9e3f42e6 591 sprintf(SENSOR_DATA.Virtual_Sensor5, "%s", Record[4]);
stefanrousseau 55:3abf9e3f42e6 592 sprintf(SENSOR_DATA.Virtual_Sensor6, "%s", Record[5]);
stefanrousseau 55:3abf9e3f42e6 593 sprintf(SENSOR_DATA.Virtual_Sensor7, "%s", Record[6]);
stefanrousseau 55:3abf9e3f42e6 594 sprintf(SENSOR_DATA.Virtual_Sensor8, "%s", Record[7]);
stefanrousseau 55:3abf9e3f42e6 595 }
stefanrousseau 55:3abf9e3f42e6 596 } //sscanf was able to assign all values
stefanrousseau 55:3abf9e3f42e6 597 return args_assigned;
stefanrousseau 55:3abf9e3f42e6 598 } //parse_usbhost_message()
stefanrousseau 55:3abf9e3f42e6 599
stefanrousseau 55:3abf9e3f42e6 600 //********************************************************************************************************************************************
stefanrousseau 55:3abf9e3f42e6 601 //* Process any received message from the USB host
stefanrousseau 55:3abf9e3f42e6 602 //********************************************************************************************************************************************
stefanrousseau 55:3abf9e3f42e6 603 void process_usb_rx(unsigned char ucNewRxByte)
stefanrousseau 55:3abf9e3f42e6 604 {
stefanrousseau 55:3abf9e3f42e6 605 if (ucNewRxByte == '?')
stefanrousseau 55:3abf9e3f42e6 606 { //just pinging
stefanrousseau 55:3abf9e3f42e6 607 usbhost_rxstring_index = 0;
stefanrousseau 55:3abf9e3f42e6 608 return;
stefanrousseau 55:3abf9e3f42e6 609 } //just pinging
stefanrousseau 55:3abf9e3f42e6 610 usbhost_rx_string[usbhost_rxstring_index++] = ucNewRxByte;
stefanrousseau 55:3abf9e3f42e6 611 if (ucNewRxByte == '\n')
stefanrousseau 55:3abf9e3f42e6 612 { //end of message
stefanrousseau 55:3abf9e3f42e6 613 usbhost_rx_string[usbhost_rxstring_index] = 0; //null terminate string
stefanrousseau 55:3abf9e3f42e6 614 usbhost_rxstring_index = 0;
stefanrousseau 55:3abf9e3f42e6 615 parse_usbhost_message();
stefanrousseau 55:3abf9e3f42e6 616 } //end of message
stefanrousseau 55:3abf9e3f42e6 617 } //process_usb_rx()
stefanrousseau 55:3abf9e3f42e6 618
stefanrousseau 55:3abf9e3f42e6 619 void ProcessUsbInterface(void)
stefanrousseau 55:3abf9e3f42e6 620 {
stefanrousseau 55:3abf9e3f42e6 621 //Process the USB host UART receive commands:
stefanrousseau 55:3abf9e3f42e6 622 if (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
stefanrousseau 55:3abf9e3f42e6 623 {
stefanrousseau 55:3abf9e3f42e6 624 bUsbConnected = true;
stefanrousseau 55:3abf9e3f42e6 625 while (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
stefanrousseau 55:3abf9e3f42e6 626 {
stefanrousseau 55:3abf9e3f42e6 627 unsigned char ucByteFromHost = usb_uart_rx_buff[usb_uart_rx_buff_getptr++]; //Copy latest received byte
stefanrousseau 55:3abf9e3f42e6 628 process_usb_rx(ucByteFromHost);
stefanrousseau 55:3abf9e3f42e6 629 } //while (usb_uart_rx_buff_getptr != usb_uart_rx_buff_putptr)
stefanrousseau 55:3abf9e3f42e6 630 } // if there are USB UART bytes to receive
stefanrousseau 55:3abf9e3f42e6 631 //USB host UART transmit:
stefanrousseau 55:3abf9e3f42e6 632 //while (usb_uart_tx_buff_getptr != usb_uart_tx_buff_putptr)
stefanrousseau 55:3abf9e3f42e6 633 //{
stefanrousseau 55:3abf9e3f42e6 634 //pc.putc(usb_uart_tx_buff[usb_uart_tx_buff_getptr++]);
stefanrousseau 55:3abf9e3f42e6 635 //}
stefanrousseau 55:3abf9e3f42e6 636 } //ProcessUsbInterface()
stefanrousseau 55:3abf9e3f42e6 637
stefanrousseau 56:cb42ff383dab 638 // This function is called when a character goes into the RX buffer.
stefanrousseau 56:cb42ff383dab 639 void UsbUartRxCallback(MODSERIAL_IRQ_INFO *info)
stefanrousseau 55:3abf9e3f42e6 640 {
stefanrousseau 56:cb42ff383dab 641 // Get the pointer to our MODSERIAL object that invoked this callback.
stefanrousseau 56:cb42ff383dab 642 MODSERIAL *pc = info->serial;
stefanrousseau 56:cb42ff383dab 643 while (pc->readable())
stefanrousseau 56:cb42ff383dab 644 {
stefanrousseau 56:cb42ff383dab 645 usb_uart_rx_buff[usb_uart_rx_buff_putptr++] = pc->getcNb();
stefanrousseau 56:cb42ff383dab 646 }
stefanrousseau 55:3abf9e3f42e6 647 }
stefanrousseau 55:3abf9e3f42e6 648 #endif
stefanrousseau 55:3abf9e3f42e6 649
stefanrousseau 4:f83bedd9cab4 650 void sensors_init(void)
stefanrousseau 4:f83bedd9cab4 651 {
stefanrousseau 55:3abf9e3f42e6 652 #ifdef USE_VIRTUAL_SENSORS
stefanrousseau 56:cb42ff383dab 653 pc.attach(&UsbUartRxCallback, MODSERIAL::RxIrq);
stefanrousseau 55:3abf9e3f42e6 654 #endif
stefanrousseau 61:f6b93129f954 655 Init_HTS221();
stefanrousseau 4:f83bedd9cab4 656 Init_Si7020();
stefanrousseau 4:f83bedd9cab4 657 Init_Si1145();
stefanrousseau 61:f6b93129f954 658 Init_motion_sensor();
stefanrousseau 69:5a3414cc7531 659 Init_GPS();
stefanrousseau 4:f83bedd9cab4 660 } //sensors_init
stefanrousseau 4:f83bedd9cab4 661
stefanrousseau 4:f83bedd9cab4 662 void read_sensors(void)
stefanrousseau 4:f83bedd9cab4 663 {
stefanrousseau 61:f6b93129f954 664 Read_HTS221();
stefanrousseau 4:f83bedd9cab4 665 Read_Si7020();
stefanrousseau 4:f83bedd9cab4 666 Read_Si1145();
stefanrousseau 61:f6b93129f954 667 Read_motion_sensor();
stefanrousseau 69:5a3414cc7531 668 Read_GPS();
stefanrousseau 4:f83bedd9cab4 669 } //read_sensors