Go Down

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

marque

Oct 18, 2012, 12:35 am Last Edit: Aug 20, 2013, 10:38 pm by marque Reason: 1
UPDATE!

• Now it's possible to import, export and share the profiles
• In program mode it's easy to set up the profiles
• In program mode Click button to edit the button text
• Long click in program mode to edit the datastring send to the arduino
• In the default import folder there are two example profiles to use with the example arduino code
• There is a new example arduino code in the openings post
• Reset function added to remove all settings and start from scratch
• Option added to send the seekbar values on every change for quick response
• More to come!

Arduino controller Pro is now supported from Android 2.2 to 4.3!




*************************************************************************************************************************************


Now there is a android application to control all your arduino projects from you phone, all around the world.

There are three version for this application:
Arduino Controller Pro
Arduino Controller Lite
Arduino Controller Pro, Free with ads

You can use this as an universal remote for all you arduino projects, like alarm systems, home automation, HVAC installations and much more.

I hope you can use this app in you arduino project and please let me know what you use it for or encounter problems.
If you have recommendations, found any bugs, or need extra buttons, please let me know as well.
When you like it please give it a rating on google play.

 


Arduino Controller Pro.
Arduino Controller Pro, Free
And the example sketch on dropbox
Share your profiles and sketches in this topic


Pro benefits:
• Eight different tabs to control eight different Arduino 's
• Program mode to easily edit the layout and settings 
• Sliding seekbars to control RGB led 's, servos, etc.
• Import, export and share your profiles
• Examples to get you started very quick
• URL and IP support 
• 32 buttons on each profile
• Buttons will hide when you don't use them
• Two different themes 
• And more..

***************************************Tutorial*******************************************

First of all you need a arduino with ethernet shield connected to your router. A wifi connection is possible as well but is not supported by te example sketch.
Download the example sketch  and upload it to the arduino.
Install the Arduino Controller application on your android device. When you start the Arduino Controller application on your Android device for the first time you just see two buttons and a small browser webview showing some kind of error. This is because there is no connection to your arduino yet.
Hit your "menu" softbutton and select "settings"
   

In the settings screen you fill in the IP of your Arduino divice. You can find the IP in the serial monitor when you run the example sketch on the arduino. Fill in the port number, this is port 80 by default.
The aplication name (only in the Pro version) is the name you like for your project like "arduino alarm" or "heat pump".
Fill in the prefix used to send variables to the arduino. Prefix 1 = "b", Prefix 2 = "c" for the example sketch.
Now it is time assign the strings and text labels to some buttons.
String 01 = "1"      Text 01 = "1 on"
String 02 = "2"      Text 02 = "1 off"
String 03 = "3"      Text 03 = "2 on"
String 04 = "4"      Text 04 = "2 off"
Now you can save your settings.


Now your Arduino controller is all set!


Permissions:
android.permission.ACCESS_NETWORK_STATE
used to detect Wifi or mobile connection
android.permission.INTERNET
Guess :-)
android.permission.READ_EXTERNAL_STORAGE
Used to import profiles from SD card
android.permission.WRITE_EXTERNAL_STORAGE
Used to export profiles to SD card



***************************************Changelog*******************************************
V 2.1.5
Arduino controller Pro is now supported from Android 2.2 to 4.3!

V 2.1.0
• Now it's possible to import, export and share the profiles
• In program mode it's easy to set up the profiles
• In program mode Click button to edit the button text
• Long click in program mode to edit the datastring send to the arduino
• In the default import folder there are two example profiles to use with the example arduino code
• There is a new example arduino code in the openings post
• Reset function added to remove all settings and start from scratch
• Option added to send the seekbar values on every change for quick response
• More to come!

Two permissions added:
android.permission.READ_EXTERNAL_STORAGE
Used to import profiles from SD card
android.permission.WRITE_EXTERNAL_STORAGE
Used to export profiles to SD card

V 2.0.13
Three seekbars added which send values from 0 to 255. With these seekbars it is easier to control RGB leds, motors and servo's.

V 2.0.11
Restructured the settings menu
Corrected some typo's

V 2.0.8
Now control up to 8 arduino projects with this application
Fixed some bugs

V 2.0.5
Enable or disable the profiles in settings
Fixed crash on keyboard "Next"
Now its possible to send values with the keyboard very quick
Fixed crash after resume
Fixed wrong view on tab select

V 2.0.3
Fixed some bugs that caused crashes. Thanks for all to feedback, it helps me a lot!

V 2.0.1
Changes made to this version:
- complete new layout
- three different profiles
- Updated example sketch
- example layout on profile 3 set up for the arduino example sketch
- password protected login
   
V 1.0.10
Changes made to this version:
-The size of the webview automatically adjusts to the content
-Improved use of resources and battery usage
-URL and IP support  
-Custom activity name
-More buttons (32!)
-Buttons are hidden when you don't use them
-Strings can contain letters and numbers




pwillard

On the main page there is a large banner across the top that links to a missing web page.  So I call this a broken solution and a bad idea.

marque

Haha, this is no ad!! This a webview where the response of you arduino webserver show up. This is don so the arduino code is as simple as possible. If you try the example sketsh and set you ip, port and some buttons you will see there are no ads. just try.

tochinet

But is there an Android app to _upload_ a sketch to an Arduino ? In understand that a complete port of the IDE or avr-gcc is a bit complex, but while I could find an STK500 port for ... Palm, nothing for Android. How comes ?

grim

Awesome! thanks Marque for posting this.  I don't know if I would have been able to figure this out on my own, especially the Android app (which does NOT have ads).

SamuelFreeman

#7
Nov 12, 2012, 01:48 pm Last Edit: Nov 12, 2012, 04:06 pm by SamuelFreeman Reason: 1
Hi Marque,

Wonderful combo you have written here, has become very handy to me. I would love to know how you went about coding the android part. Anyway I showed my code mods and what not here http://arduino.cc/forum/index.php/topic,132021.0.html

I'd like to control my relay setup with buttons also, would you have any tips for integrating this?

Thank you again!

marque

#8
Nov 15, 2012, 07:22 pm Last Edit: Nov 15, 2012, 07:25 pm by marque Reason: 1
@ grim - Thanks for your comment! Glad i could help.

@ SamuelFreeman - Coding for arduino is easy when compared to android! It took me days to get it working. For now i made two apps, one to control a arduino connected to my heating system (thermoduino) and this universal arduino controller. I use arduino controller to controll my somfy shades for wich i connected a 8 channel relay bord to a arduino and it works great! I have plans to make a dedicated android app to control my somfy shades.

Here is the code to control 6 relay's:

edit - code was to big??? so it is in the next post.

marque

Code: [Select]
/*********************** Marque's Somfy Controler *****************************/
/*
 Hardware used:
 Arduino uno
 Ethernet Shield W5100
 8 channel 5v relay
 
 */
#include <SPI.h>
#include <Ethernet.h>
#include <HTTPClient.h>

#define DEBUG1 1  //debug info

char AA =  3;     // pin connected to relay 1
char BB =  4;     // pin connected to relay 1
char CC =  5;     // pin connected to relay 1
char DD =  6;     // pin connected to relay 1
char EE =  7;     // pin connected to relay 1
char FF =  8;     // pin connected to relay 1
char II =  0;     // pin connected to relay 1


long intervalNetworkCheck = 1500;
long previousMillis = 0;
unsigned long currentMillis;
boolean networkonline = false;
boolean DNS = false;

String sketchname = String(30);             //skech name

String readString = String(30);

float set;
float Smin;
float Smax;
float Set;
int Ic;

byte mac[] = {
 0xDE, 0xBD, 0xBE, 0xBF, 0xFE, 0xED };
IPAddress ip(192,168,1,69);
EthernetServer server(80);
EthernetClient client;
IPAddress Google1    (74,125,228,32 );  
//IPAddress Google2    ( 173,194,75,94);  
IPAddress ServerToCheck = Google1;             // set the name of the server to check
char dnsServerToCheck[] = "google.com";             // set the name of the server to check

char content_main_top[] = "<body bgcolor=black><font color=white><center>";
char aa[] = "A omhoog";
char bb[] = "A stop";
char cc[] = "A omlaag";
char dd[] = "B omhoog";
char ee[] = "B stop";
char ff[] = "B omlaag";
char ii[] = "info";


int pgm_lastIndexOf(uint8_t c, const char * p)
{
 int last_index = -1; // -1 indicates no match
 uint8_t b;
 for(int i = 0; true; i++) {
   b = pgm_read_byte(p++);
   if (b == c)
     last_index = i;
   else if (b == 0) break;
 }
 return last_index;
}

// displays at startup the Sketch running in the Arduino

void display_srcfile_details(void){
 const char *the_path = PSTR(__FILE__);           // save RAM, use flash to hold __FILE__ instead

 int slash_loc = pgm_lastIndexOf('/',the_path); // index of last '/'
 if (slash_loc < 0) slash_loc = pgm_lastIndexOf('\\',the_path); // or last '\' (windows, ugh)

 int dot_loc = pgm_lastIndexOf('.',the_path);   // index of last '.'
 if (dot_loc < 0) dot_loc = pgm_lastIndexOf(0,the_path); // if no dot, return end of string

if (DEBUG1) Serial.print("\nSketch: ");

 for (int i = slash_loc+1; i < dot_loc; i++) {
   uint8_t b = pgm_read_byte(&the_path[i]);
   if (b != 0)
   {
     sketchname.concat((char) b);
   }
   else break;
 }
if (DEBUG1) Serial.print(sketchname);

if (DEBUG1) Serial.print(", Compiled on: ");
if (DEBUG1) Serial.print(__DATE__);
if (DEBUG1) Serial.print(" at ");
if (DEBUG1) Serial.print(__TIME__);
if (DEBUG1) Serial.print("\n");
}

/* start ethernet */
void init_eth()
{
if (DEBUG1) Serial.println("Ethernet start");
 Ethernet.begin(mac);
 server.begin();  
if (DEBUG1) Serial.print("IP address is ");
if (DEBUG1) Serial.println(Ethernet.localIP());
if (DEBUG1) Serial.print("Gateway IP address is ");
if (DEBUG1) Serial.println(Ethernet.gatewayIP());
if (DEBUG1) Serial.print("DNS IP address is ");
if (DEBUG1) Serial.println(Ethernet.dnsServerIP());
}

/***  Network check ***/
void networkcheck()
{

if (DEBUG1) Serial.println ("Network check");
 if (client.connect(dnsServerToCheck, 80))
 {
  delay(300);
 if (DEBUG1) Serial.print("connected to ");
 if (DEBUG1) Serial.println(dnsServerToCheck);
  client.println("GET / HTTP/1.1");
  client.println();
  while (client.connected())
  {
   if (client.available())
   {
    char c = client.read();
    if (readString.length() < 20)
   if (DEBUG1) Serial.print(c);
    {
      readString.concat(c);
    }
    if (c == '\n')
    {
      client.stop();
     if (DEBUG1) Serial.println();
     if (DEBUG1) Serial.println("DNS OK");
      DNS = true;
      networkonline = true;
      readString="";
    }
  }
  if (!client.connected())
  {
    client.stop();
    readString="";
  }
}
}
if (!DNS)
{
if (DEBUG1) Serial.println("DNS Error");
if (DEBUG1) Serial.println("Check for IP");
 if (client.connect(ServerToCheck, 80))
 {
  delay(300);
 if (DEBUG1) Serial.print("connected to ");
 if (DEBUG1) Serial.println(ServerToCheck);
  client.println("GET / HTTP/1.1");
  client.println();
  while (client.connected())
  {
   if (client.available())
   {
    char c = client.read();
    if (readString.length() < 20)
   if (DEBUG1) Serial.print(c);
    {
      readString.concat(c);
    }
    if (c == '\n')
    {
      client.stop();
     if (DEBUG1) Serial.println();
     if (DEBUG1) Serial.println("IP lookup OK");
     if (DEBUG1) Serial.println("DNS not available");
      networkonline = true;
      readString="";
    }
  }
  if (!client.connected())
  {
   if (DEBUG1) Serial.println("Disconnecting");
    client.stop();
    readString="";
  }
}
}
else
{
 if (DEBUG1) Serial.println("Network Error");
  networkonline = false;
}
}
}

/******** Setup *******************/

void setup()
{
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
// pinMode(Heater, OUTPUT);
 //pinMode(Buzzer, OUTPUT);

//  pinMode (Led, OUTPUT);
//  pinMode (53, OUTPUT);
 Serial.begin(9600);
 display_srcfile_details();

 init_eth();
// init_avviso();
 networkcheck();
 if (DEBUG1) Serial.println("Setup complete");
 if (DEBUG1) Serial.println("");
}

/****************************** Loop *****************************/

void loop()
{
 run_eth();
 
/****Every x seconds check network conection***/
if (!networkonline)
{
 currentMillis = millis();
 if(currentMillis - previousMillis > intervalNetworkCheck)
 {
   networkcheck();
 }
}
}
/** Check fot Ethernet Client ***/
void run_eth()
{
 EthernetClient client = server.available();
 if (client)
 {
   boolean sentHeader = false;
   boolean currentLineIsBlank = true;
   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 (DEBUG1) Serial.write(c);

        if (int Ih = readString.indexOf("H"));
          if(readString.indexOf("H") < 1)
      {
        readString.concat(c);
      }
       if (c == '\n' && currentLineIsBlank) {
         if (DEBUG1) Serial.println("end");
         if (DEBUG1) Serial.println(readString);
          int Is = readString.indexOf("/");
          int Iq = readString.indexOf("?");
          if(readString.indexOf("?") > 1)
          {
           if (DEBUG1) Serial.println("? found");
            int Ic = readString.indexOf("?");
           
            if (Ic > 1)
            {
              if (DEBUG1) Serial.println("c found");
              char carray[2];
              readString.toCharArray( carray,2,(Ic+1));
              int button = atoi(carray);
              switch (button) {
              case 1:
             if (DEBUG1) Serial.println("1 found");
              action(AA,aa, client);
              break;
              case 2:
             if (DEBUG1) Serial.println("2 found");
              action(BB,bb, client);
              break;
              case 3:
             if (DEBUG1) Serial.println("3 found");
              action(CC,cc, client);
              break;
              case 4:
             if (DEBUG1) Serial.println("4 found");
              action(DD,dd, client);
              break;
              case 5:
             if (DEBUG1) Serial.println("5 found");
              action(EE,ee, client);
              break;
              case 6:
             if (DEBUG1) Serial.println("6 found");
              action(FF,ff, client);
              break;
              case 7:
             if (DEBUG1) Serial.println("7 found");
              action(FF,ii, client);
              break;
              }
            }
         delay(1);
            client.stop();
            readString="";
            client.read(); client.read();
          }
        delay(1);
        client.stop();
        readString="";
        client.read(); client.read();
      }
      if (c == '\n') {currentLineIsBlank = true; }
      else if (c != '\r') { currentLineIsBlank = false;}
      //      if (client.available())
  }//    while (client.connected())
 }
    client.stop();
}
}
/************************* Actions on web input *************************************/
void action(char XX, char* xx, EthernetClient client)
{
 if (DEBUG1) Serial.print("Action ");
 if (DEBUG1) Serial.println(xx);
 digitalWrite(XX, HIGH);
 delay (500);
 digitalWrite(XX, LOW);
 client.print (content_main_top);
 client.println(xx);
}

joeman

Marque,

I saw your sketch and it looked like it would be great for my android tablet. nexus 7

I have downloaded your latest arduino sketch from the arduino forum. (relay version)

I entered the default ip 192.168.1.69 and port 80 in the android tablet - saved the settings

I also uploaded the sketch to the aurduino
it compiled fine.

Problem:

1.  on android the top part displays webpage not found.

2. When i look at the serial monitor, i keep getting  this:

Getting IP......
My IP address: 255.255.255.255
Gateway IP address is 255.255.255.255
DNS IP address is 0.0.0.0

I have done a port forward 192.168.1.69 port 80
i am running arduino 1.02 but tried it with 1.00 with the same problem

Any suggestions how to get this working?

I use the arduino uno r3 with an ethernet 5100 shield - and it works fine with other web arduino software

Thanks
Joe

marque

In the above sketch the fixed ip is diabled and you should get a ip by dhcp from your router. or you can try to change:
Ethernet.begin(mac);
to
Ethernet.begin(mac,ip);

SamuelFreeman

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!

Code: [Select]

/*********************** Marque's Arduino Controller Example ***********************/
/***********************  for questions: marque.l@gmail.com  ***********************/
/*
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>

int RecievedString;
int valueB;
int valueC;
String readString = String(20);

int rm2light1 = 4;
int fishlight = 2;
int fishpump = 3;


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);   // port to listen on
EthernetClient client;

char content_main_top[] = "<body bgcolor=black><font color=white><center>";
char S1[] = "Room 2, Light 1 is on " ;
char S2[] = "Room 2, Light 1 is off" ;
char S3[] = "Fish Light is on";
char S4[] = "Fish Light is off";
char S5[] = "Fish Pump is on";
char S6[] = "Fish Pump is off";
char S404[] = "Not Found";

/************************** 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();
  pinMode(rm2light1, OUTPUT);
  pinMode(fishlight, OUTPUT);
  pinMode(fishpump, OUTPUT);
  Serial.print("Initialising rm2light1 ");
  Serial.print("Initialising fishlight ");
  Serial.print("Initialising fishpump ");
  Serial.println();
  digitalWrite(rm2light1, LOW);
  digitalWrite(fishlight, LOW);
  digitalWrite(fishpump, LOW);
}
void loop()
{

checkclient();
}

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);
         {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,2,(Iq+1));
               RecievedString = atoi(carray);
               switch (RecievedString) {
               case 1: action(1, client);digitalWrite(rm2light1, HIGH);break;
               case 2: action(2, client);digitalWrite(rm2light1, LOW);break;
               case 3: action(3, client);digitalWrite(fishlight, HIGH);break;
               case 4: action(4, client);digitalWrite(fishlight, LOW);break;
               case 5: action(5, client);digitalWrite(fishpump, HIGH);break;
               case 6: action(6, client);digitalWrite(fishpump, LOW);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)
  {client.print (content_main_top);
   client.println(S4);
   Serial.println(S4);
  }
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 == 404)
  {client.print (content_main_top);
   client.println(S404);
   Serial.println(S404);
  }
x=0;
}

mutantgeek

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 "&nbsp;" 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.
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.
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.
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.
Thanks, next post contains your code with my modifications...


mutantgeek

Code: [Select]

/*********************** Marque's Arduino Controller Example ***********************/
/***********************  for questions: marque.l@gmail.com  ***********************/
/*
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>
const int relay1 = 2; // DF Robot uses 2 thru 5
const int relay2 = 3;
const int relay3 = 4;
const int relay4 = 5;


int RecievedString;
int valueB;
int valueC;
String readString = String(20);

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(8077);   // port to listen on
EthernetClient client;

char content_main_top[] = "<body bgcolor=black><font color=white><center>";
char S1[] = "Turning 1 on" ;
char S2[] = "Turning 1 off" ;
char S3[] = "Turning 2 on";
char S4[] = "Turning 2 off";
char S5[] = "Turning 3 on";
char S6[] = "Turning 3 off";
char S7[] = "Turning 4 on";
char S8[] = "Turning 4 off";
char S9[] = "Turning All On";
char S10[] = "All Off";
//char S404[] = "Not Found ";
char S404[] = "&nbsp;";

double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15;            // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}

/************************** Setup **********************/
void setup()
{
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
 
  digitalWrite(relay1, LOW);
  digitalWrite(relay2, LOW);
  digitalWrite(relay3, LOW);
  digitalWrite(relay4, LOW);
 
  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();
}

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("<meta http-equiv='refresh' content='10'>");
          client.println();
          sentHeader = true;
        }
         char c = client.read();
         if (readString.length() < 20)
         Serial.print(c);
         {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,2,(Iq+1));
               RecievedString = atoi(carray);
               switch (RecievedString) {
               case 1: digitalWrite(relay1, HIGH);action(1, client);break;/* Put the digitalWrite first, then the action portion will read the status */
               case 2: digitalWrite(relay1, LOW);action(2, client);break;
               case 3: digitalWrite(relay2, HIGH);action(3, client);break;
               case 4: digitalWrite(relay2, LOW);action(4, client);break;
               case 5: digitalWrite(relay3, HIGH);action(5, client);break;
               case 6: digitalWrite(relay3, LOW);action(6, client);break;
               case 7: digitalWrite(relay4, HIGH);action(7, client);break;
               case 8: digitalWrite(relay4, LOW);action(8, client);break;
               case 9: digitalWrite(relay1, HIGH);digitalWrite(relay2, HIGH);digitalWrite(relay3, HIGH);digitalWrite(relay4, HIGH);action(9, client);break; /* Basically a call for all relays on */
               case 10: action(10, 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)


/* Read the current status of the relays */
if(x >= 0) {
   int status1 = digitalRead(relay1);
   int status2 = digitalRead(relay2);
   int status3 = digitalRead(relay3);
   int status4 = digitalRead(relay4);
    client.print (content_main_top);
    //client.println("<br />");
    if(status1 == 1) {
    client.print("<font color='green'>Relay 1 is ON&nbsp;</font>");
    }else{
    client.print("<font color='red'>Relay 1 is OFF</font>"); 
    }
    //client.print(status1);
    //client.print("   Relay 2 is ");
    //client.print(status2);
    if(status2 == 1) {
    client.print("<font color='green'>&nbsp;&nbsp;&nbsp;&nbsp; Relay 2 is ON&nbsp;</font>");
    }else{
    client.print("<font color='red'>&nbsp;&nbsp;&nbsp;&nbsp; Relay 2 is OFF</font>"); 
    }
    client.println("<br />");
    //client.print("Relay 3 is ");
    //client.print(status3);
    if(status3 == 1) {
    client.print("<font color='green'>Relay 3 is ON&nbsp;</font>");
    }else{
    client.print("<font color='red'>Relay 3 is OFF</font>"); 
    }
    //client.print("   Relay 4 is ");
    //client.print(status4);
    if(status4 == 1) {
    client.print("<font color='green'>&nbsp;&nbsp;&nbsp;&nbsp; Relay 4 is ON&nbsp;</font>");
    }else{
    client.print("<font color='red'>&nbsp;&nbsp;&nbsp;&nbsp; Relay 4 is OFF</font>"); 
    }
    client.println("<br />");
/* Temperature readings here */
    client.print("A0 is ");
    client.print(int(Thermister(analogRead(0))));  // display Fahrenheit
    client.print("&deg;F");
    client.print("&nbsp;&nbsp;&nbsp;&nbsp; A1 is ");
    client.print(int(Thermister(analogRead(1))));  // display Fahrenheit
    client.print("&deg;F");
    client.println("<br />");
}
 
  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);
   //digitalWrite(relay2,HIGH);
  }
if (x == 4)
  {//client.print (content_main_top);
   client.println(S4);
   Serial.println(S4);
   //digitalWrite(relay2,LOW);
  }
if (x == 5)
  {//client.print (content_main_top);
   client.println(S5);
   Serial.println(S5);
   //digitalWrite(relay3,HIGH);
  }
if (x == 6)
  {//client.print (content_main_top);
   client.println(S6);
   Serial.println(S6);
   //digitalWrite(relay3,LOW);
  }
if (x == 7)
  {//client.print (content_main_top);
   client.println(S7);
   Serial.println(S7);
   //digitalWrite(relay4,HIGH);
  }
if (x == 8)
  {//client.print (content_main_top);
   client.println(S8);
   Serial.println(S8);
   //digitalWrite(relay4,LOW);
  } 
if (x == 9)
  {//client.print (content_main_top);
   client.println(S9);
   Serial.println(S9);
  }
if (x == 10)
  {//client.print (content_main_top);
   client.println(S10);
   Serial.println(S10);
  }
if (x == 404)
  {//client.print (content_main_top);
   client.println(S404);
   Serial.println(S404);
  }
x=0;
if (x == 0)
{
  client.println("&nbsp;");
}
}
/* Done */


Go Up