/* This code is working for on/off International Market Product PoC on ACM..
Author:@hpdjoy
([email protected])
©Paratronics technonlogy and services.
*/
//define the blynk credentials here......
#define BLYNK_TEMPLATE_ID "TMPL3AkaNDoWc"
#define BLYNK_TEMPLATE_NAME "ACM V2"
#define BLYNK_AUTH_TOKEN "idH4XrI8m82Ypf3i5WIrqIAoXo-92DKx"
//Define the Pins of the sensors motors and actuators here......
#define RLY_IND_CKR1 19 // induction cooker on off button
#define RLY_Temp_IND_CKR1_Plus 18 // induction temprature increase button
#define RLY_Temp_IND_CKR1_Minus 5 // induction temprature decrease button
#define ps_pot_sens 32 // proximity sensor
#define LoadCell_Rice_DT 15 // loadcell pins for rice
#define LoadCell_Rice_SCK 2
#define LoadCell_Water_DT 0// loadcell pins for Water
#define LoadCell_Water_SCK 4
#define Water_VLV 9// motor that will fetch water
#define Rice_VLV 10 //Decide it
#include <WiFi.h>
#include "HX711.h"
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Variables used in this programs are declared here.
int potStatus=0;
//Assign ESP32 WiFI Credential........
char ssid[] = "hpd";
char pass[] = "12345679";
char auth[] = BLYNK_AUTH_TOKEN;// we can use diffrent token for diffrent cooking menu using multiple app.
float calibration_factor = -130460; // This -130460 value is obtained using the SparkFun_HX711_Calibration sketch FOR 1100g MASS
int cookingStatus=0;
int isWaterPoured =0;
int isRicePoured = 0;
float dispRice, currRice,initRice;
bool initRiceRead= false;
float dispWater,currWater,initWater;
bool initWaterRead = false;
HX711 scale1,scale2; // load cell Variables
BlynkTimer timer;
void setup() {
Serial.begin(115200);
//Attempt WiFi connection (This code can be removed from the actual code, not necessary for functionality)
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(1200); // Wait for suggested comeback time
Serial.print(".");
}
if(WiFi.status() == WL_CONNECTED){
Serial.print("\n");
Serial.println("Connected to WIFI");
}
Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
if (Blynk.connected()) {
Serial.println("Connected to Blynk app");
} else {
Serial.println("FAILED to connect to Blynk app");
}
//Initiate the pins and functionality here...
pinMode(RLY_IND_CKR1, OUTPUT);
pinMode(RLY_Temp_IND_CKR1_Plus, OUTPUT);
pinMode(RLY_Temp_IND_CKR1_Minus, OUTPUT);
pinMode(ps_pot_sens,INPUT);
//Intititating all the actuators to off State.(for safty reasons only)
digitalWrite(RLY_IND_CKR1, HIGH);
digitalWrite(RLY_Temp_IND_CKR1_Plus, HIGH);
digitalWrite(RLY_Temp_IND_CKR1_Minus, HIGH);
//rice load cell
scale1.begin(LoadCell_Rice_DT, LoadCell_Rice_SCK);
scale1.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
//Water load cell
scale2.begin(LoadCell_Water_DT, LoadCell_Water_SCK);
scale2.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch.
}
void loop() {
Blynk.run();// blynk app sends commands to esp32 for 1:turning on/off induction 2: inc/dec temp of induction
sendRiceWeightToBlynk(); //loadcell data for rice has sent to blynk app.
readDataFromPotSens();// r
sendWaterWeightToBlynk();
if(cookingStatus==1 ){
cooking();
}
}
// ON/OFF logic for induction power.
BLYNK_WRITE(V0) {
int relayState = param.asInt();//Takes input from blynk app...
if (relayState == 1){
digitalWrite(RLY_IND_CKR1, LOW); //turns on the induction main switch
Serial.println("button pressed for ON");
delay(100);
digitalWrite(RLY_IND_CKR1, HIGH);
delay(100);
} else if (relayState == 0) {
digitalWrite(RLY_IND_CKR1, LOW); //turns off the induction main switch
Serial.println("button pressed for OFF");
delay(100);
digitalWrite(RLY_IND_CKR1, HIGH);
delay(100);
}
}
// temprature increasing logic
BLYNK_WRITE(V1){
int relayState1 = param.asInt();
if (relayState1 == 1) {
digitalWrite(RLY_Temp_IND_CKR1_Plus, LOW);//turns on the induction temp positive switch
Serial.println("KEY pressed for temp+");
delay(100);
digitalWrite(RLY_Temp_IND_CKR1_Plus, HIGH);
}
}
// temprature decreasing logic
BLYNK_WRITE(V2){
int relayState2 = param.asInt();
if (relayState2 == 1) {
digitalWrite(RLY_Temp_IND_CKR1_Minus, LOW);
Serial.println("KEY pressed for temp-");
delay(100);
digitalWrite(RLY_Temp_IND_CKR1_Minus, HIGH);
}
}
// reading proximity to detect the pan.
int readDataFromPotSens(){
int psPotValue = digitalRead(ps_pot_sens);
if(psPotValue==1){
Serial.printf("POT detected");
}else{
Serial.print("POT not detected");
//readDataFromPotSens();// I was trying to use recursive loop.
}
return 1;
}
// rice weight measurement
void sendRiceWeightToBlynk() {
scale1.set_scale(calibration_factor);
Serial.print("Reading: ");
float weight = scale1.get_units(1); // Get weight reading in Kg
if(initRiceRead==false){
initRice = weight;
Serial.print("Initial Rice Weight");
Serial.print(initRice);
Serial.print(.Kg);
initRiceRead=true;
}
currRice=weight;
Serial.print(weight, 1);
Serial.print("Kg"); // Change this to kg and re-adjust the calibration factor if you follow SI units.
Serial.println();
Blynk.virtualWrite(V5, weight); // Send weight data to Blynk Gauge widget on virtual pin V5
}
// water weight measurement
void sendWaterWeightToBlynk(){
scale2.set_scale(calibration_factor);
Serial.print("Reading: ");
float waterWeight = scale2.get_units(1); // Get weight reading in Kg
if(initWaterRead==false){
initWater = waterWeight ;
Serial.print("Initiate water Weight");
Serial.print(initWater);
Serial.print(.Kg);
initWaterRead=true;
}
currWater = waterWeight;
Serial.print(waterWeight, 1);
Serial.print("Kg"); // Change this to kg and re-adjust the calibration factor if you follow SI units.
Serial.println();
Blynk.virtualWrite(V6, waterWeight); // Sent weight data to Blynk Gauge widget on virtual pin V6
}
// this button on blynk app to give final command to cook..
BLYNK_WRITE(V7){
int cookingStatus = param.asInt();
if(cookingStatus==1){
Serial.print("Cooking.....");
}
else{
Serial.print("Cooking not started yet......");
}
}
void cooking(){
isWaterPoured = waterPouring(dispWater, currWater,initWater);// Cheaked!
if(isWaterPoured!=0){
isRicePoured=ricePouring(dispRice,currRice,initRice);
if( isRicePoured !=0){
cook();
}
}
}
int waterPouring(float dispWater, float currWater,float initWater){
if (dispWater <= (initWater-currWater )) {
// Turn on the water valve
digitalWrite(Water_VLV, HIGH);
return 0;
} else {
// Turn off the water valve
digitalWrite(Water_VLV, LOW);
Serial.println("water disptched !");
return 1;
}
}
int ricePouring(float dispRice,float currRice,float initRice){
if (dispRice <= (initRice-currRice )) {
// Turn on the water valve
digitalWrite(Rice_VLV, HIGH);
return 0;
} else {
// Turn off the water valve
digitalWrite(Rice_VLV,LOW);
Serial.println("Rice disptched !");
return 1;
}
}
void cook(){
Serial.println("cooking started........");
digitalWrite(RLY_IND_CKR1, HIGH);
delay(1000);
if (digitalRead(RLY_IND_CKR1) != LOW) {
cook();
} else {
Serial.println("Cooking done :) ");
delay(1000);
exit(0);
}
}