Goedendag,
Dit is mijn eerste vraag op dit forum, omdat ik al een tijdje aan het proberen ben om via een knop in een webbrowser, een relais welke aangesloten zit op de arduino uno, aan te sturen. Nu lukt me dit wel, echter de knop (in de webbrowser) blijft in gedrukt en daardoor blijft ook het relais bekrachtigd.
Wat ik wil is dat ik via een webbrowser een knop in druk m.b.v. de muis, dat de tekst op de knop verandert naar "Uit", er een relais via een arduino uno bekrachtigd wordt en dat na 100 ms de knop in de webbrowser automatisch "los gelaten"wordt en daarmee ook de aansturing van het relais.
Kan iemand mij een schop in de goede richting geven?
/*
- John
*/
#include <SPI.h> // Initialize the Serial Peripheral Interface library.
#include <Ethernet.h> // Initialize the Ethernet server library
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0E, 0xCD, 0x5F }; // Enter a MAC address and IP address for your controller below.
IPAddress ip(192,168,2, 150); // The IP address will be dependent on your local network:
EthernetServer server(80); // Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
String relay1State = "Uit"; // Relay 1 state and pin
const int relay1 = 4;
String relay2State = "Uit"; // Relay 2 state and pin
const int relay2 = 5;
char linebuf[80]; // Client variables
int charcount=0;
void setup() {
pinMode(relay1, OUTPUT); // Relay 1 module prepared
digitalWrite(relay1, HIGH);
pinMode(relay2, OUTPUT); // Relay 2 module prepared
digitalWrite(relay2, HIGH);
Serial.begin(9600); // Open serial communication at a baud rate of 9600
Ethernet.begin(mac, ip); // start the Ethernet connection and the server:
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
// Display dashboard page with on/off button for relay
void dashboardPage(EthernetClient &client) {
client.println("");
client.println("<meta name="viewport" content="width=device-width, initial-scale=1">");
client.println("
Garagedeur openen via IntraNet - <a href="/">Refresh
");client.println("
Relais 1 - Status: " + relay1State + "
"); // Genereert de knoppen Aan/Uit van relais 1if(relay1State == "Uit"){ // Als relais 1 aangestuurd wordt, staat er op de knop "Aan"
client.println("<a href="/relay1on">Aan");
}
else if(relay1State == "Aan"){ // Als relais 1 NIET aangestuurd wordt, staat er op de knop "Uit"
client.println("<a href="/relay1off">Uit");
}
client.println("
Relais 2 - Status: " + relay2State + "
"); // Genereert de knoppen Aan/Uit van relais 2if(relay2State == "Uit"){ // Als relais 2 aangestuurd wordt, staat er op de knop "Aan"
client.println("<a href="/relay2on">Aan");
}
else if(relay2State == "Aan"){ // Als relais 2 NIET aangestuurd wordt, staat er op de knop "Uit"
client.println("<a href="/relay2off">Uit");
}
client.println("");
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
memset(linebuf,0,sizeof(linebuf));
charcount=0;
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
linebuf[charcount]=c;
if (charcount<sizeof(linebuf)-1) charcount++;
// 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) {
dashboardPage(client);
break;
}
if (c == '\n') {
if (strstr(linebuf,"GET /relay1off") > 0){
digitalWrite(relay1, HIGH);
relay1State = "Uit";
}
if (strstr(linebuf,"GET /relay1on") > 0){
digitalWrite(relay1, LOW);
relay1State = "Aan";
}
if (strstr(linebuf,"GET /relay2off") > 0){
digitalWrite(relay2, HIGH);
relay2State = "Uit";
}
else if (strstr(linebuf,"GET /relay2on") > 0){
digitalWrite(relay2, LOW);
relay2State = "Aan";
}
// you're starting a new line
currentLineIsBlank = true;
memset(linebuf,0,sizeof(linebuf));
charcount=0;
}
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 disonnected");
}
}
Bij voorbaat dank,
John