using code alone works fine ..adding to another sketch fails

Hi there i have been looking through this site for a while now ..very informative yet i cant seem to find an answer to my predicament.I am using a php file to obtain the forecast temp for singapore for tomorrow. Which works fine. it displays on my localhost webpage as eg. <30>
If i use the following sketch my arduino can read it.
But if i use that sketch in my main sketch it doesnt want to connect or will say its connected and then not show the data or say there is no data. Ive tried various ways as making the "dataget" sketch a seperate function and calling it and i have place it within the loop and setup as it is originally made. Whichever way i cant seem to get it to work. I am quite new at this and plod along albiet by trial and error. But any help would be appreciated in informing me how i would go about this. I am not sure whether you would require both sketches. Ill post the dataget one and if needed ill post the other which is much longer. Thankyou

//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>

////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte server[] = { 192,168,0,2 }; //ip Address of the server you will connect to

//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/index1.php HTTP/1.0";


// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
////////////////////////////////////////////////////////////////////////

EthernetClient client;
int convertedtemp;
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
String responseString;
void setup(){
  delay( 100 ); 
  Ethernet.begin(mac);
  Serial.begin(9600);
}

void loop(){
  String pageValue = connectAndRead(); //connect to the server and read the output

  Serial.println(pageValue); //print out the findings.

  delay(5000); //wait 5 seconds before connecting again
}

String connectAndRead(){
  //connect to the server

  Serial.println("connecting...");

  //port 80 is typical of a www page
  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.print("GET ");
    client.println(location);
    client.println();

    //Connected - Read the page
    return readPage(); //go and read the output

  }else{
    return "connection failed";
  }

}

String readPage(){
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '<' ) { //'<' is our begining character
        startRead = true; //Ready to start reading the part 
      }else if(startRead){

        if(c != '>'){ //'>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }else{
          //got what we need here! We can disconnect now
          startRead = false;
          //Serial.print(inString);
          //convertedtemp = atoi(inString).c_str());
          int val = atoi(inString);
          //int convertedhumid = atoi(getValuesFromKey(responseString, "relative_humidity").c_str());
          Serial.println(val);
          client.stop();
          client.flush();
                  
                   Serial.println("disconnecting.");
          //return inString;
//return ;
        }

      }
    }

  }

}

Not much point showing the sketch that works without also showing the sketch that fails.

...R

that brings us to a question of the best way to post a long sketch. spliting it into two posts is a pita.....

Part 1 of code

/******************* CONSTANTS AND VARIABLES *******************
****************************************************************/
//INCLUDES LIBRARIES

#include <Time.h>  
#include <TimeLord.h>
#include <Wire.h>       // Needed for I2C communication
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t
#include <DS3231.h>
#include "rtc_ds3231.h"
#include <LiquidCrystal.h>
#include <dht.h>
#include <DS3231.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

///RELAYS and pins for relays
#define LIGHTS 10 //Digital pin for relay/lights RELAY 1
#define FAN 6  // pin for 12 volt fan air circulation
#define PUMP 5 //Pin for pump for tree
#define MISTER 4 // Fogger


//INPUTS
#define dht_dpin A0 //no ; here. TEMP PIN
dht DHT;
#define DS1307_ADDRESS 0x68 // address of RTC screen
#define DEBUG 1    //Set to 1 to enable debug messages through serial port monitor
#define LIGHTS_ON 0
#define LIGHTS_OFF 1
//#define WATER_ON 0
//#define WATER_OFF 1
// LCD STUFF
#define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
LiquidCrystal_I2C	lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
DS3231 Clock;
bool Century=false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
//byte yearlcd, monthlcd, date, DoW, hourlcd, minutelcd, secondlcd;


int decPlaces = 0;
int tempSensor = A3;
int n = 1;
int temp_val = 0;

//SETTINGS FOR TEMP RELAY
int LowTempOn = 14;                   // Low Temperature On setting
int LowTempOff = 24;                  // Low Temperature Off setting
int HighTempOn = 29;                 // High Temperature On setting
int HighTempOff = 25;                // High Temperature Off setting

//SETTINGS FOTR TIME
const int TIMEZONE = 10; //MST
const float LATITUDE = -34.92866, LONGITUDE = 138.59863; // set your position here

TimeLord myLord; // TimeLord Object, Global variable
byte sunTime[]  = {0, 0, 0, 1, 1, 13}; // 17 Oct 2013
int minNow, minLast = -1, hourNow, hourLast = -1, minOfDay; //time parts to trigger various actions.     // -1 init so hour/min last inequality is triggered the first time around
int mSunrise, mSunset; //sunrise and sunset expressed as minute of day (0-1439)





/************************ ARDUINO SETUP  ***********************
****************************************************************/
void setup()  {
  Serial.begin(9600);
  while (!Serial) ; // Needed for Leonardo only
  digitalWrite(LIGHTS ,LIGHTS_OFF);
  pinMode(LIGHTS, OUTPUT);
 //digitalWrite(WATER ,WATER_OFF);
  //pinMode(WATER, OUTPUT);
  // Start the I2C interface
	Wire.begin();
lcd.begin (16,2); //  <<----- My LCD was 16x2
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home


 
  
    
  // TimeLord Object Initialization //
  myLord.TimeZone(TIMEZONE * 60);
  myLord.Position(LATITUDE, LONGITUDE);
  //myLord.DstRules(3,2,11,1,60); // DST Rules for USA
       
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");  
 
}
void ReadDS3231()
{
  int second,minute,hour,date,month,year,temperature; 
  second=Clock.getSecond();
  minute=Clock.getMinute();
  hour=Clock.getHour(h12, PM);
  date=Clock.getDate();
  month=Clock.getMonth(Century);
  year=Clock.getYear();
  
  //temperature=Clock.getTemperature();
  lcd.setCursor(0, 1);
 
}


/******************** MAIN LOOP STARTS HERE  *******************
****************************************************************/
void loop(){   
  
  ReadDS3231();delay(1000);
	// Get the hour,and minute, 
	lcd.print(Clock.getHour(h12, PM), DEC);
	lcd.print(':');
	lcd.print(Clock.getMinute(), DEC);



DHT.read11(dht_dpin);

#if DEBUG == 1

Serial.print("hour is");
Serial.print(Clock.getHour(h12, PM), DEC);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temp value=");
Serial.print("temperature = ");
Serial.print(DHT.temperature,decPlaces);
Serial.println("C ");
#endif

delay(800);//Don't try to access too frequently... in theory
//should be once per two seconds, fastest,
//but seems to work after 0.8 second.


lcd.setCursor(0, 0);
lcd.print("T=");
lcd.print(DHT.temperature,decPlaces);
lcd.print("C ");
lcd.print("H=");
lcd.print(DHT.humidity,decPlaces);
lcd.print("%");

part 2 yeps had to split it . I just cant seem to find a place to put the first code for it to run properly??

if (timeStatus()!= timeNotSet) {
       minNow = minute();
    if (minNow != minLast) {
        minLast = minNow;
        hourNow = hour();
        minOfDay = hourNow * 60 + minNow; //minute of day will be in the range 0-1439
        #if DEBUG == 1
          Serial.print(" hourNow:");
          Serial.print(hourNow);
          Serial.print(" minNow:");
          Serial.print(minNow);
          Serial.print("  minOfDay:");
          Serial.print(minOfDay);
          Serial.print("  minLast:");
          Serial.print(minLast);
          Serial.print("  hourLast:");
          Serial.print(hourLast);
          Serial.println();
        #endif
        if (hourNow != hourLast) { // Noting that the Sunrise/Sunset is only calculated every hour => less power. 
      /* Sunrise: */
      sunTime[3] = day(); // Uses the Time library to give Timelord the current date
      sunTime[4] = month();
      sunTime[5] = year();
      myLord.SunRise(sunTime); // Computes Sun Rise. Prints:
      mSunrise = sunTime[2] * 60 + sunTime[1];
      #if DEBUG == 1
                          Serial.print("SUNRISE:");
          //DisplayTime(sunTime);
                        #endif
      /* Sunset: */
      sunTime[3] = day(); // Uses the Time library to give Timelord the current date
      sunTime[4] = month();
            sunTime[5] = year();
      myLord.SunSet(sunTime); // Computes Sun Set. Prints:
      mSunset = sunTime[2] * 60 + sunTime[1];
            #if DEBUG == 1
              Serial.print("SUNSET:");
              //DisplayTime(sunTime);
            #endif
            hourLast = hourNow;
       }
       
        
        
             if(minOfDay > mSunrise || minOfDay <= mSunset) 
    { 
    digitalWrite(LIGHTS,LIGHTS_ON); 
    }

    else
    {
    digitalWrite(LIGHTS,LIGHTS_OFF);
    }
if( digitalRead(LIGHTS) == LIGHTS_OFF )
 { Serial.print("  LED State:OFF"); }
   else
    {
    Serial.print("  LED State:ON");
    }    
        
        #if DEBUG == 1
        
          Serial.print("  mSunrise:");
          Serial.print(mSunrise);
          Serial.print("  mSunset:");
          Serial.print(mSunset);
          Serial.print("  LED State:");
          Serial.print(minOfDay < mSunrise || minOfDay >= mSunset);
          Serial.println();
        #endif
        
        // if (minOfDay == mSunrise || minOfDay == mSunset) whistle(); Maybe cute to add sound later
    } // End: if (minNow != minLast)
  } // End:  if (timeStatus()!= timeNotSet)
} // End loop()

/************************* FUNCTIONS  **************************
****************************************************************/



byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
  
}

You don't have to split it. You can add the .ino file as an attachment.

A description of what the code in the program is supposed to do would be a big help. Trying to figure out what a program does by reading the code is very time consuming.

...R

Hi again,sorry about my ignorance but i am learning what is needed. The program controls various things ,fan ,heater ,lights, humidifier and water pump. The lights going on or off aare determined by the sunset and sunrise times in singapore. This is determined by the use of the timelord library. The actions of the heater ,humidifier and pump are determined by temperature and humidity readings from the dht11 sensor.
I live in adelaide australia and i use this program to have my orchid terrarium as close as possible to the general humidity temp and light in singapore. The program (the one i split) works as expected .Lights come on at sunrise time ,heater turns on to keep a set temp etc.
With the first sketch i put up it reads an input from a php file which connects to openweathermap and obtains the forecast max temp for the following day and presents it as such <30>.
When i add the contents of the first sketch i posted which reads the output of the php file into the larger/main sketch it doesnt work. Ive tried even putting the whole sketch into the loop .Ive tried it as the sketch is eg put whatever is in setup into main sketch loop into loop etc. Ive tried calling it as a function though when i do it connects, reads data but then looses the data after i returns back to the loop.The main sketch doesnt get affected by me adding the smaller sketch ..it runs as normal. Just seems the smaller sketch doesnt like living with my sketch lol .....
And why am i trying to do this .. because well i guess I want to and i know it can be done.

So i hope ive explained enough
Much appreciated

Your Reply #6 gives a description of your project.

What I was hoping you would give is a description of your program (the one with the problem) - this part does X, this next part does Y, if ZZ happens this part is supposed to do A, but it is only doing B.

And have you posted both the working program (I think that is in your original Post) and also the non-working program as a single .ino file ?

Help us to help you.

...R

Hi,
It probably isn't the cause of your problem, but I'd use a 16x4 LCD instead of two 16x2 and write a function for the display.

#include <DS3231.h>[color=#222222][/color]
#include "rtc_ds3231.h"[color=#222222][/color]
#include <LiquidCrystal.h>[color=#222222][/color]
#include <dht.h>[color=#222222][/color]
#include <DS3231.h>  <<<<---- declared twice[color=#222222][/color]
#include <LCD.h>[color=#222222][/color]
#include <LiquidCrystal_I2C.h>

Tom...... :slight_smile: