This is an example application based on Mbed-OS LoRaWAN protocol APIs. The Mbed-OS LoRaWAN stack implementation is compliant with LoRaWAN v1.0.2 specification.

Dependencies:   Lorawan_Version_0_1

Dependents:   Lorawan_Version_0_1

Committer:
jacktractive
Date:
Tue Jan 21 13:20:59 2020 +0000
Revision:
70:65b2f1cc2859
Parent:
69:316fee01f5d9
Child:
71:ca2425c0a864
Lora und GPS test mit Testdaten

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 */
jacktractive 66:a11c79073f1f 17 #include <stdio.h>
jacktractive 66:a11c79073f1f 18
jacktractive 66:a11c79073f1f 19 #include "Watchdog.h"
jacktractive 66:a11c79073f1f 20 #include "events/EventQueue.h"
jacktractive 66:a11c79073f1f 21
jacktractive 66:a11c79073f1f 22 // Application helpers
mbed_official 0:7037ed05f54f 23 #include "trace_helper.h"
jacktractive 66:a11c79073f1f 24
jacktractive 66:a11c79073f1f 25 #include <mbed.h>
jacktractive 65:3061ea235a0c 26 #include "mbed_stats.h"
jacktractive 69:316fee01f5d9 27 #include "Light.h"
jacktractive 69:316fee01f5d9 28 #include "GPS.h"
jacktractive 69:316fee01f5d9 29 #include "Lora.h"
jacktractive 63:2bfceda4c30c 30
jacktractive 69:316fee01f5d9 31
jacktractive 69:316fee01f5d9 32 #define MAX_NUMBER_OF_EVENTS 20
jacktractive 69:316fee01f5d9 33 static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);
jacktractive 69:316fee01f5d9 34
jacktractive 66:a11c79073f1f 35
jacktractive 68:41fff9c3fb4f 36 DigitalIn USERButton(PB_15);
jacktractive 66:a11c79073f1f 37
jacktractive 64:85fa08519e1e 38 bool GPS_activ,LORA_activ,AtHome;
jacktractive 66:a11c79073f1f 39
jacktractive 66:a11c79073f1f 40
jacktractive 68:41fff9c3fb4f 41 uint32_t TickCounter;
jacktractive 69:316fee01f5d9 42 uint32_t TickTime=1000;
jacktractive 69:316fee01f5d9 43 uint32_t LoraTickCounter;
jacktractive 69:316fee01f5d9 44 uint32_t LoraTickTime=15000;
jacktractive 69:316fee01f5d9 45
jacktractive 63:2bfceda4c30c 46 bool IsLoading;
jacktractive 63:2bfceda4c30c 47 AnalogIn LadeSpannung(PB_0); // 1/11 der gleichgerichteten Spannung am Dynamo
jacktractive 70:65b2f1cc2859 48
jacktractive 70:65b2f1cc2859 49 //Light
jacktractive 70:65b2f1cc2859 50 DigitalOut LichtAus(PC_12);
jacktractive 70:65b2f1cc2859 51 DigitalOut LichtHell(PC_9);
jacktractive 70:65b2f1cc2859 52 Light light(&ev_queue,&LichtAus,&LichtHell);
jacktractive 70:65b2f1cc2859 53
jacktractive 70:65b2f1cc2859 54 //GPS
jacktractive 70:65b2f1cc2859 55 DigitalOut GPSdisable(PC_6);
jacktractive 70:65b2f1cc2859 56 GPS GPS_modul(PC_4, PC_5, 9600,&GPSdisable);
jacktractive 70:65b2f1cc2859 57 uint32_t TimeWithoutMoving;
jacktractive 66:a11c79073f1f 58
jacktractive 69:316fee01f5d9 59 Lora lora(&ev_queue, &light, &GPS_modul);
mbed_official 0:7037ed05f54f 60
mbed_official 0:7037ed05f54f 61
jacktractive 66:a11c79073f1f 62 /**
jacktractive 66:a11c79073f1f 63 * Event handler.
jacktractive 66:a11c79073f1f 64 *
jacktractive 66:a11c79073f1f 65 * This will be passed to the LoRaWAN stack to queue events for the
jacktractive 66:a11c79073f1f 66 * application which in turn drive the application.
jacktractive 66:a11c79073f1f 67 */
jacktractive 63:2bfceda4c30c 68
jacktractive 63:2bfceda4c30c 69 static void LifeTicker()
jacktractive 63:2bfceda4c30c 70 {
jacktractive 68:41fff9c3fb4f 71 //Watchdog
jacktractive 66:a11c79073f1f 72 Watchdog &watchdog = Watchdog::get_instance();
jacktractive 66:a11c79073f1f 73 Watchdog::get_instance().kick(); // kick the Watchdog before the timeout
jacktractive 66:a11c79073f1f 74
jacktractive 68:41fff9c3fb4f 75 //Lifetick
jacktractive 68:41fff9c3fb4f 76 TickCounter=TickCounter+1;
jacktractive 69:316fee01f5d9 77 printf("\n[LiveTick] --- [%i] ", TickCounter);
jacktractive 69:316fee01f5d9 78
jacktractive 69:316fee01f5d9 79 //Dynamo - Fahrrad in Bewegung
jacktractive 70:65b2f1cc2859 80 if(LadeSpannung.read()*3.3f*11 > 5) {
jacktractive 69:316fee01f5d9 81 IsLoading = 1;
jacktractive 69:316fee01f5d9 82 TimeWithoutMoving =0 ;
jacktractive 69:316fee01f5d9 83 printf("[SYSTEM] Akku laden mit: %fV", LadeSpannung.read()*3.3f*11);
jacktractive 69:316fee01f5d9 84 }else {
jacktractive 69:316fee01f5d9 85 IsLoading = 0;
jacktractive 69:316fee01f5d9 86 TimeWithoutMoving=TimeWithoutMoving+TickTime;
jacktractive 69:316fee01f5d9 87 printf("[SYSTEM] Fahrrad steht seit: %is", TimeWithoutMoving/1000);
jacktractive 69:316fee01f5d9 88 }
jacktractive 69:316fee01f5d9 89 light.adjust(IsLoading,TimeWithoutMoving);
jacktractive 69:316fee01f5d9 90
jacktractive 70:65b2f1cc2859 91 if (!USERButton){
jacktractive 70:65b2f1cc2859 92 light.Blinken_ein(4000);
jacktractive 70:65b2f1cc2859 93 GPS_modul.set_manual_tracking(true);
jacktractive 70:65b2f1cc2859 94 };
jacktractive 69:316fee01f5d9 95 }
jacktractive 69:316fee01f5d9 96
jacktractive 69:316fee01f5d9 97
jacktractive 69:316fee01f5d9 98 static void LoraTicker()
jacktractive 69:316fee01f5d9 99 {
jacktractive 69:316fee01f5d9 100
jacktractive 69:316fee01f5d9 101
jacktractive 69:316fee01f5d9 102 //Lora Lifetick
jacktractive 69:316fee01f5d9 103 LoraTickCounter=LoraTickCounter+1;
jacktractive 69:316fee01f5d9 104 printf("\n\n[LoraTick] --- [%i]", LoraTickCounter);
jacktractive 66:a11c79073f1f 105
jacktractive 68:41fff9c3fb4f 106 //Sleep Statistik
jacktractive 68:41fff9c3fb4f 107 mbed_stats_cpu_t stats;
jacktractive 68:41fff9c3fb4f 108 mbed_stats_cpu_get(&stats);
jacktractive 69:316fee01f5d9 109 printf("\n[SYSTEM] Uptime: %llu Idle: %llu Sleep: %llu Deep: %llu", stats.uptime / 1000 , stats.idle_time / 1000, stats.sleep_time / 1000,stats.deep_sleep_time / 1000);
jacktractive 66:a11c79073f1f 110
jacktractive 68:41fff9c3fb4f 111 //GPS
jacktractive 70:65b2f1cc2859 112 GPS_modul.idle(TimeWithoutMoving>300000);
jacktractive 70:65b2f1cc2859 113 GPS_modul.GPS_aktiv();
jacktractive 68:41fff9c3fb4f 114
jacktractive 70:65b2f1cc2859 115 //LORA
jacktractive 70:65b2f1cc2859 116 lora.send_Position_to_Lora(0x01, GPS_modul.time,GPS_modul.longitude,GPS_modul.latitude);
jacktractive 63:2bfceda4c30c 117 }
jacktractive 63:2bfceda4c30c 118
jacktractive 66:a11c79073f1f 119
jacktractive 66:a11c79073f1f 120
jacktractive 66:a11c79073f1f 121 /**
jacktractive 66:a11c79073f1f 122 * Entry point for application
jacktractive 66:a11c79073f1f 123 */
mbed_official 46:a124538e2891 124 int main(void)
jacktractive 64:85fa08519e1e 125 {
jacktractive 66:a11c79073f1f 126 Watchdog &watchdog = Watchdog::get_instance();
jacktractive 66:a11c79073f1f 127
jacktractive 66:a11c79073f1f 128 watchdog.start(30000);
jacktractive 66:a11c79073f1f 129 uint32_t watchdog_timeout = watchdog.get_timeout();
jacktractive 67:7bb75ed97500 130 printf("Watchdog initialized to %iu ms.\r\n", watchdog_timeout);
jacktractive 68:41fff9c3fb4f 131
jacktractive 68:41fff9c3fb4f 132 ev_queue.call_every(TickTime,LifeTicker);
jacktractive 69:316fee01f5d9 133 ev_queue.call_every(LoraTickTime,LoraTicker);
jacktractive 67:7bb75ed97500 134
jacktractive 67:7bb75ed97500 135
mbed_official 0:7037ed05f54f 136 // setup tracing
jacktractive 70:65b2f1cc2859 137 setup_trace();
jacktractive 61:2b30d8e75fe7 138
jacktractive 69:316fee01f5d9 139 lora.Lora_init();
jacktractive 69:316fee01f5d9 140
jacktractive 68:41fff9c3fb4f 141 // make your event queue dispatching events forever
jacktractive 68:41fff9c3fb4f 142 ev_queue.dispatch_forever();
jacktractive 68:41fff9c3fb4f 143
jacktractive 68:41fff9c3fb4f 144 return 0;
jacktractive 68:41fff9c3fb4f 145 }
jacktractive 68:41fff9c3fb4f 146
jacktractive 66:a11c79073f1f 147 // EOF