├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── compile_examples.sh ├── examples ├── 1_GPS_Features │ └── 1_GPS_Features.ino ├── 2_Accelerometer │ └── 2_Accelerometer.ino └── 3_WakeOnMove │ └── 3_WakeOnMove.ino ├── library.properties └── src ├── AssetTracker.cpp ├── AssetTracker.h └── AssetTracker └── AssetTracker.h /.gitignore: -------------------------------------------------------------------------------- 1 | *.bin 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 12 4 | install: 5 | - npm install -g particle-cli 6 | - 'mkdir ~/.particle && echo {\"access_token\": \"$PARTICLE_TOKEN\"} > ~/.particle/particle.config.json' 7 | script: 8 | - ./compile_examples.sh 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Electron Asset Tracker 2 | [![Build Status](https://travis-ci.org/particle-iot/AssetTracker.svg?branch=master)](https://travis-ci.org/particle-iot/AssetTracker) 3 | 4 | _The Asset Tracker has a lot of features, but some of these aren't as straightforward to use as simple LEDs and sensors. This library abstracts a lot of the complexity and provides simple functions with broad capability. It wraps modified versions of both Adafruit's Ultimate GPS and LIS3DH Arduino libraries, as well as some novel functions._ 5 | 6 | ## Examples 7 | 8 | 1. __GPS Features__ - How to use the GPS efficiently, and some nice Electron functions 9 | 2. __Accelerometer__ - Using the accelerometer with some cute tricks 10 | 3. __WakeOnMove__ - Waking up from sleep mode when the Electron is moved 11 | 12 | -------------------------------------------------------------------------------- /compile_examples.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Compile each library example as a test 3 | 4 | set -xe 5 | 6 | PLATFORMS="photon electron" 7 | EXAMPLES=`find examples -mindepth 1 -maxdepth 1 -not -empty -type d | sort` 8 | 9 | for platform in $PLATFORMS; do 10 | for example in $EXAMPLES; do 11 | particle --no-update-check compile $platform $example 12 | done 13 | done 14 | -------------------------------------------------------------------------------- /examples/1_GPS_Features/1_GPS_Features.ino: -------------------------------------------------------------------------------- 1 | /* ----------------------------------------------------------- 2 | This example shows a lot of different features. As configured here 3 | it will check for a good GPS fix every 10 minutes and publish that data 4 | if there is one. If not, it will save you data by staying quiet. It also 5 | registers 3 Particle.functions for changing whether it publishes, 6 | reading the battery level, and manually requesting a GPS reading. 7 | ---------------------------------------------------------------*/ 8 | 9 | // Getting the library 10 | #include "AssetTracker.h" 11 | 12 | // Set whether you want the device to publish data to the internet by default here. 13 | // 1 will Particle.publish AND Serial.print, 0 will just Serial.print 14 | // Extremely useful for saving data while developing close enough to have a cable plugged in. 15 | // You can also change this remotely using the Particle.function "tmode" defined in setup() 16 | int transmittingData = 1; 17 | 18 | // Used to keep track of the last time we published data 19 | long lastPublish = 0; 20 | 21 | // How many minutes between publishes? 10+ recommended for long-time continuous publishing! 22 | int delayMinutes = 10; 23 | 24 | // Creating an AssetTracker named 't' for us to reference 25 | AssetTracker t = AssetTracker(); 26 | 27 | // A FuelGauge named 'fuel' for checking on the battery state 28 | FuelGauge fuel; 29 | 30 | // setup() and loop() are both required. setup() runs once when the device starts 31 | // and is used for registering functions and variables and initializing things 32 | void setup() { 33 | // Sets up all the necessary AssetTracker bits 34 | t.begin(); 35 | 36 | // Enable the GPS module. Defaults to off to save power. 37 | // Takes 1.5s or so because of delays. 38 | t.gpsOn(); 39 | 40 | // Opens up a Serial port so you can listen over USB 41 | Serial.begin(9600); 42 | 43 | // These three functions are useful for remote diagnostics. Read more below. 44 | Particle.function("tmode", transmitMode); 45 | Particle.function("batt", batteryStatus); 46 | Particle.function("gps", gpsPublish); 47 | } 48 | 49 | // loop() runs continuously 50 | void loop() { 51 | // You'll need to run this every loop to capture the GPS output 52 | t.updateGPS(); 53 | 54 | // if the current time - the last time we published is greater than your set delay... 55 | if (millis()-lastPublish > delayMinutes*60*1000) { 56 | // Remember when we published 57 | lastPublish = millis(); 58 | 59 | //String pubAccel = String::format("%d,%d,%d", t.readX(), t.readY(), t.readZ()); 60 | //Serial.println(pubAccel); 61 | //Particle.publish("A", pubAccel, 60, PRIVATE); 62 | 63 | // Dumps the full NMEA sentence to serial in case you're curious 64 | Serial.println(t.preNMEA()); 65 | 66 | // GPS requires a "fix" on the satellites to give good data, 67 | // so we should only publish data if there's a fix 68 | if (t.gpsFix()) { 69 | // Only publish if we're in transmittingData mode 1; 70 | if (transmittingData) { 71 | // Short publish names save data! 72 | Particle.publish("G", t.readLatLon(), 60, PRIVATE); 73 | } 74 | // but always report the data over serial for local development 75 | Serial.println(t.readLatLon()); 76 | } 77 | } 78 | } 79 | 80 | // Allows you to remotely change whether a device is publishing to the cloud 81 | // or is only reporting data over Serial. Saves data when using only Serial! 82 | // Change the default at the top of the code. 83 | int transmitMode(String command) { 84 | transmittingData = atoi(command); 85 | return 1; 86 | } 87 | 88 | // Actively ask for a GPS reading if you're impatient. Only publishes if there's 89 | // a GPS fix, otherwise returns '0' 90 | int gpsPublish(String command) { 91 | if (t.gpsFix()) { 92 | Particle.publish("G", t.readLatLon(), 60, PRIVATE); 93 | 94 | // uncomment next line if you want a manual publish to reset delay counter 95 | // lastPublish = millis(); 96 | return 1; 97 | } else { 98 | return 0; 99 | } 100 | } 101 | 102 | // Lets you remotely check the battery status by calling the function "batt" 103 | // Triggers a publish with the info (so subscribe or watch the dashboard) 104 | // and also returns a '1' if there's >10% battery left and a '0' if below 105 | int batteryStatus(String command){ 106 | // Publish the battery voltage and percentage of battery remaining 107 | // if you want to be really efficient, just report one of these 108 | // the String::format("%f.2") part gives us a string to publish, 109 | // but with only 2 decimal points to save space 110 | Particle.publish("B", 111 | "v:" + String::format("%.2f",fuel.getVCell()) + 112 | ",c:" + String::format("%.2f",fuel.getSoC()), 113 | 60, PRIVATE 114 | ); 115 | // if there's more than 10% of the battery left, then return 1 116 | if (fuel.getSoC()>10){ return 1;} 117 | // if you're running out of battery, return 0 118 | else { return 0;} 119 | } 120 | -------------------------------------------------------------------------------- /examples/2_Accelerometer/2_Accelerometer.ino: -------------------------------------------------------------------------------- 1 | /* ----------------------------------------------------------- 2 | This example shows some clever ways to use the accelerometer 3 | and report that data efficiently. It also includes the transmit 4 | mode and battery reading functions from the previous example. 5 | 6 | By default, it publishes only when there's a significant bump 7 | against it AND there's been at least 10 minutes since the last 8 | publish. NOTE: the first publish will only happen after the 9 | device has been on for at least this long! 10 | ---------------------------------------------------------------*/ 11 | 12 | // Getting the library 13 | #include "AssetTracker.h" 14 | 15 | // Set whether you want the device to publish data to the internet by default here. 16 | // 1 will Particle.publish AND Serial.print, 0 will just Serial.print 17 | // Extremely useful for saving data while developing close enough to have a cable plugged in. 18 | // You can also change this remotely using the Particle.function "tmode" defined in setup() 19 | int transmittingData = 1; 20 | 21 | // Used to keep track of the last time we published data 22 | long lastPublish = 0; 23 | 24 | // How many minutes minimum between publishes? 10+ recommended! 25 | int delayMinutes = 10; 26 | 27 | // Threshold to trigger a publish 28 | // 9000 is VERY sensitive, 12000 will still detect small bumps 29 | int accelThreshold = 12000; 30 | 31 | // Creating an AssetTracker named 't' for us to reference 32 | AssetTracker t = AssetTracker(); 33 | 34 | // A FuelGauge named 'fuel' for checking on the battery state 35 | FuelGauge fuel; 36 | 37 | // setup() and loop() are both required. setup() runs once when the device starts 38 | // and is used for registering functions and variables and initializing things 39 | void setup() { 40 | // Sets up all the necessary AssetTracker bits 41 | t.begin(); 42 | 43 | // Opens up a Serial port so you can listen over USB 44 | Serial.begin(9600); 45 | 46 | // These three functions are useful for remote diagnostics. Read more below. 47 | Particle.function("aThresh",accelThresholder); 48 | Particle.function("tmode", transmitMode); 49 | Particle.function("batt", batteryStatus); 50 | } 51 | 52 | // loop() runs continuously 53 | void loop() { 54 | // Check if there's been a big acceleration 55 | if (t.readXYZmagnitude() > accelThreshold) { 56 | // Create a nice string with commas between x,y,z 57 | String pubAccel = String::format("%d,%d,%d", t.readX(), t.readY(), t.readZ()); 58 | 59 | // Send that acceleration to the serial port where it can be read by USB 60 | //Serial.println(pubAccel); 61 | Serial.println(t.readXYZmagnitude()); 62 | 63 | // If it's set to transmit AND it's been at least delayMinutes since the last one... 64 | if (transmittingData && ((millis()-lastPublish) > (delayMinutes*60*1000))) { 65 | lastPublish = millis(); 66 | Particle.publish("A", pubAccel, 60, PRIVATE); 67 | } 68 | 69 | } 70 | } 71 | 72 | // Remotely change the trigger threshold! 73 | int accelThresholder(String command) { 74 | accelThreshold = atoi(command); 75 | return 1; 76 | } 77 | 78 | // Allows you to remotely change whether a device is publishing to the cloud 79 | // or is only reporting data over Serial. Saves data when using only Serial! 80 | // Change the default at the top of the code. 81 | int transmitMode(String command) { 82 | transmittingData = atoi(command); 83 | return 1; 84 | } 85 | 86 | // Lets you remotely check the battery status by calling the function "batt" 87 | // Triggers a publish with the info (so subscribe or watch the dashboard) 88 | // and also returns a '1' if there's >10% battery left and a '0' if below 89 | int batteryStatus(String command) { 90 | // Publish the battery voltage and percentage of battery remaining 91 | // if you want to be really efficient, just report one of these 92 | // the String::format("%f.2") part gives us a string to publish, 93 | // but with only 2 decimal points to save space 94 | Particle.publish("B", 95 | "v:" + String::format("%.2f",fuel.getVCell()) + 96 | ",c:" + String::format("%.2f",fuel.getSoC()), 97 | 60, PRIVATE 98 | ); 99 | // if there's more than 10% of the battery left, then return 1 100 | if (fuel.getSoC() > 10) { 101 | return 1; 102 | 103 | // if you're running out of battery, return 0 104 | } else { 105 | return 0; 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /examples/3_WakeOnMove/3_WakeOnMove.ino: -------------------------------------------------------------------------------- 1 | 2 | #include "Particle.h" 3 | 4 | #include "AssetTracker.h" 5 | 6 | // Project Location: 7 | 8 | 9 | // System threading is required for this project 10 | SYSTEM_THREAD(ENABLED); 11 | 12 | // Global objects 13 | FuelGauge batteryMonitor; 14 | AssetTracker t; 15 | 16 | // This is the name of the Particle event to publish for battery or movement detection events 17 | // It is a private event. 18 | const char *eventName = "accel"; 19 | 20 | // Various timing constants 21 | const unsigned long MAX_TIME_TO_PUBLISH_MS = 60000; // Only stay awake for 60 seconds trying to connect to the cloud and publish 22 | const unsigned long TIME_AFTER_PUBLISH_MS = 4000; // After publish, wait 4 seconds for data to go out 23 | const unsigned long TIME_AFTER_BOOT_MS = 5000; // At boot, wait 5 seconds before going to sleep again (after coming online) 24 | const unsigned long TIME_PUBLISH_BATTERY_SEC = 4 * 60 * 60; // every 4 hours, send a battery update 25 | 26 | const uint8_t movementThreshold = 16; 27 | 28 | // Stuff for the finite state machine 29 | enum State { ONLINE_WAIT_STATE, RESET_STATE, RESET_WAIT_STATE, PUBLISH_STATE, SLEEP_STATE, SLEEP_WAIT_STATE, BOOT_WAIT_STATE }; 30 | State state = ONLINE_WAIT_STATE; 31 | unsigned long stateTime = 0; 32 | int awake = 0; 33 | 34 | void setup() { 35 | Serial.begin(9600); 36 | t.begin(); 37 | } 38 | 39 | void loop() { 40 | 41 | switch(state) { 42 | case ONLINE_WAIT_STATE: 43 | if (Particle.connected()) { 44 | state = RESET_STATE; 45 | } 46 | if (millis() - stateTime > 5000) { 47 | stateTime = millis(); 48 | Serial.println("waiting to come online"); 49 | } 50 | break; 51 | 52 | case RESET_STATE: 53 | Serial.println("resetting accelerometer"); 54 | 55 | if (!t.setupLowPowerWakeMode(movementThreshold)) { 56 | Serial.println("accelerometer not found"); 57 | state = SLEEP_STATE; 58 | break; 59 | } 60 | 61 | state = BOOT_WAIT_STATE; 62 | break; 63 | 64 | case PUBLISH_STATE: 65 | if (Particle.connected()) { 66 | // The publish data contains 3 comma-separated values: 67 | // whether movement was detected (1) or not (0) The not detected publish is used for battery status updates 68 | // cell voltage (decimal) 69 | // state of charge (decimal) 70 | char data[32]; 71 | float cellVoltage = batteryMonitor.getVCell(); 72 | float stateOfCharge = batteryMonitor.getSoC(); 73 | snprintf(data, sizeof(data), "%d,%.02f,%.02f", awake, cellVoltage, stateOfCharge); 74 | 75 | Particle.publish(eventName, data, 60, PRIVATE); 76 | 77 | // Wait for the publish to go out 78 | stateTime = millis(); 79 | state = SLEEP_WAIT_STATE; 80 | } else { 81 | // Haven't come online yet 82 | if (millis() - stateTime >= MAX_TIME_TO_PUBLISH_MS) { 83 | // Took too long to publish, just go to sleep 84 | state = SLEEP_STATE; 85 | } 86 | } 87 | break; 88 | 89 | case SLEEP_WAIT_STATE: 90 | if (millis() - stateTime >= TIME_AFTER_PUBLISH_MS) { 91 | state = SLEEP_STATE; 92 | } 93 | break; 94 | 95 | case BOOT_WAIT_STATE: 96 | if (millis() - stateTime >= TIME_AFTER_BOOT_MS) { 97 | // To publish the battery stats after boot, set state to PUBLISH_STATE 98 | // To go to sleep immediately, set state to SLEEP_STATE 99 | state = PUBLISH_STATE; 100 | } 101 | break; 102 | 103 | case SLEEP_STATE: 104 | // Sleep 105 | System.sleep(WKP, RISING, TIME_PUBLISH_BATTERY_SEC, SLEEP_NETWORK_STANDBY); 106 | 107 | // This delay should not be necessary, but sometimes things don't seem to work right 108 | // immediately coming out of sleep. 109 | delay(500); 110 | 111 | awake = ((t.clearAccelInterrupt() & LIS3DH_INT1_SRC_IA) != 0); 112 | 113 | Serial.printlnf("awake=%d", awake); 114 | 115 | state = PUBLISH_STATE; 116 | stateTime = millis(); 117 | break; 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=AssetTracker 2 | version=0.0.9 3 | license=LGPL 4 | author=Richard Whitney 5 | sentence=Functions to make the Asset Tracker for Electron easy to use! 6 | url=https://github.com/spark/AssetTracker 7 | repository=https://github.com/spark/AssetTracker.git 8 | architectures=particle-electron 9 | dependencies.Adafruit_GPS=1.0.3 10 | dependencies.Adafruit_LIS3DH=1.0.3 11 | -------------------------------------------------------------------------------- /src/AssetTracker.cpp: -------------------------------------------------------------------------------- 1 | #include "AssetTracker.h" 2 | #include "math.h" 3 | 4 | //----------------- Tracker ----------------// 5 | 6 | Adafruit_GPS gps = Adafruit_GPS(&Serial1); 7 | Adafruit_LIS3DH accel = Adafruit_LIS3DH(A2); 8 | 9 | uint8_t internalANT[]={0xB5,0x62,0x06,0x13,0x04,0x00,0x00,0x00,0xF0,0x7D,0x8A,0x2A}; 10 | uint8_t externalANT[]={0xB5,0x62,0x06,0x13,0x04,0x00,0x01,0x00,0xF0,0x7D,0x8B,0x2E}; 11 | 12 | AssetTracker::AssetTracker() { 13 | 14 | } 15 | 16 | void AssetTracker::begin() { 17 | accel.begin(LIS3DH_DEFAULT_ADDRESS); 18 | 19 | // Default to 5kHz low-power sampling 20 | accel.setDataRate(LIS3DH_DATARATE_LOWPOWER_5KHZ); 21 | 22 | // Default to 4 gravities range 23 | accel.setRange(LIS3DH_RANGE_4_G); 24 | 25 | // Turn on the GPS module 26 | // gpsOn(); 27 | } 28 | 29 | float AssetTracker::readLat() { 30 | return gps.latitude; 31 | } 32 | 33 | float AssetTracker::readLon() { 34 | return gps.longitude; 35 | } 36 | 37 | float AssetTracker::readLatDeg() { 38 | return gps.latitudeDegrees; 39 | } 40 | 41 | float AssetTracker::readLonDeg() { 42 | return gps.longitudeDegrees; 43 | } 44 | 45 | float AssetTracker::readHDOP() { 46 | return gps.HDOP; 47 | } 48 | 49 | float AssetTracker::getGpsAccuracy() { 50 | // 1.8 taken from specs at https://learn.adafruit.com/adafruit-ultimate-gps/ 51 | return 1.8 * readHDOP(); 52 | } 53 | 54 | uint32_t AssetTracker::getGpsTimestamp() { 55 | // Return timestamp in milliseconds, from last GPS reading 56 | // 0 if no reading has been done 57 | // (This returns the milliseconds of current day) 58 | return gps.hour * 60 * 60 * 1000 + gps.minute * 60 * 1000 + gps.seconds * 1000 + gps.milliseconds; 59 | } 60 | 61 | uint8_t AssetTracker::getHour(){ 62 | return gps.hour; 63 | } 64 | 65 | uint8_t AssetTracker::getMinute(){ 66 | return gps.minute; 67 | } 68 | 69 | uint8_t AssetTracker::getSeconds(){ 70 | return gps.seconds; 71 | } 72 | 73 | uint16_t AssetTracker::getMilliseconds(){ 74 | return gps.milliseconds; 75 | } 76 | 77 | uint8_t AssetTracker::getYear(){ 78 | return gps.year; 79 | } 80 | 81 | uint8_t AssetTracker::getMonth(){ 82 | return gps.month; 83 | } 84 | 85 | uint8_t AssetTracker::getDay(){ 86 | return gps.day; 87 | } 88 | 89 | uint8_t AssetTracker::getSatellites(){ 90 | return gps.satellites; 91 | } 92 | 93 | uint8_t AssetTracker::getFixQuality(){ 94 | return gps.fixquality; 95 | } 96 | 97 | float AssetTracker::getSpeed(){ 98 | return gps.speed; 99 | } 100 | 101 | float AssetTracker::getGeoIdHeight(){ 102 | return gps.geoidheight; 103 | } 104 | 105 | float AssetTracker::getAltitude(){ 106 | return gps.altitude; 107 | } 108 | 109 | String AssetTracker::readLatLon() { 110 | String latLon = String::format("%f,%f",gps.latitudeDegrees,gps.longitudeDegrees); 111 | return latLon; 112 | } 113 | 114 | void AssetTracker::gpsOn() { 115 | // Power to the GPS is controlled by a FET connected to D6 116 | pinMode(D6,OUTPUT); 117 | digitalWrite(D6,LOW); 118 | gps.begin(9600); 119 | gps.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); 120 | delay(500); 121 | // Default is 1 Hz update rate 122 | gps.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); 123 | delay(500); 124 | gps.sendCommand(PGCMD_NOANTENNA); 125 | delay(500); 126 | //for ublox maxm8 gps only 127 | //internal antenna selected as default 128 | for(uint8_t i=0;i<12;i++) 129 | { 130 | Serial1.write(internalANT[i]); 131 | } 132 | delay(500); 133 | } 134 | 135 | void AssetTracker::gpsOff() { 136 | digitalWrite(D6,HIGH); 137 | } 138 | 139 | char* AssetTracker::preNMEA() { 140 | return gps.lastNMEA(); 141 | } 142 | 143 | bool AssetTracker::gpsFix() { 144 | if ((gps.latitude == 0.0) || (gps.longitude == 0.0)){ 145 | return false; 146 | } else { 147 | return true; 148 | } 149 | //return gps.fix; 150 | } 151 | 152 | bool AssetTracker::antennaInternal(){ 153 | 154 | for(uint8_t i=0;i<12;i++) 155 | { 156 | Serial1.write(internalANT[i]); //send the command to gps modul 157 | } 158 | return true; 159 | 160 | } 161 | 162 | bool AssetTracker::antennaExternal(){ 163 | 164 | for(uint8_t i=0;i<12;i++) 165 | { 166 | Serial1.write(externalANT[i]); //send the command to gps modul 167 | } 168 | return true; 169 | 170 | } 171 | 172 | // char AssetTracker::checkGPS(){ 173 | // char c = gps.read(); 174 | // return c; 175 | // } 176 | 177 | void AssetTracker::updateGPS() { 178 | //char c = gps.read(); 179 | // // if a sentence is received, we can check the checksum, parse it... 180 | // if (gps.newNMEAreceived()) { 181 | // // a tricky thing here is if we print the NMEA sentence, or data 182 | // // we end up not listening and catching other sentences! 183 | // // so be very wary if using OUTPUT_ALLDATA and trytng to print out data 184 | // //Serial.println(gps.lastNMEA()); // this also sets the newNMEAreceived() flag to false 185 | // 186 | // if (!gps.parse(gps.lastNMEA())) { 187 | // // this also sets the newNMEAreceived() flag to false 188 | // return; // we can fail to parse a sentence in which case we should just wait for another 189 | // } 190 | // } 191 | //read the complete sentence 192 | while (Serial1.available()) { 193 | char c = gps.read(); 194 | if (gps.newNMEAreceived()) { 195 | gps.parse(gps.lastNMEA()); 196 | } 197 | } 198 | } 199 | 200 | int AssetTracker::readX() { 201 | accel.read(); 202 | return accel.x; 203 | } 204 | 205 | int AssetTracker::readY() { 206 | accel.read(); 207 | return accel.y; 208 | } 209 | 210 | int AssetTracker::readZ() { 211 | accel.read(); 212 | return accel.z; 213 | } 214 | 215 | int AssetTracker::readXYZmagnitude() { 216 | accel.read(); 217 | int magnitude = sqrt((accel.x*accel.x)+(accel.y*accel.y)+(accel.z*accel.z)); 218 | return magnitude; 219 | } 220 | 221 | bool AssetTracker::setupLowPowerWakeMode(uint8_t movementThreshold) { 222 | return accel.setupLowPowerWakeMode(movementThreshold); 223 | } 224 | 225 | uint8_t AssetTracker::clearAccelInterrupt() { 226 | return accel.clearInterrupt(); 227 | } 228 | -------------------------------------------------------------------------------- /src/AssetTracker.h: -------------------------------------------------------------------------------- 1 | #ifndef AssetTracker_h 2 | #define AssetTracker_h 3 | 4 | #include "application.h" 5 | #include "math.h" 6 | 7 | #include "Adafruit_GPS.h" 8 | #include "Adafruit_LIS3DH.h" 9 | 10 | 11 | class AssetTracker { 12 | 13 | public: 14 | 15 | AssetTracker(); 16 | 17 | void 18 | begin(void), 19 | updateGPS(void), 20 | gpsOn(void), 21 | gpsOff(void); 22 | int 23 | readX(void), 24 | readY(void), 25 | readZ(void), 26 | readXYZmagnitude(void); 27 | float 28 | readLat(void), 29 | readLon(void), 30 | readLatDeg(void), 31 | readLonDeg(void), 32 | readHDOP(void), 33 | getGpsAccuracy(void), 34 | getSpeed(), 35 | getGeoIdHeight(), 36 | getAltitude(); 37 | bool 38 | antennaInternal(void), 39 | antennaExternal(void), 40 | gpsFix(void); 41 | char 42 | checkGPS(void), 43 | *preNMEA(void); 44 | String 45 | readLatLon(void); 46 | bool 47 | setupLowPowerWakeMode(uint8_t movementThreshold = 16); 48 | uint8_t 49 | getHour(), 50 | getMinute(), 51 | getSeconds(), 52 | getYear(), 53 | getMonth(), 54 | getDay(), 55 | getSatellites(), 56 | getFixQuality(), 57 | clearAccelInterrupt(); 58 | uint16_t 59 | getMilliseconds(); 60 | uint32_t 61 | getGpsTimestamp(); 62 | 63 | private: 64 | 65 | }; 66 | 67 | #endif // AssetTracker_h 68 | -------------------------------------------------------------------------------- /src/AssetTracker/AssetTracker.h: -------------------------------------------------------------------------------- 1 | #include "../AssetTracker.h" --------------------------------------------------------------------------------