vape heater
Tags: javascript, html, cloud, github
Published: 2023-12-25 16:11:07

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SimpleDHT.h>
#include <LowPower.h>
#include “i2c.h”
#include “i2c_BMP280.h”

BMP280 bmp280;
LiquidCrystal_I2C lcd(0x3f,20,4);

SimpleDHT22 dht22;

//Define the pins
const int ButtonPin = 2;
const int pinDHT22 = 3;
const int DownPowerPin = 4;
const int UpPowerPin = 5;

const int FiringPin = 10;
const int BuzzerPin = 8;
const int PowerBoost = 11;
const int LedPin = 13;


 

// Discrete Variable Declarations
int ButtonState = 0;
int PowerBoostState = 0;
int BuzzerShutoffOK = 1;
int PowerLevelRaw = 50;
int PowerLevelScaled = 0;
int PowerSetting = 10;
int UpPowerValue = 1;
int DownPowerValue = 1;

// BMP280 Vars
float BMPtemperature;
float BMPpascal;
static float BMPmeters, BMPmetersold;

// Timers
unsigned long ButtonMillisStart = 0;
unsigned long ButtonMillisTimeout = 6000;

unsigned long FlashMillisStart = 0;
unsigned long FlashMillisTimeout = 100;

unsigned long SleepMillisStart = 0;
unsigned long SleepMillisTimeout = 300000;

 

int ButtonTimeout = 0;
int VapeTime = 0;
int VapeTimeLast = 0;

// Counters
int MyCounter = 0;
int LightCounter = 0;

// One Shots
int ButtonRising = 0;
int ButtonFalling = 0;
int UpPowerONS = 0;
int DownPowerONS = 0;
char LastSecond;

 

// This is for the Real Time Clock

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
return( (val/16*10) + (val%16) );
}

 

//—————————————-
void wakeUp()
{
// Just a handler for the pin interrupt.
}
//—————————————-

 

void setup() {
Wire.begin();
Serial.begin(9600);

 

//Serial.print(“Probe BMP280: “);
if (bmp280.initialize()){ //Serial.println(“BMP Sensor found”);
}
else
{
//Serial.println(“BMP Sensor missing”);
while (1) {}
}

// onetime-measure:
bmp280.setEnabled(0);
bmp280.triggerMeasurement();

//LCD Setup
lcd.init(); //initialize the lcd

// RTC
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
//setDS3231time(30,16,18,7,1,9,2017);

 

// Setup Pin Modes
pinMode(DownPowerPin, INPUT_PULLUP);
pinMode(UpPowerPin, INPUT_PULLUP);
pinMode(ButtonPin, INPUT_PULLUP);
pinMode(BuzzerPin, OUTPUT);
pinMode(PowerBoost, INPUT_PULLUP);
pinMode(LedPin, OUTPUT);
lcd.noBacklight();


//Configure Wireless
lcd.backlight();
lcd.setCursor(0,1);
lcd.print(“Setting Mode”);
Serial.println(“AT+CWMODE=1”);
delay(1000);

lcd.setCursor(0,1);
lcd.print(“Connecting Network”);
Serial.println(“AT+CWJAP=\”yourSSID\”,\”yourPASSWORD\””);
delay(3000);

}

 

// —————————————
// RTC Logic for Setting clock
// —————————————
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year){
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}

void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}

// —————————————
// End of RTC Code
// —————————————

 

 

//—————————————-
void loop() {
// read the state of the pushbuttons:
ButtonState = digitalRead(ButtonPin);
PowerBoostState = digitalRead(PowerBoost);
DownPowerValue = digitalRead(DownPowerPin);
UpPowerValue = digitalRead(UpPowerPin);

// Get the BMP280 Data
bmp280.awaitMeasurement();
bmp280.getTemperature(BMPtemperature);
bmp280.getPressure(BMPpascal);
bmp280.getAltitude(BMPmeters);
BMPmetersold = (BMPmetersold * 10 + BMPmeters)/11;

bmp280.triggerMeasurement();

// get the milliseconds for the timers.
unsigned long CurrentMillis = millis();

//Low Power Setup
//attachInterrupt(0, wakeUp, LOW);
if ((CurrentMillis-SleepMillisStart) > SleepMillisTimeout){
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
}

 

// Power Level Control
if (!DownPowerValue){
if (DownPowerONS==0){
PowerLevelRaw=PowerLevelRaw-5;
}
DownPowerONS = 1;
if (PowerLevelRaw < 0){
PowerLevelRaw = 0;
}
}else{
DownPowerONS=0;
}

if (!UpPowerValue){
if (UpPowerONS==0){
PowerLevelRaw=PowerLevelRaw+5;
}
UpPowerONS = 1;
if (PowerLevelRaw > 255){
PowerLevelRaw = 255;
}
}else{
UpPowerONS=0;
}


 

 

PowerLevelScaled = PowerLevelRaw/1;
if (!PowerBoostState){
PowerLevelScaled=PowerLevelScaled*1.3;
}

 

 

// ************************* Button is PRESSED *************************
if (ButtonState == LOW) {
SleepMillisStart=CurrentMillis;
ButtonFalling = 0;

// turn on the backlight on lcd

if ((CurrentMillis -FlashMillisStart) < FlashMillisTimeout){
lcd.backlight();
}else if ((CurrentMillis -FlashMillisStart) < (FlashMillisTimeout *2)){
lcd.noBacklight();
} else {
FlashMillisStart = CurrentMillis;
}

 

VapeTime = ((CurrentMillis-ButtonMillisStart) / 1000);
if (VapeTime!=VapeTimeLast){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(“*** Running ***”);
lcd.setCursor(0,1);
if (PowerBoostState==1){
lcd.print (“High Power: “);
}else{
lcd.print (“Low Power: “);
}
lcd.print(VapeTime);
VapeTimeLast=VapeTime;
}

 

if ((CurrentMillis – ButtonMillisStart) < ButtonMillisTimeout){
digitalWrite(LedPin,HIGH);
analogWrite(FiringPin, PowerLevelScaled);
}else{
ButtonTimeout = 1;
digitalWrite(LedPin,LOW);
digitalWrite(BuzzerPin,HIGH);
analogWrite(FiringPin, 0);

// Clear One Shot

}
ButtonRising = 1;
}else{
// ************************* Button is RELEASED ************************
ButtonRising = 0;
if (LightCounter > 10000){LightCounter = 0;}
if (ButtonFalling == 0){LightCounter = 0;}
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
if (second!=LastSecond){
LightCounter++;
float temperature = 0;
float humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
//Serial.print(“Read DHT22 failed, err=”); Serial.println(err);delay(200);
return;
}

 

//lcd.backlight();
if (LightCounter > 300){
lcd.noBacklight();
}else{
lcd.backlight();
}
//lcd.clear();
lcd.setCursor(0,0);
lcd.print(hour);
lcd.print(“:”);
if (minute < 10){lcd.print(“0”);}
lcd.print(minute);
lcd.print(“:”);
if (second < 10){lcd.print(“0″);}
lcd.print(second);
lcd.print (” “);
temperature=temperature*1.8+32;
lcd.print(temperature);
lcd.print((char)223);
lcd.print (“F “);
LastSecond = second;

lcd.setCursor(0,1);
lcd.print(“Humidity: “);
lcd.print(humidity);
lcd.print(“% “);


lcd.setCursor(0,2);
lcd.print(“PSR: “);
lcd.print(BMPpascal/3386.38867);
lcd.print(” In Hg “);

lcd.setCursor(0,3);
lcd.print(“Raw Power: “);
lcd.print(PowerLevelScaled);
lcd.print(” “);
}
ButtonTimeout = 0;
ButtonMillisStart = CurrentMillis;
if (BuzzerShutoffOK == 1) {
digitalWrite(BuzzerPin,LOW);
}
digitalWrite(LedPin,LOW);
analogWrite(FiringPin, 0);

ButtonFalling = 1;
}

// Alarm Logic
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);

 

if ((hour==5) and (minute==0)){
//Serial.println(“Alarm”);
BuzzerShutoffOK=0;
digitalWrite(BuzzerPin,HIGH);
}else{
BuzzerShutoffOK=1;
}

 

if (!UpPowerValue and !DownPowerValue and !PowerBoostState){
analogWrite(FiringPin, 0);

lcd.clear();
lcd.setCursor(0,0);
lcd.print(“Opening Garage Door”);
lcd.setCursor(0,1);

lcd.setCursor(0,1);
lcd.print(“Connecting Door… “);
Serial.println(“AT+CIPSTART=\”TCP\”,\”192.168.1.178\”,80″);
delay(2000);

lcd.setCursor(0,1);
lcd.print(“Setting Length… “);
Serial.println(“AT+CIPSEND=20”);
delay(1000);

lcd.setCursor(0,1);
lcd.print(“Sending Command… “);
Serial.println(“GET /?DoorCMD=DC”);
Serial.println(“”);
delay(3000);

lcd.setCursor(0,1);
lcd.print(“Closing Connection “);
Serial.println(“AT+CIPCLOSE”);
delay(1000);

lcd.clear();

}
/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-heating-system
 */

#include <OneWire.h>
#include <DallasTemperature.h>

#define SENSOR_PIN  2   // Arduino pin connected to DS18B20 sensor's DQ pin
#define RELAY_PIN   A5  // Arduino pin connected to relay which connected to heating element

const int TEMP_THRESHOLD_UPPER = 20; // upper threshold of temperature, change to your desire value
const int TEMP_THRESHOLD_LOWER = 15; // lower threshold of temperature, change to your desire value

OneWire oneWire(SENSOR_PIN);         // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library

float temperature;    // temperature in Celsius

void setup() {
  Serial.begin(9600); // initialize serial
  sensors.begin();    // initialize the sensor
  pinMode(RELAY_PIN, OUTPUT); // initialize digital pin as an output
}

void loop() {
  sensors.requestTemperatures();             // send the command to get temperatures
  temperature = sensors.getTempCByIndex(0);  // read temperature in Celsius

  if(temperature > TEMP_THRESHOLD_UPPER) {
    Serial.println("The heating element is turned off");
    digitalWrite(RELAY_PIN, LOW); // turn off
  } else if(temperature < TEMP_THRESHOLD_LOWER){
    Serial.println("The heating element is turned on");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }

  delay(500);
}