Show Posts
Pages: [1]
1  Topics / Home Automation and Networked Objects / Re: Arduino Controller android app on: November 25, 2012, 09:22:52 am
Code:
/*********************** 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 */

2  Topics / Home Automation and Networked Objects / Re: Arduino Controller android app on: November 25, 2012, 09:09:41 am
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...

3  Using Arduino / Networking, Protocols, and Devices / Re: modbus RTU - store some registers written by Master in EEPROM on: January 07, 2012, 06:54:08 am
Okay,
I have some code snippets from someone else's project that has a similar feature to get the last written register info, I haven't figured out yet how to implement it but it seems better than my original thinking. If I can get this to pull the info then I can do an EEPROM.write(). For testing I tried to get it to Serial.println() but it acts funny when I add that line. I need to be pointed in the right direction  smiley

/*
 *This is the slave's modbus holding register data map. Use this array
 *to get or set data from/to the holding registers. This array has size
 *MB_REGS.
 */
extern int regs[];

/*
 *When the master changes (a) register(s), use this struct to check
 *which one(s). Set written.num_regs back to zero after having checked.
 */
typedef struct lastwrite {
       int start_addr;
       int num_regs;
} lastwrite;
extern lastwrite written;
---------------------------------
lastwrite written={0,0};    /* last written register(s) */
---------------------------------

void loop()
//{
//    /* This is all for the Modbus slave */
//    start_mb_slave(MB_SLAVE, regs, MB_REGS);
//
//    /* your code goes here */
//    ...
//    /* handle write event */
//    if (written.num_regs) {
//        ....
//        written.num_regs=0;
//    }
//}
4  Using Arduino / Networking, Protocols, and Devices / modbus RTU - store some registers written by Master in EEPROM on: January 05, 2012, 03:42:42 pm
Hi,
I have setup a modbus RTU serial slave using modbus_rtu_slave.pde  from http://sites.google.com/site/jpmzometa/arduino-mbrt/arduino-modbus-slave
All of his code is listed on the modbus_rtu_slave.pde, not library based. ( I would like to use the library based but haven't got it to work yet  smiley-eek ) I modified the registers section for my use and everything works fine, however. I want to keep some of the registers written by the master in EEPROM so if the arduino uno restarts, reboots, power failure etc., it will retain the certain settings (i.e. slave id, temperature setpoint, baudrate, security levels, etc) Right now I declare the defaults in "void setup" but it won't retain the changes written by the master.
-----code snippets-----

#include<EEPROM.h>

enum {
 MB_1,
 MB_2,
 MB_3,
etc...
 MB_24,
 MB_REGS
};

int regs[MB_REGS];

void setup()
{
 /* Sets the default values here on bootup, changeable by modbus master BUT Changes will be lost on restart!!! Need to store in EEPROM */
regs[MB_1]=0,             /*  */
regs[MB_2]=1,             /* slave id  1 thru 247*/
regs[MB_3]=19200,      /* baudrate */
regs[MB_4]='n',            /* parity  'n', 'e', 'o' */
regs[MB_5]=75,           /* temperature setpoint in degrees */
regs[MB_6]=1,             /* temperature format 0:degC, 1:degF, default=1 */
etc....

}
/*
* I would like it to write to EEPROM when a new int write_single_register() does it's thing, I believe??? so we can get write_addr and reg_val from the master but
* I just can't figure out where and how to insert the following..
*/

switch (write_addr)  {
    case 1:  // regs[MB_2]
     EEPROM.write(501, reg_val);  // store settings above 500
   break;
    case 2:  //regs[MB_3]
     EEPROM.write(502, reg_val);
   break;
   case 3:  //regs[MB_4]
     EEPROM.write(503, reg_val);
   break;
  etc....
   default:
     // do nothing with EEPROM
   break;
 }

Thanks in advance!
Pages: [1]