Go Down

Topic: Arduino Controller android app (Read 63096 times) previous topic - next topic

Thanks i was thinking about doing a home automation project (probably just lights) but i havent coded anything for android before so ive been putting it off this will definately help

marque


Howdy Marque,

Any idea why my other pins are high? I can pop a led into any of my unused pins and the led lights up?

Very soon I'm going to buy another etherten and use the first one as a lightswitch that will send the status of the pins through to another etherten connected to my server. Server will use php to write values to a db, then switch relays accordlingly. As I'm not the best coder its a daunting task!


I have no idea why you unused pins are high, i have no experiance with the etherten. I'm an beginning amateur as well, i think a pro or more experianced dev will laugh about my arduino / android code. :D

marque


Hi, I've been playing around with the Arduino code a little. I am using a DF Robot Relay Shield (has 4 relays)and 2 10K thermistors from adafruit to read temperature.
After the code preforms your digitalWrite to operate the relay, the code will do a digitalRead to see what the current state of the relays are and an analogRead to get temperature data. From the android a "9" will turn On all the relays, a "0" will cause a 404 which I changed to a space " " to clear the Line of text "Turning on 1" etc,from the android screen leaving just the Read data. Color coded the relays red for off and green for on.

Looks good!
Quote
Couple of issues with the Android code, I can only send 0 thru 9, if I enter 10 it sends 1, enter 21 it sends 2, etc.

In the exapmle code the arduino looks for one digit after the questionmark, so you need to make a little change in the arduino code to make it look for two....
change in the checkclient() part:
Code: [Select]
else
             {
               char carray[2];
               readString.toCharArray( carray,2,(Iq+1));
               RecievedString = atoi(carray);
               switch (RecievedString) {

to:
Code: [Select]
else
             {
               char carray[2];
               readString.toCharArray( carray,3,(Iq+1));
               RecievedString = atoi(carray);
               switch (RecievedString) {

so it will look for two digit, but you need to send strings with two digits to make it work well (eg. 01, 02, 09, 10, 99)
Quote
Second the android controller keeps running after exit,draining my battery, so when I am done using the app I have to go to manage apps and forcestop the app.

I tink it's the way android is build but it should not drain your battery bacuase it is just some simple code.
Quote
Would like to be able to put in a URL address (i.e. myarduino.mysite.com) so I can use dynamic ip forwarding without having to know the IP address of the arduino.

Perhaps in a next version, thanks for the sugestion!


maidbloke

@marque: Great work!   ;)


Any idea why my other pins are high? I can pop a led into any of my unused pins and the led lights up?


Is it likely to be this reason?
http://www.arduino.cc/en/Reference/digitalWrite
Quote
The pullup resistor is enough to light an LED dimly, so if LEDs appear to work, but very dimly, this is a likely cause.

Marque ,
  Wow , I love the app ...I sent an email to you as well , but i also wanted to say THANK YOU! on the forum. I am now able to use your app to control my home audio system . I tried the Android tablet in my garage + outside and it works great ! I activated more buttons in the code and found it very straight forward . I have other code that I want to integrate as well as some low voltage lighting.
  Keep up the great work and MANY THANKS on a job well done!
Build_it_Bob 

djjoshuad

Are there any plans to enable USB serial communication with an Arduino in this app? 

#21
Jan 18, 2013, 04:07 pm Last Edit: Jan 18, 2013, 04:18 pm by pisayjames Reason: 1
finally.. i found the most important part of my project.. hehe thank you for this post.. uhmm marque can you post or share the codes from you android app? cause i want to see it.. i am really noob to making android app and i want to see hwo the code is constructed if you dont mind.. anyway thank you so much for this post.. i will try it tomorrow hehe

additional question.. uhmm can you tell me which part in your sketch the actions the arduino makes after recieving strings from the app? cause i want to insert ther actions like digitalwrite for my relays to turn on.. thank you

i downloaded the app sir..i downlaoded it on my samsung galaxy tab.. how will i go to settings screen? i dont see other button that goes to settings screen?

RB

SUPERB!!

Thank you Marque!!!

This is just what I needed. I just started on my room-automation project today, so I'm sure I'll come up with some suggestions soon enough :)

I tried rating the app, but unfortunately i don't have a Google+ account as yet -- will get around to that soon!

Thanks,
R

PS - Though I've made some changes and its working great, i have no clue what the following code does. Could you please explain it to me in just a brief overview sentence or two? Thanks!
Code: [Select]
{readString.concat(c);}
         if (c == 'H')
         {
           Serial.println();
           int Is = readString.indexOf("/");
           int Iq = readString.indexOf("?");
           int Ib = readString.indexOf("b");
           int Ic = readString.indexOf("c");
           if(readString.indexOf("?") > 1)
           {
             if (Ib == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ib+1));
               Serial.println(carray);
                valueB = atof(carray);
               Serial.print("B is now: ");
               Serial.println(valueB);
               client.print (content_main_top);
               client.print("B is now: ");
               client.print(valueB);
             }
            else if (Ic == (Iq+1))
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ic+1));
               Serial.println(carray);
                valueC = atof(carray);
               Serial.print("C is now: ");
               Serial.println(valueC);
               client.print (content_main_top);
               client.print("C is now: ");
               client.print(valueC);
             }


marque

#25
Jan 30, 2013, 12:13 am Last Edit: Jan 30, 2013, 12:44 am by marque Reason: 1

finally.. i found the most important part of my project.. hehe thank you for this post.. uhmm marque can you post or share the codes from you android app? cause i want to see it.. i am really noob to making android app and i want to see hwo the code is constructed if you dont mind.. anyway thank you so much for this post.. i will try it tomorrow hehe

additional question.. uhmm can you tell me which part in your sketch the actions the arduino makes after recieving strings from the app? cause i want to insert ther actions like digitalwrite for my relays to turn on.. thank you


The first piece of code looks for the recieved string and send it to the second piece of code where the action takes place.
First piece of code:
Code: [Select]
else
            {
              char carray[2];
              readString.toCharArray( carray,2,(Iq+1));
              RecievedString = atoi(carray);
              switch (RecievedString) {
              case 1: action(1, client);break;          //if found a "1" goto void action and preform action 1
              case 2: action(2, client);break;
              case 3: action(3, client);break;
              case 4: action(4, client);break;
              case 5: action(5, client);break;
              case 6: action(6, client);break;
              default: action(404, client);
              }


Second piece of code:
Code: [Select]
void action(int x, EthernetClient client)           //this is where the actions take place
{  
 if (x == 1)                                          //if found "1" this action takes place
 {client.print (content_main_top);     
  client.println(S1);                               //put your actions for button 1 here, like digitalwrite etc...
  Serial.println(S1);
 }
if (x == 2)                                          //if found "1" this action takes place
 {client.print (content_main_top);
  client.println(S2);                            //put your actions for button 2 here
  Serial.println(S2);
 }

marque


i downloaded the app sir..i downlaoded it on my samsung galaxy tab.. how will i go to settings screen? i dont see other button that goes to settings screen?

You can find the settings in the menu. You do not have a standard menu button as such. What you have is a softkey which can be found in different places depending on which android version you are in. This softkey takes the form of OPTIONS or SETTINGS which can be selected like a menu. IT looks like 3/4 horizontal bars.

I hope that answers your questions and I'm more than happy to help you out again anytime.

marque


SUPERB!!

Thank you Marque!!!

This is just what I needed. I just started on my room-automation project today, so I'm sure I'll come up with some suggestions soon enough :)

I tried rating the app, but unfortunately i don't have a Google+ account as yet -- will get around to that soon!

Thanks,
R

PS - Though I've made some changes and its working great, i have no clue what the following code does. Could you please explain it to me in just a brief overview sentence or two? Thanks!
Code: [Select]
{readString.concat(c);}
         if (c == 'H')
         {
           Serial.println();
           int Is = readString.indexOf("/");
           int Iq = readString.indexOf("?");
           int Ib = readString.indexOf("b");     //arduino looks for a b
           int Ic = readString.indexOf("c");     //arduino looks for a c
           if(readString.indexOf("?") > 1)
           {
             if (Ib == (Iq+1))                          // if there is a b
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ib+1));     // read the value after the "b"
               Serial.println(carray);
                valueB = atof(carray);                               //set the found value to variable "valueB"
               Serial.print("B is now: ");
               Serial.println(valueB);                                  //print the found value
               client.print (content_main_top);
               client.print("B is now: ");
               client.print(valueB);
             }
            else if (Ic == (Iq+1))                                             // the same for "c"
             {
               char carray[5];
               readString.toCharArray( carray,5,(Ic+1));
               Serial.println(carray);
                valueC = atof(carray);
               Serial.print("C is now: ");
               Serial.println(valueC);
               client.print (content_main_top);
               client.print("C is now: ");
               client.print(valueC);
             }



in thise piece of code you can set two variable with you android app.  In the settings screen you can set a prefix in the two textboxes top right. in the first one put a "b" and in the second one put a "c". on the main screen you can put a value in the two text views top left and hit the send button. the app will send a HTTP GET request in the following format: "http://yourip:yourport/?prefixvalue" , "http://192.168.1.120:80/?b2800". where the "b" is the prefix you have set in the settingsscreen and 2800 is the value you set in the main screen. The arduino sketch will look for a "b" or a "c" and sets the variable to the value that follows.
I have commented the code above, hope it clears it up for you.

#28
Jan 30, 2013, 12:48 am Last Edit: Jan 30, 2013, 12:55 am by pisayjames Reason: 1
@marque

uhmm i send you a personal message.. hope you can help me with it.. thank you so much... anyways if you have not received it, i was asking for your sketch/code in the android app.. because i want to modify some parts from it because my professor ask me for some changes in the app for my project study.. im still new with android programming but your app will be a good start for me to work with.. hope you can help me..

RB

#29
Jan 30, 2013, 08:58 am Last Edit: Jan 30, 2013, 09:03 am by RB Reason: 1

in thise piece of code you can set two variable with you android app.


Thanks for the explanation! The app works superbly!!! THANKS AGAIN :)

I've set it to control a servo (which will be connected to a dimmer). I'm sharing the code below just in case anyone else wishes to control a servo with the app. The relays/switches part of the code below is still incomplete, but the servo part works perfectly. I've also applied the code change to allow 2-digit codes, so make sure they are all 01, 02, 03 in your android app settings.

Currently you have 2 buttons "dimmer down" (04) and "dimmer up" (14) that you can use to move the servo by a pre-defined angle. It also disconnects the servo after 1 second of no command, to avoid unnecessary current draw/vibrations, and also let it be freely moveable to allow manual control of the dimmer.

I've not changed anything in the checkClient() function.

Next, i'll be adding an IR control :)

Code: [Select]
/************ Marque's Arduino Controller Example ************/
/***********  for questions: marque.l@gmail.com  ***********/
/*
http://arduino.cc/forum/index.php?topic=127770.0

In the android app settingsscreen you can set up you IP and port.
For this example you need to call prefix 1 "B" and prefix 2 "C".
*/
#include <Ethernet.h>
#include <HTTPClient.h>
#include <SPI.h>
#include <Servo.h> // servo library added

//MARQUESOFT's CODE
int RecievedString;
int valueB;
int valueC;
String readString = String(20);

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x9A, 0x4D };
EthernetServer server(80);   // port to listen on
EthernetClient client;

char content_main_top[] = "<body bgcolor=black><font color=white><center>";
char S1[] = "Globe OFF" ;
char S2[] = "Couch Lights OFF" ;
char S3[] = "Loft Lights OFF";
char S4[] = "Dim Globe";
char S5[] = "05";
char S6[] = "06";
char S11[] = "Globe ON" ;
char S12[] = "Couch Lights ON" ;
char S13[] = "Loft Lights ON";
char S14[] = "Brighten Globe";
char S15[] = "15";
char S16[] = "16";
char S404[] = "Not Found";


//DIMMER SETUP
Servo dimmerServo;  // create a servo object
int servoPin = 9;
int dimmerMax = 170; // the furthest the servo+dimmer goes anti-clockwise
int dimmerMin = 0; // the furthest the servo+dimmer goes clockwise
int dimmerIncrement = 10; // the step size for each button press
int dimmerVal = 0;
int dimmerValueShowPhone = dimmerMax - dimmerVal;
boolean detachServo = false; // note if the servo needs to be detached
long detachServoRequestTime = 0; //the millis at which detach servo was last requested

//RELAY SETUP



/************************** Setup **********************/
void setup()
{
 Serial.begin(9600);
 Serial.println("Getting IP......");
 Ethernet.begin(mac);
 Serial.print("My IP address: ");
 Ethernet.localIP().printTo(Serial);
 Serial.println();
 Serial.print("Gateway IP address is ");
 Ethernet.gatewayIP().printTo(Serial);
 Serial.println();
 Serial.print("DNS IP address is ");
 Ethernet.dnsServerIP().printTo(Serial);
 Serial.println();
}
void loop()
{
 checkclient();

 if (detachServo == true) { detachServoAfterDelay(); }
}

void checkclient()
{
 EthernetClient client = server.available();
 if (client)  
 {
   boolean sentHeader = false;
   while (client.connected())
   {
     if (client.available())
     {
       if(!sentHeader){
       client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();
         sentHeader = true;
       }
        char c = client.read();
        if (readString.length() < 20)
        Serial.print(c); //For debuggin in Serial Monitor window
        {readString.concat(c);}
        if (c == 'H')
        {
          Serial.println();
          int Is = readString.indexOf("/");
          int Iq = readString.indexOf("?");
          int Ib = readString.indexOf("b");
          int Ic = readString.indexOf("c");
          if(readString.indexOf("?") > 1)
          {
            if (Ib == (Iq+1))
            {
              char carray[5];
              readString.toCharArray( carray,5,(Ib+1));
              Serial.println(carray);
               valueB = atof(carray);
              Serial.print("B is now: ");
              Serial.println(valueB);
              client.print (content_main_top);
              client.print("B is now: ");
              client.print(valueB);
            }
           else if (Ic == (Iq+1))
            {
              char carray[5];
              readString.toCharArray( carray,5,(Ic+1));
              Serial.println(carray);
               valueC = atof(carray);
              Serial.print("C is now: ");
              Serial.println(valueC);
              client.print (content_main_top);
              client.print("C is now: ");
              client.print(valueC);
            }
            else
            {
              char carray[2];
              readString.toCharArray( carray,3,(Iq+1));
              RecievedString = atoi(carray);
              switch (RecievedString) {
              case 1: action(1, client);break;
              case 2: action(2, client);break;
              case 3: action(3, client);break;
              case 4: action(4, client);break;
              case 5: action(5, client);break;
              case 6: action(6, client);break;
              case 11: action(11, client);break;
              case 12: action(12, client);break;
              case 13: action(13, client);break;
              case 14: action(14, client);break;
              case 15: action(15, client);break;
              case 16: action(16, client);break;              
              default: action(404, client);
              }
            }
         delay(1);
            client.stop();
            readString="";
            client.read(); client.read();
          }
        if (Iq < 0)
        {
        action(404, client);
        delay(1);
        client.stop();
        readString="";
        client.read(); client.read();
        }
         delay(1);
        client.stop();
        readString="";
        client.read(); client.read();
      }
    }
  }
}
}

void action(int x, EthernetClient client)
{  
 if (x == 1)
 {client.print (content_main_top);
  client.println(S1);
  Serial.println(S1);
 }
if (x == 2)
 {client.print (content_main_top);
  client.println(S2);
  Serial.println(S2);
 }
if (x == 3)
 {client.print (content_main_top);
  client.println(S3);
  Serial.println(S3);
 }
if (x == 4) // Dimmer Globe Down
 {client.print (content_main_top);
  client.println(S4);
  Serial.println(S4);
  dimmerDown();
  client.println(dimmerValueShowPhone);
 }
if (x == 5)
 {client.print (content_main_top);
  client.println(S5);
  Serial.println(S5);
 }
if (x == 6)
 {client.print (content_main_top);
  client.println(S6);
  Serial.println(S6);
 }
if (x == 11)
 {client.print (content_main_top);
  client.println(S11);
  Serial.println(S11);
 }
if (x == 12)
 {client.print (content_main_top);
  client.println(S12);
  Serial.println(S12);
 }
if (x == 13)
 {client.print (content_main_top);
  client.println(S13);
  Serial.println(S13);
 }
if (x == 14)
 {client.print (content_main_top);
  client.println(S14);
  Serial.println(S14);
  dimmerUp();
  client.println(dimmerValueShowPhone);  
 }
if (x == 15)
 {client.print (content_main_top);
  client.println(S15);
  Serial.println(S15);
 }
if (x == 16)
 {client.print (content_main_top);
  client.println(S16);
  Serial.println(S16);
 }
if (x == 404)
 {client.print (content_main_top);
  client.println(S404);
  Serial.println(S404);
 }
 
x=0;
}


void dimmerUp() {
 if (detachServo == false) { dimmerServo.attach(servoPin); } // only re-attach if its already been detached
 dimmerVal = dimmerVal - dimmerIncrement;
 if (dimmerVal < dimmerMin) { dimmerVal = dimmerMin; } // making sure it doesnt cross the minimum
 dimmerServo.write(dimmerVal);
 delay(10);   // wait for the servo to get there
 detachServo = true; // flags it to be detached after a few seconds
 detachServoRequestTime = millis();  
 dimmerValueShowPhone = dimmerMax - dimmerVal;  
}

void dimmerDown() {
 if (detachServo == false) { dimmerServo.attach(servoPin); } // only re-attach if its already been detached
 dimmerVal = dimmerVal + dimmerIncrement;
 if (dimmerVal > dimmerMax) { dimmerVal = dimmerMax; }  // making sure it doesnt cross the maximum
 dimmerServo.write(dimmerVal);
 delay(10);   // wait for the servo to get there
 detachServo = true; // flags it to be detached after a few seconds
 detachServoRequestTime = millis();
 dimmerValueShowPhone = dimmerMax - dimmerVal;
}

void detachServoAfterDelay() {   // timed detach for servo after dimmer up or dimmer down to prevent heat / fatigue / vibrations for the servo
 unsigned long currentMillis = millis();  
 if ( (currentMillis - detachServoRequestTime) > 1000 ) //time has passed
 {
   dimmerServo.detach();
   detachServo = false;
 }
}

Go Up