Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Adding token to end of URL on: December 17, 2012, 07:37:31 pm
Hello,
I have an Ethernet shield and relay board hooked up to my Arduino.  I have a webpage set up and its working fine.  At the moment I have on/off buttons for the relay that, when clicked, append a couple letters with tokens onto the URL.  This is then tokenized using strtok, etc. and the switch is turned on or off...  

Code:
...
...
char *response = strtok(readString, "?");

          response = strtok(NULL, "=");
          if(strcmp (response, "A") == 0){
            val = 1;
          }
          else {
            val = atoi(response);
          }
          response = strtok(NULL, ".");
          tempHr = atoi(response);
          response = strtok(NULL, ".");
          tempMin = atoi(response);
          response = strtok(NULL, ";");
            if(strcmp (response, "T") == 0){
              trueFalse = 'T';
            }
            else
              trueFalse = 'F';
...
...

So now I'm working on a different part of the code where I need to have the user be able to set a weekly schedule for the relays.  I'd like to create text boxes for each day of the week in which the user can input a time like "5:30" and then a GET HTTP request will be sent so that the Arduino can change the time accordingly.
I have it somewhat working, however I'm having problems with tokenizing the requests. The following code sends the request and works fine:
Code:
         client.print("<form action='http://192.168.1.134:8088/' method='GET'>");
          client.print("<input type='text' name='2' maxlength='5' size='5' />");
          client.print("<intput type='submit' value='Submit' /></form><br>");

But I don't know how I can tokenize it when I already have code in the loop that is tokenizing a different type of information for the individual on/off buttons.  For example, when the on/off button is pressed for relay 2 the URL will look like this:

http://192.168.1.134:8088/?2=T;

And when I type in the text box to change the time to "5:30" for relay 2the URL will be:

http://192.168.1.134:8088/?2=5%3A30

I realize the ":" is encoded to %3A so I will have to deal with that as well (although 5.30 works fine but I'd rather not use that);

So as you can see, my problem is that I need to tokenize the URL differently depending on the request.  Is there an easy way of doing this?

Thanks for any help!  I hope this all made sense...

EDIT: I've been thinking of adapting my project using the concepts here http://www.cs.helsinki.fi/u/ljlukkar/iot/#conclusions but I don't know anything about java.  Would it make more sense for me to try and learn to do this?
2  Using Arduino / Programming Questions / Re: User defined timeAlarm times? on: December 12, 2012, 02:03:17 pm
The alarmRepeat() method is typically called from setup(). This is so that it is not called on every pass through loop(). However, if the time to have the alarm go off changes, because of user input, there is no reason not to call alarmRepeat() from loop(), once in a while.

Once in a while...so 8 times (one for each sprinkler zone) probably wouldn't be the best idea?

Would it work to create global variables such as h1, h2, h3 etc. for hours and m1, m2, m3 etc. for minutes, call the alarmRepeat() function in setup(), and have the user change the variables in loop()?  Or would I need to call the alarmRepeat() functions in loop() for this to work? 
3  Using Arduino / Programming Questions / User defined timeAlarm times? on: December 12, 2012, 12:30:40 pm
Hello,
I'm making an Arduino controlled sprinkler system using an ethernet shield so that it can be controlled through the web.  I'm working on the schedule and wonder if there is a way to use the timeAlarms library in a way so that the user can change the dates/times of the alarms?  What I have in mind is to create a number of functions such as:

Code:
  Alarm.alarmRepeat(h1,m1,0, ZoneOne);
  Alarm.alarmRepeat(h2,m2,0, ZoneTwo);
...
...

And the user will have input boxes for each zone to change the time, date, etc.  However, from what I understand the alarmRepeat function is supposed to be called in the setup(), not from the loop(), so would this even be possible? 

Thanks for any help!
-Ryan
4  Using Arduino / Programming Questions / Re: Dynamic DNS only working locally on: December 03, 2012, 04:01:27 pm
Interesting...well it seems to be working for what I need right now so I can't complain. Thanks a lot for the help!
5  Using Arduino / Programming Questions / Re: Dynamic DNS only working locally on: December 03, 2012, 03:44:15 pm
That was it! It's working in Firefox now, but still not in Internet explorer.  Although this isn't very important to me since I never use IE.
6  Using Arduino / Programming Questions / Re: Dynamic DNS only working locally on: December 03, 2012, 03:28:37 pm
Ah alright, I see.  Well something interesting is happening when I port forward to 8088; now it is working fine on my iPhone, in Firefox it is just displaying the html text code, and in Internet Explorer it is displaying the page correctly but none of the buttons work.  Any ideas of why this might be happening?
7  Using Arduino / Programming Questions / Re: Dynamic DNS only working locally on: December 03, 2012, 03:11:15 pm
I added the subnet and gateway and now I have this:
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>

// General
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,0,0);
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"};
char trueFalse;
int val;
int cursorPin;

//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, gateway, subnet);
  server.begin();
...
...
...

However, it is still not working...I had also already port forwarded port 80 to 192.168.1.134 on my router, although I didn't enable DMZ.  Should I be using DMZ instead of port forwarding?

EDIT: I deleted my port forwarding settings, and enabled DMZ for the Arduino IP address and it is working the same as before; still no access from outside networks
8  Using Arduino / Programming Questions / Dynamic DNS only working locally on: December 03, 2012, 01:07:50 pm
Hi,
I have an Arduino project using an ethernet shield and I wanted to be able to access it remotely, so I set up a dynamic DNS service with www.no-ip.com.  It works fine when I access it through my local network but if I try from an outside network it gives me the following error:

"Response Error.

Technical  Description:
502 Bad Gateway - Response Error, a bad response was received from another proxy server or the destination origin server"

From what I understand, this means that www.no-ip.com has sent the request to the correct IP address but there was no server there?

Here's my Arduino code (not sure if this would be part of the problem though):

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>

// General
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
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"};
char trueFalse;
int val;
int cursorPin;

//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();
 
  //Sets the LEDpin as an output
  for(int i = 0; i < 8; i++){
    pinMode(pin[i],OUTPUT);
    digitalWrite(pin[i],HIGH);
 }
 
   //LCD Setup
   pinMode(backLight, OUTPUT);
   digitalWrite(backLight, HIGH);
   lcd.begin(20,4);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("SprinkDuino");
   lcd.setCursor(0,1);
   lcd.print("Version 1.0");
   delay(5000);
   lcd.clear();
}

void loop()
{
EthernetClient client = server.available();
  if (client)
  {
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        int pos = strlen(readString);
        if(pos < 30){
         readString[pos++] = c;
         readString[pos] = '\0';
        }
       
        if (c == '\n' && currentLineIsBlank)
        {
         
          char *response = strtok(readString, "?");

          response = strtok(NULL, "=");
          if(strcmp (response, "A") == 0){
            val = 1;
          }
          else {
            val = atoi(response);
          }
          response = strtok(NULL, ";");
          if(strcmp (response, "T") == 0){
            trueFalse = 'T';
          }
          else {
            trueFalse = 'F';
          }
       
          client.println("<html>");
          //Refresh page every 5 seconds to keep page live
          client.print("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<h1>SprinkDuino</h1>");
          client.println("<br><br>");
          client.print("<table border=1><tr>");
          client.print("<td><a href=\"./?A=T;\"><button>Turn All On</button></a>");
          client.print("<a href=\"./?A=F;\"><button>Turn All Off</button></a><br><br>");
         
          //Individual pin control
          for(int i = 0; i < 8; i++){
            if ((val == (i + 2)) && (trueFalse == 'T')) {
              digitalWrite(pin[i],LOW);
            }
            else if ((val == (i + 2)) && (trueFalse == 'F')) {
              digitalWrite(pin[i],HIGH);
            }
           
            //Turn all on or off
            else if ((val == 1) && (trueFalse == 'T')) {
              for(int i = 0; i < 8; i++){
                digitalWrite(pin[i],LOW);
              }
            }
            else if ((val == 1) && (trueFalse == 'F')) {
              for(int i = 0; i < 8; i++){
                digitalWrite(pin[i],HIGH);
              }
            }
           
            //Changes state[] depending on pin status           
            for(int h = 0; h < 8; h++){
             if(digitalRead(pin[h]) == LOW){
              state[h] = "ON";             
             }
             else{
              state[h] = "OFF";
             }
            }

            client.print("Sprinkler ");
            client.print(pin[i]);
            client.print(" is ");
            client.print(state[i]);
           
            if (state[i] == "ON") {
              client.print("<a href=\"./?");
              client.print(pin[i]);
              client.print("=F;\"><button>Turn Off</button><a>");
            }
            else {
              client.print("<a href=\"./?");
              client.print(pin[i]);
              client.print("=T;\"><button>Turn On</button><a>");
            }           
            client.print("<br>");
          }
          client.print("</td><td>");
          client.print("<h1>Schedule</h1>");
          client.print("</td></tr>");
          client.print("</table>");
          client.println("</html>");
          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("Time: ");
  lcd.print(hour());
  printDigits(minute());
  printDigits(second());
  lcd.setCursor(0,2);
  lcd.print("1|2|3|4|5|6|7");
}

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

Another quick question...If I change the port to anything but 80 like 8080, it still works but only prints the output text from the Arduino.  I tried changing my firewall settings and even turning it off, but it still didn't interpret the code as html.  This isn't too big of a problem for me since port 80 seems to be doing the trick, but just thought I'd ask.

Thanks!
9  Using Arduino / Programming Questions / Re: NTP Schedule on: December 03, 2012, 12:51:15 pm
I read up on it, and I think I'll give it a shot before trying to mess with client/server code!  Thanks for the help guys
10  Using Arduino / Programming Questions / Re: NTP Schedule on: December 03, 2012, 11:23:05 am
Thanks Cybernetician, I'll check that out.

PaulS - It doesn't need to be too accurate, it would probably be fine if it was up to an hour off.  Would an RTC need to be re-programmed every once in a while to keep its accuracy?
11  Using Arduino / Programming Questions / NTP Schedule on: December 02, 2012, 06:26:30 pm
Hi everyone,

I wrote some code for an automated sprinkler system using an Arduino Uno, ethernet shield, and an 8 relay board.  I would like to be able to set a weekly schedule for it and am planning on using the timeAlarms library and NTP time.  I'm just wondering what the best way is to implement the NTP into my existing code (I'm using the example from http://arduino.cc/en/Tutorial/UdpNtpClient).  I'm trying to figure out when to send and read the packet from the time server...this is just confusing to me since I am already reading information from a client using the ethernet shield.  Should I send the NTP packet before I connect to the client, or after the client is available?

Aso, all of the output in the NTP example is done in an if() statement:

Code:
if ( Udp.parsePacket() ) {  
    // We've received a packet, read the data from it
    Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);  
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;  
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);
...
...
...
}

(I will be using client.print in place of serial.print)

Do I need to put my existing void loop() code inside this if() statement?  Or should it be vice versa?


This is my existing code:
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>

// General
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
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"};
char trueFalse;
int val;
int cursorPin;

// NTP
unsigned int localPort = 8888;
IPAddress timeServer(192, 43, 244, 18); // time.nist.gov NTP server
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer [NTP_PACKET_SIZE];
EthernetUDP Udp; // A UDP instanve to let us send and receibe packets over UDP

//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();
  
  //Sets the LEDpin as an output
  for(int i = 0; i < 8; i++){
    pinMode(pin[i],OUTPUT);
    digitalWrite(pin[i],HIGH);
 }
 
   //LCD Setup
   pinMode(backLight, OUTPUT);
   digitalWrite(backLight, HIGH);
   lcd.begin(20,4);
   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("SprinkDuino");
   lcd.setCursor(0,1);
   lcd.print("Version 1.0");
   delay(5000);
   lcd.clear();
  
   //TIME Setup
   Udp.begin(localPort);
}

void loop()
{
  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();
        int pos = strlen(readString);
        if(pos < 30){
         readString[pos++] = c;
         readString[pos] = '\0';
        }
        
        if (c == '\n' && currentLineIsBlank) {
          
          char *response = strtok(readString, "?");

          response = strtok(NULL, "=");
          if(strcmp (response, "A") == 0){
            val = 1;
          }
          else {
            val = atoi(response);
          }
          response = strtok(NULL, ";");
          if(strcmp (response, "T") == 0){
            trueFalse = 'T';
          }
          else {
            trueFalse = 'F';
          }
        
          client.println("<html>");
          //Refresh page every 5 seconds to keep page live
          client.print("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<h1>SprinkDuino</h1>");
          client.println("<br><br>");
          client.print("<table border=1><tr>");
          client.print("<td><a href=\"./?A=T;\"><button>Turn All On</button></a>");
          client.print("<a href=\"./?A=F;\"><button>Turn All Off</button></a><br><br>");
          
          //Individual pin control
          for(int i = 0; i < 8; i++){
            if ((val == (i + 2)) && (trueFalse == 'T')) {
              digitalWrite(pin[i],LOW);
            }
            else if ((val == (i + 2)) && (trueFalse == 'F')) {
              digitalWrite(pin[i],HIGH);
            }
            
            //Turn all on or off
            else if ((val == 1) && (trueFalse == 'T')) {
              for(int i = 0; i < 8; i++){
                digitalWrite(pin[i],LOW);
              }
            }
            else if ((val == 1) && (trueFalse == 'F')) {
              for(int i = 0; i < 8; i++){
                digitalWrite(pin[i],HIGH);
              }
            }
            
            //Changes state[] depending on pin status            
            for(int h = 0; h < 8; h++){
             if(digitalRead(pin[h]) == LOW){
              state[h] = "ON";              
             }
             else{
              state[h] = "OFF";
             }
            }

            client.print("Sprinkler ");
            client.print(pin[i]);
            client.print(" is ");
            client.print(state[i]);
            
            if (state[i] == "ON") {
              client.print("<a href=\"./?");
              client.print(pin[i]);
              client.print("=F;\"><button>Turn Off</button><a>");
            }
            else {
              client.print("<a href=\"./?");
              client.print(pin[i]);
              client.print("=T;\"><button>Turn On</button><a>");
            }            
            client.print("<br>");
          }
          client.print("</td><td>");
          client.print("<h1>Schedule</h1>");
          client.print("</td></tr>");
          client.print("</table>");
          client.println("</html>");
          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();
  }
}


// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address)
{
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the packets)
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:        
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer,NTP_PACKET_SIZE);
  Udp.endPacket();
}

Thanks for any help!
12  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 24, 2012, 12:07:47 am
Not really. Wait until the entire line arrives, then parse it. That is, wait until the newline, like in the link.

So this would be more appropriate?:
Code:
...
...
           if (strcmp (state[i], "ON") == 0){                      //Turns sprinkler pin on or off
              client.print(F("<td>"));
              client.print(F("<a href=\"./?"));
              client.print(pin[i]);
              client.print("=");
              client.print(off);
              client.print(F("\"><button>Turn Off</button><a>"));
              client.print(F("</td>"));
              client.print(F("<tr>"));
             
              char* response = strtok(readString, "?");
              response = strtok(NULL, "=");
              val = atoi(response);       
              response = strtok(NULL, ";");           
              if(response == "T"){
                trueFalse = 'T';
              }
              else {
                trueFalse = 'F';
              }
            }
...
...

Maybe you should go back to a basic web server setup and get it working again. Too much unneeded churn to diagnose and correct the problem. Apparently it wasn't a String class issue.

I think I'll do that.  I'm going insane trying to get this to work haha.
Thanks again for all the help everyone
13  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 23, 2012, 11:51:07 pm
I must've accidentally deleted that when I changed to the token method.  Would this be the correct way to do that?
Code:
#include <SPI.h>
#include <Ethernet.h>
#include <LiquidCrystal.h>
#include <Time.h>
#include <TimeAlarms.h>

byte mac[6] = { 0x90, 0xA2, 0xDA, 0x0D, 0x98, 0x26 };
IPAddress ip(192,168,1,134);
EthernetServer server(80);

int pin[8] = {2,3,4,5,6,7,8,9};
static char readString[30] = {'\0'};
static char *state[8] = {"OFF","OFF","OFF","OFF","OFF","OFF","OFF","OFF",};
static char *on = "T;";
static char *off = "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';
        }
        
        char* response = strtok(readString, "?");
        response = strtok(NULL, "=");
        int val = atoi(response);        
        response = strtok(NULL, ";");        
        char trueFalse;        
        if(response == "T"){
          trueFalse = 'T';
        }
        else {
          trueFalse = 'F';
        }

        if (c == '\n' && currentLineIsBlank) {          
          for(int i = 0; i < 8; i++){
            if ((val == pin[i]) && (trueFalse == 'T')) {          //Turns current pin on
              digitalWrite(pin[i], LOW);          
              state[i] = "ON";
            }
            else if ((val == pin[i]) && (trueFalse == 'F')) {    //Turns current pin off
              digitalWrite(pin[i], HIGH);
              state[i] = "OFF";
            }
          }
          if((val == 10) && (trueFalse == 'T')) {              //Turns all pins off
            for(int i = 0; i < 8; i++){
             digitalWrite(pin[i], HIGH);
             state[i] = "OFF";
            }
          }
          else if((val == 10) && (trueFalse == 'F')) {       //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=\"./?10=T;\"><button><font size=4>TURN ALL <font color=green>ON</font></font></button><a></td>"));
          client.println(F("<td><a href=\"./?10=F;\"><button><font size=4>TURN ALL <font color=red>OFF</font></font></button><a></td>"));
          client.println(F("<tr>"));
          
          for(int i = 0; i < 8; 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(strcmp (state[i], "ON") == 0){
             client.print(F("<b><font color=green>"));
             client.print(state[i]);
             client.print(F("</font></b></font>"));
             client.print(F("</td>"));
           }
           else {
             client.print(F("<b><font color=red>"));
             client.print(state[i]);
             client.print(F("</font></b></font>"));
             client.print(F("</td>"));
           }
           if (strcmp (state[i], "ON") == 0){                      //Turns sprinkler pin on or off
              client.print(F("<td>"));
              client.print(F("<a href=\"./?"));
              client.print(pin[i]);
              client.print("=");
              client.print(off);
              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(pin[i]);
              client.print("=");
              client.print(on);
              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);
}

Thanks for the link, input makes a lot more sense now.
14  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 23, 2012, 11:31:04 pm
Thanks for catching that, I changed it to
Code:
if (strcmp (state[i], "ON") == 0)

Still crashing though  smiley-sad
15  Using Arduino / Programming Questions / Re: link sends user to code page? on: November 23, 2012, 10:30:18 pm
Thanks for all the suggestions! I changed the code to using the token method and this is what I came up with:

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);
EthernetServer server(80);

int pin[8] = {2,3,4,5,6,7,8,9};
static char readString[30] = {'\0'};
static char *state[8] = {"OFF","OFF","OFF","OFF","OFF","OFF","OFF","OFF",};
static char *on = "T;";
static char *off = "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)
        {
           pos++;
           readString[pos] = c;
        }
        char* response = strtok(readString, "?");
        response = strtok(NULL, "=");
        int val = atoi(response);       
        response = strtok(NULL, ";");       
        char trueFalse;       
        if(response == "T"){
          trueFalse = 'T';
        }
        else {
          trueFalse = 'F';
        }

        if (c == '\n' && currentLineIsBlank) {         
          for(int i = 0; i < 8; i++){
            if ((val == pin[i]) && (trueFalse == 'T')) {          //Turns current pin on
              digitalWrite(pin[i], LOW);           
              state[i] = "ON";
            }
            else if ((val == pin[i]) && (trueFalse == 'F')) {    //Turns current pin off
              digitalWrite(pin[i], HIGH);
              state[i] = "OFF";
            }
          }
          if((val == 10) && (trueFalse == 'T')) {              //Turns all pins off
            for(int i = 0; i < 8; i++){
             digitalWrite(pin[i], HIGH);
             state[i] = "OFF";
            }
          }
          else if((val == 10) && (trueFalse == 'F')) {       //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=\"./?10=T;\"><button><font size=4>TURN ALL <font color=green>ON</font></font></button><a></td>"));
          client.println(F("<td><a href=\"./?10=F;\"><button><font size=4>TURN ALL <font color=red>OFF</font></font></button><a></td>"));
          client.println(F("<tr>"));
         
          for(int i = 0; i < 8; 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(strcmp (state[i], "ON") == 0){
             client.print(F("<b><font color=green>"));
             client.print(state[i]);
             client.print(F("</font></b></font>"));
             client.print(F("</td>"));
           }
           else {
             client.print(F("<b><font color=red>"));
             client.print(state[i]);
             client.print(F("</font></b></font>"));
             client.print(F("</td>"));
           }
           if (strcmp (state[i], "ON") == 0){                      //Turns sprinkler pin on or off
              client.print(F("<td>"));
              client.print(F("<a href=\"./?"));
              client.print(pin[i]);
              client.print("=");
              client.print(off);
              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(pin[i]);
              client.print("=");
              client.print(on);
              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);
}
EDIT: I changed the code a bit since this post and updated it ^^
However, it's still crashing like before so I can't tell if it's working...any idea what that could be?  
Pages: [1] 2