mini code pour xbee
Dependencies: mbed ConfigFile EthernetInterface WebSocketClient mbed-rtos
Fork of app4Coordo by
main.cpp
- Committer:
- trixrabbit
- Date:
- 2014-02-25
- Revision:
- 6:2ab1318e2b02
- Parent:
- 5:bf1d3d99cfe9
- Child:
- 7:891d50fe1ee5
File content as of revision 6:2ab1318e2b02:
#include "mbed.h" #include <string> #include <sstream> #include "ConfigFile.h" #include "EthernetInterface.h" #include "Websocket.h" // Pins DigitalOut myled(LED1); DigitalOut myled2(LED2); DigitalOut reset(p8); // Serial COMs Serial pc(USBTX, USBRX); Serial xbee(p13, p14); // Variables char url_s[BUFSIZ]; char dataBuf[20]; int dataBuf_i; bool trame_ready; string panID; // Ethernet interface EthernetInterface eth; // Configuration file LocalFileSystem local("local"); ConfigFile cfg; // State machine ENUM enum {STEP_START, STEP_LENGTH, STEP_TYPE, STEP_MAC, STEP_NET, STEP_OPT, STEP_DATA, STEP_CHECK}; void configInit() { char *key1 = "PANID"; char *key2 = "URL"; char value[BUFSIZ]; // Read a configuration file from a mbed. if (!cfg.read("/local/initconf.cfg")) { error("Failure to read a configuration file.\n\r"); } /* * Get a configuration value. */ if (cfg.getValue(key1, &value[0], sizeof(value))) { std::stringstream panID_s(value); panID_s >> panID; pc.printf("'%s'='%s'\n\r", key1, value); } if (cfg.getValue(key2, &value[0], sizeof(value))) { memcpy(url_s,value,BUFSIZ); } } void eth_ws_init(Websocket* ws) { eth.init(); eth.connect(); pc.printf("IP Address is %s\n\r", eth.getIPAddress()); wait(2); if(!ws->connect()) { error("Failure establish connection to the WebSocket.\n\r"); } } void xbee_init() { reset = 0; wait_ms(400); reset = 1; wait(4); 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) { panID_size++; panID = "0" + panID; } panID_d = strtol(panID.c_str(),NULL,16); //converti le panID en chiffre for(i=0;i<panID_size/2;i++) { panID_array[i] = panID_d%256; //decoupe en packet de 8 bytes (2 characteres) panID_d = panID_d >> 8; } i--; //i est incrémentré une fois de trop dans le for avant de sortir de la boucle //construction des 3 trames pour changer le panID char size = panID_size/2 + 0x04; //length + 0x04 char checkSum = 0; xbee.putc(0x7E); // start delimeter xbee.putc(0x00); // length xbee.putc(size); // length xbee.putc(0x09); // frame type : AT command queue xbee.putc(0x47); // frame ID xbee.putc(0x49); //AT command : (ID) xbee.putc(0x44); //AT command : (ID) 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 xbee.putc(0x04); // length xbee.putc(0x09); // frame type : AT command queue xbee.putc(0x49); // frame ID xbee.putc(0x57); //AT command : (WR) xbee.putc(0x52); //AT command : (WR) xbee.putc(0x04); //checksum xbee.putc(0x7E); // start delimeter xbee.putc(0x00); // length xbee.putc(0x04); // length xbee.putc(0x09); // frame type : AT command xbee.putc(0x4A); // frame ID xbee.putc(0x41); //AT command : (AC) xbee.putc(0x43); //AT command : (AC) xbee.putc(0x28); //checksum } void xbee_receive() { static int state = STEP_START; char data = xbee.getc(); static int length_i; static int length; static int mac_i; static int net_i; switch(state) { case STEP_START: if(data == 0x7E) { state = STEP_LENGTH; length_i = 0; length = 0; dataBuf_i = 0; } break; case STEP_LENGTH: length += data; length_i++; if(length_i == 2) { state = STEP_TYPE; length -= 12; } break; case STEP_TYPE: if(data == 0x90) //Receive packet { state = STEP_MAC; mac_i = 0; } else { state = STEP_START; } break; case STEP_MAC: mac_i++; if(mac_i == 8) { state = STEP_NET; net_i = 0; } break; case STEP_NET: net_i++; if(net_i == 2) { state = STEP_OPT; } break; case STEP_OPT: if(data == 0x01) { state = STEP_DATA; } else { state = STEP_START; } break; case STEP_DATA: length--; dataBuf[dataBuf_i] = data; dataBuf_i++; if(length == 0) { dataBuf[dataBuf_i] = '\0'; state = STEP_CHECK; trame_ready = true; } break; case STEP_CHECK: //check CS state = STEP_START; break; } } int main() { configInit(); Websocket ws(url_s); eth_ws_init(&ws); xbee_init(); myled = 1; myled2 = 1; trame_ready = false; while(1) { if(xbee.readable()) { xbee_receive(); myled2 = ! myled2; } if(trame_ready == true) { ws.send(dataBuf); //pc.printf(dataBuf); //pc.printf("\n\r"); trame_ready = false; } } //ws.close(); // eth.disconnect(); }