programme intégré

Dependencies:   MODSERIAL Servo mbed

Revision:
0:d811d2c0da76
Child:
1:ca1ad3dc37b3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 23 17:33:36 2018 +0000
@@ -0,0 +1,476 @@
+#include "mbed.h"                                                                       //
+#include <iostream>                                                                     //
+#include <string>                                                                       //
+#include <fstream>                                                                      //Déclaration des librairies utilisées
+#include "MODSERIAL.h"                                                                  //
+#include "Servo.h"                                                                      //
+#include <Serial.h>
+#include "stdio.h"
+
+Serial pc(USBTX, USBRX);
+Serial rfid1(p9, NC);
+Serial rfid2(NC, p10);
+MODSERIAL esp(p28, p27);                                                                // tx, rx
+DigitalOut reset(p26);
+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 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 capext (p12) ;                                                                //Capteur de présence extérieur
+DigitalIn capint (p11) ;                                                                //Capteur de présence intérieur                                                       //Capteur détectant la fermeture 
+DigitalIn CTS(p8);
+DigitalIn F411 (p7);                                                                    //Detection alimentation servomoteur (CHALLENGE LEGRAND !!!!!!!!!!!)
+
+DigitalOut led1(LED1);                                                                  //Led n°1 du mbed
+DigitalOut led2(LED2);                                                                  //Led n°2 du mbed
+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 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
+
+int i = 0;
+int e = 0;
+int  count,ended,timeout;
+
+char buf[1024];
+char snd[255];
+
+char ssid[32] = "IOP_LEGRAND_2";                                                        //Nom du WIFI IOP
+char pwd [32] = "legrandMDP";                                                           //Mot de passe du WIFI IOP
+
+char ECHA[5] = "ECHA";                                                                  //Le chat vient de l'extérieur
+char ICHA[5] = "ICHA";                                                                  //Le chat vient de l'intérieur
+
+
+char tcpip[14] = "192.168.10.14";                                                       //Adresse IP du module ESP8666
+char tcpport[3] = "23";
+
+int longueur_recue = 0;                                                         
+int nouvelle_reception = 0;
+int a = 0;
+int b = 0;
+
+char recept[18];
+char tabconnect[10] = {0};
+char tabclosed[9] = {0};
+char tabrec[5] = {0};
+char connect[10] = "0,CONNECT";
+char closed[9] = "0,CLOSED";
+
+void HandShake();
+void Antenna();
+void field();
+void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(),WIFI(), Donnee();                //Déclaration des voids 
+
+using namespace std;
+
+int main()                                                                              //Début du int main
+{
+    
+    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);
+    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;
+
+    
+
+        if (CTS == 0) 
+        {
+            // pc.printf("- [CTS = 0] -");
+            // pc.printf("\n\r");
+            //HandShake();
+            for (int i=0; i<2; i++) 
+            {
+                rfid2.putc(lireuid[i]);
+            }
+            wait_ms(25);
+            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++) 
+                {
+                    rfid2.putc(lireuid[i]);
+                    led2=1;
+                }
+                wait_ms(50);
+                for (int e=1; e<5; e++) {
+                    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 : ");
+                for (int i=0; i<2; i++) pc.printf("%X ",lireuid[i]);
+                pc.printf("\n\r");
+                pc.printf("Status = %X\n\r",reponse_rfid1[0]);
+                pc.printf("UID = ");
+                for (int i=1; i<5; i++) pc.printf("%X ",reponse_rfid[i]);
+                pc.printf("\n\r");
+                pc.printf("---------------------------------------");
+                pc.printf("\n\r");
+            } 
+            else if (reponse_rfid1[0] == 0xC0) 
+            {
+                field();
+            }
+            if (reponse_rfid1[0] == 0xE0) 
+            {
+                Antenna();
+            }
+            //reponse_rfid[1]=0x00;
+            
+            for(int i=1; i<5; i++) 
+            {
+                tag[i-1] = reponse_rfid[i];
+            }
+            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]) 
+                    {
+                        egal1 = 1;
+                        pc.printf("Tag1 - OK ");
+                        pc.printf("\n\r");
+                        break;
+                        //pc.printf(tag[i]);
+                    } 
+                    else if(tag[i] != tag1[i]) 
+                    {
+                        pc.printf("Tag1 - NOK ");
+                        pc.printf("\n\r");
+                        egal1 = 0;
+                        break;
+                        //pc.printf(tag[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]) 
+                    {
+                        pc.printf("Tag2 - NOK ");
+                        pc.printf("\n\r");
+                        egal2 = 0;
+                        break;
+                        //pc.printf(tag[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]) 
+                    {
+                        pc.printf("Tag3 - NOK ");
+                        pc.printf("\n\r");
+                        egal3 = 0;
+                        break;
+                        //pc.printf(tag[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]) 
+                    {
+                        pc.printf("Tag4 - NOK ");
+                        pc.printf("\n\r");
+                        egal4 = 0;
+                        break;
+                        //pc.printf(tag[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]) 
+                    {
+                        pc.printf("Tag5 - NOK ");
+                        pc.printf("\n\r");
+                        egal5 = 0;
+                        break;
+                        //pc.printf(tag[i]);
+                   }
+                }
+            reponse_rfid[1]=0x00;
+            }
+
+            led1=0;
+            led2=0;
+            led3=0;
+
+            wait_ms(500);
+        
+    }
+    
+    
+    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
+    {
+        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                                                       
+        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
+            {
+                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)
+                }
+            }
+                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)
+                }
+            }
+                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°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°4
+            default :                                                                   //Fin du mode manuel                    
+        }                                                                               //Fin du switch
+    }                                                                                   //Fin du mode manuel
+    }
+}                                                                                       //Fin du int main
+        //+++++++++++++++++++++++++++++ Voids RFID +++++++++++++++++++++++++++++
+/*void HandShake(){
+  pc.printf("\n\r");
+  pc.printf("CTS IS TO 0");
+  pc.printf("\n\r");
+}*/
+
+void Antenna()
+{
+    pc.printf("\n\r");
+    pc.printf("ANTENNA NOT DETECTED");
+    pc.printf("\n\r");
+}
+
+void field()
+{
+    pc.printf("\n\r");
+    pc.printf("NOT TAG IN FIELD");
+    pc.printf("\n\r");
+}
+
+//  +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
+void ESPconfig()
+{
+    wait(5);
+    strcpy(snd,"AT\r\n");
+    SendCMD();
+    timeout=1;
+    getreply();
+    wait(1);
+    pc.printf("-------------- Redemarrage du module --------------\r\n");               //Redémarrage du module WIFI ESP8266
+    strcpy(snd,"AT+RST\r\n");                                                           //Commande redémarrage du module WIFI ESP8266
+    SendCMD();
+    timeout=5;
+    getreply();
+    pc.printf(buf);
+
+    wait(3);                                                                            //Délai inter-étape
+
+    pc.printf("\n---------- Mode Wifi ----------\r\n");                                 //Initialisation du mode WIFI
+    strcpy(snd, "AT+CWMODE=3\r\n");                                                     //Commande initialisation du module WIFI ESP8266
+    SendCMD();
+    timeout=4;
+    getreply();
+    pc.printf(buf);
+
+    wait(2);                                                                            //Délai inter-étape
+
+    pc.printf("\n---------- Connexion au point d acces ----------\r\n");                //Connection au serveur IOP
+    pc.printf("ssid = %s   pwd = %s\r\n",ssid,pwd);
+    strcpy(snd, "AT+CWJAP=\"");                                                         //Commande de connection au serveur IOP
+    strcat(snd, ssid);
+    strcat(snd, "\",\"");
+    strcat(snd, pwd);
+    strcat(snd, "\"\r\n");
+    SendCMD();
+    timeout=10;
+    getreply();
+    pc.printf(buf);                                                                     //Statut de l'étape (OK/ERROR)
+
+    wait(5);                                                                            //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
+    SendCMD();
+    timeout=3;
+    getreply();
+    pc.printf(buf);                                                                     //Statut de l'étape (OK/ERROR)
+
+    wait(5);                                                                            //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
+    SendCMD();
+    timeout=5;
+    getreply();
+    pc.printf(buf);                                                                     //Statut de l'étape (OK/ERROR)
+    
+    wait(5);                                                                            //Délai inter-étape
+    
+    
+}                                                                                       //Fin du void config
+
+void SendCMD()
+{
+    esp.printf("%s", snd);
+}                                                                               
+
+void getreply()
+{
+    memset(buf, '\0', sizeof(buf));
+    t.start();
+    ended=0;
+    count=0;
+    while(!ended) {
+        if(esp.readable()) {
+            buf[count] = esp.getc();
+            count++;
+        }
+        if(t.read() > timeout) {
+            ended = 1;
+            t.stop();
+            t.reset();
+        }
+    }
+}
\ No newline at end of file