Tag Archives: bluetooth

Part 3: Bluetooth communications with 2 Arduino devices, using App Inventor

Please start with “Part 1: Basic Bluetooth communications using App Inventor” to learn how to configure, set up and program an App Inventor app that communicates over Bluetooth between two Android devices. Then, read “How to connect App Inventor apps to Arduino using Bluetooth” before going through this tutorial!

Then continue with this tutorial.

This tutorial shows how an App Inventor app can communicate with 2 (or more) Arduino boards and Bluetooth devices simultaneously. These instructions assume you are familiar with the code and hardware presented in Part 1 and Part 2 and How to connect App Inventor apps to Arduino using Bluetooth“. This tutorial uses the same Arduino source code as in that tutorial.

A follow up tutorial will show how to simplify some of this code for supporting multiple Bluetooth devices.

Brief Reminder

Bluetooth is a short range, low power, limited speed wireless communications technology. The original Bluetooth technology provided a serial communications link between two paired devices (as compared to an individual data packet sent between up to n devices using the much newer Bluetooth LE – see here and here for information on Bluetooth LE).

Arduino is a microcontroller board for building hardware projects. You can write software for Arduino using a programming language similar to the C++ programming language.

The code used in these examples has been tested with some specific Bluetooth modules connected to Arduino. These include the JY-MCU (Amazon (Prime), Amazon (non-Prime) and also some HC-05 and HC-06 based Bluetooth modules.

Getting Started

  • Read the prior tutorials (Part 1 and Part 2 and How to connect App Inventor apps to Arduino using Bluetooth“)
  • Build two Arduino boards each with an appropriate Bluetooth module as described in the prior tutorial.
  • Compile and load the Arduino software in to each of the Arduino boards.
  • Test and confirm that your basic LED lights flash for the original, single Bluetooth connection case.
  • Then, with two working boards, continue to this tutorial.

User Interface View

The original app supported just one device, so there was just a single “Connect” and “Disconnect” button. This version demonstrates how to connect more than one Bluetooth device so we need separate buttons for each device. Similarly, we must add a second status and data sending item to the screen:

 

Before running this app, be sure to use Android | Settings | Bluetooth to “pair” your Bluetooth devices with Android.

Then, run the app and select Connect to Device 1. This displays a list of available Bluetooth devices in the vicinity. Select your specific Bluetooth device for the connection. Do this for both Bluetooth devices.

Once connected, you can send some simple commands to the Arduino board. Commands are very simple – a single number – to tell the Arduino to do something (this confirms that the Bluetooth link is working). If we enter a single digit 1 and then press Send Numeric 1, the Arduino board will send back 2 bytes of data which will then be displayed on the app screen. If we enter a single digit 4 and then press Send Numeric 1, a value of 4 is transmitted over Bluetooth to the Arduino board, which responds by flashing the externally connected LED.

Because the text box for data entry has its property set to NumbersOnly, a pop up numeric keypad displays when entering data, rather than the usual Android text keyboard.

Video Demonstration this App

I created a short video showing this app in operation. There are two versions of the video – one is standard 2D format and the other is in VR 3D format for viewing on Google Cardboard-like viewers used with smart phones to watch VR videos.

2D (normal) version: https://youtu.be/BU2gIAxbY_o

VR 3D SBS version: https://www.youtube.com/watch?v=UJIggzZgld4

That version is in 3D, for viewing with VR 3D viewers or 3D TVs or monitors.

Continue reading Part 3: Bluetooth communications with 2 Arduino devices, using App Inventor

Free-Download App Inventor Bluetooth Communications Cheat Sheet

Download here: App Inventor Bluetooth Cheat Sheet (PDF)

Covers basic Bluetooth text communications with links to tutorials on sending numeric and binary data, and connecting to Arduino over App Inventor Bluetooth.

App Inventor sample source code available here.

High res, suitable for printing. Feel free to share with others.

Here is a GIF image of the first page but use the PDF for printing (higher resolution) and it includes both pages:

Voila_Capture 2015-10-28_08-35-41_PM

Post comments here or on our Facebook group page. Thank you!

E-Books and Printed Books

If you find these tutorials helpful (I hope you do!) please take a look at my books on App Inventor. To learn more about the books and where to get them (they are inexpensive) please see my App Inventor Books page.

  • App Inventor 2 Introduction (Volume 1 e-book)
    Step-by-step guide to easy Android programming
  • App Inventor 2 Advanced Concepts (Volume 2 e-book)
    Step-by-step guide to Advanced features including TinyDB
  • App Inventor 2 Databases and Files (Volume 3 e-book)
    Step-by-step TinyDB, TinyWebDB, Fusion Tables and Files
  • App Inventor 2 Graphics, Animation and Charts (Volume 4 e-book and printed book)
    Step-by-step guide to graphics, animation and charts

Thank you for visiting! — Ed

Android battery life – and how to extend your battery power

When I had a Nexus 4 running Android 4.3 and earlier, I used an app called Juice Defender to extend the time between battery charging. I often went 2 days without recharging the battery!

But Juice Defender has not been updated since 2012 and due to Android changes, Juice Defender no longer works well.

Then, Android 5 resulted in worse battery life for many of us! My phone was discharging half the battery in 8 hours, even when not being used.

The only way to extend battery power is to reduce power demand. That means turning off hardware features that may not need to be used all the time (WiFi, Bluetooth, GPS location), dimming the screen or turning it off and so on. Another way to is reduce the frequency that apps wake up to “sync” or go online.

Good “Battery Saver” apps work by intelligently switching features off and reducing their frequency of use. Some battery saver apps are good and some are awful; in fact, some have displayed false battery status to pretend they are saving power! I have tried numerous Battery Savers but found only one that works effectively on the Nexus 5: Avast Battery Manager (see link below).

With that in mind, here are ideas that may help your device reduce its power demand and extend is battery life between charges:

  • Install Avast Battery Manager from Google Play. This works well for me using its “Automatic mode” settings. The app also provides information about which apps are consuming power on your phone. You may choose to stop, disable or uninstall apps that consume excess power.
  • Google Chrome and GMail apps are power hogs relative to other apps. It seems that if you visit a page, like a financial page, that periodically “auto refreshes” (e.g. for stock market data), this auto refresh may continue to occur periodically when you are not using the phone (this is my hypothesis – its not yet verified.) Avoid leaving Chrome on such pages, if you can. In GMail, go to the GMail menu (the one where you can select Inbox, Sent, Outbox, etc), scroll all the way to the bottom and choose Settings. For your GMail account, uncheck Sync GMail – and then manually resync GMail by swiping down from the top when you are using GMail. For POP3/IMAP email accounts (if any), set the sync time to 60 minutes (the longest option available) – or go to Settings | Accounts, select the email account, and turn off sync completely.
  • Many apps start up when your device is powered up and drain a small amount of power running in the background. Even if you never use the apps. Uninstall apps that you no longer use or you do not need.
  • Use Wi-Fi, if available, instead of cellular data. Generally, good Wi-Fi data links are much faster than cellular data, which means data can be uploaded or downloaded in less time. That means the transmitter (which uses more power) is active for less time, helping to reduce power. Further, due to some issues in how the cellular data protocol works, the cellular transmitter remains in an elevated power state for several seconds after being active for a data transmission. Related: While out and about and using only cellular data, turn off WiFi. You may also consider disabling Bluetooth and Location services.
  • If battery life seems to be getting worse, go to Settings | Storage, and scroll down to Cached data. Select “Cached data” and then follow the pop up menu to clear the cached memory. This is not something you do every day – but when the battery has gotten bad, taking this step every once in a while has significantly improved the battery power.
  • If you are using Avast, you can likely skip this step: Go to Settings | Battery and click on the 3 vertical buttons at upper right. Then click on Battery Saver and set this to “On”. Normally, Android’s own battery saver only activates when the battery is very low, but you can activate it manually. This built in Battery Saver reduces app data synchronization with the network, disables location services and does a few other things to reduce power. The Battery Saver is automatically turned off and remains off, once you plug in to a charger.
  • Aggressive: Set your device to Airplane mode. This turns off all built in radios and suspends background apps from doing data communications. Again, if using Avast, you are already getting good power management and this step adds only a little to the battery life. And while its activated, you cannot receive voice or text messages either!

Hopefully these suggestions are helpful to you!

How to connect App Inventor apps to Arduino using Bluetooth wireless

How to Connect App Inventor apps to Arduino Using Bluetooth

Bluetooth is a low power, short range wireless technology built in to many phones, tablets and other devices.

MIT App Inventor 2 supports a set of Bluetooth communication functions that may be used to send data between smart phones and tablets (see previous tutorials: Part 1, Part 2)

This capability may be extended so that App Inventor apps can communicate with Arduino-based devices and other embedded systems.

This tutorial describes how to interface App Inventor apps running on Android to Arduino devices, via the Bluetooth wireless link.

What is Bluetooth?

Bluetooth is an industry standard for low power, short range wireless communications between devices such as personal computers, printers, smart phones, tablets, wireless headphones, wireless stereo speakers, sensor systems (like in security alarms) and other applications.

To learn more about Bluetooth technology (and why it has a funny name!), please read our first tutorial on Bluetooth.

What is Arduino?

Arduino is an open hardware, open software platform for building small electronic devices. The Arduino board is a “microcontroller” – that is, a complete – albeit small, inexpensive and with limited function – computer. Arduino is a popular choice for do-it-yourself projects and is well established in the “Maker” community of DIY project builders. (Side note: I will be at the San Francisco Maker Faire on Saturday, May 16th, 2015).

This is not a tutorial about Arduino boards, software or electronics and presumes the reader is familiar with Arduino development. To learn more about Arduino (and you should learn more about it!) start at the Arduino web site.

This tutorial assumes you have the Arduino software development environment installed on your computer and are familiar with Arduino development.

HARDWARE: Setting Up Arduino for Bluetooth Wireless Communications

There are several versions of the Arduino board; I used the Uno version but others should work just fine.

The Arduino board does not contain Bluetooth hardware – to implement Bluetooth requires using a third-party Bluetooth module. I use the JY-MCU Bluetooth module . IMPORTANT – not all Bluetooth modules will work with App Inventor!  While new versions of Android support all versions of Bluetooth, App Inventor (at the time of this writing) supports “classic” Bluetooth only. In particular, App Inventor does not support the newer Bluetooth LE (Low Energy) version, at least it does not support the Bluetooth LE module that I have.

I can confirm that the JY-MCU Bluetooth module works but the Bluetooth LE modules I have do not work with App Inventor.  My phone can see the Bluetooth LE device but the App Inventor source code cannot communicate with the LE devices.

Where to buy the JY-MCU Module online: Amazon (Prime), Amazon (non-Prime)

The module is also available from other vendors.

Photo shows my Arduino UNO board, at left, a prototyping breadboard with a status LED set up, and the JY-MCU Bluetooth module, just above the breadboard.

DSC_1266Click through to  see how the Arduino and Bluetooth module are setup, and get the Arduino source code and the App Inventor source code!

Continue reading How to connect App Inventor apps to Arduino using Bluetooth wireless

Coming soon: Bluetooth to connect Android App Inventor code to Arduino

I  have an App Inventor app running on my Nexus 5 and talking to an Arduino board via Bluetooth. I finally had a chance to work on this!

UPDATE: Here is the link to the final code and tutorial information!

What I have  now is some experimental code not suitable for posting online. I will be revising this code to turn it into a simple example that will provide basic functionality, and then present a tutorial on putting it to use in your own applications.

Update Tuesday Cinco de Mayo (in the U.S.): I have the demo code up and running. Next up is to test and write up the tutorial! It’s coming! The first tutorial will be simple – intended to get you up and running.  I will eventually create some more advanced features.

Longer term, I may create a more general solution for passing data packets back and forth between an Android App Inventor app and an Arduino board, so that many types of applications may be supported using my basic code library.

Arduino is a small microcontroller board used by hobbyists and others to add computing to small devices, art projects, robots, Internet connected devices and much more. Arduino is not part of App Inventor. Arduino is, for an “embedded system” easy to use in terms of building electronics hardware and writing control software. By writing App Inventor code to talk to an Arduino board, we open an entire world of new possibilities using simplified development (App Inventor on Android, and Arduino on the hardware side).

  • Use your phone or tablet to remote control an Arduino device over a Bluetooth link
  • Use an Arduino device to monitor remote sensors, and then link sensor inputs to an Android phone over Bluetooth
  • Conceptually, an Arduino device could monitor local sensors (temperature, humidity, security alarms), and transmit sensor data to an Android phone, which, in turn, could forward the data onto an Internet location.

Part 2: Sending numeric data using App Inventor Bluetooth communications

Part 1 of this tutorial introduced Bluetooth communications and implemented a simple method of sending text data back and forth between two Android devices over the Bluetooth wireless link. If you are not familiar with using App Inventor’s Bluetooth component, start with Part 1.

In Part 2, a data packet concept is introduced to guide the communications between devices, and is used to send a combination of text and numeric data. This section introduces the concept of binary numbers so that you can understand why we would handle text and numbers in different ways.

This tutorial modifies the user interface of both the client and server programs introduced in Part 1. Then, blocks code is added to send text and numeric data. Numeric data is sent as binary data using special methods of the Bluetooth components.

Related:

Continue reading Part 2: Sending numeric data using App Inventor Bluetooth communications