#define BLYNK_TEMPLATE_ID "x"
#define BLYNK_TEMPLATE_NAME "Kiln"
#define BLYNK_AUTH_TOKEN "x"
#define BLYNK_PRINT Serial
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
BlynkTimer timer;
char auth [] = BLYNK_AUTH_TOKEN;
//pins:
// Define the analogue pin used to read the temperature sensor (A0) */
const int LM35Pin0 = A0;
const int LM35Pin1 = A1;
const int LM35Pin2 = A2;
//////////////////
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 16, 2);
////////////////
//temp 0 = right hand side of kiln, temp 1 = center, temp 2 = left
float temperature0;
float temperature1;
float temperature2;
int lightonetemp;
int lighttwotemp;
int lightthreetemp;
int maxtemp;
int maxtempreached = 0;
int maxtempvariable;
int maxtempalert;
int powertoggle = 0;
int light = 0;
int alreadyalerted = 0;
/////////////////////////
//ethernet setup
IPAddress ip(192, 168, 1, 160);
IPAddress dns(192, 168, 1, 254);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
/////////////////////////
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V3);
Blynk.syncVirtual(V4);
Blynk.syncVirtual(V5);
Blynk.syncVirtual(V6);
Blynk.syncVirtual(V7);
Blynk.syncVirtual(V9);
}
BLYNK_WRITE(1)
{ if ( param.asInt() == 0 )
{
digitalWrite(5, HIGH); //right socket
}
else
{
digitalWrite(5, LOW);
}
}
BLYNK_WRITE(2)
{ if ( param.asInt() == 0 )
{
digitalWrite(2, HIGH); //middle socket
}
else
{
digitalWrite(2, LOW);
}
}
BLYNK_WRITE(3)
{ if ( param.asInt() == 0 )
{
digitalWrite(3, HIGH); //left socket
}
else
{
digitalWrite(3, LOW);
}
}
BLYNK_WRITE(9)
{ if ( param.asInt() == 1 )
{
powertoggle = 0;
}
else
{
powertoggle = 1;
Blynk.virtualWrite(V8, "Kiln Off");
Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V3, 0);
digitalWrite(5, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}
}
BLYNK_WRITE(10)
{
if ( param.asInt() == 1 )
{
light = 0;
maxtempreached = 0;
alreadyalerted = 0;
Blynk.virtualWrite(V8, "Reset Temps"); //info box
Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V3, 0);
digitalWrite(5, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}
}
BLYNK_WRITE(V4) //temp to turn light one on
{
lightonetemp = param.asInt(); // Get value as integer
}
BLYNK_WRITE(V5) //temp to turn light one on
{
lighttwotemp = param.asInt(); // Get value as integer
}
BLYNK_WRITE(V6) //temp to turn light one on
{
lightthreetemp = param.asInt(); // Get value as integer
}
BLYNK_WRITE(V7) //temp to turn lights off
{
maxtemp = param.asInt(); // Get value as integer
}
/////////////////////////
void setup()
{
Serial.begin(9600);
delay(10);
pinMode(5, OUTPUT); //right hand socket
pinMode(2, OUTPUT); //middle socket
pinMode(3, OUTPUT); //left socket
analogReference(INTERNAL);
Ethernet.begin(ip, dns, gateway, subnet);
Blynk.begin(auth, "blynk.cloud", 8080);
timer.setTimeout(3600000L, [] () {} ); // dummy/sacrificial Function timers with ID 0 cannot be turned on/off
timer.setInterval(10000, UpdateTemp); // Miliseconds to seconds
//LCD setup
lcd.init(); //initialize the lcd
lcd.backlight(); //open the backlight
Blynk.virtualWrite(V8, "Waiting Light one Temp");
Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V3, 0);
digitalWrite(5, HIGH); //sockets off
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
}
void loop()
{
Blynk.run();
timer.run();
}
void UpdateTemp()
{
maxtempvariable = maxtemp - 2;
maxtempalert = maxtemp + 3;
// Read the analog value from the LM35 sensor
temperature0 = analogRead(LM35Pin0) * 0.1039; //throw away, I don't believe this is required but just incase it helps make a difference smoothing results between different analogue pins
for (int x = 0; x < 64; x++) // 64(max) analogue readings for averaging
{
temperature0 = temperature0 + analogRead(LM35Pin0); // add each value
}
temperature1 = analogRead(LM35Pin1) * 0.1039; //throw away
for (int x = 0; x < 64; x++)
{
temperature1 = temperature1 + analogRead(LM35Pin1);
}
temperature2 = analogRead(LM35Pin2) * 0.1039; //throw away
for (int x = 0; x < 64; x++)
{
temperature2 = temperature2 + analogRead(LM35Pin2);
}
temperature0 = temperature0 * 0.1039 / 64; //calibration to degress c divide by number of readings
temperature1 = temperature1 * 0.1039 / 64;
temperature2 = temperature2 * 0.1039 / 64;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(("Center Temp:") + String(temperature1) + ("C"));
lcd.setCursor(0, 1);
lcd.print(("Left Temp:") + String(temperature2) + ("C"));
Blynk.virtualWrite(V20, temperature0);
Blynk.virtualWrite(V21, temperature1);
Blynk.virtualWrite(V22, temperature2);
if (powertoggle == 0)
{
if (temperature2 > lightonetemp && maxtempreached == 0 && light == 0)
{
light = 1;
digitalWrite(3, LOW); //left socket on
Blynk.virtualWrite(V8, "Light 1 on"); //info box
Blynk.virtualWrite(V3, 1);
}
if (temperature2 > lighttwotemp && maxtempreached == 0 && light == 1)
{
light = 2;
digitalWrite(2, LOW); //middle socket on
Blynk.virtualWrite(V8, "Light 1 and 2 on");
Blynk.virtualWrite(V2, 1);
Blynk.logEvent("update", "Light 2 is now on!");
}
if (temperature2 > lightthreetemp && maxtempreached == 0 && light == 2)
{
light = 3;
digitalWrite(5, LOW); //right socket on
Blynk.virtualWrite(V8, "Light 1, 2 and 3 on");
Blynk.virtualWrite(V1, 1);
Blynk.logEvent("update", "Light 3 is now on!");
}
if (temperature2 > maxtemp)
{
maxtempreached = 1;
digitalWrite(5, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
Blynk.virtualWrite(V8, "Max Temp reached, idling");
Blynk.virtualWrite(V1, 0);
Blynk.virtualWrite(V2, 0);
Blynk.virtualWrite(V3, 0);
if (alreadyalerted == 0)
{
Blynk.logEvent("update", "Kiln has reaced max Temperature! Record the time");
alreadyalerted = 1;
}
}
if (temperature2 < maxtempvariable && maxtempreached == 1)
{
digitalWrite(5, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
Blynk.virtualWrite(V8, "Reheating to max temp");
Blynk.virtualWrite(V1, 1);
Blynk.virtualWrite(V2, 1);
Blynk.virtualWrite(V3, 1);
}
}
if (temperature0 > maxtempalert)
{
Blynk.logEvent("update", "Kiln Temp 0 too Hot!");
}
if (temperature1 > maxtempalert)
{
Blynk.logEvent("update", "Kiln Temp 1 too Hot!");
}
if (temperature2 > maxtempalert)
{
Blynk.logEvent("update", "Kiln Temp 2 too Hot!");
}
}