Hello, I have an arduino mega with ethernet shieId and I need to call a function/subroutine when an HTML button is pressed, but I don't know exactly how to do it. I read many examples where all use readString.indexOf and exange a string, but I would like to call directly from HTML button my function that will change my pin status from LOW to HIGH. I just need to keep in HIGH for 3 seconds a digital pin.
I could use something similar: client.print("<INPUT TYPE=BUTTON OnClick=digitalWrite(lout_1, HIGH) VALUE= OPEN DOOR >");
but I'm wondering if there is a way to call a subroutine from HTML button. Do I need a java script? I already declared my void function as:
void opendoor() {
digitalWrite(out_1, HIGH);
delay(3000);
digitalWrite(out_1, LOW)
}
How can i call it from HTML button? client.print(""); It will work?
Thank you for help!
I have an arduino mega with ethernet shieId and I need to call a function/subroutine when an HTML button is pressed, but I don't know exactly how to do it.
Where does that function live? If it is on the Arduino, you can't call the function from the HTML code.
More details are needed.
Is the Arduino acting as client or server?
Arduino is the server, and the client is my smartphone: with arduino IP I load a webpage with an HTML button: when I push it I need to change my digitalPin in HIGH for 3 second. I made a void function in Arduino code for doing it, after void setup(), but now I need to call it from HTML button.
There is a way for doing it? Soon I post the code, thank you a lot.
I made a void function in Arduino code for doing it, after void setup(), but now I need to call it from HTML button.
There is a way for doing it?
If the button is a submit button on a form, and the form has an action associated with it, pressing the button generates a new GET request that is sent to the Arduino. You need, on the Arduino, to pay attention to what is being requested. Check the request for a ? followed by some data. If present, call the appropriate function.
Here is my simple code:
the problem is in the only HTML button: when I press it I would like that apriporta() starts. Thank you
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 220 };
EthernetServer serverfranzolio(80);
String readString; //variabile stringa, che però non dovrebbe più servire
byte porta = 2;
void setup()
{
Ethernet.begin(mac, ip);
pinMode(porta, OUTPUT);
}
void apriporta()
{
digitalWrite(porta, HIGH);
delay(3000);
digitalWrite(porta, LOW);
}
void loop(){
EthernetClient client=serverfranzolio.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
readString.concat(c);
if (c == '\n' && currentLineIsBlank) {
//con client.print inizio a dichiarare l'html
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>APERTURA PORTA</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.print("
");
client.print("<INPUT TYPE=BUTTON OnClick=apriporta VALUE= Apriporta >");
client.print("<p><p/>");
client.print("</body></html>");
readString="";
delay(1);
client.flush();
client.stop();
}
}
}
}
}
Please use code tags when posting code - it stops the forum software from mangling your code. You can fix that by editing your original post, selecting the code, and clicking the # button in the edit window to add the code tags.
I think your problem is here:
client.print("<INPUT TYPE=BUTTON OnClick=apriporta VALUE= Apriporta >");
You have to set up a communication mechanism as PaulS suggests: Device how you will represent your button press in an HTTP request (I suggest using a FORM submit). Change the code I quoted to output some HTML that initiates the HTTP request when the button is pressed. Write some code on the Arduino that will detect when the received URL request corresponds to a request triggered by your button, and carry out whichever action you want associated with it.
Simple web server test code for controlling an arduino pin.
//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ')
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port
String readString;
//////////////////////
void setup(){
pinMode(5, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
//enable serial data print
Serial.begin(9600);
Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString); //print to serial monitor for debuging
//now output HTML data header
if(readString.indexOf('?') >=0) { //don't send new page
client.println("HTTP/1.1 204 Zoomkat");
client.println();
client.println();
}
else {
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<TITLE>Arduino GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Zoomkat's simple Arduino button</H1>");
client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a>");
client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");
//client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
client.println("<IFRAME name=inlineframe style=\"display:none\" >");
client.println("</IFRAME>");
client.println("</BODY>");
client.println("</HTML>");
}
delay(1);
//stopping client
client.stop();
///////////////////// control arduino pin
if(readString.indexOf("on") >0)//checks for on
{
digitalWrite(5, HIGH); // set pin 4 high
Serial.println("Led On");
}
if(readString.indexOf("off") >0)//checks for off
{
digitalWrite(5, LOW); // set pin 4 low
Serial.println("Led Off");
}
//clearing string for next read
readString="";
}
}
}
}
}
Hello, I changed my project, I try to explain you what I'm trying to do: I need to read every second a pin status, for this I can use this project with ajax.
It works well, but it's based in Ethernet.h library. I would like to protect it with a password, and in Arduino examples there is a good example: Webduino->web_Autentication, that it's based in Webserver.h library.
I tried to join there two sketch, but I have problems with the javascript: the text doesn't refresh automatically.
There is someone that can help me? Really thanks.