Code APP3

Dependencies:   mbed EthernetInterface WebSocketClient mbed-rtos BufferedSerial

Fork of APP3_Lab by Jean-Philippe Fournier

main.cpp

Committer:
Cheroukee
Date:
2017-10-02
Revision:
25:7b808fb9e025
Parent:
24:29ec42daa82e
Child:
26:cbf539141bfe

File content as of revision 25:7b808fb9e025:

#include "mbed.h"

#include "rtos.h"

#include "EthernetInterface.h"
#include "Websocket.h"

#include "xbee.h"
#include "parser.h"
#include "sensors.h"

#define IS_COORDINATOR 1

#define PAN_ID 0xC0FFEE

#define BUFFER_SIZE 64

char recv_buff[BUFFER_SIZE] = {0};
DigitalOut loop_led(LED4);

Ticker flipper;
Ticker error_ticker;
void display_error();
DigitalOut error_led(LED3);

void set_remote_xbee_dio4(bool set, zigbee_addr_64_t addr);

void set_pan_id(long pan_id);

zigbee_addr_64_t addr_64[255];
volatile int last_addr_64_index = 0;

int process_frame(frame_t* frame);

void send_del_to_all();
void get_all_sensors();

#if IS_COORDINATOR   
void coordinator();
#else
void routeur();
#endif

int main() {

    xbee_init();

#if IS_COORDINATOR    
    coordinator();
#else
    routeur();
#endif
}

#if IS_COORDINATOR
void coordinator()
{    
    coordinator_config_t config = read_coordinator_config();
    
    EthernetInterface eth;
    eth.init("192.168.3.3", "255.255.255.0", "192.168.3.2"); //Use DHCP
    eth.connect();
    
    Websocket ws(config.server_url);
    ws.connect();
        
    set_pan_id(config.pan_id);

    frame_t current_frame;
    
    flipper.attach(&send_del_to_all, 1.0); 
    while(1)
    {
        ws.send("Allo");
        bool finished_packet = receive(&current_frame, BUFFER_SIZE);
        
        if (finished_packet)
        {            
            process_frame(&current_frame);                       
        }
        wait_ms(1);
    }
}
#else
void routeur()
{    
    router_config_t config = read_router_config();
    
    set_pan_id(config.pan_id);
    
    initialize_sensors();

    flipper.attach(&get_all_sensors, config.refresh_freq);
    
    frame_t current_frame;
    while(1)
    {        
        bool finished_packet = receive(&current_frame, BUFFER_SIZE);
        
        if (finished_packet)
        {            
            process_frame(&current_frame);                       
        }   
        wait_ms(1);
    }
}
#endif

void set_pan_id(long pan_id)
{
    char pan_id_buffer[8] = {0};
    for (int i = 0; i < 8; i++)
    {
        pan_id_buffer[i] = 0xFF & (pan_id >> 8 * (7 - i));
    }
    at_command_set('I', 'D', pan_id_buffer, 8);
}

int process_frame(frame_t* frame)
{
    Serial pc(USBTX, USBRX); // tx, rx
    if (frame->length <= 0)
    {
        return -1;
    }
    /*for (int i = 0; i < frame->length; i++)
    {
        char current = frame->buffer[i];        
    }    */
    if (frame->buffer[0] == 0x8B)
    {
        if(frame->buffer[5] != 0x00)
        {
            error_led = 1;
            error_ticker.attach(&display_error, 1.0);    
        }
    }
    else if (frame->buffer[0] == 0x90)
    {
        // Manage source address
        // Detect the source address and list it dynamically
        zigbee_addr_64_t temp_addr_64;        
        bool already_exist = false;
        // Read 64 bit address that was in message
        temp_addr_64.addr_0 = frame->buffer[1];        
        temp_addr_64.addr_1 = frame->buffer[2];        
        temp_addr_64.addr_2 = frame->buffer[3];        
        temp_addr_64.addr_3 = frame->buffer[4];        
        temp_addr_64.addr_4 = frame->buffer[5];        
        temp_addr_64.addr_5 = frame->buffer[6];        
        temp_addr_64.addr_6 = frame->buffer[7];        
        temp_addr_64.addr_7 = frame->buffer[8];       
        
        // Verify if the received address is new
        for(int j = 0; j < last_addr_64_index; j++)
        {
            if(addr_64_equal(temp_addr_64,addr_64[j]))
            {
                already_exist = true;
            }
        }
        
        // If it is New and our array isn't full of devices add it to the array
        if(!already_exist && last_addr_64_index < 255)
        {
            last_addr_64_index++;
            addr_64[last_addr_64_index] = temp_addr_64;
        }
          
        //Data starts at 12
        for (int i = 12; i <  frame->length; i++)
        {
            pc.putc(frame->buffer[i]);   
        }        
    }    
    return 0;
}

void send_del_to_all()
{
    loop_led = !loop_led;
    static bool flip = false;
    flip = !flip;
    for (int i = 0; i < last_addr_64_index; i++)
    {
        set_remote_xbee_dio4(flip, addr_64[i]); 
    }
}

void set_remote_xbee_dio4(bool set, zigbee_addr_64_t addr)
{
    if (set)
    {
        remote_at_command_set(AT_COMMAND_DIO4_MSB, AT_COMMAND_DIO4_LSB,
            AT_COMMAND_DIO_OUT_LOW, 0x02, addr);
    }
    else
    {
        remote_at_command_set(AT_COMMAND_DIO4_MSB, AT_COMMAND_DIO4_LSB,
            AT_COMMAND_DIO_OUT_HIGH, 0x02, addr);
    }
}

void get_all_sensors()
{
    loop_led = !loop_led;
    char sensor_buffer[64] = {};
    DECLARE_ADDR64_COORD
    DECLARE_ADDR16_UNKNOWN_OR_BCAST
    for (int i = 0; i < 2; i++)
    {
        sensor_t sensor = (*p[i])();
        
        if (sensor.sensor_type == 1)
        {
            sprintf(sensor_buffer, "button::%u\n\r", sensor.sensor_result.Bouton.etat != 0 ? 1 : 0);
            transmit_request(sensor_buffer, 8 + 1 + 2, 0, USE_ADDR64_COORD, USE_ADDR16_UNKNOWN_OR_BCAST);  
        }
        else if (sensor.sensor_type == 2)
        {
            sprintf(sensor_buffer, "accel::%3.2f%3.2f%3.2f\n\r", sensor.sensor_result.Accelerometre.x, 
                                    sensor.sensor_result.Accelerometre.y, sensor.sensor_result.Accelerometre.z);
            transmit_request(sensor_buffer, 7 + 15 + 2, 0, USE_ADDR64_COORD, USE_ADDR16_UNKNOWN_OR_BCAST);
        }
    }     
}

void display_error()
{
    error_led = 0;
    error_ticker.detach();
}