Share My Creation Garage Door Opener (with source code)

Hi all:

*** ADDED Project with B4XPages working on B4A and B4J. See post #15

Not sure if this is the rigth place, or if I should publish it in Examples, @Erel, feel free to move.
The code is not perfect, but it works.
Most of code is copied/pasted/modified from forums, so this app is made by the forum people.

Attached a B4A and a B4R app. You will need an ESP8266 and a remote.
The ESP8266 sends a FCM message to the B4A app when someone rings the bell.
From the B4A app, you can send a mqtt message to the ESP to close the contacts of a remote to open a garage door.
You can send the mqtt message from the app or from a widget.

The schema is something like this. I open two garage doors and a pedestrian door, so it can be simplified with just a remote.
*** WARNING ***
I get the ring bell signal from a 220v doorbell, so I use a 220v relay. You don't have to do this if you don't know what you're doing, it's dangerous.
You can avoid the relay and get the doorbell signal input from an open/close contact, or even omit this part if you just want to open the door instead.
*******************

Schema:
schema.jpg


So you need to open your remote, and try to close the button contacts. If your door opens, it will work
1579182805422.png


Feel free to improve it and share!!

Some things you could add:
- Loggin and log in a database when someone open the door
- Check the GPS position before opening with the widget, to avoid open the door when you are far away by mistake
- You can see the images from your webcam with a webview
- Send a picture taken when someone press the doorbell with the FCM notification.
It would be great using this project from @miker2069 instead of ESP8266 to get the picture an open the door.

Installed:

1579189365880.png
installed.jpg


 

Attachments

  • B4ARemoteGarageDoor.zip
    95.3 KB · Views: 1,000
  • B4RRemoteGarageDoor.zip
    5.9 KB · Views: 783
Last edited:

josejad

Expert
Licensed User
Longtime User
Thanks a lot Peter.
I really have no idea about Arduino or electronics, so when I made the question in the forum about the hardware I would need, I was surprised this can be done with just a small and cheap ESP8266.
So it's funny I haven't seen more projects like this here, and I post my project just to show newbies like me how easy could it be.

 

miker2069

Active Member
Licensed User
Longtime User
this is great :) - I did something very similar a couple years ago (not in B4R but just plain arduino ide and a blynk app). I'm actually in the process of converting it to B4R and B4A/B4i). As a suggestion, you could also use a PNP transistor as a switch to close the button on the remote (you can then eliminate the clicking sound of the relay all together). Also, you can get eliminate the coin battery and power the remote directly from the esp8266 3.3v pin and ground,

I love the "swipe out" feature of your app - that's so cool, great work!
 

miker2069

Active Member
Licensed User
Longtime User
Thanks @miker2069 !!



It's a great idea!!


I have no idea about this, I have something more to study 🙃

I hope to play soon with your project with the ESP32.
Yes - specially a PNP transistor (as Peter pointed out there are other types NPNs, MOSFET). Without getting into the nitty gritty details (youtube has some great videos on the topic), when "activated", PNP transistors effectively act the same way as a button (it shorts two wires) by driving the GPIO pin from the ESP low. NPNs can't really simulate a "short" in the way you would need it to mimic the short created by a button push - I learned this the hard way :)
 

miker2069

Active Member
Licensed User
Longtime User
What is the "swipe" feature you used at the end of the video called specifically? Honestly that was equally as cool. I'd like to go read more about it.
 

josejad

Expert
Licensed User
Longtime User
What is the "swipe" feature you used at the end of the video called specifically? Honestly that was equally as cool. I'd like to go read more about it.
Hum... I think you're talking about the widget.
The swipe is not really done in my app. If you see, I press the home button, so I go to the launcher. I swipe two screens of my launcher and then I use the widget of my app.
 

miker2069

Active Member
Licensed User
Longtime User
So I could even avoid using the relay's module, if I understand rigth?
Yes, the PNP would replace the relay to the remote, you'd (obviously) keep the relay to the 220v door belay). Relays are still mechanical devices that will eventually where out (it would be a while but they still have moving parts that will wear out). Plus relays are much slower than transistors. You just need a PNP (like a bc547) and a 1K resistor . I'll try and draw something up to show what I used to wire up my remote.
 

miker2069

Active Member
Licensed User
Longtime User
Hum... I think you're talking about the widget.
The swipe is not really done in my app. If you see, I press the home button, so I go to the launcher. I swipe two screens of my launcher and then I use the widget of my app.
Ahh gotcha - thanks!
 

sfields30

New Member
I found this looking for how to use mqtt in B4A. Looking forward to playing with it.

I also have an ESP8266 wired to my garage doors, that I currently use with OpenHab. Going to try adapting this project to an Android-direct app to open my doors from my phone, bypassing OpenHab.

I wanted to point out that, rather than using a remote, you can wire your relay directly to the opener, using the same connectors that a wired wall button uses.
 

max123

Well-Known Member
Licensed User
Longtime User
Hi @José J. Aguilar , great project. I downloaded and tried it, for now I only tried B4R part and compiled, but I found something on your code that I do not know.

I admit that principally I've downloaded this project to know how send FCM notifications from ESP using B4R.

In your B4R code you connect to MQTT broker, you get a time with NTP server, but how you send an FCM notification ?

In the code the ConnectFCM() sub is never called, there are some Connect() but seem to be related to MQTT.

I even do not know because you use this in initialization on Sub AppStart:
B4X:
Dim Options As MqttConnectOptions
Options.Initialize("user", "password")  ' If your mqtt server don't use User and Password, use mqtt.Connect
MQTT.Connect2(options)
... and then just after that you call Connect(0) that call this function that connect MQTT again but without login
B4X:
Sub Connect(unused As Byte)
    If MQTT.Connect = False Then
        Log("Trying to connect again to MQTT broker")
        CallSubPlus("Connect", 1000, 0)
        Return
    End If
    Log(">>>>>>>> Connected to MQTT broker")
    MQTT.Subscribe("esp", 0)
End Sub
Please, I miss someting here ?

Thanks
 
Last edited:

josejad

Expert
Licensed User
Longtime User
Hi Max:

I'm glad you liked the project.

to know how send FCM notifications from ESP using B4R
This is the code I used to send the FCM Messages


In the code the ConnectFCM() sub is never called, there are some Connect() but seem to be related to MQTT.
You're right, reading this post it looks like I was using a deprecated way of sending the FCM messages, and I didn't clean it from the code.
 

max123

Well-Known Member
Licensed User
Longtime User
Thanks Josè, so the right way is to use HttpJob, so this is the right code part ?
B4X:
Private Sub SendMessage(Topic() As Byte, Title() As Byte, Body() As Byte)
    Dim bc As ByteConverter
    HttpJob.Initialize(bc.StringFromBytes(Topic))
 
    Dim buffer(300) As Byte 'must be large enough to hold the message payload
 
    Dim raf As RandomAccessFile
    raf.Initialize(buffer, True)
 
    WriteBytes(raf, "{""data"":{""title"":""")
    WriteBytes(raf, Title)
    WriteBytes(raf, """,""body"":""")
    WriteBytes(raf, Body)
    WriteBytes(raf, """}")
 
    ' End of data
    WriteBytes(raf, ",""to"":""\/topics\/")    ' Forse vanno rimossi i simboli escape \
    WriteBytes(raf, Topic)
    WriteBytes(raf, """")
    WriteBytes(raf, ",""priority"": 10")
 
    If bc.StartsWith(Topic, "ios_") Then
        WriteBytes(raf, ",""notification"": {""title"": """)
        WriteBytes(raf, Title)
        WriteBytes(raf, """,""body"":""")
        WriteBytes(raf, Body)
        WriteBytes(raf, """, ""sound"": ""default""}")
    End If
 
    WriteBytes(raf, "}")
 
    HttpJob.AddHeader("Authorization", JoinBytes(Array("key=".GetBytes, API_KEY)))
    HttpJob.AddHeader("Content-Type", "application/json")
 
    Log("stack: ", StackBufferUsage, ", buffer size:", raf.CurrentPosition)
 
    HttpJob.Post("https://fcm.googleapis.com/fcm/send", bc.SubString2(buffer, 0, raf.CurrentPosition))
Please remove ConnectFCM sub so other users do not confuse like me. :p

If you interested I wrote an FCM library for ESP (but in C++) capable to send vocal notifications to B4A, support any language (you'll need to have a language installed on Android). It is sophysticated, you can set more things, like language, voice pitch etc... it even speak numbers, integers or floating.

In yor project example using this, when a person drill a doorbell your phone speak 'Someone at the door...."

Thanks
 
Last edited:
Top