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

main.cpp

Committer:
jacktractive
Date:
2020-01-21
Revision:
70:65b2f1cc2859
Parent:
69:316fee01f5d9
Child:
71:ca2425c0a864

File content as of revision 70:65b2f1cc2859:

/**
 * Copyright (c) 2017, Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <stdio.h>

#include "Watchdog.h"
#include "events/EventQueue.h"

// Application helpers
#include "trace_helper.h"

#include <mbed.h>
#include "mbed_stats.h"
#include "Light.h"
#include "GPS.h"
#include "Lora.h"


#define MAX_NUMBER_OF_EVENTS            20
static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS *EVENTS_EVENT_SIZE);


DigitalIn USERButton(PB_15);

bool GPS_activ,LORA_activ,AtHome;


uint32_t TickCounter;
uint32_t TickTime=1000;
uint32_t LoraTickCounter;
uint32_t LoraTickTime=15000;

bool IsLoading;
AnalogIn LadeSpannung(PB_0); // 1/11 der gleichgerichteten Spannung am Dynamo

//Light
DigitalOut LichtAus(PC_12);
DigitalOut LichtHell(PC_9);
Light light(&ev_queue,&LichtAus,&LichtHell);

//GPS
DigitalOut GPSdisable(PC_6);
GPS GPS_modul(PC_4, PC_5, 9600,&GPSdisable);
uint32_t TimeWithoutMoving;

Lora lora(&ev_queue, &light, &GPS_modul);


/**
 * Event handler.
 *
 * This will be passed to the LoRaWAN stack to queue events for the
 * application which in turn drive the application.
 */

static void LifeTicker()
{
//Watchdog
    Watchdog &watchdog = Watchdog::get_instance();    
    Watchdog::get_instance().kick(); // kick the Watchdog before the timeout
        
//Lifetick
    TickCounter=TickCounter+1;
    printf("\n[LiveTick] --- [%i] ", TickCounter);
    
//Dynamo - Fahrrad in Bewegung
   if(LadeSpannung.read()*3.3f*11 > 5) {     
        IsLoading = 1;
        TimeWithoutMoving =0 ;
        printf("[SYSTEM] Akku laden mit: %fV", LadeSpannung.read()*3.3f*11);
    }else {        
        IsLoading = 0;
        TimeWithoutMoving=TimeWithoutMoving+TickTime;
        printf("[SYSTEM] Fahrrad steht seit: %is", TimeWithoutMoving/1000);
     }        
    light.adjust(IsLoading,TimeWithoutMoving);
    
     if (!USERButton){
         light.Blinken_ein(4000);
         GPS_modul.set_manual_tracking(true); 
         };           
}


static void LoraTicker()
{
    
    
//Lora Lifetick
    LoraTickCounter=LoraTickCounter+1;
    printf("\n\n[LoraTick] --- [%i]", LoraTickCounter);
    
//Sleep Statistik
   mbed_stats_cpu_t stats;
   mbed_stats_cpu_get(&stats);
   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);
    
//GPS
    GPS_modul.idle(TimeWithoutMoving>300000);
    GPS_modul.GPS_aktiv();   
    
//LORA    
    lora.send_Position_to_Lora(0x01, GPS_modul.time,GPS_modul.longitude,GPS_modul.latitude);           
}



/**
 * Entry point for application
 */
int main(void)
{ 
    Watchdog &watchdog = Watchdog::get_instance();
    
    watchdog.start(30000);
    uint32_t watchdog_timeout = watchdog.get_timeout();
    printf("Watchdog initialized to %iu ms.\r\n", watchdog_timeout);
           
    ev_queue.call_every(TickTime,LifeTicker);
    ev_queue.call_every(LoraTickTime,LoraTicker);
 
    
    // setup tracing
    setup_trace();
    
    lora.Lora_init();
    
    // make your event queue dispatching events forever
    ev_queue.dispatch_forever();
    
    return 0;
}

// EOF