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
main.cpp
- Committer:
- benjaminroy
- Date:
- 2017-02-12
- Revision:
- 7:ea6d3c6ac200
- Parent:
- 6:fd7d91edcf60
- Child:
- 8:9c34eb3cb3ef
File content as of revision 7:ea6d3c6ac200:
// COORDINATOR #include "EthernetInterface.h" #include "ConfigFile.h" #include "mbed.h" DigitalOut reset(p8); Serial xbee(p13, p14); // tx, rx Serial pc(USBTX, USBRX); // tx, rx uint16_t portNumber = 0; uint16_t panId = 0; char serverAddress[32]; char portNbr[4]; char panIdChar[5]; bool boolLED = false; void printBuffer(uint8_t bufferSize, uint8_t* buffer) { for(int k = 0; k <= bufferSize; k++) { pc.printf("%X-", buffer[k]); } printf("\n"); } void readConfigFile() { LocalFileSystem local("local"); ConfigFile cfg; char *serverAddressStr = "serverAddr"; char *portNumberStr = "portNb"; char *panIdStr = "panID"; if (!cfg.read("/local/input.cfg")) { error("Erreur dans la lecture du fichier de configuration.\n"); } else { cfg.getValue(panIdStr, &panIdChar[0], sizeof(panIdChar)); cfg.getValue(serverAddressStr, &serverAddress[0], sizeof(serverAddress)); cfg.getValue(portNumberStr, &portNbr[0], sizeof(portNbr)); portNumber = (uint16_t)strtol(portNbr, NULL, 10); panId = (uint16_t)strtol(panIdChar, NULL, 10); printf("The server address is %s\n", serverAddress); printf("The port number is %i\n", portNumber); printf("The PAN ID is %i\n", panId); } } uint16_t getFrameSize(uint8_t value1, uint8_t value2) { union { uint16_t u16_value; uint8_t u8_value[2]; } length; length.u8_value[1] = value1; length.u8_value[0] = value2; return length.u16_value; } void toogleLED() { // Addresse 64 bits = 00 13 A2 00 40 8B 41 6E //uint8_t high[20] = {0x7E, 0x00, 0x10, 0x17, 0x00, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3D}; //uint8_t low[20] = {0x7E, 0x00, 0x10, 0x17, 0x00, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3E}; uint8_t high[20] = {0x7E, 0x00, 0x10, 0x17, 0x01, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x05, 0x3C}; uint8_t low[20] = {0x7E, 0x00, 0x10, 0x17, 0x02, 0x00, 0x13, 0xA2, 0x00, 0x40, 0x8B, 0x41, 0x6E, 0xFF, 0xFE, 0x02, 0x44, 0x34, 0x04, 0x3C}; for (int i = 0; i < 20;) { if (xbee.writeable()) { if (boolLED) { xbee.putc(high[i]); } else { xbee.putc(low[i]); } i++; } } if (boolLED) { pc.printf("Et un haut!\n"); } else { pc.printf("Et un bas!\n"); } boolLED = !boolLED; } void initSocket() { //int repSize; //while (true) { //sprintf (buffer, (const char *)xbee.getc()); //sock.send_all(buffer, sizeof(buffer)-1); //repSize = sock.receive(buffer, sizeof(buffer)-1); // if (repSize <= 0) { // printf("Error"); // sock.close(); // break; // } //buffer[repSize] = '\0'; //printf("Received %d chars from server:\n%s\n", repSize, buffer); //} } void checkForError(uint8_t* buffer) { if (buffer[0] == 0x97) { if (buffer[1] == 0x02) { pc.printf("Erreur dans la remote AT command pour mettre la DEL a l'etat bas.\n"); } else if (buffer[1] == 0x01) { pc.printf("Erreur dans la remote AT command pour mettre la DEL a l'etat haut\n"); } } } void checksum(uint16_t frameSize, uint8_t* buffer) { uint32_t checkSum = 0; for (int i = 0; i < frameSize; i++) { checkSum += buffer[i]; } if ((0xFF - (checkSum & 0xFF)) != buffer[frameSize]) { pc.printf("Erreur dans le checksum. \n"); } //checkForError(buffer); } void readFrame(){ uint8_t sizeBytes[2] = { 0 }; uint8_t buffer[104] = { 0 }; if (xbee.readable() && xbee.getc() == 0x7E) { for (int i = 0; i < 2;) { sizeBytes[i] = xbee.getc(); i++; } uint16_t bufferSize = getFrameSize(sizeBytes[0], sizeBytes[1]); for (int j = 0; j <= bufferSize;) { if (xbee.readable()) { buffer[j] = xbee.getc(); j++; } } checksum(bufferSize, buffer); printBuffer(bufferSize, buffer); } } int main() { pc.printf("Starting a coordinator... \n"); EthernetInterface eth; TCPSocketConnection sock; reset = 0; wait_ms(1); reset = 1; wait_ms(1); readConfigFile(); xbee.baud(9600); // Set baud rate xbee.printf("ATID %i\r",panId); // Set the 64-bit PAN ID xbee.printf("ATWR \r"); // xbee.printf("ATCN \r"); if (eth.init() != 0) { // Use DHCP pc.printf("Erreur d'initialisation du RJ45.\n"); } if (eth.connect() != 0) { pc.printf("Erreur de connection du RJ45\n"); } if (sock.connect(serverAddress, portNumber) != 0) { pc.printf("Erreur de socket.\n"); } //sock.send_all(buffer, sizeof(buffer)-1); //initSocket(); //Set ticker to blink LED at each 1 sec //Ticker blinkLED; // blinkLED.attach(&toogleLED, 1); while(1) { readFrame(); //wait_ms(1000); //toogleLED(); } }