Pages: [1]   Go Down
Author Topic: Standard Web remote to control 2 relays  (Read 1352 times)
0 Members and 1 Guest are viewing this topic.
New Brunswick, Canada
Offline Offline
Newbie
*
Karma: 1
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, new here. been trying to rewrite code to control 2 relays via web remote but not having much luck. Want second outputPin =7 and can rename 2nd relay to whatever...  Code now is as follows:

// Web server for switching 2 relays

#include "etherShield.h"
#include "ETHER_28J60.h"
#include "avr/wdt.h"

int outputPin = 6;
int Led_1 = 4;
int Led_2 = 5;
int relayStatus;

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24}; // this just needs to be unique for your network,
static uint8_t ip[4] = {10, 100, 2, 45}; // IP address for the webserver
static uint16_t port = 80; // Use port 80 - the standard for HTTP
ETHER_28J60 e;

void setup()
{
wdt_disable();

e.setup(mac, ip, port);
pinMode(outputPin, OUTPUT);
pinMode(Led_1, OUTPUT);
pinMode(Led_2, OUTPUT);

digitalWrite(outputPin, LOW);
digitalWrite(Led_1, LOW);
digitalWrite(Led_2, HIGH);

wdt_enable(WDTO_2S);
}

void loop()
{
char* params;
if (params = e.serviceRequest())
{
e.print("<h1><a href='/?led=off'>Web Remote</a></h1>");
if (strcmp(params, "?led=on") == 0)
{
digitalWrite(outputPin, HIGH);
digitalWrite(Led_1, HIGH);
digitalWrite(Led_2, LOW);
e.print("<a href='?led=off'><button style='border: 10px solid #ff0000;font-size:800%; border-left: 20px solid #ff0000' type='button'>RELAY1 IS ON</button></a>");
}
else if (strcmp(params, "?led=off") == 0)
{
digitalWrite(outputPin, LOW);
digitalWrite(Led_1, LOW);
digitalWrite(Led_2, HIGH);
e.print("<a href='?led=on'><button style='border: 10px solid #000;font-size:800%; border-left: 20px solid #000' type='button'>RELAY1 IS OFF</button></a>");
}
else
{
relayStatus = digitalRead(outputPin);
if (relayStatus == LOW)
{
e.print("<a href='?led=on'><button style='border: 10px solid #000;font-size:800%; border-left: 20px solid #000' type='button'>RELAY1 IS OFF</button></a>");
}
else
{
e.print("<a href='?led=off'><button style='border: 10px solid #ff0000;font-size:800%; border-left: 20px solid #ff0000' type='button'>RELAY1 IS ON</button></a>");
}
//e.print("<h1><a href='/?led=off'>secondary line of text after buttons</a></h1>");
}
e.respond();
}
wdt_reset();
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
been trying to rewrite code to control 2 relays via web remote but not having much luck. Want second outputPin =7 and can rename 2nd relay to whatever.
So, what's the problem? outputPin is just a name. There's nothing magic about it. Create another variable to hold the other pin number. Call it TweedleDee for all that it really matters.

Of course, if you do, don't expect us to help you debug the code. Names should be meaningful.

You don't really have an "output" connected to the pin, do you? So, name the pin for whatever it really controls, instead.
Logged

New Brunswick, Canada
Offline Offline
Newbie
*
Karma: 1
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have 2, 5v relays to control I have one of them working now, with the current code, on pin 6. I was going to hook the 2nd relay to pin 7 I figured i needed another int outputPin  line that =7. (only want to use pin 7 to keep wires tidy, can use another if need be)

And sorry I meant the button name, It could just be called "Relay 2 IS ON".... ect... 

And yes i'd like to replace outputPin  with Relay1  or Relay2 .... just so it referrs to exactly what it's controlling...
So i'd assume i could write this:

int relay1 = 6;
int relay2 = 7;

As far as the rest of the code, i just unsure on how to have it display the second button and in turn activate the second relay.

Thanks for the help! New at this but eager to learn!

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So i'd assume i could write this:

int relay1 = 6;
int relay2 = 7;
Exactly.

Code:
e.print("<a href='?led=off'><button style='border: 10px solid #ff0000;font-size:800%; border-left: 20px solid #ff0000' type='button'>RELAY1 IS ON</button></a>");
None of that style stuff is strictly needed. Save memory; dump it.

You can simply replicate this line, for a second button with the text "RELAY2 IS ON". Replicate the corresponding off line, too.

Now, to keep them straight, you really should change the href part of all 4 statements, to ?relay1=off, ?relay2=off, ?relay1=on, and ?relay2=on, respectively.

Then, you have 4 blocks like this:
Code:
if (strcmp(params, "?led=on") == 0)
but for ?relay1=off, ?relay2=off, ?relay1=on, and ?relay2=on.
Logged

New Brunswick, Canada
Offline Offline
Newbie
*
Karma: 1
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I cant seem to get it to work. I can get second button to show up but cant get them to work seperately... any chance you could post adjusted code and explain the added lines? Not sure where to add the 4 blocks as you Quoted...  I seem to learn by doing and making mistakes, but my mistakes are not getting me anywhere!!  If i see it i can better understand how it works....

This is what i have...  i left the button style info as it made the buttons extremely small on my smartphone...

Quote
// Control 2 seperate relays via wifi

#include "etherShield.h"
#include "ETHER_28J60.h"
#include "avr/wdt.h"

int relay1 = 6;
int relay2 = 7;
int Led_1 = 4;
int Led_2 = 5;
int relayStatus;

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24}; // this just needs to be unique for your network,
static uint8_t ip[4] = {10, 100, 2, 45}; // IP address for the webserver
static uint16_t port = 80; // Use port 80 - the standard for HTTP
ETHER_28J60 e;


void setup()

wdt_disable();

e.setup(mac, ip, port);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(Led_1, OUTPUT);
pinMode(Led_2, OUTPUT);

digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(Led_1, LOW);
digitalWrite(Led_2, HIGH);

wdt_enable(WDTO_2S);
}

void loop()
{
char* params;
if (params = e.serviceRequest())
{
e.print("<h1><a href='/?led=off'>DANNY'S WIFI REMOTE</a></h1>");
if (strcmp(params, "?led=on") == 0)
{
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(Led_1, HIGH);
digitalWrite(Led_2, LOW);
e.print("<a href='?led=off'><button style='border: 10px solid #ff0000;font-size:800%; border-left: 20px solid #ff0000' type='button'>RELAY1 ON</button></a>");
e.print("<a href='?led=off'><button style='border: 10px solid #ff0000;font-size:800%; border-left: 20px solid #ff0000' type='button'>RELAY2 ON</button></a>");
}
if (strcmp(params, "?led=off") == 0)
{
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(Led_1, LOW);
digitalWrite(Led_2, HIGH);
e.print("<a href='?led=on'><button style='border: 10px solid #000;font-size:800%; border-left: 20px solid #000' type='button'>RELAY1 OFF</button></a>");
e.print("<a href='?led=on'><button style='border: 10px solid #000;font-size:800%; border-left: 20px solid #000' type='button'>RELAY2 OFF</button></a>");
}
else
{
relayStatus = digitalRead(relay1);
relayStatus = digitalRead(relay2);
if (relayStatus == LOW)
{
e.print("<a href='?led=on'><button style='border: 10px solid #000;font-size:800%; border-left: 20px solid #000' type='button'>RELAY1 OFF</button></a>");
e.print("<a href='?led=on'><button style='border: 10px solid #000;font-size:800%; border-left: 20px solid #000' type='button'>RELAY2 OFF</button></a>");
}
else
{
e.print("<a href='?led=off'><button style='border: 10px solid #ff0000;font-size:800%; border-left: 20px solid #ff0000' type='button'>RELAY1 ON</button></a>");
e.print("<a href='?led=off'><button style='border: 10px solid #ff0000;font-size:800%; border-left: 20px solid #ff0000' type='button'>RELAY2 ON</button></a>");


e.respond();
}
wdt_reset();
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This is what i have.
Apparently, you aren't listening.

Quote
i left the button style info as it made the buttons extremely small on my smartphone...
So what? Get it working with a device with a real display first.

Quote
Then, you have 4 blocks like this:
but for ?relay1=off, ?relay2=off, ?relay1=on, and ?relay2=on.
This means that you ALSO need to change the href statements.
Logged

New Brunswick, Canada
Offline Offline
Newbie
*
Karma: 1
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Apparently, you aren't listening.

no, i just dont understand. I'm in too deep for my experience level... Ill research what everything means and give it another shot. 

Thanks
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The href bit defines what gets added to the GET request.

Initially, when the browser executes a GET request to have the Arduino serve up the page, it will look something like this:
GET / HTTP 1.0

When the form has submit buttons, with action statements, the resulting GET request is different. Suppose you press the button that says RELAY1 ON on it. The GET request that the browser makes will look something like:
GET /?led=off HTTP 1.0

When you press the button that says RELAY2 ON, the GET request that the browser makes will look something like:
GET /?led=off HTTP 1.0

Now, how is the Arduino supposed to tell which button you pressed?

If the href value for the first button (RELAY1 ON) is "<a href='?frog>" and the href value for the second button (RELAY2 ON) is "<a href='?tomato>", the two GET requests will be something like:
GET /?frog HTTP 1.0
and
GET /?tomato HTTP 1.0

Now, the Arduino can see quite easily that different buttons were pressed.

Clearly, the intent is to make the GET request comprehensible to the Arduino, but it is also reasonable to make the GET request comprehensible to mere humans, too. That's why I suggested that the href values be ?relay1=off, ?relay2=off, ?relay1=on, and ?relay2=on. You can look at each of them and tell right away what the code in the if(strcmp() == 0) block should do. That's not so easy with ?frog, ?tomato, ?sunspot, and ?elephant.

Logged

New Brunswick, Canada
Offline Offline
Newbie
*
Karma: 1
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much for the help on this Paul.

I am able to switch 2 seperate relays now with 2 buttons showing.

Now i have a temperature sensor on its way, i just need to figure out how to tack that code onto the end so it will display under my buttons smiley-wink

Cheers for now!
Logged

Pages: [1]   Go Up
Jump to: