Go Down

Topic: How to combine 2 codes? (Read 806 times) previous topic - next topic

LeandroTo

Hi, I would like to learn Arduino language well, but at this time do not give me the time,
I need to combine these two codes. This down below the post....
Code: [Select]
#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
#include <Wire.h>


#define DHT11PIN 2


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 20 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };


EthernetServer server(8246);

dht11 DHT11;



void setup()   
{
 
  Serial.begin(9600);
  while (!Serial) {
    ;
  }


 
  Ethernet.begin(mac, ip,gateway,subnet);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


void loop()   
{
 
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
   
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
       
        if (c == '\n' && currentLineIsBlank) {
         
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
         
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.print("Temperatura, Humedad y Rocio");   
          client.println("<br />");   

         
          int chk = DHT11.read(DHT11PIN);

          Serial.print("Leyendo sensor: ");
          switch (chk)
          {
          case 0:
            Serial.println("OK");
            break;
          case -1:
            Serial.println("Checksum error");
            break;
          case -2:
            Serial.println("Time out error");
            break;
          default:
            Serial.println("Unknown error");
            break;
          } 

          client.print("Temperatura (C): ");
          client.println((float)DHT11.temperature, 1); 
          client.println("<br />"); 
         
          client.print("Temperatura (F): ");
          client.println(Fahrenheit(DHT11.temperature), 1);
          client.println("<br />"); 
         
          client.print("Humedad (%): ");
          client.println((float)DHT11.humidity, 0); 
          client.println("<br />");   

          client.print("Temperatura (K): ");
          client.println(Kelvin(DHT11.temperature), 1);
          client.println("<br />");   

          client.print("Punto De Rocio Normal (C): ");
          client.println(dewPoint(DHT11.temperature, DHT11.humidity));
          client.println("<br />");   

          client.print("Punto De Rocio Rapido (C): ");
          client.println(dewPointFast(DHT11.temperature, DHT11.humidity));
          client.println("<br />");   


          client.println("</html>");
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
         currentLineIsBlank = false;
        }
      }
    }
   
    delay(1);
   
    client.stop();
    Serial.println("client disonnected");
  }
}
double Fahrenheit(double celsius)
{
  return 1.8 * celsius + 32;
}


double Kelvin(double celsius)
{
  return celsius + 273.15;
}


double dewPoint(double celsius, double humidity)
{
  double A0= 373.15/(273.15 + celsius);
  double SUM = -7.90298 * (A0-1);
  SUM += 5.02808 * log10(A0);
  SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
  SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
  SUM += log10(1013.246);
  double VP = pow(10, SUM-3) * humidity;
  double T = log(VP/0.61078);   // temp var
  return (241.88 * T) / (17.558-T);
}


double dewPointFast(double celsius, double humidity)
{
  double a = 17.271;
  double b = 237.7;
  double temp = (a * celsius) / (b + celsius) + log(humidity/100);
  double Td = (b * temp) / (a - temp);
  return Td;
}

holmes4

The way to do this is as follows,

1. fully understand the first program

2. fully understand the second program

3. work out what it is you want

4. write the new code

Mark

LeandroTo

Hi, What I'm saying is that if someone kind I could combine the two codes

PaulS

Quote
What I'm saying is that if someone kind I could combine the two codes

To do what? You need to define what the resulting program is supposed to do.

Quote
but at this time do not give me the time,

Why not? When is our homework due? Why didn't we start earlier?

CrossRoads

Combine it the usual way:
stuff that goes before void setup
stuff that goes in void setup
stuff that goes in void loop
functions that are outside of setup & loop stay outside loop.

Get rid of duplicate calls, resolve duplicate pin assignments, resolve any duplicate variable names.
Then debug as needed.

So maybe something like this as a starting point:
Code: [Select]

#include <Ethernet.h>
#include <SPI.h>
#include <avr/pgmspace.h>

//#include <SPI.h>
//#include <Ethernet.h>
#include <dht11.h>
#include <Wire.h>


#define DHT11PIN 2

prog_char string_0[] PROGMEM = "<html><body><h2>Casa Domotica Con Sensor De Temperatura Y Humedad</h2><font size= 4><form method=GET>";
prog_char string_1[] PROGMEM = "<br><input type=submit name=b1 value=Led1>";
prog_char string_2[] PROGMEM = "<br><input type=submit name=b2 value=Led2>";
prog_char string_3[] PROGMEM = "<br><input type=submit name=b3 value=Led3>";
prog_char string_4[] PROGMEM = "<br><input type=submit name=b4 value=Led4>";
prog_char string_5[] PROGMEM = "";  //"<br>Insert your name here:";
prog_char string_6[] PROGMEM = "";  //"<input name=msg value=no_name MAXLENGTH=20>";
prog_char string_7[] PROGMEM = "</form></body></html>";
prog_char string_8[] PROGMEM = "Encendido (ON)";
prog_char string_9[] PROGMEM = "Apagado (OFF)";

prog_char string_10[] PROGMEM = "<meta http-equiv=refresh content=30 > "; //for auto refresh

PROGMEM const char *string_table[] =     // change "string_table" name to suit
{  
 string_0,
 string_1,
 string_2,
 string_3,
 string_4,
 string_5,
 string_6,
 string_7,
 string_8,
 string_9,
 string_10
};

char buffer[85];    // make sure this is large enough for the largest string it must hold


byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 20 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };

String inString = String(35);

EthernetServer server(8246);


boolean led1 = false;
boolean led2 = false;
boolean led3 = false;
boolean led4 = false;

String msg="";
int tam=0;
int st1=9,st2=9,st3=9,st4=9;




//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//byte ip[] = { 192, 168, 1, 20 };
//byte gateway[] = { 192, 168, 1, 1 };
//byte subnet[] = { 255, 255, 255, 0 };


EthernetServer server(8246);

dht11 DHT11;



void setup()
{
 Serial.begin(9600);
 Ethernet.begin(mac, ip,gateway,subnet);
 server.begin();
 Serial.println("Serial READY");
 Serial.println("Ethernet READY");
 Serial.println("Server READY");
 pinMode(4,OUTPUT);
 pinMode(5,OUTPUT);
 pinMode(6,OUTPUT);
 pinMode(7,OUTPUT);
 pinMode(8,INPUT);
 pinMode(9,INPUT);

//Serial.begin(9600);
while (!Serial) {
;
}



//Ethernet.begin(mac, ip,gateway,subnet);
//server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}

void loop()
{

 EthernetClient client = server.available();

 int led=0;
 if (client) {
 
   // an http request ends with a blank line
   boolean current_line_is_blank = true;
   while (client.connected()) {
   
     if (client.available()) {
   
       char c = client.read();
       // if we've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so we can send a reply
       if (inString.length() < 35) {
           inString.concat(c);
         
        }
       if (c == '\n' && current_line_is_blank) {
       
         if(inString.indexOf("b1")>0){
           
            if(led1==false){
              st1=8;
              led1=true;
              digitalWrite(4,HIGH);
            }
            else{
              st1=9;
              led1=false;
              digitalWrite(4,LOW);
            }
            led=1;
           
          }
          if(inString.indexOf("b2")>0){
           
            if(led2==false){
              st2=8;
              led2=true;
              digitalWrite(5,HIGH);
            }
            else{
              st2=9;
              led2=false;
              digitalWrite(5,LOW);
            }
            led=2;
           
          }
          if(inString.indexOf("b3")>0){
           
            if(led3==false){
              st3=8;
              led3=true;
              digitalWrite(6,HIGH);
            }
            else{
              st3=9;
              led3=false;
              digitalWrite(6,LOW);
            }
            led=3;
           
          }
          if(inString.indexOf("b4")>0){
           
            if(led4==false){
              st4=8;
              led4=true;
              digitalWrite(7,HIGH);
            }
            else{
              st4=9;
              led4=false;
              digitalWrite(7,LOW);
            }
            led=4;
           
          }
          /*
          if(inString.indexOf("msg")>0){
             char charBuf1[50];
             char charBuf2[50];
             strcpy(msg,(char*)inString.substring(inString.indexOf("g")+2,inString.indexOf(" H")));                        
             //Serial.print("msg: ");
             Serial.println(msg);
          }
        */
         // send a standard http response header
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println();

         strcpy_P(buffer, (char*)pgm_read_word(&(string_table[0]))); // Necessary casts and dereferencing, just copy.
         client.println( buffer );
         for (int i = 1; i < 8; i++)
         {
           strcpy_P(buffer, (char*)pgm_read_word(&(string_table[i]))); // Necessary casts and dereferencing, just copy.
           client.println( buffer );
           switch(i){
           
             case 1: strcpy_P(buffer, (char*)pgm_read_word(&(string_table[st1]))); client.println( buffer ); break;
             case 2: strcpy_P(buffer, (char*)pgm_read_word(&(string_table[st2]))); client.println( buffer ); break;
             case 3: strcpy_P(buffer, (char*)pgm_read_word(&(string_table[st3]))); client.println( buffer ); break;
             case 4: strcpy_P(buffer, (char*)pgm_read_word(&(string_table[st4]))); client.println( buffer ); break;
           
           }
           delay(30);
         }
         if(digitalRead(8)==HIGH){
           client.println("<br>Botao 1, ON");
         }else{
           client.println("<br>Botao 1, OFF");
         }
         if(digitalRead(9)==HIGH){
           client.println("<br>Botao 2, ON");
         }else{
           client.println("<br>Botao 2, OFF");
         }
       
        //strcpy_P(buffer, (char*)pgm_read_word(&(string_table[10]))); client.println( buffer );
 
             
         break;
       }
       if (c == '\n') {
         // we're starting a new line
         current_line_is_blank = true;
       } else if (c != '\r') {
         // we've gotten a character on the current line
         current_line_is_blank = false;
       }
     }
   }
   // give the web browser time to receive the data
   delay(1);
   inString = "";
   client.stop();
 }
EthernetClient client = server.available();
if (client) {
Serial.println("new client");

boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);

if (c == '\n' && currentLineIsBlank) {

client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");

client.println("<meta http-equiv=\"refresh\" content=\"5\">");
client.print("Temperatura, Humedad y Rocio");
client.println("<br />");


int chk = DHT11.read(DHT11PIN);

Serial.print("Leyendo sensor: ");
switch (chk)
{
case 0:
Serial.println("OK");
break;
case -1:
Serial.println("Checksum error");
break;
case -2:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}

client.print("Temperatura (C): ");
client.println((float)DHT11.temperature, 1);
client.println("<br />");

client.print("Temperatura (F): ");
client.println(Fahrenheit(DHT11.temperature), 1);
client.println("<br />");

client.print("Humedad (%): ");
client.println((float)DHT11.humidity, 0);
client.println("<br />");

client.print("Temperatura (K): ");
client.println(Kelvin(DHT11.temperature), 1);
client.println("<br />");

client.print("Punto De Rocio Normal (C): ");
client.println(dewPoint(DHT11.temperature, DHT11.humidity));
client.println("<br />");

client.print("Punto De Rocio Rapido (C): ");
client.println(dewPointFast(DHT11.temperature, DHT11.humidity));
client.println("<br />");


client.println("</html>");
break;
}
if (c == '\n') {
currentLineIsBlank = true;
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}

delay(1);

client.stop();
Serial.println("client disonnected");
}
}

// functions left out to save some space
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

LeandroTo

Hi, I have no time because I am a high school student.
What I do is a home automation with LEDs and sensor controlled temperature and humidity right through the ethernet

UKHeliBob

If this is a personal project then why do you have a deadline ?  Take your time and follow the advice given by holmes4 and CrossRoads.
If this is a school assignment then there probably is a deadline but you are supposed to work out the solution for yourself, or at least try to.

In either case you need to define what the combined program should do.  They cannot just be combined with no more detail as to what is required.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

alnath


Hi, I have no time because I am a high school student.
What I do is a home automation with LEDs and sensor controlled temperature and humidity right through the ethernet

XD
Holmes4 an Crossroads told you everything you need to know (actually, Crossroads did much  more than that ;) ) - It won't take you much time to combine the 2 parts.
But, as UKHeliBob says, it will likely be more complicated than that. Even if the two parts work when on their own, they probably won't work 100% together. This debugging part will certainly be the hardest, and you need to know exactly what you want your project to do. A sketch which compiles without error is necessary but not enough, it doesn't mean it does what you want it to do, and I'm afraid you'll need time anyway.

LeandroTo

What I do is a home automation with LEDs and sensor controlled temperature and humidity right through the ethernet.
I want you when you enter the ip see as this temperature and humidity and control the LEDs through the page.

PaulS

Quote
What I do is a home automation with LEDs and sensor controlled temperature and humidity right through the ethernet.
I want you when you enter the ip see as this temperature and humidity and control the LEDs through the page.

Yeah, we got that. What have YOU tried?


LeandroTo

Hello, I already know how combined but How do I appear in a temperature and humidity display and the other to control the LEDs

zoomkat

Code that displays the analog input values of the arduino board.

Code: [Select]

// zoomkat's meta refresh data frame test page 5/25/13
// use http://192.168.1.102:84 in your brouser for main page
// http://192.168.1.102:84/data static data page
// http://192.168.1.102:84/datastart meta refresh data page
// for use with W5100 based ethernet shields
// set the refresh rate to 0 for fastest update
// use STOP for single data updates

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

const int analogInPin0 = A0;
const int analogInPin1 = A1;
const int analogInPin2 = A2;
const int analogInPin3 = A3;
const int analogInPin4 = A4;
const int analogInPin5 = A5;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // arduino 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
unsigned long int x=0; //set refresh counter to 0
String readString;

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

void setup(){
  Serial.begin(9600);
    // disable SD SPI if memory card in the uSD slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("meta refresh data frame test 5/25/13"); // so I can keep track of what is loaded
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
         if (readString.length() < 100) {
          readString += c;
         }
        //check if HTTP request has ended
        if (c == '\n') {

          //check get atring received
          Serial.println(readString);

          //output HTML data header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          //generate data page
          if(readString.indexOf("data") >0) {  //checks for "data" page
            x=x+1; //page upload counter
            client.print("<HTML><HEAD>");
            //meta-refresh page every 1 seconds if "datastart" page
            if(readString.indexOf("datastart") >0) client.print("<meta http-equiv='refresh' content='1'>");
            //meta-refresh 0 for fast data
            if(readString.indexOf("datafast") >0) client.print("<meta http-equiv='refresh' content='0'>");
            client.print("<title>Zoomkat's meta-refresh test</title></head><BODY><br>");
            client.print("page refresh number: ");
            client.print(x); //current refresh count
            client.print("<br><br>");
           
              //output the value of each analog input pin
            client.print("analog input0 is: ");
            client.print(analogRead(analogInPin0));
           
            client.print("<br>analog input1 is: ");
            client.print(analogRead(analogInPin1));
                       
            client.print("<br>analog input2 is: ");
            client.print(analogRead(analogInPin2));
           
            client.print("<br>analog input3 is: ");
            client.print(analogRead(analogInPin3));
                                   
            client.print("<br>analog input4 is: ");
            client.print(analogRead(analogInPin4));
           
            client.print("<br>analog input5 is: ");
            client.print(analogRead(analogInPin5));
            client.println("<br></BODY></HTML>");
           }
          //generate main page with iframe
          else
          {
            client.print("<HTML><HEAD><TITLE>Zoomkat's frame refresh test</TITLE></HEAD>");
            client.print("Zoomkat's Arduino frame meta refresh test 5/25/13");
            client.print("<BR><BR>Arduino analog input data frame:<BR>");
            client.print("&nbsp;&nbsp;<a href='http://192.168.1.102:84/datastart' target='DataBox' title=''yy''>META-REFRESH</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/data' target='DataBox' title=''xx''>SINGLE-STOP</a>");
            client.print("&nbsp;&nbsp;&nbsp;&nbsp;<a href='http://192.168.1.102:84/datafast' target='DataBox' title=''zz''>FAST-DATA</a><BR>");
            client.print("<iframe src='http://192.168.1.102:84/data' width='350' height='250' name='DataBox'>");
            client.print("</iframe><BR></HTML>");
          }
          delay(1);
          //stopping client
          client.stop();
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up