Cloud storage for your IoT projects

App Inventor

App Inventor [4] [5] is a web-based Android app creation tool with a graphical programming environment. App Inventor has two main screens: the Designer screen, on which you lay out the Android app, and the Blocks screen, where you build the logic. On the right side of the top menubar, the Designer and Blocks buttons allow you to toggle between the two screens.

On the Designer screen, an app layout is created by dragging a component from the Palette window onto the Viewer. The App Inventor Android app I create here will read Raspberry Pi values from the Firebase IoT database and write a value back.

The visual layout of this application uses a Button, two Labels, a ListView, and a TextBox component. A non-visual FirebaseDB component connects to the Firebase Realtime Database (Figure 10). After a component is added to the application, you can rename or delete that component from the Components window. In the Properties window, you can edit the features of a component. For example, for the FirebaseDB component, I need to configure the Token and URL to match my project credentials.

Figure 10: App Inventor screen layout.

Once the layout design is complete, you add logic by clicking on the Blocks button in the top menubar. To build the logic, you select an object in the Blocks window and click on the specific block you want to use.

App Inventor is pretty amazing when it comes to doing some very quick prototyping. The entire app in this example only uses two main blocks (Figure 11).

Figure 11: App Inventor logic to read/write to Firebase.

The when FirebaseDB1.DataChanged block is executed whenever new data arrives into the Firebase database. The DataChanged block returns tag and value variables. The tag variable is the top-level item name, (i.e., pi in this example). The value will be a string of the items and their values, for example:

{pi_value2 = 34, pi_value1=77}

A replace all text block removes the leading and trailing } characters and then passes the string to the ListView component. Note this same code will pass two or 2,000 tags in the pi tag section. The when BT_PUT.Click block writes the text entered on the screen into the android/and_value1 item in the Firebase database.

After the screen layout and logic are complete, the Build menu item compiles the app, which can then be made available as an APK downloadable file or as a QR code link. Figure 12 shows the final Android app communicating with the Firebase Realtime Database.

Figure 12: Android App Inventor app talking to Firebase.

Python Data Monitoring Example

The example IoT Firebase Realtime Database is quite simple. Data monitoring or Supervisory Control and Data Acquisition (SCADA) projects usually require more information than just a value. A more realistic sensor database would include fields such as a tag name, description, status, time, and units. By adding some indexing in the Firebase security rules (Figure 13), you can create queries and sort the data, such as Points in alarm or Point values between 2:00 and 2:15.

Figure 13: Indexing on Firebase to allow queries.

In Listing 2, the statement

tag_sum = db.child("pi").order_by_child("status").equal_to("ALARM").get()

Listing 2

firebase2.py

 

(line 14) shows only records that have the ALARM status. Some other filter options include .limit_to_first(n), .start_at(time1).end_at(time2).get(), and .order_by_value().

A good next step would be to pass important filtered information to Firebase's messaging feature.

Summary

Without a lot of configuration, you can configure a Google Firebase project and have Python, Node-RED, and App Inventor apps read and write values. The level of programming complexity is on par with an MQTT implementation; however, Google Firebase offers a lot more future functionality that you wouldn't have with MQTT, such as file storage, machine learning, messaging, and server-side functions.

The Author

You can investigate more neat projects by Pete Metcalfe and his daughters at https://funprojects.blog.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News