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:
Sun Jan 19 15:53:57 2020 +0000
Revision:
69:316fee01f5d9
Parent:
68:41fff9c3fb4f
Child:
70:65b2f1cc2859
Objectorientiert

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 67:7bb75ed97500 41 DigitalOut GPSdisable(PC_6);
jacktractive 68:41fff9c3fb4f 42 uint32_t TickCounter;
jacktractive 69:316fee01f5d9 43 uint32_t TickTime=1000;
jacktractive 69:316fee01f5d9 44 uint32_t LoraTickCounter;
jacktractive 69:316fee01f5d9 45 uint32_t LoraTickTime=15000;
jacktractive 69:316fee01f5d9 46
jacktractive 69:316fee01f5d9 47
jacktractive 69:316fee01f5d9 48
jacktractive 68:41fff9c3fb4f 49
jacktractive 68:41fff9c3fb4f 50 uint32_t TimeWithoutMoving;
jacktractive 66:a11c79073f1f 51
jacktractive 63:2bfceda4c30c 52 bool IsLoading;
jacktractive 63:2bfceda4c30c 53 AnalogIn LadeSpannung(PB_0); // 1/11 der gleichgerichteten Spannung am Dynamo
jacktractive 69:316fee01f5d9 54 static Light light(&ev_queue);
jacktractive 69:316fee01f5d9 55 static GPS GPS_modul(PC_4, PC_5, 9600);
jacktractive 66:a11c79073f1f 56
jacktractive 69:316fee01f5d9 57 Lora lora(&ev_queue, &light, &GPS_modul);
mbed_official 0:7037ed05f54f 58
mbed_official 0:7037ed05f54f 59
jacktractive 66:a11c79073f1f 60 /**
jacktractive 66:a11c79073f1f 61 * Event handler.
jacktractive 66:a11c79073f1f 62 *
jacktractive 66:a11c79073f1f 63 * This will be passed to the LoRaWAN stack to queue events for the
jacktractive 66:a11c79073f1f 64 * application which in turn drive the application.
jacktractive 66:a11c79073f1f 65 */
jacktractive 63:2bfceda4c30c 66
jacktractive 63:2bfceda4c30c 67 static void LifeTicker()
jacktractive 63:2bfceda4c30c 68 {
jacktractive 68:41fff9c3fb4f 69 //Watchdog
jacktractive 66:a11c79073f1f 70 Watchdog &watchdog = Watchdog::get_instance();
jacktractive 66:a11c79073f1f 71 Watchdog::get_instance().kick(); // kick the Watchdog before the timeout
jacktractive 66:a11c79073f1f 72
jacktractive 68:41fff9c3fb4f 73 //Lifetick
jacktractive 68:41fff9c3fb4f 74 TickCounter=TickCounter+1;
jacktractive 69:316fee01f5d9 75 printf("\n[LiveTick] --- [%i] ", TickCounter);
jacktractive 69:316fee01f5d9 76
jacktractive 69:316fee01f5d9 77 //Dynamo - Fahrrad in Bewegung
jacktractive 69:316fee01f5d9 78 if(LadeSpannung.read() > 0.05f) {
jacktractive 69:316fee01f5d9 79 IsLoading = 1;
jacktractive 69:316fee01f5d9 80 TimeWithoutMoving =0 ;
jacktractive 69:316fee01f5d9 81 printf("[SYSTEM] Akku laden mit: %fV", LadeSpannung.read()*3.3f*11);
jacktractive 69:316fee01f5d9 82 }else {
jacktractive 69:316fee01f5d9 83 IsLoading = 0;
jacktractive 69:316fee01f5d9 84 TimeWithoutMoving=TimeWithoutMoving+TickTime;
jacktractive 69:316fee01f5d9 85 printf("[SYSTEM] Fahrrad steht seit: %is", TimeWithoutMoving/1000);
jacktractive 69:316fee01f5d9 86 }
jacktractive 69:316fee01f5d9 87 light.adjust(IsLoading,TimeWithoutMoving);
jacktractive 69:316fee01f5d9 88
jacktractive 69:316fee01f5d9 89 if (!USERButton){light.Blinken_ein(10000);};
jacktractive 69:316fee01f5d9 90 }
jacktractive 69:316fee01f5d9 91
jacktractive 69:316fee01f5d9 92
jacktractive 69:316fee01f5d9 93 static void LoraTicker()
jacktractive 69:316fee01f5d9 94 {
jacktractive 69:316fee01f5d9 95
jacktractive 69:316fee01f5d9 96
jacktractive 69:316fee01f5d9 97 //Lora Lifetick
jacktractive 69:316fee01f5d9 98 LoraTickCounter=LoraTickCounter+1;
jacktractive 69:316fee01f5d9 99 printf("\n\n[LoraTick] --- [%i]", LoraTickCounter);
jacktractive 66:a11c79073f1f 100
jacktractive 68:41fff9c3fb4f 101 //Sleep Statistik
jacktractive 68:41fff9c3fb4f 102 mbed_stats_cpu_t stats;
jacktractive 68:41fff9c3fb4f 103 mbed_stats_cpu_get(&stats);
jacktractive 69:316fee01f5d9 104 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 105
jacktractive 68:41fff9c3fb4f 106
jacktractive 68:41fff9c3fb4f 107 //GPS
jacktractive 69:316fee01f5d9 108 GPS_modul.idle(TimeWithoutMoving<300000);
jacktractive 69:316fee01f5d9 109 if (!GPS_modul.is_idle){
jacktractive 69:316fee01f5d9 110 GPS_modul.GPS_aktiv();
jacktractive 68:41fff9c3fb4f 111 }
jacktractive 68:41fff9c3fb4f 112 else{
jacktractive 69:316fee01f5d9 113 printf("\n[GPS] Idle - Energie sparen");
jacktractive 68:41fff9c3fb4f 114 }
jacktractive 68:41fff9c3fb4f 115
jacktractive 68:41fff9c3fb4f 116 //LORA
jacktractive 69:316fee01f5d9 117
jacktractive 69:316fee01f5d9 118 if(false){
jacktractive 68:41fff9c3fb4f 119 printf("\n[LORA] Connected\n");
jacktractive 69:316fee01f5d9 120 lora.send_Position_to_Lora(0x01, GPS_modul.time,GPS_modul.longitude,GPS_modul.latitude);
jacktractive 64:85fa08519e1e 121 }
jacktractive 64:85fa08519e1e 122 else
jacktractive 64:85fa08519e1e 123 {
jacktractive 68:41fff9c3fb4f 124 printf("\n[LORA] not Connected\n");
jacktractive 67:7bb75ed97500 125 }
jacktractive 63:2bfceda4c30c 126 }
jacktractive 63:2bfceda4c30c 127
jacktractive 66:a11c79073f1f 128
jacktractive 66:a11c79073f1f 129
jacktractive 66:a11c79073f1f 130 /**
jacktractive 66:a11c79073f1f 131 * Entry point for application
jacktractive 66:a11c79073f1f 132 */
mbed_official 46:a124538e2891 133 int main(void)
jacktractive 64:85fa08519e1e 134 {
jacktractive 66:a11c79073f1f 135 Watchdog &watchdog = Watchdog::get_instance();
jacktractive 66:a11c79073f1f 136
jacktractive 66:a11c79073f1f 137 watchdog.start(30000);
jacktractive 66:a11c79073f1f 138 uint32_t watchdog_timeout = watchdog.get_timeout();
jacktractive 67:7bb75ed97500 139 printf("Watchdog initialized to %iu ms.\r\n", watchdog_timeout);
jacktractive 68:41fff9c3fb4f 140
jacktractive 68:41fff9c3fb4f 141 ev_queue.call_every(TickTime,LifeTicker);
jacktractive 69:316fee01f5d9 142 ev_queue.call_every(LoraTickTime,LoraTicker);
jacktractive 67:7bb75ed97500 143
jacktractive 67:7bb75ed97500 144
mbed_official 0:7037ed05f54f 145 // setup tracing
jacktractive 69:316fee01f5d9 146 //setup_trace();
jacktractive 61:2b30d8e75fe7 147
jacktractive 69:316fee01f5d9 148 lora.Lora_init();
jacktractive 69:316fee01f5d9 149
jacktractive 68:41fff9c3fb4f 150 // make your event queue dispatching events forever
jacktractive 68:41fff9c3fb4f 151 ev_queue.dispatch_forever();
jacktractive 68:41fff9c3fb4f 152
jacktractive 68:41fff9c3fb4f 153 return 0;
jacktractive 68:41fff9c3fb4f 154 }
jacktractive 68:41fff9c3fb4f 155
jacktractive 66:a11c79073f1f 156 // EOF