Polytech school project. RICM4 students, see http://air.imag.fr/index.php/Projets-2016-2017-Station_de_pompage_connect%C3%A9e for more information
Dependencies: SX1272Lib mbed WakeUp
Fork of SX1272PingPong by
Diff: main.cpp
- Revision:
- 14:261007103beb
- Parent:
- 13:edb9b443c1dd
- Child:
- 16:85fb5e37def7
--- a/main.cpp Wed Feb 17 09:17:46 2016 +0000 +++ b/main.cpp Tue Feb 28 13:11:52 2017 +0000 @@ -2,15 +2,21 @@ #include "main.h" #include "sx1272-hal.h" #include "debug.h" +#include "trame.h" +#include "ordre.h" +#include "pompe.h" +//#include "niveau.h" /* Set this flag to '1' to display debug messages on the console */ #define DEBUG_MESSAGE 1 +#define ID_DEVICE 0x24 +#define ID_STATION 0x55 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ #define USE_MODEM_LORA 1 #define USE_MODEM_FSK !USE_MODEM_LORA -#define RF_FREQUENCY 868000000 // Hz +#define RF_FREQUENCY 865000000 // Hz #define TX_OUTPUT_POWER 14 // 14 dBm #if USE_MODEM_LORA == 1 @@ -46,7 +52,7 @@ #error "Please define a modem in the compiler options." #endif -#define RX_TIMEOUT_VALUE 3500000 // in us +#define RX_TIMEOUT_VALUE 30000000//3500000 // in us #define BUFFER_SIZE 32 // Define the payload size here #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) @@ -86,8 +92,8 @@ */ SX1272MB2xAS Radio( NULL ); -const uint8_t PingMsg[] = "PING"; -const uint8_t PongMsg[] = "PONG"; +//const uint8_t PingMsg[] = "PING"; +//const uint8_t PongMsg[] = "PONG"; uint16_t BufferSize = BUFFER_SIZE; uint8_t Buffer[BUFFER_SIZE]; @@ -100,7 +106,7 @@ uint8_t i; bool isMaster = true; - debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); + debug( "\n\n\r SX1272 Cuve Demo Application \n\n\r" ); // Initialize Radio driver RadioEvents.TxDone = OnTxDone; @@ -157,10 +163,26 @@ #endif - debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); + debug_if( DEBUG_MESSAGE, "Starting Cuve loop\r\n" ); led = 0; - + + DigitalOut pompe(PC_14); + + //Pompe pompe(PC_14); + + DigitalIn p1(PC_15);//(PC_15); //Poid fort + DigitalIn p2(PH_0); + DigitalIn p3(PH_1); //Poid faible + + //Niveau cuve(); + //char niveau = (char) ((p1.read()<<2) | (p2.read()<<1) | p3.read()); + TrameData dataPaquet(ID_DEVICE, 63, (char) pompe.read(), 0x1F, 0xF); + + bool enAttente = false; + + //TODO + // Distinguer deux temps. Il faut pouvoir s'endormir une heure, mais aussi écouter pendant 5 minutes Radio.Rx( RX_TIMEOUT_VALUE ); while( 1 ) @@ -168,107 +190,105 @@ switch( State ) { case RX: - if( isMaster == true ) + //If the board is the master, it have the initiative + if( enAttente == true ) { + debug("1\r\n"); if( BufferSize > 0 ) { - if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) + debug("2\r\n"); + debug((const char* )Buffer); + debug(" "); + debug((const char* ) ID_DEVICE); + Ordre trameRecue((char*)Buffer); + //Si message recu alors trouver ordre correspondant et faire traitement + if( trameRecue.getIdRecepteur() == (char) ID_DEVICE ) { - led = !led; - debug( "...Pong\r\n" ); - // Send the next PING frame - strcpy( ( char* )Buffer, ( char* )PingMsg ); - // We fill the buffer with numbers for the payload - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; + if( trameRecue.getIdEmetteur() == (char) ID_STATION ){ + //Ordre pour nous + led = !led; + debug( "...Ordre recu \r\n" ); + Ordre o((char *) Buffer); + + //TODO executer ordre + debug("Executer Ordre\r\n"); + + //On attend 5 minutes + debug("Dormir\r\n"); + wait(10); + enAttente = false; + Radio.Rx( RX_TIMEOUT_VALUE ); + }else{ + debug("autre station\r\n"); } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); - } - else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) - { // A master already exists then become a slave - debug( "...Ping\r\n" ); - led = !led; - isMaster = false; - // Send the next PONG frame - strcpy( ( char* )Buffer, ( char* )PongMsg ); - // We fill the buffer with numbers for the payload - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); } else // valid reception but neither a PING or a PONG message { // Set device as master ans start again + debug("Reception message pour quelqu'un d'autre\r\n"); isMaster = true; - Radio.Rx( RX_TIMEOUT_VALUE ); + Radio.Rx( RX_TIMEOUT_VALUE ); //La on reattend pour 5 minutes à chaque foi que qq'un envoit un message c'est pas top.... } } } - else + else //enAttente ==false donc on envoit les donnees { - if( BufferSize > 0 ) + //Recuperer les données capteurs + //dataPaquet.mettreAJourEtatPompe(pompe.etat()); + //dataPaquet.mettreAJourNiveauCuve(cuve.getNiveauCuve()); + debug("envoie1\r\n"); + dataPaquet.mettreAJourEtatPompe((char) 0); + dataPaquet.mettreAJourNiveauCuve((char) 0x1F); + dataPaquet.mettreAJourNiveauBatterie((char) 0xF); //TODO + + char * trame = dataPaquet.creerTrame(); + debug( trame); + strcpy( ( char* ) Buffer, trame); + for (i = TAILLE_TRAME_DATA; i < BufferSize; i++ ) { - if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) - { - led = !led; - debug( "...Ping\r\n" ); - // Send the reply to the PING string - strcpy( ( char* )Buffer, ( char* )PongMsg ); - // We fill the buffer with numbers for the payload - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); - } - else // valid reception but not a PING as expected - { // Set device as master and start again - isMaster = true; - Radio.Rx( RX_TIMEOUT_VALUE ); - } + Buffer[i] = i - 4; } + wait_ms( 10 ); + // Send the data + Radio.Send( Buffer, BufferSize ); + enAttente = true; } State = LOWPOWER; break; case TX: led = !led; - if( isMaster == true ) - { - debug( "Ping...\r\n" ); - } - else - { - debug( "Pong...\r\n" ); - } + debug( "Envoi des donnees...\r\n" ); Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case RX_TIMEOUT: - if( isMaster == true ) + debug("envoie2\r\n"); + dataPaquet.mettreAJourEtatPompe((char) 0); + dataPaquet.mettreAJourNiveauCuve((char) 0x1F); + dataPaquet.mettreAJourNiveauBatterie((char) 0xF); //TODO + + char * trame = dataPaquet.creerTrame(); + debug( trame); + strcpy( ( char* ) Buffer, trame); + for (i = TAILLE_TRAME_DATA; i < BufferSize; i++ ) { - // Send the next PING frame - strcpy( ( char* )Buffer, ( char* )PingMsg ); - for( i = 4; i < BufferSize; i++ ) - { - Buffer[i] = i - 4; - } - wait_ms( 10 ); - Radio.Send( Buffer, BufferSize ); + Buffer[i] = i - 4; } - else - { - Radio.Rx( RX_TIMEOUT_VALUE ); - } + wait_ms( 10 ); + // Send the data + Radio.Send( Buffer, BufferSize ); + enAttente = true; + //On attend 5 minutes + //debug("Dormir\r\n"); + //wait(10); + //enAttente = false; + //Radio.Rx( RX_TIMEOUT_VALUE ); State = LOWPOWER; break; case RX_ERROR: + //Erreur CRC, faire une demande de renvoi + //TODO // We have received a Packet with a CRC error, send reply as if packet was correct - if( isMaster == true ) + /*if( isMaster == true ) { // Send the next PING frame strcpy( ( char* )Buffer, ( char* )PingMsg ); @@ -289,7 +309,10 @@ } wait_ms( 10 ); Radio.Send( Buffer, BufferSize ); - } + }*/ + debug("Erreur CRC \r\n"); + Radio.Rx( RX_TIMEOUT_VALUE ); + State = LOWPOWER; break; case TX_TIMEOUT: