//LED panel libraries #include "ArduinoGraphics.h" #include "Arduino_LED_Matrix.h" //Temp Sensor #include //Wifi code #include #include "arduinoWifi.h" //Custom Lib for Username, etc //Constants #define PROBE_PIN 4 #define LOW_TEMP 39 #define MID_TEMP 41 DHT11 probe(PROBE_PIN); ArduinoLEDMatrix matrix; int prevTemp = 0; int loopCount = 0; int maxLoops = 15 * 30; // (15 minutes by 30 second increments) int errorCount = 0; //Wifi char ssid[] = SECRET_SSID; char pass[] = SECRET_PASS; char server[] = SECRET_DOMAIN; WiFiSSLClient client; int currentFridgeState = -1; boolean relayOn = false; void setup() { Serial.begin(9600); matrix.begin(); reconnectWiFi(); } void reconnectWiFi() { Serial.println("Connecting to WiFi..."); while (WiFi.begin(ssid, pass) != WL_CONNECTED) { Serial.print("."); delay(1000); } Serial.println("\nWiFi connected"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); } void loop() { Serial.println(); int temperature = 0; int humidity = 0; double tempF = 0; // Attempt to read the temperature and humidity values from the DHT11 sensor. int result = probe.readTemperatureHumidity(temperature, humidity); if (result == 0) { errorCount = 0; tempF = (double)temperature; tempF = (tempF * 9/5) + 32; int tempFInt = (int)tempF; Serial.print("Temperature: "); Serial.print(tempF,1); //Serial.print(" °F\tHumidity: "); //Serial.print(humidity); //If the relay is not on, do not turn it back on until the temp is above Mid Temp if (!relayOn) relayOn = (MID_TEMP < tempF); else { //If the relay is already on, run until LOW temp is reached relayOn = (LOW_TEMP < tempF); } Serial.print(" %\tRelay: "); if (relayOn) { //Only toggle fridge state if it changed if (currentFridgeState != 0) { toggleSwitch(true, true); currentFridgeState = 0; } Serial.print("On"); } else { //Only toggle fridge state if it changed if (currentFridgeState != 1) { toggleSwitch(false, true); currentFridgeState = 1; } Serial.print("Off"); } Serial.print("\tLoopCount: "); Serial.print(loopCount); Serial.print("/"); Serial.println(maxLoops); //Only update LED when temp changes if (prevTemp != tempFInt) { updateLED(tempFInt,3); //Update previous temp. prevTemp = tempFInt; } } else { // Print error message based on the error code. Serial.println(DHT11::getErrorString(result)); //Show error on LED updateLED("E1"); //After ten errors set tempF to zero to turn off fridge compressor if (errorCount > 10) tempF = 0; errorCount++; } //Every 15 minutes if (loopCount == 0) { //Ping the temp to the website, retry once if failed //Only ping the temp if it is not zero, zero on error if (tempF > 0) pingTemp(tempF, true); } delay(2000); loopCount++; if (loopCount > maxLoops) { //Reset loop cound so we ping temp next run loopCount = 0; //Also reset fridge state so we run the relay switch, next run currentFridgeState = -1; } } void updateLED(double temp, int maxLen) { char cstr[maxLen]; dtostrf(temp, maxLen, 1, cstr); updateLED(cstr); } void updateLED(int temp, int maxLen) { //Create char array out of tempFInt char cstr[maxLen]; itoa(temp, cstr, 10); //cstr[2] = '°'; updateLED(cstr); } void updateLED(char* cstr) { //Update the LCD matrix.beginDraw(); matrix.textFont(Font_5x7); matrix.beginText(1, 1, 0xFFFFFF); // X=0, Y=1, color=white (my board has just red) matrix.println(cstr); // Prints the temp matrix.endText(); matrix.endDraw(); } boolean pingTemp(double currentTemp, boolean retry) { String uri = "/updateTemp.php?temp="+String(currentTemp, 1); return hitURL(uri, retry); } boolean toggleSwitch(boolean state, boolean retry) { String uri = "/kasaSwitch.php?on="; if (state) uri = uri+"true"; else uri = uri + "false"; return hitURL(uri, retry); } boolean hitURL(String uri, boolean retry) { boolean result = false; if (client.connect(server, 443)) { Serial.println("Connected to server"); // Make an HTTP GET request client.print("GET "); client.print(uri); client.println(" HTTP/1.1"); client.print("Host: "); client.println(server); client.println("Connection: close"); client.println(); // Read the response while (client.connected()) { String line = client.readStringUntil('\n'); Serial.println(line); } client.stop(); //Serial.println("Disconnected from server"); result = true; } else { Serial.print("Connection failed, retry?: "); Serial.println(retry); if (retry) { WiFi.end(); reconnectWiFi(); //reconnect to wifi and retry once result = hitURL(uri,false); } else updateLED("CF"); } return result; }