Problem with my code using Time, glitches at 1am every day

Hi All,

I know that the problem is obviously my sketch but I have no idea what is wrong. The sketch is controlling an irrigation system in my garden and once an hour takes readings from analog moisture sensors and posts this to a remote MySQL database by using the bridge and sending a POST request using a Python script.

I am pretty sure the Python script is not causing the problem as it has been working fine.

I made a lot of changes to the script and now for some reason instead of updating the MySQL database once per hour as it was doing it is now updating ok at once per hour until 01:00 then it starts updating every 12 seconds until 01:10 and then goes back to working normally, I have looked over and over and cant seem to spot the problem.

Here is the code

#include <Bridge.h> 

#include <OneWire.h> 
#include <DallasTemperature.h> //Library for using DS18B20 temp sensors
#include <Process.h> //needed for date/time functions etc
#include <Console.h> //for debug

Process date;            

/*Listing of IO PINS
 * ANALOG PINS
 * AN0  - Light sensor
 * AN1  - Moisture 1 (vegetable patch)
 * AN2  - Moisture 2 (Beans)
 * AN3  - Moisture 3 (Cold Frame)
 * AN4  -
 * AN5  - 
 *  
 * 
 * DIGITAL pins
 * 0    - RESERVED for RS232
 * 1    - RESERVED for RS232
 * 2    - Temperature sensors data pin
 * 3    - Moisture 1 sensor Power
 * 4    - Moisture 2 sensor Power
 * 5    - Moisture 3 sensor Power
 * 6    - Water < 25% input (Normally HIGH)
 * 7    - Water < 50% input (Normally HIGH)
 * 8    - Water zone 1 (Vegetable patch)
 * 9    - Water zone 2 (Beans)
 * 10   - Water zone 3 (Cold Frame)
 * 11   - Pump 
 * 12   - 
 * 13   -
 * 
 * 
 * Ideal Coldframe temperature 25-30 degC
 * 
 * 
 * Dry sensor readings: Moisture1 = 461
 * Dry sensor readings: Moisture1 = 482
 * Dry sensor readings: Moisture3 = 400
 * 
 * Wet sensor readings: Moisture1 = 360 or less
 * Wet sensor readings: Moisture2 = 360 or less
 * Wet sensor readings: Moisture3 = 360 or less
 * 
 * LIGHT SENSOR
 * Dark reading = 
 * 
 * 
 * Watering Times = Zone 1 - 2 mins
 *                  Zone 2 - 2 mins
 *                  Zone 3 - 30 secs
 */


//Setup IO and pin names


// Data wire is plugged into pin 2 on the Arduino 
#define ONE_WIRE_BUS 2 

/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS); 
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);
/********************************************************************/ 

//Setup Vars, constants etc
long LastUpdate = 0;

//Holds sensor readlings 
int Light = 0;
float OSTemp = 0;
float ISTemp = 0;
int Moisture1 = 0;
int Moisture2 = 0;
int Moisture3 = 0;
int Zone1Done = 0;
int Zone2Done = 0;
int Zone3Done = 0;



//Strings and vars related to time and timing
int timestamp =0; //holds the latest timestamp updated
int lastupdate = 0; //holds timestamp of last update for comparison
int interval = 3600; //seconds until update required again  3600 = 1 hour

int Setpoint1 = 450; //setpoint for low moisture level
int Setpoint2 = 450;
int Setpoint3 = 450;

int Trigger1=0;
int Trigger2=0;
int Trigger3=0;
int RunCycle=0;
int LastCycleTime=0; //Timestamp of when last watering cycle completed

int Watered1 = 0;
int Watered2 = 0;
int Watered3 = 0;

int Zone1Time = 60; //Times for watering zones (seconds)
int Zone2Time = 40;
int Zone3Time = 20;


int VentStatus = 0;
int WaterHalf = 0;
int WaterLow = 0;

int LowLastState = 0;//Used to detect state change
int HalfLastState = 0;


void setup()
{
  
   Bridge.begin();  // start the bridge
   Console.begin(); // for debug
   delay(5000); // Allow time

   pinMode(0, INPUT);
   pinMode(1, INPUT);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(6, INPUT);
   pinMode(7, INPUT);
   pinMode(8, OUTPUT);
   pinMode(9, OUTPUT);
   pinMode(10, OUTPUT);
   pinMode(11, OUTPUT);
   digitalWrite(3, LOW); 
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
   digitalWrite(8, HIGH);
   digitalWrite(9, HIGH);
   digitalWrite(10, HIGH);
   digitalWrite(11, HIGH);
  


 // INITIALLY ADD TIME NOW AS LAST UPDATE TIME AS A STARTING FIGURE    
   if (!date.running()) {
      date.begin("date");
      date.addParameter("+%s");
      date.run();
   }
 
  //if there's a result from the date process, parse it:
  while (date.available() > 0) {
   
   lastupdate = date.readString().toInt(); //change the last update time to current time
   timestamp = lastupdate;
   }
LowLastState = digitalRead(6);
HalfLastState = digitalRead(7);

}



void loop()
{
  //Console.println("Starting main loop");
  //Read Light Level sensor
  
  Light = analogRead(A0);
  //Read Temperature sensors
  sensors.requestTemperatures(); //Request teperatures from sensors
  OSTemp = sensors.getTempCByIndex(0);
  ISTemp = sensors.getTempCByIndex(1);

  
  //Check water level
  CheckWaterLevel();
  
  
  //Update the current timestamp
   if (!date.running()) {
      date.begin("date");
      date.addParameter("+%s");
      date.run();
    }
   //if there's a result from the date process, parse it:
  while (date.available() > 0) {
    // get the result of the date process as a unix timestamp
    timestamp = date.readString().toInt();
  }

  Console.print("timestamp =");
  Console.print(timestamp);
  Console.print("   lastupdate =  ");
  Console.print(lastupdate);
  Console.print("   Seconds = ");
  Console.println(timestamp - lastupdate);
  

  if (timestamp > (lastupdate + interval))
  {//Triggered once an hour

      //Read Analog Moisutre sensors
     ReadMoisture();
     Console.println("Updating sensors");
     UpdateSensors(); //Write values to MySQL DB
     lastupdate = timestamp; //change value of lastupdate to now
  }
  
  delay(2500); //delay at end of main loop

}

void ReadMoisture(){
//Read Analog Moisutre sensors
      
      digitalWrite(3, HIGH);
      delay(1000); //delay to stabilise power
      Moisture1 = analogRead(A1);
      delay(50);
      digitalWrite(3, LOW);

      
      digitalWrite(4, HIGH);
      delay(1000); //delay to stabilise power
      Moisture2 = analogRead(A2);
      delay(50);
      digitalWrite(4, LOW);

      
      digitalWrite(5, HIGH);
      delay(1000); //delay to stabilise power
      Moisture3 = analogRead(A3);
      delay(50);
      digitalWrite(5, LOW);
}

void UpdateActions(){
 //Write values to MySQL 
 
      Bridge.put("WaterHalf", String(WaterHalf));
      delay(250); 
      Bridge.put("WaterLow", String(WaterLow)); 
      delay(250);
      Bridge.put("VentStatus", String(VentStatus)); 
      delay(250);
      Bridge.put("Watered1", String(Watered1)); 
      delay(250);
      Bridge.put("Watered2", String(Watered2)); 
      delay(250);
      Bridge.put("Watered3", String(Watered3)); 
      delay(1000);
      /*
      Process p;
   
      p.runShellCommand("python /mnt/sda1/arduino/www/gardenino/postactions.py"); //run Python script on Linux side to POST to MySQL database
      while(p.running());
*/
}

void UpdateSensors(){

 
      Bridge.put("Light", String(Light));
      delay(250); 
      Bridge.put("OSTemp", String(OSTemp)); 
      delay(250);
      Bridge.put("ISTemp", String(ISTemp)); 
      delay(250);
      Bridge.put("Moisture1", String(Moisture1)); 
      delay(250);
      Bridge.put("Moisture2", String(Moisture2)); 
      delay(250);
      Bridge.put("Moisture3", String(Moisture3)); 
      delay(1000);

      Process p;
   
      p.runShellCommand("python /mnt/sda1/arduino/www/gardenino2/postsensors.py"); //run Python script on Linux side to POST to MySQL database
      while(p.running());

  
}

void CheckWaterLevel(){
  
  if (digitalRead(6) == HIGH){
    WaterLow = 1;
  }else{
    WaterLow = 0;  
  }

  if (digitalRead(7) == HIGH){
    WaterHalf = 1;
  }else{
    WaterHalf = 0;
  }

//Check for state change of level sensors and report
  
  if (WaterHalf != HalfLastState){ //Call procedure to update logs
    
    HalfLastState = WaterHalf;
    UpdateActions();
  }

  if (WaterLow != LowLastState){ //Call procedure to update logs
    
    LowLastState = WaterLow;
    UpdateActions();
  }
}

Any help is greatly appreciated.

Thanks

Tom

And the python script is

This is the Python script
[code]import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge')

from bridgeclient import BridgeClient as bridgeclient
value = bridgeclient()

Light = value.get("Light") 
OSTemp = value.get("OSTemp")
ISTemp = value.get("ISTemp")
Moisture1 = value.get("Moisture1")
Moisture2 = value.get("Moisture2")
Moisture3 = value.get("Moisture3") #Got all the values from the bridge side, now to POST them to the server-side PHP script to be added to the MySQL database

import urllib
params = urllib.urlencode({'StringTest': "#################", 'Light': Light, 'OSTemp': OSTemp, 'ISTemp': ISTemp, 'Moisture1': Moisture1, 'Moisture2': Moisture2, 'Moisture3': Moisture3})
f = urllib.urlopen("http://www.carp-fish.co.uk/#######.php", params)
print f.read()

Please ignore the ## as I have put those in to blank out the url of the script it is sending POST data to and there is a string that is checked to match in the script as some basic form of security before anything will attempt to be parsed in the PHP script.[/code]

Please ignore the ### as that info could potentially be used to find the php script on server or a string that is sent as part of a verification method to ensure that the data came from my Arduino only.

I seem to have fixed it, by changing the timestamp and lastinterval vars from integer to long, I'm pretty sure this was working before but the script has been running over 24 hours without any odd behaviour.