Jean-Olivier Lessard
/
Alex_mange_moi
qsdasd
Revision 8:e8ebb98f3c83, committed 2018-12-03
- Comitter:
- JioLessard
- Date:
- Mon Dec 03 16:46:52 2018 +0000
- Parent:
- 7:26199246b2b4
- Commit message:
- mange moi bien
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Jun 23 14:05:26 2017 -0500 +++ b/main.cpp Mon Dec 03 16:46:52 2018 +0000 @@ -1,51 +1,175 @@ -/* Copyright C2014 ARM, MIT License - * - * Author: Doug Anson (doug.anson@arm.com) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software - * and associated documentation files the "Software", to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING - * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ +// Programme pour coordinateur +//Alex Veilleux veia2304 +//Jean-Olivier Lessard lesj1706 #include "mbed.h" #include "EthernetInterface.h" #include "Websocket.h" -// Blinky -DigitalOut led(LED1); +//Variable globale +DigitalOut led1(LED1); +DigitalOut led2(LED1); + +DigitalOut reset(p8); +Serial serial_connection(p13, p14); + +LocalFileSystem local("local"); +int panChar; +char url; -int main() { +//Permet la configuration du PAN id selon le fichier de configuration +void set_PAN(){ + int panLSB = (uint8_t)panChar; + int panMSB = (uint8_t)(panChar >> 8); + + serial_connection.putc(0x7E); + serial_connection.putc(0x00); + serial_connection.putc(0x06); + serial_connection.putc(0x09); + serial_connection.putc(0x01); + serial_connection.putc(0x49); //I + serial_connection.putc(0x44); //D + //serial_connection.putc(0xAA); // PAN ID, TODO + //serial_connection.putc(0xBD); // PAN ID, TODO + serial_connection.putc(panMSB); // PAN ID, TODO + serial_connection.putc(panLSB); // PAN ID, TODO + serial_connection.putc(0x00); // CheckSum + + serial_connection.putc(0x7E); + serial_connection.putc(0x00); + serial_connection.putc(0x04); + serial_connection.putc(0x09); + serial_connection.putc(0x01); + serial_connection.putc(0x57); + serial_connection.putc(0x52); + serial_connection.putc(0x4C); + + serial_connection.putc(0x7E); + serial_connection.putc(0x00); + serial_connection.putc(0x04); + serial_connection.putc(0x09); + serial_connection.putc(0x01); + serial_connection.putc(0x41); + serial_connection.putc(0x43); + serial_connection.putc(0x71); +} - // announce - printf("Websocket Example v1.0.0\r\n"); +//Permet la lecture du PAN et de l'Url dans un fichier local +//Entré: aucune +//Sortie: Sotcké dans variable globales +void Read_FileChar() { + printf("Debut readfile \n\r"); + + FILE *fp ; + fp = fopen("/local/char2.txt", "r"); + + //Si file non trouvé + if (fp == NULL) { + printf("Can't open input file in.list!\n\r"); + exit(1); + } + + //lecture ligne par ligne + fscanf(fp, "%X", &panChar); + fscanf(fp, "%s", &url); + + //Fermeture fichier + fclose (fp); + fp = NULL; + printf("Fin readfile\n\r"); +} + +int main() { + char message[1000]; + Read_FileChar(); - // Create a network interface and connect + int cpt_frame = 0; + int length_msb = 0; + int length_lsb = 0; + + //Init reset xbee + reset = 0; + led1 = 0; + wait_ms(1); + reset = 1; + wait_ms(1); + + set_PAN(); + + // Début programme + printf("\r\n Websocket Example v1.0.0\r\n"); + + //Lecture du fichier de configuration + + char *realurl; + asprintf(&realurl, "%s%s%s", "ws://", &url, "/"); + printf("Real url for socket: %s\n\r", realurl); + + //network interface création EthernetInterface eth; eth.connect(); printf("IP Address is %s\n\r", eth.get_ip_address()); - - // Create a websocket instance - Websocket ws("ws://example.com:8080/", ð); + + // Websocket création + Websocket ws(realurl, ð); int connect_error = ws.connect(); - // begin main loop - while (true) { - - // blink... - led = !led; - wait(0.5); - - int error_c = ws.send("Hello World\r\n"); + bool trameAcce = false; + char accData[6]; + int cptAcc; + + while(1) { + if (serial_connection.readable()){ + char hex_value = serial_connection.getc(); + //printf("%X\n\r", hex_value); + if (hex_value == 0x7E){ // Détecte une nouvelle trame + cpt_frame = 0; + trameAcce = false; + cptAcc = 0; + } + else if(cpt_frame == 1){ // Lecture du length MSB + length_msb = hex_value; + } + else if(cpt_frame == 2){ // Lecture du length LSB + length_lsb = hex_value; + if (length_lsb == 0x0E){ + //Envoie au serveur par websocket pour bouton + int error_c = ws.send("Button pressed\r\n"); + printf("Error code : %d \r\n", error_c); + if(error_c == 1) + printf("Error send message"); + else{ + printf("Message send\r\n"); + ws.read(message); + printf("The message read is: %s \r\n", message); + } + } + else if(0x12){ //Longueur pour trame de l'accelerometre + trameAcce = true; + } + } + else if (trameAcce && cpt_frame >= 0x0F && cpt_frame < (length_lsb + 0x03)){ //Permet d'aller chercher le data dynamiquement selon la longueur de la trame + //printf("%X\n\r", hex_value); + accData[cptAcc] = hex_value; + cptAcc++; + } + + //Envoie au serveur par websocket pour accelerometre + if(trameAcce && cptAcc >= 6){ + int error_c = ws.send("Button pressed\r\n"); + + if(error_c == 1) + printf("Error send message"); + else{ + printf("Message send\r\n"); + ws.read(message); + printf("Value x: 0x%X%X Value y: 0x%X%X Value z: 0x%X%X \r\n", accData[0], accData[1], accData[2], accData[3], accData[4], accData[5]); + } + + trameAcce = false; + cptAcc = 0; + } + cpt_frame++; + } } -} \ No newline at end of file +}