mini code pour xbee
Dependencies: mbed ConfigFile EthernetInterface WebSocketClient mbed-rtos
Fork of app4Coordo by
main.cpp@6:2ab1318e2b02, 2014-02-25 (annotated)
- Committer:
- trixrabbit
- Date:
- Tue Feb 25 15:15:11 2014 +0000
- Revision:
- 6:2ab1318e2b02
- Parent:
- 5:bf1d3d99cfe9
- Child:
- 7:891d50fe1ee5
avec ajout de panId dans config
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
passelin | 0:ed0e18b7fa30 | 1 | #include "mbed.h" |
passelin | 2:5942af2ffe8b | 2 | #include <string> |
trixrabbit | 6:2ab1318e2b02 | 3 | #include <sstream> |
passelin | 3:85025db3fbd1 | 4 | #include "ConfigFile.h" |
passelin | 4:7d7d6e4b6810 | 5 | #include "EthernetInterface.h" |
passelin | 4:7d7d6e4b6810 | 6 | #include "Websocket.h" |
passelin | 0:ed0e18b7fa30 | 7 | |
passelin | 4:7d7d6e4b6810 | 8 | // Pins |
passelin | 0:ed0e18b7fa30 | 9 | DigitalOut myled(LED1); |
passelin | 1:568707763458 | 10 | DigitalOut myled2(LED2); |
passelin | 0:ed0e18b7fa30 | 11 | DigitalOut reset(p8); |
passelin | 4:7d7d6e4b6810 | 12 | |
passelin | 4:7d7d6e4b6810 | 13 | // Serial COMs |
passelin | 0:ed0e18b7fa30 | 14 | Serial pc(USBTX, USBRX); |
passelin | 0:ed0e18b7fa30 | 15 | Serial xbee(p13, p14); |
passelin | 0:ed0e18b7fa30 | 16 | |
passelin | 4:7d7d6e4b6810 | 17 | // Variables |
passelin | 5:bf1d3d99cfe9 | 18 | char url_s[BUFSIZ]; |
passelin | 5:bf1d3d99cfe9 | 19 | char dataBuf[20]; |
passelin | 5:bf1d3d99cfe9 | 20 | int dataBuf_i; |
passelin | 2:5942af2ffe8b | 21 | bool trame_ready; |
trixrabbit | 6:2ab1318e2b02 | 22 | string panID; |
passelin | 2:5942af2ffe8b | 23 | |
passelin | 4:7d7d6e4b6810 | 24 | // Ethernet interface |
passelin | 4:7d7d6e4b6810 | 25 | EthernetInterface eth; |
passelin | 4:7d7d6e4b6810 | 26 | |
passelin | 4:7d7d6e4b6810 | 27 | // Configuration file |
passelin | 3:85025db3fbd1 | 28 | LocalFileSystem local("local"); |
passelin | 3:85025db3fbd1 | 29 | ConfigFile cfg; |
passelin | 3:85025db3fbd1 | 30 | |
passelin | 4:7d7d6e4b6810 | 31 | // State machine ENUM |
passelin | 2:5942af2ffe8b | 32 | enum {STEP_START, STEP_LENGTH, STEP_TYPE, STEP_MAC, STEP_NET, STEP_OPT, STEP_DATA, STEP_CHECK}; |
passelin | 2:5942af2ffe8b | 33 | |
passelin | 3:85025db3fbd1 | 34 | void configInit() |
passelin | 3:85025db3fbd1 | 35 | { |
passelin | 3:85025db3fbd1 | 36 | char *key1 = "PANID"; |
passelin | 3:85025db3fbd1 | 37 | char *key2 = "URL"; |
passelin | 3:85025db3fbd1 | 38 | char value[BUFSIZ]; |
passelin | 3:85025db3fbd1 | 39 | |
passelin | 3:85025db3fbd1 | 40 | // Read a configuration file from a mbed. |
passelin | 3:85025db3fbd1 | 41 | if (!cfg.read("/local/initconf.cfg")) |
passelin | 3:85025db3fbd1 | 42 | { |
passelin | 5:bf1d3d99cfe9 | 43 | error("Failure to read a configuration file.\n\r"); |
passelin | 3:85025db3fbd1 | 44 | } |
passelin | 3:85025db3fbd1 | 45 | |
passelin | 3:85025db3fbd1 | 46 | /* |
passelin | 3:85025db3fbd1 | 47 | * Get a configuration value. |
passelin | 3:85025db3fbd1 | 48 | */ |
passelin | 3:85025db3fbd1 | 49 | if (cfg.getValue(key1, &value[0], sizeof(value))) |
passelin | 3:85025db3fbd1 | 50 | { |
trixrabbit | 6:2ab1318e2b02 | 51 | std::stringstream panID_s(value); |
trixrabbit | 6:2ab1318e2b02 | 52 | panID_s >> panID; |
passelin | 5:bf1d3d99cfe9 | 53 | pc.printf("'%s'='%s'\n\r", key1, value); |
passelin | 3:85025db3fbd1 | 54 | } |
passelin | 3:85025db3fbd1 | 55 | |
passelin | 3:85025db3fbd1 | 56 | if (cfg.getValue(key2, &value[0], sizeof(value))) |
passelin | 3:85025db3fbd1 | 57 | { |
passelin | 5:bf1d3d99cfe9 | 58 | memcpy(url_s,value,BUFSIZ); |
passelin | 3:85025db3fbd1 | 59 | } |
passelin | 3:85025db3fbd1 | 60 | } |
passelin | 3:85025db3fbd1 | 61 | |
passelin | 5:bf1d3d99cfe9 | 62 | void eth_ws_init(Websocket* ws) |
passelin | 4:7d7d6e4b6810 | 63 | { |
passelin | 4:7d7d6e4b6810 | 64 | eth.init(); |
passelin | 4:7d7d6e4b6810 | 65 | eth.connect(); |
passelin | 4:7d7d6e4b6810 | 66 | |
passelin | 5:bf1d3d99cfe9 | 67 | pc.printf("IP Address is %s\n\r", eth.getIPAddress()); |
passelin | 4:7d7d6e4b6810 | 68 | |
passelin | 4:7d7d6e4b6810 | 69 | wait(2); |
passelin | 4:7d7d6e4b6810 | 70 | |
passelin | 5:bf1d3d99cfe9 | 71 | if(!ws->connect()) |
passelin | 4:7d7d6e4b6810 | 72 | { |
passelin | 5:bf1d3d99cfe9 | 73 | error("Failure establish connection to the WebSocket.\n\r"); |
passelin | 4:7d7d6e4b6810 | 74 | } |
passelin | 4:7d7d6e4b6810 | 75 | } |
passelin | 4:7d7d6e4b6810 | 76 | |
passelin | 2:5942af2ffe8b | 77 | void xbee_init() |
passelin | 0:ed0e18b7fa30 | 78 | { |
passelin | 0:ed0e18b7fa30 | 79 | reset = 0; |
passelin | 1:568707763458 | 80 | wait_ms(400); |
passelin | 1:568707763458 | 81 | reset = 1; |
trixrabbit | 6:2ab1318e2b02 | 82 | wait(4); |
trixrabbit | 6:2ab1318e2b02 | 83 | int panID_size; |
trixrabbit | 6:2ab1318e2b02 | 84 | int panID_array[8]; |
trixrabbit | 6:2ab1318e2b02 | 85 | int i; |
trixrabbit | 6:2ab1318e2b02 | 86 | long int panID_d; |
trixrabbit | 6:2ab1318e2b02 | 87 | panID_size = panID.length(); //vérifie si le panID est pair ou impair |
trixrabbit | 6:2ab1318e2b02 | 88 | if(panID_size%2 != 0) |
trixrabbit | 6:2ab1318e2b02 | 89 | { |
trixrabbit | 6:2ab1318e2b02 | 90 | panID_size++; |
trixrabbit | 6:2ab1318e2b02 | 91 | panID = "0" + panID; |
trixrabbit | 6:2ab1318e2b02 | 92 | } |
trixrabbit | 6:2ab1318e2b02 | 93 | |
trixrabbit | 6:2ab1318e2b02 | 94 | panID_d = strtol(panID.c_str(),NULL,16); //converti le panID en chiffre |
trixrabbit | 6:2ab1318e2b02 | 95 | for(i=0;i<panID_size/2;i++) |
trixrabbit | 6:2ab1318e2b02 | 96 | { |
trixrabbit | 6:2ab1318e2b02 | 97 | panID_array[i] = panID_d%256; //decoupe en packet de 8 bytes (2 characteres) |
trixrabbit | 6:2ab1318e2b02 | 98 | panID_d = panID_d >> 8; |
trixrabbit | 6:2ab1318e2b02 | 99 | |
trixrabbit | 6:2ab1318e2b02 | 100 | |
trixrabbit | 6:2ab1318e2b02 | 101 | } |
trixrabbit | 6:2ab1318e2b02 | 102 | i--; //i est incrémentré une fois de trop dans le for avant de sortir de la boucle |
trixrabbit | 6:2ab1318e2b02 | 103 | |
trixrabbit | 6:2ab1318e2b02 | 104 | |
trixrabbit | 6:2ab1318e2b02 | 105 | //construction des 3 trames pour changer le panID |
trixrabbit | 6:2ab1318e2b02 | 106 | char size = panID_size/2 + 0x04; //length + 0x04 |
trixrabbit | 6:2ab1318e2b02 | 107 | char checkSum = 0; |
trixrabbit | 6:2ab1318e2b02 | 108 | xbee.putc(0x7E); // start delimeter |
trixrabbit | 6:2ab1318e2b02 | 109 | xbee.putc(0x00); // length |
trixrabbit | 6:2ab1318e2b02 | 110 | xbee.putc(size); // length |
trixrabbit | 6:2ab1318e2b02 | 111 | xbee.putc(0x09); // frame type : AT command queue |
trixrabbit | 6:2ab1318e2b02 | 112 | xbee.putc(0x47); // frame ID |
trixrabbit | 6:2ab1318e2b02 | 113 | xbee.putc(0x49); //AT command : (ID) |
trixrabbit | 6:2ab1318e2b02 | 114 | xbee.putc(0x44); //AT command : (ID) |
trixrabbit | 6:2ab1318e2b02 | 115 | checkSum = checkSum + 0x09 + 0x47 + 0x49 + 0x44; |
trixrabbit | 6:2ab1318e2b02 | 116 | while(i>=0) |
trixrabbit | 6:2ab1318e2b02 | 117 | { |
trixrabbit | 6:2ab1318e2b02 | 118 | pc.printf(" \n\r valeur = %d",panID_array[i]); |
trixrabbit | 6:2ab1318e2b02 | 119 | xbee.putc(panID_array[i]); |
trixrabbit | 6:2ab1318e2b02 | 120 | checkSum += panID_array[i]; |
trixrabbit | 6:2ab1318e2b02 | 121 | i--; |
trixrabbit | 6:2ab1318e2b02 | 122 | } |
trixrabbit | 6:2ab1318e2b02 | 123 | checkSum = 0xff - checkSum; |
trixrabbit | 6:2ab1318e2b02 | 124 | xbee.putc(checkSum); // checksum |
trixrabbit | 6:2ab1318e2b02 | 125 | pc.printf("\n\r %d %x", checkSum,checkSum); |
trixrabbit | 6:2ab1318e2b02 | 126 | |
trixrabbit | 6:2ab1318e2b02 | 127 | xbee.putc(0x7E); // start delimeter |
trixrabbit | 6:2ab1318e2b02 | 128 | xbee.putc(0x00); // length |
trixrabbit | 6:2ab1318e2b02 | 129 | xbee.putc(0x04); // length |
trixrabbit | 6:2ab1318e2b02 | 130 | xbee.putc(0x09); // frame type : AT command queue |
trixrabbit | 6:2ab1318e2b02 | 131 | xbee.putc(0x49); // frame ID |
trixrabbit | 6:2ab1318e2b02 | 132 | xbee.putc(0x57); //AT command : (WR) |
trixrabbit | 6:2ab1318e2b02 | 133 | xbee.putc(0x52); //AT command : (WR) |
trixrabbit | 6:2ab1318e2b02 | 134 | xbee.putc(0x04); //checksum |
trixrabbit | 6:2ab1318e2b02 | 135 | |
trixrabbit | 6:2ab1318e2b02 | 136 | |
trixrabbit | 6:2ab1318e2b02 | 137 | xbee.putc(0x7E); // start delimeter |
trixrabbit | 6:2ab1318e2b02 | 138 | xbee.putc(0x00); // length |
trixrabbit | 6:2ab1318e2b02 | 139 | xbee.putc(0x04); // length |
trixrabbit | 6:2ab1318e2b02 | 140 | xbee.putc(0x09); // frame type : AT command |
trixrabbit | 6:2ab1318e2b02 | 141 | xbee.putc(0x4A); // frame ID |
trixrabbit | 6:2ab1318e2b02 | 142 | xbee.putc(0x41); //AT command : (AC) |
trixrabbit | 6:2ab1318e2b02 | 143 | xbee.putc(0x43); //AT command : (AC) |
trixrabbit | 6:2ab1318e2b02 | 144 | xbee.putc(0x28); //checksum |
trixrabbit | 6:2ab1318e2b02 | 145 | |
passelin | 2:5942af2ffe8b | 146 | } |
passelin | 2:5942af2ffe8b | 147 | |
passelin | 2:5942af2ffe8b | 148 | void xbee_receive() |
passelin | 2:5942af2ffe8b | 149 | { |
passelin | 2:5942af2ffe8b | 150 | static int state = STEP_START; |
passelin | 2:5942af2ffe8b | 151 | |
passelin | 2:5942af2ffe8b | 152 | char data = xbee.getc(); |
passelin | 2:5942af2ffe8b | 153 | |
passelin | 2:5942af2ffe8b | 154 | static int length_i; |
passelin | 5:bf1d3d99cfe9 | 155 | static int length; |
passelin | 5:bf1d3d99cfe9 | 156 | static int mac_i; |
passelin | 2:5942af2ffe8b | 157 | static int net_i; |
passelin | 2:5942af2ffe8b | 158 | |
passelin | 2:5942af2ffe8b | 159 | switch(state) |
passelin | 2:5942af2ffe8b | 160 | { |
passelin | 2:5942af2ffe8b | 161 | case STEP_START: if(data == 0x7E) |
passelin | 2:5942af2ffe8b | 162 | { |
passelin | 2:5942af2ffe8b | 163 | state = STEP_LENGTH; |
passelin | 2:5942af2ffe8b | 164 | length_i = 0; |
passelin | 2:5942af2ffe8b | 165 | length = 0; |
passelin | 5:bf1d3d99cfe9 | 166 | dataBuf_i = 0; |
passelin | 2:5942af2ffe8b | 167 | } |
passelin | 2:5942af2ffe8b | 168 | break; |
passelin | 2:5942af2ffe8b | 169 | |
passelin | 2:5942af2ffe8b | 170 | case STEP_LENGTH: length += data; |
passelin | 2:5942af2ffe8b | 171 | length_i++; |
passelin | 2:5942af2ffe8b | 172 | if(length_i == 2) |
passelin | 2:5942af2ffe8b | 173 | { |
passelin | 2:5942af2ffe8b | 174 | state = STEP_TYPE; |
passelin | 2:5942af2ffe8b | 175 | length -= 12; |
passelin | 2:5942af2ffe8b | 176 | } |
passelin | 2:5942af2ffe8b | 177 | break; |
passelin | 2:5942af2ffe8b | 178 | |
passelin | 2:5942af2ffe8b | 179 | case STEP_TYPE: if(data == 0x90) //Receive packet |
passelin | 2:5942af2ffe8b | 180 | { |
passelin | 2:5942af2ffe8b | 181 | state = STEP_MAC; |
passelin | 2:5942af2ffe8b | 182 | mac_i = 0; |
passelin | 2:5942af2ffe8b | 183 | } |
passelin | 2:5942af2ffe8b | 184 | else |
passelin | 2:5942af2ffe8b | 185 | { |
passelin | 2:5942af2ffe8b | 186 | state = STEP_START; |
passelin | 2:5942af2ffe8b | 187 | } |
passelin | 2:5942af2ffe8b | 188 | break; |
passelin | 2:5942af2ffe8b | 189 | |
passelin | 2:5942af2ffe8b | 190 | case STEP_MAC: mac_i++; |
passelin | 2:5942af2ffe8b | 191 | if(mac_i == 8) |
passelin | 2:5942af2ffe8b | 192 | { |
passelin | 2:5942af2ffe8b | 193 | state = STEP_NET; |
passelin | 2:5942af2ffe8b | 194 | net_i = 0; |
passelin | 2:5942af2ffe8b | 195 | } |
passelin | 2:5942af2ffe8b | 196 | break; |
passelin | 2:5942af2ffe8b | 197 | |
passelin | 2:5942af2ffe8b | 198 | case STEP_NET: net_i++; |
passelin | 2:5942af2ffe8b | 199 | if(net_i == 2) |
passelin | 2:5942af2ffe8b | 200 | { |
passelin | 2:5942af2ffe8b | 201 | state = STEP_OPT; |
passelin | 2:5942af2ffe8b | 202 | } |
passelin | 2:5942af2ffe8b | 203 | break; |
passelin | 2:5942af2ffe8b | 204 | |
passelin | 2:5942af2ffe8b | 205 | case STEP_OPT: if(data == 0x01) |
passelin | 2:5942af2ffe8b | 206 | { |
passelin | 2:5942af2ffe8b | 207 | state = STEP_DATA; |
passelin | 2:5942af2ffe8b | 208 | } |
passelin | 2:5942af2ffe8b | 209 | else |
passelin | 2:5942af2ffe8b | 210 | { |
passelin | 2:5942af2ffe8b | 211 | state = STEP_START; |
passelin | 2:5942af2ffe8b | 212 | } |
passelin | 2:5942af2ffe8b | 213 | break; |
passelin | 2:5942af2ffe8b | 214 | |
passelin | 2:5942af2ffe8b | 215 | case STEP_DATA: length--; |
passelin | 5:bf1d3d99cfe9 | 216 | dataBuf[dataBuf_i] = data; |
passelin | 5:bf1d3d99cfe9 | 217 | dataBuf_i++; |
passelin | 2:5942af2ffe8b | 218 | if(length == 0) |
passelin | 2:5942af2ffe8b | 219 | { |
passelin | 5:bf1d3d99cfe9 | 220 | dataBuf[dataBuf_i] = '\0'; |
passelin | 2:5942af2ffe8b | 221 | state = STEP_CHECK; |
passelin | 2:5942af2ffe8b | 222 | trame_ready = true; |
passelin | 2:5942af2ffe8b | 223 | } |
passelin | 2:5942af2ffe8b | 224 | break; |
passelin | 2:5942af2ffe8b | 225 | |
passelin | 2:5942af2ffe8b | 226 | case STEP_CHECK: //check CS |
passelin | 2:5942af2ffe8b | 227 | state = STEP_START; |
passelin | 4:7d7d6e4b6810 | 228 | break; |
passelin | 2:5942af2ffe8b | 229 | } |
passelin | 2:5942af2ffe8b | 230 | |
passelin | 2:5942af2ffe8b | 231 | } |
passelin | 2:5942af2ffe8b | 232 | |
passelin | 2:5942af2ffe8b | 233 | int main() |
passelin | 2:5942af2ffe8b | 234 | { |
passelin | 3:85025db3fbd1 | 235 | |
passelin | 3:85025db3fbd1 | 236 | configInit(); |
passelin | 5:bf1d3d99cfe9 | 237 | |
passelin | 5:bf1d3d99cfe9 | 238 | Websocket ws(url_s); |
passelin | 5:bf1d3d99cfe9 | 239 | eth_ws_init(&ws); |
passelin | 5:bf1d3d99cfe9 | 240 | |
passelin | 2:5942af2ffe8b | 241 | xbee_init(); |
passelin | 2:5942af2ffe8b | 242 | |
passelin | 2:5942af2ffe8b | 243 | |
passelin | 2:5942af2ffe8b | 244 | myled = 1; |
passelin | 2:5942af2ffe8b | 245 | myled2 = 1; |
passelin | 2:5942af2ffe8b | 246 | |
passelin | 2:5942af2ffe8b | 247 | trame_ready = false; |
passelin | 2:5942af2ffe8b | 248 | |
passelin | 0:ed0e18b7fa30 | 249 | while(1) |
passelin | 0:ed0e18b7fa30 | 250 | { |
passelin | 0:ed0e18b7fa30 | 251 | if(xbee.readable()) |
passelin | 0:ed0e18b7fa30 | 252 | { |
passelin | 2:5942af2ffe8b | 253 | xbee_receive(); |
passelin | 1:568707763458 | 254 | myled2 = ! myled2; |
passelin | 0:ed0e18b7fa30 | 255 | } |
passelin | 5:bf1d3d99cfe9 | 256 | |
passelin | 2:5942af2ffe8b | 257 | if(trame_ready == true) |
passelin | 5:bf1d3d99cfe9 | 258 | { |
passelin | 5:bf1d3d99cfe9 | 259 | ws.send(dataBuf); |
passelin | 4:7d7d6e4b6810 | 260 | |
passelin | 5:bf1d3d99cfe9 | 261 | //pc.printf(dataBuf); |
passelin | 5:bf1d3d99cfe9 | 262 | //pc.printf("\n\r"); |
passelin | 2:5942af2ffe8b | 263 | trame_ready = false; |
passelin | 2:5942af2ffe8b | 264 | } |
passelin | 0:ed0e18b7fa30 | 265 | } |
passelin | 4:7d7d6e4b6810 | 266 | |
passelin | 4:7d7d6e4b6810 | 267 | //ws.close(); |
passelin | 4:7d7d6e4b6810 | 268 | // eth.disconnect(); |
passelin | 0:ed0e18b7fa30 | 269 | } |