Page 1 of 1

Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 11:34
by appelflap
Mod edit: Post splitted into new topic. Original topoic: http://domoticz.com/forum/viewtopic.php?f=17&t=5509
---------------------------------------
Yes! I've found/made a solution :) and call it #open or in Dutch 'hekje open'.

Only two thing to fix:
- Telegram seems to spawn Zombie processes. I found this out because I added my home server to a Opsview/Nagios monitor. After a day of running (and receiving Telegram messages) it wil create up to 10 zombie/<defunct> 'sh' processes. Don't know which step creates them, but they are gone after restarting the Telegram daemon. I can't find any other telegram/domoticz user that has this problem.
- Finding a way to automatically send #open to myself when I start the car within the fence. Tried it with IFTTT, but the rule of 'when connecting to a bluetooth device' does not let you specify a device. It would work if I could make a rule that looks at my carkit + GPS location and then sends the #open.

I'm controlling the gate through telegram->domoticz->libusb/digispark->digiusb->remote.
I found out that the FAAC remote has a OOK rolling 868 code that is not easy to receive/transmit with standard cheap prints, because you need a fast clock (or a modified RFM12B print).
A spare remote costs a litle bit more, but is easier to implement. A small ATTiny85 print 'pushes' the putton for me and the remote print is powered by the usb in stead of the 2 CR2032u batteries.
I modded the original tg/scripts/generic/receive.lua slightly because I do not use a separate phone number for the Telegram server. So now I receive all #open calls my self so I know which friends open my gate. Then all texts sent to me would be checked for commands and people would receive 'INVALID COMMAND' notices. But I changed it so that commands have to start with a '#'.

Costed € 46,- in total and has:
- FAAC remote (€40,-) to copy the code to from the one I already had.
- Digispark ATTiny85 usb print (€ 6,0)
- CNY17-3 optocoupler for pushing the button (<€1)
- A 100-200Ohm resistor for controlling the current through the optocoupler

The original front of the remote still fits on the print, because everything is soldered on the back. Only the two yellow wires go to the front to short the push button.
Image

Image

tg/scripts/user/open.sh

Code: Select all

#!/bin/bash
# Sends text to lib-usb Digistump device

#Settings
SendMsgTo=$1

#Send sensor values with telegram
$TelegramScript msg $SendMsgTo "Please wait, pushing button..."

##############################################################################

sent=`sudo /home/albert/progs/digiusb/send 01 --debug | tail -n 1`
if [[ "$sent" == "Error -22 releasing Interface 0" ]] ; then
    ResultString="Gate should be opening!\n"
elif [[ "$sent" == "Error -1 releasing Interface 0" ]] ; then
    ResultString="Sorry, something went wrong. Better luck next time!\n"
elif [[ "$sent" =~ "No Digispark" ]] ; then
    ResultString="Remote not connected!\n"
else
    ResultString="WHAAAAAAAA!\n"
fi

##############################################################################
$TelegramScript msg $SendMsgTo $ResultString
hekjeopen.ino Arduino file

Code: Select all

// #open by <[email protected]>
// Switch outputs through usb and turn them off after a delay.
// This way the remote does not stay on if something goes wrong with tg/domoticz

#include <DigiUSB.h>

int delayms = 1500;
int inputNr = 0;

void setup() {
  DigiUSB.begin();
  pinMode(0,OUTPUT);
  pinMode(1,OUTPUT);
  pinMode(2,OUTPUT);
}

void get_input() {
  inputNr = 0;
  int value;
  while (true) { // loop forever
    if (DigiUSB.available()) {
      // something to read
      value = DigiUSB.read();
      
      if (value == '\n' ) {
      // Breaking on newline
        break;
      }
    
      if(value == '0') {
      // Turning %d OFF
        digitalWrite(inputNr,LOW);
      }
      else if(value == '1') {
      // Turning %d ON
        digitalWrite(inputNr,HIGH);
      }
      inputNr++;

      if(inputNr > 2) {
      // Breaking on input count
        break;
      }
    }
    
    // refresh the usb port for 10 milliseconds
    DigiUSB.delay(1);
  }
}

void loop() {
  int i = 0;
//  DigiUSB.println("Turn input 0-2 ON/OFF");
//  DigiUSB.println("For example: 100 (0 ON, 1 OFF, 2 OFF)");
//  DigiUSB.println("Waiting for input... ");
  get_input();
  // Turn all inputs to low
  DigiUSB.delay(delayms);
  digitalWrite(0,LOW);
  digitalWrite(1,LOW);
  digitalWrite(2,LOW);
  DigiUSB.println("OK");
}
changed HandleCommand function in tg/scripts/generic/receive.lua

Code: Select all

function HandleCommand(cmd, SendTo)
        print("Handle command function started with " .. cmd .. " and " .. SendTo)
        local found=0
        local f = io.popen("ls " .. ScriptPath)
        SpacePos=string.find(cmd, "% ")
        HashPos=string.find(cmd, "#")
        if(HashPos ~= 1) then
           return found
        end

        if(SpacePos==nil) then
          cmda = string.sub(cmd,2,string.len(cmd))
          stuff = ''
        else
          cmda = string.sub(cmd,2,SpacePos-1)
          stuff = string.sub(cmd,SpacePos+1,string.len(cmd))
        end
        if(Commands[cmda] ~= nil)then
            loadstring('return '..cmda..'(...)')(SendTo,cmda,stuff)
            found = 1
        else
            cmda = string.lower(cmda)
            for line in f:lines() do
                print("checking line ".. line)
                if(line:match(cmda)) then
                    print(line)
                    os.execute(ScriptPath  .. line .. ' ' .. SendTo .. ' ' .. stuff)
                    found=1
                end
            end
        end
        return found
end

Re: Connect 868.33Mhz Garage Door to Domoticz

Posted: Monday 13 July 2015 12:01
by ThinkPad
Very cool, thanks for sharing!

For the zombie processes; maybe you can use a cron job to restart the Telegram daemon every hour or so?
That ATTiny85 USB thingy is also cool, i saw it a while ago on Banggood and directly added it to my wishlist hehe. But don't have any projects now where i can use it in.

For opening the gate when starting your car you could use someting like an ESP8266 which you ping constantly. Domoticz has built in functionality for this. If it responds to ping = On.
Let the ESP join your network once and it will remember it. Then give it power when the car is running.

IF esp = On (responds to ping) AND gate = closed
DO SET gate = open

P.S. I splitted your post to a new topic (from: http://domoticz.com/forum/viewtopic.php?t=5509). This deserves an own topic :mrgreen:

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 12:32
by appelflap
Creating a cron to restart the process is not a solution in my opinion :). I will try figure out where the zombies come from and post it here.
Restarting Telegram causes the daemon to re-read the last n received messages. So if one of them was #open it will open the gate because of the restart. And starting Telegram and connecting to all servers takes a couple of minutes. You would get a delay in opening of the gate if it happens while sending the command.

ESP8266 is a cool idea. Ping has a delay though, because the unit would also have to register on the network fast enough before I drive out. And I would have to check if the WiFi reaches my apartment on the 2nd floor.
Maybe I could just modify a 433MHz button to be pressed when starting the car. My WiFi access point is far from my window, but the Domoticz server+antennae is near the window. I also see the neighbors' weather station on the ground floor, so could be possible :)

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 13:15
by ThinkPad
Don't know what kind of phone do you have, but you could also look at NFC. Stick one in your car, smack your phone on it and gate opens.

I once ordered a cheap set of NFC-stickers from eBay.
Downside is that the phone has to be unlocked i thought (last time i tried) because of security (otherwise some malicious URL could be opened automatically). I used a sticker on my nightstand, to call a Domoticz 'going to bed' scene. Worked fine, it opened a URL in the browser of my phone.

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 13:49
by appelflap
Thx ThinkPad, that's a really cool idea! My phone does not lock in the car because it is connected to a 'safe bluetooth device' with smart lock setting on Android.
I always put my phone in the cradle in my car. So I could place the sticker on/in the cradle. How would you you trigger a Telegram message? I couldn't find (android)NFC in the IFTTT recipe maker.

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 14:17
by ThinkPad
Use something like 'Tasker'. Unlimited possibilities then :mrgreen:
I use it to turn the screen of my tablet mounted on the wall on/off according to a virtual switch in Domoticz. See topic about that: viewtopic.php?f=28&t=4733

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 14:27
by simonrg
appelflap wrote:Creating a cron to restart the process is not a solution in my opinion :). I will try figure out where the zombies come from and post it here.
Restarting Telegram causes the daemon to re-read the last n received messages. So if one of them was #open it will open the gate because of the restart. And starting Telegram and connecting to all servers takes a couple of minutes. You would get a delay in opening of the gate if it happens while sending the command
If you want to continue to use Telegram, then the official Telegram bots are probably the way to go, as the bots just use html calls, so need for a specific telegram client running on your Domoticz system, you can send notifications directly. Then you just need a handler to issue html calls to receive messages and connect to Domoticz. Once that's running then you can use anything on your phone that either sends Telegram messages or can issue html calls to interact with your handler.

I have created a program to connect the bot to Domoticz, based on the Telegram and XMPP bots, I just need to find some time to write up some directions. In the meantime this explains how to set up the notification side - http://www.domoticz.com/wiki/Telegram_Bot.

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 14:51
by appelflap
@simonrg: I will take a look at the bot. Thank you for the idea and url. The scripts can be modified easily for use with url in stead of Telegram local daemon.

Just installed Tasker, but was not able to find the NFC input within the refund window. So uninstalled it for now. @ThinkPad: do you have a hint on how to select NFC for a rule/task?

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 15:00
by ThinkPad
Hmmm i had a quick look for you, but also can't find it. Maybe a Tasker plugin is needed.

Edit: Yup, you need a plugin: http://android.stackexchange.com/questi ... -in-tasker
They also mention 'Trigger', you could also try that one.

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Monday 13 July 2015 15:25
by appelflap
Found an other program that's free and does the same: Trigger
Tried id with my public transport card and it worked with this program after installing the extra Trigger: Tag Reuse Plugin.
I let it call an url on which I will let domoticz listen.. or implement the Telegram Bot.

[edit] lol, did not see your hint for Trigger ThinkPad :D [/edit]

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Tuesday 14 July 2015 13:39
by appelflap
I placed a NFC tag (public transport, NS, one time only card) in the cradle of my phone in my car. But to do that correctly you need a Geofence filter before sending the #open command.
Trigger can do that with a PRO version. But it asks for too much privacy info. So I discontinued the Trigger option.

Screenshots Dutch only..
ImageImageImageImage

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Wednesday 25 May 2016 20:03
by DavidDeSloovere
Great solution. Still waiting to have my gate and FAAC system installed, but will certainly check back on this topic when that happens.

Re: Connected FAAC gate to Domoticz (control with Telegram)

Posted: Friday 16 November 2018 12:19
by Jakubb
Thx for this solution