I am developing the web-based application using Arduino with ethernet shield. I'm facing trouble with comparing the user-defined text-box value with predefined set value. my logic is simple if the user-defined value is greater than predefined value then digital pin should be high otherwise low.
My Arduino code is as 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
};
//IPAddress ip(192, 168, 1, 177); //by default in exmple code
IPAddress ip(192, 168, 0, 111); //taken from webserver example pdf in iot beginners kit
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
float sv = 2.0;
float pv;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// 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 (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: close"); // the connection will be closed after completion of the response
//client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<title>TTSD Control System</title>");
client.println("<fieldset>");
client.println("<legend>Thermo Vacccum Test System Division</legend>");
client.println("<head/>");
client.println("<script type = 'text/javascript'>");
//client.println("function v() {var pv = document.getElementById('txt').value");
client.println("function v()");
client.println("var pv = document.getElementById('txt').value");
if(pv>=sv)
{
digitalWrite(4, HIGH);
//return true;
}
else if(pv<sv)
{
digitalWrite(4, LOW);
//return false;
}
client.println("</script>");
client.println("<body>");``
client.println("<form name = 'frm'>");
client.println("<input type = 'text' id='txt'/>");
client.println("<input type = 'submit' name = 'click' value = 'submit' onclick = 'return v()'/>");
client.println("</form>");
client.println("</body>");
// output the value of each analog input pin
int analogChannel = 0;
float sensorReading = analogRead(analogChannel);
pv = ((sensorReading*5)/1023);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(pv);
client.println("<br />");
client.println("</fieldset>");
client.println("</html>");
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);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
/*int v(){
if(pv>=sv)
{
digitalWrite(4, HIGH);
return true;
}
else if(pv<sv)
{
digitalWrite(4, LOW);
return false;
}
}*/
when I clicked on submit button, it will not get into the if...else condition. Here I'm using digital pin 4 and SV stand for SET Value which is fixed 2.0 and PV stand for Process Value, which I am going to compare with SV.
Thanks in Advance.
Aucun commentaire:
Enregistrer un commentaire