Hi. My goal is to control all the pins i have spare from the Ethernet shield like 3 to 9, i want to have buttons in the the html and different names like “lights” “door” “siren” “etc”. Of course i care about the pins state so if i change PC or browser or just open the html (web server) after two days to know exactly the state of the pin.
i have a code to start i found a matrix way and a single way for one pin but the Q is can i make to work to my needs.
Thank you in Advance even if you don’t answer i know the time is precious to all of us, now days…
Nikos Pap.
Infrastructure Engineer T.E.
matrix way
#include <Ethernet.h>
#include <SPI.h>
//network NB: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 10, 10 };
byte gateway[] = { 192, 168, 10, 254 };
byte subnet[] = { 255, 255, 255, 0 };
String inString = String(35);
String Led;
int led[] = {00, 2, 3, 4 , 5, 6, 7, 8, 9 }; //Led pins num 0 in array is not used
int numofleds = 8; //Number of Leds
String value[] = {"on","on","on","on","on","on","on","on","on"}; //startup all led are off
Server server(80);
String data;
void setup()
{
Serial.begin(115200);
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//set pin mode
for (int j= 1; j < (numofleds + 1); j++){
pinMode(led[j], OUTPUT);
}
Serial.println("Serial READY");
Serial.println("Ethernet READY");
Serial.println("Server READY");
Serial.println("info @ Support-Plus");
Serial.println("");
Serial.println("www.support-plus.gr");
}
void loop()
{
Client client = server.available();
if(client){
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if(client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (inString.length() < 35) {
inString.concat(c);
}
if (c == '\n' && current_line_is_blank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body><form method=get>");
client.println("<p>Led controller</p>");
for(int i= 1; i < (numofleds + 1) ;i++){
Led = String("led") + i;
if(inString.indexOf(Led+"=on")>0 || inString.indexOf("all=on")>0){
Serial.println(Led+"on");
digitalWrite(led, HIGH);
value = "off";
}else if(inString.indexOf(Led+"=off")>0 || inString.indexOf("all=off")>0 ){
Serial.println(Led+"on");
digitalWrite(led, LOW);
value = "on";
}
client.println("
"+Led+" <input type=submit name="+Led+" value="+value+">");
}
client.println("
All <input type=submit name=all value=on><input type=submit name=all value=off>");
client.println("</from></html></body>");
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
} else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
inString = "";
client.stop();
}
}
one led below
#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[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[]={192,168,0, 149};
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(80);
int LEDpin = 7;
String readString = String(30);
String state = String(3);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
//Sets the LEDpin as an output
pinMode(LEDpin,OUTPUT);
digitalWrite(LEDpin,LOW);
state = "OFF";
}
void loop()
{
// listen for incoming clients
Client 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 LED = readString.indexOf("LED=");
if (readString.substring(LED,LED+5) == "LED=T") {
digitalWrite(LEDpin,HIGH);
state = "ON";
}
else if (readString.substring(LED,LED+5) == "LED=F") {
digitalWrite(LEDpin,LOW);
state = "OFF";
}
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("LED is ");
client.print(state);
client.print("
");
if (state == "ON") {
client.println("<a href=\"./?LED=F\">Turn Off<a>");
}
else {
client.println("<a href=\"./?LED=T\">Turn On<a>");
}
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();
}
}