Lab1 - Rmic2324
Lab1 - Rmic2324
2023-2024, MEEC
1 INTRODUCTION
This assignment is intended to be followed by the students in order to give their first steps in
Android IoT application programming. In the course of this tutorial, a simple Android app will be
developed and connected to a Google Firebase project. The developed app will be used as a basis
for subsequent lab assignments. It is assumed that the students have the Android Studio IDE
installed in their machines.
The instructions in this assignment are abridged so that, at each step, the students will have to
investigate how to implement the requested functionality. At the end of this document, a list of
useful references is provided.
1|P á g i n a
REDES MÓVEIS E INTERNET DAS COISAS
2023-2024, MEEC
1) Create a new project called “Assignment 1 GR00”, with project type “Empty Views Activity”
and minimum SDK version 19 (Android 4.4 KitKat). The GR00 indicates the RMSF lab group
number, in this case group 0. The language should be “Java”.
2) Verify that the new project has a single activity called MainActivity, whose class is defined
in file MainActivity.java. The layout of MainActivity is very simple and has only two widgets:
a ConstraintLayout with a TextView inside.
3) Modify the layout so that it will include two TextView gadgets and one Button gadget
positioned as shown in Fig. 1. The default text in each gadget indicates the database item
to which it will be associated. You can check the layout using the Android Emulator.
4) Customize the app icon to be the IST logo1!
1) When creating the app, one is asked the name of the package, which is the application ID.
The latter can be found in project’s app folder inside build.gradle.
1 https://developer.android.com/studio/write/image-asset-studio#create-adaptive
2 Some instructions and outdated regarding the current version of Gradle and should be adapted. See,
for example this link.
2|P á g i n a
REDES MÓVEIS E INTERNET DAS COISAS
2023-2024, MEEC
2) There are different 2 main build gradle files. To access the build gradle of the project/ app
choose one of “Project: …” or “Module: …” within the app folder.
3) In the new versions of build.gradle it’s recommended to put the “Build script” given by
Firebase tutorial before plugins and “ all projects” below plugins and on top of task clean.
4) When configuring build.gradle at app level, don’t forget to add the following dependency:
implementation 'com.google.firebase:firebase-database'.
5) This video may be useful: https://www.youtube.com/watch?v=hjKtOMvauus .
1) Add functionality to class MainActivity, so that the value of the measurement database
item is shown in the measurement TextView.
2) Add functionality to class MainActivity class, so that:
a. Whenever the alarm_status database item is set to false the alarm_status TextView
displays “Nothing to Report”.
b. Whenever the alarm_status database item is set to true the alarm_status TextView
displays “ALERT! ALERT!” and one notification sound is played.
3) Add functionality to class MainActivity class, so that:
a. The enable_alert_sound Button is initialized with a string that corresponds to the
value of the enable_alert_sound database item. In case this is true, the button will
display “Disable Buzzer”; in case it is false, the button will display “Enable Buzzer”.
b. Whenever the enable_alert_sound Button displays “Disable Buzzer” and the user
presses the button, database item enable_alert_sound will be set to false and the
button will display “Enable Buzzer”.
c. Whenever the enable_alert_sound Button displays “Enable Buzzer” and the user
presses the button, database item enable_alert_sound will be set to true and the
button will display “Disable Buzzer”.
Tip 1: Add the following imports, which will let you call the Firebase API methods:
• com.google.firebase.database.DataSnapshot;
3|P á g i n a
REDES MÓVEIS E INTERNET DAS COISAS
2023-2024, MEEC
• com.google.firebase.database.DatabaseError;
• com.google.firebase.database.DatabaseReference;
• com.google.firebase.database.FirebaseDatabase;
• com.google.firebase.database.ValueEventListener;
Tip2: Class DatabaseReference will allow you to get references to the Firebase database
resources through calls to DatabaseReference::getInstance().getReference().child(…). Get and
set operations on the database items can be accomplished through
DatabaseReference::getValue(…) and DatabaseReference::SetValue(…) methods3.
Tip3: When configuring build.gradle at app level, don’t forget to add the following dependency:
implementation 'com.google.firebase:firebase-database'.
4|P á g i n a
REDES MÓVEIS E INTERNET DAS COISAS
2023-2024, MEEC
The node MCU will receive power from the USB interface and will be connected with a HC-SR04
ultrasonic sensor (see Fig. 3) and a piezo buzzer (actuator).
NodeMCU includes an onboard LED that can be controlled by programs through the GPIO2 pin
of the ESP8266 chip.
1) The Node MCU will be programmed using the Arduino IDE. In order for the Node MCU to be
recognized in this development environment, configure Arduino IDE for NodeMCU according
to the instructions in this link or an equivalent one.
2) Mount the circuit as depicted in Fig. 4.
Fig. 4: Ultrasonic detector and alarm circuit using NodeMCU and HC-SR04.
3) Install the Firebase Arduino Client Library for ESP8266 and ESP32 by Mobizt as shown in Fig.
5.
5|P á g i n a
REDES MÓVEIS E INTERNET DAS COISAS
2023-2024, MEEC
Fig. 5: Library Manager of the Arduino IDE with Firebase ESP8266 Client library installed.
4) Based on a library example (e.g., Blynk.ino, or Basic.ino), build your own Arduino program to
perform the following actions in the main loop (loop() function):
• Read the distance measured by the sensor. If the distance is shorter than 20 cm,
consider that an intruder is detected, otherwise, there is no alert situation. Update the
alarm_status database variable accordingly. In case motion is detected, increment the
measurement database variable and activate the NodeMCU LED.
• If the enable_alert_sound database variable is true and motion was detected, sound the
buzzer (suggestion: use the tone() function together with pitch constants provided in
file pitch.h).
• Wait for 1 second before the end of the loop() function.
5) In order to configure the .ino project, you will need to obtain the API_KEY and
DATABASE_URL parameters, which can be retrieved from the Google Firebase console. In
order to obtain the API Key, go to Project Overview/Project settings. The Database URL can be
directly retrieved from Realtime Database.
6) NodeMCU is based on the ESP8266 chip. This chip is incompatible with eduroam
authentication procedures. In order to connect to Internet, inside the classroom use the
following WiFi network:
• SSID: Labs-LSD
• Password: aulaslsd
7) Once the NodeMCU program is correct, you should observe the expected hardware behavior
and the Firebase database variables should change according to the motion detection events.
Once the Android App is running, it should also show the current database status, as well as
to enable and disable the buzzer when the user presses the button.
7 REFERENCES
6|P á g i n a