Garage Door Opener Calling by Phone Via Arduino and ASTERISK Behind a FritzBox

by lebon1089 in Circuits > Arduino

523 Views, 2 Favorites, 0 Comments

Garage Door Opener Calling by Phone Via Arduino and ASTERISK Behind a FritzBox

ard2.jpg
gara3.jpg
gara_dia.jpg

Recognizing whether the gate is open or closed. The limit switch on the garage door tells the ARDUINO whether the garage is open or closed.

  • When the gate opens, the Arduino calls the gara_auf.php file
  • When the gate closes, the Arduino calls the gara_zu.php file


The respective PHP file copies the correct sound file, which ASTERISK then plays.


gara_auf.php:

<?php
system("wget --no-cache --spider http://10.45.0.23/LED=GAON");
copy('/usr/share/asterisk/sounds/garage_Auf_40cent.gsm', '/usr/share/asterisk/sounds/garage_aktuell.gsm');
?>


  • The wget call controls an LED in the SmartHome to see, the garage is open (LED=GAON) *
  • The copy command copies the sound file "garage_auf_40cent.gsm" (garage is OPEN announcement) as "garage_aktuell.gsm" because ASTERISK ALWAYS plays the file "garage_aktuell.gsm" after picking it up

gara_zu.php:

<?php
system("wget --no-cache --spider http://10.45.0.23/LED=GAOFF");
copy('/usr/share/asterisk/sounds/garage_Zu_40cent.gsm', '/usr/share/asterisk/sounds/garage_aktuell.gsm');
?>


  • The wget call controls an LED in the SmartHome to see, the garage is cloes (LED=GAOFF) *
  • The copy command copies the sound file "garage_zu_40cent.gsm" (garage is CLOSED announcement) as "garage_aktuell.gsm" because ASTERISK ALWAYS plays the file "garage_aktuell.gsm" after picking it up


To operate the garage, see Step 4 - this is done by ASTERISK's Extensions.conf.


*10.45.0.23 is another self-made SmartHome device. See here: https://www.instructables.com/Smart-Home-Event-Viewer/

Supplies

  • Some Linux machine
  • running ASTERISK
  • running Apache
  • running PHP
  • A VOIP-compatible router, with me a FritzBox
  • Arduino Nano with an ENC28j60 Module or a Wemos D1
  • Powersupply for the microcontroller
  • Housing for the Arduino hardware
  • 1 5 Volt Relay
  • 1 resistor 1 kOhm
  • a limit switch on the garage door
  • some LAN wiring

Configure FritzBox for the ASTERISK Registration

FB1.jpg
fb2.jpg

ASTERISK must register with the FritzBox. For this I define a telephony device and a phone number with a user and a password.

FritzBox start page --> Telephony devices --> Set up a new device


Telephone (with and without answering machine) --> LAN/WLAN (IP telephone) --> Name for the connection (here ast-pi-user) --> Username and Password --> Assign phone number --> Choose whether the phone should answer all calls or only respond to calls to specific numbers. --> accept all calls --> Apply


Thus, ASTERISK can log on to the FritzBox

SIP.CONF | ASTERISK Registration at the FritzBox

ast1.jpg

We edit the sip.conf (/etc/asterisk/sip.conf) as follows

[general]
port=5060
bindaddr=0.0.0.0
dtmfmode=inband          // IMPORTANT that ASTERISK recognizes and processes the key tones

register=ast-pi-user:XXXX@10.45.0.1/622       // Username and Password as defined in the telephony device in the FritzBox and the IP of the router

[ast-pi-user]
username=ast-pi-user     // Username as defined in the telephony device in the router
secret=XXXX              // Password as defined in the telephony device in the router
type=friend
insecure=port,invite
host=10.45.0.1           // IP-Address of the Router
fromdomain=10.45.0.1     // IP-Address of the Router
fromuser=ast-pi-user     // Username as defined in the telephony device in the router
context=anruf            // The context to be called, more on that later
dtmfmode=inband          // IMPORTANT that ASTERISK recognizes and processes the key tones
allow=ulaw
allow=alaw
allow=gsm


EXTENSIONS.CONF | What Should ASTERISK Do When Calling

ast2.jpg
convertio.jpg

In EXTENSIONS.CONF we define the context that ASTERISK should call after accepting the call.

The sound files must be created as "GSM" files. This can be done at https://convertio.co/de/mp3-gsm/ or any other converter

[anruf]
   // Name of the Context as defined in the sip.conf

exten=>622,1,Answer()  
   // Pick up the phone
exten=>622,n,Background(garage_aktuell)
   // Sound file 1 says "Hello" and whether the garage is currently open or closed

exten=>1234,1,System(wget --no-cache -S http://10.45.0.27/BUTTON)
  // When "1234" was entered on the phone keypad, invoke "wget", more on that later
exten=>1234,2,Playback(coderichtig-ade-brbl)
  // ASTERISK says goodbye
exten=>1234,3,Hangup()
   // ASTERSIK hangs up

exten=>i,1,Hangup()
exten=>i,2,Playback(code-falsch-ade-brbl)
  // If "1234" was NOT entered, Sound File 2 (code-wrong-ade-brbl) says the password was wrong and hangs up

The ARDUINO | Hardware & Software

ard1.jpg
ard2.jpg

The Arduino Sketch

The Limit-Switch is connected to the digital pins 2 and 3

The Relay is connected to the digital pin 5

The remote IP of the Apache Webserver to process the PHP files is here the 10.45.0.2

#include <UIPEthernet.h>

byte mac[] = {0xDE, 0xAE, 0xBD, 0xEF, 0xFE , 0xED};
IPAddress ip(10, 45, 0, 27);
IPAddress subnet(255,255,0,0);
IPAddress gateway(10,45,0,1);
EthernetServer server(80);

String readString;
String reader = "";
int rel = 5;   // relay pin
int auf = 2;   // Limit-Switch-Pin
int zu = 3;   // Limit-Switch-Pin
int a = 0;
int interval = 10;
int crontimer;
char serverrem[] = "10.45.0.2";   //The IP-Address of the Apache-Machine
volatile int aufzu = 2;
volatile unsigned long alteZeit=0, entprellZeit=1000;
volatile unsigned long aZ=0, eZ=2000;

void setup() {
 Ethernet.begin(mac, ip);
 server.begin();
 Serial.begin(9600);
 pinMode(rel, OUTPUT);
 digitalWrite(rel, HIGH);
 pinMode(auf, INPUT);
 pinMode(zu, INPUT);
 Serial.print("Garagencontroller gestartet");
 Serial.println(aufzu);
}

void loop() {
EthernetClient client = server.available();

if (a == 0 and digitalRead(zu) == LOW) {
if (client.connect("10.45.0.2", 80)) {
 client.println("GET /gara/gara_auf.php"); // When the limit switch switches to "closed", call "gara_auf.php".
 a = 1;
 client.println("Host: 10.45.0.2");
 client.println("Connection: close");
 Serial.println("gara_auf.php aufgerufen");
 client.stop();
 client.println();
}
}

if (a == 1 and digitalRead(auf) == HIGH) {
if (client.connect("10.45.0.2", 80)) {
 client.println("GET /gara/gara_zu.php"); // When the limit switch switches to "open", call "gara_zu.php".
 a = 0;
 client.println("Host: 10.45.0.2");
 client.println("Connection: close");
 Serial.println("gara_zu.php aufgerufen");
 client.println();
 client.stop();
}
}

if (!client) {
 return;
}

while(!client.available()){
 delay(1);
}

String request = client.readStringUntil('\r');
if (client) {
 Serial.println("new client");
 boolean currentLineIsBlank = true;
 while (client.connected()) {
 char c = client.read();
 if (client.available()) {
   if (c == '\n' && currentLineIsBlank) {
     client.println("HTTP/1.1 200 OK");
     client.println("Content-Type: text/html");
     client.println("Connection: close");
     client.println();
     client.println("<!DOCTYPE HTML>");
     client.println("<html>");

     if (request.indexOf("/BUTTON") != -1) { // If the call "http://10.45.0.27/BUTTON" comes, ...
       Serial.println("Vorm Schalten");
       digitalWrite(rel, LOW);              // Turn relay ON
       delay(500);                          // Wait 500 milliseconds
       digitalWrite(rel, HIGH);             // Turn relay OFF
       Serial.println("Nachm Schalten");
       client.println("Garage geschaltet");
     }
     client.println("</html>");
     break;
   }
   if (c == '\n') {
     currentLineIsBlank = true;
   } else if (c != '\r') {
     currentLineIsBlank = false;
     }
   }
 }

delay(1);
client.stop();
Serial.println("client disconnected");
}
}


My php-files (gara_auf.php / gara_zu.php) are located in the subdirectory /gara/ below the html-root of the Apache weserver.


The Arduino code is very easy to adapt if an ESP (e.g. Wemos D1) is to be used for WLAN.