recepteur.cpp 6.67 KB
////////////////////////////////////////////////////////////////////////////////
/*RECEPTEUR LoRa
par : Robin Cavalieri
      IMA5SC
PFE 2018 */
////////////////////////////////////////////////////////////////////////////////
//Headers
#include "mbed.h"
#include "main.h"
#include "sx1276-hal.h"
#include "debug.h"
////////////////////////////////////////////////////////////////////////////////
#define DEBUG_MESSAGE   0
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Modulation LoRa active
#define USE_MODEM_LORA  1
#define USE_MODEM_FSK   !USE_MODEM_LORA
//Paramètres
#define RF_FREQUENCY 868000000 //Hz : Fréquence de travail pour UE
#define TX_OUTPUT_POWER 14     //dBm : puissance de sortie du signal
#define LORA_BANDWIDTH 1         // 0:125 kHz de BP
#define LORA_SPREADING_FACTOR 10 // [SF7..SF12] Portée du débit. grande portée inutile
#define LORA_CODINGRATE 1
#define LORA_PREAMBLE_LENGTH 8   // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 5    // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_FHSS_ENABLED false //true initial
#define LORA_NB_SYMB_HOP 4
#define LORA_IQ_INVERSION_ON false
#define LORA_CRC_ENABLED true
#define RX_TIMEOUT_VALUE 0      // in ms
#define BUFFER_SIZE 32        // Define the payload size here
#define TRAME_SIZE 18
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Variables globales
typedef enum
{
    LOWPOWER = 0,
    IDLE,
    RX,
    RX_TIMEOUT,
    RX_ERROR,
    CAD,
    CAD_DONE
}AppStates_t;

volatile AppStates_t State = LOWPOWER;
static RadioEvents_t RadioEvents;
SX1276MB1xAS Radio( NULL );
uint16_t BufferSize = BUFFER_SIZE;
uint8_t Buffer[BUFFER_SIZE];
int16_t RssiValue = 0.0;
int8_t SnrValue = 0.0;
uint8_t receptMsg[TRAME_SIZE];
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Fonctions utiles
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
    Radio.SetChannel( HoppingFrequencies[0] );
    Radio.Sleep( );
    BufferSize = size;
    memcpy( Buffer, payload, BufferSize );
    RssiValue = rssi;
    SnrValue = snr;
    State = RX;
    debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
}

void OnRxTimeout( void )
{
    Radio.SetChannel( HoppingFrequencies[0] );
    Radio.Sleep( );
    Buffer[BufferSize] = 0;
    State = RX_TIMEOUT;
    debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
}

void OnRxError( void )
{
    Radio.SetChannel( HoppingFrequencies[0] );
    Radio.Sleep( );
    State = RX_ERROR;
    debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
}

void OnFhssChangeChannel( uint8_t channelIndex )
{
    Radio.SetChannel( HoppingFrequencies[channelIndex] );
    debug_if( DEBUG_MESSAGE, "F%d-", channelIndex );
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
void TrameComp()
{
    //Création d'une trame de comparaison pour vérifier si l'id de l'emetteur est bonne
    /*
    [1...] : ID de l'emetteur
    [...] : ID du recepteur cible
    [...] : Température communiquée ici 20 pour l'exemple
    */
    //Converti en ASCII
    receptMsg[0] = 0x7C; //emetteur |
    receptMsg[1] =  0x46; //emetteur F
    receptMsg[2] =  0x33; //emetteur 3
    receptMsg[3] =  0x30; //emetteur 0
    receptMsg[4] =  0x33; //emetteur 3
    receptMsg[5] =  0x52; //emetteur R
    receptMsg[6] =  0x45; //emetteur E
    //
    receptMsg[7] =  0x46; //recepteur F
    receptMsg[8] =  0x30; //recepteur 0
    receptMsg[9] =  0x39; //recepteur 9
    receptMsg[10] =  0x31; //recepteur 1
    receptMsg[11] = 0x43; //recepteur C
    //
    receptMsg[12] = 0x32; //temperature non vérifiee
    receptMsg[13] = 0x31; //temperature non verifiee
    receptMsg[14] = 0x7C; //emetteur |
    //
    receptMsg[15] = 0x30; //cpt non vérifé
    receptMsg[16] = 0x30; //cpt non vérifié
    receptMsg[17] = 0x30; //cpt non vérifé
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Main réception
int main( void )
{
    debug("\nRecepteur SX1276 \n\n\r");

    // Initialize Radio driver
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.RxError = OnRxError;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
    Radio.Init(&RadioEvents);

    //Véification de la communication avec la board
    while(Radio.Read(REG_VERSION) == 0x00)
    {
        debug("Aucune radio detectee\n\r", NULL);
        wait(1);
    }

    //Checking du matériel connecté
    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\r Materiel detecte : SX1276MB1LAS \n\r" );
    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\r Materiel detecte : SX1276MB1MAS \n\r" );

    //Paramétrage du canal
    Radio.SetChannel( HoppingFrequencies[0] );

    #if USE_MODEM_LORA == 1

        debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r" );
        debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r" );

        Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
                             LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
                             LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
                             LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
                             LORA_IQ_INVERSION_ON, true );

    #else
        #error "Aucun modem defini"
    #endif
        TrameComp();
        debug("Reception en cours...\r\n");
        debug("Etat initial du Buffer : %s\r\n",(char*)Buffer);

        while(1)
        {
            Radio.Rx(RX_TIMEOUT_VALUE); //Reception mode en continu car TIME_OUT_VALUE = 0
            if( BufferSize > 0 )
            {
                if(strncmp((const char*)Buffer, (const char*)receptMsg, TRAME_SIZE-6) == 0) //-2 pour ne vérifier que l'entete
                {
                    //Buffer de données
                    debug("%s\n",(char*)Buffer);
                    //Puissance de reception du signal
                    debug("%d\n",RssiValue);
                    //PROGRAMME PYTHON recorder.py POUR ENREGISTRER LES DONNEES sortant du port série
                    //il faut vider le buffer
                    memset (Buffer, NULL, BUFFER_SIZE);
                }
                wait_ms(1000);
            }
        }
}
////////////////////////////////////////////////////////////////////////////////