Connecting to ThingSpeak after ezTime

I'm trying to post temperature data to ThingSpeak. I am able to connect to ThingSpeak on the first attempt, at the end of void setup(), but all attempts after the call to the "getTheTime()" function returns error code 301. If I move "getTheTime()" to before "sendToThingspeak(), I can't connect at all.
I don't understand why the call to "getTheTime()" prevents subsequent connections to ThingSpeak. Any help would be greatly appreciated.

Paul

// V1.6 Test temp sensors
// V1.7 Add timers
// V1.8 Add Thingspeak code
#include <arduino-timer.h>
#include <ezTime.h>
#include <Ethernet.h>
#include <SPI.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <TFT_FastPin.h>
#include <TFT_ILI9341.h>
#include <User_Setup.h>
#include "secrets.h"
#include "ThingSpeak.h"
#define ONE_WIRE_BUS 28     // Arduino pin for temp sensors
#define trigPin 44 //Sensor Echo pin
#define echoPin 42 //Sensor Trip pin
#define lineColor ILI9341_GREEN
#define textColor ILI9341_RED
#define textColor2 ILI9341_BLUE
#define fillColor ILI9341_WHITE
#define pinkColor 0xb1f8
#define ltYellow 0xb5c7
// Sensor pins
#define sensorPower 32  // Power for level sensor (was 23)
#define sensorPin A8    // Water level sensor pin
#define relayPin 33     // Controls water valve via Elk input (was 25)
#define swPin A5        // Selects display screen
#define ONE_WIRE_BUS 28 // Arduino pin for temp sensors
#define trigPin 44      // Distance Sensor Echo pin
#define echoPin 42      // Distance Sensor Trip pin

TFT_ILI9341 tft = TFT_ILI9341();
TFT_ILI9341 tft1 = TFT_ILI9341();

//IPAddress ip(192, 168, 1, 75); // Assign IP address to ethernet shield
//EthernetServer server(80);   // Create a server at port 80
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
EthernetClient client;
EthernetClient clientA;
auto timer = timer_create_default(); // create a timer with default settings
Timer<> default_timer; // save as above
// Addresses for pond sensors
/*DeviceAddress  tempPh  = {0x28, 0xFF, 0x74, 0x14, 0x53, 0x04, 0x00, 0xA8}; // Pumphouse sensor
  DeviceAddress  temp1ft = {0x28, 0x1e, 0x18, 0x40, 0x19, 0x20, 0x06, 0x2c}; // 1'
  DeviceAddress  temp2ft = {0x28, 0xab, 0xec, 0x40, 0x19, 0x20, 0x06, 0x79}; // 2'
  DeviceAddress  temp3ft = {0x28, 0xfe, 0x02, 0x86, 0x19, 0x20, 0x06, 0xf4}; // 3'
  DeviceAddress  tempPit = {0x28, 0xe4, 0x00, 0x79, 0xa2, 0x16, 0x03, 0xcd}; // Pump pit
*/
//***********Temporary addresses*****************
DeviceAddress  tempPh  = {0x28, 0xB5, 0x44, 0xE8, 0x05, 0x00, 0x00, 0x64}; // Pumphouse sensor -GREEN
DeviceAddress  temp1ft = {0x28, 0xE8, 0x4E, 0xE8, 0x05, 0x00, 0x00, 0xBD}; // 1'  -  YELLOW
DeviceAddress  temp2ft = {0x28, 0x1C, 0xDC, 0xE8, 0x05, 0x00, 0x00, 0x27}; // 2'  -  PURPLE
DeviceAddress  temp3ft = {0x28, 0x0F, 0x5F, 0xE8, 0x05, 0x00, 0x00, 0xA0}; // 3'  -  RED
DeviceAddress  tempPit = {0x28, 0x01, 0x2B, 0xB8, 0x4C, 0x20, 0x01, 0x39}; // Pump pit  -  Water oroof
////////////////////////////////////////////////////
String versionNumber = "1.7.2.1";  // Version of this sketch
///////////////////////////////////////////////////
// Value for storing water level
bool filling = true;
int level = 0;
int avLevel = 0;
int fullLevel = 3000;
int fillLevel = 400;
int fillsToday = 0;
int fillsYesterday = 0;
int d = 1, m = 1, y = 2000, h = 1, mn = 1, s = 1;
int i = 0;     // Counter
int a = 0;     // Counter
int pass = 0;  // Counts passes through screens
int oldDay = 1;   // Saves previous date
int oldHour;     ///////
int oldMinute;        ////// holds old time values
int oldSecond;    //////
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
//String amPM = "  ";
String pondStatus = "Waiting ";
const char* tsserver = "api.thingspeak.com";
unsigned long channelNumber = deleted;  // "Greenhouse" channel
const char* writeKey = "deleted";  // Greenhouse write key
float TempPh = 70, Temp1ft = 71, Temp2ft = 72, Temp3ft = 73, TempPit = 74;
byte mac[] = {0xDE, 0xA0, 0x5E, 0x23, 0x8E, 0x4D};
int percent;
float duration, distance;
Timezone PST;

//**********************************************************************
void setup() {
  PST.setPosix("UTC+7:00");
  PST.setDefault();
  tft.begin();
  //Ethernet.begin(mac, ip); // Initialize Ethernet shield
  Serial.begin(115200);
  sensors.begin();
  tft.setRotation(3);
  tft.setTextSize(3);
  tft.setTextColor(ILI9341_BLACK);
  tft.fillScreen(lineColor);
  tft.setCursor(100, 20);
  tft.print("Sketch");
  tft.setCursor(20, 60);
  tft.print("Pond_Temps ");
  tft.print(versionNumber);  //Current version of this sketch
  // Enter a MAC address for your controller below. (Or use address below, just make sure it's unique on your network)
  // Newer Ethernet shields have a MAC address printed on a sticker on the shield
#define MAC_ADDRESS    { 0xBA, 0xAA, 0xAD, 0xDC, 0xFF, 0xEB }
#define EZTIME_ETHERNET
  Serial.println();
  Serial.print(F("Ethernet connection ... "));
  byte mac [] = MAC_ADDRESS;
  if (Ethernet.begin(mac) == 0) {
    Serial.println(F("failed. (Reset to retry.)"));
    while (true) {
      // ;
    };     // Hang
  } else {
    Serial.print(F("got DHCP IP: "));
    Serial.println(Ethernet.localIP());
  }
  delay(1000); // give the Ethernet shield a second to initialize:
  waitForSync();    // Wait for ezTime to get its time synchronized
  Serial.println();
  Serial.println("UTC:             " + UTC.dateTime());
  Serial.println("PST:             " + PST.dateTime());
  delay(1000);
  Serial.println("PST:             " + PST.dateTime());
  Serial.println(dateTime());
  pinMode(sensorPin, INPUT);
  pinMode(sensorPower, OUTPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(swPin, INPUT);
  digitalWrite(sensorPower, LOW);    // Set to LOW so no power flows through the sensor
  Serial.println(Ethernet.localIP());
  Serial.println(PST.dateTime(COOKIE));
  Serial.println(PST.dateTime("l, F j, Y  g:i:s"));
  sensors.setResolution(12);
  EthernetClient clientA;
  ThingSpeak.begin(clientA);
  timer.every(1000, readWaterLevel);  //Read water level once a second
  timer.every(12000, getTemps);       //Read the temp sensors every 12 seconds
  timer.every(60000, sendToThingspeak);  // Send to Thingspeak every minute
  timer.every(120000, getTheTime);   // Get UTC time every hour (3600000 milliseconds)
  tft.setTextColor(ILI9341_BLACK, ILI9341_GREEN);
  getTemps();
  sendToThingspeak();
  getTheTime();

}

//**********************************************************************
void loop() {
  timer.tick();  // tick the timer
  tft.setCursor(35, 205);
  tft.print(pondStatus);
}

//**********************************************************************
void getTemps() {
  i++;
  sensors.requestTemperatures();
  Serial.println("Request Temps");
  Serial.print("i = ");
  Serial.println(i);
  switch (i) {
    case 1:
      Serial.println("Case 1");
      TempPh = sensors.getTempF(tempPh);
      break;
    case 2:
      Serial.println("Case 2");
      Temp1ft = sensors.getTempF(temp1ft);
      break;
    case 3:
      Serial.println("Case 3");
      Temp2ft = sensors.getTempF(temp2ft);
      break;
    case 4:
      Serial.println("Case 4");
      Temp3ft = sensors.getTempF(temp3ft);
      break;
    case 5:
      Serial.println("Case 5");
      TempPit = sensors.getTempF(tempPit);
      i = 0;
      break;

  }
  Serial.println(TempPh);
  Serial.println(Temp1ft);
  Serial.println(Temp2ft);
  Serial.println(Temp3ft);
  Serial.println(TempPit);
  return true;
}

//**********************************************************************
bool readWaterLevel(void *) {
  a++;
  //Serial.println(" READING WATER LEVEL");
  digitalWrite(sensorPower, HIGH);  // Turn level sensor ON
  level = analogRead(sensorPin);    // Read sensor
  digitalWrite(sensorPower, LOW);   // Turn level sensor OFF
  // Serial.println(level);
  avLevel = (avLevel + level);
  if (a == 3) {
    a = 0;
    avLevel = (avLevel / 3);
    if (avLevel < 401) {
      //  Serial.print(avLevel);
      //  Serial.print("*******************");
      digitalWrite(relayPin, LOW);
      // Serial.println("Wire is broken");
      pondStatus = "WIRE IS BROKEN ";
    }
    else if (400 < avLevel && avLevel < 540) {
      digitalWrite(relayPin, HIGH);
      pondStatus = "POND IS FILLING";

      if (filling == true) {
        fillsToday = fillsToday + 1;
        filling = false;
      }
    }
    else {
      digitalWrite(relayPin, LOW);
      pondStatus = " POND IS FULL   ";
      filling = true;
    }
    avLevel = 0;
  }
}

//**********************************************************************
void sendToThingspeak() {
  Ethernet.begin(mac);
  ThingSpeak.setField(1, TempPh);
  ThingSpeak.setField(2, Temp1ft);
  ThingSpeak.setField(3, Temp2ft);
  ThingSpeak.setField(5, Temp3ft);
  ThingSpeak.setField(6, TempPit);
  // write to the ThingSpeak channel
  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  if (x == 200) {
    Serial.println("Channel update successful.");
  }
  else {
    Serial.println("Problem updating channel. HTTP error code " + String(x) + PST.dateTime());
  }
}

//**********************************************************************
void getTheTime() {
  waitForSync();
  Serial.println();
  Serial.println("UTC:             " + UTC.dateTime());
  Serial.println("PST:             " + PST.dateTime());
  Serial.println("5 Second delay");
  Serial.println("PST:             " + PST.dateTime());
  Serial.println(dateTime());
  tft.setCursor(85, 100);
  tft.print("Reset at");
  tft.setCursor(10, 130);
  tft.print(PST.dateTime("F j, Y"));
  tft.setCursor(80, 165);
  tft.print(PST.dateTime("h:i:s"));

}

Sorry, if I see Free Shipping in the subjec of an email, it goes straight to the trash,

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.