app3

Dependencies:   WebSocketClient mbed

Revision:
0:f62e1e20c098
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 02 15:10:48 2018 +0000
@@ -0,0 +1,202 @@
+/////////////////////////
+/////////////////////////
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "Websocket.h"
+
+
+DigitalOut P8(p8);
+DigitalOut led1(LED1);
+
+Serial pc(USBTX,USBRX,9600);
+
+Serial xbee(p13,p14,9600);
+
+class message{
+    public : 
+    int lenght;
+    int type;
+    char bits64[8];
+    char bits16[4];
+    char msg[8];
+    int checksum;
+    int paquetconfirm;
+};
+
+message message1;
+
+uint8_t table[16]={};// = {0x7E,0x00,0x15,0x11,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFE,0xE8,0xE8,0x00,0x11,0xC1,0x05,0x00,0x00,0x74,0xD7};
+                  
+
+
+void initXbee(){
+       P8 = 0;
+     wait(0.4);
+   P8 = 1;  
+}
+
+void xctu(){
+            if(xbee.readable()){
+    pc.putc(xbee.getc());}
+        if(pc.readable()){
+        xbee.putc(pc.getc());
+        }
+}
+
+void getIp(){
+    // des données a lire 
+    if(xbee.readable()){
+        // début du packet
+        //donnée 1 :
+        if(xbee.getc()==0x7E){
+            //donnée 2 :
+            if(xbee.getc()==0x00){
+                //donnée 3 :
+                message1.lenght = xbee.getc();
+                //donnée 4 :
+              message1.type = xbee.getc();
+                //donnée 5 à 12 :
+                for (int i =0;i<(8);i++){
+                    message1.bits64[i]=xbee.getc();}
+                //donnée 13 à 14 :
+                for (int i =0;i<(2);i++){
+                    message1.bits16[i]=xbee.getc();}
+                    //donnée 15 :
+                xbee.getc();
+                    // donnée de 16 à X 
+                for (int i =0;i<(message1.lenght-12);i++){
+         message1.msg[i]= xbee.getc();
+                }
+                //dernière donnée
+                message1.checksum = xbee.getc();    
+                
+            }
+        }
+    }
+}
+void getdatatest(){
+        if(xbee.readable()){
+            for(int i =0; i<sizeof(table);i++){
+                while(xbee.readable()==0){}
+                table[i]=xbee.getc();
+            }
+            
+        }
+}
+
+void sendMessageIp(){
+    char sum = 0;
+    char tabletest[]={0x7E,0x00,0x15,0x11,0x01,0x00,0x13,0xA2,0x00,0x40,0x3E,0x09,0x57,0xFF,0xFE,0xE8,0xE8,0x00,0x11,0xC1,0x05,0x00,0x00,0x74,0x00};
+        for(int i = 5;i<11;i++){
+            tabletest[i]=message1.bits64[i-5];
+        }
+        for(int i = 3;i<24;i++){
+             sum = sum + tabletest[i];
+        }
+        sum = 0xFF - sum;
+        tabletest[24]=sum;
+        for(int i =0;i<sizeof(tabletest);i++){
+        //  pc.printf("%X ",tabletest[i]);
+            xbee.putc(tabletest[i]);
+        }
+}
+
+int main() {
+     char bouton = 0x00;
+     char acc = 0x00;
+     char c[20];
+     int h=0;
+     int ledState=0;
+       // announce
+    printf("Websocket Example v1.0.0\r\n");
+    
+    // Create a network interface and connect
+    EthernetInterface eth;
+    eth.connect();
+    printf("IP Address is %s\n\r", eth.get_ip_address());
+
+    // Create a websocket instance
+    Websocket ws("ws://10.43.134.175:8000/", &eth);
+    int connect_error = ws.connect();
+    initXbee();
+    
+    // tant que l'on a pas obtenue l'adresse ip 64 du recepteur 
+    while(message1.paquetconfirm!=1){
+        // on obtient l'adresse
+        //getIp();
+        getIp();
+        if(message1.msg[0]==0x74 && message1.lenght == 0x0D){message1.paquetconfirm = 1;}
+
+    }   
+            pc.printf("ip :");
+                for(int i =0;i<sizeof(message1.bits64);i++){
+            pc.printf("%X ",message1.bits64[i]);
+        }
+                pc.printf("\n");
+    // une fois l'adresse obtenue on envoie un message avec l'ip de destination 
+        sendMessageIp();
+        sendMessageIp();
+
+        
+    // on recoi la confirmation que les deux appareils sont connecter 
+        
+        
+
+    
+    while(1){
+        //xctu(); // connection xctu
+       
+        getIp();
+        if(message1.msg[0]==0x62 && message1.msg[1]==0x6F && message1.lenght == 0x13){
+            if(bouton != message1.msg[6]){
+            bouton = message1.msg[6];
+          //  pc.printf("boutton : %c\n\r",bouton);
+            char str[12];
+            sprintf(str, "bouton : %c", bouton);
+             ws.send(str);
+             ws.read(c);
+             printf("%s\n\r", c);
+            }
+            }
+            if(acc != message1.msg[4]&& message1.msg[0]==0x61){
+                char v1 = message1.msg[3];
+                char v2 = message1.msg[4];
+               // char data[]={v1,v2};
+             //   pc.printf("accelerometre en x : %c%c,\n\r",v1,v2);
+                acc=message1.msg[4];
+                char str[12];
+                sprintf(str, "acc X : %c%c", v1,v2);
+                ws.send(str);
+                ws.read(c);
+                printf("%s\n\r", c);
+                }
+               // h++;
+                if (h == 10){
+                    if(ledState == 0){
+                        char tabletest[]={0x7E,0x00,0x10,0x17,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFE,0x02,0x44,0x31,0x05,0x70};
+                        for(int i =0;i<sizeof(tabletest);i++){
+                            xbee.putc(tabletest[i]);
+                            }
+                            ledState = 1;
+                            h=0;
+                        }
+                    if(ledState == 1){
+                        char tabletest[]={0x7E,0x00,0x10,0x17,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFE,0x02,0x44,0x31,0x04,0x71};
+                         for(int i =0;i<sizeof(tabletest);i++){
+                            xbee.putc(tabletest[i]);
+                            }
+                            ledState = 0;
+                            h=0;
+                        }
+                    
+                    }
+       //     }
+       
+       
+
+
+    }
+    
+    }
+