arduino-fridge-powercontrol/main/main.ino
2023-08-08 22:25:27 +02:00

139 lines
3.1 KiB
C++

/* References:
* https://lastminuteengineers.com/multiple-ds18b20-arduino-tutorial/
*/
#include "ArduinoJson.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
// Data wire is conntec to the Arduino digital pin 4
#define ONE_WIRE_BUS 2
#define MOSFET1 3
#define MOSFET2 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
DeviceAddress Thermometer;
int deviceCount = 0;
int analogPin = 2;
int data = 0;
char userInput;
char* commandVer;
// temperature values in °C
float tempSensor1;
float tempSensor2;
// HighTemp - when to turn on the power
float HTemp1 = 12.0;
float HTemp2 = 16.0;
// LowTemp - when to turn of the power
float LTemp1 = 10.0;
float LTemp2 = 14.0;
bool fetState1;
bool fetState2;
uint8_t addrSensor1[8] = { 0x28, 0xFF, 0x64, 0x1F, 0x79, 0xD1, 0xB1, 0x75 };
uint8_t addrSensor2[8] = { 0x28, 0xFF, 0x64, 0x1F, 0x79, 0xD7, 0xDA, 0x9A };
void setup() {
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("*fridge control*"); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (0, 0)
lcd.print(" by DeltaLima");
delay(1500);
// display default values
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1: H");
lcd.print(HTemp1);
lcd.print(" L");
lcd.print(LTemp1);
lcd.setCursor(0, 1);
lcd.print("2: H");
lcd.print(HTemp2);
lcd.print(" L");
lcd.print(LTemp2);
delay(3500);
// init MOSFET Pins
pinMode(MOSFET1, OUTPUT);
pinMode(MOSFET2, OUTPUT);
Serial.begin(9600);
sensors.begin();
}
void loop() {
// get sensor values
sensors.requestTemperatures();
tempSensor1 = sensors.getTempC(addrSensor1);
tempSensor2 = sensors.getTempC(addrSensor2);
if(tempSensor1 > HTemp1) {
digitalWrite(MOSFET1, HIGH);
fetState1 = 1;
} else if(tempSensor1 < LTemp1) {
digitalWrite(MOSFET1, LOW);
fetState1 = 0;
}
if(tempSensor2 > HTemp2) {
digitalWrite(MOSFET2, HIGH);
fetState2 = 1;
} else if(tempSensor1 < LTemp1) {
digitalWrite(MOSFET2, LOW);
fetState2 = 0;
}
// LCD Display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("1: ");
lcd.print(tempSensor1);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(14,0);
lcd.print("|");
lcd.print(fetState1);
lcd.setCursor(0, 1);
lcd.print("2: ");
lcd.print(tempSensor2);
lcd.print(" ");
lcd.print((char)223);
lcd.print("C");
lcd.setCursor(14,1);
lcd.print("|");
lcd.print(fetState2);
// JSON
StaticJsonDocument<96> jsonOut;
//jsonOut["sensor"] = "1";
jsonOut["1"]["temp"] = tempSensor1;
jsonOut["1"]["state"] = fetState1;
//jsonOut["sensor"] = "2";
jsonOut["2"]["temp"] = tempSensor2;
jsonOut["2"]["state"] = fetState2;
serializeJson(jsonOut, Serial);
Serial.println();
delay(5000);
}