Hi,
I have found the following code which I want to adapt to control 8 relays. However, before I try to modify it I wanted to see the code working with just the 4 relays.
After successfully uploading the sketch and accessing the web page through my browser I can control Relays 2, 3 and 4.
No matter what I try I can't seem to get Relay 1 to toggle on and off. I think it has something to do with the boolean array for the Pin Status but can't seem to make sense of it.
The web page button changes state in the browser, but not the actual relay.
/*
-----------------------
VCC - 3.3V
GND - GND
SCK - Pin 13
SO - Pin 12
SI - Pin 11
CS - Pin 8
------------------
*/
#include <EtherCard.h>
// MAC Address
static byte mymac[] = {
0xFA,0x5A,0x51,0x53,0x5A,0x5A };
// ip
static byte myip[] = {
192,168,0,212 };
byte Ethernet::buffer[900];
BufferFiller bfill;
int LedPins[] = {
2,3,4,5};
boolean PinStatus[] = {
1,2,3,4};
//-------------
const char http_OK[] PROGMEM =
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n\r\n";
const char http_Found[] PROGMEM =
"HTTP/1.0 302 Found\r\n"
"Location: /\r\n\r\n";
const char http_Unauthorized[] PROGMEM =
"HTTP/1.0 401 Unauthorized\r\n"
"Content-Type: text/html\r\n\r\n"
"<h1>401 Unauthorized</h1>";
//------------
void homePage()
{
bfill.emit_p(PSTR("$F"
"<title>ArduinoPIN Webserver</title>"
"Relay 1: <a href=\"?ArduinoPIN1=$F\">$F</a>
"
"Relay 2: <a href=\"?ArduinoPIN2=$F\">$F</a>
"
"Relay 3: <a href=\"?ArduinoPIN3=$F\">$F</a>
"
"Relay 4: <a href=\"?ArduinoPIN4=$F\">$F</a>"),
http_OK,
PinStatus[1]?PSTR("off"):PSTR("on"),
PinStatus[1]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
PinStatus[2]?PSTR("off"):PSTR("on"),
PinStatus[2]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
PinStatus[3]?PSTR("off"):PSTR("on"),
PinStatus[3]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"),
PinStatus[4]?PSTR("off"):PSTR("on"),
PinStatus[4]?PSTR("<font color=\"green\"><b>ON</b></font>"):PSTR("<font color=\"red\">OFF</font>"));
}
//------------------------
void setup()
{
Serial.begin(9600);
// "ethercard" (CS-pin) = 8
// if (ether.begin(sizeof Ethernet::buffer, mymac) == 0).
// "ethercard" (CS-pin) = 10
// if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0).
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0);
if (!ether.dhcpSetup());
ether.printIp("My Router IP: ", ether.myip);
//ether.staticSetup(myip);
ether.printIp("My SET IP: ", ether.myip);
//-----
for(int i = 0; i <= 4; i++)
{
pinMode(LedPins[i],OUTPUT);
digitalWrite (LedPins[i],HIGH);
PinStatus[i]=false;
}
}
// --------------------------------------
void loop()
{
delay(1);
word len = ether.packetReceive(); // check for ethernet packet
word pos = ether.packetLoop(len); // check for tcp packet
if (pos) {
bfill = ether.tcpOffset();
char *data = (char *) Ethernet::buffer + pos;
if (strncmp("GET /", data, 5) != 0) {
bfill.emit_p(http_Unauthorized);
}
else {
data += 5;
if (data[0] == ' ') {
homePage(); // Return home page
for (int i = 0; i <= 3; i++)digitalWrite(LedPins[i],!PinStatus[i+1]);
}
// "16" = "?ArduinoPIN1=on ".
else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) {
PinStatus[1] = true;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) {
PinStatus[2] = true;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN3=on ", data, 16) == 0) {
PinStatus[3] = true;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN4=on ", data, 16) == 0) {
PinStatus[4] = true;
bfill.emit_p(http_Found);
}
//------------------------------------------------------
else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) {
PinStatus[1] = false;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) {
PinStatus[2] = false;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN3=off ", data, 17) == 0) {
PinStatus[3] = false;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN4=off ", data, 17) == 0) {
PinStatus[4] = false;
bfill.emit_p(http_Found);
}
//---------------------------
else {
// Page not found
bfill.emit_p(http_Unauthorized);
}
}
ether.httpServerReply(bfill.position()); // send http response
}
}
Thanks in advance