ok

Dependencies:   mbed Projet-matthieu

Committer:
robin25
Date:
Mon May 20 09:10:38 2019 +0000
Revision:
0:17fcad2a7373
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
robin25 0:17fcad2a7373 1 #include "mbed.h"
robin25 0:17fcad2a7373 2 #include "WiflyInterface.h"
robin25 0:17fcad2a7373 3 #include <string.h>
robin25 0:17fcad2a7373 4 DigitalOut SetalarmOn(p26);
robin25 0:17fcad2a7373 5 Serial pc(USBTX, USBRX);
robin25 0:17fcad2a7373 6 #define On 0
robin25 0:17fcad2a7373 7 #define Off 1
robin25 0:17fcad2a7373 8
robin25 0:17fcad2a7373 9 /* creation de l'instance wifly
robin25 0:17fcad2a7373 10 -
robin25 0:17fcad2a7373 11 sécurité : WPA
robin25 0:17fcad2a7373 12 -
robin25 0:17fcad2a7373 13 SSID : vega
robin25 0:17fcad2a7373 14 -
robin25 0:17fcad2a7373 15 mt de passe : btssnvega
robin25 0:17fcad2a7373 16 Attention au sens des broches : TX RX
robin25 0:17fcad2a7373 17 Reset Status */
robin25 0:17fcad2a7373 18 WiflyInterface wifly(p9, p10, p12, p11, "vega", "btssnvega", WPA);
robin25 0:17fcad2a7373 19 //WiflyInterface wifly(p9, p10, p30, p29, "nom", "MDP", NONE);
robin25 0:17fcad2a7373 20 TCPSocketServer server;
robin25 0:17fcad2a7373 21 TCPSocketConnection client;
robin25 0:17fcad2a7373 22 int serveur_port = 2000;
robin25 0:17fcad2a7373 23 unsigned int NbcaracRecu = 0;
robin25 0:17fcad2a7373 24 int main()
robin25 0:17fcad2a7373 25 {
robin25 0:17fcad2a7373 26 bool client_connected = false;
robin25 0:17fcad2a7373 27 bool client_open = false;
robin25 0:17fcad2a7373 28 char buffer [100];
robin25 0:17fcad2a7373 29 pc.printf("Hello, I'm happy my Virtual Serial port is ok.\n\r");
robin25 0:17fcad2a7373 30 pc.printf("Init WIfi.\n\r");
robin25 0:17fcad2a7373 31 if (wifly.init("192.168.0.20","255.255.255.0","192.168.0.254")!=0) { //Innitialise interface ici dhcp
robin25 0:17fcad2a7373 32 pc.printf("ERREUR INIT ETHERNET\r\n");
robin25 0:17fcad2a7373 33 return-1;
robin25 0:17fcad2a7373 34 }
robin25 0:17fcad2a7373 35 pc.printf("Init Wifi ok.\n\rConnect Wifly.\n\r");
robin25 0:17fcad2a7373 36 if (!wifly.connect()) { // Connecte interface
robin25 0:17fcad2a7373 37 pc.printf("Tests en erreur, ERREUR Connect, quitte programme\r\n");
robin25 0:17fcad2a7373 38 return-1;
robin25 0:17fcad2a7373 39 // }
robin25 0:17fcad2a7373 40 }
robin25 0:17fcad2a7373 41 pc.printf("Ip sur Wifly is Ok.\n\r");
robin25 0:17fcad2a7373 42 printf("IP Address is %s\r\n",wifly.getIPAddress());
robin25 0:17fcad2a7373 43 pc.printf("\r\nVous devez maintenant installer Tcp.\n\r");
robin25 0:17fcad2a7373 44 if (server.bind (serveur_port)!=0) {
robin25 0:17fcad2a7373 45 pc.printf("Erreur Tcp bind\r\n");
robin25 0:17fcad2a7373 46 return-1;
robin25 0:17fcad2a7373 47 }
robin25 0:17fcad2a7373 48 pc.printf("Succes Tcp Bind\r\n");
robin25 0:17fcad2a7373 49 if (server.listen()!=0) {
robin25 0:17fcad2a7373 50 pc.printf("ERREUR Tcp Listen\r\n");
robin25 0:17fcad2a7373 51 return-1;
robin25 0:17fcad2a7373 52 }
robin25 0:17fcad2a7373 53 pc.printf("Succes Tcp listen\r\n");
robin25 0:17fcad2a7373 54 pc.printf("Connectez un client Tcp port %u...\r\n",serveur_port);
robin25 0:17fcad2a7373 55 client.set_blocking(false, 1); // Timeout after (100ms)
robin25 0:17fcad2a7373 56 while(1) {
robin25 0:17fcad2a7373 57 if (client_connected==false) {
robin25 0:17fcad2a7373 58 if (server.accept (client)!=0) {
robin25 0:17fcad2a7373 59 pc.printf("ERREUR connexion client\r\n"); // erreur
robin25 0:17fcad2a7373 60 return -1;
robin25 0:17fcad2a7373 61 }
robin25 0:17fcad2a7373 62 client_open = false;
robin25 0:17fcad2a7373 63 client_connected = true;
robin25 0:17fcad2a7373 64 pc.printf("Client Tcp connecte\r\n");
robin25 0:17fcad2a7373 65 NbcaracRecu = 0;
robin25 0:17fcad2a7373 66 } else {
robin25 0:17fcad2a7373 67 if ( client.is_connected() ) {
robin25 0:17fcad2a7373 68 if (pc.readable()) { // interface virtuel serie recoit donnees
robin25 0:17fcad2a7373 69 buffer[0] = pc.getc();
robin25 0:17fcad2a7373 70 client.send(buffer,1);
robin25 0:17fcad2a7373 71 }
robin25 0:17fcad2a7373 72 // lecture message
robin25 0:17fcad2a7373 73 int nbcaractere = client.receive(buffer+NbcaracRecu,sizeof(buffer)-NbcaracRecu);
robin25 0:17fcad2a7373 74 if (nbcaractere>0) { // interface Wifi recoit donnees
robin25 0:17fcad2a7373 75 NbcaracRecu = NbcaracRecu + nbcaractere;
robin25 0:17fcad2a7373 76 buffer[NbcaracRecu] = 0;
robin25 0:17fcad2a7373 77 // pour debug
robin25 0:17fcad2a7373 78 pc.printf("Jai recu ce jour : Totrecu = %u recu = %u phrase : %s\r\n Code Ascii : ",NbcaracRecu,nbcaractere,buffer);
robin25 0:17fcad2a7373 79 for (unsigned i = 0; i <NbcaracRecu; i++) {
robin25 0:17fcad2a7373 80 pc.printf("%u,",buffer[i]);
robin25 0:17fcad2a7373 81 }
robin25 0:17fcad2a7373 82 pc.printf("\r\n");
robin25 0:17fcad2a7373 83
robin25 0:17fcad2a7373 84 // si dernier octet est 10 alors message complet
robin25 0:17fcad2a7373 85
robin25 0:17fcad2a7373 86 if (buffer[NbcaracRecu-1] == 10) {
robin25 0:17fcad2a7373 87
robin25 0:17fcad2a7373 88 buffer[NbcaracRecu-2] = 0; // on met 0 en fin de message en retirant \r\n
robin25 0:17fcad2a7373 89
robin25 0:17fcad2a7373 90 // pour afficher pour verifier en debug
robin25 0:17fcad2a7373 91 for (unsigned i = 0; i <NbcaracRecu; i++) {
robin25 0:17fcad2a7373 92 pc.printf("%u,",buffer[i]);
robin25 0:17fcad2a7373 93 }
robin25 0:17fcad2a7373 94 pc.printf("\r\n");
robin25 0:17fcad2a7373 95 pc.printf("\r\n---------------------------------\r\n");
robin25 0:17fcad2a7373 96 pc.printf("Phrase complete : %s\r\n",buffer);
robin25 0:17fcad2a7373 97 // jusque la ...
robin25 0:17fcad2a7373 98
robin25 0:17fcad2a7373 99
robin25 0:17fcad2a7373 100 // on interprete les messages
robin25 0:17fcad2a7373 101 if (client_open == false) {
robin25 0:17fcad2a7373 102 if (strcmp(buffer,"connect;")==0) {
robin25 0:17fcad2a7373 103 pc.printf("Commande reconnue On\r\n");
robin25 0:17fcad2a7373 104 client.send("on\n\r",strlen("on\n\r"));
robin25 0:17fcad2a7373 105 client_open = true;
robin25 0:17fcad2a7373 106 } else {
robin25 0:17fcad2a7373 107 client_connected = false;
robin25 0:17fcad2a7373 108 client.close();
robin25 0:17fcad2a7373 109 pc.printf("Client deconnecte\r\n");
robin25 0:17fcad2a7373 110 pc.printf("Connectez un client Tcp port 2000...\r\n");
robin25 0:17fcad2a7373 111 }
robin25 0:17fcad2a7373 112 } else
robin25 0:17fcad2a7373 113 if (strcmp(buffer,"on;")==0) {
robin25 0:17fcad2a7373 114 pc.printf("Commande reconnue On\r\n");
robin25 0:17fcad2a7373 115 client.send("on\n\r",strlen("on\n\r"));
robin25 0:17fcad2a7373 116 SetalarmOn = On;
robin25 0:17fcad2a7373 117 }
robin25 0:17fcad2a7373 118 else if (strcmp(buffer,"off;")==0) {
robin25 0:17fcad2a7373 119 pc.printf("Commande reconnue Off\r\n");
robin25 0:17fcad2a7373 120 client.send("off\n\r",strlen("off\n\r"));
robin25 0:17fcad2a7373 121 SetalarmOn = Off;
robin25 0:17fcad2a7373 122 }
robin25 0:17fcad2a7373 123 else pc.printf("Commande inconnu\r\n");
robin25 0:17fcad2a7373 124
robin25 0:17fcad2a7373 125 NbcaracRecu = 0;
robin25 0:17fcad2a7373 126
robin25 0:17fcad2a7373 127 }
robin25 0:17fcad2a7373 128 }
robin25 0:17fcad2a7373 129 } else {
robin25 0:17fcad2a7373 130 client_connected = false;
robin25 0:17fcad2a7373 131 client_open = false;
robin25 0:17fcad2a7373 132 client.close();
robin25 0:17fcad2a7373 133 pc.printf("Client deconnecte\r\n");
robin25 0:17fcad2a7373 134 pc.printf("Connectez un client Tcp port 2000...\r\n");
robin25 0:17fcad2a7373 135 }
robin25 0:17fcad2a7373 136 }
robin25 0:17fcad2a7373 137 }
robin25 0:17fcad2a7373 138 }