Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente | |||
| asso:si:serrure [2026/10/12 22:13] – Serrure HUA XING QIANG, alimentation DC-DC, boîtier, bouton garage et photos nrineau | asso:si:serrure [2026/10/12 22:15] (Version actuelle) – Ajout du code source du Pi (secrets masqués) : gate-service.sh, gate.c, bouton garage, services nrineau | ||
|---|---|---|---|
| Ligne 68: | Ligne 68: | ||
| | '' | | '' | ||
| | '' | | '' | ||
| + | |||
| + | ==== Code source ==== | ||
| + | |||
| + | Version en service sur le Pi (''/ | ||
| + | |||
| + | '' | ||
| + | |||
| + | === gate-service.sh : décision ouvrir / fermer === | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | |||
| + | cd $(dirname " | ||
| + | LOG=./ | ||
| + | OPEN_DELAY=4 | ||
| + | |||
| + | validate_id() { | ||
| + | awk "/^$1 / { print \$2 }" ./perm.list | ||
| + | } | ||
| + | |||
| + | gate_error() { | ||
| + | ./gate beepe | ||
| + | } | ||
| + | |||
| + | gate_open() { | ||
| + | ./gate beepo | ||
| + | ./gate open | ||
| + | curl --connect-timeout 3 < | ||
| + | curl --connect-timeout 3 -i -X POST -H ' | ||
| + | } | ||
| + | |||
| + | gate_close() { | ||
| + | ./gate beepc | ||
| + | ./gate close | ||
| + | curl --connect-timeout 3 < | ||
| + | curl --connect-timeout 3 -i -X POST -H ' | ||
| + | } | ||
| + | |||
| + | # at boot make sure we are closed | ||
| + | ./gate close | ||
| + | |||
| + | while true; do | ||
| + | inotifywait -qq -e modify " | ||
| + | date | ||
| + | l=$(tail -n 1 " | ||
| + | if ! grep -q 'Read 26 bits' <<< | ||
| + | echo " | ||
| + | continue | ||
| + | fi | ||
| + | c=$(awk '{ print $7 }' <<< | ||
| + | if [ -z " | ||
| + | echo "empty code" | ||
| + | continue | ||
| + | fi | ||
| + | name=$(validate_id " | ||
| + | if [ -z " | ||
| + | echo " | ||
| + | gate_error | ||
| + | continue | ||
| + | fi | ||
| + | echo " | ||
| + | if inotifywait -qq -t " | ||
| + | l=$(tail -n 1 " | ||
| + | if ! grep -q 'Read 26 bits' <<< | ||
| + | echo " | ||
| + | gate_error | ||
| + | continue | ||
| + | fi | ||
| + | c2=$(awk '{ print $7 }' <<< | ||
| + | if [ " | ||
| + | echo "code 1 doesn' | ||
| + | continue | ||
| + | fi | ||
| + | echo " | ||
| + | gate_open " | ||
| + | else | ||
| + | echo " | ||
| + | gate_close " | ||
| + | fi | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | * Une ligne est valide si elle contient '' | ||
| + | * Après un badge autorisé, le script attend une 2ᵉ lecture pendant '' | ||
| + | |||
| + | === gate.c : commande de la serrure et des bips === | ||
| + | |||
| + | Compilation : '' | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | * linked with -lpthread -lwiringPi -lrt | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define PIN_C 15 | ||
| + | #define PIN_O 16 | ||
| + | #define PIN_B 7 | ||
| + | |||
| + | int gate_close() { | ||
| + | pinMode(PIN_C, | ||
| + | digitalWrite(PIN_C, | ||
| + | delay(1000); | ||
| + | digitalWrite(PIN_C, | ||
| + | pinMode(PIN_C, | ||
| + | pullUpDnControl(PIN_C, | ||
| + | } | ||
| + | |||
| + | int gate_open() { | ||
| + | pinMode(PIN_O, | ||
| + | digitalWrite(PIN_O, | ||
| + | delay(1000); | ||
| + | digitalWrite(PIN_O, | ||
| + | pinMode(PIN_O, | ||
| + | pullUpDnControl(PIN_O, | ||
| + | } | ||
| + | |||
| + | /* close sound */ | ||
| + | int gate_beepc() { | ||
| + | pinMode(PIN_B, | ||
| + | digitalWrite(PIN_B, | ||
| + | delay(150); | ||
| + | digitalWrite(PIN_B, | ||
| + | pinMode(PIN_B, | ||
| + | pullUpDnControl(PIN_B, | ||
| + | delay(100); | ||
| + | pinMode(PIN_B, | ||
| + | digitalWrite(PIN_B, | ||
| + | delay(150); | ||
| + | digitalWrite(PIN_B, | ||
| + | pinMode(PIN_B, | ||
| + | pullUpDnControl(PIN_B, | ||
| + | } | ||
| + | |||
| + | /* open sound */ | ||
| + | int gate_beepo() { | ||
| + | pinMode(PIN_B, | ||
| + | digitalWrite(PIN_B, | ||
| + | delay(1000); | ||
| + | digitalWrite(PIN_B, | ||
| + | pinMode(PIN_B, | ||
| + | pullUpDnControl(PIN_B, | ||
| + | } | ||
| + | |||
| + | /* error sound */ | ||
| + | int gate_beepe() { | ||
| + | for (int i = 0; i < 10; i++) { | ||
| + | pinMode(PIN_B, | ||
| + | digitalWrite(PIN_B, | ||
| + | delay(50); | ||
| + | digitalWrite(PIN_B, | ||
| + | pinMode(PIN_B, | ||
| + | pullUpDnControl(PIN_B, | ||
| + | delay(50); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | /* garage sound */ | ||
| + | int gate_beepg() { | ||
| + | pinMode(PIN_B, | ||
| + | |||
| + | #define TADA(on, off) digitalWrite(PIN_B, | ||
| + | #define C TADA(80, 120) | ||
| + | #define CA TADA(120, 80) | ||
| + | |||
| + | for(int i = 0; i < 4; i++) { | ||
| + | CA C C | ||
| + | CA C C | ||
| + | CA C C | ||
| + | CA C CA C | ||
| + | } | ||
| + | |||
| + | |||
| + | pinMode(PIN_B, | ||
| + | pullUpDnControl(PIN_B, | ||
| + | } | ||
| + | |||
| + | void main(int argc, char* argv[]) { | ||
| + | wiringPiSetup() ; | ||
| + | |||
| + | if (argc >=2 && strncmp(argv[1], | ||
| + | gate_open(); | ||
| + | } else if (argc >=2 && strncmp(argv[1], | ||
| + | gate_beepc(); | ||
| + | } else if (argc >=2 && strncmp(argv[1], | ||
| + | gate_beepo(); | ||
| + | } else if (argc >=2 && strncmp(argv[1], | ||
| + | gate_beepe(); | ||
| + | } else if (argc >=2 && strncmp(argv[1], | ||
| + | gate_beepg(); | ||
| + | } else { | ||
| + | gate_close(); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === garage_button.c et garage_button.sh : bouton garage === | ||
| + | |||
| + | '' | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | * linked with -lpthread -lwiringPi -lrt | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define PIN_BUTTON 16 | ||
| + | |||
| + | void main(int argc, char* argv[]) { | ||
| + | wiringPiSetupGpio() ; | ||
| + | pinMode(PIN_BUTTON, | ||
| + | pullUpDnControl(PIN_BUTTON, | ||
| + | |||
| + | /* wait for the button to be released */ | ||
| + | while(digitalRead(PIN_BUTTON) == 0) { | ||
| + | delay(100); | ||
| + | } | ||
| + | // | ||
| + | |||
| + | while(1) { | ||
| + | if(digitalRead(PIN_BUTTON) == 0) { | ||
| + | delay(500); | ||
| + | if(digitalRead(PIN_BUTTON) == 0) { | ||
| + | exit(0); | ||
| + | } | ||
| + | } | ||
| + | delay(100); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | |||
| + | cd / | ||
| + | |||
| + | while true; do | ||
| + | ./ | ||
| + | ./gate beepg | ||
| + | echo " | ||
| + | sleep 8 | ||
| + | echo " | ||
| + | curl -v http:// | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | === Services systemd === | ||
| + | |||
| + | <code ini> | ||
| + | # / | ||
| + | [Unit] | ||
| + | Description=LOV gate service | ||
| + | |||
| + | [Service] | ||
| + | Type=simple | ||
| + | User=root | ||
| + | WorkingDirectory=/ | ||
| + | ExecStart=/ | ||
| + | Restart=on-failure | ||
| + | |||
| + | [Install] | ||
| + | WantedBy=multi-user.target | ||
| + | |||
| + | # / | ||
| + | [Unit] | ||
| + | Description=Wiegand badge reader | ||
| + | |||
| + | [Service] | ||
| + | Type=simple | ||
| + | User=root | ||
| + | WorkingDirectory=/ | ||
| + | ExecStart=/ | ||
| + | Restart=on-failure | ||
| + | |||
| + | [Install] | ||
| + | WantedBy=multi-user.target | ||
| + | </ | ||
| + | |||
| + | Le bouton garage n'est pas un service : il est lancé par ''/ | ||
| ==== Commandes utiles ==== | ==== Commandes utiles ==== | ||