mini code pour xbee

Dependencies:   mbed ConfigFile EthernetInterface WebSocketClient mbed-rtos

Fork of app4Coordo by APP Team

Files at this revision

API Documentation at this revision

Comitter:
passelin
Date:
Tue Feb 25 18:11:05 2014 +0000
Parent:
6:2ab1318e2b02
Commit message:
Comments added

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Feb 25 15:15:11 2014 +0000
+++ b/main.cpp	Tue Feb 25 18:11:05 2014 +0000
@@ -1,3 +1,16 @@
+/*******************************************************************************************
+* FILE: Coordonator
+* File name: main.cpp
+* Authors: Pierluc Asselin & Marc-Antoine Dupuis
+* Object: S5 Info - APP5
+* Original Date: 22/02/2014
+* Last Modified: 25/02/2014
+********************************************************************************************/
+
+
+/*******************
+    INCLUDES
+*******************/
 #include "mbed.h"
 #include <string>
 #include <sstream>
@@ -5,32 +18,54 @@
 #include "EthernetInterface.h"
 #include "Websocket.h"
 
-// Pins
+
+/*******************
+    PINS
+*******************/
 DigitalOut myled(LED1);
 DigitalOut myled2(LED2);
 DigitalOut reset(p8);
 
-// Serial COMs
+
+/*******************
+    SERIAL
+*******************/
 Serial pc(USBTX, USBRX);
 Serial xbee(p13, p14);
 
-// Variables
+
+/*******************
+    VARIABLES
+*******************/
 char url_s[BUFSIZ];
 char dataBuf[20];
 int dataBuf_i;
 bool trame_ready;
 string panID;
 
-// Ethernet interface
+
+/*******************
+    ETHERNET
+*******************/
 EthernetInterface eth;
 
-// Configuration file
+
+/*******************
+    CONFIG FILE
+*******************/
 LocalFileSystem local("local");
 ConfigFile cfg;
 
-// State machine ENUM
+/*******************
+ STATE MACHINE ENUM
+*******************/
 enum {STEP_START, STEP_LENGTH, STEP_TYPE, STEP_MAC, STEP_NET, STEP_OPT, STEP_DATA, STEP_CHECK};
 
+
+/*******************
+configInit
+This function get the information from the configuration init file.
+*******************/
 void configInit()
 {
     char *key1 = "PANID";
@@ -48,42 +83,59 @@
      */
     if (cfg.getValue(key1, &value[0], sizeof(value))) 
     {
+        // Get PanID
         std::stringstream panID_s(value); 
         panID_s >> panID;
-        pc.printf("'%s'='%s'\n\r", key1, value);
     }
     
     if (cfg.getValue(key2, &value[0], sizeof(value))) 
     {
+        // Get URL of web socket
         memcpy(url_s,value,BUFSIZ);
     }
 }
-
+/*******************
+eth_ws_init
+This function initialize the ethernet port and the web socket connection
+*******************/
 void eth_ws_init(Websocket* ws)
 {
+    // Init and connect to ethernet port
     eth.init();
     eth.connect();
     
+    // Get IP adress
     pc.printf("IP Address is %s\n\r", eth.getIPAddress());
     
+    // Wait 2 seconds since we just connected to the network
     wait(2);
 
+    // Connect to the web socket
     if(!ws->connect())
     {
          error("Failure establish connection to the WebSocket.\n\r");
     }
 }
-
+/*******************
+xbee_init
+This function initialize the xbee module and set his panID
+*******************/
 void xbee_init()
 {
+    // Reset the xbee
     reset = 0;
     wait_ms(400);
     reset = 1;
+    
+    // Wait for the xbee to be ready to receive data    
     wait(4);
+    
+    // Build trame and send the PanID to the xbee
     int panID_size;
     int panID_array[8];
     int i;
     long int panID_d;
+    
     panID_size = panID.length(); //vérifie si le panID est pair ou impair
     if(panID_size%2 != 0)
     {
@@ -115,14 +167,12 @@
     checkSum = checkSum + 0x09 + 0x47 + 0x49 + 0x44;
     while(i>=0)
     {
-        pc.printf(" \n\r valeur = %d",panID_array[i]);
         xbee.putc(panID_array[i]);
         checkSum += panID_array[i];
         i--;
     }
     checkSum = 0xff - checkSum;
     xbee.putc(checkSum); // checksum
-    pc.printf("\n\r %d %x", checkSum,checkSum);
 
     xbee.putc(0x7E); // start delimeter
     xbee.putc(0x00); // length
@@ -144,11 +194,16 @@
     xbee.putc(0x28); //checksum
 
 }
-
+/*******************
+xbee_receive
+This function receive and decode the information from the xbee communication
+*******************/
 void xbee_receive()
 {
+    // Initialize step-machine
     static int state = STEP_START;
     
+    // Get the next caracter from xbee
     char data = xbee.getc();
     
     static int length_i;
@@ -156,9 +211,10 @@
     static int mac_i;   
     static int net_i;
     
+    // Step-Machine
     switch(state)
     {
-        case STEP_START:    if(data == 0x7E)
+        case STEP_START:    if(data == 0x7E) // Verifie if Start byte is OK.
                             {
                                 state = STEP_LENGTH;
                                 length_i = 0;
@@ -167,7 +223,7 @@
                             }       
                             break;
                             
-        case STEP_LENGTH:   length += data; 
+        case STEP_LENGTH:   length += data; //Read the length in the next 2 bytes.
                             length_i++;
                             if(length_i == 2)
                             {
@@ -176,7 +232,7 @@
                             }
                             break;
                             
-        case STEP_TYPE:     if(data == 0x90) //Receive packet
+        case STEP_TYPE:     if(data == 0x90) // Verifie if Type byte is OK.
                             {
                                 state = STEP_MAC;  
                                 mac_i = 0;  
@@ -187,7 +243,7 @@
                             }
                             break;
                             
-        case STEP_MAC:      mac_i++;
+        case STEP_MAC:      mac_i++;    // Skip the next 8 bytes. We do not need the 64bits MAC adresse since we already receive the data. xbee already accepted it
                             if(mac_i == 8)
                             {
                                 state = STEP_NET;
@@ -195,14 +251,14 @@
                             }
                             break;
                             
-        case STEP_NET:      net_i++;
+        case STEP_NET:      net_i++; // Skip the next 2 bytes. We do not need the 16bits adresse since we already receive the data. xbee already accepted it
                             if(net_i == 2)
                             {
                                 state = STEP_OPT;
                             }
                             break;
                             
-        case STEP_OPT:      if(data == 0x01)
+        case STEP_OPT:      if(data == 0x01)  // Verifie that the option bytes is OK
                             {
                                 state = STEP_DATA;    
                             }
@@ -212,7 +268,7 @@
                             }
                             break;                     
                             
-        case STEP_DATA:     length--;
+        case STEP_DATA:     length--;   // Get the data. The number of bytes to read is given by the length previously detected.
                             dataBuf[dataBuf_i] = data;
                             dataBuf_i++;
                             if(length == 0)
@@ -223,47 +279,52 @@
                             }
                             break;
                             
-        case STEP_CHECK:    //check CS
+        case STEP_CHECK:    //check ChecksumS
                             state = STEP_START;
                             break;                            
     }
   
 }
-
+/*******************
+main
+This is the main function of the coordinator program
+*******************/
 int main() 
 {
-    
+    // Get the data from config init file
     configInit();
 
+    // Create the web socket with the specified URl from the cofig init file
     Websocket ws(url_s);
+    
+    // Initialize the ethernet port and the web socket
     eth_ws_init(&ws);
     
+    // initialize the xbee communication and the PAN
     xbee_init();
 
-    
+    // Light on SENDING led and RECEIVING led (led1, led2)
     myled = 1;
     myled2 = 1;
     
+    // initialise the confimation received flag
     trame_ready = false;
     
+    //Main loop
     while(1) 
     {
+        // If data is received from the xbee, toggle RECEIVING led and analyse the data
         if(xbee.readable())
         {
             xbee_receive();
             myled2 = ! myled2;
         }
-
+        
+        // if received trame is complete, send it to the web socket and reset the confirmation flag.
         if(trame_ready == true)
         {          
-            ws.send(dataBuf);         
-            
-            //pc.printf(dataBuf);
-            //pc.printf("\n\r");     
+            ws.send(dataBuf);           
             trame_ready = false;
         }
     }
-    
-    //ws.close();
-   // eth.disconnect();
 }