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:
- 13:62404d7dab8d
- Parent:
- 12:42986a9c3682
- Child:
- 14:8b59a90725bc
--- a/main.cpp Mon Feb 13 13:01:34 2017 +0000 +++ b/main.cpp Mon Feb 13 17:09:27 2017 +0000 @@ -12,7 +12,17 @@ TCPSocketConnection sock; Ticker ticker1; -uint64_t routerAddress; +/* Mail */ +typedef struct { + char strAccelerationX[10]; + char strAccelerationY[10]; + char strAccelerationZ[10]; + char strDryContact[10]; +} mail_t; + +Mail<mail_t, 100> mail_box; + +uint8_t routerAddress[8]; uint16_t portNumber = 0; uint16_t panId = 0; char serverAddress[32]; @@ -61,14 +71,7 @@ return (uint8_t)(0xFF - (checksum & 0xFF)); } -void toogleLED() { - union { - uint64_t u64_value; - uint8_t u8_value[8]; - } address; - - address.u64_value = routerAddress; - +void toogleLED(uint8_t bufferSize, uint8_t* buffer) { // Addresse 64 bits = 00 13 A2 00 40 8B 41 6E //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}; @@ -80,14 +83,14 @@ command[3] = 0x17; if(boolLED) command[4] = 0x01; else command[4] = 0x02; - command[5] = address.u8_value[0]; - command[6] = address.u8_value[1]; - command[7] = address.u8_value[2]; - command[8] = address.u8_value[3]; - command[9] = address.u8_value[4]; - command[10] = address.u8_value[5]; - command[11] = address.u8_value[6]; - command[12] = address.u8_value[7]; + 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]; command[13] = 0xFF; command[14] = 0xFE; command[15] = 0x02; @@ -123,25 +126,34 @@ boolLED = !boolLED; } -void sendDataToServer(uint16_t data) { - /*int responseSize; - char buffer[50]; - sprintf(buffer, "Data: %i\n", data); - - if (sock.connect(serverAddress, portNumber) != 0) { - pc.printf("Erreur dans la connextion au socket.\n"); - } else { - sock.send_all(buffer, sizeof(buffer)-1); - responseSize = sock.receive(buffer, sizeof(buffer)-1); +void sendDataToServer() { + while(1) { + osEvent evt = mail_box.get(); - if (responseSize <= 0) { - pc.printf("Error"); - //sock.close(); + if (evt.status == osEventMail) { + mail_t *mail = (mail_t*)evt.value.p; + + int responseSize = 0; + char buffer[50] = { 0 }; + for (int i = 0; i < 50; i++) { + buffer[i] = 0; + } + sprintf(buffer, "%s, %s, %s\0", mail->strAccelerationX, mail->strAccelerationY, mail->strAccelerationZ); + pc.printf("Send to server: %s\n", buffer); + + sock.send_all(buffer, sizeof(buffer)-1); + responseSize = sock.receive(buffer, sizeof(buffer)-1); + + if (responseSize <= 0) { + pc.printf("Error"); + } + + buffer[responseSize] = '\0'; + pc.printf("Received %d chars from server: %s\n", responseSize, buffer); + + mail_box.free(mail); } - - buffer[responseSize] = '\0'; - pc.printf("Received %d chars from server:\n%s\n", responseSize, buffer); - }*/ + } } void checkForError(uint8_t* buffer) { @@ -170,18 +182,14 @@ // We are storing the 64 bits address of the router. void readRouterAddress(uint8_t* buffer) { - union { - uint64_t u64_value; - uint8_t u8_value[8]; - } address; - - for(int i=0; i < 8; i++) { - address.u8_value[i] = buffer[i + 2]; - } - routerAddress = address.u64_value; - - //routerAddress = buffer[2] << 56 | buffer[3] << 48 | buffer[4] << 40 | buffer[5] << 32 | buffer[6] << 24 | buffer[7] << 16 | buffer[8] << 8 | buffer[9]; - pc.printf("The router address is %X\n", routerAddress); + routerAddress[0] = buffer[2]; + routerAddress[1] = buffer[3]; + routerAddress[2] = buffer[4]; + routerAddress[3] = buffer[5]; + routerAddress[4] = buffer[6]; + routerAddress[5] = buffer[7]; + routerAddress[6] = buffer[8]; + routerAddress[7] = buffer[9]; } void readRouterData(uint8_t bufferSize, uint8_t* buffer) { @@ -194,11 +202,17 @@ // pc.printf("Acceleration X: %d\n", acc[0]); // pc.printf("Acceleration Y: %d\n", acc[1]); // pc.printf("Acceleration Z: %d\n", acc[2]); - - sendDataToServer(acc[0]); - + + mail_t *mail = mail_box.alloc(); + sprintf(mail->strAccelerationX, "Acc. X: %i\0", acc[0]); + sprintf(mail->strAccelerationY, "Acc. Y: %i\0", acc[1]); + sprintf(mail->strAccelerationZ, "Acc. Z: %i\0", acc[2]); + mail_box.put(mail); + } else if (buffer[0] == 0x90 && bufferSize == 13) { // Dry contact - // sendDataToServer(buffer[12]); + //mail_t *mail = mail_box.alloc(); + //mail->data = acc[0]; + //mail_box.put(mail); } } @@ -251,8 +265,18 @@ } pc.printf("IP Address is %s\n", eth.getIPAddress()); + int numberOfRetries = 0; + while (sock.connect(serverAddress, portNumber) != 0) { + if (numberOfRetries == 5) { + error("Erreur dans la connection au socket.\n"); + } + numberOfRetries++; + } + // Démarrage des tâches: Thread _readDataFromRouter(readDataFromRouter); + Thread _sendDataToServer(sendDataToServer); + //Thread _toggleRouterLed(toggleLed); while(1) {} }