Copy off node 1 to behave node 2

Dependencies:   mbed RF24Network RF24

main.cpp

Committer:
Giamarchi
Date:
2021-05-20
Revision:
4:c6de6d47c54a
Parent:
3:d605536db315
Child:
5:48f3f58ebd13

File content as of revision 4:c6de6d47c54a:

#include "mbed.h"
#include <RF24Network.h>
#include <RF24.h>

Serial pc(USBTX, USBRX);

DigitalOut LED_V(PA_12);
DigitalOut LED_J(PB_0);
DigitalOut LED_R(PB_7);

DigitalIn BP(PA_0);

#define nrf_CE      PA_8
#define nrf_CSN     PA_11
#define spi_MOSI    PB_5
#define spi_MISO    PB_4
#define spi_SCK     PB_3

RF24 radio(spi_MOSI, spi_MISO, spi_SCK, nrf_CE, nrf_CSN );

// Network uses that radio
RF24Network network(radio);

// Address of our node
const uint16_t this_node = 01;

// Address of the other node
const uint16_t other_node = 00;

// How often to send payload packet to the other unit
const unsigned long interval = 1000; //ms

// When did we last send?
unsigned long last_sent;
Timer t;

// How many have we sent already
unsigned long packets_sent;
Timer t_packet;

// Structure of our payload
struct payload_t 
{
    uint8_t src;        // Source du message
    uint8_t dest;       // Destination du message
    uint8_t data;       // Data du message
};

struct _node
{
    uint8_t src;        // Source du message
    uint8_t dest;       // Destination du message
    uint8_t del;
};

int main()
{
    _node node;
       
    BP.mode(PullUp);    // Resist interne Pull up
//    pc.baud(9600);
    wait_ms(1000);

    pc.printf("mBed RF24Network node : 01\r\n");
    radio.begin();
    network.begin(/*channel*/ 90, /*node address*/ this_node);
    wait_ms(2000);
    t.start();
    t_packet.start();
    
    
    while(1) 
    {
        // Refresh Dels
        LED_R = node.del & 0x01;
        LED_J = (node.del & 0x02)>>1;
        LED_V = (node.del & 0x04)>>2;
        
        // Pump the network regularly
        network.update();

        /* If it's time to send a message, send it! */
/*        unsigned long now = t.read_ms();
        if ( now >= interval  ) 
        {
            t.reset();

            pc.printf("Envoi...");
            payload_t payload_tx;
            if(!BP)
                payload_tx.num = 01;
            else
                payload_tx.num = 00;
                
            payload_tx.del++;


            RF24NetworkHeader header_tx(other_node);    // vers node "other_node"
            bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx));
            if (ok)
                pc.printf("ok.\r\n");
            else
                pc.printf("erreur.\r\n");
                
                
        }
        */
        while ( network.available() ) 
        {
            // If so, grab it and print it out
            RF24NetworkHeader header_rx;
            payload_t payload_rx;
            network.read(header_rx,&payload_rx,sizeof(payload_rx));

            pc.printf("Node 01 recu de node %d, data : %d\r\n",payload_rx.src,payload_rx.data);
            
            node.del = payload_rx.data;
        }
        if(!BP)
        {
            payload_t payload_tx;
            payload_tx.src = this_node;
            payload_tx.dest = 02;
            payload_tx.data = node.del%5;
            pc.printf("Envoi...");

            RF24NetworkHeader header_tx(other_node);    // vers node "other_node"
            bool ok = network.write(header_tx,&payload_tx,sizeof(payload_tx));
            if (ok)
                pc.printf("ok.\r\n");
            else
                pc.printf("erreur.\r\n");
            wait_ms(2000);
        }
    }
}