Code APP3
Dependencies: mbed EthernetInterface WebSocketClient mbed-rtos BufferedSerial
Fork of APP3_Lab by
main.cpp
- Committer:
- Cheroukee
- Date:
- 2017-10-02
- Revision:
- 26:cbf539141bfe
- Parent:
- 25:7b808fb9e025
- Child:
- 27:3baf4701a047
File content as of revision 26:cbf539141bfe:
#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); Thread ws_send; void send_to_ws(); typedef struct { char buffer[64]; } ws_message_t; Mail<ws_message_t, 32> messages_box; 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(); set_pan_id(config.pan_id); frame_t current_frame; ws_send.start(callback(send_to_ws)); flipper.attach(&send_del_to_all, 1.0); while(1) { bool finished_packet = receive(¤t_frame, BUFFER_SIZE); if (finished_packet) { process_frame(¤t_frame); } wait_ms(10); } } #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(¤t_frame, BUFFER_SIZE); if (finished_packet) { process_frame(¤t_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; } if (frame->buffer[0] == 0x8B) { if(frame->buffer[5] != 0x00) { error_led = 1; error_ticker.attach(&display_error, 1.0); } return 2; } 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; } ws_message_t* message = messages_box.alloc(); //Data starts at 12 for (int i = 12; i < frame->length; i++) { message->buffer[i - 12] = frame->buffer[i]; pc.putc(frame->buffer[i]); } message->buffer[frame->length - 12] = '\0'; messages_box.put(message); return 1; } return 0; } void send_to_ws() { 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(); osEvent evt; while(1) { if (!ws.is_connected()) { ws.connect(); } evt = messages_box.get(); if (evt.status == osEventMail) { ws_message_t* mail = (ws_message_t*)evt.value.p; //ws.send(mail->buffer); Thread::wait(500); messages_box.free(mail); } else { Thread::wait(100); } } } 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(); }