Felix HARERIMANA / Mbed 2 deprecated DISCO_F413ZH-trafficproject-SOUND

Dependencies:   mbed

main.cpp

Committer:
harerimana
Date:
2020-11-05
Revision:
8:3dd7a4231f6e
Parent:
7:667534ecd34e
Child:
9:6a6223dc937b

File content as of revision 8:3dd7a4231f6e:

#include "mbed.h" // Include the library of Mbed
#include "C12832.h"  // Include the library of the specific LCD
#include "mbed_trace.h"
#include "mbed_events.h"
#include "LoRaWANInterface.h"
#include "SX1276_LoRaRadio.h"
#define DEVICEID "FELIX_220014243"
#define YELLOWBLINKINGDURATION 10
#define REDLIGHTONDURATION 15
// The port we're sending and receiving on
#define MBED_CONF_LORA_APP_PORT     1

DigitalOut yellowled(LED4, 1); // Initializing the yellow light ON (same as yellowled = 1)
DigitalOut redled(LED1, 0); // Initializing the Red light to OFF (same as redled = 0)
InterruptIn pedestrianbutton(p5);

C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); // create an instance of LCD

int state = 0;  // State variable indicating that YELLOW LIGHT IS ON

char pedestrianAdvert[] = "2 COFFEES FOR THE PRICE OF 1. ONLY AT CAMELIA";
char carDriverAdvert[] = "RECEIVE 5%% REDUCTION ON YOUR FULL TANK ....";


// Device credentials, register device as OTAA in The Things Network and copy credentials here
static uint8_t DEV_EUI[] = { 0x26, 0x10, 0x20, 0x20, 0x11, 0x11, 0x20, 0x20 };
static uint8_t APP_EUI[] = { 0x70, 0xB3, 0xD5, 0x7E, 0xD0, 0x03, 0x74, 0x72 };
static uint8_t APP_KEY[] = { 0x75, 0x57, 0x56, 0x65, 0xB1, 0x8A, 0x17, 0x52, 0x1B, 0x2D, 0x45, 0xD9, 0xFC, 0x90, 0x9E, 0x05 };


// Peripherals (LoRa radio, temperature sensor and button)
SX1276_LoRaRadio radio(D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9, NC, NC, NC, NC, A4, NC, NC);
InterruptIn btn(BUTTON1);
// EventQueue is required to dispatch events around
static EventQueue ev_queue;
// Constructing Mbed LoRaWANInterface and passing it down the radio object.
static LoRaWANInterface lorawan(radio);
// Application specific callbacks
static lorawan_app_callbacks_t callbacks;
// LoRaWAN stack event handler
static void lora_event_handler(lorawan_event_t event);

// Send a message over LoRaWAN
static void send_message(long int timestamp, int value) {
    uint8_t tx_buffer[50] = { 0 };
    // Sending strings over LoRaWAN is not recommended
    sprintf((char*) tx_buffer, "{\"deviceId\":\"%s\", \"timestamp\":%ld,\"value\":%d}", DEVICEID, timestamp, value);
    int packet_len = strlen((char*) tx_buffer);
    printf("Sending %d bytes: \"%s\"\n", packet_len, tx_buffer);
    int16_t retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len, MSG_UNCONFIRMED_FLAG);
    // for some reason send() returns -1... I cannot find out why, the stack returns the right number. I feel that this is some weird Emscripten quirk
    if (retcode < 0) {
        retcode == LORAWAN_STATUS_WOULD_BLOCK ? printf("send - duty cycle violation\n")
                : printf("send() - Error code %d\n", retcode);
        return;
    }
    printf("%d bytes scheduled for transmission\n", retcode);
}

int initialize_lora() {
    if (DEV_EUI[0] == 0x0 && DEV_EUI[1] == 0x0 && DEV_EUI[2] == 0x0 && DEV_EUI[3] == 0x0 && DEV_EUI[4] == 0x0 && DEV_EUI[5] == 0x0 && DEV_EUI[6] == 0x0 && DEV_EUI[7] == 0x0) {
        printf("Set your LoRaWAN credentials first!\n");
        return -1;
    }
    printf("Press BUTTON1 to send the current value of the temperature sensor!\n");
    // Enable trace output for this demo, so we can see what the LoRaWAN stack does
    mbed_trace_init();
    if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
        printf("LoRa initialization failed!\n");
        return -1;
    }
        // prepare application callbacks
    callbacks.events = mbed::callback(lora_event_handler);
    lorawan.add_app_callbacks(&callbacks);
    // Disable adaptive data rating
    if (lorawan.disable_adaptive_datarate() != LORAWAN_STATUS_OK) {
        printf("disable_adaptive_datarate failed!\n");
        return -1;
    }
    lorawan.set_datarate(5); // SF7BW125
    lorawan_connect_t connect_params;
    connect_params.connect_type = LORAWAN_CONNECTION_OTAA;
    connect_params.connection_u.otaa.dev_eui = DEV_EUI;
    connect_params.connection_u.otaa.app_eui = APP_EUI;
    connect_params.connection_u.otaa.app_key = APP_KEY;
    connect_params.connection_u.otaa.nb_trials = 3;

    lorawan_status_t retcode = lorawan.connect(connect_params);

    if (retcode == LORAWAN_STATUS_OK ||
        retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
    } else {
        printf("Connection error, code = %d\n", retcode);
        return -1;
    }

    printf("Connection - In Progress ...\r\n");
    // make your event queue dispatching events forever
    ev_queue.dispatch_forever();
    return 0;
}

/**
 * Blinking a yellow LED.
 *
 * @param duration length of time to blink .
 */
static void yellowblinking(int duration){
    // Data to send
   // send_message("FELIX", 220014243, 1);
    int counter=0; 
    while(counter < duration)
    {
        counter+=1;
        yellowled = !yellowled;
        wait(1);
    }
    yellowled = 0;
}

/**
 * Crossing pedestrian.
 *
 * @param duration length of time for pedestrian to cross.
 */
static void pedestriancrossing(int duration){
   // Data to send
    //send_message("FELIX", 220014243, 2);
    redled = 1;
    int counter=0; 
    while(counter < duration)
    {
        counter+=1;
        wait(1);
    }
    redled = 0;
    yellowled = 1;
}

/**
 * Display content on LCD.
 *
 * @param advert The advertising text to be displayed.
 */
static void display(char *advert) {
    lcd.cls();  // Clear LCD
    lcd.locate(10, 5); // get cursor to position x=3px and y=5px
    lcd.printf(advert); // Write text into LCD buffer
    lcd.copy_to_lcd();
}


/**
 * Callback (Interrupt Service Routine) when a bouton is clicked.
 *
 */
void pedestrian_isr() {
     wait(1);
     if (state == 0) // YELLOW IS ON AND RED IS OFF
     {
        state = 1; // YELLOW IS BLINKING AND RED IS OFF
        send_message(220014243, state);
        display(pedestrianAdvert);
        yellowblinking(YELLOWBLINKINGDURATION);
        wait(1);
        state = 2;  // YELLOW IS OFF AND RED IS ON
        send_message(220014243, state);
        display(carDriverAdvert);
        pedestriancrossing(REDLIGHTONDURATION);
        state = 0;
        send_message(220014243, state);
        lcd.cls();
     }
     if(state == 2) // RED 
     {
         // extend the duration of RED by 5 seconds
     }
}

int main() {
    pedestrianbutton.rise(ev_queue.event(&pedestrian_isr)); // Registering an ISR for button click
    initialize_lora();
    // Data to send
//send_message("FELIX", 220014243, 0);
}

// Event handler
static void lora_event_handler(lorawan_event_t event) {
    switch (event) {
        case CONNECTED:
            printf("Connection - Successful\n");
            break;
        case DISCONNECTED:
            ev_queue.break_dispatch();
            printf("Disconnected Successfully\n");
            break;
        case TX_DONE:
            printf("Message Sent to Network Server\n");
            break;
        case TX_TIMEOUT:
        case TX_ERROR:
        case TX_CRYPTO_ERROR:
        case TX_SCHEDULING_ERROR:
            printf("Transmission Error - EventCode = %d\n", event);
            break;
        case JOIN_FAILURE:
            printf("OTAA Failed - Check Keys\n");
            break;
        default:
            MBED_ASSERT("Unknown Event");
    }
}