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-19
Revision:
69:316fee01f5d9
Parent:
68:41fff9c3fb4f
Child:
70:65b2f1cc2859

File content as of revision 69:316fee01f5d9:

/**
 * 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;


DigitalOut GPSdisable(PC_6);
uint32_t TickCounter;
uint32_t TickTime=1000;
uint32_t LoraTickCounter;
uint32_t LoraTickTime=15000;

    
    

uint32_t TimeWithoutMoving;

bool IsLoading;
AnalogIn LadeSpannung(PB_0); // 1/11 der gleichgerichteten Spannung am Dynamo
static Light light(&ev_queue);
static GPS GPS_modul(PC_4, PC_5, 9600);

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() > 0.05f) {     
        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(10000);};           
}


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);
    if (!GPS_modul.is_idle){
        GPS_modul.GPS_aktiv();   
    }  
    else{    
        printf("\n[GPS] Idle - Energie sparen"); 
    }        
    
//LORA
    
    if(false){
        printf("\n[LORA] Connected\n");   
        lora.send_Position_to_Lora(0x01, GPS_modul.time,GPS_modul.longitude,GPS_modul.latitude);
    }
    else 
    {
    printf("\n[LORA] not Connected\n");    
    }                
}



/**
 * 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