Shop OBEX P1 Docs P2 Docs Learn Events
Android communication with Spinneret — Parallax Forums

Android communication with Spinneret

BeanBean Posts: 8,129
edited 2011-08-31 20:45 in Accessories
I'm teaching myself android programming. And I'll let you know, it's not easy.

My initial goal is to write a program to read the accelerometer and transmit the data to the spinneret. And later to the Parallax WiFi module (hint hint).

I'm wondering if anyone has any other ideas for simple (easy) android apps ?

Bean

Comments

  • bsnutbsnut Posts: 521
    edited 2011-08-04 21:09
    I have an idea for you. You can use it for remote monitoring systems, like the one I am doing for the Spinneret contest and of course I didn't submit it on time. This type of thing is perfect for the Android phones.
    If you like to use my application, I will have the code for the system posted here in the forums by the middle of next week and will be happy to work with you if you use my application.
  • BeanBean Posts: 8,129
    edited 2011-08-05 04:52
    William,
    That sounds cool, but it's quite a bit beyond my skills at the moment.

    Right now I'm pretty much taking working code I find and tweaking it.

    P.S. If anyone wants to get started in android programming cheap, I got the Archos 28 for about $80. There is no camera or SD, but I can use if for development without having to root it or anything.

    Bean
  • BeanBean Posts: 8,129
    edited 2011-08-06 11:31
    I wonder if someone would be so kind as to try this Android application and let me know if it works.

    This is my first android app, so it may not even work. But it works on my archos 28 tablet. The zip file contains a .apk file.

    You will need to have android 2.2 or higher.

    PLEASE let me know if you try this. And let me know if it works or not.

    You should get a screen showing the accelerometer readings X, Y and Z. It will send this data to my spinneret (so you will need to have internet access) my spinneret is connected to my PC to log the data. I'll leave my spinneret running for the rest of today.

    Thanks

    Bean
  • TorTor Posts: 2,010
    edited 2011-08-06 12:59
    I'll give it a go in just a moment..

    -Tor
  • TorTor Posts: 2,010
    edited 2011-08-06 13:09
    It seems to be doing what it says, it changed the numbers correctly as I moved the phone around. It also said 'Connected', until the screen saver cut in.. after that it now says 'IO Exception' but the x/y/z numbers continue running.

    I stopped and restarted the application. It still says 'IO Exception'.

    It also runs only in landscape mode, presumably that's intentional.

    Stopping it now.

    -Tor
  • BeanBean Posts: 8,129
    edited 2011-08-06 13:17
    Tor,
    Thanks for trying it. It did log your data at my computer (Yahoo!!!).
    What happened was the spinneret was still connected, I guess the screen-saver stopped the program, but not the same way as if you exit it. I'll have to look at how to fix that.
    My next step it allow you to enter your own IP address of your own spinneret.

    Thanks again. I have reset the spinneret if you want to try it again.

    P.S. Yes I made it stay in landscape because there would be a delay as it changed between landscape and portrait modes.

    Bean
  • RonPRonP Posts: 384
    edited 2011-08-06 14:22
    Bean,

    I gave it a go it worked well with my G2. I was trying to get (x=0 y=0 and z=100) couldn't get z to 100 you need very steady hands. Works very well It said it was connected the whole time it was on.

    Ron
  • BeanBean Posts: 8,129
    edited 2011-08-06 14:27
    Thanks Ron,
    I'm modifying it so that it shows the values, then has an input line and button to allow you to transmit the values to a spinneret.
    Once I'm sure it works well I'll release it for everyone to use.
    It will be REALLY useful when the Parallax WiFi comes out. Hint...Hint...

    I also modified the spinneret code so it drop the connection if there is no activity for 10 seconds.

    Bean
  • Mike GMike G Posts: 2,702
    edited 2011-08-06 18:19
    I'm interested in getting into Android programming.
    Bean wrote:
    I got the Archos 28 for about $80. There is no camera or SD, but I can use if for development without having to root it or anything.

    What does "root it" mean? See the root directory or command line access?
  • RonPRonP Posts: 384
    edited 2011-08-06 19:05
    Mike,

    Here you go.

    Ron
  • BeanBean Posts: 8,129
    edited 2011-08-07 06:04
    Here is the new android program (be sure to uninstall the older version).

    The zip file includes the android 2.2 program that reads the accelerometer and lets you send the data to any Spinneret that is setup to receive Telnet (Port 23) data.

    It also includes a "Telnet to Serial" program for the Spinneret (be sure to change the IP address, Gateway, etc).

    You can try to send data to my spinneret by specifying "pebasic.com" as the domain.

    Let me know if you try it.

    attachment.php?attachmentid=83798&d=1312722677

    Bean
    320 x 240 - 14K
  • Don MDon M Posts: 1,647
    edited 2011-08-07 07:16
    Bean- I just installed your app on my Galaxy Tab. Seemed to work ok. I transmitted the data to your domain. Did you see it?

    Any chance of seeing your Android code?

    Don
  • BeanBean Posts: 8,129
    edited 2011-08-07 07:45
    Don,
    Could you try to send it again. I didn't have PST running.
    I'll really have to clean up the code before I'd show it to anyone. It's really a mess because I don't know what I'm doing. ;)

    Bean
  • Don MDon M Posts: 1,647
    edited 2011-08-07 08:29
    I installed it on my Samsung Acclaim phone and it seemed to work ok on the phone. I connected to your server.
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2011-08-07 19:25
    Bean,
    What are you using to program the android in? is there a program you recommend?
  • BeanBean Posts: 8,129
    edited 2011-08-08 04:53
    Brian, I'm just using the free tools:

    Java development kit (JDK)
    Android Software Development Kit (Android SDK)
    Eclipse

    Bean
  • BeanBean Posts: 8,129
    edited 2011-08-08 08:43
    Don,
    Here is the code, it is probably not "proper" by android standards. In fact if anyone is familiar with android programming, I'd love to hear what I've done incorrectly.
    package com.pebasic.accel1;
    
    
    import com.pebasic.accel1.R;
    
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    import android.app.Activity;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.widget.ToggleButton;
    import android.view.View;
    
    
    public class Accel1Activity extends Activity implements SensorEventListener {
     
     // Accelerometer X, Y, and Z values
     private TextView accelXValue;
     private TextView accelYValue;
     private TextView accelZValue;
     
     private TextView ipInput;
     private ToggleButton transmitStopBtn;
     private TextView statusView;
     private Boolean socketGood = false;
     private Socket socket;
     private PrintWriter out;
     
     private SensorManager sensorManager = null;
     
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Get a reference to a SensorManager
            sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            setContentView(R.layout.main);
           
            // Capture accelerometer related view elements
            accelXValue = (TextView) findViewById(R.id.accel_x_value);
            accelYValue = (TextView) findViewById(R.id.accel_y_value);
            accelZValue = (TextView) findViewById(R.id.accel_z_value);
           
            statusView = (TextView) findViewById(R.id.messaage);
            ipInput = (TextView) findViewById(R.id.txtIPAddress);
            transmitStopBtn = (ToggleButton)findViewById(R.id.btnStartStop);
            transmitStopBtn.setOnClickListener(new View.OnClickListener() {
            	public void onClick(View v) {
            		if (transmitStopBtn.isChecked()) {
            			statusView.setText("Start transmission");
            	        // Connect to Spinneret
            	        socketGood = false;
            	        try {
            	        	statusView.setText("Address");
            	        	InetAddress serverAddr = InetAddress.getByName(ipInput.getText().toString());
            	        	statusView.setText("Socket 23");
            	        	socket = new Socket(serverAddr, 23);
            	        	statusView.setText("Got socket");
            	        	try {
            	        		out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
            	        		statusView.setText("Transmitting...");
            	        		socketGood = true;
            	        	} catch (Exception e) {
            	        		statusView.setText("Exception");
            	        	}
            	        } catch (UnknownHostException e) {
            	        	statusView.setText("Unknown Host");
            	        } catch (IOException e) {
            	        	statusView.setText("IO Exception");
            	        } finally {
            			    if (socketGood == false) {
            				    transmitStopBtn.setChecked(false);
            			    }
            	        }    
            		} else {
            			socketGood = false;
            			statusView.setText("Transmission Stopped");
                		try {
                			socket.close();
                		} catch (IOException e) {
                		}
                	} // if transmitStopBtn.isChecked
            	}
            });
            
            // Initialize accelerometer related view elements
            accelXValue.setText("0.00");
            accelYValue.setText("0.00");
            accelZValue.setText("0.00");
        }
       
        // This method will update the UI on new sensor events
        public void onSensorChanged(SensorEvent sensorEvent) {
         synchronized (this) {
          if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
           accelXValue.setText(Integer.toString((int)(sensorEvent.values[0] * 10.0)));
           accelYValue.setText(Integer.toString((int)(sensorEvent.values[1] * 10.0)));
           accelZValue.setText(Integer.toString((int)(sensorEvent.values[2] * 10.0)));
           String message = Integer.toString((int)(sensorEvent.values[0] * 10.0))+","+
             Integer.toString((int)(sensorEvent.values[1] * 10.0))+","+
             Integer.toString((int)(sensorEvent.values[2] * 10.0))+"\r";
           if (socketGood) {
    	     out.println(message);
    		 if (out.checkError()) {
    		  statusView.setText("Exception Error");
    		  socketGood = false;
    		  transmitStopBtn.setChecked(false);
         	  }//if checkError
           } //if socketGood
          } //if sensorEvent
          
          //if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
          // orientXValue.setText(Integer.toString((int)sensorEvent.values[0]));
          // orientYValue.setText(Integer.toString((int)sensorEvent.values[1]));
          // orientZValue.setText(Integer.toString((int)sensorEvent.values[2]));       
          //}
         }
        }
        // I've chosen to not implement this method
        public void onAccuracyChanged(Sensor arg0, int arg1) {
      // TODO Auto-generated method stub
     }
        
       @Override
       protected void onPause() {
    	   if (socketGood) {
    		   socketGood=false;
    		   transmitStopBtn.setChecked(false);
     	       statusView.setText("Transmission Stopped");
        	   try {
        			socket.close();
        		} catch (IOException e) {
        		}
    	   }
    	   super.onPause();
       }
       
        @Override
        protected void onResume() {
         super.onResume();
         // Register this class as a listener for the accelerometer sensor
         sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
         // ...and the orientation sensor
         sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
        }
       
        @Override
        protected void onStop() {
           // Unregister the listener
        	if (socketGood) {
        		try {
        			socket.close();
        		} catch (IOException e) {
        		}
        	} // if socketGood
         sensorManager.unregisterListener(this);
         super.onStop();
        } 
     }
    
    

    Bean
  • BeanBean Posts: 8,129
    edited 2011-08-08 10:19
    Just a note...I changed my spinneret back to PE-Basic so the contest judges can test it.
    Please DON'T send data to pebasic.com.

    Bean
  • bsnutbsnut Posts: 521
    edited 2011-08-08 20:31
    Bean,
    Looks pretty cool for what you have done with the phone app. The free tools that you found gives me the idea to start programming my own Android phone app for my monitoring system. Do you have the links for the free tools that you are using?
  • Don MDon M Posts: 1,647
    edited 2011-08-09 08:06
    Thanks Bean.
  • Bob Lawrence (VE1RLL)Bob Lawrence (VE1RLL) Posts: 1,720
    edited 2011-08-24 11:48
    @bsnut


    I pasted in some of my notes that may help you. I will add more later)


    General Info - Android Programming:

    http://developer.android.com

    You can setup either MS Windows, Linux or Mac to developed for Android. I have it setup on Linux and MS Windows 7.0. If you don't already have these applications (1 to 4) installed start at number 1, they are in sequence . It includes the required (for the Eclipse IDE) Java Runtime Environment (JRE) required to run the Eclipse IDE.

    =====================================================================================
    1. Java JDK(software developers Kit). Install this first and confirm that it's working

    http://www.oracle.com/technetwork/java/javase/downloads/index.html


    Tips:


    MS Windows (tested on Win 7.0, make the required changes for other versions) Go to Start "All Programs" , "Accessories", "Command Prompt" . At the command prompt type: ” javac -version “. If the JDK is installed and working correctly it should report the current version.

    Debian- derived Linux: Use the terminal program. If you have the wrong version try this command : sudo update-alternatives --config java
    Mac: Use the terminal program
    =====================================================================================
    2. Eclipse IDE. Written in Java and requires on the JRE to operate. For Android It's used to write java code, test, debug etc.
    http://www.eclipse.org/downloads/

    Tips: Linux: Copy Eclipse to your home folder manually, do not use a package manager to install it or you may end up with a much older version.
    =====================================================================================

    3. Android SDK(software development kit)

    http://developer.android.com/sdk/index.html
    Tips: Add the folders “tools” and “platform-tools” to your path

    Linux: Extract and Install the Android SKD to your home folder. Add the folders tools and platform-tools to the path environment variable. Use .profile command from a term program (I use Ubuntu)

    Mac: Extract and Install the Android SKD to your home folder. Add the folders tools and platform-tools to the path environment variable. Use .profile command from a term program (I use Ubuntu)



    =====================================================================================

    4. ADT plug-in

    Choose classic or Java developer package(EE is what I use, you could use standard)

    AndroidTestVD.png
    461 x 395 - 96K
  • bsnutbsnut Posts: 521
    edited 2011-08-28 23:38
    Thanks Bob for posting that for us. I will give it try a on my Windows XP machine later on.
  • Oldbitcollector (Jeff)Oldbitcollector (Jeff) Posts: 8,091
    edited 2011-08-31 20:45
    Late to the party as usual.. Thought I'd launch out into something new, and long behold Bean was already here. :)

    Great work Terry! I'm looking forward into delving into this as well.

    OBC
Sign In or Register to comment.