Uitgangen bedienen via webpagina en hardware drukknoppen

algemene C code
Berichten: 22
Geregistreerd: 02 Dec 2013, 05:52

Uitgangen bedienen via webpagina en hardware drukknoppen

Berichtdoor KRISKRAS9660 » 02 Dec 2013, 16:55

Dag,
ik ben nieuw met Arduino bezig.
ik heb een ethernetboard en al heel wat uitgeprobeerd.
ik heb een aantal uitgangen die ik met een druktoets op de webpagina kan activeren.
Nu lukt het me niet om bijvoorbeeld uitgang (Pin 9) ook te aktiveren met een drukttoets die bijvoorbeeld op Pin A2 (analoge pin 2) is aangesloten.

Het is de bedoeling dat ik een bepaalde uitgang via de webpagina kan activeren (dit lukt).
En diezelfde uitgang ook kan activeren via de druktoets die op de analoge ingang is aangesloten.

Hieronder vind je de code:


// Program: eth_websrv_SD_Ajax_in_out



#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60

// MAC address from Ethernet shield sticker under board
//byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x94, 0x59 }; //
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x0D, 0xC7 };
byte gateway[] = { 192, 168, 0, 1 }; //
byte subnet[] = { 255, 255, 255, 0 }; //
IPAddress ip(192, 168, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[7] = {0}; // stores the states of the LEDs

void setup()
{
// disable Ethernet chip
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);

Serial.begin(9600); // for debugging

// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");


//pin 0,1 voor RX TX

// LEDs
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
// pin 4 voor SD
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);

Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
}

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
// limit the size of the stored received HTTP request
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// 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");
// remainder of header follows below, depending on if
// web page or XML page is requested
// Ajax request - send XML file
if (StrContains(HTTP_req, "ajax_inputs")) {
// send rest of HTTP header
client.println("Content-Type: text/xml");
client.println("Connection: keep-alive");
client.println();
SetLEDs();
// send XML file containing input states
XML_response(client);
}
else { // web page request
// send rest of HTTP header
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
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)
}


// checks if received HTTP request is switching on/off LEDs
// also saves the state of the LEDs
void SetLEDs(void)
{
// LED 1 (pin 2)
if (StrContains(HTTP_req, "LED1=1")) {
LED_state[0] = 1; // save LED state
digitalWrite(2, HIGH);
}
else if (StrContains(HTTP_req, "LED1=0")) {
LED_state[0] = 0; // save LED state
digitalWrite(2, LOW);
}
// LED 2 (pin 3)
if (StrContains(HTTP_req, "LED2=1")) {
LED_state[1] = 1; // save LED state
digitalWrite(3, HIGH);
}
else if (StrContains(HTTP_req, "LED2=0")) {
LED_state[1] = 0; // save LED state
digitalWrite(3, LOW);
}
// LED 3 (pin 5)
if (StrContains(HTTP_req, "LED3=1")) {
LED_state[2] = 1; // save LED state
digitalWrite(5, HIGH);
delay (1000);
LED_state[2] = 0; // save LED state
digitalWrite(5, LOW);
}
// LED 4 (pin 6)
if (StrContains(HTTP_req, "LED4=1")) {
LED_state[3] = 1; // save LED state
digitalWrite(6, HIGH);
delay(1000);
LED_state[3] = 0; // save LED state
digitalWrite(6, LOW);
}
// LED 5 (pin 7)
if (StrContains(HTTP_req, "LED5=1")) {
LED_state[4] = 1; // save LED state
digitalWrite(7, HIGH);
delay(1000);
LED_state[4] = 0; // save LED state
digitalWrite(7, LOW);
}
// LED 6 (pin 8)
if (StrContains(HTTP_req, "LED6=1")) {
LED_state[5] = 1; // save LED state
digitalWrite(8, HIGH);
delay(1000);
LED_state[5] = 0; // save LED state
digitalWrite(8, LOW);
}
// LED 7 (pin 9)
if (StrContains(HTTP_req, "LED7=1")) {
LED_state[6] = 1; // save LED state
digitalWrite(9, HIGH);
delay(1000);
LED_state[6] = 0; // save LED state
digitalWrite(9, LOW);
}
}

// send the XML file with analog values, switch status
// and LED status
void XML_response(EthernetClient cl)
{
int analog_val; // stores value read from analog inputs
int count; // used by 'for' loops

cl.print("<?xml version = \"1.0\" ?>");
cl.print("<inputs>");
// read analog inputs
for (count = 2; count <= 5; count++) { // A2 to A5
analog_val = analogRead(count);
cl.print("<analog>");
cl.print(analog_val);
cl.println("</analog>");
}
// read switches

// checkbox LED states

// button LED states

// LED1
cl.print("<LED>");
if (LED_state[0]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
// LED4
cl.print("<LED>");
if (LED_state[1]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
// LED3
cl.print("<LED>");
if (LED_state[2]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
// LED4
cl.print("<LED>");
if (LED_state[3]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
// LED5
cl.print("<LED>");
if (LED_state[4]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
// LED6
cl.print("<LED>");
if (LED_state[5]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");
//LED7
cl.print("<LED>");
if (LED_state[6]) {
cl.print("on");
}
else {
cl.print("off");
}
cl.println("</LED>");

cl.print("</inputs>");
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length)
{
for (int i = 0; i < length; i++) {
str[i] = 0;
}
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind)
{
char found = 0;
char index = 0;
char len;

len = strlen(str);

if (strlen(sfind) > len) {
return 0;
}
while (index < len) {
if (str[index] == sfind[found]) {
found++;
if (strlen(sfind) == found) {
return 1;
}
}
else {
found = 0;
}
index++;
}

return 0;
}


Wie kan me een voorbeeldje geven hoe ik dit verder aanpak.
Alvast bedankt.
Kris

Advertisement

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor nicoverduin » 02 Dec 2013, 20:41

Kris twee zaken:
a) om de leesbaarheid van code te vergroten kun je de code tussen [ code ] en [ /code ] zetten (zonder de spaties tussen de vierkante haken). Dan krijg je dit:

Code: Alles selecteren
// Program: eth_websrv_SD_Ajax_in_out

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60

// MAC address from Ethernet shield sticker under board
//byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x94, 0x59 }; //
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x0D, 0xC7 };
byte gateway[] = { 192, 168, 0, 1 }; //
byte subnet[] = { 255, 255, 255, 0 }; //
IPAddress ip(192, 168, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = { 0 }; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[7] = { 0 }; // stores the states of the LEDs

void setup() {
   // disable Ethernet chip
   pinMode(10, OUTPUT);
   digitalWrite(10, HIGH);

   Serial.begin(9600); // for debugging

   // initialize SD card
   Serial.println("Initializing SD card...");
   if (!SD.begin(4)) {
      Serial.println("ERROR - SD card initialization failed!");
      return; // init failed
   }
   Serial.println("SUCCESS - SD card initialized.");
   // check for index.htm file
   if (!SD.exists("index.htm")) {
      Serial.println("ERROR - Can't find index.htm file!");
      return; // can't find index file
   }
   Serial.println("SUCCESS - Found index.htm file.");

   //pin 0,1 voor RX TX

   // LEDs
   pinMode(2, OUTPUT);
   pinMode(3, OUTPUT);
   // pin 4 voor SD
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(8, OUTPUT);
   pinMode(9, OUTPUT);

   Ethernet.begin(mac, ip); // initialize Ethernet device
   server.begin(); // start to listen for clients
}

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
            // limit the size of the stored received HTTP request
            // buffer first part of HTTP request in HTTP_req array (string)
            // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
            if (req_index < (REQ_BUF_SZ - 1)) {
               HTTP_req[req_index] = c; // save HTTP request character
               req_index++;
            }
            // 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");
               // remainder of header follows below, depending on if
               // web page or XML page is requested
               // Ajax request - send XML file
               if (StrContains(HTTP_req, "ajax_inputs")) {
                  // send rest of HTTP header
                  client.println("Content-Type: text/xml");
                  client.println("Connection: keep-alive");
                  client.println();
                  SetLEDs();
                  // send XML file containing input states
                  XML_response(client);
               } else { // web page request
                  // send rest of HTTP header
                  client.println("Content-Type: text/html");
                  client.println("Connection: keep-alive");
                  client.println();
                  // send web page
                  webFile = SD.open("index.htm"); // open web page file
                  if (webFile) {
                     while (webFile.available()) {
                        client.write(webFile.read()); // send web page to client
                     }
                     webFile.close();
                  }
               }
               // display received HTTP request on serial port
               Serial.print(HTTP_req);
               // reset buffer index and all buffer elements to 0
               req_index = 0;
               StrClear(HTTP_req, REQ_BUF_SZ);
               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)
}

// checks if received HTTP request is switching on/off LEDs
// also saves the state of the LEDs
void SetLEDs(void) {
   // LED 1 (pin 2)
   if (StrContains(HTTP_req, "LED1=1")) {
      LED_state[0] = 1; // save LED state
      digitalWrite(2, HIGH);
   } else if (StrContains(HTTP_req, "LED1=0")) {
      LED_state[0] = 0; // save LED state
      digitalWrite(2, LOW);
   }
   // LED 2 (pin 3)
   if (StrContains(HTTP_req, "LED2=1")) {
      LED_state[1] = 1; // save LED state
      digitalWrite(3, HIGH);
   } else if (StrContains(HTTP_req, "LED2=0")) {
      LED_state[1] = 0; // save LED state
      digitalWrite(3, LOW);
   }
   // LED 3 (pin 5)
   if (StrContains(HTTP_req, "LED3=1")) {
      LED_state[2] = 1; // save LED state
      digitalWrite(5, HIGH);
      delay(1000);
      LED_state[2] = 0; // save LED state
      digitalWrite(5, LOW);
   }
   // LED 4 (pin 6)
   if (StrContains(HTTP_req, "LED4=1")) {
      LED_state[3] = 1; // save LED state
      digitalWrite(6, HIGH);
      delay(1000);
      LED_state[3] = 0; // save LED state
      digitalWrite(6, LOW);
   }
   // LED 5 (pin 7)
   if (StrContains(HTTP_req, "LED5=1")) {
      LED_state[4] = 1; // save LED state
      digitalWrite(7, HIGH);
      delay(1000);
      LED_state[4] = 0; // save LED state
      digitalWrite(7, LOW);
   }
   // LED 6 (pin 8)
   if (StrContains(HTTP_req, "LED6=1")) {
      LED_state[5] = 1; // save LED state
      digitalWrite(8, HIGH);
      delay(1000);
      LED_state[5] = 0; // save LED state
      digitalWrite(8, LOW);
   }
   // LED 7 (pin 9)
   if (StrContains(HTTP_req, "LED7=1")) {
      LED_state[6] = 1; // save LED state
      digitalWrite(9, HIGH);
      delay(1000);
      LED_state[6] = 0; // save LED state
      digitalWrite(9, LOW);
   }
}

// send the XML file with analog values, switch status
// and LED status
void XML_response(EthernetClient cl) {
   int analog_val; // stores value read from analog inputs
   int count; // used by 'for' loops

   cl.print("<?xml version = \"1.0\" ?>");
   cl.print("<inputs>");
   // read analog inputs
   for (count = 2; count <= 5; count++) { // A2 to A5
      analog_val = analogRead(count);
      cl.print("<analog>");
      cl.print(analog_val);
      cl.println("</analog>");
   }
   // read switches

   // checkbox LED states

   // button LED states

   // LED1
   cl.print("<LED>");
   if (LED_state[0]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED4
   cl.print("<LED>");
   if (LED_state[1]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED3
   cl.print("<LED>");
   if (LED_state[2]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED4
   cl.print("<LED>");
   if (LED_state[3]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED5
   cl.print("<LED>");
   if (LED_state[4]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED6
   cl.print("<LED>");
   if (LED_state[5]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   //LED7
   cl.print("<LED>");
   if (LED_state[6]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");

   cl.print("</inputs>");
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length) {
   for (int i = 0; i < length; i++) {
      str[i] = 0;
   }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind) {
   char found = 0;
   char index = 0;
   char len;

   len = strlen(str);

   if (strlen(sfind) > len) {
      return 0;
   }
   while (index < len) {
      if (str[index] == sfind[found]) {
         found++;
         if (strlen(sfind) == found) {
            return 1;
         }
      } else {
         found = 0;
      }
      index++;
   }

   return 0;
}


Als jij een analoge poort wil gebruiken als switch dan moet je bepalen wanneer de switch aan / uit staat. Nu ga je in de loop alleen maar als Client/server aan de gang. Maar na dat deel kun je natuurlijk rustig een poort lezen en actie ondernemen.
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 22
Geregistreerd: 02 Dec 2013, 05:52

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor KRISKRAS9660 » 09 Dec 2013, 20:01

Alvast bedankt voor de info,

ik wil het eerst simpel aanpakken,
ik heb pin2 als input omschreven waar ik een drukknop aanhang en waar ik LED1 mee in of uit schakel.
Dus 1x drukken = LED1 aan - 1x drukken LED1 uit
idem via de webbrowser (dit werkt) ,er is ook een visualisatie op de webpagina of de LED1 aan of uit is).

Onder //LED1 heb ik een regel toegevoegd maar werkt niet.

Wie kan me opweg helpen om dit aan te pakken , ik ben een beginner.






Code: Alles selecteren
// Program: eth_websrv_SD_Ajax_in_out

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// size of buffer used to capture HTTP requests
#define REQ_BUF_SZ 60

// MAC address from Ethernet shield sticker under board
//byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x94, 0x59 }; //
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0E, 0x0D, 0xC7 };
byte gateway[] = { 192, 168, 0, 1 }; //
byte subnet[] = { 255, 255, 255, 0 }; //
IPAddress ip(192, 168, 0, 20); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File webFile; // the web page file on the SD card
char HTTP_req[REQ_BUF_SZ] = { 0 }; // buffered HTTP request stored as null terminated string
char req_index = 0; // index into HTTP_req buffer
boolean LED_state[7] = { 0 }; // stores the states of the LEDs

void setup() {
   // disable Ethernet chip
   pinMode(10, OUTPUT);
   digitalWrite(10, HIGH);

   Serial.begin(9600); // for debugging

   // initialize SD card
   Serial.println("Initializing SD card...");
   if (!SD.begin(4)) {
      Serial.println("ERROR - SD card initialization failed!");
      return; // init failed
   }
   Serial.println("SUCCESS - SD card initialized.");
   // check for index.htm file
   if (!SD.exists("index.htm")) {
      Serial.println("ERROR - Can't find index.htm file!");
      return; // can't find index file
   }
   Serial.println("SUCCESS - Found index.htm file.");

   //pin 0,1 voor RX TX

   // LEDs
   pinMode(2, INPUT);      //(2, OUTPUT)
   pinMode(3, OUTPUT);
   // pin 4 voor SD
   pinMode(5, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(7, OUTPUT);
   pinMode(8, OUTPUT);
   pinMode(9, OUTPUT);

   Ethernet.begin(mac, ip); // initialize Ethernet device
   server.begin(); // start to listen for clients
}

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
            // limit the size of the stored received HTTP request
            // buffer first part of HTTP request in HTTP_req array (string)
            // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
            if (req_index < (REQ_BUF_SZ - 1)) {
               HTTP_req[req_index] = c; // save HTTP request character
               req_index++;
            }
            // 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");
               // remainder of header follows below, depending on if
               // web page or XML page is requested
               // Ajax request - send XML file
               if (StrContains(HTTP_req, "ajax_inputs")) {
                  // send rest of HTTP header
                  client.println("Content-Type: text/xml");
                  client.println("Connection: keep-alive");
                  client.println();
                  SetLEDs();
                  // send XML file containing input states
                  XML_response(client);
               } else { // web page request
                  // send rest of HTTP header
                  client.println("Content-Type: text/html");
                  client.println("Connection: keep-alive");
                  client.println();
                  // send web page
                  webFile = SD.open("index.htm"); // open web page file
                  if (webFile) {
                     while (webFile.available()) {
                        client.write(webFile.read()); // send web page to client
                     }
                     webFile.close();
                  }
               }
               // display received HTTP request on serial port
               Serial.print(HTTP_req);
               // reset buffer index and all buffer elements to 0
               req_index = 0;
               StrClear(HTTP_req, REQ_BUF_SZ);
               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)
}

// checks if received HTTP request is switching on/off LEDs
// also saves the state of the LEDs
void SetLEDs(void) {

   // LED 1 (pin 2)
   //if (StrContains(HTTP_req, "LED1=1")) {

     if ((StrContains(HTTP_req, "LED1=1")) ||( pinMode,2){        //<---- deze regel is gewijzigd

      LED_state[0] = 1; // save LED state
      digitalWrite(2, HIGH);
   } else if (StrContains(HTTP_req, "LED1=0")) {
      LED_state[0] = 0; // save LED state
      digitalWrite(2, LOW);
   }
   // LED 2 (pin 3)
   if (StrContains(HTTP_req, "LED2=1")) {
      LED_state[1] = 1; // save LED state
      digitalWrite(3, HIGH);
   } else if (StrContains(HTTP_req, "LED2=0")) {
      LED_state[1] = 0; // save LED state
      digitalWrite(3, LOW);
   }
   // LED 3 (pin 5)
   if (StrContains(HTTP_req, "LED3=1")) {
      LED_state[2] = 1; // save LED state
      digitalWrite(5, HIGH);
      delay(1000);
      LED_state[2] = 0; // save LED state
      digitalWrite(5, LOW);
   }
   // LED 4 (pin 6)
   if (StrContains(HTTP_req, "LED4=1")) {
      LED_state[3] = 1; // save LED state
      digitalWrite(6, HIGH);
      delay(1000);
      LED_state[3] = 0; // save LED state
      digitalWrite(6, LOW);
   }
   // LED 5 (pin 7)
   if (StrContains(HTTP_req, "LED5=1")) {
      LED_state[4] = 1; // save LED state
      digitalWrite(7, HIGH);
      delay(1000);
      LED_state[4] = 0; // save LED state
      digitalWrite(7, LOW);
   }
   // LED 6 (pin 8)
   if (StrContains(HTTP_req, "LED6=1")) {
      LED_state[5] = 1; // save LED state
      digitalWrite(8, HIGH);
      delay(1000);
      LED_state[5] = 0; // save LED state
      digitalWrite(8, LOW);
   }
   // LED 7 (pin 9)
   if (StrContains(HTTP_req, "LED7=1")) {
      LED_state[6] = 1; // save LED state
      digitalWrite(9, HIGH);
      delay(1000);
      LED_state[6] = 0; // save LED state
      digitalWrite(9, LOW);
   }
}

// send the XML file with analog values, switch status
// and LED status
void XML_response(EthernetClient cl) {
   int analog_val; // stores value read from analog inputs
   int count; // used by 'for' loops

   cl.print("<?xml version = \"1.0\" ?>");
   cl.print("<inputs>");
   // read analog inputs
   for (count = 2; count <= 5; count++) { // A2 to A5
      analog_val = analogRead(count);
      cl.print("<analog>");
      cl.print(analog_val);
      cl.println("</analog>");
   }
   // read switches

   // checkbox LED states

   // button LED states

   // LED1
   cl.print("<LED>");
   if (LED_state[0]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED4
   cl.print("<LED>");
   if (LED_state[1]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED3
   cl.print("<LED>");
   if (LED_state[2]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED4
   cl.print("<LED>");
   if (LED_state[3]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED5
   cl.print("<LED>");
   if (LED_state[4]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   // LED6
   cl.print("<LED>");
   if (LED_state[5]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");
   //LED7
   cl.print("<LED>");
   if (LED_state[6]) {
      cl.print("on");
   } else {
      cl.print("off");
   }
   cl.println("</LED>");

   cl.print("</inputs>");
}

// sets every element of str to 0 (clears array)
void StrClear(char *str, char length) {
   for (int i = 0; i < length; i++) {
      str[i] = 0;
   }
}

// searches for the string sfind in the string str
// returns 1 if string found
// returns 0 if string not found
char StrContains(char *str, char *sfind) {
   char found = 0;
   char index = 0;
   char len;

   len = strlen(str);

   if (strlen(sfind) > len) {
      return 0;
   }
   while (index < len) {
      if (str[index] == sfind[found]) {
         found++;
         if (strlen(sfind) == found) {
            return 1;
         }
      } else {
         found = 0;
      }
      index++;
   }

   return 0;
}

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor nicoverduin » 09 Dec 2013, 22:53

als je het over deze hebt, dan geloof ik dat hij niet werkt:
Code: Alles selecteren
 if ((StrContains(HTTP_req, "LED1=1")) ||( pinMode,2){        //<---- deze regel is gewijzigd

      LED_state[0] = 1; // save LED state
      digitalWrite(2, HIGH);
} else ....


Wat is je precies je bedoeling met die (pinmode, 2)?
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 22
Geregistreerd: 02 Dec 2013, 05:52

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor KRISKRAS9660 » 10 Dec 2013, 12:31

Code: Alles selecteren
 // LED 1 (pin 2)
   //if (StrContains(HTTP_req, "LED1=1")) {                               // <---met enkel de webpagina drukknop kan ik de led bedienen

     if ((StrContains(HTTP_req, "LED1=1")) ||( digitalRead,2){        //<---- gecombineerd lukt dit niet

      LED_state[0] = 1; // save LED state
      digitalWrite(3, HIGH);
   } else if (StrContains(HTTP_req, "LED1=0")) {
      LED_state[0] = 0; // save LED state
      digitalWrite(3, LOW);
   }


Dank u voor de reactie,
de bedoeling is (dit als voorbeeld) als ik op pin 2 een drukknop plaats en deze bedien (1x drukken) dat pin3 de led gaat branden.
waneer ik pin 2 nogmaals eens bedien de led (pin3) uitgaat.
evenals als ik de drukknop op de webpagina bedien ,de led (pin3) ook aangaat of uitgaat.

Je moet het bekijken als een wisselschakeling die een led aanstuurd dat bediend wordt met een drukknop aangesloten op pin2 en een drukknop op de webpagina.

kan je me een voorbeeldje geven hoe ik dat best aanpak.

Berichten: 108
Geregistreerd: 07 Aug 2013, 21:34

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor KrisG » 10 Dec 2013, 13:20

Het if statement vraagt een uitdrukking die 'true' of 'false' is.
(digitalread,2) is dat niet.

Bekijk het voorbeeld op http://arduino.cc/en/Reference/digitalRead eens.

Het resultaat van digitalread is HIGH of LOW.
je moet dus in je IF statement testen of pin 2 HIGH of LOW is, die vergelijking kan dan als resultaat 'true' of 'false' geven.


Kris
Als je het probleem gedetailleerd genoeg omschrijft, heb je meestal al de oplossing.

Berichten: 22
Geregistreerd: 02 Dec 2013, 05:52

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor KRISKRAS9660 » 12 Dec 2013, 10:51

Code: Alles selecteren
     // LED 2 (pin 3)

     int drukknop = 0;

        drukknop = digitalRead(2);                                           // read the input pin2

        if ((StrContains(HTTP_req, "LED2=1")) || ((LED_state[1] == 0)&&(drukknop == HIGH))){         //Als de webdrukknop=1 of  als (uitgang=0 en pin2 =1)dan wordt uitgang = 1
        LED_state[1] = 1;  // save LED state
        digitalWrite(3, HIGH);
         }
    else if ((StrContains(HTTP_req, "LED2=0")) || ((LED_state[1] == 1)&&(drukknop == HIGH))){      //Als de webdrukknop=0 of  als (uitgang=1 en pin2 =1)dan wordt uitgang = 1
        LED_state[1] = 0;  // save LED state
        digitalWrite(3, LOW);
        }


Dank u voor mij op weg te helpen ,met dit stukje code is het me gelukt.
De volgende stap is om een analoge ingang te gebruiken waar de drukknop is op aangesloten.
nog eventjes expirementeren....
Thanks

Berichten: 108
Geregistreerd: 07 Aug 2013, 21:34

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor KrisG » 12 Dec 2013, 11:29

Ik weet ook niet zeker hoe je het regelt, maar als ik op een drukknop druk is die pin maar hoog tot ik de knop loslaat, als jij dan net niet 'digitalRead' op die pin doet, heb je mijn actie gemist en gebeurt er niks.

Inituitief zou ik voor drukknoppen ook de interne pullup activeren en de drukknop naar ground laten verbinden, maar dat doet niets af aan de werking. En misscien heeft iemand anders daar een andere uitleg voor :)

Kris
Als je het probleem gedetailleerd genoeg omschrijft, heb je meestal al de oplossing.

Berichten: 22
Geregistreerd: 02 Dec 2013, 05:52

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor KRISKRAS9660 » 13 Dec 2013, 10:18

Code: Alles selecteren
//Op analoge ingang 2 een drukknop aansluiten en uitgang 9 hoog maken
//als de waarde meer is dan 1000 anders is de uitgang laag
//de analoge ingang ligt met een weerstand 100k aan gnd en schakel met +5V (1023).


int AI2 = 2;     //Analoge ingang A2


void setup()


Serial.begin(9600);  //Start the serial connection with the computer                       
                     //to view the result open the serial monitor
}

void loop()                     
// run over and over again
{
//getting the value reading from the pushbutton
int ValueAI2 = analogRead(AI2);   
  {
  if (ValueAI2 < 1000){

   digitalWrite(9,LOW);}

   else{
  digitalWrite(9, HIGH);}
}
// print out the value
Serial.print(ValueAI2); Serial.println(" value"); 
 
delay(1000);                                     //waiting a second
}


Kan iemand me zeggen wat er mis is aan mijn code.
als ik 5V leg aan A2 dan wordt mijn uitgang wel hoog.
maar als de 5V weg is dan wordt de uitgang NIET laag ?
wat doe ik mis?

Berichten: 108
Geregistreerd: 07 Aug 2013, 21:34

Re: Uitgangen bedienen via webpagina en hardware drukknoppe

Berichtdoor KrisG » 13 Dec 2013, 11:10

verandert ValueAI2 wel ?

Kris
Als je het probleem gedetailleerd genoeg omschrijft, heb je meestal al de oplossing.

Volgende

Terug naar C code

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 11 gasten