Code APP3

Dependencies:   mbed EthernetInterface WebSocketClient mbed-rtos BufferedSerial

Fork of APP3_Lab by Jean-Philippe Fournier

Files at this revision

API Documentation at this revision

Comitter:
Cheroukee
Date:
Sun Oct 01 18:53:18 2017 +0000
Parent:
13:5f21dd134bd2
Child:
15:c4d17caf0709
Commit message:
Added pan ID lecture

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
parser.cpp Show annotated file Show diff for this revision Revisions of this file
parser.h Show annotated file Show diff for this revision Revisions of this file
xbee.cpp Show annotated file Show diff for this revision Revisions of this file
xbee.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sun Oct 01 16:34:11 2017 +0000
+++ b/main.cpp	Sun Oct 01 18:53:18 2017 +0000
@@ -14,6 +14,8 @@
 
 void set_remote_xbee_dio4(bool set);
 
+void set_pan_id(long pan_id);
+
 #if IS_COORDINATOR   
 void coordinator();
 #else
@@ -51,7 +53,7 @@
     
     Serial pc(USBTX, USBRX); // tx, rx
     coordinator_config_t config = read_coordinator_config();
-
+    set_pan_id(config.pan_id);
     frame_t current_frame;
     bool toggle_led = false;
 
@@ -81,7 +83,7 @@
 {    
     Serial pc(USBTX, USBRX); // tx, rx
     router_config_t config = read_router_config();
-
+    set_pan_id(config.pan_id);
     char sensor_buffer[64] = {};
     initialize_sensors();
     DECLARE_ADDR64_COORD
@@ -94,7 +96,21 @@
         sprintf(sensor_buffer, "%3.2f%3.2f%3.2f", accel.Accelerometre.x, accel.Accelerometre.y, accel.Accelerometre.z);
 
         transmit_request(sensor_buffer, 15, 0, USE_ADDR64_COORD, USE_ADDR16_UNKNOWN_OR_BCAST);
+        
         wait(config.refresh_freq);
     }
 }
-#endif
\ No newline at end of file
+#endif
+
+void set_pan_id(long pan_id)
+{
+    //Serial pc(USBTX, USBRX); // tx, rx
+    char pan_id_buffer[8] = {0};
+
+    for (int i = 0; i < 8; i++)
+    {
+        pan_id_buffer[i] = 0xFF & (pan_id >> 8 * (7 - i));
+        //pc.printf("0x%x", pan_id_buffer[i]);
+    }
+    at_command_set('I', 'D', pan_id_buffer, 8);
+}
--- a/parser.cpp	Sun Oct 01 16:34:11 2017 +0000
+++ b/parser.cpp	Sun Oct 01 18:53:18 2017 +0000
@@ -1,27 +1,6 @@
 #include "parser.h"
 
 LocalFileSystem local("local");
-/*
-void ReadFile()
-{
-    Serial pc(USBTX, USBRX); // tx, rx
-    FILE *fp = fopen("/local/config.txt", "r");  // Ouvrir config.txt pour lecture seulement
-    if(fp == NULL)
-    {
-        pc.printf("Failed to find configuration file. \n\r");
-    }
-    else
-    {
-        pc.printf("Config file opened. \n\r");
-        if (fscanf(fp,"0x%x", &PanID) > 0)
-        {
-            pc.printf("PanId : 0x%x value : %u\r\n", PanID, PanID); // Display PanId
-        }
-        fscanf(fp,"%s",URL); // read URL        
-        pc.printf("ServeurURL : %s\r\n",URL); // Display URL
-        fclose(fp);
-    }
-} */
 
 coordinator_config_t read_coordinator_config()
 {
--- a/parser.h	Sun Oct 01 16:34:11 2017 +0000
+++ b/parser.h	Sun Oct 01 18:53:18 2017 +0000
@@ -5,17 +5,15 @@
 #include "mbed.h"
 
 typedef struct{
-    unsigned int pan_id;
+    long pan_id;
     char server_url[255];
 } coordinator_config_t;
 
 typedef struct {
-    unsigned int pan_id;
+    long pan_id;
     char refresh_freq;
 } router_config_t;
 
-//void ReadFile();
-
 coordinator_config_t read_coordinator_config();
 
 router_config_t read_router_config();
--- a/xbee.cpp	Sun Oct 01 16:34:11 2017 +0000
+++ b/xbee.cpp	Sun Oct 01 18:53:18 2017 +0000
@@ -27,18 +27,6 @@
     //xbee_receive_led = 1;    
 }
 
-
-bool set_pan_id(long pan_id)
-{
-    return false;
-}
-
-long get_pan_id()
-{
-    return 0L;
-}
-
-
 void build_api_frame_header(int frame_data_size)
 {
     // Delimiter
@@ -231,10 +219,46 @@
     transmit(3 + REMOTE_AT_OVERHEAD_LENGTH); 
 }
 
+
+void at_command_query(char msb, char lsb)
+{
+    char command[2] = {msb, lsb};
+    
+    build_api_frame_header(2 + REMOTE_AT_OVERHEAD_LENGTH);
+
+    set_api_frame_type(ATCommand);
+    set_api_frame_id(0x01);
+
+    set_transmit_data(7, command, 2);
+
+    build_api_frame_checksum(2 + REMOTE_AT_OVERHEAD_LENGTH);    
+
+    transmit(2 + REMOTE_AT_OVERHEAD_LENGTH); 
+}
+
+void at_command_set(char msb, char lsb, char* parameter, int parameter_length)
+{
+    char command[2] = {msb, lsb};
+    
+    build_api_frame_header(parameter_length + AT_COMMAND_OVERHEAD_LENGTH);
+
+    set_api_frame_type(ATCommand);
+    set_api_frame_id(0x01);
+
+    set_transmit_data(5, command, 2);
+    set_transmit_data(7, parameter, parameter_length);
+
+    build_api_frame_checksum(parameter_length + AT_COMMAND_OVERHEAD_LENGTH);    
+
+    transmit(parameter_length + AT_COMMAND_OVERHEAD_LENGTH); 
+}
+
 void transmit(int packet_length)
 {
+    //Serial pc(USBTX, USBRX); // tx, rx
     for (int i = 0; i < packet_length + 4; i++)
     {
+        //pc.printf(" 0x%x", TransmitBuffer[i]);
         xbee.putc(TransmitBuffer[i]);
     }
     xbee_transmit_led = !xbee_transmit_led;
@@ -304,7 +328,6 @@
     return finished_frame;    
 }
 
-
 /**
  * 
  *   // Calculate checksum
--- a/xbee.h	Sun Oct 01 16:34:11 2017 +0000
+++ b/xbee.h	Sun Oct 01 18:53:18 2017 +0000
@@ -91,6 +91,7 @@
 #define TRANSMIT_REQ_OVERHEAD_LENGTH 14
 
 #define REMOTE_AT_OVERHEAD_LENGTH 13
+#define AT_COMMAND_OVERHEAD_LENGTH 4
 
 #define AT_COMMAND_DIO4_MSB 'D'
 #define AT_COMMAND_DIO4_LSB '4'
@@ -120,11 +121,6 @@
 // Init xbee communication
 void xbee_init();
 
-// Set le PAN ID du Xbee
-bool set_pan_id(long pan_id);
-// Recupere le PAN ID du Xbee
-long get_pan_id();
-
 // insere le api frame header dans le buffer
 void build_api_frame_header(int frame_data_size);
 // insere le api frame cheksum dans le buffer
@@ -153,7 +149,10 @@
 bool transmit_request_16(char* buffer, int data_length, char options, zigbee_addr_16_t destination);
 bool transmit_request(char* buffer, int data_length, char options, zigbee_addr_64_t destination64, zigbee_addr_16_t destination16);
 
-// Envoie d'un remote at command (set ou query)
+void at_command_query(char msb, char lsb);
+void at_command_set(char msb, char lsb, char* parameter, int parameter_length);
+
+// Envoie d'un remote at command (set ou query) en broadcast
 void remote_at_command_query(char msb, char lsb, char options);
 void remote_at_command_set(char msb, char lsb, char parameter, char options);