Pages: [1]   Go Down
Author Topic: merge 2 code together. Webserver and function()  (Read 328 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

I'm not a good programmer. I'm a beginner only.

I have 1 question. I made a code :
Code:
int cap1 = 2;
int cap2 = 3;
int cap3 = 4;
int cap4 = 5;
int cod1 = 9;
int cod2 = 10;
int cod3 = 11;
int cod4 = 12;
int cod5 = 13;


void setup (){
  pinMode(cap1, INPUT_PULLUP);
  pinMode(cap2, INPUT_PULLUP);
  pinMode(cap3, INPUT_PULLUP);
  pinMode(cap4, INPUT_PULLUP);
  pinMode(cod1, OUTPUT);
  pinMode(cod2, OUTPUT);
  pinMode(cod3, OUTPUT);
  pinMode(cod4, OUTPUT);
  pinMode(cod5, OUTPUT);

  Serial.begin(9600);
}
void loop (){

 
fonction1();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);


fonction2();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);


fonction3();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);


   
}
 
 
void fonction1 (){
 
  int capteur1 = digitalRead(cap1);
 
  digitalWrite(cod2, HIGH);
  digitalWrite(cod4, HIGH);
 
 
 if (capteur1 == HIGH){
  Serial.println("S1 = 50%");
  }
  else{
  Serial.println("S1 = 100%");
  }
  delay(1);
 
}


void fonction2 (){
 
  int capteur2 = digitalRead(cap2);
 
  digitalWrite(cod1, HIGH);
  digitalWrite(cod3, HIGH);

 
 
  if (capteur2 == HIGH){
  Serial.println("S2 = 50%");
  }
  else{
  Serial.println("S2 = 100%");
  }
  delay(1);
 
 
}



void fonction3 (){
 
  int capteur3 = digitalRead(cap4);
 
  digitalWrite(cod1, HIGH);
  digitalWrite(cod5, HIGH);

 
 
  if (capteur3 == HIGH){
  Serial.println("S3 = 50%");
  }
  else{
  Serial.println("S3 = 100%");
  }
  delay(1);
 
 
}


And I want that Serial.println switch to client.println. Wich is viewable by a webpage.


I have this part of a code that I found. This is I think the basic lines of a webserver html page.

Code:
#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80



void setup()
{





    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
                // 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: close");
                    client.println("Refresh: 2");
                    client.println();
                    // envoyerla page web en ''PRINT''
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Serveur 1.0</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1><center>Serveur V1.0</center></h1>");
                   
// maybe the client.println here ??

                    client.println("</body>");
                    client.println("</html>");
                    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)
}
}


Could I merge them togheter???



thank you everybody.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried some sketch but it never works when I have mutiple function in my webserver.


void fonction1()
void fonction2()
void fonction3()




Code:
#include <SPI.h>
#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 20); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

int cap1 = 2;
int cap2 = 3;
int cap3 = 4;
int cap4 = 5;
int cod1 = 9;
int cod2 = 10;
int cod3 = 11;
int cod4 = 12;
int cod5 = 13;



void setup()
{

 pinMode(cap1, INPUT_PULLUP);
  pinMode(cap2, INPUT_PULLUP);
  pinMode(cap3, INPUT_PULLUP);
  pinMode(cap4, INPUT_PULLUP);
  pinMode(cod1, OUTPUT);
  pinMode(cod2, OUTPUT);
  pinMode(cod3, OUTPUT);
  pinMode(cod4, OUTPUT);
  pinMode(cod5, OUTPUT);

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




void loop ()
{
 
 
fonction1();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);


fonction2();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);


fonction3();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);



{
    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
                // 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: close");
                    client.println("Refresh: 2");
                    client.println();
                    // envoyerla page web en ''PRINT''
                    client.println("<!DOCTYPE html>");
                    client.println("<html>");
                    client.println("<head>");
                    client.println("<title>Serveur 1.0</title>");
                    client.println("</head>");
                    client.println("<body>");
                    client.println("<h1><center>Serveur V1.0</center></h1>");
                   

void fonction1 (){
 
  int capteur1 = digitalRead(cap1);
 
  digitalWrite(cod2, HIGH);
  digitalWrite(cod4, HIGH);
 
 
 if (capteur1 == HIGH){
  client.println("S1 = 50%");
  }
  else{
  client.println("S1 = 100%");
  }
  delay(1);
 
}


void fonction2 (){
 
  int capteur2 = digitalRead(cap2);
 
  digitalWrite(cod1, HIGH);
  digitalWrite(cod3, HIGH);

 
 
  if (capteur2 == HIGH){
  client.println("S2 = 50%");
  }
  else{
  client.println("S2 = 100%");
  }
  delay(1);
 
 
}



void fonction3 (){
 
  int capteur3 = digitalRead(cap4);
 
  digitalWrite(cod1, HIGH);
  digitalWrite(cod5, HIGH);

 
 
  if (capteur3 == HIGH){
  client.println("S3 = 50%");
  }
  else{
  client.println("S3 = 100%");
  }
  delay(1);
 

                    client.println("</body>");
                    client.println("</html>");
                    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)
}
}
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not sure just what you are trying to do, but below is some combined client/server code.

Code:
//zoomkat 7-03-12, combined client and server
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor and send an g to test client and
//see what the arduino client/server receives
//web page buttons make pin 5 high/low
//use the ' in html instead of " to prevent having to escape the "
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// http://code.google.com/p/arduino/issues/detail?id=605

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //assign arduino mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan assigned to arduino
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port arduino server will use
EthernetClient client;
char serverName[] = "web.comporium.net"; // (DNS) zoomkat's test web page server
//byte serverName[] = { 208, 104, 2, 86 }; // (IP) zoomkat web page server IP address

String readString; //used by server to capture GET request

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin 5 selected to control
  Ethernet.begin(mac,ip,gateway,gateway,subnet);
  server.begin();
  Serial.begin(9600);
  Serial.println("server/client 1.0 test 7/03/12"); // keep track of what is loaded
  Serial.println("Send an g in serial monitor to test client"); // what to do to test client
}

void loop(){
  // check for serial input
  if (Serial.available() > 0)
  {
    byte inChar;
    inChar = Serial.read();
    if(inChar == 'g')
    {
      sendGET(); // call client sendGET function
    }
  } 

  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 on browser request
            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 1.0 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 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 5 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

//////////////////////////
void sendGET() //client function to send and receive GET data from external server.
{
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.println("GET /~shb/arduino.txt HTTP/1.1"); //download text
    client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection 
    client.println(); //end of get request
  }
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();

}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

HI,

I'm working on an arduino project. I'm looking for write on a webpage the state of 3 function.


For exemple, when the output2 and the output3 are on, tell me if the input1 or input2 or input3 is ON. then write on the webpage the value.


It's hard for me to explain that. But I want to print what i see in the monitor on a web page. I already made it with a simplest sketch. But I don't know why that not work with the new sketch I shown you.

The only different thing is the 3 chunk of ''void fonction1()'' 'void fonction2()'' 'void fonction3()''
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Test that with your arduino, you will probably undestand !!!


Code:
// Take a wire, ground the input pin ''cap1 to cap4'' Take a look on ''fonction1() to fonction3() you will see that
// the leds (cod1 to cod5) are blinking. And print to the monitor a specific text depending on wich input is grounded.
int cap1 = 2;
int cap2 = 3;
int cap3 = 4;
int cap4 = 5;
int cod1 = 9;
int cod2 = 10;
int cod3 = 11;
int cod4 = 12;
int cod5 = 13;


void setup (){
  pinMode(cap1, INPUT_PULLUP);
  pinMode(cap2, INPUT_PULLUP);
  pinMode(cap3, INPUT_PULLUP);
  pinMode(cap4, INPUT_PULLUP);
  pinMode(cod1, OUTPUT);
  pinMode(cod2, OUTPUT);
  pinMode(cod3, OUTPUT);
  pinMode(cod4, OUTPUT);
  pinMode(cod5, OUTPUT);

  Serial.begin(9600);
}
void loop (){

 
fonction1();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);


fonction2();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);


fonction3();
delay(2000);
digitalWrite(cod1, LOW);
digitalWrite(cod2, LOW);
digitalWrite(cod3, LOW);
digitalWrite(cod4, LOW);
digitalWrite(cod5, LOW);


   
}
 
 
void fonction1 (){
 
  int capteur1 = digitalRead(cap1);
 
  digitalWrite(cod2, HIGH);
  digitalWrite(cod4, HIGH);
 
 
 if (capteur1 == HIGH){
  Serial.println("S1 = 50%");
  }
  else{
  Serial.println("S1 = 100%");
  }
  delay(1);
 
}


void fonction2 (){
 
  int capteur2 = digitalRead(cap2);
 
  digitalWrite(cod1, HIGH);
  digitalWrite(cod3, HIGH);

 
 
  if (capteur2 == HIGH){
  Serial.println("S2 = 50%");
  }
  else{
  Serial.println("S2 = 100%");
  }
  delay(1);
 
 
}



void fonction3 (){
 
  int capteur3 = digitalRead(cap4);
 
  digitalWrite(cod1, HIGH);
  digitalWrite(cod5, HIGH);

 
 
  if (capteur3 == HIGH){
  Serial.println("S3 = 50%");
  }
  else{
  Serial.println("S3 = 100%");
  }
  delay(1);
 
 
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int cap1 = 2;
int cap2 = 3;
int cap3 = 4;
int cap4 = 5;
int cod1 = 9;
int cod2 = 10;
int cod3 = 11;
int cod4 = 12;
int cod5 = 13;
Aren't arrays wonderful. So much less code needed.

Code:
fonction1();
Have you looked at the reference page? Did you see any functions with stupid names like these? The function does something. The name should reflect that.

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int cap[] = {2, 3, 4, 5};
int cod[] = {9, 10, 11, 12, 13};


Looks better?




not sure what you are trying to sy with the function.



fonction1() was just a name called from the void loop()...
What is your point?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What is your point?
My point is that names that don't mean anything usually indicate that you don't understand the code. If you don't understand the code, you'll have a hard time merging the two bits of code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

did you take a look on the complet code?

The code is working, Did you test it?


fonction1 was called from the void loop().

I can rename it for you if you don't love the french word;)





As i said, i'm not a expert, I'm a beginner.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is an exemple taht I saw from the reference pages. I think is the same thing I made in my sketch.



void loop() {

  while (digitalRead(buttonPin) == HIGH) {
    calibrate();
  }


void calibrate() {
 }




Maybe I'm wrong...
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 65
Posts: 3638
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The difference is simply that calibrate tells you something about what the function does. It's not ideal, I'd prefer that the name tells you what it's calibrating, but at least it's a clue. Fonction1 on the other hand tells you nothing. Worse, it implies that you're going to have Fonction2 and Fonction3 later on. You may be able to keep what they're for in your head, but why bother when you can simply give each function a sensible name that gives you that information.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

okay I'll keep in mind. So I will change the name for something more significant.


So, What should I do to change the Serial.println to client.print.

If there is no other function after the void loop it works.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, What should I do to change the Serial.println to client.print.
Use a text editor.

Quote
If there is no other function after the void loop it works.
void is a return type. If is NOT part of the function name. Stop using "void loop". Use "loop".

You need to explain this statement. Putting more functions after loop() does not affect the behavior of loop(), unless the extra functions cause memory problems, like using all of it and then some.
Logged

Pages: [1]   Go Up
Jump to: