I apologize. My post was posted with very little background and no code tags.
i am using the arduino(with ethernet shield) to communicate with a zilog z8 processor. Our school teaches embedded systems using that old processor.
The z8 is controlling a hydroponics project. The arduino is used as a web based interface to retrieve and display data.
With the help of a tutorial from startingelectronics.org I am able to set up a webpage and retrieve data from the z8 and it displays on the screen.
I have one on screen button which uses onclick to call a function. i can retrieve and display data with one button. The serial monitor displays the http request as well.
If I add a second button(and corresponding onclick function), neither button can retrieve data. the screen doesnt update and the serial monitor doesnt show an http request.
here is the code
#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x37, 0x73 };
IPAddress ip(192, 168, 0, 120); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// AJAX request for switch state
if (HTTP_req.indexOf("ajax_switch") > -1) {
// read switch state and send appropriate paragraph text
Getz8data(client);
}
else { // HTTP request for web page
// send web page - contains JavaScript with AJAX calls
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Web Page</title>");
client.println("<script>");
client.println("function Getz8data() {");
client.println("nocache = \"&nocache=\"\
+ Math.random() * 1000000;");
client.println("var request = new XMLHttpRequest();");
client.println("request.onreadystatechange = function() {");
client.println("if (this.readyState == 4) {");
client.println("if (this.status == 200) {");
client.println("if (this.responseText != null) {");
client.println("document.getElementById(\"switch_txt\")\
.innerHTML = this.responseText;");
client.println("}}}}");
client.println(
"request.open(\"GET\", \"ajax_switch\" + nocache, true);");
//client.println("request.open(\"GET\", \"ajax_switch\", true);");
client.println("request.send(null);");
client.println("}");
client.println("</script>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Sensor Data</h1>");
client.println(
"<p id=\"switch_txt\">data not requested yet</p>");
client.println("<button type=\"button\"\
onclick=\"Getz8data()\">Get temp data</button>");
client.println("</body>");
client.println("</html>");
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
void Getz8data(EthernetClient cl)
{ char outgoingbyte = 3;
int incomingbyte=0;
Serial.write(outgoingbyte);
if (Serial.available() > 0) {
incomingbyte = Serial.read();
cl.println("<p> The data is");
cl.println(incomingbyte);
cl.println("Degrees Celcius </p>");
}
}
With my limited knowledge of Ajax and the get request, I am not sure of how to add a second button.
The code I copied is for one working onscreen button. If I added multiple buttons(with onclick functions) the page doesnt update.
How could I add more buttons, have ajax update each button and still get a get request?