Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: ConfigFile EthernetInterface mbed-rtos mbed
Fork of S05APP3 by
Diff: main.cpp
- Revision:
- 18:0c71241eab4f
- Parent:
- 17:7ae8f91f46a2
- Child:
- 19:19caa5f5c5f5
--- a/main.cpp Tue Feb 14 03:51:26 2017 +0000 +++ b/main.cpp Tue Feb 14 06:16:43 2017 +0000 @@ -15,7 +15,7 @@ EthernetInterface eth; Serial xbee(p13, p14); // tx, rx Serial pc(USBTX, USBRX); // tx, rx -TCPSocketConnection sock; +TCPSocketConnection socket; Ticker ticker1; Thread *t1; @@ -31,6 +31,7 @@ Mail<mail_t, 200> mail_box; uint8_t routerAddress[8] = { 0 }; // Should be 00 13 A2 00 40 8B 41 6E +uint8_t startDelimiter = 0x7E; uint16_t portNumber = 0; uint16_t panId = 0; char serverAddress[32]; @@ -105,21 +106,21 @@ } void readATCommandResponse(uint8_t* buffer) { - if (buffer[4] == 0x01) printError("AT Command Response: Error.\n"); + if (buffer[4] == 0x01) printError("AT Command Response: Error.\n"); else if (buffer[4] == 0x02) printError("AT Command Response: Invalid Command.\n"); else if (buffer[4] == 0x03) printError("AT Command Response: Invalid Parameter.\n"); else if (buffer[4] == 0x04) printError("AT Command Response: Tx Failure.\n"); } void readRemoteATCommandResponse(uint8_t* buffer) { - if (buffer[14] == 0x01) printError("Remote AT Command Response: Error.\n"); + if (buffer[14] == 0x01) printError("Remote AT Command Response: Error.\n"); else if (buffer[14] == 0x02) printError("Remote AT Command Response: Invalid Command.\n"); else if (buffer[14] == 0x03) printError("Remote AT Command Response: Invalid Parameter.\n"); else if (buffer[14] == 0x04) printError("Remote AT Command Response: Tx Failure.\n"); } void readTransmitStatus(uint8_t* buffer) { - if (buffer[5] == 0x01) pc.printf("Transmit Status: An expedted MAC acknowledgement never occured. \n"); + if (buffer[5] == 0x01) pc.printf("Transmit Status: An expedted MAC acknowledgement never occured. \n"); else if (buffer[5] == 0x02) pc.printf("Transmit Status: CCA failure. \n"); else if (buffer[5] == 0x03) pc.printf("Transmit Status: Packet was purgedwithoutbeing transmitted. \n"); else if (buffer[5] == 0x04) pc.printf("Transmit Status: Physical error on the interface with the WiFi transceiver. \n"); @@ -139,7 +140,7 @@ } void readModemStatus(uint8_t* buffer) { - if (buffer[1] == 0x00) pc.printf("Modem status: Hardware reset.\n"); + if (buffer[1] == 0x00) pc.printf("Modem status: Hardware reset.\n"); else if (buffer[1] == 0x01) printError("Modem status: Watchdog timer reset.\n"); else if (buffer[1] == 0x02) printError("Modem status: Joined network.\n"); else if (buffer[1] == 0x03) printError("Modem status: Disassociated.\n"); @@ -185,27 +186,22 @@ } /* -* Faire allumer une DEL, reliée à un des ports du XBEE (DIO) du Routeur, en utilisant une commande de configuration à distance provenant du Coordinateur +* Faire allumer une DEL, reliée à un des ports du XBEE (DIO) du Routeur, en utilisant une commande de configuration à distance provenant du Coordinateur +* HIGH: {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3C}; +* LOW: {0x7E, 0x00, 0x10, 0x17, 0x02, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3C}; */ void toggleRouterLed() { - // HIGH: {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3C}; - // LOW: {0x7E, 0x00, 0x10, 0x17, 0x02, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3C}; bool led = false; - uint8_t command[20] = {0}; + uint8_t command[20] = { 0 }; - Thread::signal_wait(0x2); // Attendre que l'adresse du noeud rooter soit connue - command[0] = 0x7E; + Thread::signal_wait(0x2); // Attendre que l'adresse du noeud rooteur soit connue + command[0] = startDelimiter; command[1] = 0x00; command[2] = 0x10; command[3] = 0x17; - command[5] = routerAddress[0]; - command[6] = routerAddress[1]; - command[7] = routerAddress[2]; - command[8] = routerAddress[3]; - command[9] = routerAddress[4]; - command[10] = routerAddress[5]; - command[11] = routerAddress[6]; - command[12] = routerAddress[7]; + for (uint8_t i = 0; i < sizeof(routerAddress); i++) { + command[i + 5] = routerAddress[i]; + } command[13] = 0xFF; command[14] = 0xFE; command[15] = 0x02; @@ -222,9 +218,9 @@ command[4] = 0x02; command[18] = 0x04; } - command[19] = calculateChecksum(3, 20, command); + command[19] = calculateChecksum(3, sizeof(command), command); - sendDataToXbee(20, command); + sendDataToXbee(sizeof(command), command); led = !led; led1 = !led1; } @@ -246,8 +242,8 @@ sprintf(buffer, "%s\0", mail->strDryContact); } - sock.send_all(buffer, sizeof(buffer)-1); - responseSize = sock.receive(buffer, sizeof(buffer)-1); + socket.send_all(buffer, sizeof(buffer)-1); + responseSize = socket.receive(buffer, sizeof(buffer)-1); buffer[responseSize] = '\0'; pc.printf("Received %d chars from server: %s\n", responseSize, buffer); @@ -259,15 +255,10 @@ /* * Lire et décomposer les trames reçues (adresse, données, status): */ -void readRouterAddress(uint8_t* buffer) { - routerAddress[0] = buffer[1]; - routerAddress[1] = buffer[2]; - routerAddress[2] = buffer[3]; - routerAddress[3] = buffer[4]; - routerAddress[4] = buffer[5]; - routerAddress[5] = buffer[6]; - routerAddress[6] = buffer[7]; - routerAddress[7] = buffer[8]; +void readRouterAddress(uint8_t* buffer) { + for (uint8_t i = 0; i < 8; i++) { + routerAddress[i] = buffer[i + 1]; + } t1->signal_set(0x02); } @@ -295,7 +286,7 @@ void readDataFromRouter(){ while(1) { - if (xbee.readable() && xbee.getc() == 0x7E) { + if (xbee.readable() && xbee.getc() == startDelimiter) { uint8_t buffer[104] = { 0 }; uint8_t bufferSize = (xbee.getc() << 8 ) | (xbee.getc() & 0xff); memcpy(buffer, readDataFromXbee(bufferSize), bufferSize + 1); @@ -313,8 +304,6 @@ } else if (buffer[0] == 0x09) { // Frame Type: AT Command Response readATCommandResponse(buffer); } - } else { - // Erreur dans la tranmission de la trame... } } } @@ -324,26 +313,22 @@ * Poser le ID du réseau PAN à rejoindre: */ void setCoordinatorPanId(uint16_t panId) { - char _8bitsPanId[2]; - _8bitsPanId[0] = (panId >> 8) & 0xFF; - _8bitsPanId[1] = panId & 0xFF; + char _8bitsPanId[2] = { (panId >> 8) & 0xFF, panId & 0xFF }; - uint8_t setPanIdBuffer[10] = {0x07, 0x00, 0x06, 0x09, 0x01, 0x49, 0x44, _8bitsPanId[0], _8bitsPanId[1], 0x00}; - uint8_t saveChangesBuffer[8] = {0x7E, 0x00, 0x04, 0x09, 0x02, 0x57, 0x52, 0x4B}; - uint8_t applyChangesBuffer[8] = {0x7E, 0x00, 0x04, 0x09, 0x03, 0x41, 0x43, 0x6F}; - setPanIdBuffer[9] = calculateChecksum(0, 10, setPanIdBuffer); // Calculate the checksum - - sendDataToXbee(10, setPanIdBuffer); // Set the 64-bit PAN ID - sendDataToXbee(8, saveChangesBuffer); // Save the changes - sendDataToXbee(8, applyChangesBuffer); // Apply changes by sending the CN command + uint8_t setPanIdBuffer[] = { startDelimiter, 0x00, 0x06, 0x09, 0x01, 0x49, 0x44, _8bitsPanId[0], _8bitsPanId[1], 0x00 }; + uint8_t saveChangesBuffer[] = { startDelimiter, 0x00, 0x04, 0x09, 0x02, 0x57, 0x52, 0x4B }; + uint8_t applyChangesBuffer[] = { startDelimiter, 0x00, 0x04, 0x09, 0x03, 0x41, 0x43, 0x6F }; + setPanIdBuffer[sizeof(setPanIdBuffer) - 1] = calculateChecksum(0, sizeof(setPanIdBuffer), setPanIdBuffer); // Calculate the checksum + + sendDataToXbee(sizeof(setPanIdBuffer), setPanIdBuffer); // Set the 64-bit PAN ID + sendDataToXbee(sizeof(saveChangesBuffer), saveChangesBuffer); // Save the changes + sendDataToXbee(sizeof(applyChangesBuffer), applyChangesBuffer); // Apply changes by sending the CN command } /* * Envoyer un signal au thread qui fait clignoter la DEL du noeud routeur: */ -void isr() { - t1->signal_set(0x1); -} +void isr() { t1->signal_set(0x1); } // -------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------- @@ -370,7 +355,7 @@ } /*int numberOfRetries = 0; - while (sock.connect(serverAddress, portNumber) != 0) { + while (socket.connect(serverAddress, portNumber) != 0) { if (numberOfRetries == 5) { error("Erreur dans la connection au socket.\n"); }