Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Custom_LSM303 Custom_UBloxGPS LRAT-mbed-os USBDevice mbed-lora-radio-drv stm32EEPROM
Fork of LRAT-example-lorawan by
main.cpp@32:f211b8c28273, 2018-08-08 (annotated)
- Committer:
- Huot87
- Date:
- Wed Aug 08 20:12:26 2018 +0000
- Revision:
- 32:f211b8c28273
- Parent:
- 31:f03c183e2bf6
Commit of Accelerometer Intro code to Project Repo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:7037ed05f54f | 1 | /** |
mbed_official | 0:7037ed05f54f | 2 | * Copyright (c) 2017, Arm Limited and affiliates. |
mbed_official | 0:7037ed05f54f | 3 | * SPDX-License-Identifier: Apache-2.0 |
mbed_official | 0:7037ed05f54f | 4 | * |
mbed_official | 0:7037ed05f54f | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 0:7037ed05f54f | 6 | * you may not use this file except in compliance with the License. |
mbed_official | 0:7037ed05f54f | 7 | * You may obtain a copy of the License at |
mbed_official | 0:7037ed05f54f | 8 | * |
mbed_official | 0:7037ed05f54f | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 0:7037ed05f54f | 10 | * |
mbed_official | 0:7037ed05f54f | 11 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 0:7037ed05f54f | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 0:7037ed05f54f | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 0:7037ed05f54f | 14 | * See the License for the specific language governing permissions and |
mbed_official | 0:7037ed05f54f | 15 | * limitations under the License. |
mbed_official | 0:7037ed05f54f | 16 | */ |
mbed_official | 0:7037ed05f54f | 17 | #include <stdio.h> |
tchari | 30:ff249a9156dc | 18 | #include "mbed.h" |
mbed_official | 3:8c7198d1a2a1 | 19 | |
mbed_official | 0:7037ed05f54f | 20 | #include "lorawan/LoRaWANInterface.h" |
mbed_official | 0:7037ed05f54f | 21 | #include "lorawan/system/lorawan_data_structures.h" |
mbed_official | 0:7037ed05f54f | 22 | #include "events/EventQueue.h" |
mbed_official | 0:7037ed05f54f | 23 | |
mbed_official | 0:7037ed05f54f | 24 | // Application helpers |
mbed_official | 0:7037ed05f54f | 25 | #include "DummySensor.h" |
mbed_official | 0:7037ed05f54f | 26 | #include "trace_helper.h" |
mbed_official | 0:7037ed05f54f | 27 | #include "lora_radio_helper.h" |
mbed_official | 0:7037ed05f54f | 28 | |
tchari | 30:ff249a9156dc | 29 | #include "mbed-trace/mbed_trace.h" |
tchari | 30:ff249a9156dc | 30 | #define TRACE_GROUP "MAIN" |
tchari | 30:ff249a9156dc | 31 | |
Huot87 | 32:f211b8c28273 | 32 | // Huot Added LSM303AGR_ACC |
Huot87 | 32:f211b8c28273 | 33 | #include "LSM303AGRAccSensor.h" |
Huot87 | 32:f211b8c28273 | 34 | |
mbed_official | 0:7037ed05f54f | 35 | using namespace events; |
mbed_official | 0:7037ed05f54f | 36 | |
mbed_official | 12:5015dfead3f2 | 37 | // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. |
mbed_official | 12:5015dfead3f2 | 38 | // This example only communicates with much shorter messages (<30 bytes). |
mbed_official | 12:5015dfead3f2 | 39 | // If longer messages are used, these buffers must be changed accordingly. |
mbed_official | 12:5015dfead3f2 | 40 | uint8_t tx_buffer[30]; |
mbed_official | 12:5015dfead3f2 | 41 | uint8_t rx_buffer[30]; |
mbed_official | 0:7037ed05f54f | 42 | |
Huot87 | 32:f211b8c28273 | 43 | // ACCELEROMETER AXES VARIABLE INITIALIZATION |
Huot87 | 32:f211b8c28273 | 44 | int x = 0; |
Huot87 | 32:f211b8c28273 | 45 | bool interruptOccured = false; |
Huot87 | 32:f211b8c28273 | 46 | |
Huot87 | 32:f211b8c28273 | 47 | int32_t axes[3]; |
Huot87 | 32:f211b8c28273 | 48 | |
mbed_official | 0:7037ed05f54f | 49 | /* |
mbed_official | 0:7037ed05f54f | 50 | * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing |
mbed_official | 0:7037ed05f54f | 51 | */ |
mbed_official | 0:7037ed05f54f | 52 | #define TX_TIMER 10000 |
mbed_official | 0:7037ed05f54f | 53 | |
mbed_official | 0:7037ed05f54f | 54 | /** |
mbed_official | 0:7037ed05f54f | 55 | * Maximum number of events for the event queue. |
mbed_official | 12:5015dfead3f2 | 56 | * 10 is the safe number for the stack events, however, if application |
mbed_official | 0:7037ed05f54f | 57 | * also uses the queue for whatever purposes, this number should be increased. |
mbed_official | 0:7037ed05f54f | 58 | */ |
mbed_official | 12:5015dfead3f2 | 59 | #define MAX_NUMBER_OF_EVENTS 10 |
mbed_official | 0:7037ed05f54f | 60 | |
mbed_official | 0:7037ed05f54f | 61 | /** |
mbed_official | 0:7037ed05f54f | 62 | * Maximum number of retries for CONFIRMED messages before giving up |
mbed_official | 0:7037ed05f54f | 63 | */ |
tchari | 30:ff249a9156dc | 64 | #define CONFIRMED_MSG_RETRY_COUNTER 15 |
mbed_official | 0:7037ed05f54f | 65 | |
mbed_official | 0:7037ed05f54f | 66 | /** |
mbed_official | 0:7037ed05f54f | 67 | * Dummy pin for dummy sensor |
mbed_official | 0:7037ed05f54f | 68 | */ |
mbed_official | 0:7037ed05f54f | 69 | #define PC_9 0 |
mbed_official | 0:7037ed05f54f | 70 | |
mbed_official | 0:7037ed05f54f | 71 | /** |
mbed_official | 0:7037ed05f54f | 72 | * Dummy sensor class object |
mbed_official | 0:7037ed05f54f | 73 | */ |
mbed_official | 0:7037ed05f54f | 74 | DS1820 ds1820(PC_9); |
mbed_official | 0:7037ed05f54f | 75 | |
mbed_official | 0:7037ed05f54f | 76 | /** |
mbed_official | 0:7037ed05f54f | 77 | * This event queue is the global event queue for both the |
mbed_official | 0:7037ed05f54f | 78 | * application and stack. To conserve memory, the stack is designed to run |
mbed_official | 0:7037ed05f54f | 79 | * in the same thread as the application and the application is responsible for |
mbed_official | 0:7037ed05f54f | 80 | * providing an event queue to the stack that will be used for ISR deferment as |
mbed_official | 0:7037ed05f54f | 81 | * well as application information event queuing. |
mbed_official | 0:7037ed05f54f | 82 | */ |
mbed_official | 0:7037ed05f54f | 83 | static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS * EVENTS_EVENT_SIZE); |
mbed_official | 0:7037ed05f54f | 84 | |
mbed_official | 0:7037ed05f54f | 85 | /** |
mbed_official | 0:7037ed05f54f | 86 | * Event handler. |
mbed_official | 0:7037ed05f54f | 87 | * |
mbed_official | 0:7037ed05f54f | 88 | * This will be passed to the LoRaWAN stack to queue events for the |
mbed_official | 0:7037ed05f54f | 89 | * application which in turn drive the application. |
mbed_official | 0:7037ed05f54f | 90 | */ |
mbed_official | 0:7037ed05f54f | 91 | static void lora_event_handler(lorawan_event_t event); |
mbed_official | 0:7037ed05f54f | 92 | |
mbed_official | 0:7037ed05f54f | 93 | /** |
mbed_official | 2:dc95ac6d6d4e | 94 | * Constructing Mbed LoRaWANInterface and passing it down the radio object. |
mbed_official | 0:7037ed05f54f | 95 | */ |
mbed_official | 2:dc95ac6d6d4e | 96 | static LoRaWANInterface lorawan(radio); |
mbed_official | 0:7037ed05f54f | 97 | |
mbed_official | 0:7037ed05f54f | 98 | /** |
mbed_official | 0:7037ed05f54f | 99 | * Application specific callbacks |
mbed_official | 0:7037ed05f54f | 100 | */ |
mbed_official | 0:7037ed05f54f | 101 | static lorawan_app_callbacks_t callbacks; |
mbed_official | 0:7037ed05f54f | 102 | |
tchari | 30:ff249a9156dc | 103 | int mytime; |
tchari | 30:ff249a9156dc | 104 | int mybatt; |
tchari | 30:ff249a9156dc | 105 | double mylat; |
tchari | 30:ff249a9156dc | 106 | double mylon; |
tchari | 30:ff249a9156dc | 107 | |
Huot87 | 32:f211b8c28273 | 108 | #define NEOM8M_ADR_GPS 0x42 |
Huot87 | 32:f211b8c28273 | 109 | #define LSM303_ADR_ACC 0x32 |
Huot87 | 32:f211b8c28273 | 110 | #define LSM303_ADR_MAG 0x3C |
Huot87 | 32:f211b8c28273 | 111 | |
Huot87 | 32:f211b8c28273 | 112 | // ------ HUOT Change |
Huot87 | 32:f211b8c28273 | 113 | // Had to change I2C to use DevI2C to get below LSM303AGRAccSensor Constructor to function |
Huot87 | 32:f211b8c28273 | 114 | static DevI2C i2c(PB_9, PB_8); |
tchari | 30:ff249a9156dc | 115 | |
Huot87 | 32:f211b8c28273 | 116 | static LSM303AGRAccSensor accel(&i2c, LSM303_ADR_ACC, PB_14); |
Huot87 | 32:f211b8c28273 | 117 | |
Huot87 | 32:f211b8c28273 | 118 | InterruptIn accelPin(PB_14); |
Huot87 | 32:f211b8c28273 | 119 | |
Huot87 | 32:f211b8c28273 | 120 | void accelRunning(); |
Huot87 | 32:f211b8c28273 | 121 | void accelSetup(); |
Huot87 | 32:f211b8c28273 | 122 | |
tchari | 30:ff249a9156dc | 123 | |
mbed_official | 0:7037ed05f54f | 124 | /** |
mbed_official | 0:7037ed05f54f | 125 | * Entry point for application |
mbed_official | 0:7037ed05f54f | 126 | */ |
mbed_official | 0:7037ed05f54f | 127 | int main (void) |
mbed_official | 0:7037ed05f54f | 128 | { |
mbed_official | 0:7037ed05f54f | 129 | // setup tracing |
mbed_official | 0:7037ed05f54f | 130 | setup_trace(); |
mbed_official | 0:7037ed05f54f | 131 | |
mbed_official | 0:7037ed05f54f | 132 | // stores the status of a call to LoRaWAN protocol |
mbed_official | 0:7037ed05f54f | 133 | lorawan_status_t retcode; |
mbed_official | 0:7037ed05f54f | 134 | |
tchari | 30:ff249a9156dc | 135 | printf("\r\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -\r\n"); |
tchari | 30:ff249a9156dc | 136 | |
Huot87 | 32:f211b8c28273 | 137 | // HUOT -- ACCCELEROMETER SENSOR SETUP |
Huot87 | 32:f211b8c28273 | 138 | accelSetup(); |
Huot87 | 32:f211b8c28273 | 139 | |
Huot87 | 32:f211b8c28273 | 140 | while (true) { |
Huot87 | 32:f211b8c28273 | 141 | if (interruptOccured) { |
Huot87 | 32:f211b8c28273 | 142 | printf("+++++++ INTERRUPT COUNTER: %d +++++++ \r\n", x); |
Huot87 | 32:f211b8c28273 | 143 | |
Huot87 | 32:f211b8c28273 | 144 | // Write to all accel regs to configure interrupt. |
Huot87 | 32:f211b8c28273 | 145 | accel.write_reg(LSM303AGR_ACC_INT1_CFG, 0x2A); |
Huot87 | 32:f211b8c28273 | 146 | printf("Enable X and Y Axes High Interrupt Events. \r\n"); |
Huot87 | 32:f211b8c28273 | 147 | |
Huot87 | 32:f211b8c28273 | 148 | // READ SOURCE DATA |
Huot87 | 32:f211b8c28273 | 149 | uint8_t data = 0; |
Huot87 | 32:f211b8c28273 | 150 | accel.read_reg(LSM303AGR_ACC_INT1_SOURCE, &data); |
Huot87 | 32:f211b8c28273 | 151 | printf("DATA = %02x \r\n", data); |
Huot87 | 32:f211b8c28273 | 152 | |
Huot87 | 32:f211b8c28273 | 153 | interruptOccured = false; |
Huot87 | 32:f211b8c28273 | 154 | } else { |
Huot87 | 32:f211b8c28273 | 155 | printf("========= No Interrupt on this pass... ========= \r\n"); |
Huot87 | 32:f211b8c28273 | 156 | } |
Huot87 | 32:f211b8c28273 | 157 | |
Huot87 | 32:f211b8c28273 | 158 | wait(4); |
Huot87 | 32:f211b8c28273 | 159 | } |
Huot87 | 32:f211b8c28273 | 160 | |
tchari | 30:ff249a9156dc | 161 | mytime = 0; |
tchari | 30:ff249a9156dc | 162 | mybatt = 15; |
tchari | 30:ff249a9156dc | 163 | mylat = 51.06509; |
tchari | 30:ff249a9156dc | 164 | mylon = -114.08895; |
tchari | 30:ff249a9156dc | 165 | |
mbed_official | 0:7037ed05f54f | 166 | // Initialize LoRaWAN stack |
mbed_official | 2:dc95ac6d6d4e | 167 | if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 168 | printf("\r\n LoRa initialization failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 169 | return -1; |
mbed_official | 0:7037ed05f54f | 170 | } |
mbed_official | 0:7037ed05f54f | 171 | |
mbed_official | 0:7037ed05f54f | 172 | printf("\r\n Mbed LoRaWANStack initialized \r\n"); |
tchari | 30:ff249a9156dc | 173 | //printf("MBED_CONF_LORA_APP_PORT: %d", MBED_CONF_LORA_APP_PORT); |
mbed_official | 0:7037ed05f54f | 174 | |
mbed_official | 0:7037ed05f54f | 175 | // prepare application callbacks |
mbed_official | 0:7037ed05f54f | 176 | callbacks.events = mbed::callback(lora_event_handler); |
mbed_official | 2:dc95ac6d6d4e | 177 | lorawan.add_app_callbacks(&callbacks); |
mbed_official | 0:7037ed05f54f | 178 | |
mbed_official | 0:7037ed05f54f | 179 | // Set number of retries in case of CONFIRMED messages |
mbed_official | 2:dc95ac6d6d4e | 180 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
mbed_official | 0:7037ed05f54f | 181 | != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 182 | printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n"); |
mbed_official | 0:7037ed05f54f | 183 | return -1; |
mbed_official | 0:7037ed05f54f | 184 | } |
mbed_official | 0:7037ed05f54f | 185 | |
mbed_official | 0:7037ed05f54f | 186 | printf("\r\n CONFIRMED message retries : %d \r\n", |
mbed_official | 0:7037ed05f54f | 187 | CONFIRMED_MSG_RETRY_COUNTER); |
mbed_official | 0:7037ed05f54f | 188 | |
mbed_official | 0:7037ed05f54f | 189 | // Enable adaptive data rate |
mbed_official | 2:dc95ac6d6d4e | 190 | if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
mbed_official | 0:7037ed05f54f | 191 | printf("\r\n enable_adaptive_datarate failed! \r\n"); |
mbed_official | 0:7037ed05f54f | 192 | return -1; |
mbed_official | 0:7037ed05f54f | 193 | } |
mbed_official | 0:7037ed05f54f | 194 | |
mbed_official | 0:7037ed05f54f | 195 | printf("\r\n Adaptive data rate (ADR) - Enabled \r\n"); |
mbed_official | 0:7037ed05f54f | 196 | |
mbed_official | 2:dc95ac6d6d4e | 197 | retcode = lorawan.connect(); |
tchari | 30:ff249a9156dc | 198 | /*if (retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
tchari | 30:ff249a9156dc | 199 | printf("Retcode = Connecting."); |
tchari | 30:ff249a9156dc | 200 | }*/ |
mbed_official | 0:7037ed05f54f | 201 | if (retcode == LORAWAN_STATUS_OK || |
mbed_official | 0:7037ed05f54f | 202 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
mbed_official | 0:7037ed05f54f | 203 | } else { |
mbed_official | 0:7037ed05f54f | 204 | printf("\r\n Connection error, code = %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 205 | return -1; |
mbed_official | 0:7037ed05f54f | 206 | } |
mbed_official | 0:7037ed05f54f | 207 | |
mbed_official | 0:7037ed05f54f | 208 | printf("\r\n Connection - In Progress ...\r\n"); |
mbed_official | 0:7037ed05f54f | 209 | |
mbed_official | 0:7037ed05f54f | 210 | // make your event queue dispatching events forever |
mbed_official | 0:7037ed05f54f | 211 | ev_queue.dispatch_forever(); |
mbed_official | 3:8c7198d1a2a1 | 212 | |
mbed_official | 3:8c7198d1a2a1 | 213 | return 0; |
mbed_official | 0:7037ed05f54f | 214 | } |
mbed_official | 0:7037ed05f54f | 215 | |
mbed_official | 0:7037ed05f54f | 216 | /** |
mbed_official | 0:7037ed05f54f | 217 | * Sends a message to the Network Server |
mbed_official | 0:7037ed05f54f | 218 | */ |
mbed_official | 0:7037ed05f54f | 219 | static void send_message() |
mbed_official | 0:7037ed05f54f | 220 | { |
mbed_official | 0:7037ed05f54f | 221 | uint16_t packet_len; |
mbed_official | 0:7037ed05f54f | 222 | int16_t retcode; |
mbed_official | 0:7037ed05f54f | 223 | float sensor_value; |
mbed_official | 0:7037ed05f54f | 224 | |
mbed_official | 0:7037ed05f54f | 225 | if (ds1820.begin()) { |
mbed_official | 0:7037ed05f54f | 226 | ds1820.startConversion(); |
mbed_official | 0:7037ed05f54f | 227 | sensor_value = ds1820.read(); |
mbed_official | 0:7037ed05f54f | 228 | printf("\r\n Dummy Sensor Value = %3.1f \r\n", sensor_value); |
mbed_official | 0:7037ed05f54f | 229 | ds1820.startConversion(); |
mbed_official | 0:7037ed05f54f | 230 | } else { |
mbed_official | 0:7037ed05f54f | 231 | printf("\r\n No sensor found \r\n"); |
mbed_official | 0:7037ed05f54f | 232 | return; |
mbed_official | 0:7037ed05f54f | 233 | } |
mbed_official | 0:7037ed05f54f | 234 | |
tchari | 30:ff249a9156dc | 235 | //packet_len = sprintf((char*) tx_buffer, "Dummy Sensor Value is %3.1f", |
tchari | 30:ff249a9156dc | 236 | // sensor_value); |
tchari | 30:ff249a9156dc | 237 | //packet_len = sprintf((char*) tx_buffer, "VAL=%3.1f", sensor_value); |
tchari | 30:ff249a9156dc | 238 | //packet_len = sprintf((char*) tx_buffer, "%d,%d,%f,%f", mytime, mybatt, mylat, mylon); |
tchari | 30:ff249a9156dc | 239 | //printf("BUF: |%s|", tx_buffer); |
tchari | 30:ff249a9156dc | 240 | time_t tNow = time(NULL); |
tchari | 30:ff249a9156dc | 241 | printf("Clock: %d\r\n", tNow); |
tchari | 30:ff249a9156dc | 242 | mytime = tNow; |
lpeters | 31:f03c183e2bf6 | 243 | char cmd[2]; |
Huot87 | 32:f211b8c28273 | 244 | |
Huot87 | 32:f211b8c28273 | 245 | //char buf[1024]; |
Huot87 | 32:f211b8c28273 | 246 | |
Huot87 | 32:f211b8c28273 | 247 | // ---------- HUOT COMMENT BLOCK OUT ------ |
Huot87 | 32:f211b8c28273 | 248 | /* |
lpeters | 31:f03c183e2bf6 | 249 | char buf; |
lpeters | 31:f03c183e2bf6 | 250 | //cmd[0] = 0xFF; |
lpeters | 31:f03c183e2bf6 | 251 | //i2c.write(NEOM8M_ADR_GPS, cmd, 1); |
lpeters | 31:f03c183e2bf6 | 252 | //i2c.read(NEOM8M_ADR_GPS, buf, 1024); |
lpeters | 31:f03c183e2bf6 | 253 | cmd[0] = 0x20; |
lpeters | 31:f03c183e2bf6 | 254 | cmd[1] = 0x57; |
lpeters | 31:f03c183e2bf6 | 255 | i2c.write(LSM303_ADR_ACC, cmd, 2); |
lpeters | 31:f03c183e2bf6 | 256 | cmd[0] = 0x20; |
Huot87 | 32:f211b8c28273 | 257 | //i2c.write(LSM303_ADR_ACC, cmd, 1); |
lpeters | 31:f03c183e2bf6 | 258 | i2c.read(LSM303_ADR_ACC, &buf, 1); |
lpeters | 31:f03c183e2bf6 | 259 | //i2c.read(LSM303_ADR_ACC, buf, 1); |
lpeters | 31:f03c183e2bf6 | 260 | printf("Return Value: %02x", buf); |
Huot87 | 32:f211b8c28273 | 261 | */ |
Huot87 | 32:f211b8c28273 | 262 | |
Huot87 | 32:f211b8c28273 | 263 | /* HUOT -- TEST OF ACCELEROMETER GET DATA FROM AXES |
Huot87 | 32:f211b8c28273 | 264 | if (x % 2 == 0) { |
Huot87 | 32:f211b8c28273 | 265 | accel.get_x_axes(axes); |
Huot87 | 32:f211b8c28273 | 266 | printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
Huot87 | 32:f211b8c28273 | 267 | } |
Huot87 | 32:f211b8c28273 | 268 | */ |
tchari | 30:ff249a9156dc | 269 | int ilat = (int)(mylat * 100000); |
tchari | 30:ff249a9156dc | 270 | int ilon = (int)(mylon * 100000); |
tchari | 30:ff249a9156dc | 271 | packet_len = 11; |
tchari | 30:ff249a9156dc | 272 | tx_buffer[0] = (mytime >> 24) & 0xFF; |
tchari | 30:ff249a9156dc | 273 | tx_buffer[1] = (mytime >> 16) & 0xFF; |
tchari | 30:ff249a9156dc | 274 | tx_buffer[2] = (mytime >> 8) & 0xFF; |
tchari | 30:ff249a9156dc | 275 | tx_buffer[3] = (mytime >> 0) & 0xFF; |
tchari | 30:ff249a9156dc | 276 | tx_buffer[4] = ((mybatt << 4) & 0xF0) | ((ilat >> 22) & 0x0F); |
tchari | 30:ff249a9156dc | 277 | tx_buffer[5] = (ilat >> 14) & 0xFF; |
tchari | 30:ff249a9156dc | 278 | tx_buffer[6] = (ilat >> 6) & 0xFF; |
tchari | 30:ff249a9156dc | 279 | tx_buffer[7] = ((ilat << 2) & 0xFC) | ((ilon >> 24) & 0x03); |
tchari | 30:ff249a9156dc | 280 | tx_buffer[8] = (ilon >> 16) & 0xFF; |
tchari | 30:ff249a9156dc | 281 | tx_buffer[9] = (ilon >> 8) & 0xFF; |
tchari | 30:ff249a9156dc | 282 | tx_buffer[10] = (ilon >> 0) & 0xFF; |
tchari | 30:ff249a9156dc | 283 | printf("\r\nBUF: |"); |
tchari | 30:ff249a9156dc | 284 | int i; |
tchari | 30:ff249a9156dc | 285 | for (i = 0; i < packet_len; i++) { printf("%02x", tx_buffer[i]); } |
tchari | 30:ff249a9156dc | 286 | printf("|\r\n"); |
tchari | 30:ff249a9156dc | 287 | mytime++; |
tchari | 30:ff249a9156dc | 288 | if (mybatt == 0) { mybatt = 15; } else { mybatt--; } |
mbed_official | 2:dc95ac6d6d4e | 289 | retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, |
mbed_official | 0:7037ed05f54f | 290 | MSG_CONFIRMED_FLAG); |
mbed_official | 0:7037ed05f54f | 291 | |
mbed_official | 0:7037ed05f54f | 292 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 293 | retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - WOULD BLOCK\r\n") |
mbed_official | 0:7037ed05f54f | 294 | : printf("\r\n send() - Error code %d \r\n", retcode); |
mbed_official | 26:f07f5febf97f | 295 | |
mbed_official | 26:f07f5febf97f | 296 | if (retcode == LORAWAN_STATUS_WOULD_BLOCK) { |
mbed_official | 26:f07f5febf97f | 297 | //retry in 3 seconds |
mbed_official | 26:f07f5febf97f | 298 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 299 | ev_queue.call_in(3000, send_message); |
mbed_official | 26:f07f5febf97f | 300 | } |
mbed_official | 26:f07f5febf97f | 301 | } |
mbed_official | 0:7037ed05f54f | 302 | return; |
mbed_official | 0:7037ed05f54f | 303 | } |
mbed_official | 0:7037ed05f54f | 304 | |
mbed_official | 0:7037ed05f54f | 305 | printf("\r\n %d bytes scheduled for transmission \r\n", retcode); |
Huot87 | 32:f211b8c28273 | 306 | |
Huot87 | 32:f211b8c28273 | 307 | x += 1; |
Huot87 | 32:f211b8c28273 | 308 | |
mbed_official | 12:5015dfead3f2 | 309 | memset(tx_buffer, 0, sizeof(tx_buffer)); |
mbed_official | 0:7037ed05f54f | 310 | } |
mbed_official | 0:7037ed05f54f | 311 | |
mbed_official | 0:7037ed05f54f | 312 | /** |
mbed_official | 0:7037ed05f54f | 313 | * Receive a message from the Network Server |
mbed_official | 0:7037ed05f54f | 314 | */ |
mbed_official | 0:7037ed05f54f | 315 | static void receive_message() |
mbed_official | 0:7037ed05f54f | 316 | { |
mbed_official | 0:7037ed05f54f | 317 | int16_t retcode; |
mbed_official | 2:dc95ac6d6d4e | 318 | retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer, |
mbed_official | 12:5015dfead3f2 | 319 | sizeof(rx_buffer), |
mbed_official | 0:7037ed05f54f | 320 | MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG); |
mbed_official | 0:7037ed05f54f | 321 | |
mbed_official | 0:7037ed05f54f | 322 | if (retcode < 0) { |
mbed_official | 0:7037ed05f54f | 323 | printf("\r\n receive() - Error code %d \r\n", retcode); |
mbed_official | 0:7037ed05f54f | 324 | return; |
mbed_official | 0:7037ed05f54f | 325 | } |
mbed_official | 0:7037ed05f54f | 326 | |
mbed_official | 0:7037ed05f54f | 327 | printf(" Data:"); |
mbed_official | 0:7037ed05f54f | 328 | |
mbed_official | 0:7037ed05f54f | 329 | for (uint8_t i = 0; i < retcode; i++) { |
mbed_official | 0:7037ed05f54f | 330 | printf("%x", rx_buffer[i]); |
mbed_official | 0:7037ed05f54f | 331 | } |
mbed_official | 0:7037ed05f54f | 332 | |
mbed_official | 0:7037ed05f54f | 333 | printf("\r\n Data Length: %d\r\n", retcode); |
mbed_official | 0:7037ed05f54f | 334 | |
mbed_official | 12:5015dfead3f2 | 335 | memset(rx_buffer, 0, sizeof(rx_buffer)); |
mbed_official | 0:7037ed05f54f | 336 | } |
mbed_official | 0:7037ed05f54f | 337 | |
mbed_official | 0:7037ed05f54f | 338 | /** |
mbed_official | 0:7037ed05f54f | 339 | * Event handler |
mbed_official | 0:7037ed05f54f | 340 | */ |
mbed_official | 0:7037ed05f54f | 341 | static void lora_event_handler(lorawan_event_t event) |
mbed_official | 0:7037ed05f54f | 342 | { |
tchari | 30:ff249a9156dc | 343 | tr_debug("In lora_event_handler(%d)...", event); |
mbed_official | 0:7037ed05f54f | 344 | switch (event) { |
mbed_official | 0:7037ed05f54f | 345 | case CONNECTED: |
mbed_official | 0:7037ed05f54f | 346 | printf("\r\n Connection - Successful \r\n"); |
mbed_official | 0:7037ed05f54f | 347 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 348 | send_message(); |
mbed_official | 0:7037ed05f54f | 349 | } else { |
mbed_official | 0:7037ed05f54f | 350 | ev_queue.call_every(TX_TIMER, send_message); |
mbed_official | 0:7037ed05f54f | 351 | } |
mbed_official | 0:7037ed05f54f | 352 | |
mbed_official | 0:7037ed05f54f | 353 | break; |
mbed_official | 0:7037ed05f54f | 354 | case DISCONNECTED: |
mbed_official | 0:7037ed05f54f | 355 | ev_queue.break_dispatch(); |
mbed_official | 0:7037ed05f54f | 356 | printf("\r\n Disconnected Successfully \r\n"); |
mbed_official | 0:7037ed05f54f | 357 | break; |
mbed_official | 0:7037ed05f54f | 358 | case TX_DONE: |
mbed_official | 0:7037ed05f54f | 359 | printf("\r\n Message Sent to Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 360 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 361 | send_message(); |
mbed_official | 0:7037ed05f54f | 362 | } |
mbed_official | 0:7037ed05f54f | 363 | break; |
mbed_official | 0:7037ed05f54f | 364 | case TX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 365 | case TX_ERROR: |
mbed_official | 0:7037ed05f54f | 366 | case TX_CRYPTO_ERROR: |
mbed_official | 0:7037ed05f54f | 367 | case TX_SCHEDULING_ERROR: |
mbed_official | 0:7037ed05f54f | 368 | printf("\r\n Transmission Error - EventCode = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 369 | // try again |
mbed_official | 0:7037ed05f54f | 370 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 0:7037ed05f54f | 371 | send_message(); |
mbed_official | 0:7037ed05f54f | 372 | } |
mbed_official | 0:7037ed05f54f | 373 | break; |
mbed_official | 0:7037ed05f54f | 374 | case RX_DONE: |
mbed_official | 0:7037ed05f54f | 375 | printf("\r\n Received message from Network Server \r\n"); |
mbed_official | 0:7037ed05f54f | 376 | receive_message(); |
mbed_official | 0:7037ed05f54f | 377 | break; |
mbed_official | 0:7037ed05f54f | 378 | case RX_TIMEOUT: |
mbed_official | 0:7037ed05f54f | 379 | case RX_ERROR: |
mbed_official | 0:7037ed05f54f | 380 | printf("\r\n Error in reception - Code = %d \r\n", event); |
mbed_official | 0:7037ed05f54f | 381 | break; |
mbed_official | 0:7037ed05f54f | 382 | case JOIN_FAILURE: |
mbed_official | 0:7037ed05f54f | 383 | printf("\r\n OTAA Failed - Check Keys \r\n"); |
mbed_official | 0:7037ed05f54f | 384 | break; |
mbed_official | 26:f07f5febf97f | 385 | case UPLINK_REQUIRED: |
mbed_official | 26:f07f5febf97f | 386 | printf("\r\n Uplink required by NS \r\n"); |
mbed_official | 26:f07f5febf97f | 387 | if (MBED_CONF_LORA_DUTY_CYCLE_ON) { |
mbed_official | 26:f07f5febf97f | 388 | send_message(); |
mbed_official | 26:f07f5febf97f | 389 | } |
mbed_official | 26:f07f5febf97f | 390 | break; |
mbed_official | 0:7037ed05f54f | 391 | default: |
mbed_official | 0:7037ed05f54f | 392 | MBED_ASSERT("Unknown Event"); |
mbed_official | 0:7037ed05f54f | 393 | } |
mbed_official | 0:7037ed05f54f | 394 | } |
mbed_official | 0:7037ed05f54f | 395 | |
Huot87 | 32:f211b8c28273 | 396 | void accelRunning() |
Huot87 | 32:f211b8c28273 | 397 | { |
Huot87 | 32:f211b8c28273 | 398 | interruptOccured = true; |
Huot87 | 32:f211b8c28273 | 399 | x++; |
Huot87 | 32:f211b8c28273 | 400 | |
Huot87 | 32:f211b8c28273 | 401 | // printf("++++++++++++++ Interrupt Called! Get Drunk! +++++++++++++++++++ \r\n"); |
Huot87 | 32:f211b8c28273 | 402 | |
Huot87 | 32:f211b8c28273 | 403 | // printf("COUNTER: ---- %d \r\n", x++); |
Huot87 | 32:f211b8c28273 | 404 | |
Huot87 | 32:f211b8c28273 | 405 | // uint8_t srcData; |
Huot87 | 32:f211b8c28273 | 406 | // accel.read_reg(LSM303AGR_ACC_INT1_SOURCE, &srcData); |
Huot87 | 32:f211b8c28273 | 407 | // printf("SRC DATA = %02x \r\n", srcData); |
Huot87 | 32:f211b8c28273 | 408 | |
Huot87 | 32:f211b8c28273 | 409 | // accel.get_x_axes(axes); |
Huot87 | 32:f211b8c28273 | 410 | // printf("LSM303AGR [acc/mg]: %6ld, %6ld, %6ld\r\n", axes[0], axes[1], axes[2]); |
Huot87 | 32:f211b8c28273 | 411 | } |
Huot87 | 32:f211b8c28273 | 412 | |
Huot87 | 32:f211b8c28273 | 413 | // ---- HUOT ACCEL SETUP |
Huot87 | 32:f211b8c28273 | 414 | void accelSetup() |
Huot87 | 32:f211b8c28273 | 415 | { |
Huot87 | 32:f211b8c28273 | 416 | int initCode = accel.init(NULL); |
Huot87 | 32:f211b8c28273 | 417 | printf("INIT CODE = %d \r\n", initCode); |
Huot87 | 32:f211b8c28273 | 418 | |
Huot87 | 32:f211b8c28273 | 419 | int enableCode = accel.enable(); |
Huot87 | 32:f211b8c28273 | 420 | printf("ENABLE CODE = %d \r\n", enableCode); |
Huot87 | 32:f211b8c28273 | 421 | |
Huot87 | 32:f211b8c28273 | 422 | /* HP FILTER BYPASS |
Huot87 | 32:f211b8c28273 | 423 | // SETUP INTERRUPT |
Huot87 | 32:f211b8c28273 | 424 | // ODR = 100 Hz |
Huot87 | 32:f211b8c28273 | 425 | accel.write_reg(LSM303AGR_ACC_CTRL_REG1, 0x67); |
Huot87 | 32:f211b8c28273 | 426 | printf("ODR Set \r\n"); |
Huot87 | 32:f211b8c28273 | 427 | // High-Pass filter Disabled |
Huot87 | 32:f211b8c28273 | 428 | accel.write_reg(LSM303AGR_ACC_CTRL_REG2, 0x00); |
Huot87 | 32:f211b8c28273 | 429 | printf("HP Filter Disabled \r\n"); |
Huot87 | 32:f211b8c28273 | 430 | // Interrupt Driven to INT1 pad |
Huot87 | 32:f211b8c28273 | 431 | accel.write_reg(LSM303AGR_ACC_CTRL_REG3, 0x40); |
Huot87 | 32:f211b8c28273 | 432 | printf("INT1 Pad \r\n"); |
Huot87 | 32:f211b8c28273 | 433 | // Full Scale = 2 g |
Huot87 | 32:f211b8c28273 | 434 | accel.write_reg(LSM303AGR_ACC_CTRL_REG4, 0x00); |
Huot87 | 32:f211b8c28273 | 435 | printf("Full Scale set at 2G's \r\n"); |
Huot87 | 32:f211b8c28273 | 436 | // Interrupt Latched |
Huot87 | 32:f211b8c28273 | 437 | accel.write_reg(LSM303AGR_ACC_CTRL_REG5, 0x08); |
Huot87 | 32:f211b8c28273 | 438 | printf("Interrupt Latched \r\n"); |
Huot87 | 32:f211b8c28273 | 439 | // Threshold = 250 mg |
Huot87 | 32:f211b8c28273 | 440 | accel.write_reg(LSM303AGR_ACC_INT1_THS, 0x01); |
Huot87 | 32:f211b8c28273 | 441 | printf("Threshold set at XXX mg's \r\n"); |
Huot87 | 32:f211b8c28273 | 442 | // Duration Set |
Huot87 | 32:f211b8c28273 | 443 | accel.write_reg(LSM303AGR_ACC_INT1_DURATION, 0x00); |
Huot87 | 32:f211b8c28273 | 444 | printf("Duration set to 0"); |
Huot87 | 32:f211b8c28273 | 445 | // Enable X-High and Y-High Interrupt Generation |
Huot87 | 32:f211b8c28273 | 446 | accel.write_reg(LSM303AGR_ACC_INT1_CFG, 0x05); |
Huot87 | 32:f211b8c28273 | 447 | printf("Enable X and Y Axes High Interrupt Events. \r\n"); |
Huot87 | 32:f211b8c28273 | 448 | */ |
Huot87 | 32:f211b8c28273 | 449 | // SETUP INTERRUPT |
Huot87 | 32:f211b8c28273 | 450 | // ODR = 100 Hz |
Huot87 | 32:f211b8c28273 | 451 | accel.write_reg(LSM303AGR_ACC_CTRL_REG1, 0x67); |
Huot87 | 32:f211b8c28273 | 452 | printf("ODR Set \r\n"); |
Huot87 | 32:f211b8c28273 | 453 | // High-Pass filter Enabled |
Huot87 | 32:f211b8c28273 | 454 | accel.write_reg(LSM303AGR_ACC_CTRL_REG2, 0x09); |
Huot87 | 32:f211b8c28273 | 455 | printf("HP Filter Enabled \r\n"); |
Huot87 | 32:f211b8c28273 | 456 | // Interrupt Driven to INT1 pad |
Huot87 | 32:f211b8c28273 | 457 | accel.write_reg(LSM303AGR_ACC_CTRL_REG3, 0x40); |
Huot87 | 32:f211b8c28273 | 458 | printf("INT1 Pad \r\n"); |
Huot87 | 32:f211b8c28273 | 459 | // Full Scale = 2 g |
Huot87 | 32:f211b8c28273 | 460 | accel.write_reg(LSM303AGR_ACC_CTRL_REG4, 0x00); |
Huot87 | 32:f211b8c28273 | 461 | printf("Full Scale set at 2G's \r\n"); |
Huot87 | 32:f211b8c28273 | 462 | // Interrupt Latched |
Huot87 | 32:f211b8c28273 | 463 | accel.write_reg(LSM303AGR_ACC_CTRL_REG5, 0x08); |
Huot87 | 32:f211b8c28273 | 464 | printf("Interrupt Latched \r\n"); |
Huot87 | 32:f211b8c28273 | 465 | |
Huot87 | 32:f211b8c28273 | 466 | accel.write_reg(LSM303AGR_ACC_INT1_THS, 0x01); |
Huot87 | 32:f211b8c28273 | 467 | printf("Threshold set at XXX mg's \r\n"); |
Huot87 | 32:f211b8c28273 | 468 | // Duration Set |
Huot87 | 32:f211b8c28273 | 469 | accel.write_reg(LSM303AGR_ACC_INT1_DURATION, 0x00); |
Huot87 | 32:f211b8c28273 | 470 | printf("Duration set to 0"); |
Huot87 | 32:f211b8c28273 | 471 | |
Huot87 | 32:f211b8c28273 | 472 | // DUMMY READ TO FORCE HP FILTER TO CURRENT ACCELERATION VALUE |
Huot87 | 32:f211b8c28273 | 473 | uint8_t read_ref; |
Huot87 | 32:f211b8c28273 | 474 | accel.read_reg(LSM303AGR_ACC_REFERENCE, &read_ref); |
Huot87 | 32:f211b8c28273 | 475 | |
Huot87 | 32:f211b8c28273 | 476 | // Enable X-High, Y-High and Z-High Interrupt Generation |
Huot87 | 32:f211b8c28273 | 477 | accel.write_reg(LSM303AGR_ACC_INT1_CFG, 0x2A); |
Huot87 | 32:f211b8c28273 | 478 | printf("Enable X and Y Axes High Interrupt Events. \r\n"); |
Huot87 | 32:f211b8c28273 | 479 | |
Huot87 | 32:f211b8c28273 | 480 | accelPin.rise(&accelRunning); |
Huot87 | 32:f211b8c28273 | 481 | |
Huot87 | 32:f211b8c28273 | 482 | // ATTEMPT TO READ SOURCE DATA |
Huot87 | 32:f211b8c28273 | 483 | uint8_t data = 0; |
Huot87 | 32:f211b8c28273 | 484 | accel.read_reg(LSM303AGR_ACC_INT1_SOURCE, &data); |
Huot87 | 32:f211b8c28273 | 485 | printf("DATA = %02x \r\n", data); |
Huot87 | 32:f211b8c28273 | 486 | |
Huot87 | 32:f211b8c28273 | 487 | |
Huot87 | 32:f211b8c28273 | 488 | // uint8_t data = 0; |
Huot87 | 32:f211b8c28273 | 489 | // |
Huot87 | 32:f211b8c28273 | 490 | // accel.read_reg(, &data); |
Huot87 | 32:f211b8c28273 | 491 | // printf("DATA = %02x \r\n", data); |
Huot87 | 32:f211b8c28273 | 492 | // |
Huot87 | 32:f211b8c28273 | 493 | // uint8_t srcData; |
Huot87 | 32:f211b8c28273 | 494 | // accel.read_reg(LSM303AGR_ACC_INT1_SOURCE, &srcData); |
Huot87 | 32:f211b8c28273 | 495 | // printf("SRC DATA = %02x \r\n", srcData); |
Huot87 | 32:f211b8c28273 | 496 | |
Huot87 | 32:f211b8c28273 | 497 | |
Huot87 | 32:f211b8c28273 | 498 | |
Huot87 | 32:f211b8c28273 | 499 | } |
Huot87 | 32:f211b8c28273 | 500 | |
mbed_official | 0:7037ed05f54f | 501 | // EOF |