nord vpnnord vpn
Ad

Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
3.6 kB
0
Indexable
Never
#define BLYNK_TEMPLATE_ID "TMPL5AZgmVJ64"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#define BLYNK_AUTH_TOKEN "w-uFgyBo5_owTPKI021RAu_Y9WShfRnH"
#define BLYNK_PRINT Serial            
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h>  // For OTA with ESP8266
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA
#include <Wire.h>
#include "PCF85063TP.h"

PCD85063TP cloc;
uint8_t hour, minute, second, month, dayOfMonth, year, dayOfWeek, startTime, endTime, currTime;
uint32_t start;
uint32_t stop;
int moisture, temp,humidity,mappedValue;

BlynkTimer timer;

void checkPhysicalButton();


int relay1State = LOW;
int pushButton1State = HIGH;

char ssid[] = "justpixit";
char pass[] = "Pixit123";

#define RELAY_PIN_1      D6   //D6
#define PUSH_BUTTON_1     D7   //D4
#define VPIN_BUTTON_1    V3 
#define OTA_HOSTNAME "Home_Automation"


BLYNK_CONNECTED() {

  // Request the latest state from the server

  Blynk.syncVirtual(VPIN_BUTTON_1);

  // Alternatively, you could override server state using:
 // Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
 // Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
 // Blynk.virtualWrite(VPIN_BUTTON_3, relay3State);
 // Blynk.virtualWrite(VPIN_BUTTON_4, relay4State);

}

// When App button is pushed - switch the state

BLYNK_WRITE(VPIN_BUTTON_1) {
  relay1State = param.asInt();
  digitalWrite(RELAY_PIN_1, relay1State);
}


void checkPhysicalButton()
{
  if (digitalRead(PUSH_BUTTON_1) == LOW) {
    // pushButton1State is used to avoid sequential toggles
    if (pushButton1State != LOW) {

      // Toggle Relay state
      relay1State = !relay1State;
      digitalWrite(RELAY_PIN_1, relay1State);

      // Update Button Widget
      Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
      Serial.println("Relay ON"); 
    }
    pushButton1State = LOW;
  } else {
    pushButton1State = HIGH; 
    Serial.println("Relay OFF"); 
  }

}
int mapValue(int x, int in_min, int in_max, int out_min, int out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void setup()
{

  Serial.begin(115200);
  cloc.begin();
  Wire.begin();
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  ArduinoOTA.setHostname(OTA_HOSTNAME);  // For OTA - Use your own device identifying name
  ArduinoOTA.begin();  // For OTA
  Wire.setClock(100000);
  //Serial.print(stat, HEX);
  Serial.println();
  //cloc
  uint8_t ret = cloc.calibratBySeconds(0, -0.000041);
  Serial.print("offset value: ");
  Serial.print("0x");
  Serial.println(ret, HEX);

  pinMode(RELAY_PIN_1, OUTPUT);
  pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
  digitalWrite(RELAY_PIN_1, relay1State);


  // Setup a function to be called every 100 ms
  timer.setInterval(500L, checkPhysicalButton);
}
void myTimerEvent()
{
  Blynk.virtualWrite(V0, mappedValue);
  delay(10);
}
void loop()
{
  Blynk.run();
  ArduinoOTA.handle();  // For OTA
  moisture = analogRead(A0);
  mappedValue = mapValue(moisture, 250, 600, 100, 0); //change mapping
  Serial.print("moisture: ");
  Serial.print(mappedValue, 1);
  Serial.print("  Actual Moisture: ");
  Serial.println(moisture);
  delay(2500);
  timer.run();
  relaycontrol();
  delay(5);
  Blynk.virtualWrite(V0, mappedValue);
}
void relaycontrol(){
  if (mappedValue > 70){  
    digitalWrite(RELAY_PIN_1, HIGH);
    Serial.println("Relay Off"); 
  }
  if (mappedValue < 25){
    digitalWrite(RELAY_PIN_1, LOW);
    Serial.println("Relay On"); 
  }
}


nord vpnnord vpn
Ad