Is Arduino running – Er døren låst?

Is Arduino running – Er døren låst?

(For dansk version klik på Dannebrog øverst i Sidebar)

A friend asked me for some help with a project in his mothers house. During the day she is going to be visited by caregivers who shall help her with household and personal care. He wants to be sure, that they remember to lock the main door. He already did install a switch. When locked the switch is closed and otherwise it is open. The rest was up to me, – some electronic gadget to report the state of the lock

Easy peasy, I thought. Just a Nano connected to the Blynk server, some wires and a LED to show Power On. The Fritzing diagram can hardly be more simple:

CheckDoor Fritzing

A red LED for locked, a green LED for open and a lcd-widget to live up the screen of the smartphone and a few lines of code should get this project running. With SimpleTimer and a snippet of proper code it will be possibly to limit the updates to situations, where things change. No big deal.

But starting to work with the project, I soon realized that things appeared to be a little more complicated. To make a trustworthy app there certainly are things to be taken into consideration. The app always shows the latest bit of information received but how can you know that it is not obsolete due to a breakdown somewhere along the road? You MUST know whether your Nano is up and running or not. If it is down, the information on the screen is worthless. To complicate things even more I experienced, that my screen was not updated, when my phone returned from standby mode. So the first impression did not last. Some way or the other I had to build some security into the code to make the system robust and reliable.

I got it narrowed down to these three fields of interest to be taken care of in the code to make the app reliable and aplicable:

  1. ‘Arduino is active’ indicator
  2. In sync or not? Ensure that the app is being updated when Arduino is down, the net is down, app return from shut down or standby
  3. Is my Nano connected to the server? Some kind of indicator at the hardware.

1. ‘Arduino is active’ indicator.

This can be achieved with a new LED-widget on virtual pin V31. To show activity the LED must be active, – i.e. blinking. It can be done with these small functions:

With a little help from SimpleTimer they can be timed like this in setup():

Now the blue LED in my app is blinking when Arduino is running. If it does not blink, my Arduino is down and the information on my screen is not reliable.

2. In sync or not?

This can be dealt with in different ways and I did experiment with quite a few of the solutions suggested by the staff behind Blynk. I tried BLYNK_CONNECTED, syncAll() and syncVirtual(pin) but finally decided to sync every 30 seconds with a call to my own function updateApp(). To spice up things I added a notify-widget to the app. Now there will be a notification each time the state of the lock changes with a limitation of max one notification each minute. I chose 90 secs to be sure to get my message through. And again with a little help from Simpletimer:

3. Is my Nano connected to the server?

Inspired by the staff behind the Blynk-project, I tried different approaches to this problem. But none was satisfying. Then I dived into the header-files Blynk.h and UIPEthernet.h and found a PUBLIC method, connected(), which can do the job. It is implemented in my blink-functions like this:

Originally the LED on CONNECT_PIN was an indikator for Power On. Now it is blinking to show “connection OK” instead – my Arduino is running AND is connected to the Blynk-server. If connection is lost, the LED will blink on, though. But just for 9-10 times, the Arduino Heartbeat. After that it will stop blinking.


CheckDoor Hardware

(Døren er LÅST – Door is LOCKED. Døren er ÅBEN – Door is OPEN)

Blynk app

As seen on the pictures, I have these widgets in my app:

  • LED on V29 (red)
  • LED on V30 (green)
  • LED on V31 (blue)
  • LCD on V1, – in Advanced Mode
  • – and a Notify-widget (remember to permit Blynk to send notifications to your smartphone)

That’s it.

You can get the code here:


Leave a Reply

Your email address will not be published. Required fields are marked *