Code APP3
Dependencies: mbed EthernetInterface WebSocketClient mbed-rtos BufferedSerial
Fork of APP3_Lab by
Diff: main.cpp
- Revision:
- 25:7b808fb9e025
- Parent:
- 24:29ec42daa82e
- Child:
- 26:cbf539141bfe
--- a/main.cpp Mon Oct 02 17:35:34 2017 +0000 +++ b/main.cpp Mon Oct 02 18:46:45 2017 +0000 @@ -2,6 +2,9 @@ #include "rtos.h" +#include "EthernetInterface.h" +#include "Websocket.h" + #include "xbee.h" #include "parser.h" #include "sensors.h" @@ -15,15 +18,23 @@ char recv_buff[BUFFER_SIZE] = {0}; DigitalOut loop_led(LED4); -void set_remote_xbee_dio4(bool set); +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]; -int last_addr_64_index = 0; +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 @@ -41,80 +52,56 @@ #endif } -void set_remote_xbee_dio4(bool set) -{ - if (set) - { - remote_at_command_set(AT_COMMAND_DIO4_MSB, AT_COMMAND_DIO4_LSB, - AT_COMMAND_DIO_OUT_LOW, 0x02); - } - else - { - remote_at_command_set(AT_COMMAND_DIO4_MSB, AT_COMMAND_DIO4_LSB, - AT_COMMAND_DIO_OUT_HIGH, 0x02); - } -} - #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; - - bool toggle_led = false; + + flipper.attach(&send_del_to_all, 1.0); while(1) { + ws.send("Allo"); bool finished_packet = receive(¤t_frame, BUFFER_SIZE); if (finished_packet) { process_frame(¤t_frame); } - - //set_remote_xbee_dio4(toggle_led); - toggle_led = !toggle_led; - - loop_led = !loop_led; - wait_ms(100); + wait_ms(1); } } #else void routeur() { router_config_t config = read_router_config(); + set_pan_id(config.pan_id); - char sensor_buffer[64] = {}; + initialize_sensors(); - DECLARE_ADDR64_COORD - DECLARE_ADDR16_UNKNOWN_OR_BCAST - DigitalOut led_3(LED3); - led_3 = 1; + flipper.attach(&get_all_sensors, config.refresh_freq); + + frame_t current_frame; while(1) - { - 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); - led_3 = !led_3; - } - 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); - } - } - loop_led = !loop_led; - wait(config.refresh_freq); + { + bool finished_packet = receive(¤t_frame, BUFFER_SIZE); + + if (finished_packet) + { + process_frame(¤t_frame); + } + wait_ms(1); } } #endif @@ -140,7 +127,15 @@ { char current = frame->buffer[i]; } */ - if (frame->buffer[0] == 0x90) + 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 @@ -179,4 +174,59 @@ } } return 0; -} \ No newline at end of file +} + +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(); +}