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.

Identifying the cause of serious errors in App Inventor apps

Some times App Inventor 2 apps stop working (well mine sometimes do this, may be your’s don’t!)

Besides looking carefully at your blocks code, there are some additional steps that may be helpful in identifying the cause of the problem.

1. Check your blocks code very carefully to see if you can spot an error in your program.

2. Add a status message area in the user interface (using the Designer) and then in your blocks code, add blocks to assign status texts to this message area. Your status message could include a message indicating what section of your code is running, or to display the values of variables in your program.

3. Add a special “Error handler” to catch and display information when the program encounters a serious problem. The following section is adapted from my tutorial on basic Bluetooth communications.

Error  Handling

For most of our App Inventor apps, we ignore potential errors – if errors occur, the app stops running and Android displays error messages.

Our app can intercept the error condition by adding an error event handler to the main screen, Screen1. In the Blocks editor, select the Screen1 component and then select the When Screen1.ErrorOccurred event. Drag this to the Blocks editor and then add the following code.

The ErrorOccurred event has four parameter values (local variables) that contain information about the error. This error handler displays the error values on your device screen, rather than immediately shutting down the app.

BTServerErrorHandler

 

To use this code, add a text label to your user interface – in the example above, the label has been renamed to lblStatus.

There is no guarantee that these error values will identify the specific problem but they may give an indication as to where the problem is occurring in your program!

App Inventor adds new source code “Gallery”

App Inventor has added a new source code “Gallery” for sharing source code with others.

From the My Projects list of your projects, at the far right, find a new feature to “Publish to Gallery”. To use, create a graphic image to illustrate your app – such as a screenshot. You will later upload this to the Gallery along with a description of what your source code does.

Add a link to a web page (if any) that contains a tutorial or video about your app or source code.

PublishGallery

To find other code already added to the Gallery, click on the Gallery menu item near the top/upper right of the AI menu.

Have fun!

(And yes, I am back from recent travels – still recovering – fell a sleep after lunch today! I hope to start adding some of the source code from this blog to the Gallery soon!)

How to take a Screenshot

(This works on my Android devices) To take a snapshot of what is on your Android device screen, press the device power button and the volume control DOWN button simultaneously. This captures a screen snapshot and saves it to your Photo Gallery.  Email that photo to yourself or do what ever you do to transfer files from your device to your computer, for subsequent processing/viewing, editing, re-sizing and what have you.

Can you build an App Inventor app for the Google Play store?

I wrote this in response to a question on this web site’s Facebook page.

Someone asked if there was perhaps a paid version of App Inventor required for building apps that go in the Google Play store? No, there is no paid version.

You do not need a paid version of App Inventor; just use the Build .apk option to create an Android package file that can be installed on an Android device.

You can then submit your apps to the Google Play store. You will need to pay a US $25, one time only, to open a developer account. Thereafter, you may upload as many apps as you want to the Play store.

You’ll also need to create some screen snapshots and upload those as part of your app description in the Play store. There is much additional good information here – http://beta.appinventor.mit.edu/learn/reference/other/appstoplay.html

How to create App Inventor apps for the Google Play store is on my list of future tutorials. Unfortunately, I do not have time right at the moment to put that together – but eventually I will have something and describe the process in step-by-step detail!

Multiline text input in App Inventor

The App Inventor TextBox control supports both single line and multi-line input; the TextBox control default to single line mode – and you may not have realized it can support multiple line text input too!

In single line input, text is entered using the on screen keyboard, followed by the Enter key (or you can use an external Bluetooth keyboard). Once the enter key is pressed, the on screen keyboard goes away.

Multi-line mode is enabled by checking the Multiline checkbox item in the control properties. However, the Enter key is used to enter multiple text lines – that means you need to complete the multiline text entry by adding another control, such as a Done button.

The multi-line control is easy to use – and needs just a single line of blocks code to implement! Describing the operation of the control takes more time!

App View

The multiline TextBox looks like a single line text box, as shown here, after entering “first line” into the TextBox control as the app is running.

Screenshot_2015-04-08-18-03-40

To enter a second (or third…) line, tap the green/blue Enter button at the lower right of the onscreen keyboard. Here is the result after typing three lines of text:

Screenshot_2015-04-08-18-03-57

Continue reading Multiline text input in App Inventor

Learn to program with App Inventor