acsip s76g

Dependencies:   TinyGPSPlus

Committer:
frank_dunn1
Date:
Wed Jul 08 08:54:48 2020 +0000
Revision:
53:a3344ee53c2c
Parent:
52:922d8fdaae44
mods

Who changed what in which revision?

UserRevisionLine numberNew 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>
frank_dunn1 52:922d8fdaae44 18 #include "mbed.h"
mbed_official 0:7037ed05f54f 19 #include "lorawan/LoRaWANInterface.h"
mbed_official 0:7037ed05f54f 20 #include "lorawan/system/lorawan_data_structures.h"
mbed_official 0:7037ed05f54f 21 #include "events/EventQueue.h"
frank_dunn1 53:a3344ee53c2c 22 #include "TinyGPSPlus.h"
mbed_official 0:7037ed05f54f 23 // Application helpers
mbed_official 0:7037ed05f54f 24 #include "trace_helper.h"
mbed_official 0:7037ed05f54f 25 #include "lora_radio_helper.h"
mbed_official 0:7037ed05f54f 26
mbed_official 0:7037ed05f54f 27 using namespace events;
mbed_official 0:7037ed05f54f 28
mbed_official 12:5015dfead3f2 29 // Max payload size can be LORAMAC_PHY_MAXPAYLOAD.
mbed_official 12:5015dfead3f2 30 // This example only communicates with much shorter messages (<30 bytes).
mbed_official 12:5015dfead3f2 31 // If longer messages are used, these buffers must be changed accordingly.
mbed_official 12:5015dfead3f2 32 uint8_t tx_buffer[30];
mbed_official 12:5015dfead3f2 33 uint8_t rx_buffer[30];
frank_dunn1 53:a3344ee53c2c 34 char buf[128];
mbed_official 0:7037ed05f54f 35
frank_dunn1 53:a3344ee53c2c 36 char c;
mbed_official 0:7037ed05f54f 37 /*
mbed_official 0:7037ed05f54f 38 * Sets up an application dependent transmission timer in ms. Used only when Duty Cycling is off for testing
mbed_official 0:7037ed05f54f 39 */
mbed_official 0:7037ed05f54f 40 #define TX_TIMER 10000
mbed_official 0:7037ed05f54f 41
frank_dunn1 53:a3344ee53c2c 42
frank_dunn1 53:a3344ee53c2c 43
mbed_official 0:7037ed05f54f 44 /**
mbed_official 0:7037ed05f54f 45 * Maximum number of events for the event queue.
mbed_official 12:5015dfead3f2 46 * 10 is the safe number for the stack events, however, if application
mbed_official 0:7037ed05f54f 47 * also uses the queue for whatever purposes, this number should be increased.
mbed_official 0:7037ed05f54f 48 */
mbed_official 12:5015dfead3f2 49 #define MAX_NUMBER_OF_EVENTS 10
mbed_official 0:7037ed05f54f 50
mbed_official 0:7037ed05f54f 51 /**
mbed_official 0:7037ed05f54f 52 * Maximum number of retries for CONFIRMED messages before giving up
mbed_official 0:7037ed05f54f 53 */
mbed_official 0:7037ed05f54f 54 #define CONFIRMED_MSG_RETRY_COUNTER 3
mbed_official 0:7037ed05f54f 55
frank_dunn1 53:a3344ee53c2c 56 #define DATABUFFER_SIZE 128
frank_dunn1 53:a3344ee53c2c 57
frank_dunn1 53:a3344ee53c2c 58
frank_dunn1 53:a3344ee53c2c 59
frank_dunn1 53:a3344ee53c2c 60
frank_dunn1 53:a3344ee53c2c 61 Serial gpsPort(PC_10,PC_11,115200);
frank_dunn1 52:922d8fdaae44 62 Serial pc(PA_9,PA_10);
frank_dunn1 53:a3344ee53c2c 63 TinyGPSPlus gps;
frank_dunn1 53:a3344ee53c2c 64 DigitalOut GPS_LEVEL_SHIFTER_EN(PC_6);
frank_dunn1 53:a3344ee53c2c 65 DigitalOut GPS_RESET(PB_2);
mbed_official 0:7037ed05f54f 66 /**
mbed_official 0:7037ed05f54f 67 * This event queue is the global event queue for both the
mbed_official 0:7037ed05f54f 68 * application and stack. To conserve memory, the stack is designed to run
mbed_official 0:7037ed05f54f 69 * in the same thread as the application and the application is responsible for
mbed_official 0:7037ed05f54f 70 * providing an event queue to the stack that will be used for ISR deferment as
mbed_official 0:7037ed05f54f 71 * well as application information event queuing.
mbed_official 0:7037ed05f54f 72 */
mbed_official 46:a124538e2891 73 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
mbed_official 0:7037ed05f54f 74
mbed_official 0:7037ed05f54f 75 /**
mbed_official 0:7037ed05f54f 76 * Event handler.
mbed_official 0:7037ed05f54f 77 *
mbed_official 0:7037ed05f54f 78 * This will be passed to the LoRaWAN stack to queue events for the
mbed_official 0:7037ed05f54f 79 * application which in turn drive the application.
mbed_official 0:7037ed05f54f 80 */
mbed_official 0:7037ed05f54f 81 static void lora_event_handler(lorawan_event_t event);
mbed_official 0:7037ed05f54f 82
mbed_official 0:7037ed05f54f 83 /**
mbed_official 46:a124538e2891 84 * Constructing Mbed LoRaWANInterface and passing it the radio object from lora_radio_helper.
mbed_official 0:7037ed05f54f 85 */
mbed_official 2:dc95ac6d6d4e 86 static LoRaWANInterface lorawan(radio);
mbed_official 0:7037ed05f54f 87
mbed_official 0:7037ed05f54f 88 /**
mbed_official 0:7037ed05f54f 89 * Application specific callbacks
mbed_official 0:7037ed05f54f 90 */
mbed_official 0:7037ed05f54f 91 static lorawan_app_callbacks_t callbacks;
mbed_official 0:7037ed05f54f 92
frank_dunn1 53:a3344ee53c2c 93 void gpsSetup()
frank_dunn1 53:a3344ee53c2c 94 {
frank_dunn1 53:a3344ee53c2c 95
frank_dunn1 53:a3344ee53c2c 96 GPS_LEVEL_SHIFTER_EN=1;
frank_dunn1 53:a3344ee53c2c 97 wait(0.2);
frank_dunn1 53:a3344ee53c2c 98 GPS_RESET=0;
frank_dunn1 53:a3344ee53c2c 99
frank_dunn1 53:a3344ee53c2c 100 wait(1);
frank_dunn1 53:a3344ee53c2c 101
frank_dunn1 53:a3344ee53c2c 102 GPS_RESET=1;
frank_dunn1 53:a3344ee53c2c 103
frank_dunn1 53:a3344ee53c2c 104 // wait(1);
frank_dunn1 53:a3344ee53c2c 105 //gpsPort.printf("@GSR\r\n");//"@GNS 0x3\r\n"
frank_dunn1 53:a3344ee53c2c 106 //wait(.25);
frank_dunn1 53:a3344ee53c2c 107 //gpsPort.printf("@GNS 0x3\r\n");
frank_dunn1 53:a3344ee53c2c 108 //wait(.25);
frank_dunn1 53:a3344ee53c2c 109 //gpsPort.printf("@BSSL 0x25\r\n");
frank_dunn1 53:a3344ee53c2c 110 //wait(.25);
frank_dunn1 53:a3344ee53c2c 111 //gpsPort.printf("@GCD\r\n");
frank_dunn1 53:a3344ee53c2c 112 //wait(.25);
frank_dunn1 53:a3344ee53c2c 113 }
frank_dunn1 53:a3344ee53c2c 114
frank_dunn1 53:a3344ee53c2c 115 void getGpsData()
frank_dunn1 53:a3344ee53c2c 116 {
frank_dunn1 53:a3344ee53c2c 117
frank_dunn1 53:a3344ee53c2c 118 c= gpsPort.getc();
frank_dunn1 53:a3344ee53c2c 119
frank_dunn1 53:a3344ee53c2c 120 //pc.putc(c);
frank_dunn1 53:a3344ee53c2c 121 switch (c) {
frank_dunn1 53:a3344ee53c2c 122 case '\n':
frank_dunn1 53:a3344ee53c2c 123 if (gps.satellites.isValid() && gps.satellites.value() > 3 && gps.hdop.hdop() > 0) {
frank_dunn1 53:a3344ee53c2c 124 snprintf(buf, 128, "{\"lat\":%lf,\"lng\":%lf}", gps.location.lat(), gps.location.lng());
frank_dunn1 53:a3344ee53c2c 125 } else {
frank_dunn1 53:a3344ee53c2c 126 snprintf(buf, 128, "Satellites: %lu, time: %04d-%02d-%02dT%02d:%02d:%02d.%02d",
frank_dunn1 53:a3344ee53c2c 127 gps.satellites.value(), gps.date.year(), gps.date.month(), gps.date.day(),
frank_dunn1 53:a3344ee53c2c 128 gps.time.hour(), gps.time.minute(), gps.time.second(), gps.time.centisecond());
frank_dunn1 53:a3344ee53c2c 129 }
frank_dunn1 53:a3344ee53c2c 130 pc.printf("%s\r\n", buf);
frank_dunn1 53:a3344ee53c2c 131 break;
frank_dunn1 53:a3344ee53c2c 132 default:
frank_dunn1 53:a3344ee53c2c 133 gps.encode(c);
frank_dunn1 53:a3344ee53c2c 134 break;
frank_dunn1 53:a3344ee53c2c 135 }
frank_dunn1 53:a3344ee53c2c 136
frank_dunn1 53:a3344ee53c2c 137 ev_queue.call(getGpsData);
frank_dunn1 53:a3344ee53c2c 138 }
frank_dunn1 53:a3344ee53c2c 139
frank_dunn1 53:a3344ee53c2c 140
mbed_official 0:7037ed05f54f 141 /**
mbed_official 0:7037ed05f54f 142 * Entry point for application
mbed_official 0:7037ed05f54f 143 */
mbed_official 46:a124538e2891 144 int main(void)
frank_dunn1 53:a3344ee53c2c 145
frank_dunn1 53:a3344ee53c2c 146 {
frank_dunn1 53:a3344ee53c2c 147 //getGpsData();
frank_dunn1 53:a3344ee53c2c 148 gpsSetup();
frank_dunn1 53:a3344ee53c2c 149
frank_dunn1 53:a3344ee53c2c 150
frank_dunn1 53:a3344ee53c2c 151 pc.printf("hello");
mbed_official 0:7037ed05f54f 152 // setup tracing
mbed_official 0:7037ed05f54f 153 setup_trace();
mbed_official 0:7037ed05f54f 154
mbed_official 0:7037ed05f54f 155 // stores the status of a call to LoRaWAN protocol
mbed_official 0:7037ed05f54f 156 lorawan_status_t retcode;
mbed_official 0:7037ed05f54f 157
mbed_official 0:7037ed05f54f 158 // Initialize LoRaWAN stack
mbed_official 2:dc95ac6d6d4e 159 if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
frank_dunn1 52:922d8fdaae44 160 pc.printf("\r\n LoRa initialization failed! \r\n");
mbed_official 0:7037ed05f54f 161 return -1;
mbed_official 0:7037ed05f54f 162 }
mbed_official 0:7037ed05f54f 163
frank_dunn1 52:922d8fdaae44 164 pc.printf("\r\n Mbed LoRaWANStack initialized \r\n");
mbed_official 0:7037ed05f54f 165
mbed_official 0:7037ed05f54f 166 // prepare application callbacks
mbed_official 0:7037ed05f54f 167 callbacks.events = mbed::callback(lora_event_handler);
mbed_official 2:dc95ac6d6d4e 168 lorawan.add_app_callbacks(&callbacks);
mbed_official 0:7037ed05f54f 169
mbed_official 0:7037ed05f54f 170 // Set number of retries in case of CONFIRMED messages
mbed_official 2:dc95ac6d6d4e 171 if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
mbed_official 46:a124538e2891 172 != LORAWAN_STATUS_OK) {
frank_dunn1 52:922d8fdaae44 173 pc.printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
mbed_official 0:7037ed05f54f 174 return -1;
mbed_official 0:7037ed05f54f 175 }
mbed_official 0:7037ed05f54f 176
frank_dunn1 53:a3344ee53c2c 177 pc.printf("\r\n CONFIRMED message retries : %d \r\n",
frank_dunn1 53:a3344ee53c2c 178 CONFIRMED_MSG_RETRY_COUNTER);
mbed_official 0:7037ed05f54f 179
mbed_official 0:7037ed05f54f 180 // Enable adaptive data rate
mbed_official 2:dc95ac6d6d4e 181 if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
frank_dunn1 52:922d8fdaae44 182 pc.printf("\r\n enable_adaptive_datarate failed! \r\n");
mbed_official 0:7037ed05f54f 183 return -1;
mbed_official 0:7037ed05f54f 184 }
mbed_official 0:7037ed05f54f 185
frank_dunn1 52:922d8fdaae44 186 pc.printf("\r\n Adaptive data rate (ADR) - Enabled \r\n");
mbed_official 0:7037ed05f54f 187
mbed_official 2:dc95ac6d6d4e 188 retcode = lorawan.connect();
mbed_official 0:7037ed05f54f 189
mbed_official 0:7037ed05f54f 190 if (retcode == LORAWAN_STATUS_OK ||
mbed_official 46:a124538e2891 191 retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
mbed_official 0:7037ed05f54f 192 } else {
frank_dunn1 52:922d8fdaae44 193 pc.printf("\r\n Connection error, code = %d \r\n", retcode);
mbed_official 0:7037ed05f54f 194 return -1;
mbed_official 0:7037ed05f54f 195 }
mbed_official 0:7037ed05f54f 196
frank_dunn1 53:a3344ee53c2c 197 pc.printf("\r\n Connection - In Progress ...\r\n");
frank_dunn1 53:a3344ee53c2c 198
frank_dunn1 53:a3344ee53c2c 199
frank_dunn1 53:a3344ee53c2c 200 ev_queue.call(getGpsData);
mbed_official 0:7037ed05f54f 201 // make your event queue dispatching events forever
mbed_official 0:7037ed05f54f 202 ev_queue.dispatch_forever();
mbed_official 3:8c7198d1a2a1 203
mbed_official 3:8c7198d1a2a1 204 return 0;
mbed_official 0:7037ed05f54f 205 }
mbed_official 0:7037ed05f54f 206
mbed_official 0:7037ed05f54f 207 /**
mbed_official 0:7037ed05f54f 208 * Sends a message to the Network Server
mbed_official 0:7037ed05f54f 209 */
mbed_official 0:7037ed05f54f 210 static void send_message()
mbed_official 0:7037ed05f54f 211 {
mbed_official 0:7037ed05f54f 212 uint16_t packet_len;
mbed_official 0:7037ed05f54f 213 int16_t retcode;
frank_dunn1 53:a3344ee53c2c 214 pc.printf("\n\rHello world");
mbed_official 0:7037ed05f54f 215
marcozecchini 50:6e615eea1e6f 216 packet_len = sprintf((char *) tx_buffer, "Hello world");
mbed_official 0:7037ed05f54f 217
mbed_official 2:dc95ac6d6d4e 218 retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
mbed_official 47:b6d132f1079f 219 MSG_UNCONFIRMED_FLAG);
mbed_official 0:7037ed05f54f 220
mbed_official 0:7037ed05f54f 221 if (retcode < 0) {
frank_dunn1 52:922d8fdaae44 222 retcode == LORAWAN_STATUS_WOULD_BLOCK ? pc.printf("send - WOULD BLOCK\r\n")
frank_dunn1 52:922d8fdaae44 223 : pc.printf("\r\n send() - Error code %d \r\n", retcode);
mbed_official 26:f07f5febf97f 224
mbed_official 26:f07f5febf97f 225 if (retcode == LORAWAN_STATUS_WOULD_BLOCK) {
mbed_official 26:f07f5febf97f 226 //retry in 3 seconds
mbed_official 26:f07f5febf97f 227 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
mbed_official 26:f07f5febf97f 228 ev_queue.call_in(3000, send_message);
mbed_official 26:f07f5febf97f 229 }
mbed_official 26:f07f5febf97f 230 }
mbed_official 0:7037ed05f54f 231 return;
mbed_official 0:7037ed05f54f 232 }
mbed_official 0:7037ed05f54f 233
frank_dunn1 52:922d8fdaae44 234 pc.printf("\r\n %d bytes scheduled for transmission \r\n", retcode);
mbed_official 12:5015dfead3f2 235 memset(tx_buffer, 0, sizeof(tx_buffer));
frank_dunn1 53:a3344ee53c2c 236
mbed_official 0:7037ed05f54f 237 }
mbed_official 0:7037ed05f54f 238
mbed_official 0:7037ed05f54f 239 /**
mbed_official 0:7037ed05f54f 240 * Event handler
mbed_official 0:7037ed05f54f 241 */
mbed_official 0:7037ed05f54f 242 static void lora_event_handler(lorawan_event_t event)
mbed_official 0:7037ed05f54f 243 {
mbed_official 0:7037ed05f54f 244 switch (event) {
mbed_official 0:7037ed05f54f 245 case CONNECTED:
frank_dunn1 53:a3344ee53c2c 246 pc.printf("\r\n Connection - Successful \r\n");
mbed_official 0:7037ed05f54f 247 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
mbed_official 0:7037ed05f54f 248 send_message();
mbed_official 0:7037ed05f54f 249 } else {
mbed_official 0:7037ed05f54f 250 ev_queue.call_every(TX_TIMER, send_message);
mbed_official 0:7037ed05f54f 251 }
mbed_official 0:7037ed05f54f 252
mbed_official 0:7037ed05f54f 253 break;
mbed_official 0:7037ed05f54f 254 case DISCONNECTED:
mbed_official 0:7037ed05f54f 255 ev_queue.break_dispatch();
frank_dunn1 53:a3344ee53c2c 256 pc.printf("\r\n Disconnected Successfully \r\n");
mbed_official 0:7037ed05f54f 257 break;
mbed_official 0:7037ed05f54f 258 case TX_DONE:
frank_dunn1 53:a3344ee53c2c 259 pc.printf("\r\n Message Sent to Network Server \r\n");
mbed_official 0:7037ed05f54f 260 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
mbed_official 0:7037ed05f54f 261 send_message();
frank_dunn1 53:a3344ee53c2c 262
mbed_official 0:7037ed05f54f 263 }
mbed_official 0:7037ed05f54f 264 break;
mbed_official 0:7037ed05f54f 265 case TX_TIMEOUT:
mbed_official 0:7037ed05f54f 266 case TX_ERROR:
mbed_official 0:7037ed05f54f 267 case TX_CRYPTO_ERROR:
mbed_official 0:7037ed05f54f 268 case TX_SCHEDULING_ERROR:
frank_dunn1 53:a3344ee53c2c 269 pc.printf("\r\n Transmission Error - EventCode = %d \r\n", event);
mbed_official 0:7037ed05f54f 270 // try again
mbed_official 0:7037ed05f54f 271 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
mbed_official 0:7037ed05f54f 272 send_message();
mbed_official 0:7037ed05f54f 273 }
mbed_official 0:7037ed05f54f 274 break;
mbed_official 0:7037ed05f54f 275 case RX_DONE:
frank_dunn1 53:a3344ee53c2c 276 pc.printf("\r\n Received message from Network Server \r\n");
mbed_official 0:7037ed05f54f 277 break;
mbed_official 0:7037ed05f54f 278 case RX_TIMEOUT:
mbed_official 0:7037ed05f54f 279 case RX_ERROR:
frank_dunn1 53:a3344ee53c2c 280 pc.printf("\r\n Error in reception - Code = %d \r\n", event);
mbed_official 0:7037ed05f54f 281 break;
mbed_official 0:7037ed05f54f 282 case JOIN_FAILURE:
frank_dunn1 53:a3344ee53c2c 283 pc.printf("\r\n OTAA Failed - Check Keys \r\n");
mbed_official 0:7037ed05f54f 284 break;
mbed_official 26:f07f5febf97f 285 case UPLINK_REQUIRED:
frank_dunn1 53:a3344ee53c2c 286 pc.printf("\r\n Uplink required by NS \r\n");
mbed_official 26:f07f5febf97f 287 if (MBED_CONF_LORA_DUTY_CYCLE_ON) {
mbed_official 26:f07f5febf97f 288 send_message();
mbed_official 26:f07f5febf97f 289 }
mbed_official 26:f07f5febf97f 290 break;
mbed_official 0:7037ed05f54f 291 default:
mbed_official 0:7037ed05f54f 292 MBED_ASSERT("Unknown Event");
mbed_official 0:7037ed05f54f 293 }
mbed_official 0:7037ed05f54f 294 }
mbed_official 0:7037ed05f54f 295
mbed_official 0:7037ed05f54f 296 // EOF