mini code pour xbee

Dependencies:   mbed ConfigFile EthernetInterface WebSocketClient mbed-rtos

Fork of app4Coordo by APP Team

Committer:
passelin
Date:
Tue Feb 25 18:11:05 2014 +0000
Revision:
7:891d50fe1ee5
Parent:
6:2ab1318e2b02
Comments added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
passelin 7:891d50fe1ee5 1 /*******************************************************************************************
passelin 7:891d50fe1ee5 2 * FILE: Coordonator
passelin 7:891d50fe1ee5 3 * File name: main.cpp
passelin 7:891d50fe1ee5 4 * Authors: Pierluc Asselin & Marc-Antoine Dupuis
passelin 7:891d50fe1ee5 5 * Object: S5 Info - APP5
passelin 7:891d50fe1ee5 6 * Original Date: 22/02/2014
passelin 7:891d50fe1ee5 7 * Last Modified: 25/02/2014
passelin 7:891d50fe1ee5 8 ********************************************************************************************/
passelin 7:891d50fe1ee5 9
passelin 7:891d50fe1ee5 10
passelin 7:891d50fe1ee5 11 /*******************
passelin 7:891d50fe1ee5 12 INCLUDES
passelin 7:891d50fe1ee5 13 *******************/
passelin 0:ed0e18b7fa30 14 #include "mbed.h"
passelin 2:5942af2ffe8b 15 #include <string>
trixrabbit 6:2ab1318e2b02 16 #include <sstream>
passelin 3:85025db3fbd1 17 #include "ConfigFile.h"
passelin 4:7d7d6e4b6810 18 #include "EthernetInterface.h"
passelin 4:7d7d6e4b6810 19 #include "Websocket.h"
passelin 0:ed0e18b7fa30 20
passelin 7:891d50fe1ee5 21
passelin 7:891d50fe1ee5 22 /*******************
passelin 7:891d50fe1ee5 23 PINS
passelin 7:891d50fe1ee5 24 *******************/
passelin 0:ed0e18b7fa30 25 DigitalOut myled(LED1);
passelin 1:568707763458 26 DigitalOut myled2(LED2);
passelin 0:ed0e18b7fa30 27 DigitalOut reset(p8);
passelin 4:7d7d6e4b6810 28
passelin 7:891d50fe1ee5 29
passelin 7:891d50fe1ee5 30 /*******************
passelin 7:891d50fe1ee5 31 SERIAL
passelin 7:891d50fe1ee5 32 *******************/
passelin 0:ed0e18b7fa30 33 Serial pc(USBTX, USBRX);
passelin 0:ed0e18b7fa30 34 Serial xbee(p13, p14);
passelin 0:ed0e18b7fa30 35
passelin 7:891d50fe1ee5 36
passelin 7:891d50fe1ee5 37 /*******************
passelin 7:891d50fe1ee5 38 VARIABLES
passelin 7:891d50fe1ee5 39 *******************/
passelin 5:bf1d3d99cfe9 40 char url_s[BUFSIZ];
passelin 5:bf1d3d99cfe9 41 char dataBuf[20];
passelin 5:bf1d3d99cfe9 42 int dataBuf_i;
passelin 2:5942af2ffe8b 43 bool trame_ready;
trixrabbit 6:2ab1318e2b02 44 string panID;
passelin 2:5942af2ffe8b 45
passelin 7:891d50fe1ee5 46
passelin 7:891d50fe1ee5 47 /*******************
passelin 7:891d50fe1ee5 48 ETHERNET
passelin 7:891d50fe1ee5 49 *******************/
passelin 4:7d7d6e4b6810 50 EthernetInterface eth;
passelin 4:7d7d6e4b6810 51
passelin 7:891d50fe1ee5 52
passelin 7:891d50fe1ee5 53 /*******************
passelin 7:891d50fe1ee5 54 CONFIG FILE
passelin 7:891d50fe1ee5 55 *******************/
passelin 3:85025db3fbd1 56 LocalFileSystem local("local");
passelin 3:85025db3fbd1 57 ConfigFile cfg;
passelin 3:85025db3fbd1 58
passelin 7:891d50fe1ee5 59 /*******************
passelin 7:891d50fe1ee5 60 STATE MACHINE ENUM
passelin 7:891d50fe1ee5 61 *******************/
passelin 2:5942af2ffe8b 62 enum {STEP_START, STEP_LENGTH, STEP_TYPE, STEP_MAC, STEP_NET, STEP_OPT, STEP_DATA, STEP_CHECK};
passelin 2:5942af2ffe8b 63
passelin 7:891d50fe1ee5 64
passelin 7:891d50fe1ee5 65 /*******************
passelin 7:891d50fe1ee5 66 configInit
passelin 7:891d50fe1ee5 67 This function get the information from the configuration init file.
passelin 7:891d50fe1ee5 68 *******************/
passelin 3:85025db3fbd1 69 void configInit()
passelin 3:85025db3fbd1 70 {
passelin 3:85025db3fbd1 71 char *key1 = "PANID";
passelin 3:85025db3fbd1 72 char *key2 = "URL";
passelin 3:85025db3fbd1 73 char value[BUFSIZ];
passelin 3:85025db3fbd1 74
passelin 3:85025db3fbd1 75 // Read a configuration file from a mbed.
passelin 3:85025db3fbd1 76 if (!cfg.read("/local/initconf.cfg"))
passelin 3:85025db3fbd1 77 {
passelin 5:bf1d3d99cfe9 78 error("Failure to read a configuration file.\n\r");
passelin 3:85025db3fbd1 79 }
passelin 3:85025db3fbd1 80
passelin 3:85025db3fbd1 81 /*
passelin 3:85025db3fbd1 82 * Get a configuration value.
passelin 3:85025db3fbd1 83 */
passelin 3:85025db3fbd1 84 if (cfg.getValue(key1, &value[0], sizeof(value)))
passelin 3:85025db3fbd1 85 {
passelin 7:891d50fe1ee5 86 // Get PanID
trixrabbit 6:2ab1318e2b02 87 std::stringstream panID_s(value);
trixrabbit 6:2ab1318e2b02 88 panID_s >> panID;
passelin 3:85025db3fbd1 89 }
passelin 3:85025db3fbd1 90
passelin 3:85025db3fbd1 91 if (cfg.getValue(key2, &value[0], sizeof(value)))
passelin 3:85025db3fbd1 92 {
passelin 7:891d50fe1ee5 93 // Get URL of web socket
passelin 5:bf1d3d99cfe9 94 memcpy(url_s,value,BUFSIZ);
passelin 3:85025db3fbd1 95 }
passelin 3:85025db3fbd1 96 }
passelin 7:891d50fe1ee5 97 /*******************
passelin 7:891d50fe1ee5 98 eth_ws_init
passelin 7:891d50fe1ee5 99 This function initialize the ethernet port and the web socket connection
passelin 7:891d50fe1ee5 100 *******************/
passelin 5:bf1d3d99cfe9 101 void eth_ws_init(Websocket* ws)
passelin 4:7d7d6e4b6810 102 {
passelin 7:891d50fe1ee5 103 // Init and connect to ethernet port
passelin 4:7d7d6e4b6810 104 eth.init();
passelin 4:7d7d6e4b6810 105 eth.connect();
passelin 4:7d7d6e4b6810 106
passelin 7:891d50fe1ee5 107 // Get IP adress
passelin 5:bf1d3d99cfe9 108 pc.printf("IP Address is %s\n\r", eth.getIPAddress());
passelin 4:7d7d6e4b6810 109
passelin 7:891d50fe1ee5 110 // Wait 2 seconds since we just connected to the network
passelin 4:7d7d6e4b6810 111 wait(2);
passelin 4:7d7d6e4b6810 112
passelin 7:891d50fe1ee5 113 // Connect to the web socket
passelin 5:bf1d3d99cfe9 114 if(!ws->connect())
passelin 4:7d7d6e4b6810 115 {
passelin 5:bf1d3d99cfe9 116 error("Failure establish connection to the WebSocket.\n\r");
passelin 4:7d7d6e4b6810 117 }
passelin 4:7d7d6e4b6810 118 }
passelin 7:891d50fe1ee5 119 /*******************
passelin 7:891d50fe1ee5 120 xbee_init
passelin 7:891d50fe1ee5 121 This function initialize the xbee module and set his panID
passelin 7:891d50fe1ee5 122 *******************/
passelin 2:5942af2ffe8b 123 void xbee_init()
passelin 0:ed0e18b7fa30 124 {
passelin 7:891d50fe1ee5 125 // Reset the xbee
passelin 0:ed0e18b7fa30 126 reset = 0;
passelin 1:568707763458 127 wait_ms(400);
passelin 1:568707763458 128 reset = 1;
passelin 7:891d50fe1ee5 129
passelin 7:891d50fe1ee5 130 // Wait for the xbee to be ready to receive data
trixrabbit 6:2ab1318e2b02 131 wait(4);
passelin 7:891d50fe1ee5 132
passelin 7:891d50fe1ee5 133 // Build trame and send the PanID to the xbee
trixrabbit 6:2ab1318e2b02 134 int panID_size;
trixrabbit 6:2ab1318e2b02 135 int panID_array[8];
trixrabbit 6:2ab1318e2b02 136 int i;
trixrabbit 6:2ab1318e2b02 137 long int panID_d;
passelin 7:891d50fe1ee5 138
trixrabbit 6:2ab1318e2b02 139 panID_size = panID.length(); //vérifie si le panID est pair ou impair
trixrabbit 6:2ab1318e2b02 140 if(panID_size%2 != 0)
trixrabbit 6:2ab1318e2b02 141 {
trixrabbit 6:2ab1318e2b02 142 panID_size++;
trixrabbit 6:2ab1318e2b02 143 panID = "0" + panID;
trixrabbit 6:2ab1318e2b02 144 }
trixrabbit 6:2ab1318e2b02 145
trixrabbit 6:2ab1318e2b02 146 panID_d = strtol(panID.c_str(),NULL,16); //converti le panID en chiffre
trixrabbit 6:2ab1318e2b02 147 for(i=0;i<panID_size/2;i++)
trixrabbit 6:2ab1318e2b02 148 {
trixrabbit 6:2ab1318e2b02 149 panID_array[i] = panID_d%256; //decoupe en packet de 8 bytes (2 characteres)
trixrabbit 6:2ab1318e2b02 150 panID_d = panID_d >> 8;
trixrabbit 6:2ab1318e2b02 151
trixrabbit 6:2ab1318e2b02 152
trixrabbit 6:2ab1318e2b02 153 }
trixrabbit 6:2ab1318e2b02 154 i--; //i est incrémentré une fois de trop dans le for avant de sortir de la boucle
trixrabbit 6:2ab1318e2b02 155
trixrabbit 6:2ab1318e2b02 156
trixrabbit 6:2ab1318e2b02 157 //construction des 3 trames pour changer le panID
trixrabbit 6:2ab1318e2b02 158 char size = panID_size/2 + 0x04; //length + 0x04
trixrabbit 6:2ab1318e2b02 159 char checkSum = 0;
trixrabbit 6:2ab1318e2b02 160 xbee.putc(0x7E); // start delimeter
trixrabbit 6:2ab1318e2b02 161 xbee.putc(0x00); // length
trixrabbit 6:2ab1318e2b02 162 xbee.putc(size); // length
trixrabbit 6:2ab1318e2b02 163 xbee.putc(0x09); // frame type : AT command queue
trixrabbit 6:2ab1318e2b02 164 xbee.putc(0x47); // frame ID
trixrabbit 6:2ab1318e2b02 165 xbee.putc(0x49); //AT command : (ID)
trixrabbit 6:2ab1318e2b02 166 xbee.putc(0x44); //AT command : (ID)
trixrabbit 6:2ab1318e2b02 167 checkSum = checkSum + 0x09 + 0x47 + 0x49 + 0x44;
trixrabbit 6:2ab1318e2b02 168 while(i>=0)
trixrabbit 6:2ab1318e2b02 169 {
trixrabbit 6:2ab1318e2b02 170 xbee.putc(panID_array[i]);
trixrabbit 6:2ab1318e2b02 171 checkSum += panID_array[i];
trixrabbit 6:2ab1318e2b02 172 i--;
trixrabbit 6:2ab1318e2b02 173 }
trixrabbit 6:2ab1318e2b02 174 checkSum = 0xff - checkSum;
trixrabbit 6:2ab1318e2b02 175 xbee.putc(checkSum); // checksum
trixrabbit 6:2ab1318e2b02 176
trixrabbit 6:2ab1318e2b02 177 xbee.putc(0x7E); // start delimeter
trixrabbit 6:2ab1318e2b02 178 xbee.putc(0x00); // length
trixrabbit 6:2ab1318e2b02 179 xbee.putc(0x04); // length
trixrabbit 6:2ab1318e2b02 180 xbee.putc(0x09); // frame type : AT command queue
trixrabbit 6:2ab1318e2b02 181 xbee.putc(0x49); // frame ID
trixrabbit 6:2ab1318e2b02 182 xbee.putc(0x57); //AT command : (WR)
trixrabbit 6:2ab1318e2b02 183 xbee.putc(0x52); //AT command : (WR)
trixrabbit 6:2ab1318e2b02 184 xbee.putc(0x04); //checksum
trixrabbit 6:2ab1318e2b02 185
trixrabbit 6:2ab1318e2b02 186
trixrabbit 6:2ab1318e2b02 187 xbee.putc(0x7E); // start delimeter
trixrabbit 6:2ab1318e2b02 188 xbee.putc(0x00); // length
trixrabbit 6:2ab1318e2b02 189 xbee.putc(0x04); // length
trixrabbit 6:2ab1318e2b02 190 xbee.putc(0x09); // frame type : AT command
trixrabbit 6:2ab1318e2b02 191 xbee.putc(0x4A); // frame ID
trixrabbit 6:2ab1318e2b02 192 xbee.putc(0x41); //AT command : (AC)
trixrabbit 6:2ab1318e2b02 193 xbee.putc(0x43); //AT command : (AC)
trixrabbit 6:2ab1318e2b02 194 xbee.putc(0x28); //checksum
trixrabbit 6:2ab1318e2b02 195
passelin 2:5942af2ffe8b 196 }
passelin 7:891d50fe1ee5 197 /*******************
passelin 7:891d50fe1ee5 198 xbee_receive
passelin 7:891d50fe1ee5 199 This function receive and decode the information from the xbee communication
passelin 7:891d50fe1ee5 200 *******************/
passelin 2:5942af2ffe8b 201 void xbee_receive()
passelin 2:5942af2ffe8b 202 {
passelin 7:891d50fe1ee5 203 // Initialize step-machine
passelin 2:5942af2ffe8b 204 static int state = STEP_START;
passelin 2:5942af2ffe8b 205
passelin 7:891d50fe1ee5 206 // Get the next caracter from xbee
passelin 2:5942af2ffe8b 207 char data = xbee.getc();
passelin 2:5942af2ffe8b 208
passelin 2:5942af2ffe8b 209 static int length_i;
passelin 5:bf1d3d99cfe9 210 static int length;
passelin 5:bf1d3d99cfe9 211 static int mac_i;
passelin 2:5942af2ffe8b 212 static int net_i;
passelin 2:5942af2ffe8b 213
passelin 7:891d50fe1ee5 214 // Step-Machine
passelin 2:5942af2ffe8b 215 switch(state)
passelin 2:5942af2ffe8b 216 {
passelin 7:891d50fe1ee5 217 case STEP_START: if(data == 0x7E) // Verifie if Start byte is OK.
passelin 2:5942af2ffe8b 218 {
passelin 2:5942af2ffe8b 219 state = STEP_LENGTH;
passelin 2:5942af2ffe8b 220 length_i = 0;
passelin 2:5942af2ffe8b 221 length = 0;
passelin 5:bf1d3d99cfe9 222 dataBuf_i = 0;
passelin 2:5942af2ffe8b 223 }
passelin 2:5942af2ffe8b 224 break;
passelin 2:5942af2ffe8b 225
passelin 7:891d50fe1ee5 226 case STEP_LENGTH: length += data; //Read the length in the next 2 bytes.
passelin 2:5942af2ffe8b 227 length_i++;
passelin 2:5942af2ffe8b 228 if(length_i == 2)
passelin 2:5942af2ffe8b 229 {
passelin 2:5942af2ffe8b 230 state = STEP_TYPE;
passelin 2:5942af2ffe8b 231 length -= 12;
passelin 2:5942af2ffe8b 232 }
passelin 2:5942af2ffe8b 233 break;
passelin 2:5942af2ffe8b 234
passelin 7:891d50fe1ee5 235 case STEP_TYPE: if(data == 0x90) // Verifie if Type byte is OK.
passelin 2:5942af2ffe8b 236 {
passelin 2:5942af2ffe8b 237 state = STEP_MAC;
passelin 2:5942af2ffe8b 238 mac_i = 0;
passelin 2:5942af2ffe8b 239 }
passelin 2:5942af2ffe8b 240 else
passelin 2:5942af2ffe8b 241 {
passelin 2:5942af2ffe8b 242 state = STEP_START;
passelin 2:5942af2ffe8b 243 }
passelin 2:5942af2ffe8b 244 break;
passelin 2:5942af2ffe8b 245
passelin 7:891d50fe1ee5 246 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
passelin 2:5942af2ffe8b 247 if(mac_i == 8)
passelin 2:5942af2ffe8b 248 {
passelin 2:5942af2ffe8b 249 state = STEP_NET;
passelin 2:5942af2ffe8b 250 net_i = 0;
passelin 2:5942af2ffe8b 251 }
passelin 2:5942af2ffe8b 252 break;
passelin 2:5942af2ffe8b 253
passelin 7:891d50fe1ee5 254 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
passelin 2:5942af2ffe8b 255 if(net_i == 2)
passelin 2:5942af2ffe8b 256 {
passelin 2:5942af2ffe8b 257 state = STEP_OPT;
passelin 2:5942af2ffe8b 258 }
passelin 2:5942af2ffe8b 259 break;
passelin 2:5942af2ffe8b 260
passelin 7:891d50fe1ee5 261 case STEP_OPT: if(data == 0x01) // Verifie that the option bytes is OK
passelin 2:5942af2ffe8b 262 {
passelin 2:5942af2ffe8b 263 state = STEP_DATA;
passelin 2:5942af2ffe8b 264 }
passelin 2:5942af2ffe8b 265 else
passelin 2:5942af2ffe8b 266 {
passelin 2:5942af2ffe8b 267 state = STEP_START;
passelin 2:5942af2ffe8b 268 }
passelin 2:5942af2ffe8b 269 break;
passelin 2:5942af2ffe8b 270
passelin 7:891d50fe1ee5 271 case STEP_DATA: length--; // Get the data. The number of bytes to read is given by the length previously detected.
passelin 5:bf1d3d99cfe9 272 dataBuf[dataBuf_i] = data;
passelin 5:bf1d3d99cfe9 273 dataBuf_i++;
passelin 2:5942af2ffe8b 274 if(length == 0)
passelin 2:5942af2ffe8b 275 {
passelin 5:bf1d3d99cfe9 276 dataBuf[dataBuf_i] = '\0';
passelin 2:5942af2ffe8b 277 state = STEP_CHECK;
passelin 2:5942af2ffe8b 278 trame_ready = true;
passelin 2:5942af2ffe8b 279 }
passelin 2:5942af2ffe8b 280 break;
passelin 2:5942af2ffe8b 281
passelin 7:891d50fe1ee5 282 case STEP_CHECK: //check ChecksumS
passelin 2:5942af2ffe8b 283 state = STEP_START;
passelin 4:7d7d6e4b6810 284 break;
passelin 2:5942af2ffe8b 285 }
passelin 2:5942af2ffe8b 286
passelin 2:5942af2ffe8b 287 }
passelin 7:891d50fe1ee5 288 /*******************
passelin 7:891d50fe1ee5 289 main
passelin 7:891d50fe1ee5 290 This is the main function of the coordinator program
passelin 7:891d50fe1ee5 291 *******************/
passelin 2:5942af2ffe8b 292 int main()
passelin 2:5942af2ffe8b 293 {
passelin 7:891d50fe1ee5 294 // Get the data from config init file
passelin 3:85025db3fbd1 295 configInit();
passelin 5:bf1d3d99cfe9 296
passelin 7:891d50fe1ee5 297 // Create the web socket with the specified URl from the cofig init file
passelin 5:bf1d3d99cfe9 298 Websocket ws(url_s);
passelin 7:891d50fe1ee5 299
passelin 7:891d50fe1ee5 300 // Initialize the ethernet port and the web socket
passelin 5:bf1d3d99cfe9 301 eth_ws_init(&ws);
passelin 5:bf1d3d99cfe9 302
passelin 7:891d50fe1ee5 303 // initialize the xbee communication and the PAN
passelin 2:5942af2ffe8b 304 xbee_init();
passelin 2:5942af2ffe8b 305
passelin 7:891d50fe1ee5 306 // Light on SENDING led and RECEIVING led (led1, led2)
passelin 2:5942af2ffe8b 307 myled = 1;
passelin 2:5942af2ffe8b 308 myled2 = 1;
passelin 2:5942af2ffe8b 309
passelin 7:891d50fe1ee5 310 // initialise the confimation received flag
passelin 2:5942af2ffe8b 311 trame_ready = false;
passelin 2:5942af2ffe8b 312
passelin 7:891d50fe1ee5 313 //Main loop
passelin 0:ed0e18b7fa30 314 while(1)
passelin 0:ed0e18b7fa30 315 {
passelin 7:891d50fe1ee5 316 // If data is received from the xbee, toggle RECEIVING led and analyse the data
passelin 0:ed0e18b7fa30 317 if(xbee.readable())
passelin 0:ed0e18b7fa30 318 {
passelin 2:5942af2ffe8b 319 xbee_receive();
passelin 1:568707763458 320 myled2 = ! myled2;
passelin 0:ed0e18b7fa30 321 }
passelin 7:891d50fe1ee5 322
passelin 7:891d50fe1ee5 323 // if received trame is complete, send it to the web socket and reset the confirmation flag.
passelin 2:5942af2ffe8b 324 if(trame_ready == true)
passelin 5:bf1d3d99cfe9 325 {
passelin 7:891d50fe1ee5 326 ws.send(dataBuf);
passelin 2:5942af2ffe8b 327 trame_ready = false;
passelin 2:5942af2ffe8b 328 }
passelin 0:ed0e18b7fa30 329 }
passelin 0:ed0e18b7fa30 330 }