asas

Dependencies:   mbed WebSocketClient

main.cpp

Committer:
JioLessard
Date:
2018-12-03
Revision:
0:7e9e4d5000cd

File content as of revision 0:7e9e4d5000cd:

#include "mbed.h"
#include "EthernetInterface.h"
#include "Websocket.h"

//Variable global
DigitalOut led1(LED1);
DigitalOut led2(LED1);

//xbee pin
DigitalOut reset(p8);
Serial serial_connection(p13, p14);

//pour lecture fichier local
LocalFileSystem local("local"); 
int panChar;
char url;

//Configuration PAN pour Xbee
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(0x0A);  // 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);  
}


//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);
        /*
        printf("%X \n\r" , panChar);
        printf("%s \n\r" , &url);*/

        //Fermeture fichier
    fclose (fp);     
      fp = NULL;
    printf("Fin readfile\n\r");
}

int main() {   
     char message[1000];
     Read_FileChar();
    
      int cpt_frame = 0;
    int length_msb = 0;
    int length_lsb = 0;
    
    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());
        
    // Websocket création
    //Websocket ws("ws://example.com:8080/", &eth);
      //Websocket ws("ws://192.168.137.1:8000/", &eth);
      Websocket ws(realurl, &eth);
    int connect_error = ws.connect();
    
     //Bouce principale
    while (true){   
                 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;
            }
            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){
                    printf("\n\r Pressed");
                }
            }
            else if (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);
            }
            cpt_frame++;
        }
            /*
        int error_c = ws.send("Hello World\r\n");
                 
              if(error_c == 1)
                    printf("Error send message");
                else{
                    printf("Message send\r\n");
                    wait_ms(100);
                    ws.read(message);
                    printf("The message read is: %s \r\n", message);
                }*/
    }
}