Control zmtrigger with your own alarm and arduino

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
sagitt
Posts: 32
Joined: Mon Apr 30, 2012 1:13 pm

Control zmtrigger with your own alarm and arduino

Post by sagitt » Sun Jan 28, 2018 10:39 am

Hi, i worked hard on this. I created an Arduino sketch to send commands to zmtrigger.pl and execute commands reading Serial Output from an Arduino with an Ethernet Shield. I tested it on Arduino UNO but has some instability issue due low memory. So it tested it on Arduino Mega 2560 and all works geat. (Arduino IDE 1.8.5 or 1.9.0 beta and on Server side i'm using Ubuntu Server 16.04 LTS).

I called it "AlarmControl for zmtrigger.pl and cmd_zm". I've not found other similar on the web, only some base sketch for ESP8266 and similar.

It have:
- Commands for 7 ZoneMinder Monitors (Is easy add more monitor and can use zm_cmd_status_X to enable or disable all o single monitor command [true\false]);
- Command status with LED for see when the command are sent (Green and Red LED -> PIN 12 and 11 + GND);
- Connection and Flood\Wait check with LED Blink (Blue LED -> PIN 13 + GND)
- Realtime Signal Check (With Timers)
- Anti-Flood (Very hard to integrate but very useful - Use Blue LED for Enabled status)
- Full DEBUG output (In Italian, but is easy to translate with google translator)


It checks when the Alarm close a pin to GND:
- I used PIN A0 and GND for zmtrigger.pl. When you link them, it send instantaneally the commands to zmtrigger.pl to record. it send for 4 times, [4 times * Main delay (cmd_delay_0)] (After this it goes in anti-flood mode). if you disconnect A0 PIN from GND, it send however the stop record command. (Alarm ringing, so it record)
- I used PIN 9/10 and GND for cmd_zm (My Bash\Perl Scripts). When you link them, it send instantaneally the commands to enable or disable Modect mode (Alarm enabled, so it record only on move - Alarm disabled, so it record only when ringing)

1. Use https://www.miniwebtool.com/mac-address-generator/ for generate a MAC Address.
2. This sketch need Timer.h Library https://playground.arduino.cc/Code/Timer.

ARDUINO SKETCH:

AlarmControl.ino: -> Change MAC Address and Client\Server IP

Code: Select all

// AlarmControl for zmtrigger.pl - v2.0 - By Sagitt (Email: sagitt@email.it).

// Importazione Librerie Ethernet Shield
#include <SPI.h>
#include <Ethernet.h>
#include <Timer.h>

// Impostazione Variabili Copyright
PROGMEM const String sketch_name = "AlarmControl for zmtrigger.pl";
PROGMEM const String sketch_version = "2.0";
PROGMEM const String sketch_creator = "Sagitt";

// Impostazione Variabili Ethernet Shield
PROGMEM const int server_port = 6802;
PROGMEM const String client_mac_address_print = "AA:BB:CC:DD:EE:FF"; // <- CHANGE IT
byte client_mac_address[] = { 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF }; // <- CHANGE IT
IPAddress client_ip_address(192, 168, 0, 100); // <- CHANGE IT
IPAddress server_ip_address(192, 168, 0, 200); // <- CHANGE IT
EthernetClient client;

// Impostazione Variabili PIN zmtrigger.pl
PROGMEM const int pin_0 = A0;                  // Allarme Suona

// Impostazione Variabili PIN cmd_zm
PROGMEM const int pin_4 = 9;                   // Allarme Attivato
PROGMEM const int pin_5 = 10;                  // Allarme Disattivato

// Impostazione Variabili PIN LED
PROGMEM const int pin_1 = 12;                  // Verde
PROGMEM const int pin_2 = 11;                  // Rosso
PROGMEM const int pin_3 = 13;                  // Blu

// Impostazione Variabili Valori PIN
int pin_0_value = 1;
int pin_4_value = 1;
int pin_5_value = 1;
int pin_1_2_3_value = 1;
int pin_1_2_3_cmdled = 1;

// Impostazione Variabili Generiche
PROGMEM const int debug_mode = 0;               // 0 = Disabilitata | 1 = Solo temporizzazione | 2 = Temporizzazione e Avvisi | >= 3 = Tutto
boolean cmd_status_sent = false;
boolean cmd_status_delay = false;
boolean cmd_status_flood = false;
boolean cmd_status_flood_debug = false;
boolean cmd_status_led = true;
PROGMEM const int cmd_flood_limit = 4;
int cmd_flood_inc = 0;
int cmd_delay_timer = 0;
int cmd_flood_timer = 0;

// Impostazione Variabili Temporizzazione e Anti-Flood
Timer antiflood;
Timer cmddelay;
PROGMEM const long fast_delay = 100;
PROGMEM const long slow_delay = 1000;
PROGMEM const long wait_delay = 300;
PROGMEM const long cmd_delay_0 = 150000;         // Reale
PROGMEM const long cmd_delay_0_debug = 15000;    // Debug
PROGMEM const long cmd_delay_1 = 500;
PROGMEM const long cmd_flood_delay_0 = cmd_delay_0 * 2;
PROGMEM const long cmd_flood_delay_1 = 500;
PROGMEM const long cmd_flood_delay_2 = 5000;
PROGMEM const long cmd_flood_delay_3 = 10000;
PROGMEM const long cmd_flood_time_0 = ( ( cmd_delay_0 * cmd_flood_limit ) / 1000 ) / 60;
PROGMEM const long cmd_flood_time_1 = ( ( cmd_flood_delay_0 ) / 1000 ) / 60;

// Impostazione Variabili Comando zmtrigger.pl
int zm_alarm_type = 1;                                  // 1 = Ringing | 2 = Stopped
String zm_alarm_string_0 = "";
String zm_alarm_string_1 = "";
PROGMEM const String zm_alarm_ringing_0 = "|on+145|200|Alarm|Ringing|- Alarm";
PROGMEM const String zm_alarm_ringing_1 = "1" + zm_alarm_ringing_0;
PROGMEM const String zm_alarm_ringing_2 = "2" + zm_alarm_ringing_0;
PROGMEM const String zm_alarm_ringing_3 = "3" + zm_alarm_ringing_0;
PROGMEM const String zm_alarm_ringing_4 = "4" + zm_alarm_ringing_0;
PROGMEM const String zm_alarm_ringing_5 = "5" + zm_alarm_ringing_0;
PROGMEM const String zm_alarm_ringing_6 = "6" + zm_alarm_ringing_0;
PROGMEM const String zm_alarm_ringing_7 = "7" + zm_alarm_ringing_0;
PROGMEM const String zm_alarm_stopped_0 = "|off|0|Alarm|Stopped|- Alarm";
PROGMEM const String zm_alarm_stopped_1 = "1" + zm_alarm_stopped_0;
PROGMEM const String zm_alarm_stopped_2 = "2" + zm_alarm_stopped_0;
PROGMEM const String zm_alarm_stopped_3 = "3" + zm_alarm_stopped_0;
PROGMEM const String zm_alarm_stopped_4 = "4" + zm_alarm_stopped_0;
PROGMEM const String zm_alarm_stopped_5 = "5" + zm_alarm_stopped_0;
PROGMEM const String zm_alarm_stopped_6 = "6" + zm_alarm_stopped_0;
PROGMEM const String zm_alarm_stopped_7 = "7" + zm_alarm_stopped_0;
PROGMEM const String zm_alarm_cancel_0 = "|off|0|Alarm|Cancelled|- Alarm";
PROGMEM const String zm_alarm_cancel_1 = "1" + zm_alarm_cancel_0;
PROGMEM const String zm_alarm_cancel_2 = "2" + zm_alarm_cancel_0;
PROGMEM const String zm_alarm_cancel_3 = "3" + zm_alarm_cancel_0;
PROGMEM const String zm_alarm_cancel_4 = "4" + zm_alarm_cancel_0;
PROGMEM const String zm_alarm_cancel_5 = "5" + zm_alarm_cancel_0;
PROGMEM const String zm_alarm_cancel_6 = "6" + zm_alarm_cancel_0;
PROGMEM const String zm_alarm_cancel_7 = "7" + zm_alarm_cancel_0;

// Impostazione Variabili Comando cmd_zm
int cmd_zm_type = 0;                                  // 1 = Attivato | 2 = Disattivato
PROGMEM const String cmd_zm_string_0 = "+ALARM+";
PROGMEM const String cmd_zm_string_1 = "-ALARM-";

// Impostazione Variabili Stato zmtrigger.pl e cmd_zm
PROGMEM const boolean zm_cmd_status_0 = true;   // Tutte
PROGMEM const boolean zm_cmd_status_1 = true;   // Balcone
PROGMEM const boolean zm_cmd_status_2 = true;   // Ingresso
PROGMEM const boolean zm_cmd_status_3 = true;  // Soggiorno
PROGMEM const boolean zm_cmd_status_4 = true;  // Corridoio
PROGMEM const boolean zm_cmd_status_5 = true;  // Camera 1
PROGMEM const boolean zm_cmd_status_6 = true;  // Camera 2
PROGMEM const boolean zm_cmd_status_7 = true;  // Camera 3

// Configurazione Iniziale Dispositivo
void setup() {
  // Impostazione Stato PIN
  pinMode(pin_0, INPUT_PULLUP);
  pinMode(pin_4, INPUT_PULLUP);
  pinMode(pin_5, INPUT_PULLUP);
  pinMode(pin_1, OUTPUT);
  pinMode(pin_2, OUTPUT);
  pinMode(pin_3, OUTPUT);
  // Impostazione Velocita' Porta Seriale
  Serial.begin(115200);
  Serial.print(sketch_name);
  Serial.print(" - v");
  Serial.print(sketch_version);
  Serial.print(" - By ");
  Serial.println(sketch_creator);
  Serial.println();
  Serial.println("Avvio del sistema in corso...");
  Serial.println();
  // Impostazione Indirizzo IP Ethernet Shield
  Ethernet.begin(client_mac_address, client_ip_address);
  delay(slow_delay);
  // Test Connessione Server
  Serial.print("MAC del client: \t");
  Serial.println(client_mac_address_print);
  Serial.print("IP del client:  \t");
  Serial.println(client_ip_address);
  Serial.print("IP del server:  \t");
  Serial.println(server_ip_address);
  Serial.print("Porta del server: \t");
  Serial.println(server_port);
  Serial.println();
  delay(slow_delay);
  Serial.println("Avvio test di funzionamento dei LED in corso...");
  Serial.println();
  delay(fast_delay);
  Serial.print("1 -> \t LED 1 -> \t PIN: ");
  Serial.println(pin_1);
  ledblink(0, 1);
  Serial.print("2 -> \t LED 2 -> \t PIN: ");
  Serial.println(pin_2);
  ledblink(0, 2);
  Serial.print("3 -> \t LED 3 -> \t PIN: ");
  Serial.println(pin_3);
  ledblink(0, 3);
  Serial.println();
  delay(slow_delay);
  Serial.println("Verifica stato dispositivi attivi per l'invio dei comandi in corso...");
  if (zm_cmd_status_0 == false) {
    Serial.println("L'invio dei comandi è stato disabilitato!");
  }
  else {
    Serial.print("1 -> \t ");
    if (zm_cmd_status_1 == true) {
      Serial.println("ATTIVATO");
    }
    else {
      Serial.println("DISATTIVATO");
    }
    Serial.print("2 -> \t ");
    if (zm_cmd_status_2 == true) {
      Serial.println("ATTIVATO");
    }
    else {
      Serial.println("DISATTIVATO");
    }
    Serial.print("3 -> \t ");
    if (zm_cmd_status_3 == true) {
      Serial.println("ATTIVATO");
    }
    else {
      Serial.println("DISATTIVATO");
    }
    Serial.print("4 -> \t ");
    if (zm_cmd_status_4 == true) {
      Serial.println("ATTIVATO");
    }
    else {
      Serial.println("DISATTIVATO");
    }
    Serial.print("5 -> \t ");
    if (zm_cmd_status_5 == true) {
      Serial.println("ATTIVATO");
    }
    else {
      Serial.println("DISATTIVATO");
    }
    Serial.print("6 -> \t ");
    if (zm_cmd_status_6 == true) {
      Serial.println("ATTIVATO");
    }
    else {
      Serial.println("DISATTIVATO");
    }
    Serial.print("7 -> \t ");
    if (zm_cmd_status_7 == true) {
      Serial.println("ATTIVATO");
    }
    else {
      Serial.println("DISATTIVATO");
    }
  }
  Serial.println();
  delay(slow_delay);
  Serial.println("Avvio test di connessione al server in corso...");
  if (client.connect(server_ip_address, server_port)) {
    Serial.println("Test riuscito [A] - Connessione stabilita!");
    Serial.println();
    delay(fast_delay);
    client.stop();
  }
  else {
    Serial.println("Test fallito [B] - Connessione non riuscita!");
    Serial.println();
    delay(fast_delay);
    client.stop();
  }
  delay(slow_delay);
}

// Ciclo Analisi Segnali Dispositivo
void loop() {
  //Serial.println("Loop Debug");                                           // Debug "-1"
  antiflood.update();
  cmddelay.update();
  delay(fast_delay);
  pin_0_value = digitalRead(pin_0);
  pin_4_value = digitalRead(pin_4);
  pin_5_value = digitalRead(pin_5);
  if (debug_mode >= 2) {
    Serial.print("Valore PIN: ");                                           // Debug
    Serial.print("A0");                                                     // Debug
    Serial.print(" rilevato: ");                                            // Debug
    Serial.println(pin_0_value);                                            // Debug
    Serial.print("Valore PIN: ");                                           // Debug
    Serial.print(pin_4);                                                    // Debug
    Serial.print(" rilevato: ");                                            // Debug
    Serial.println(pin_4_value);                                            // Debug
    Serial.print("Valore PIN: ");                                           // Debug
    Serial.print(pin_5);                                                    // Debug
    Serial.print(" rilevato: ");                                            // Debug
    Serial.println(pin_5_value);                                            // Debug
  }
  delay(fast_delay);
  if (pin_4_value == 0) {
    cmd_zm(1);
  }
  if (pin_5_value == 0) {
    cmd_zm(2);
  }
  delay(fast_delay);
  if (!client.connected()) {
    if (debug_mode >= 2) {
      Serial.println("Non connesso - Attesa di un evento in corso...");     // Debug
    }
    client.stop();
  }
  delay(fast_delay);
  if (cmd_status_delay == false) {
    if (cmd_status_flood == false) {
      if (pin_0_value == 0) {
        if (cmd_flood_inc < cmd_flood_limit) {
          ++cmd_flood_inc;
          cmd_status_sent = true;
          cmd_status_flood = false;
          cmd_status_flood_debug = true;
          cmd_status_delay = false;
          Serial.println("Segnale rilevato [A] - Invio dei comandi in corso...");
          Serial.print("Invio: ");
          Serial.print(cmd_flood_inc);
          Serial.print(" di ");
          Serial.print(cmd_flood_limit);
          if (cmd_flood_time_0 > 0) {
            Serial.print(" [");
            Serial.print(cmd_flood_time_0);
            if (cmd_flood_time_0 == 1) {
              Serial.println(" minuto complessivo di registrazione]");
            }
            else {
              Serial.println(" minuti complessivi di registrazione]");
            }
          }
          Serial.println();
          delay(fast_delay);
          disableantiflood();
          delay(fast_delay);
          enablecmddelay();
          delay(fast_delay);
          zm_alarm_type = 1;
          zmtrigger();
          Serial.println();
          delay(fast_delay);
          client.stop();
          delay(cmd_flood_delay_2);
          if (cmd_flood_inc >= cmd_flood_limit) {
            enableantiflood();
            delay(fast_delay);
            goto cmd_flood_enabled;
          }
          else {
            delay(fast_delay);
            goto cmd_delay_enabled;
          }
        }
        else {
          enableantiflood();
          delay(fast_delay);
          goto cmd_flood_enabled;
        }
      }
      else {
cmd_signal_check:
        if (cmd_status_sent == true) {
          cmd_flood_inc = 0;
          cmd_status_sent = false;
          cmd_status_flood = false;
          cmd_status_flood_debug = true;
          cmd_status_delay = false;
          Serial.println("Segnale NON più rilevato [B] - Invio dei comandi in corso...");
          Serial.print("Ripristino valore invii a: ");
          Serial.println(cmd_flood_inc);
          Serial.println();
          delay(fast_delay);
          disableantiflood();
          delay(fast_delay);
          zm_alarm_type = 2;
          zmtrigger();
          Serial.println();
          delay(fast_delay);
          client.stop();
          delay(cmd_flood_delay_2);
        }
      }
    }
    else {
cmd_flood_enabled:
      cmd_flood_inc = 0;
      cmd_status_delay = false;
      if (pin_0_value == 1) {
        if (cmd_status_sent == true) {
          cmd_status_flood_debug = false;
          disableantiflood();
          delay(fast_delay);
          goto cmd_signal_check;
        }
      }
      if (debug_mode >= 2) {
        Serial.println("Anti-Flood attivo - In attesa della disattivazione...");       // Debug
      }
    }
  }
  else {
cmd_delay_enabled:
    if (cmd_status_flood == false) {
      if (cmd_status_sent == true) {
        ledblink(1, 3);
      }
    }
    if (pin_0_value == 1) {
      if (cmd_status_sent == true) {
        disablecmddelay();
        delay(fast_delay);
        goto cmd_signal_check;
      }
    }
  }
}

// Esecuzione Comando zmtrigger.pl
void zmtrigger() {
  if (client.connect(server_ip_address, server_port)) {
    if (client.connected()) {
      if (zm_cmd_status_0 == true) {
        ledblink(3, 0);
        zm_alarm_string_0 = "";
        zm_alarm_string_1 = "";
        delay(fast_delay);
        if (zm_cmd_status_1 == true) {
          if (zm_alarm_type == 1) {
            zm_alarm_string_0 = zm_alarm_ringing_1;
            zm_alarm_string_1 = zm_alarm_cancel_1;
          }
          else {
            zm_alarm_string_0 = zm_alarm_stopped_1;
          }
          if (zm_alarm_string_1 == zm_alarm_cancel_1) {
            client.println(zm_alarm_string_1);
            Serial.print("1 -> \t ");
            Serial.println(zm_alarm_string_1);
            delay(wait_delay);
          }
          client.println(zm_alarm_string_0);
          Serial.print("1 -> \t ");
          Serial.println(zm_alarm_string_0);
          ledblink(1, zm_alarm_type);
        }
        if (zm_cmd_status_2 == true) {
          if (zm_alarm_type == 1) {
            zm_alarm_string_0 = zm_alarm_ringing_2;
            zm_alarm_string_1 = zm_alarm_cancel_2;
          }
          else {
            zm_alarm_string_0 = zm_alarm_stopped_2;
          }
          if (zm_alarm_string_1 == zm_alarm_cancel_2) {
            client.println(zm_alarm_string_1);
            Serial.print("2 -> \t ");
            Serial.println(zm_alarm_string_1);
            delay(wait_delay);
          }
          client.println(zm_alarm_string_0);
          Serial.print("2 -> \t ");
          Serial.println(zm_alarm_string_0);
          ledblink(1, zm_alarm_type);
        }
        if (zm_cmd_status_3 == true) {
          if (zm_alarm_type == 1) {
            zm_alarm_string_0 = zm_alarm_ringing_3;
            zm_alarm_string_1 = zm_alarm_cancel_3;
          }
          else {
            zm_alarm_string_0 = zm_alarm_stopped_3;
          }
          if (zm_alarm_string_1 == zm_alarm_cancel_3) {
            client.println(zm_alarm_string_1);
            Serial.print("3 -> \t ");
            Serial.println(zm_alarm_string_1);
            delay(wait_delay);
          }
          client.println(zm_alarm_string_0);
          Serial.print("3 -> \t ");
          Serial.println(zm_alarm_string_0);
          ledblink(1, zm_alarm_type);
        }
        if (zm_cmd_status_4 == true) {
          if (zm_alarm_type == 1) {
            zm_alarm_string_0 = zm_alarm_ringing_4;
            zm_alarm_string_1 = zm_alarm_cancel_4;
          }
          else {
            zm_alarm_string_0 = zm_alarm_stopped_4;
          }
          if (zm_alarm_string_1 == zm_alarm_cancel_4) {
            client.println(zm_alarm_string_1);
            Serial.print("4 -> \t ");
            Serial.println(zm_alarm_string_1);
            delay(wait_delay);
          }
          client.println(zm_alarm_string_0);
          Serial.print("4 -> \t ");
          Serial.println(zm_alarm_string_0);
          ledblink(1, zm_alarm_type);
        }
        if (zm_cmd_status_5 == true) {
          if (zm_alarm_type == 1) {
            zm_alarm_string_0 = zm_alarm_ringing_5;
            zm_alarm_string_1 = zm_alarm_cancel_5;
          }
          else {
            zm_alarm_string_0 = zm_alarm_stopped_5;
          }
          if (zm_alarm_string_1 == zm_alarm_cancel_5) {
            client.println(zm_alarm_string_1);
            Serial.print("5 -> \t ");
            Serial.println(zm_alarm_string_1);
            delay(wait_delay);
          }
          client.println(zm_alarm_string_0);
          Serial.print("5 -> \t ");
          Serial.println(zm_alarm_string_0);
          ledblink(1, zm_alarm_type);
        }
        if (zm_cmd_status_6 == true) {
          if (zm_alarm_type == 1) {
            zm_alarm_string_0 = zm_alarm_ringing_6;
            zm_alarm_string_1 = zm_alarm_cancel_6;
          }
          else {
            zm_alarm_string_0 = zm_alarm_stopped_6;
          }
          if (zm_alarm_string_1 == zm_alarm_cancel_6) {
            client.println(zm_alarm_string_1);
            Serial.print("6 -> \t ");
            Serial.println(zm_alarm_string_1);
            delay(wait_delay);
          }
          client.println(zm_alarm_string_0);
          Serial.print("6 -> \t ");
          Serial.println(zm_alarm_string_0);
          ledblink(1, zm_alarm_type);
        }
        if (zm_cmd_status_7 == true) {
          if (zm_alarm_type == 1) {
            zm_alarm_string_0 = zm_alarm_ringing_7;
            zm_alarm_string_1 = zm_alarm_cancel_7;
          }
          else {
            zm_alarm_string_0 = zm_alarm_stopped_7;
          }
          if (zm_alarm_string_1 == zm_alarm_cancel_7) {
            client.println(zm_alarm_string_1);
            Serial.print("7 -> \t ");
            Serial.println(zm_alarm_string_1);
            delay(wait_delay);
          }
          client.println(zm_alarm_string_0);
          Serial.print("7 -> \t ");
          Serial.println(zm_alarm_string_0);
          ledblink(1, zm_alarm_type);
        }
        delay(fast_delay);
        zm_alarm_string_0 = "";
        zm_alarm_string_1 = "";
      }
      else {
        Serial.println("Nessun comando inviato [A] - L'invio dei comandi è stato disabilitato!");
        if (debug_mode >= 1) {
          ledblink(1, zm_alarm_type);
        }
        delay(slow_delay);
      }
      client.stop();
    }
    else {
      cmd_flood_inc = 0;
      cmd_status_sent = false;
      cmd_status_flood = false;
      cmd_status_flood_debug = true;
      cmd_status_delay = false;
      Serial.println("Invio fallito [A] - Connessione al server non riuscita!");
      Serial.print("Ripristino valore invii a: ");
      Serial.print(cmd_flood_inc);
      Serial.println();
      delay(fast_delay);
      disableantiflood();
      delay(fast_delay);
      disablecmddelay();
      delay(fast_delay);
      client.stop();
      ledblink(2, 3);
    }
  }
  else {
    cmd_flood_inc = 0;
    cmd_status_sent = false;
    cmd_status_flood = false;
    cmd_status_flood_debug = true;
    cmd_status_delay = false;
    Serial.println("Invio fallito [B] - Connessione al server non riuscita!");
    Serial.print("Ripristino valore invii a: ");
    Serial.print(cmd_flood_inc);
    Serial.println();
    delay(fast_delay);
    disableantiflood();
    delay(fast_delay);
    disablecmddelay();
    delay(fast_delay);
    client.stop();
    ledblink(2, 3);
  }
}
// Esecuzione Comando cmd_zm
void cmd_zm(int cmd_zm_type) {
  if (zm_cmd_status_0 == true) {
    if (cmd_zm_type == 1) {
      Serial.println("Segnale rilevato [C] - Scrittura valore su porta seriale in corso...");
      Serial.println();
      delay(slow_delay);
      Serial.print(cmd_zm_string_0);
      delay(slow_delay);
      Serial.println();
      ledblink(2, cmd_zm_type);
      delay(cmd_flood_delay_3);
    }
    if (cmd_zm_type == 2) {
      Serial.println("Segnale rilevato [D] - Scrittura valore su porta seriale in corso...");
      Serial.println();
      delay(slow_delay);
      Serial.print(cmd_zm_string_1);
      delay(slow_delay);
      Serial.println();
      ledblink(2, cmd_zm_type);
      delay(cmd_flood_delay_3);
    }
  }
  else {
    Serial.println("Nessun comando inviato [B] - L'invio dei comandi è stato disabilitato!");
    if (debug_mode >= 1) {
      ledblink(2, cmd_zm_type);
    }
    delay(slow_delay);
  }
}

// Esecuzione Lampeggio LED
void ledblink(int pin_1_2_3_cmdled, int pin_1_2_3_value) {
  if (pin_1_2_3_cmdled == 0) {
    if (debug_mode >= 2) {
      Serial.println("Esecuzione lampeggio singolo lento LED");        // Debug
    }
    ledcmd(pin_1_2_3_value);
    delay(slow_delay);
    ledcmd(0);
    delay(fast_delay);
  }
  if (pin_1_2_3_cmdled == 1) {
    if (debug_mode >= 2) {
      Serial.println("Esecuzione lampeggio singolo rapido LED");       // Debug
    }
    ledcmd(pin_1_2_3_value);
    delay(cmd_delay_1);
    ledcmd(0);
    delay(cmd_delay_1);
  }
  if (pin_1_2_3_cmdled == 2) {
    if (debug_mode >= 2) {
      Serial.println("Esecuzione lampeggio multiplo rapido LED");      // Debug
    }
    ledcmd(pin_1_2_3_value);
    delay(fast_delay);
    ledcmd(0);
    delay(fast_delay);
    ledcmd(pin_1_2_3_value);
    delay(fast_delay);
    ledcmd(0);
    delay(fast_delay);
    ledcmd(pin_1_2_3_value);
    delay(fast_delay);
    ledcmd(0);
    delay(slow_delay);
  }
  if (pin_1_2_3_cmdled == 3) {
    if (debug_mode >= 2) {
      Serial.println("Esecuzione accensione continuata LED");           // Debug
    }
    ledcmd(pin_1_2_3_value);
  }
}

void ledcmd(int pin_1_2_3_value) {
  if (cmd_status_led == true) {
    if (pin_1_2_3_value == 0) {
      digitalWrite(pin_1, LOW);
      digitalWrite(pin_2, LOW);
      digitalWrite(pin_3, LOW);
      if (debug_mode >= 2) {
        Serial.println("Spegnimento di tutti i LED");                      // Debug
      }
    }
    else {
      if (pin_1_2_3_value == 1) {
        digitalWrite(pin_1, HIGH);
        if (debug_mode >= 2) {
          Serial.print("Accensione del LED 1 sul PIN: ");                // Debug
          Serial.println(pin_1);                                         // Debug
        }
      }
      if (pin_1_2_3_value == 2) {
        digitalWrite(pin_2, HIGH);
        if (debug_mode >= 2) {
          Serial.print("Accensione del LED 2 sul PIN: ");                // Debug
          Serial.println(pin_2);                                         // Debug
        }
      }
      if (pin_1_2_3_value == 3) {
        digitalWrite(pin_3, HIGH);
        if (debug_mode >= 2) {
          Serial.print("Accensione del LED 3 sul PIN: ");                // Debug
          Serial.println(pin_3);                                         // Debug
        }
      }
    }
  }
}

// Esecuzione Comando Timer Delay
void enablecmddelay() {
  cmd_status_delay = true;
  if (debug_mode == 0) {
    cmd_delay_timer = cmddelay.after(cmd_delay_0, disablecmddelay);      // Debug
  }
  else {
    if (debug_mode >= 1) {
      cmd_delay_timer = cmddelay.after(cmd_delay_0_debug, disablecmddelay);
    }
    if (debug_mode >= 2) {
      Serial.println("Timer ritardo comando avviato!");                  // Debug
      Serial.println();
    }
  }
  delay(fast_delay);
}

void disablecmddelay() {
  cmd_status_delay = false;
  cmddelay.stop(cmd_delay_timer);
  if (debug_mode >= 2) {
    Serial.println("Timer ritardo comando fermato!");                    // Debug
    Serial.println();
  }
  delay(fast_delay);
}

// Esecuzione Comando Timer Anti-Flood
void enableantiflood() {
  cmd_flood_inc = 0;
  cmd_status_delay = false;
  cmd_status_flood = true;
  cmd_status_flood_debug = false;
  cmd_flood_timer = antiflood.after(cmd_flood_delay_0, disableantiflood);
  ledblink(3, 3);
  if (debug_mode >= 2) {
    Serial.println("Timer Anti-Flood avviato!");                          // Debug
  }
  Serial.print("Anti-Flood attivato [A] - Limite di invii superato. Invio dei comandi bloccato!");
  if (cmd_flood_time_1 > 0) {
    Serial.print(" [Per ");
    Serial.print(cmd_flood_time_1);
    if (cmd_flood_time_1 == 1) {
      Serial.println(" minuto di attesa]");
    }
    else {
      Serial.println(" minuti di attesa]");
    }
  }
  Serial.println();
  delay(fast_delay);
}

void disableantiflood() {
  cmd_status_delay = false;
  cmd_status_flood = false;
  antiflood.stop(cmd_flood_timer);
  ledblink(3, 0);
  if (debug_mode >= 2) {
    Serial.println("Timer Anti-Flood fermato!");                          // Debug
  }
  if (cmd_status_flood_debug == false) {
    Serial.println("Anti-Flood disattivato [B] - Invio dei comando sbloccato!");
    Serial.println();
  }
  cmd_status_flood_debug = false;
  delay(fast_delay);
}

STARTUP SCRIPT:

Copy it into /etc/init.d/ and call it "arduino". Execute commands to enable it at startup:

# chmod 755 /etc/init.d/arduino
# chmod a+x /etc/init.d/arduino
# sudo update-rc.d arduino defaults
# sudo update-rc.d arduino enable

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides:          Arduino
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Arduino listener daemon
# Description:       Start Arduino listener daemon at boot
### END INIT INFO

process="arduino.pl"
cmd="/etc/zm/arduino.pl"
user="root"

case "$1" in
	'start')
		echo "Starting Arduino listener daemon now"
		sudo -u "$user" $cmd
	;;
	'stop')
		echo "Stopping Arduino listener daemon now"
		killall -9 $process
	;;
	'info')
		echo "Use start or stop parameters to start or stop Arduino listener daemon manually"
	;;
	*)
		echo "Usage: $0 {start|stop|info}"
		exit 1
	;;
esac

exit 0

EXECUTE SCRIPT:

Copy scripts into /sbin/and Execute commands:

# sudo apt-get update && sudo apt-get intall libdevice-serialport-perl screen
# chmod 755 /sbin/cmd_*

cmd_zm: -> Change your ZoneMinder USER, PASSWORD and HOST:PORT

Code: Select all

#!/bin/sh
username="USER"
password="PASSWORD"
host="http://localhost:80/zm"
file="/etc/zm/cookie.txt"

case "$1" in
	'statusid')
		if [ -e $file ]; then
  			echo "WARNING: Cookie file: $file already exist... Using it!"
		else
   			echo "WARNING: Cookie file: $file does not exist... Creating it!"
			curl -d "username=$username&password=$password&action=login&view=console" -c $file $host/index.php
			sleep 1
			chmod 600 $file
			echo Cookie file: $file created.
		fi
		curl -b $file -XPOST $host/api/monitors/$2.json -d "Monitor[Function]=$3&Monitor[Enabled]=$4"
		echo Changed ID $2 ZoneMinder monitor status to $3 now.

	;;
	'makecookie')
		if [ -e $file ]; then
  			echo "WARNING: Cookie file: $file already exist... Overwriting it!"
		else
   			echo "WARNING: Cookie file: $file does not exist... Creating it!"
		fi
		curl -d "username=$username&password=$password&action=login&view=console" -c $file $host/index.php
		sleep 1
		chmod 600 $file
		echo Cookie file: $file created.
	;;
	'deletecookie')
		if [ -e $file ]; then
  			echo "WARNING: Cookie file: $file exist... Deleting it!"
			rm $file
			echo Cookie file: $file deleted.
		else
   			echo "WARNING: Cookie file: $file does not exist... Can't delete it!"
		fi
	;;
	'info')
		echo "Use statusid <ID> <None|Monitor|Modect|Record|Mocord|Nodect> <0|1> parameters to change monitor status or makecookie\deletecookie to manage cookie file manually"
	;;
	*)
		echo "Usage: $0 {statusid|makecookie|deletecookie|info}"
		exit 1
	;;
esac

exit 0
cmd_arduino: -> Change your device path

Code: Select all

#!/bin/sh
device="/dev/ttyACM0"
baudrate="115200"
cmd1="cmd_zm statusid"
cmd2="Modect 1"
cmd3="Nodect 1"
cmd4="None 1"

case "$1" in
	'read')
		screen $device $baudrate
	;;
	'alarm')
		cmd_zm makecookie
		sleep 0.5
		if [ "$2" = "on" ]; then
			$cmd1 1 $cmd2
			sleep 0.5
			$cmd1 2 $cmd2
			sleep 0.5
			$cmd1 3 $cmd2
			sleep 0.5
			$cmd1 4 $cmd2
			sleep 0.5
			$cmd1 5 $cmd2
			sleep 0.5
			$cmd1 6 $cmd2
			sleep 0.5
			$cmd1 7 $cmd2
			echo All enabled ZoneMinder IP Cameras have been successfully setted on "$cmd2".
		fi
		if [ "$2" = "off" ]; then
			$cmd1 1 $cmd3
			sleep 0.5
			$cmd1 2 $cmd3
			sleep 0.5
			$cmd1 3 $cmd3
			sleep 0.5
			$cmd1 4 $cmd3
			sleep 0.5
			$cmd1 5 $cmd3
			sleep 0.5
			$cmd1 6 $cmd3
			sleep 0.5
			$cmd1 7 $cmd3
			echo All enabled ZoneMinder IP Cameras have been successfully setted on "$cmd3".
		fi
	;;
	'info')
		echo "Use read to start serial read session or use alarm <on|off> parameters to set alarm status on or off manually"
	;;
	*)
		echo "Usage: $0 {read|alarm|info}"
		exit 1
	;;
esac

exit 0

Copy script into /etc/zm/ and Execute commands:

# chmod 755 /etc/zm/arduino.pl

arduino.pl:-> Change your device path

Code: Select all

#!/usr/bin/perl

use Device::SerialPort;

$device = "/dev/ttyACM0";
my $port = Device::SerialPort->new($device);

$port->baudrate(115200);
$port->databits(8);
$port->parity("none");
$port->stopbits(1);

$cycles_number = 1;
$read_chars = 7;

$value_activated = "+ALARM+";
$value_deactivated = "-ALARM-";

$cmd_activated = "cmd_arduino alarm on";
$cmd_deactivated = "cmd_arduino alarm off";

while($cycles_number) {
	my $read_string = $port->read($read_chars);
	print "$read_string";
	system("$cmd_activated") if($read_string eq $value_activated);
	system("$cmd_deactivated") if($read_string eq $value_deactivated);
}

So, I hope that it can help someone!
And sorry for my bad english. :D

PS: I Attached my DEV BOARD and Serial Output (DEBUG) Test. :!:
Attachments
DEVBOARD.jpg
DEV BOARD
DEVBOARD.jpg (952.41 KiB) Viewed 1677 times
DEBUGTEST.png
DEBUG TEST
DEBUGTEST.png (767.84 KiB) Viewed 1677 times
Last edited by sagitt on Thu Feb 08, 2018 8:39 am, edited 21 times in total.

sagitt
Posts: 32
Joined: Mon Apr 30, 2012 1:13 pm

Re: Control zmtrigger with your own alarm and arduino

Post by sagitt » Sun Feb 04, 2018 1:24 pm

Hi.
I updated it with better LED control, Better send times, showtext for timestamp ecc.. (v2.0)

This is My Dev board with leds :)
Attachments
1.jpg
1.jpg (1003.6 KiB) Viewed 1593 times
2.jpg
2.jpg (871.06 KiB) Viewed 1593 times
3.jpg
3.jpg (899.17 KiB) Viewed 1593 times

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests