Friday 29 March 2013

Storage Options day-night IP camera


Following on from this thread:- http://arduino.cc/forum/index.php/topic,53498.0.html I bought a storage options day-night IP camera. Excellent value. Lots of functionality see:- http://www.storageoptions.com/products/ip-cameras/indoor/ip-camera Including motion alarm and night vision!

My only criticism is that support a bit poor especially zero support for so called advanced use of the motion sensing alarm output, which I guess is the one some arduino users would want to use. I know I did. However they do say up-front they don't support advanced use. But I though at least they could tell us what each of those terminal on the back are for!

Anyway after contacting Storage Options and getting nothing. I went on and worked it out so I thought to post an example in case anyone else wants a go!.

Arduino sketch below.

See the above drawing of the green I/O screw terminals on the back of IP camera. Wire up according to that and just follow the camera's manual for setup of alarms in the camera software.

Code ----------------------------------------------------------------------------------------------
const int IPalarm = 6;      // the number of the IP camera INPUT pin
const int ledPin = 4;      // the number of the LED pin to attach led with 560 ohm resitor
char flag1 = ' '; // char to store alarm data

void setup () {
 
// setup the arduino pins
  pinMode(IPalarm, INPUT);
  digitalWrite(IPalarm, LOW);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);


}

void loop () {
 
  if (digitalRead (IPalarm) == HIGH) // if IP camera alarm activated set flag1 to 'i'
 
   {
   
     flag1 = 'i';
   }
 
    switch (flag1) { // action according to flag1 (using switch case because I am using lots of different values of flag1 in a bigger project)
// could just use if... else

    case 'i': //IP motion alarm was triggered
      digitalWrite(ledPin, HIGH);
      break;
 
    default:  // No alarm
      digitalWrite(ledPin, LOW);

    }// end of switch case

}

Wednesday 13 March 2013

Free Android Java Code

It can be tricky finding good resources when you are starting out, so I want to recommend the above website  for people studying programming http://www.deitel.com/. Once you register you get access to gigabytes of information, tutorials, code etc.. without even having to buy their books. AWESOME! Thanks Deitel. I am currently studying the Android-Programmers-App-Driven-Developer-ebook. I use the Java forum at Stack Overflow for asking questions. Happy Troll-free surfing ;)

There are probably others out there so I'll post them when I find them. Please feel free to post links to useful sites in the comments. Nice for viewing code outside of IDE: Programmer's Notepad.

Java Thread Example for a Beginner in Android

 /*
 * Simple use of Android Thread
 * Flash the screen between an array of colours at an interval
 *
 */

At first finding out how to work with threads in Android Java was quite tricky, so I've posted this simple example I put together from various tutorials and expert input from Stack Overflow.



Code:------------------------------------------------------------------

package biz.consett.mydraw;

public class AColor // a list of colours you can use in hex format
{

    final static int RED = 0xFFFF0000; // hex alpha_R_G_B
    final static int GREEN = 0xFF00FF00;
    final static int BLUE = 0xFF0000FF;
    final static int PURPLE = 0xFFAA00AA;
}


package biz.consett.mydraw;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

/*
 * Simple use of Android Thread
 * Flash the screen between an array of colours at an interval
 *
 */

public class MainActivity extends Activity
{

    static final int[] COLORS =
    {AColor.RED,  AColor.BLUE, AColor.GREEN};// colour array
    // Colour Red Green Blue
    // Index [0] [1] [2]

    private int currentColor = 0;

    private View MYVIEW = null;
    //boolean whichColor = true;

    final int interval = 100; // 0.6 second

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MYVIEW = (View) findViewById(R.id.my_view);

        MYVIEW.setBackgroundColor(Color.RED);// set initial colour

        new Thread(new Runnable()
        {

            // @Override
            public void run()
            {

                while (true)
                {
                    try
                    {
                        Thread.sleep(interval); // sleep for interval
                      
                    }
                  
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                        updateColor();
                  
                } // end of  while true loop
              
              
            }// end of run

        }).start(); // end of runnable

    } // end of onCreate bundle

    private void updateColor()
    {
      
        runOnUiThread(new Runnable()
        {

            @Override
            public void run()
            {

                if (currentColor > COLORS.length - 1)
                {
                    currentColor = 0;
                  
                }
                MYVIEW.setBackgroundColor(COLORS[currentColor]);
              
                currentColor++;
              
            }// end of run
        });
      
    }
}// -------------END OF MainActivity extends Activity-----------------------


MANIFEST:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="biz.consett.mydraw"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="biz.consett.mydraw.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


LAYOUT:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <View
      
    android:id="@+id/my_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  
    </View>
  
</RelativeLayout>


END CODE:-----------------------------------------