I have updated the Java TCP client to switch the relay ON and OFF at set times during day
// TCP_relay_timer_2 - switch relay ON and OFF at set times during day
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.concurrent.TimeUnit;
import java.time.LocalDateTime;
public class TCP_relay_timer_2
{
static int port=80; // HTTP port to send/receive on
static String remoteIPaddress= ("192.168.1.210");//"127.0.0.1"); // IP of remote machine
// method to switch relay ON or OFF
static Boolean TPC_relay(Boolean debug, String command) throws Exception
{
if(debug) System.out.println("\nTCPClient: IP address " + InetAddress.getLocalHost().toString() + " port " + port );
if(debug) System.out.println("Connecting to TCPServer on to " + remoteIPaddress + " socket " + port);
Socket socket = new Socket( remoteIPaddress, port ); // open socket to server
PrintStream objOut = new PrintStream( socket.getOutputStream() ); // OK, open streams
Scanner objIn = new Scanner( socket.getInputStream() );
if(debug) System.out.println("Sending to " + remoteIPaddress + " socket " + port + " data: " + command);
objOut.println(command); // send string
// loop waiting for message fom server - check for relay ON or OFF
while(true)
try {
String s = (String) objIn.nextLine(); // read string and display it
if(debug) System.out.println("Client received: " + s );
if(s.startsWith("Relay is now: ON"))
{if(debug) System.out.println("******* relay ON **********"); relayState=true; }
if(s.startsWith("Relay is now: OFF"))
{if(debug) System.out.println("******* relay OFF **********"); relayState=false; }
}
catch (Exception se) {break;}
return true;
}
static Boolean relayState=false;
// switch ON relay and check result
// if dubug is true print debuf information, e.g.HTTP response
// if check_state is ture communicate with server to make sure it is OFF
static public void relay_ON(Boolean debug, Boolean check_state) throws Exception {
if(!check_state && relayState)return;
System.out.println("\nswitching relay ON ");
TPC_relay(debug, "/RELAY=ON");
if(relayState) System.out.println("checked relay is ON ! ");
else System.out.println("checked switch ON failed!");
}
// switch OFF relayand check result
// if dubug is true print debuf information, e.g.HTTP response
// if check_state is ture communicate with server to make sure it is OFF
static public void relay_OFF(Boolean debug, Boolean check_state) throws Exception {
if(!check_state && !relayState)return;
System.out.println("\nswitching relay OFF ");
TPC_relay(debug, "/RELAY=OFF");
if(!relayState) System.out.println("checked relay is OFF ! ");
else System.out.println("checked switch OFF failed!");
}
// simple time class
public static class Time {
// constructor setting hour and minutes
Time(int hour, int min) { this.hour=hour; this.min=min; }
public String toString() {
return this.hour + ":" + ((min < 10) ? "0" : "") + min; }
// return true if time >= on and < off
public Boolean compare(Time on, Time off) {
// test for ON hour > OFF hour, i.e. overnight - if so add 24 to OFF hour
if(on.hour <= off.hour) {
// On < OFF so normal day ON/OFF timing
if(((hour+min/60f) >= on.hour+on.min/60f) && (hour+min/60f) < (off.hour+off.min/60f) )
return true; else return false;
}
else
// overnight ON/OFF timing
if((hour+min/60f) >= (on.hour+on.min/60f) || (hour+min/60f) < (off.hour+off.min/60f) )
return true; else return false;
}
private int hour, min;
};
public static void main(String args[]) throws Exception
{
relay_OFF(true,true);
// sample ON/OFF times - last time is overnight
// note: no check on overlapping ON/OFF times etc
Time[] timeON ={new Time(8, 28), new Time(8, 33), new Time(8, 36), new Time(21,30)};
Time[] timeOFF={new Time(8, 29), new Time(8, 35), new Time(8, 37), new Time(7, 0)};
System.out.println("timeON[0] = " + timeON[0]);
System.out.println("timeOFF[0] = " + timeOFF[0]);
// test overnight time compare
Time timeNow=new Time(22, 00);
//System.out.println(timeNow.compare(timeON[3], timeOFF[3]));
//timeNow=new Time(2, 00);
//System.out.println(timeNow.compare(timeON[3], timeOFF[3]));
LocalDateTime date1 = LocalDateTime.now();
// check time to send command to switch ON/OFF every minute
while(true) {
timeNow=new Time(date1.getHour(),date1.getMinute());
System.out.print("timeNow = " + timeNow);
Boolean relayON=false;
// itterate thru times checking relay should be ON or OFF
for(int i=0; i< timeON.length;i++)
if(timeNow.compare(timeON[i], timeOFF[i])) relayON=true;
if(relayON) relay_ON(false, true);
else relay_OFF(false,true);
//if(relayState) System.out.println(" relay is ON ! ");
//else System.out.println(" relay is OFF !");
int lastMinute=date1.getMinute();
// wait for minute to change
do {
TimeUnit.SECONDS.sleep(5);
date1 = LocalDateTime.now();
}
while(lastMinute == date1.getMinute());
}
}
}
ran it for a few days appears to work OK including over night settings
a simple test run gave
> run TCP_relay_timer_2
switching relay OFF
TCPClient: IP address BB-DELL2/169.254.164.171 port 80
Connecting to TCPServer on to 192.168.1.210 socket 80
Sending to 192.168.1.210 socket 80 data: /RELAY=OFF
Client received: HTTP/1.1 200 OK
Client received: Content-Type: text/html
Client received:
Client received: <!DOCTYPE HTML>
Client received: <html>
Client received: <head><title>ESP8266 RELAY Control</title></head>
Client received: Relay is now: OFF<br><br>
******* relay OFF **********
Client received: Turn <a href="/RELAY=OFF">OFF</a> RELAY<br>
Client received: Turn <a href="/RELAY=ON">ON</a> RELAY<br>
Client received: </html>
checked relay is OFF !
timeON[0] = 8:28
timeOFF[0] = 8:29
timeNow = 8:27
switching relay OFF
checked relay is OFF !
timeNow = 8:28
switching relay ON
checked relay is ON !
timeNow = 8:29
switching relay OFF
checked relay is OFF !
timeNow = 8:30
switching relay OFF
checked relay is OFF !
timeNow = 8:31
switching relay OFF
checked relay is OFF !
timeNow = 8:32
switching relay OFF
checked relay is OFF !
timeNow = 8:33
switching relay ON
checked relay is ON !
timeNow = 8:34
switching relay ON
checked relay is ON !
timeNow = 8:35
switching relay OFF
checked relay is OFF !
timeNow = 8:36
switching relay ON
checked relay is ON !
timeNow = 8:37
switching relay OFF
checked relay is OFF !
timeNow = 8:38
switching relay OFF
checked relay is OFF !
timeNow = 8:39
switching relay OFF
checked relay is OFF !
I think it is simpler to equip the relay Microcontroller with a RTC to control the relay switching times which can be updated using a web client