I made an automatic water misting fan. The temperature monitor software checks temperature at certain interval and decides if it should turn on/off the water misting fan.
I think what the temperature monitor software does might meet your needs.
Please note that you have to add JLink.jar to this java project.
/Applications/Mathematica.app/Contents/SystemFiles/Links/JLink/JLink.jar
If you use Linux or Windows, you have to modify the path of MathKernel to create KernelLink.
/Applications/Mathematica.app/Contents/MacOS/MathKernel
Java (JDK 8):
import com.wolfram.jlink.KernelLink;
import com.wolfram.jlink.MathLinkException;
import com.wolfram.jlink.MathLinkFactory;
import java.util.Optional;
public class ScheduledTask {
public static void main(String[] argv) throws MathLinkException {
createKernelLink()
.map(link -> loadPackage(link))
.map(link -> checkTemperature(link));
}
private static Optional<KernelLink> createKernelLink() {
String[] options = {"-linkmode", "launch", "-linkname",
"/Applications/Mathematica.app/Contents/MacOS/MathKernel"};
try {
KernelLink link = MathLinkFactory.createKernelLink(options);
return Optional.of(link);
} catch (MathLinkException e) {
System.out.println("Fatal error opening link: " + e.getMessage());
return Optional.empty();
}
}
private static KernelLink loadPackage(KernelLink kernelLink) {
String packagePath = System.getProperty("user.dir") + "/ac.m";
String loadPackage = "Get[\"" + packagePath + "\"]";
evaluate(kernelLink, loadPackage);
return kernelLink;
}
private static void evaluate(KernelLink kernelLink, String loadPackage) {
try {
kernelLink.discardAnswer();
kernelLink.evaluate(loadPackage);
kernelLink.discardAnswer();
} catch (MathLinkException e) {
e.printStackTrace();
}
}
private static Void checkTemperature(KernelLink link) {
int interval = 2000;
String expr = "checkTemperature[]";
return runScheduledTask(link, interval, expr);
}
private static Void runScheduledTask(KernelLink link, int interval, String expr) {
Runnable task = () -> {
try {
link.evaluate(expr);
link.discardAnswer();
System.out.print(".");
} catch (MathLinkException e) {
e.printStackTrace();
}
};
while(true) {
try{
task.run();
Thread.sleep(interval);
}
catch(Exception e){
link.close();
}
}
}
}
Wolfram:
(* wemo switch https://github.com/iancmcc/ouimeaux *)
(* istats https://github.com/Chris911/iStats *)
cpuTemperature[] := Module[{command, xs, cs, position},
command = {"/usr/local/bin/istats", "extra"};
xs = RunProcess[command]["StandardOutput"] // ImportString[#, "CSV"] & // First;
cs = Map[Select[PrintableASCIIQ], Characters@xs] // Flatten;
position = FirstPosition[cs, "["] // First;
Take[cs, position-4] // StringJoin // StringCases[#, x__ ~~ ":" ~~ y__->y]&//First//ToExpression]
temperatureHighQ[highTemperature_] := Module[{temperature},
temperature = cpuTemperature[];
If[temperature > highTemperature, (Pause[1];
cpuTemperature[] > temperature), False]]
temperatureLowQ[lowTemperature_] := Module[{temperature},
temperature = cpuTemperature[];
If[temperature < lowTemperature, (Pause[1];
cpuTemperature[] < temperature), False]]
wemoSwitch[x_] := Module[{command},
If[x, Print["High"], Print["Low"]];
command = {"/usr/local/bin/wemo", "switch", "aircondition",
If[x, "on", "off"]};
RunProcess[command]];
checkTemperature[] := Module[{high, low},
high = 50;
low = 46;
Which[
temperatureHighQ[high], wemoSwitch[True] ,
temperatureLowQ[low], wemoSwitch[False]]]
aircondition[] := Module[{task, interval},
interval = 5;
task = RunScheduledTask[checkTemperature[], interval];
While[True, Pause[2]]]
wemoSwitch[False]