1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#include <SPI.h> //bibliothèqe pour SPI
#include <Ethernet.h> //bibliothèque pour Ethernet
#include <Keypad.h> // inclusion de la librairie pour clavier matriciel
byte mac[] = {0x90, 0xA2, 0xDA, 0x0F, 0xDF, 0xAB}; //adresse mac de votre carte
IPAddress IPserver(192,168,1,3);
IPAddress IParduino(192,168,1,52);
EthernetServer serveur(80); // déclare l'objet serveur au port d'écoute 80
// Clavier
//********
const int BrocheClavier1 = 2;
const int BrocheClavier2 = 3;
const int BrocheClavier3 = 4;
const int BrocheClavier4 = 5;
const int BrocheClavier5 = 6;
const int BrocheClavier6 = 7;
const int BrocheClavier7 = 8;
char Clavier[5];
int compteur = 0;
const byte ROWS = 4; // Définition du nombre de lignes
const byte COLS = 3; // Définition du nombre de colonnes
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[ROWS] = { BrocheClavier7, BrocheClavier6, BrocheClavier5, BrocheClavier4}; // Définition des ports où sont raccordées les lignes
byte colPins[COLS] = {BrocheClavier3, BrocheClavier2, BrocheClavier1}; // Définition des ports où sont raccordées les colonnes
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Création du clavier
int Code ; // Code pour digicode
void setup()
{
pinMode(13, OUTPUT);
pinMode(10, OUTPUT);
digitalWrite(13, LOW);
digitalWrite(10, LOW);
Serial.begin (9600); //initialisation de communication série
Ethernet.begin (mac, IParduino); //initialisation de la communication Ethernet
Serial.print("*\n-> Le serveur est sur l'adresse : ");
Serial.println(Ethernet.localIP()); //on affiche l'adresse IP de la connexion
EthernetClient client;
serveur.begin(); // démarre l'écoute
}
void loop() {
// Lecture clavier
//---------------------
char key = keypad.getKey(); // Lecture de la touche actionnée
if (key != NO_KEY) // Si une touche est actionnée
{
Clavier[compteur++]= key;
Clavier[compteur]= '\0'; // termine la chaîne de caractères
if (key == '#') // clavier digiode
{
Code= atoi(Clavier); // conversion en int
compteur = 0;
Serial.println(Code);
//gestionClient();
Envoyer();
// return;
}
}
gestionClient(); // fonction qui gère toute la communication avec le client
}
//----------------------Fonctions----------------------
//fonction qui gère la communication avec le client
void gestionClient() {
EthernetClient client = serveur.available(); //on écoute le port
if (client) { //si client existe
Serial.print("Code dans la fonction gestion client");
Serial.println(Code);
Serial.println("Client en ligne"); //on le dit...
while (client.connected()) { // si le client est connecté
GET(client); //appel de la fonction de décodage
Serial.println("Fin de communication avec le client\n");
client.stop(); //on déconnecte le client
}
}
}
//fonctin décodage GET
void GET(EthernetClient cl) {
boolean lu = 0; //variable pour indiquer l'état de lecture
while (cl.available()) { // tant qu'il a des infos à transmettre
char c = cl.read(); // on lit le caractère
delay(1); //delai de lecture
if (c == '?' && lu == 0) { //si "?" repéré
c = cl.read(); //on lit le caractère suivant qui contient la donnée
int lecture = int(c); //on la transforme en nombre
if (lecture == 77)
{
Serial.println("Code Master\n");
digitalWrite(13, HIGH);
}
else if (lecture == 86)
{
Serial.println("Code Visiteur\n");
digitalWrite(10, HIGH);
}
else
{
Serial.println("erreur code\n");
}
delay(1000);
lu = 1; // on dit qu'on a lu l'info
digitalWrite(13, LOW);
digitalWrite(10, LOW);
}
}
}
void Envoyer() {
EthernetClient client;
if (client.connect(IPserver, 80))
{
Serial.println("-> Connected");
// Make a HTTP request:
client.print( "GET /domotique/traitement-digicode.php/?Code=");
Serial.println("GET /domotique/traitement-digicode.php?");
// client.print("Code=");
// Serial.println("Code=");
client.print(Code);
Serial.println(Code);
//client.print("&&");
client.println( " HTTP/1.1");
client.println( "Host: 192.168.1.3" );
// client.println(IPserver);
//client.println( "Content-Type: application/x-www-form-urlencoded" );
client.println( "Connection: close" );
client.println();
client.println();
client.stop();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
} |
Partager