Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed LoRaWAN-lib SX1272Lib
Diff: app/main.cpp
- Revision:
- 11:8407ff54c40e
- Parent:
- 10:1a85ff06be1a
--- a/app/main.cpp Wed Jan 23 16:28:51 2019 +0000
+++ b/app/main.cpp Wed Feb 06 19:16:44 2019 +0000
@@ -12,9 +12,9 @@
/*!
* Defines the application data transmission duty cycle. 1s, value in [ms].
*/
-#define APP_TX_DUTYCYCLE 1000
-#define TAB_MAX 10
-#define APP_TX_DUTYCYCLE_RND 250
+#define APP_TX_DUTYCYCLE 500
+#define TAB_MAX 20
+#define APP_TX_DUTYCYCLE_RND 5
#define LORAWAN_DEFAULT_DATARATE DR_5
#define LORAWAN_CONFIRMED_MSG_ON true
@@ -40,8 +40,8 @@
//############INITIALISATION LIAISON SERIE####################
void saisie_serie(int tab[6]);
-int tabd[6]= {0,0,0,1,1,2018};
-
+//int tabd[6]= {0,0,0,1,1,2018};
+ int tabd[6];
InterruptIn bouton(USER_BUTTON); // Déclaration de l'interruption extern activée par le bouton utilisateur bleu
//Serial pc(USBTX, USBRX); // tx, rx//Serial pc(SERIAL_TX, SERIAL_RX); // Déclaration de entrées pour la liaison série
@@ -184,7 +184,7 @@
}LoRaMacDownlinkStatus;
volatile bool DownlinkStatusUpdated = false;
-void SerialDisplayRefresh( void )
+void SerialDisplayRefresh( int tabd[6] )
{
MibRequestConfirm_t mibReq;
@@ -221,7 +221,7 @@
case 'R':
case 'r':
// Refresh Serial screen
- SerialDisplayRefresh( );
+ SerialDisplayRefresh(tabd);
break;
default:
break;
@@ -233,12 +233,12 @@
* \brief Prepares the payload of the frame
*/
-uint8_t poidstest=34;
-uint8_t heuretest=0;
+int poidstest=34;
+
typedef struct {
- uint8_t heure;//[8]; // Augmentation taille prog. Solution : heure = pointeur vers malloc (26 ou sizeof ctime)=1bit
- uint8_t masse;//[8];
+ int heure[8]; // Augmentation taille prog. Solution : heure = pointeur vers malloc (26 ou sizeof ctime)=1bit
+ int masse[8];
} data;
data data_packet[TAB_MAX];
@@ -256,33 +256,19 @@
}
//##################################
-static void PrepareTxFrame( uint8_t port,data datat )//, 2EME PARAMETRE TABLEAU DONNEE INIT=>APPDATA DANS FONCION fournir 1 case tableau data_packet
-{
- int dpacket[8];
- time_t seconds=time(NULL);
+static void PrepareTxFrame( uint8_t port,data datat[TAB_MAX],int compt )//, 2EME PARAMETRE TABLEAU DONNEE INIT=>APPDATA DANS FONCION fournir 1 case tableau data_packet
+{
switch( port )
{
case 15:
{
- //printf("%d\r\n",seconds);
- copy_time(dpacket,seconds);
- /*for(int k=0;k<8;k++)
- {
- printf("%X",dpacket[k]);
- }
- printf("\n\r");*/
+
for(int h=0;h<8;h++)
{
-
-
- AppData[h]=dpacket[h];
- //AppData[h]=datat.heure;
- //printf(" ||H:%d|\n\r",AppData[0]);
-
- //printf(" ||P:%d|\n\r",AppData[16]);
+ AppData[h]=datat[compt].heure[h];
}
- AppData[16]=datat.masse;
+ AppData[16]=datat[compt].masse[0];
}
break;
case 224:
@@ -368,9 +354,11 @@
if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK )
{
+
return false;
}
return true;
+
}
/*!
@@ -454,6 +442,7 @@
* \param [IN] mcpsIndication - Pointer to the indication structure,
* containing indication attributes.
*/
+uint8_t c_send=TAB_MAX;
static void McpsIndication( McpsIndication_t *mcpsIndication )
{
if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK )
@@ -505,6 +494,7 @@
LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2;
}
LoRaMacDownlinkStatus.DownlinkCounter++;
+ c_send--;
LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData;
LoRaMacDownlinkStatus.Port = mcpsIndication->Port;
LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer;
@@ -710,13 +700,29 @@
NextTx = true;
UplinkStatusUpdated = true;
}
+ void fil (int tabd[6],time_t seconde)
+ {
+ char buffer[8];
+ strftime(buffer, 8, "%Y\n", localtime(&seconde));
+ tabd[5]=atoi(buffer);
+ strftime(buffer, 8, "%m\n", localtime(&seconde));
+ tabd[4]=atoi(buffer);
+ strftime(buffer, 8, "%d\n", localtime(&seconde));
+ tabd[3]=atoi(buffer);
+ strftime(buffer, 8, "%S\n", localtime(&seconde));
+ tabd[2]=atoi(buffer);
+ strftime(buffer, 8, "%M\n", localtime(&seconde));
+ tabd[1]=atoi(buffer);
+ strftime(buffer, 8, "%H\n", localtime(&seconde));
+ tabd[0]=atoi(buffer);
+
+}
/*######################################################################
* Main application entry point.
*/
-
int main( void )
{
//####### INITIALISATION MODULE LORA ############
@@ -741,12 +747,12 @@
bouton.rise(&interruption_extern_bouton); //initialisation de l'interruption extern sur le bouton user
//####### RTC INIT ########
- set_time(1514764800);
+ //set_time(1514764800);
int config_s=0;
//####### VARIABLES ########
uint8_t compt=0;
- uint8_t c_send=TAB_MAX;
+
while( 1 )
{
@@ -772,155 +778,183 @@
//SerialDisplayUpdateLedState( 2, Led2State );
SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize );
}
+
+
}
-
- //time_t seconds = time(NULL);
- //sprintf(data_packet[compt].heure,"%s",ctime(&seconds));// copie de la valeur de la RTC en cours dans le tableau de structure data_packet
-
- data_packet[0].heure=heuretest;
- data_packet[0].masse=poidstest;
+ data_packet[compt].masse[0]=poidstest;
poidstest++;
- heuretest++;
-
- //printf("%d\r\n",compt);
-
- wait(1);
switch (etat)
{
case 0: //bouton non enclenché
- {
+ {
+ time_t seconds=time(NULL);
+ fil (tabd,seconds);
+ SerialDisplaytest( compt,0 );
+ SerialDisplaytest( c_send,1 );
SerialDisplayRTCactive(etat);
SerialDisplayTIME(tabd);
- time_t seconds = time(NULL);
- switch( DeviceState )
+ //time_t seconds = time(NULL)
+ if (compt<=(TAB_MAX-1)||(c_send==0))
{
+ if( c_send==0)
+ {
+ compt=0;
+ c_send=TAB_MAX;
+ }
+ wait(3);
+ //time_t seconds=time(NULL);
+ copy_time(data_packet[compt].heure,seconds);
+ compt++;
+ }
- case DEVICE_STATE_INIT:
+ else
+ { //SerialDisplaytest( compt+1,8 );
+ switch( DeviceState )
{
- LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
- LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
- LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
- LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
- LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
- TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
+ case DEVICE_STATE_INIT:
+ {
+ LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm;
+ LoRaMacPrimitives.MacMcpsIndication = McpsIndication;
+ LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm;
+ LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
+ LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks );
+ TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent );
- mibReq.Type = MIB_ADR;
- mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
- LoRaMacMibSetRequestConfirm( &mibReq );
- mibReq.Type = MIB_PUBLIC_NETWORK;
- mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
- LoRaMacMibSetRequestConfirm( &mibReq );
- LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
- SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
+ mibReq.Type = MIB_ADR;
+ mibReq.Param.AdrEnable = LORAWAN_ADR_ON;
+ LoRaMacMibSetRequestConfirm( &mibReq );
+ mibReq.Type = MIB_PUBLIC_NETWORK;
+ mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK;
+ LoRaMacMibSetRequestConfirm( &mibReq );
+ LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
+ SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON );
- LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
- LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
- LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
- LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
- LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
- LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
- LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
+ LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 );
+ LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 );
+ LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 );
+ LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 );
+ LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 );
+ LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 );
+ LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 );
- mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
- mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
- LoRaMacMibSetRequestConfirm( &mibReq );
+ mibReq.Type = MIB_RX2_DEFAULT_CHANNEL;
+ mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
+ LoRaMacMibSetRequestConfirm( &mibReq );
- mibReq.Type = MIB_RX2_CHANNEL;
- mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
- LoRaMacMibSetRequestConfirm( &mibReq );
+ mibReq.Type = MIB_RX2_CHANNEL;
+ mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 };
+ LoRaMacMibSetRequestConfirm( &mibReq );
- SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
- SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
- SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
+ SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION );
+ SerialDisplayUpdateAdr( LORAWAN_ADR_ON );
+ SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK );
- LoRaMacDownlinkStatus.DownlinkCounter = 0;
+ LoRaMacDownlinkStatus.DownlinkCounter = 0;
- DeviceState = DEVICE_STATE_JOIN;
- break;
- }
- case DEVICE_STATE_JOIN:
- {
- mibReq.Type = MIB_NET_ID;
- mibReq.Param.NetID = LORAWAN_NETWORK_ID;
- LoRaMacMibSetRequestConfirm( &mibReq );
+ DeviceState = DEVICE_STATE_JOIN;
+
+ break;
+ }
+ case DEVICE_STATE_JOIN:
+ {
+ mibReq.Type = MIB_NET_ID;
+ mibReq.Param.NetID = LORAWAN_NETWORK_ID;
+ LoRaMacMibSetRequestConfirm( &mibReq );
- mibReq.Type = MIB_DEV_ADDR;
- mibReq.Param.DevAddr = DevAddr;
- LoRaMacMibSetRequestConfirm( &mibReq );
-
- mibReq.Type = MIB_NWK_SKEY;
- mibReq.Param.NwkSKey = NwkSKey;
- LoRaMacMibSetRequestConfirm( &mibReq );
+ mibReq.Type = MIB_DEV_ADDR;
+ mibReq.Param.DevAddr = DevAddr;
+ LoRaMacMibSetRequestConfirm( &mibReq );
- mibReq.Type = MIB_APP_SKEY;
- mibReq.Param.AppSKey = AppSKey;
- LoRaMacMibSetRequestConfirm( &mibReq );
+ mibReq.Type = MIB_NWK_SKEY;
+ mibReq.Param.NwkSKey = NwkSKey;
+ LoRaMacMibSetRequestConfirm( &mibReq );
- mibReq.Type = MIB_NETWORK_JOINED;
- mibReq.Param.IsNetworkJoined = true;
- LoRaMacMibSetRequestConfirm( &mibReq );
+ mibReq.Type = MIB_APP_SKEY;
+ mibReq.Param.AppSKey = AppSKey;
+ LoRaMacMibSetRequestConfirm( &mibReq );
- DeviceState = DEVICE_STATE_SEND;
- IsNetworkJoinedStatusUpdate = true;
- break;
- }
- case DEVICE_STATE_SEND:
- {
- if( NextTx == true )
+ mibReq.Type = MIB_NETWORK_JOINED;
+ mibReq.Param.IsNetworkJoined = true;
+ LoRaMacMibSetRequestConfirm( &mibReq );
+ //c_send--;
+ // SerialDisplaytest( c_send,3 );
+ DeviceState = DEVICE_STATE_SEND;
+ IsNetworkJoinedStatusUpdate = true;
+ break;
+ }
+ case DEVICE_STATE_SEND:
+ {
+ if(NextTx == true )
+ {
+ SerialDisplayUpdateUplinkAcked( false );
+ SerialDisplayUpdateDonwlinkRxData( false );
+
+ ///prepare tx
+ PrepareTxFrame( AppPort,data_packet,compt-c_send);
+ NextTx = SendFrame( );
+ SerialDisplaytest( compt-c_send,6 );
+
+
+
+ }
+
+ if( ComplianceTest.Running == true )
+ {
+ // Schedule next packet transmission
+ TxDutyCycleTime = 500; // 5000 ms
+
+
+
+ }
+ else
+ {
+
+ // Schedule next packet transmission
+ TxDutyCycleTime = 75;//APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
+ }
+
+ DeviceState = DEVICE_STATE_CYCLE;
+
+
+
+ break;
+ }
+ case DEVICE_STATE_CYCLE:
{
- SerialDisplayUpdateUplinkAcked( false );
- SerialDisplayUpdateDonwlinkRxData( false );
-
- ///prepare tx
- PrepareTxFrame( AppPort,data_packet[0] );
- NextTx = SendFrame( );
-
-
- }
- if( ComplianceTest.Running == true )
- {
- // Schedule next packet transmission
- TxDutyCycleTime = 1000; // 5000 ms
- }
- else
- {
+ //
+
+
// Schedule next packet transmission
- TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
+ TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
+ TimerStart( &TxNextPacketTimer );
+ DeviceState = DEVICE_STATE_SLEEP;
+ break;
}
- DeviceState = DEVICE_STATE_CYCLE;
- break;
- }
- case DEVICE_STATE_CYCLE:
- {
- DeviceState = DEVICE_STATE_SLEEP;
- // Schedule next packet transmission
- TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime );
- TimerStart( &TxNextPacketTimer );
- break;
+ case DEVICE_STATE_SLEEP:
+ {
+ // Wake up through events
+ break;
+ }
+ default:
+ {
+ DeviceState = DEVICE_STATE_INIT;
+ break;
+ }
+
}
- case DEVICE_STATE_SLEEP:
- {
-
- // Wake up through events
- break;
- }
- default:
- {
- DeviceState = DEVICE_STATE_INIT;
- break;
- }
-
+
}
- break;
+ break;
}
case 1 : //bouton enclenché
{
SerialDisplayRTCactive(etat);
saisie_serie(tabd);
SerialDisplayTIME(tabd);
+ //chk_and_set_time(tabd);
config_s=unix_timestamp(tabd);
set_time(config_s);
//printf( "%d\r\n",etat);
@@ -933,4 +967,3 @@
}
}
-