Posts with «setup» label

Bluetooth Android Processing 1


PART ONE


Introduction

This is a four part tutorial which will take you through step-by-step on how to create Android apps on your Mobile device that will allow you to communicate with your Arduino over Bluetooth. This tutorial is based upon the Windows environment and an Android device like the Samsung Galaxy S2 Phone.
I will take you through setting up your computer and phone, and will move through in stages so that you understand what each part of the bluetooth code is actually doing. Obviously you will need to ensure that you have a Bluetooth Shield on your Arduino to be able to walk through this tutorial with me.
If you are not interested in the step-by-step instructions, you can jump straight to the end (Part 4) which will have the complete Arduino and Android/Processing code that was used in the following video:

The Goal of this project (Video)


Setting up Processing for Android applications:
For the latest and most up to date version, please follow the instructions on this website: http://wiki.processing.org/w/Android

    Step One:
    Download the Android SDK from this website:
    http://developer.android.com/sdk/index.html

    Android SDK Download:
    Make sure to select the "Use and Existing IDE" link, as per the picture below.



    When you select the "Use an Existing IDE" link, it will then show you the appropriate download to use. This is what it should look like.

    Select the "Download the SDK Tools for Windows" link.
    Once you have downloaded the Android SDK, go ahead and install it as per the instructions below.
    These instruction can also be found here.






    Installing the necessary packages in the Android SDK Manager program
    Make the following 3 selections:
    • Tools: Android SDK Platform-tools
    • API 10: SDK Platform
    • Extras: Google USB Driver
    Then select the button on the bottom-right to install your selections.

    Here is a picture of the Android SDK Manager selections:


    While you may decide to download other packages,
    you MUST download API 10: SDK Platform .
    Do not leave this one out !!



    Step Two: Processing Download

    Download the latest Processing IDE(version 2.0 Beta 8) from this website:
    http://processing.org/download/

    I am using Windows 7, and have chosen to download the Windows 32 bit version as shown below.




    Load Processing, and switch to Android mode, as per the image below.




    You should now have an empty sketch window which looks something like this.





    Step Three: Setting up the Android Hardware device (Phone)
    For the latest steps you can have a look at this site:
    http://developer.android.com/tools/device.html

    However, these are the ones that I carried out:

    Turn on USB debugging on your Android Phone:
    To find out what Android Version you are on, have a look at
        Settings > About Phone : look for heading "Android Version".
    • My Android version is 2.3.4 on my Samsung Galaxy S2.
    To Enable USB Debugging:
       
    Settings > Applications > Development > Select (or Enable) USB debugging

      For those of you who have a different Android version, have a look below:





      Downloading the USB driver for your Android Phone(Windows Users)
      If you are developing on Windows and would like to connect an Android-powered device to test your applications, then you need to install the appropriate USB driver. Have a look at this site for more information on how to download the USB driver for your phone:
      http://developer.android.com/tools/extras/oem-usb.html

      I have a Samsung Galaxy S2 phone, so I had to go to the Samsung Site here:
      http://www.samsung.com/us/support/downloads

      But because I am not in the USA, I had to click on the link for "non-US products":
      http://www.samsung.com/us/support/downloads/global

      You will need the model number of your phone:
      On the Samsung Galaxy S2, you can go into
          Settings > About Phone => Model number. Otherwise, it is located behind the battery.
      • My Phone's Model Number is: GT-I9100
      See the image below for the link to press if you have a non-US phone.



      Then I continued with the install of the USB driver as per the document below:
      http://developer.android.com/tools/extras/oem-usb.html







      Step Four: Android-Processing Sketch
      We will now test our our current setup and make sure that we can run a simple Processing Sketch on the Phone. Bluetooth functionality will be tested later on, so all we need for this step, is our computer, our Android phone, and a USB cable. While it is possible to run this sketch without an Android phone (by using the emulator), I personally do not have the patience to wait an eternity while the emulator boots up... (yes, it takes an eternity)... In this tutorial, we are going to test it on the device (phone).
      This sketch has an orange background and a black circle which you can move around the screen with your finger (that's it) - I did say it was going to be a simple sketch.

      Copy and paste the following Android-Processing sketch into the IDE, and then press the (Run on Device) button, which is the triangle button or press Ctrl-R.


      Android/Processing Sketch 1: Circle Dragger

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      /*Circle Dragger: Simple Android-Processing Sketch written by ScottC on 13/03/2013.
      Visit: http://arduinobasics.blogspot.com/
      */

      int circleWidth = 150;
      void setup(){
      orientation(LANDSCAPE);
      }
      void draw(){
      background(255,100,0);
      fill(0);
      ellipse(mouseX,mouseY,circleWidth,circleWidth);
      }


      You should see an orange screen appear on your phone. Move your finger across the screen and watch as the black circle follows your finger.
      No, not rocket science, but hopefully everything worked as planned. If you want to change the colour of the background or circle, this is a good site:
      http://www.colorpicker.com/



      Step Five: Bluetooth testing:
      We are now going to walk through Bluetooth connectivity. While we could just use a library to do all the heavy lifting for us, I decided to explore Bluetooth functionality from scratch. This will hopefully provide greater returns in the long run. Ok, lets create a new Android/Processing Sketch which changes its behaviour depending on whether Bluetooth is enabled or disabled when the sketch is run. We will display a red screen when Bluetooth is switched off, and green when Bluetooth is switched on.

      To enable Bluetooth on my Samsung Galaxy SII phone:
      • Settings >Wireless and Network > Bluetooth Settings > Bluetooth (Turn on Bluetooth) - check the box

      To disable Bluetooth on my Samsung Galaxy SII phone:
      • Settings >Wireless and Network > Bluetooth Settings > Bluetooth - Uncheck the box

      In the processing/android IDE, you need to make sure that you update the AndroidManifest.xml file to grant specific permissions. You can either edit the file manually in the sketch folder, however, it is much easier and safer to do the following. In the processing/android IDE, select:
      •   Android > Sketch permissions  (as per the picture below)

      • Make sure that BLUETOOTH and BLUETOOTH_ADMIN are selected (as per the picture below). Then press the OK button.


      Then copy and paste the following sketch into the processing/android IDE:


      Android/Processing Sketch 2: BluetoothChecker1
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      /*BluetoothChecker1: Written by ScottC on 17 March 2013
      This will show a red screen if Bluetooth is off,
      and a green screen when Bluetooth is switched on */


      import android.bluetooth.BluetoothAdapter;

      BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
      void setup(){
      orientation(LANDSCAPE);
      }
      void draw(){
      if(bluetooth.isEnabled()){
      background(10,255,30);
      }
      else {
      background(255,10,30);
      }
      }


      When you run the BluetoothChecker1 sketch on the device, you will either see a red screen or a green screen depending on whether you had Bluetooth enabled or disabled at the time. Ok, pretty boring, but it is a start. What if we wanted to ask the USER if they would like to enable Bluetooth at the beginning? We could then change the appearance of the screen depending on their selected answer. Before we add this functionality, I would recommend that you read about the following concepts introduced in the next sketch.
      While it is actually possible to turn bluetooth on without asking for permission, I thought I would retain my manners for the following sketch:

      Android/Processing Sketch 3: BluetoothChecker2
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      /*BluetoothChecker2: Written by ScottC on 17 March 2013

      If Bluetooth is already ON when you run this sketch,
      the background will display BLUE.

      If Bluetooth is OFF when you run this sketch but you
      agree to turn it on, the background will display GREEN.

      If Bluetooth is OFF when you run this sketch and then
      choose to keep it off, the background will display RED.

      =======================================================*/


      import android.bluetooth.BluetoothAdapter;
      import android.content.Intent;
      int BACKGND=0; //Set the background to BLUE
      //Get the default Bluetooth adapter
      BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();

      /*The startActivityForResult() launches an Activity which is
      used to request the user to turn Bluetooth on.
      The following onActivityResult() method is called when the
      Activity exits. */

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data){
      if(requestCode==0){
      if(resultCode == RESULT_OK){
      BACKGND=2;
      //Set the background to GREEN
      }
      else {
      BACKGND=1;
      //Set the background to RED
      }
      }
      }
      void setup(){
      orientation(LANDSCAPE);

      /*IF Bluetooth is NOT enabled,
      then ask user permission to enable it */

      if (!bluetooth.isEnabled()) {
      Intent requestBluetooth =
      new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
      startActivityForResult(requestBluetooth, 0);
      }
      }
      void draw(){
      if(BACKGND==0){
      background(10,10,255);
      //Set background to BLUE
      }
      else if(BACKGND==1) {
      background(255,10,10);
      //Set background to RED
      }
      else {
      background(10,255,10);
      //Set background to GREEN
      }
      }

      I tried my best to explain the code via the comments within. I hope it made sense.


      Useful Links:

      Android Processing Wiki: http://wiki.processing.org/w/Android

      Here is a good tutorial which helped me put Processing and Android together:
      http://www.creativeapplications.net/android/mobile-app-development-processing-android-tutorial/

      And most importantly:
      The Android Developers site : Bluetooth



      Click here for PART TWO

      Click here for PART THREE

      Click here for PART FOUR

       
       



      If you like this page, please do me a favour and show your appreciation :

       
      Visit my ArduinoBasics Google + page.
      Follow me on Twitter by looking for ScottC @ArduinoBasics.
      Have a look at my videos on my YouTube channel.


       
       

       
       
       


      However, if you do not have a google profile...
      Feel free to share this page with your friends in any way you see fit.

      Arduino UNO - XBee Setup

      There are many XBee tutorials out there, but I could not find one that I could apply to my specific Arduino/Shield/Xbee configuration. While this particular configuration may not apply to you, perhaps it will send you in the right direction, so please read on.
      Please note, that your XBee shield may require you to perform extra steps, so please do further reading before jumping in head first. I am not an expert, so don't blame me if you fry your board.

      The following set of steps worked for me:


      Step 1: Download and Install X-CTU

      The easiest way to setup your XBee module is via the X-CTU software from Digi. It can be downloaded from their site here:  http://www.digi.com/support/productdetail?pid=3257

      • Select  the Diagnostics, Utilities and MIBs section


      • Download and install the XCTU 32-bit ver 5.2.7.5 installer (do a virus check after download)
      • Please note that this is for Windows platforms only

      • FYI: We will run the software later



      Step 2: Upload the bare-minimum script on your Arduino controller

      I perform this step to prevent any previous projects from interfering. This may not be necessary, but I tend to do this before wiring up any of my new projects. This is the script I load onto the Arduino:

      Bare Minimum Arduino Sketch:
      1
      2
      3
      4
      5
      6
      7
      void setup() {

      }

      void loop() {

      }




      Step 3: Connect the Shield to Arduino, and XBee to Shield.


      • Disconnect the Arduino UNO from the computer. 

      • Insert the XBee module into the Shield.
      • This is the XBee Shield and XBee module side by side




        This is the XBee module on the XBee Shield


      • The pins from the XBee module should slot into the respective headers on the Shield.


      • Connect the XBee shield to the Arduino

      • The pins from the XBee shield should slot into the respective headers on the Arduino board.



      • Move the little white switch in the corner to USB.

        • There are 2 options - USB mode and XBee mode.
        • We want USB mode which will allow the computer to communicate with the XBee module



      Step 4: Connect Arduino to computer, and run the X-CTU Software


      • With the XBee module and Shield connected to the Arduino, and the Shield's white switch in USB mode;  connect the Arduino to the computer using a USB cable.
      • This Arduino board appears as COM7 on my computer.
      • Here is what the X-CTU software looks like when it loads up




      • Press the Test / Query button to see if the computer can talk to the XBee module.
        • If successful, you should see something like this:





      • If you accidentally leave the XBee shield in XBee mode, or if your computer fails to communicate with the XBee module, you may encounter these screens.


      If you get a successful Test / Query, then move onto the next step.


      Step 5: Read XBee Firmware settings:

      We can now try to read the XBee firmware settings by
      • Selecting the Modem Configuration Tab

      • Select the Read button to read the XBee firmware settings


      Step 6: Download older version of firmware if necessary

      • If you did not have any issues with the previous step, then continue to step 7, otherwise read on.
      • In my case, the software could not find the firmware file necessary to read the firmware settings.
      • In step 4, I could see that I had the XB24 Modem type, and 10E8 firmware version,

      • I tried downloading new versions, but this did not seem to work, as the computer could not detect any newer updates available. The problem I had, was that I needed an "older version" of firmware to communicate with the XBee module in order to read/write new settings to it.
      • I could not select the firmware version from the drop down boxes, which indicated that this particular firmware version was not installed on my computer. So here is what I did.
        • I went back to the Digi site and selected the Firmware update section.
        • This provided a link to an FTP site with "previous versions" of firmware.





      • I then selected the relevant firmware version from the list, and downloaded the zip file to a logical folder on my hard-drive.  There is no need to unzip the file, because the X-CTU software will look for a zipped file.



      • In the X-CTU software, in the Modem Configurations tab, select the "Download new versions button", and select File. Navigate to the zip-file just downloaded and select it. 


      • The software should tell you that it has updated successfully or something to that effect.
      • The modem type and version should now be an available option in the drop down boxes, however, we will continue where we left off (in step 5) and try an read the firmware settings from the XBee module, by pressing the Read button in the Modem Configurations tab on the X-CTU software.
      • It should look something like this:

      TO BE CONTINUED.......