I have been struggling with a sketch for days now, and have finally achieved something very close to my goal.
Here is what I have working, and the ")<---" in the CODE will show where I was able to get things working, and what lines I entered.
ATTN: This WORKS. Will NOT FAIL due to /html tag, though I don't understand why. No complaints. Just curious.
CODE:
// Command for server GET and POST request.
void serverCmd(WebServer &server, WebServer::ConnectionType type, char *url_tail, bool tail_complete)
{
int chk;
float t = dht.readTemperature(); )<------ THIS block of 3 lines.
float h = dht.readHumidity();
if (type == WebServer::POST)
{
bool repeat;
char name[20], value[16];
do
{
repeat = server.readPOSTparam( name, 20, value, 16);
if (strcmp( name, "On" ) == 0)
{
setLatchChannelOn( atoi(value) );
}
if (strcmp( name, "Off" ) == 0)
{
setLatchChannelOff( atoi(value) );
}
if (strcmp( name, "AllOff" ) == 0)
{
sendRawValueToLatch1(0);
sendRawValueToLatch2(0);
}
} while (repeat);
server.httpSeeOther(PREFIX);
return;
}
server.httpSuccess();
if (type == WebServer::GET)
{
P(message) =
"<html>"
"<head>"
"<meta http-equiv=\"refresh\" content=\"5\">" )<-----THIS line
"<title>"
"PAGE"
"</title>"
"</head>"
"<body>"
"<H1 style='font-family:hobo std;'> PAGE</H1>"
"<form action='/page' method='POST'>"
"<p><button name='AllOff' value='0'>All Off</button></p>"
"<p><button type='submit' name='On' value='1'>1 On</button><button type='submit' name='Off' value='1'>Off</button>.......<button type='submit' name='On' value='2'>2 On</button><button type='submit' name='Off' value='2'>Off</button></p>"
"<p><button type='submit' name='On' value='3'>3 On</button><button type='submit' name='Off' value='3'>Off</button>.......<button type='submit' name='On' value='4'>4 On</button><button type='submit' name='Off' value='4'>Off</button></p>"
"<p><button type='submit' name='On' value='5'>5 On</button><button type='submit' name='Off' value='5'>Off</button>.......<button type='submit' name='On' value='6'>6 On</button><button type='submit' name='Off' value='6'>Off</button></p>"
"<p><button type='submit' name='On' value='7'>7 On</button><button type='submit' name='Off' value='7'>Off</button>.......<button type='submit' name='On' value='8'>8 On</button><button type='submit' name='Off' value='8'>Off</button></p>"
"</form>"
"</body></html>";
server.printP(message);
{
server.println("<H3>");
server.print("Temperature: ");
server.println("</H3>");
server.println("<H2>");
server.print(t*1.8+32);
server.println(" °");
server.println("F");
server.println("</H2>");
server.println("<H3>"); ) <--- This, from { to }
server.print("Humidity: ");
server.println("</H3>");
server.println("<p />");
server.println("<H2>");
server.print(h);
server.print(" %\t");
server.println("</H2>");
}
}
}
//SETUP
void setup()
{
Ethernet.begin(mac, ip);
Wire.begin();
Serial.begin( 9600 );
dht.begin();
webserver.setDefaultCommand(&serverCmd);
webserver.begin();
initialiseShield(SHIELD_1_I2C_ADDRESS);
sendRawValueToLatch1(0);
}
//LOOP
void loop()
{
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
Serial.println(F("Failed to read from DHT"));
} else {
Serial.print(F("Temperature: "));
Serial.print(t);
Serial.println(F(" *C"));
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F(" %\t"));
delay(1000);
}
webserver.processConnection();
}
//...->
void initialiseShield(int shieldAddress)
{
Wire.beginTransmission(shieldAddress);
Wire.write(0x12);
Wire.write(0x20); // use table 1.4 addressing
Wire.endTransmission();
Wire.beginTransmission(shieldAddress);
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // Set all of bank A to outputs
Wire.endTransmission();
}
//...->
void toggleLatchChannel(byte channelId)
{
if( channelId >= 1 && channelId <= 8 )
{
byte shieldOutput = channelId;
byte channelMask = 1 << (shieldOutput - 1);
shield1BankA = shield1BankA ^ channelMask;
sendRawValueToLatch1(shield1BankA);
}
}
//...->
void setLatchChannelOn (byte channelId)
{
if( channelId >= 1 && channelId <= 8 )
{
byte shieldOutput = channelId;
byte channelMask = 1 << (shieldOutput - 1);
shield1BankA = shield1BankA | channelMask;
sendRawValueToLatch1(shield1BankA);
}
}
//...->
void setLatchChannelOff (byte channelId)
{
if( channelId >= 1 && channelId <= 8 )
{
byte shieldOutput = channelId;
byte channelMask = 255 - ( 1 << (shieldOutput - 1));
shield1BankA = shield1BankA & channelMask;
sendRawValueToLatch1(shield1BankA);
}
}
//...->
void sendRawValueToLatch1(byte rawValue)
{
Wire.beginTransmission(SHIELD_1_I2C_ADDRESS);
Wire.write(0x12); // Select GPIOA
Wire.write(rawValue); // Send value to bank A
shield1BankA = rawValue;
Wire.endTransmission();
}
The only thing I have left to figure out is how to get the T/H figures to print ABOVE the relay buttons, rather than below, but when I move the lines of code that pertain to the T/H data, it makes my buttons disappear, or the Buttons/Title disappear depending on where I place the code. Which is somewhat confusing.
Maybe someone can help with that part?
Thank you,
Darren