Show Posts
Pages: 1 [2]
16  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 23, 2012, 08:50:54 pm
Frankly, it's hard to help you with general parsing questions. Seeing the string(s) you want to parse, and helping parse those specific strings would be far easier.

I based my program off the example Arduino server code, and to be honest I'm actually still confused on that part.  Here is my program(still a little messy and unfinished):
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

int pin[8] = {2,3,4,5,6,7,8,9};
static char readString[30];
static char *state[8] = {"OFF","OFF","OFF","OFF","OFF","OFF","OFF","OFF",};
static char *pinOn[8] = {"PIN2=T","PIN3=T","PIN4=T","PIN5=T","PIN6=T","PIN7=T","PIN8=T","PIN9=T"};
static char *pinOff[8] = {"PIN2=F","PIN3=F","PIN4=F","PIN5=F","PIN6=F","PIN7=F","PIN8=F","PIN9=F"};
/* LCD */
LiquidCrystal lcd(14,15,16,17,18,19);
int backLight = 1;
void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
 
  //Time setup
  setTime(7,30,0,11,22,12);
 
  //LCD setup
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH);
  lcd.begin(20, 4);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(F("SprinkDuino"));
  delay(5000);
  lcd.clear();
 
  //Sets relay pins (2-9) as output
  for(int i = 2; i < 10; i++){
   pinMode(i, OUTPUT);
  }
  for(int i = 2; i < 10; i++){
   digitalWrite(i, HIGH);          //HIGH is off
  }
}

void loop()
{
  digitalClockDisplay();
  Alarm.delay(1000);
 
  EthernetClient client = server.available();                // listen for incoming clients
  if (client) {
    boolean currentLineIsBlank = true;                       // an http request ends with a blank line
    while (client.connected()) {
      if (client.available()) {
       
        char c = client.read();
        int pos = strlen(readString);
        if(pos < 29)
        {
           readString[pos++] = c;
           readString[pos] = '\0';
        }
        if (c == '\n' && currentLineIsBlank) {         
          for(int i = 0; i < 8; i++){
            if (strstr(readString, "PIN") == pinOn[i]) {          //Turns current pin on
              digitalWrite(pin[i], LOW);           
              state[i] = "ON";
            }
            else if (strstr(readString, "PIN") == pinOff[i]) {    //Turns current pin off
              digitalWrite(pin[i], HIGH);
              state[i] = "OFF";
            }
          }
          if(strstr(readString, "PIN") == "PINA=F") {              //Turns all pins off
            for(int i = 0; i < 8; i++){
             digitalWrite(pin[i], HIGH);
             state[i] = "OFF";
            }
          }
          else if(strstr(readString, "PIN") == "PINA=T") {       //Turns all pins on
            for(int i = 0; i < 8; i++){
             digitalWrite(pin[i], LOW);
             state[i] = "ON";
            }
          }
          }
          client.println(F("HTTP/1.1 200 OK"));
          client.println(F("Content-Type: text/html"));
          client.println();
          client.println(F("<title>SprinkDuino</title>"));
          client.println(F("<h1 color=red style=text-align:center;>Welcome to SprinkDuino!</h1>"));
          client.print(F("<table border=0><tr>"));
          client.println(F("<td><a href=\"./?PINA=T\"><button><font size=4>TURN ALL <font color=green>ON</font></font></button><a></td>"));
          client.println(F("<td><a href=\"./?PINA=F\"><button><font size=4>TURN ALL <font color=red>OFF</font></font></button><a></td>"));
          client.println(F("<tr>"));
         
          for(int i = 2; i < 10; i++){                    //Prints state of sprinkler pin
           client.print(F("<td>"));
           client.print(F("<font size=4>Sprinkler "));
           client.print(i-1);
           client.print(F(" is "));
           if(state[i-2] == "ON"){
             client.print(F("<b><font color=green>"));
             client.print(state[i-2]);
             client.print(F("</font></b></font>"));
             client.print(F("</td>"));
           }
           else {
             client.print(F("<b><font color=red>"));
             client.print(state[i-2]);
             client.print(F("</font></b></font>"));
             client.print(F("</td>"));
           }
           if (state[i-2] == "ON") {                      //Turns sprinkler pin on or off
              client.print(F("<td>"));
              client.print(F("<a href=\"./?"));
              client.print(pinOff[i-2]);
              client.print(F("\"><button>Turn Off</button><a>"));
              client.print(F("</td>"));
              client.print(F("<tr>"));
            }
            else {
              client.print(F("<td>"));
              client.print(F("<a href=\"./?"));
              client.print(pinOn[i-2]);
              client.print(F("\"><button>Turn On</button><a>"));
              client.print(F("</td>"));
              client.print(F("<tr>"));
            }         
          client.print(F("</table>"));
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    readString[0] = '\0';
    // close the connection:
    client.stop();
  }
}

void digitalClockDisplay()
{
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(F("Time"));
  lcd.setCursor(0,1);
  lcd.print(hour());
  printDigits(minute());
  printDigits(second());
}

void printDigits(int digits)
{
  lcd.print(F(":"));
  if(digits < 10)
    lcd.print('0');
  lcd.print(digits);
}

So if you want pin 2 to turn on, you would click the button which appends "PIN2=T" onto the end of the URL.  readString is then set to PIN2=T and compared to pinOn[0].  If it is true pin 2 is set to LOW, turning the relay on.  I just don't fully get what 'c' is doing...I'm guessing since it is expected to be the new line character '\n', once you press the button the program will interpret the URL and set the readString accordingly?

Sorry to be jumping around to different topics, but the program is now crashing similar to how it was previously.  When I open up the page, it repeats the following over and over (with a few buttons that I couldn't paste here):
Code:
Welcome to SprinkDuino!

Sprinkler 1 is OFF
HTTP/1.1 200 OK Content-Type: text/html

Since I'm not using strings anymore, I'm guessing it's some kind of memory leak?  Could it also be because I'm trying to do too much formatting with the HTML?
17  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 23, 2012, 06:19:00 pm
Ahh alright that makes sense now, thanks!  I just have a couple other quick questions regarding C-style strings...

I changed this:
Code:
if (readString.length() < 30) {
          readString.concat(c);
        }
to this:
Code:
if (strlen(readString) < 30) {
          strcat(readString, &c);
        }
What I want is to concatenate the char c onto readString but it gave me errors unless I wrote it as the address of c, which isn't going to do what i want is it?
EDIT: I figured out I could just make a new char array and concatenate it as follows:
Code:
        char c = client.read();
        char newC[2];
        newC[0] = c;
        newC[1] = '\0';
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply

        if (strlen(readString) < 30) {
          strcat(readString, newC);
        }
^^This should be fine, right?


I changed this:
Code:
int PIN = readString.indexOf("PIN");
to this:
Code:
int PIN = *strrchr(readString, 'PIN');
It is supposed to set PIN to the index of where "PIN" is in the readString (which is actually a char array now, not a string).  I looked up how to do this with C strings and this is the only function I thought would work.  

And this:
Code:
         for(int i = 0; i < 8; i++){
            if (readString.substring(PIN,PIN+6) == pinOn[i]) {          //Turns current pin on if strings match
              digitalWrite(pin[i], LOW);          
              state[i] = "ON";
            }
to this:
Code:
         for(int i = 0; i < 8; i++){
            if (substr(PIN,PIN+6,readString) == pinOn[i]) {          //Turns current pin on if strings match
              digitalWrite(pin[i], LOW);          
              state[i] = "ON";
            }
Which is supposed to take a substring of readString starting at the index of PIN, and is six characters long (or should it be seven to include the null character?) then compare it to another string.

Thanks for your help, I've been working on these for hours and can't seem to get them right.  I'm sure some of it has to do with pointers, which I always get a little confused by.
18  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 23, 2012, 11:42:02 am
Do you mean I need to declare the variable and initialize it with something other than the words?
Code:
static char *pinOn[8] = {"PIN2=T","PIN3=T","PIN4=T","PIN5=T","PIN6=T","PIN7=T","PIN8=T","PIN9=T"};
^^So this wouldn't work?

I don't have much experience with 2D arrays...would you recommend it over using an array of pointers?
19  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 22, 2012, 09:33:54 pm
Sorry, I was l little confused...well I still am lol.
So then the declaration should look like this, correct?:
Code:
static char readString[30];
static char state[8];
static char pinOn[8];
static char pinOff[8];
What I don't get about this is how can I make a static char array that holds a word in each index?  The way I have it means that 'state' is going to be a character array that holds 8 single charactors, doesn't it?  Would I have to make a two dimensional array for each index to hold 5 characters such as "hello"?
20  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 21, 2012, 10:43:40 am
I haven't used C in a while since most things I do are in C++, would these be appropriate conversions?
Code:
int pin[8] = {2,3,4,5,6,7,8,9};
char readString;
char *state[8];
char *pinOn[8];
char *pinOff[8];
char *pinTime[8];

I keep getting an error at this line:
Code:
       if (sizeof(readString) < 30) {
          strcat(readString, c);
Am I using the correct functions to get the length and for concatenation?  It's saying "Invalid conversion from 'char' to '*char' " when it gets to the concatenation.

EDIT:Also, should I do this every time I print something, even if its just HTML code?:
Code:
client.print(F("Sprinkler "));

Thanks!
21  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 20, 2012, 11:36:52 pm
Thanks, I'll definitely change that!  Is there supposed to be a fix for that soon?
22  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 20, 2012, 10:32:52 pm
Alright, I had a microSD card in the ethernet shield but didn't program the Arduino to use it (I just put it there for future use).  I took it out and now my program works fine!  Hmmm...
23  Using Arduino / Programming Questions / link sends user to code page? on: November 20, 2012, 10:16:37 pm
Hi everyone,
I'm making a web based sprinkler control system using an Arduino UNO R3, and ethernet shield, and an 8-relay board.  It's all working fine and I have some code written up which was working great until now.  I have a button that turns all the relays on, and a button that turns all relays off.  I also have buttons for each individual relay to turn on or off.  Out of nowhere, when I was testing the program, I clicked on the "turn all on" button and it turns all the relays on (like its supposed to do) but right after that it redirects me to a page of HTML code.  It looks like this:
Code:
ton>Turn On</button><a><br><br>Sprinkler 2 is <b><font color=red></b>OFF</font><br><a href="./?PIN3=T"><button>Turn On</button><a><br><br>Sprinkler 3 is <b><font color=red></b>OFF</font><br><a href="./?PIN4=T"><button>Turn On</button><a><br><br>Sprinkler 4 is <b><font color=red></b>OFF</font><br><a href="./?PIN5=T"><button>Turn On</button><a><br><br>Sprinkler 5 is <b><font color=red></b>OFF</font><br><a href="./?PIN6=T"><button>Turn On</button><a><br><br>Sprinkler 6 is <b><font color=red></b>OFF</font><br><a href="./?PIN7=T"><button>Turn On</button><a><br><br>Sprinkler 7 is <b><font color=red></b>OFF</font><br><a href="./?PIN8=T"><button>Turn On</button><a><br><br>Sprinkler 8 is <b><font color=red></b>OFF</font><br><a href="./?PIN9=T"><button>Turn On</button><a><br><br>
<br><br>
Sprinkler 1 is <b><font color=red></b>OFF</font><br><a href="./?
PIN2=T
"><button>Turn On</button><a>
<br><<a href=HTTP/1.1 200 OK
Content-Type: text/html

<title>SprinkDuino</title>
<h1 color=red style=text-align:center;>Welcome to SprinkDuino!</h1>
<br><br>
<a href="./?PINA=T">TURN ALL ON<a>
<br><br>
<a href="./?PINA=F">TURN ALL OFF<a>
<br><br>
Sprinkler 1 is <b><font color=green></b>ON</font><br><a href="./?PIN2=F"><button>Turn Off</button><a><br><br>Sprinkler 2 is <b><font color=red></b>OFF</font><br><a href="./?PIN3=T"><button>Turn On</button><a><br><br>Sprinkler 3 is <b><font color=red></b>OFF</font><br><a href="./?PIN4=T"><button>Turn On</button><a><br><br>Sprinkler 4 is <b><font color=red></b>OFF</font><br><a href="./?PIN5=T"><button>Turn On</button><a><br><br>Sprinkler 5 is <b><font color=red></b>OFF</font><br><a href="./?PIN6=T"><button>Turn On</button><a><br><br>Sprinkler 6 is <b><font color=red></b>OFF</font><br><a href="./?PIN7=T"><button>Turn On</button><a><br><br>Sprinkler 7 is <b><font color=red></b>OFF</font><br><a href="./?PIN8=T"><button>Turn On</button><a><br><br>Sprinkler 8 is <b><font color=red></b>OFF</font><br><a href="./?PIN9=T"><button>Turn On</button><a><br><br>FF</font><br><a href="./?PIN2=T"><button>Turn On</button><a><br><br>Sprinkler 2 is <b><font color=red></b>OFF</font><br><a href="./?PIN3=T"><button>Turn On</button><a><br><br>Sprinkler 3 is <b><font color=red></b>OFF</font><br><a href="./?PIN4=T"><button>Turn On</button><a><br><br>Sprinkler 4 is <b><font color=red></b>OFF</font><br><a href="./?PIN5=T"><button>Turn On</button><a><br><br>Sprinkler 5 is <b><font color=red></b>OFF</font><br><a href="./?PIN6=T"><button>Turn On</button><a><br><br>Sprinkler 6 is <b><font color=red></b>OFF</font><br><a href="./?PIN7=T"><button>Turn On</button><a><br><br>Sprinkler 7 is <b><font color=red></b>OFF</font><br><a href="./?PIN8=T"><button>Turn On</button><a><br><br>Sprinkler 8 is <b><font color=red></b>OFF</font><br><a href="./?PIN9=T"><button>Turn On</button><a><br><br>
<br><br>
Sprinkler 1 is <b><font color=red></b>OFF</font><br><a href="./?
PIN2=T
"><button>Turn On</button><a>
<br><<a href=HTTP/1.1 200 OK
Content-Type: text/html
....
....
....

...and its pretty long.  I'm guessing this is the output code from the Arduino, but I can't figure out whats going wrong.  I didn't change any code between the time it was working and started doing this.  Hopefully one of you has some idea of what's going on!

Here's my program code:
Code:
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

int pin[8] = {2,3,4,5,6,7,8,9};
String readString = String(30);
String state[8] = String(3);
String pinOn[8] = String(6);
String pinOff[8] = String(6);

void setup()
{
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
 
  //Sets relay pins (2-9) as output
  for(int i = 2; i < 10; i++){
   pinMode(i, OUTPUT);
  }
  for(int i = 2; i < 10; i++){
   digitalWrite(i, HIGH);
   state[i-2] = "OFF";
  }
  pinOn[0] = "PIN2=T";
  pinOn[1] = "PIN3=T";
  pinOn[2] = "PIN4=T";
  pinOn[3] = "PIN5=T";
  pinOn[4] = "PIN6=T";
  pinOn[5] = "PIN7=T";
  pinOn[6] = "PIN8=T";
  pinOn[7] = "PIN9=T";
 
  pinOff[0] = "PIN2=F";
  pinOff[1] = "PIN3=F";
  pinOff[2] = "PIN4=F";
  pinOff[3] = "PIN5=F";
  pinOff[4] = "PIN6=F";
  pinOff[5] = "PIN7=F";
  pinOff[6] = "PIN8=F";
  pinOff[7] = "PIN9=F";
}

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply

        if (readString.length() < 30) {
          readString.concat(c);
        }

        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          int PIN = readString.indexOf("PIN");
         
          for(int i = 0; i < 8; i++){
            if (readString.substring(PIN,PIN+6) == pinOn[i]) {
              digitalWrite(pin[i], LOW);           
              state[i] = "ON";
            }
            else if (readString.substring(PIN,PIN+6) == pinOff[i]) {
              digitalWrite(pin[i], HIGH);
              state[i] = "OFF";
            }
          }
          if(readString.substring(PIN,PIN+6) == "PINA=F") {
            for(int i = 0; i < 8; i++){
             digitalWrite(pin[i], HIGH);
             state[i] = "OFF";
            }
          }
          else if(readString.substring(PIN,PIN+6) == "PINA=T") {
            for(int i = 0; i < 8; i++){
             digitalWrite(pin[i], LOW);
             state[i] = "ON";
            }
          }
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<title>SprinkDuino</title>");
          client.println("<h1 color=red style=text-align:center;>Welcome to SprinkDuino!</h1>");
          client.println("<br><br>");
         
          client.println("<a href=\"./?PINA=T\"><button>TURN ALL ON</button><a>");
          client.println("<br><br>");
          client.println("<a href=\"./?PINA=F\"><button>TURN ALL OFF</button><a>");
          client.println("<br><br>");
         
          for(int i = 2; i < 10; i++){
           client.print("Sprinkler ");
           client.print(i-1);
           client.print(" is ");
           if(state[i-2] == "ON"){
             client.print("<b><font color=green></b>");
             client.print(state[i-2]);
             client.print("</font>");
             client.print("<br>");
           }
           else if(state[i-2] == "OFF"){
             client.print("<b><font color=red></b>");
             client.print(state[i-2]);
             client.print("</font>");
             client.print("<br>");
           }
           if (state[i-2] == "ON") {
              client.print("<a href=\"./?");
              client.print(pinOff[i-2]);
              client.print("\"><button>Turn Off</button><a>");
            }
            else {
              client.print("<a href=\"./?");
              client.print(pinOn[i-2]);
              client.print("\"><button>Turn On</button><a>");
            }
           client.print("<br><br>");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    readString = "";
    // close the connection:
    client.stop();
  }
}

Thanks in advance!
-Ryan
24  Using Arduino / Programming Questions / Re: Learning web programming on: November 14, 2012, 10:10:51 pm
Perfect! Thanks a bunch!
25  Using Arduino / Programming Questions / Learning web programming on: November 14, 2012, 09:51:16 pm
Hi everyone,
I know C++ pretty well and understand most of the Arduino programming, but I have no experience in writing client/server programs and would like to learn how...what would be a good resource for this?  I want to be able to write my own programs for web control of the Arduino with the ethernet shield without copying someone else's code.
Thanks in advance!
26  Using Arduino / Displays / Re: Using LCD with multiple shields...no pins left on: November 14, 2012, 09:20:36 pm
Thanks!  I think I'll try the shift register first, just to save money since I already have a display
27  Using Arduino / Displays / Using LCD with multiple shields...no pins left on: November 14, 2012, 09:05:41 pm
Hi everyone, I hope I'm posting this in the right place...

I have an Arduino Uno R3 and made a web controlled relay system using an Arduino ethernet shield and SainSmart 8 channel relay.  It all works fine, but I also purchased a 20x4 LCD display that I would like to use (this one -> http://www.amazon.com/LCD-Module-Arduino-White-Blue/dp/B003B22UR0) and half of the pins the LCD needs to connect to on the Arduino are already used...The ethernet shield needs four, and my relay is taking up eight, and I'm guessing if I still connect to the pins using a breadboard it won't work because the code I have is already controlling those pins.  What are my options?
I'm pretty new to Arduino, but it all seems pretty straightforward...maybe I'm missing something?
Thanks for any help!
-Ryan
Pages: 1 [2]