Share My Creation JG IoT Alarm (or the power of Firebase...)

Discussion in 'Share Your Creations' started by freedom2000, Aug 21, 2016.

  1. freedom2000

    freedom2000 Well-Known Member Licensed User

    Here is my latest creation, quite simple but efficient !


    It's basically a kind of generic alarm system :
    • sensors send alarms
    • smartphones receive alarms
    • smartphones clear alarms
    You can have as many sensors you want
    You can have as many smartphones you want
    The system can work in fully automatic mode, alarms being sent only when you are not at home (option)

    All this stuff is synchronized with firebase Messaging (here is the Magic !)

    How does it work ?
    - sensor sends a Message, Firebase routes it to all the associated phones
    - each phone can clear the alarm ... and sends a message to firebase, which sends it to all phones
    - each phone is thus a de-centralized alarm central

    The real time capabilities of firebase are amazing, any alarm is cleared in less than a few seconds. I have tested this in France between phones distant of more than 600 km :)

    Finally it's a kind of real time MQTT broker, where no connection of clients are needed.
    Interesting also is that firebase messages wake up the App (static intent ?) so no battery drain while sleeping, no foreground notification, it just works even when the phone is locked.

    Registration of sensors is done while the sensor is not connected to your wifi router. It then exposes its own SSID where you can connect... configure it and that's it. Afterwards the sensor automatically connects to your router and of course to internet.
    You can share your configuration with other smartphones. This is done with firebase storage using secured file sharing.

    Sensors are all based around ESP8266. They are cheap and reliable (provided you reboot them from time to time.)
    • The PIR sensor on the picture can trigger a long pulse able to wake up the ESP8266 (CH-PD pin).
    • ESP8266 then boots and does its job to send firebase messages, and enters into deep sleep
    • After this time out, the PIR sensor totally shuts down the ESP8266, and looks for the next alarm

    My basic idea was to have at home a fully automatic alarm system. We are always forgetting to switch on/off our alarm...
    Now, as our familly is fully equiped with Androids phones, and as nobody forgets this precious device, we have an automatic alarm system :)

    Thank you @Erel for this fast and efficient firebase integration into B4A.
    And thank you to all of you who share these nice libraries.

    [edit] see it in action with a smoke detector below
    Last edited: May 8, 2017
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Are you saying it is de-centralized because each app is responsible for maintaining its own database state?
    What happens if a device is off, when some one else cleared/generated the alarm, and its turn on, how does the devices get resynchronized?

    Do you have an automated way to reboot the sensors?
    How is the power draw on ESP8266 and how are you powering it currently?
    Have you found a useful housing for it?

    Great work and thanks for sharing.
    fredo likes this.
  3. freedom2000

    freedom2000 Well-Known Member Licensed User


    If device is off, it looses the alarm... next time it will be on it should resynchronize
    As it is off it won't see the message so it won't answer to it, so no problem for the others phones.
    And I did set the priority to "high" so that the messages are sent immediately regardless of the current state off each smartphone.

    The nice thing of my system is that ESP8266 is "always off", they boot when the alarm is triggered by the sensor.
    So a lot of advantages :
    - low power consumption
    - fresh "boot state" --> safer for the ESP8266
    - automatic shut down when the alarm pulse goes down (I set its timeout to 2min)

    For the moment I only have a proto running
    I will proabably design a devoted pcb where PIR sensor and ESP8266 will be stacked, so will be the size of PIR sensor foot print
    I will power it on a 5V supply (either a battery pack for smartphone, or directly a 220V to 5V phone charger with USB plug, or both !)

    And yes, you got it, each smartphone is autonomous and will maintain its own database.
    advantages are :
    • I don't have to pay for firebase DB... it only relies on firebase messaging. So I could put the app on the store without problem
    • nothing remains on the cloud...
    • each phone can select the sensors it wants (or not) to monitor and receive/send acknowledgments
    • each phone can share its database to initialize a new phone and let it enter the (family local) club in one clic !
    Another advantage I have thought about right now, is that you could get notification when a family smartphone enters the house... Nice to follow your kids and be sure they are safe at home !
  4. Miko To

    Miko To New Member

    Hello, i'm using Wio Link ESP8266 based micro controller, but i cannot send message to firebase it says Connection Refused.
    I hope you can help me. :D
  5. freedom2000

    freedom2000 Well-Known Member Licensed User

    here is the code I use for the connection
    You can secure it a little more with https

    declarations :
    /** Google Firebase Messaging server address */
    char serverName[] = 
    /** Google Cloud Messaging host */
    String hostName = "";
    /** WiFiClient class 
    to communicate with GCM server */
    WiFiClient gcmClient;
    code :
    gcmClient.stop(); // Just to be sure
      #ifdef DEBUG_OUT
    Serial.print("Connecting to ");
    String resultJSON; // For later use
    if (gcmClient.connect(serverName, 80)) {
        #ifdef DEBUG_OUT
    Serial.println("Connected to GCM server");
  6. cambopad

    cambopad Active Member Licensed User

    As I know PIR sensor can detect human and animal, so we can use it to get alert to our phone when someone is trying to enter our home.

    One question, how can can I connect the PIR sensor with our phone/b4a app? Do we need additional equipments?
  7. freedom2000

    freedom2000 Well-Known Member Licensed User

    Yes sure you need an ESP8266 with the hadoc firmware
    cambopad likes this.
  8. cambopad

    cambopad Active Member Licensed User

    Thanks, I will google it for more details!
  9. walterf25

    walterf25 Well-Known Member Licensed User

    Hi freedom this is very awesome, i can see a lot of potential with this, i had a similar idea many years ago even before android became popular, I've always thought an alarm system like this where you can monitor everything from anywhere the best thing about it is that you don't need to pay additional fees to a monitoring company, there's a lot of room for improvement on this sort of ideas, I wish you the best of luck, i've never played around with this ESP8266 modules, I might just have to purchase a few and try to find some time to play with them, especially since Halloween is coming here in the U.S. i'd like to scare the hell out of trick or treaters :D with a few PIRs and these other ESP modules, i think it would be fun.

  10. freedom2000

    freedom2000 Well-Known Member Licensed User

    Yes Walter, imagine what you want to do and there will be for sure a way to do it with an esp !

    Btw if interested i can order a few more boards...
  11. freedom2000

    freedom2000 Well-Known Member Licensed User

    In a previous post I explained how I connected a Dash Button to a fire/smoke detector.

    The result was nice, but I didn't rely on Amazon to send notifications... They could change everything when they want, so I decided to test something else : connect an ESP8266 to the sensor and use PIR Alarm App to capture firebase notification.
    I frst tried to directly hook the ESP on the pulse output of the main chip, but it didn't work as this output was a 6KHz pulse to trigger the siren...

    Thus I had been forced to solder a few components to have an "envelop detection circuit".

    envelop detector.jpg

    The idea was to switch on the ESP only when the siren is On. On the previous schema the output signal is tied on a gate of a N Mosfet to use it as a switch.
    And this switch powers the ESP8266.

    Here is the view of the final hack


    And a close up of the envelop detector.
    As you can see I directly connected the ESP8266 on the 3.3V voltage regulator of the Smoke sensor !


    And finally a video of the system working

    Last edited: May 8, 2017
  12. Peter Simpson

    Peter Simpson Well-Known Member Licensed User

    An excellent video and idea @Freedom
    freedom2000 likes this.
  13. freedom2000

    freedom2000 Well-Known Member Licensed User

    Whaou --> thank you !
    alexhi likes this.
  14. hookshy

    hookshy Well-Known Member Licensed User

    Tu est un fort electronicien sur b4a . Je reve d'avoir le temp de developer de nouveau.
    freedom2000 likes this.