The Hiking Pal tracking device firmware. See full description on the detail page: https://www.hackster.io/bowenfeng/hiking-pal-v1-07c02d

Dependencies:   FXOS8700CQ MODSERIAL mbed

Fork of Avnet_ATT_Cellular_IOT by Avnet

Committer:
bowenfeng
Date:
Wed Dec 21 08:13:25 2016 +0000
Revision:
82:a6c68da2b94a
Parent:
77:c65eae5b9958
Child:
84:0bf8168b690a
Hiking Pal tracking device initial commit

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 55:3abf9e3f42e6 18 #include "mbed.h"
JMF 0:9d5134074d84 19 #include <cctype>
JMF 0:9d5134074d84 20 #include <string>
JMF 2:0e2ef866af95 21 #include "config_me.h"
stefanrousseau 4:f83bedd9cab4 22 #include "sensors.h"
stefanrousseau 61:f6b93129f954 23 #include "cell_modem.h"
stefanrousseau 11:e6602513730f 24 #include "hardware.h"
stefanrousseau 11:e6602513730f 25
stefanrousseau 61:f6b93129f954 26 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used
stefanrousseau 56:cb42ff383dab 27 MODSERIAL pc(USBTX, USBRX, 256, 256); // tx, rx with default tx, rx buffer sizes
stefanrousseau 63:90d7c69993cd 28 MODSERIAL mdm(PTD3, PTD2, 4096, 4096);
stefanrousseau 16:17c5916f2d12 29 DigitalOut led_green(LED_GREEN);
stefanrousseau 16:17c5916f2d12 30 DigitalOut led_red(LED_RED);
stefanrousseau 16:17c5916f2d12 31 DigitalOut led_blue(LED_BLUE);
JMF 0:9d5134074d84 32
JMF 0:9d5134074d84 33
stefanrousseau 3:26b3cc155f39 34 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 35 //* Create string with sensor readings that can be sent to flow as an HTTP get
stefanrousseau 3:26b3cc155f39 36 //********************************************************************************************************************************************
stefanrousseau 12:7c94ec5069dc 37 K64F_Sensors_t SENSOR_DATA =
stefanrousseau 3:26b3cc155f39 38 {
stefanrousseau 12:7c94ec5069dc 39 .Temperature = "0",
stefanrousseau 12:7c94ec5069dc 40 .Humidity = "0",
stefanrousseau 12:7c94ec5069dc 41 .AccelX = "0",
stefanrousseau 12:7c94ec5069dc 42 .AccelY = "0",
stefanrousseau 12:7c94ec5069dc 43 .AccelZ = "0",
stefanrousseau 12:7c94ec5069dc 44 .MagnetometerX = "0",
stefanrousseau 12:7c94ec5069dc 45 .MagnetometerY = "0",
stefanrousseau 12:7c94ec5069dc 46 .MagnetometerZ = "0",
stefanrousseau 12:7c94ec5069dc 47 .AmbientLightVis = "0",
stefanrousseau 12:7c94ec5069dc 48 .AmbientLightIr = "0",
stefanrousseau 12:7c94ec5069dc 49 .UVindex = "0",
stefanrousseau 12:7c94ec5069dc 50 .Proximity = "0",
stefanrousseau 12:7c94ec5069dc 51 .Temperature_Si7020 = "0",
stefanrousseau 55:3abf9e3f42e6 52 .Humidity_Si7020 = "0",
stefanrousseau 55:3abf9e3f42e6 53 .Virtual_Sensor1 = "0",
stefanrousseau 55:3abf9e3f42e6 54 .Virtual_Sensor2 = "0",
stefanrousseau 55:3abf9e3f42e6 55 .Virtual_Sensor3 = "0",
stefanrousseau 55:3abf9e3f42e6 56 .Virtual_Sensor4 = "0",
stefanrousseau 55:3abf9e3f42e6 57 .Virtual_Sensor5 = "0",
stefanrousseau 55:3abf9e3f42e6 58 .Virtual_Sensor6 = "0",
stefanrousseau 55:3abf9e3f42e6 59 .Virtual_Sensor7 = "0",
stefanrousseau 71:45a5e426df81 60 .Virtual_Sensor8 = "0",
stefanrousseau 72:b500e1507b5f 61 .GPS_Satellites = "0",
stefanrousseau 71:45a5e426df81 62 .GPS_Latitude = "0",
stefanrousseau 71:45a5e426df81 63 .GPS_Longitude = "0",
stefanrousseau 71:45a5e426df81 64 .GPS_Altitude = "0",
stefanrousseau 71:45a5e426df81 65 .GPS_Speed = "0",
stefanrousseau 71:45a5e426df81 66 .GPS_Course = "0"
stefanrousseau 3:26b3cc155f39 67 };
stefanrousseau 12:7c94ec5069dc 68
fkellermavnet 77:c65eae5b9958 69 void display_app_firmware_version(void)
fkellermavnet 77:c65eae5b9958 70 {
fkellermavnet 77:c65eae5b9958 71 PUTS("\r\n\r\nApp Firmware: Release 1.0 - built: "__DATE__" "__TIME__"\r\n\r\n");
fkellermavnet 77:c65eae5b9958 72 }
fkellermavnet 77:c65eae5b9958 73
bowenfeng 82:a6c68da2b94a 74 void GenerateModemString(char * modem_string, long long hid, long long sid)
stefanrousseau 3:26b3cc155f39 75 {
bowenfeng 82:a6c68da2b94a 76 sprintf(
bowenfeng 82:a6c68da2b94a 77 modem_string,
bowenfeng 82:a6c68da2b94a 78 "GET %s/hikings/%lld/sessions/%lld/moves?lat=%s&lng=%s HTTP/1.1\r\nHost: %s\r\n\r\n",
bowenfeng 82:a6c68da2b94a 79 FLOW_BASE_URL,
bowenfeng 82:a6c68da2b94a 80 hid,
bowenfeng 82:a6c68da2b94a 81 sid,
bowenfeng 82:a6c68da2b94a 82 SENSOR_DATA.GPS_Latitude,
bowenfeng 82:a6c68da2b94a 83 SENSOR_DATA.GPS_Longitude,
bowenfeng 82:a6c68da2b94a 84 MY_SERVER_URL);
bowenfeng 82:a6c68da2b94a 85
stefanrousseau 3:26b3cc155f39 86 } //GenerateModemString
stefanrousseau 3:26b3cc155f39 87
stefanrousseau 3:26b3cc155f39 88
stefanrousseau 3:26b3cc155f39 89 //Periodic timer
stefanrousseau 3:26b3cc155f39 90 Ticker OneMsTicker;
stefanrousseau 3:26b3cc155f39 91 volatile bool bTimerExpiredFlag = false;
stefanrousseau 3:26b3cc155f39 92 int OneMsTicks = 0;
stefanrousseau 3:26b3cc155f39 93 int iTimer1Interval_ms = 1000;
stefanrousseau 3:26b3cc155f39 94 //********************************************************************************************************************************************
stefanrousseau 3:26b3cc155f39 95 //* Periodic 1ms timer tick
stefanrousseau 3:26b3cc155f39 96 //********************************************************************************************************************************************
stefanrousseau 3:26b3cc155f39 97 void OneMsFunction()
stefanrousseau 3:26b3cc155f39 98 {
stefanrousseau 3:26b3cc155f39 99 OneMsTicks++;
stefanrousseau 3:26b3cc155f39 100 if ((OneMsTicks % iTimer1Interval_ms) == 0)
stefanrousseau 3:26b3cc155f39 101 {
stefanrousseau 3:26b3cc155f39 102 bTimerExpiredFlag = true;
stefanrousseau 3:26b3cc155f39 103 }
stefanrousseau 3:26b3cc155f39 104 } //OneMsFunction()
stefanrousseau 3:26b3cc155f39 105
stefanrousseau 16:17c5916f2d12 106 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 107 //* Set the RGB LED's Color
stefanrousseau 16:17c5916f2d12 108 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
stefanrousseau 16:17c5916f2d12 109 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 110 void SetLedColor(unsigned char ucColor)
stefanrousseau 16:17c5916f2d12 111 {
stefanrousseau 16:17c5916f2d12 112 //Note that when an LED is on, you write a 0 to it:
stefanrousseau 16:17c5916f2d12 113 led_red = !(ucColor & 0x1); //bit 0
stefanrousseau 16:17c5916f2d12 114 led_green = !(ucColor & 0x2); //bit 1
stefanrousseau 16:17c5916f2d12 115 led_blue = !(ucColor & 0x4); //bit 2
stefanrousseau 16:17c5916f2d12 116 } //SetLedColor()
stefanrousseau 16:17c5916f2d12 117
stefanrousseau 16:17c5916f2d12 118 //********************************************************************************************************************************************
stefanrousseau 61:f6b93129f954 119 //* Process the JSON response. In this example we are only extracting a LED color.
stefanrousseau 16:17c5916f2d12 120 //********************************************************************************************************************************************
stefanrousseau 16:17c5916f2d12 121 bool parse_JSON(char* json_string)
stefanrousseau 16:17c5916f2d12 122 {
stefanrousseau 16:17c5916f2d12 123 char* beginquote;
stefanrousseau 16:17c5916f2d12 124 char token[] = "\"LED\":\"";
stefanrousseau 16:17c5916f2d12 125 beginquote = strstr(json_string, token );
stefanrousseau 16:17c5916f2d12 126 if ((beginquote != 0))
stefanrousseau 16:17c5916f2d12 127 {
stefanrousseau 16:17c5916f2d12 128 char cLedColor = beginquote[strlen(token)];
stefanrousseau 64:09004cd610df 129 PRINTF(GRN "LED Found : %c" DEF "\r\n", cLedColor);
stefanrousseau 16:17c5916f2d12 130 switch(cLedColor)
stefanrousseau 16:17c5916f2d12 131 {
stefanrousseau 16:17c5916f2d12 132 case 'O':
stefanrousseau 16:17c5916f2d12 133 { //Off
stefanrousseau 16:17c5916f2d12 134 SetLedColor(0);
stefanrousseau 16:17c5916f2d12 135 break;
stefanrousseau 16:17c5916f2d12 136 }
stefanrousseau 16:17c5916f2d12 137 case 'R':
stefanrousseau 16:17c5916f2d12 138 { //Red
stefanrousseau 16:17c5916f2d12 139 SetLedColor(1);
stefanrousseau 16:17c5916f2d12 140 break;
stefanrousseau 16:17c5916f2d12 141 }
stefanrousseau 16:17c5916f2d12 142 case 'G':
stefanrousseau 16:17c5916f2d12 143 { //Green
stefanrousseau 16:17c5916f2d12 144 SetLedColor(2);
stefanrousseau 16:17c5916f2d12 145 break;
stefanrousseau 16:17c5916f2d12 146 }
stefanrousseau 16:17c5916f2d12 147 case 'Y':
stefanrousseau 16:17c5916f2d12 148 { //Yellow
stefanrousseau 16:17c5916f2d12 149 SetLedColor(3);
stefanrousseau 16:17c5916f2d12 150 break;
stefanrousseau 16:17c5916f2d12 151 }
stefanrousseau 16:17c5916f2d12 152 case 'B':
stefanrousseau 16:17c5916f2d12 153 { //Blue
stefanrousseau 16:17c5916f2d12 154 SetLedColor(4);
stefanrousseau 16:17c5916f2d12 155 break;
stefanrousseau 16:17c5916f2d12 156 }
stefanrousseau 16:17c5916f2d12 157 case 'M':
stefanrousseau 16:17c5916f2d12 158 { //Magenta
stefanrousseau 16:17c5916f2d12 159 SetLedColor(5);
stefanrousseau 16:17c5916f2d12 160 break;
stefanrousseau 16:17c5916f2d12 161 }
stefanrousseau 16:17c5916f2d12 162 case 'T':
stefanrousseau 16:17c5916f2d12 163 { //Turquoise
stefanrousseau 16:17c5916f2d12 164 SetLedColor(6);
stefanrousseau 16:17c5916f2d12 165 break;
stefanrousseau 16:17c5916f2d12 166 }
stefanrousseau 16:17c5916f2d12 167 case 'W':
stefanrousseau 16:17c5916f2d12 168 { //White
stefanrousseau 16:17c5916f2d12 169 SetLedColor(7);
stefanrousseau 16:17c5916f2d12 170 break;
stefanrousseau 16:17c5916f2d12 171 }
stefanrousseau 16:17c5916f2d12 172 default:
stefanrousseau 16:17c5916f2d12 173 {
stefanrousseau 16:17c5916f2d12 174 break;
stefanrousseau 16:17c5916f2d12 175 }
stefanrousseau 16:17c5916f2d12 176 } //switch(cLedColor)
stefanrousseau 16:17c5916f2d12 177 return true;
stefanrousseau 16:17c5916f2d12 178 }
stefanrousseau 16:17c5916f2d12 179 else
stefanrousseau 16:17c5916f2d12 180 {
stefanrousseau 16:17c5916f2d12 181 return false;
stefanrousseau 16:17c5916f2d12 182 }
stefanrousseau 16:17c5916f2d12 183 } //parse_JSON
stefanrousseau 16:17c5916f2d12 184
JMF 0:9d5134074d84 185 int main() {
stefanrousseau 61:f6b93129f954 186 static unsigned ledOnce = 0;
stefanrousseau 72:b500e1507b5f 187 //delay so that the debug terminal can open after power-on reset:
fkellermavnet 77:c65eae5b9958 188 wait (5.0);
stefanrousseau 61:f6b93129f954 189 pc.baud(115200);
fkellermavnet 77:c65eae5b9958 190
fkellermavnet 77:c65eae5b9958 191 display_app_firmware_version();
fkellermavnet 77:c65eae5b9958 192
stefanrousseau 64:09004cd610df 193 PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n\r\n");
JMF 0:9d5134074d84 194
stefanrousseau 61:f6b93129f954 195 //Initialize the I2C sensors that are present
stefanrousseau 11:e6602513730f 196 sensors_init();
stefanrousseau 12:7c94ec5069dc 197 read_sensors();
stefanrousseau 11:e6602513730f 198
stefanrousseau 61:f6b93129f954 199 // Set LED to RED until init finishes
stefanrousseau 61:f6b93129f954 200 SetLedColor(0x1); //Red
JMF 0:9d5134074d84 201 // Initialize the modem
stefanrousseau 64:09004cd610df 202 PRINTF("\r\n");
stefanrousseau 61:f6b93129f954 203 cell_modem_init();
fkellermavnet 77:c65eae5b9958 204 display_wnc_firmware_rev();
fkellermavnet 77:c65eae5b9958 205
stefanrousseau 61:f6b93129f954 206 // Set LED BLUE for partial init
stefanrousseau 61:f6b93129f954 207 SetLedColor(0x4); //Blue
JMF 0:9d5134074d84 208
stefanrousseau 3:26b3cc155f39 209 //Create a 1ms timer tick function:
stefanrousseau 61:f6b93129f954 210 iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;
stefanrousseau 3:26b3cc155f39 211 OneMsTicker.attach(OneMsFunction, 0.001f) ;
fkellermavnet 26:8d6e7e7cdcae 212
JMF 2:0e2ef866af95 213 // Send and receive data perpetually
JMF 2:0e2ef866af95 214 while(1) {
stefanrousseau 55:3abf9e3f42e6 215 #ifdef USE_VIRTUAL_SENSORS
stefanrousseau 55:3abf9e3f42e6 216 ProcessUsbInterface();
stefanrousseau 55:3abf9e3f42e6 217 #endif
stefanrousseau 3:26b3cc155f39 218 if (bTimerExpiredFlag)
stefanrousseau 3:26b3cc155f39 219 {
stefanrousseau 3:26b3cc155f39 220 bTimerExpiredFlag = false;
stefanrousseau 4:f83bedd9cab4 221 read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
stefanrousseau 3:26b3cc155f39 222 char modem_string[512];
bowenfeng 82:a6c68da2b94a 223 GenerateModemString(&modem_string[0], 1482292398681, 1482293421314);
stefanrousseau 61:f6b93129f954 224 char myJsonResponse[512];
stefanrousseau 61:f6b93129f954 225 if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0]))
fkellermavnet 20:27a4f27254d0 226 {
bowenfeng 82:a6c68da2b94a 227 // if (!ledOnce)
bowenfeng 82:a6c68da2b94a 228 // {
bowenfeng 82:a6c68da2b94a 229 // ledOnce = 1;
bowenfeng 82:a6c68da2b94a 230 // SetLedColor(0x2); //Green
bowenfeng 82:a6c68da2b94a 231 // }
bowenfeng 82:a6c68da2b94a 232 // parse_JSON(&myJsonResponse[0]);
stefanrousseau 16:17c5916f2d12 233 }
stefanrousseau 3:26b3cc155f39 234 } //bTimerExpiredFlag
stefanrousseau 3:26b3cc155f39 235 } //forever loop
JMF 0:9d5134074d84 236 }