I have a arduino nano with a enc28j60 setup as a web server.
The webpage has buttons on it that toggle the state of output pins on the nano.
I am trying to make the buttons on the webpage dynamic instead of hard coded.
I use the serial monitor to print the variable data that i need.
When i print tha variable data in the program setup() it works as required.
When i try and insert variable data into the button href it fails to create the button.
When i print tha data in the program loop() it fails to print.
Any ideas what i am doing wrong?
#include <UIPEthernet.h> // include UIPEthernet library
uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; // define mac address
//IPAddress myIP(192,168,18,68); // define server ip address - AVANZA
IPAddress myIP(192,168,0,68); // define server ip address - NEXT
UIPServer server = UIPServer(80); // configure server on port 80
String opNum[8] =
// {"Start", "Stop","Up","Down","Inch","Base","Latch","Enable"}; // array of web browser button options
{"111", "222","333","444","555","666","777","888"}; // array of web browser output numbers
int opPins[8] = {2,3,4,5,6,7,8,9}; // array of digital output pin numbers
String temp = "\"/111\"";
void setup() {
Serial.begin(9600); // start serial port
UIPEthernet.begin(mac,myIP); // start ethernet hardware settings
server.begin(); // start web server
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP()); // display server IP address
Serial.println("**** Printing ****");
Serial.println("<a href="+temp+">Start</a> <br>");
Serial.println();
pinMode(2, OUTPUT); // output pin 2
pinMode(3, OUTPUT); // output pin 3
pinMode(4, OUTPUT); // output pin 4
pinMode(5, OUTPUT); // output pin 5
pinMode(6, OUTPUT); // output pin 6
pinMode(7, OUTPUT); // output pin 7
pinMode(8, OUTPUT); // output pin 8
pinMode(9, OUTPUT); // output pin 9
}
void loop() {
if (UIPClient client = server.available()) { // enable client connection if server available
if (client) { // check for a new web browser client
Serial.println("new web browser client"); // display new web browser client
String currentLine = ""; // String for data from the web browser client
String tempLine = "";
while (client.connected()) { // loop while the web browser client connected
if (client.available()) { // check for bytes to read from the web browser client
char c = client.read(); // read a byte from the web browser client
Serial.write(c); // display the web browser client data byte
if (c == '\n') { // check if the byte is a newline character
if (currentLine.length() == 0) { // check for the end of data from the web browser client
client.println("HTTP/1.1 200 OK"); // send server http response to web browser client
client.println("Content-type:text/html"); // send server content type response
client.println(); // send empty line to end response
Serial.println("**** Not printing ???? ****");
Serial.println("<a href="+temp+">Start</a> <br>");
client.print("<a href="+temp+">Start</a> <br>");
client.print("<a href=\"/111\">Start</a> <br>"); // create web browser client start button
client.print("<a href=\"/222\">Stop</a> <br>"); // create web browser client stop button
client.print("<a href=\"/333\">Ramp Up</a> <br>"); // create web browser client ramp up button
client.print("<a href=\"/444\">Ramp Down</a> <br>"); // create web browser client ramp down button
client.print("<a href=\"/555\">Inch</a> <br>"); // create web browser client inch button
client.print("<a href=\"/666\">Run Base</a> <br>"); // create web browser client run to base button
client.print("<a href=\"/777\">Set Latch</a> <br>"); // create web browser client set latch position button
client.print("<a href=\"/888\">Enable Latch</a> <br>"); // create web browser client enable latch position button
break; // break out of the while loop
}
else { // check for a newline
tempLine = currentLine; // save current line string
currentLine = ""; // clear currentLine
}
}
else if (c != '\r') { // check for anything else but a carriage return
currentLine += c; // add data byte to the end of the currentLine
}
}
}
client.stop(); // close the web browser client connection
Serial.println("client disconnected"); // display web browser client disconnected
for (int i = 0; i < 8; i++) { // setup loop counter
if (tempLine.indexOf(opNum[i]) > 0) { // check if any web browser button pressed
toggleOutput(opPins[i]); // set output for the button pressed
}
}
}
}
}
void toggleOutput(int outputNumber) { // get output number pressed on web browser
digitalWrite(outputNumber, HIGH); // turn on output
Serial.println(digitalRead (outputNumber), DEC); // display staus of led
delay(1000); // wait 1 second
digitalWrite(outputNumber, LOW); // turn off output
Serial.println(digitalRead (outputNumber), DEC); // display staus of led
}