programme intégré

Dependencies:   MODSERIAL Servo mbed

Revision:
1:ca1ad3dc37b3
Parent:
0:d811d2c0da76
Child:
2:566244a93a7a
diff -r d811d2c0da76 -r ca1ad3dc37b3 main.cpp
--- a/main.cpp	Wed May 23 17:33:36 2018 +0000
+++ b/main.cpp	Wed May 30 21:46:48 2018 +0000
@@ -15,14 +15,14 @@
 Timer t;                                                                                //Timer
 Servo myservo(p21);                                                                     //Servomoteur
 DigitalIn manuel1 (p20);                                                                //Mode manuel position n°1 (entrée seulement)
-DigitalIn manuel2 (p19);                                                                //Mode manuel position n°2 (sortie seulement)
-DigitalOut ledmm (p18);                                                                  //Led indiquant si le mode manuel est activé
+DigitalIn manuel2 (p19);                                                                //Mode manuel position n°2 (sortie seulement)(non réalisé car changement de servomoteur)
+DigitalOut ledmm (p18);                                                                 //Led indiquant si le mode manuel est activé
 DigitalIn manuel3 (p17);                                                                //Mode manuel position n°3 (entréé ou sortie)
 DigitalIn manuel4 (p16);                                                                //Mode manuel position n°4 (verrouillage complet)
 DigitalIn bpmanuel (p15);                                                               //Bouton ON/OFF du mode manuel
-DigitalIn capteurfermeture (p14);
+DigitalIn capteurfermeture (p14);                                                       //Capteur détectant la fermeture
 DigitalIn capext (p12) ;                                                                //Capteur de présence extérieur
-DigitalIn capint (p11) ;                                                                //Capteur de présence intérieur                                                       //Capteur détectant la fermeture 
+DigitalIn capint (p11) ;                                                                //Capteur de présence intérieur
 DigitalIn CTS(p8);
 DigitalIn F411 (p7);                                                                    //Detection alimentation servomoteur (CHALLENGE LEGRAND !!!!!!!!!!!)
 
@@ -31,25 +31,26 @@
 DigitalOut led3(LED3);                                                                  //Led n°3 du mbed
 DigitalOut led4(LED4);                                                                  //Led n°4 du mbed
 
-int egal1 = 0;                                                                          //tag1
-int egal2 = 0;
-int egal3 = 0;
-int egal4 = 0;
-int egal5 = 0;
-int tag1[4]= {0xAE,0xFB,0x63,0x10};
-int tag2[4]= {0x25,0x02,0x64,0x10};
-int tag3[4]= {0x3F,0xFC,0x63,0x10};
-int tag4[4]= {0xCE,0xEE,0x63,0x10};
-int tag5[4]= {0xD6,0xD9,0x63,0x10};
-int tag[4]= {0x00,0x00,0x00,0x00};
-int reponse_rfid[5];
+int egal1 = 0;                                                                          //TAG RFID n°1
+int egal2 = 0;                                                                          //TAG RFID n°2
+int egal3 = 0;                                                                          //TAG RFID n°3
+int egal4 = 0;                                                                          //TAG RFID n°
+int egal5 = 0;                                                                          //TAG RFID n°
+int tag1[4]= {0xAE,0xFB,0x63,0x10};                                                     //Identifiant TAG RFID n°1
+int tag2[4]= {0x25,0x02,0x64,0x10};                                                     //Identifiant TAG RFID n°2
+int tag3[4]= {0x3F,0xFC,0x63,0x10};                                                     //Identifiant TAG RFID n°3
+int tag4[4]= {0xCE,0xEE,0x63,0x10};                                                     //Identifiant TAG RFID n°4
+int tag5[4]= {0xD6,0xD9,0x63,0x10};                                                     //Identifiant TAG RFID n°5
+int tag[4]= {0x00,0x00,0x00,0x00};                                                      //Tableau relevant l'identifiant du TAG RFID
+int reponse_rfid[5];                                                        
 int reponse_rfid1[255];
 int lireuid[8]= {0x52,0x00};
-//int trame_rfid[8]={0x7A};
-//int tag_rfid[8]={0x76,0x01};
-//int uid[8]={0x55};
 
 int position ;                                                                          //Position de la chatière
+float pos1 = 1.0 ;                                                                      //Position n°1 (entrée seulement)
+float pos3 = 0.001 ;                                                                    //Position n°3 (complétement ouvert)
+float pos4 = 0.42 ;                                                                     //Position n°4 (complétement fermé)
+
 
 int i = 0;
 int e = 0;
@@ -65,10 +66,10 @@
 char ICHA[5] = "ICHA";                                                                  //Le chat vient de l'intérieur
 
 
-char tcpip[14] = "192.168.10.14";                                                       //Adresse IP du module ESP8666
+char tcpip[14] = "192.168.1.42";                                                       //Adresse IP du module ESP8666
 char tcpport[3] = "23";
 
-int longueur_recue = 0;                                                         
+int longueur_recue = 0;
 int nouvelle_reception = 0;
 int a = 0;
 int b = 0;
@@ -80,61 +81,61 @@
 char connect[10] = "0,CONNECT";
 char closed[9] = "0,CLOSED";
 
+int drapeau = 1;
+
 void HandShake();
 void Antenna();
 void field();
-void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(),WIFI(), Donnee();                //Déclaration des voids 
+void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(),WIFI(), Donnee();                //Déclaration des voids
 
 using namespace std;
 
 int main()                                                                              //Début du int main
 {
-    
+ledmm = 0;
+myservo = 0.001 ;                                                                       //La chatière est fermé
+wait_ms(200);
+
     reset=0;                                                                            //Reset du module ESP8266
     pc.baud(9600);                                                                      //Vitesse de transmission du terminal
     pc.printf("\f\n\r--------------- Demarrage du module WIFI ---------------\n\r");    //Démarrage du module WIFI ESP8266
-    wait(0.5);
+    wait(0.25);
     reset=1;
     timeout=2;
     getreply();
 
     esp.baud(115200);                                                                   //Vitesse de transmission du module WIFI ESP8266
-     
+
     ESPconfig();                                                                        //Configuration du module WIFI ESP8266
 
     pc.printf("\f\n\r- Module pret -\n\r");                                             //Le module WIFI ESP8266
-    
-    while(1)
-    {
-    // Partie RFID
-    
-    egal1=0;
-    egal2=0;
-    egal3=0;
-    egal4=0;
-    egal5=0;
+        
+    while(1) {
+        // Partie RFID
+        led4=1;
+        ledmm = 0;
+        egal1=0;
+        egal2=0;
+        egal3=0;
+        egal4=0;
+        egal5=0;
 
-    
+
 
-        if (CTS == 0) 
-        {
-            // pc.printf("- [CTS = 0] -");
-            // pc.printf("\n\r");
-            //HandShake();
-            for (int i=0; i<2; i++) 
-            {
+        if (CTS == 0) {
+            led4=0;
+            led2=1;
+
+            for (int i=0; i<2; i++) {
                 rfid2.putc(lireuid[i]);
             }
             wait_ms(25);
-            for (int e=0; e<1; e++) 
-            {
+            for (int e=0; e<1; e++) {
                 reponse_rfid1[e]=(rfid1.getc());
                 led1=1;
             }
-            if (reponse_rfid1[0] == 0xD6) 
-            {
-                for (int i=0; i<2; i++) 
-                {
+            if (reponse_rfid1[0] == 0xD6) {
+                for (int i=0; i<2; i++) {
                     rfid2.putc(lireuid[i]);
                     led2=1;
                 }
@@ -143,9 +144,7 @@
                     reponse_rfid[e]=(rfid1.getc());
                     led3=1;
                 }
-                /*for(int i=0;i<5;i++){
-                  tag[i] = {0};
-                }*/
+
                 pc.printf("---------------------------------------");
                 pc.printf("\n\r");
                 pc.printf("Trame de reponse du PCB en hexa : ");
@@ -157,39 +156,28 @@
                 pc.printf("\n\r");
                 pc.printf("---------------------------------------");
                 pc.printf("\n\r");
-            } 
-            else if (reponse_rfid1[0] == 0xC0) 
-            {
+            } else if (reponse_rfid1[0] == 0xC0) {
                 field();
             }
-            if (reponse_rfid1[0] == 0xE0) 
-            {
+            if (reponse_rfid1[0] == 0xE0) {
                 Antenna();
             }
-            //reponse_rfid[1]=0x00;
-            
-            for(int i=1; i<5; i++) 
-            {
+                for(int i=1; i<5; i++) {
                 tag[i-1] = reponse_rfid[i];
             }
-            if(tag[0] != 0x00) 
-            {
+            if(tag[0] != 0x00) {
                 pc.printf("\n\r");
                 pc.printf("tableau de 0: %X", tag[0]);
                 pc.printf("\n\r");
-                
-                for(int i=0; i<4; i++) 
-                {
-                    if(tag[i] == tag1[i]) 
-                    {
+
+                for(int i=0; i<4; i++) {
+                    if(tag[i] == tag1[i]) {
                         egal1 = 1;
                         pc.printf("Tag1 - OK ");
                         pc.printf("\n\r");
                         break;
                         //pc.printf(tag[i]);
-                    } 
-                    else if(tag[i] != tag1[i]) 
-                    {
+                    } else if(tag[i] != tag1[i]) {
                         pc.printf("Tag1 - NOK ");
                         pc.printf("\n\r");
                         egal1 = 0;
@@ -197,18 +185,14 @@
                         //pc.printf(tag[i]);
                     }
                 }
-                for(int i=0; i<4; i++) 
-                {
-                    if(tag[i] == tag2[i]) 
-                    {
+                for(int i=0; i<4; i++) {
+                    if(tag[i] == tag2[i]) {
                         egal2 = 1;
                         pc.printf("Tag2 - OK ");
                         pc.printf("\n\r");
                         break;
                         //pc.printf(tag[i]);
-                    } 
-                    else if(tag[i] != tag2[i]) 
-                    {
+                    } else if(tag[i] != tag2[i]) {
                         pc.printf("Tag2 - NOK ");
                         pc.printf("\n\r");
                         egal2 = 0;
@@ -216,18 +200,14 @@
                         //pc.printf(tag[i]);
                     }
                 }
-                for(int i=0; i<4; i++) 
-                {
-                    if(tag[i] == tag3[i]) 
-                    {
+                for(int i=0; i<4; i++) {
+                    if(tag[i] == tag3[i]) {
                         egal3 = 1;
                         pc.printf("Tag3 - OK ");
                         pc.printf("\n\r");
                         break;
                         //pc.printf(tag[i]);
-                    } 
-                    else if(tag[i] != tag3[i]) 
-                    {
+                    } else if(tag[i] != tag3[i]) {
                         pc.printf("Tag3 - NOK ");
                         pc.printf("\n\r");
                         egal3 = 0;
@@ -235,18 +215,14 @@
                         //pc.printf(tag[i]);
                     }
                 }
-                for(int i=0; i<4; i++) 
-                {
-                    if(tag[i] == tag4[i]) 
-                    {
+                for(int i=0; i<4; i++) {
+                    if(tag[i] == tag4[i]) {
                         egal4 = 1;
                         pc.printf("Tag4 - OK ");
                         pc.printf("\n\r");
                         break;
                         //pc.printf(tag[i]);
-                    } 
-                    else if(tag[i] != tag4[i]) 
-                    {
+                    } else if(tag[i] != tag4[i]) {
                         pc.printf("Tag4 - NOK ");
                         pc.printf("\n\r");
                         egal4 = 0;
@@ -254,121 +230,149 @@
                         //pc.printf(tag[i]);
                     }
                 }
-                for(int i=0; i<4; i++) 
-                {
-                    if(tag[i] == tag5[i]) 
-                    {
+                for(int i=0; i<4; i++) {
+                    if(tag[i] == tag5[i]) {
                         egal5 = 1;
                         pc.printf("Tag5 - OK ");
                         pc.printf("\n\r");
                         break;
                         //pc.printf(tag[i]);
-                    } 
-                    else if(tag[i] != tag5[i]) 
-                    {
+                    } else if(tag[i] != tag5[i]) {
                         pc.printf("Tag5 - NOK ");
                         pc.printf("\n\r");
                         egal5 = 0;
                         break;
                         //pc.printf(tag[i]);
-                   }
+                    }
                 }
-            reponse_rfid[1]=0x00;
+                reponse_rfid[1]=0x00;
             }
 
             led1=0;
             led2=0;
-            led3=0;
+            led3=1;
 
             wait_ms(500);
+
+        }
+
+
+        if(capext ==1 && egal1 ==1) {                                                  //Si le capteur extérieur est à 1 & le TAG 1 est détecté
+            pc.printf("---------- Envoi de donnees au serveur ----------\r\n");        //Envoi de données au serveur
+            strcpy(snd, "AT+CIPSEND=4\r\n");                                           //Commande envoyant le nombre de bytes voulu
+            SendCMD();
+            timeout=5;
+            getreply();
+            pc.printf(buf);
+            strcpy(snd, "ECHA\r\n");                                                    //Le chat vient de l'extérieur
+            SendCMD();
+            timeout=5;
+            getreply();
+            pc.printf(buf);                                                             //Statut de l'étape (OK/ERROR)
+            wait(2);
+            if (F411==1) 
+            {                                                                           //Vérification alimentation servo par F411
+                myservo = 0.42 ;                                                        //Position du servomoteur n°3 (ouvert)
+                wait(5);   
+                while(capteurfermeture==0)
+                {
+                    led2=1;
+                    }
+                    led2=0;
+                    wait(2);
+                    myservo=0.001;
+                    position = 4;
+                    } 
+            }                
+
+            myservo = 0.001 ;                                                           //La chatière est fermé
+            position = 4;
+                
+            }
+
         
-    }
-    
     
-    if(capext >=1 & egal1 ==1)                                                                      //Si le capteur extérieur est à 1
-    {
-        pc.printf("---------- Envoi de donnees au serveur ----------\r\n");             //Envoi de données au serveur
-        strcpy(snd, "AT+CIPSEND=4\r\n");                                                //Commande envoyant le nombre de bytes voulu
-        SendCMD();
-        timeout=5;
-        getreply();
-        pc.printf(buf);
-        strcpy(snd, "ECHA\r\n");                                                        //Le chat vient de l'extérieur                                                      
-        SendCMD();
-        timeout=5;
-        getreply();
-        pc.printf(buf);                                                                 //Statut de l'étape (OK/ERROR)
-    }
-    
-    if(capint >=1 & egal1 ==1)                                                                      //Si le capteur intérieur est à 1
-    {
+
+    if(capint ==1 && egal1 ==1)                                                         //Si le capteur intérieur est à 1 & le TAG 1 est détecté
+    {                                                       
         pc.printf("---------- Envoi de donnees au serveur ----------\r\n");             //Envoi de données au serveur
         strcpy(snd, "AT+CIPSEND=4\r\n");                                                //Commande envoyant le nombre de bytes voulu
         SendCMD();
         timeout=5;
         getreply();
         pc.printf(buf);
-        strcpy(snd, "ICHA\r\n");                                                        //Le chat vient de l'intérieur                                                       
+        strcpy(snd, "ICHA\r\n");                                                        //Le chat vient de l'intérieur
         SendCMD();
         timeout=5;
         getreply();
         pc.printf(buf);                                                                 //Statut de l'étape (OK/ERROR)
-    }    
-    
-    
-    
-    if (bpmanuel ==1)                                                                   //Activation du mode manuel
-    {
-        ledmm = 1 ;                                                                     //Allumage Led indiquant que le mode manuel est activé
-                
-        switch(position)                                                                //Switch en fonction de la position de la chatière pour le mode manuel 
-        {                                                                               
-            case 1 :                                                                    //Mode manuel position n°1 (entrée seulement)
-            if (F411 ==1)                                                               //Vérification alimentation servo par F411
+        wait(2);
+        if (F411 ==1) 
+        {                                                                               //Vérification alimentation servo par F411 (MyHome)
+            myservo = 0.42 ;                                                            //Position du servomoteur n°3 (ouvert)
+            wait(5)    ;                                                                
+          while(capteurfermeture==0)
             {
-                myservo = 0.25;                                                         //Position du servomoteur n°1
-                if(capteurfermeture==1)                                                 //Vérification de la fermeture de la chatière
-                {
-                    position = 1;                                                       //La chatière est en position n°1 (entrée seulement)
-                }
+                led3=1;
+            } 
+                led3=0;
+                wait(2);                                                           
+                myservo = 0.001 ;                                                       //La chatière est fermé
+                position=4;                                                             
+        }
+    }
+
+    
+
+
+
+    while(bpmanuel ==1 ) {                                                             //Activation du mode manuel
+        ledmm = 1 ;                                                                    //Allumage Led indiquant que le mode manuel est activé
+        if (manuel1==1){
+            position = 1;
             }
-                break;                                                                  //Fin du case n°1
-            case 2 :                                                                    //Mode manuel position n°2 (sortie seulement)
-            if (F411 ==1)                                                               //Vérification alimentation servo par F411
-            {
-                myservo = 0.5;                                                          //Position du servomoteur n°2
-                if(capteurfermeture==1)                                                 //Vérification de la fermeture de la chatière
-                {
-                    position = 2;                                                       //La chatière est en position n°2 (sortie seulement)
+         if (manuel3==1){
+            position = 3;
+            }
+        if (manuel4==1){
+            position = 4;
+            }   
+        switch(position) {                                                                  //Switch en fonction de la position de la chatière pour le mode manuel
+            case 1 :                                                                        //Mode manuel position n°1 (entrée seulement)
+                                                                                            
+                                                                                            //Position du servomoteur n°1
+                    if(capteurfermeture==1) {                                               //Vérification de la fermeture de la chatière
+                    myservo = pos1 ;
+                        position = 1;
+                                                                                            //La chatière est en position n°1 (entrée seulement)
+                    
                 }
-            }
-                break;                                                                  //Fin du case n°2
-            case 3 :                                                                    //Mode manuel position n°3 (entréé ou sortie) 
-            if (F411 ==1)                                                               //Vérification alimentation servo par F411
-            {
-                myservo = 0.75 ;                                                        //Position du servomoteur n°3
-                if(capteurfermeture==1)                                                 //Vérification de la fermeture de la chatière
-                {
-                    position = 3;                                                       //La chatière est en position n°3 (entréé ou sortie)
+                break;                                                                      //Fin du case n°1
+                     
+            case 4 :                                                                        //Mode manuel position n°3 (entréé ou sortie)
+                                                                                            //Vérification alimentation servo par F411
+                                                                                            //Position du servomoteur n°3
+                    if(capteurfermeture==1) {                                               //Vérification de la fermeture de la chatière
+                    myservo = pos3 ;
+                        position = 3;                                                       //La chatière est en position n°3 (entréé ou sortie)
+                    
                 }
-            }
-                break;                                                                  //Fin du case n°3
-            case 4 :                                                                    //Mode manuel position n°4 (verrouillage complet) 
-            if (F411 ==1)                                                               //Vérification alimentation servo par F411
-            {
-                myservo = 1 ;                                                           //Position du servomoteur n°4
-                if(capteurfermeture==1)                                                 //Vérification de la fermeture de la chatière
-                {
-                    position = 4;                                                       //La chatière est en position n°4 (verrouillage complet)
+                break;                                                                      //Fin du case n°3
+            case 3 :                                                                        //Mode manuel position n°4 (verrouillage complet)
+                                                                                            //Position du servomoteur n°4
+                    if(capteurfermeture==1) {                                               //Vérification de la fermeture de la chatière
+                    myservo = pos4 ;
+                        position = 4;                                                       //La chatière est en position n°4 (verrouillage complet)
+              
                 }
-            }
                 break;                                                                  //Fin du case n°4
-            default :                                                                   //Fin du mode manuel                    
-        }                                                                               //Fin du switch
-    }                                                                                   //Fin du mode manuel
-    }
-}                                                                                       //Fin du int main
-        //+++++++++++++++++++++++++++++ Voids RFID +++++++++++++++++++++++++++++
+            default :                                                                   //Fin du mode manuel
+                       }                                                                //Fin du switch
+        } myservo=0.001;
+     
+                                                                                     //Fin du mode manuel
+}                                                                                  //Fin du int main
+//+++++++++++++++++++++++++++++ Voids RFID +++++++++++++++++++++++++++++
 /*void HandShake(){
   pc.printf("\n\r");
   pc.printf("CTS IS TO 0");
@@ -428,7 +432,7 @@
     getreply();
     pc.printf(buf);                                                                     //Statut de l'étape (OK/ERROR)
 
-    wait(5);                                                                            //Délai inter-étape
+    wait(2);                                                                            //Délai inter-étape
 
     pc.printf("\n---------- Adresse IP local ----------\r\n");                          //Adressage IP
     strcpy(snd, "AT+CIFSR\r\n");                                                        //Commande affichant adresse IP et MAC du PC ainsi que du module WIFI ESP8266
@@ -437,24 +441,24 @@
     getreply();
     pc.printf(buf);                                                                     //Statut de l'étape (OK/ERROR)
 
-    wait(5);                                                                            //Délai inter-étape
+    wait(3);                                                                            //Délai inter-étape
 
     pc.printf("\n---------- TCP Client ----------\r\n");                                //Connection au serveur en tant que client
-    strcpy(snd, "AT+CIPSTART=\"TCP\",\"192.168.10.12\",55555\r\n");                     //Commande connection au serveur : Protocol , Adresse IP serveur , port
+    strcpy(snd, "AT+CIPSTART=\"TCP\",\"192.168.2.1\",55555\r\n");                     //Commande connection au serveur : Protocol , Adresse IP serveur , port
     SendCMD();
     timeout=5;
     getreply();
     pc.printf(buf);                                                                     //Statut de l'étape (OK/ERROR)
-    
-    wait(5);                                                                            //Délai inter-étape
-    
-    
+
+    wait(3);                                                                            //Délai inter-étape
+
+
 }                                                                                       //Fin du void config
 
 void SendCMD()
 {
     esp.printf("%s", snd);
-}                                                                               
+}
 
 void getreply()
 {