Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define OvenRelayPin 21
- #define TempDO 19
- #define TempCS 23
- #define TempCLK 5
- #define FlamePin 18
- #define buzzerPin 22
- #define BLYNK_TEMPLATE_ID "TMPLY0JjXp3H"
- #define BLYNK_DEVICE_NAME "IOTOven"
- #define BLYNK_AUTH_TOKEN "Lz1WZTaGaiTvRg9EDSpRBQvJbaTVQZJB"
- #define BLYNK_FIRMWARE_VERSION "0.1.0"
- #define BLYNK_PRINT Serial
- #define APP_DEBUG
- #include "max6675.h"
- #include "BlynkEdgent.h"
- #include <TimeLib.h>
- int thermoDO = TempDO;
- int thermoCS = TempCS;
- int thermoCLK = TempCLK;
- int setTemp;
- int Temp = 0;
- int FlameStatus = 0;
- MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
- BlynkTimer timer;
- BlynkTimer timer2;
- bool led_set;
- long timer_start_set = 0xFFFF;
- long timer_stop_set = 0xFFFF;
- unsigned char weekday_set;
- long rtc_sec;
- unsigned char day_of_week;
- bool led_status;
- bool update_blynk_status;
- bool led_timer_on_set;
- // #########################################################################################################
- // LED 1
- BLYNK_WRITE(V0)
- {
- int val = param.asInt();
- if ( led_timer_on_set == 0 )
- led_set = val;
- else
- update_blynk_status = 1;
- }
- // setTemp
- BLYNK_WRITE(V1)
- {
- int SsetTemp = param.asInt();
- setTemp = SsetTemp;
- Serial.print("setTemp = ");
- Serial.println(setTemp);
- }
- // Timer 1
- BLYNK_WRITE(V2)
- {
- unsigned char week_day;
- TimeInputParam t(param);
- if (t.hasStartTime() && t.hasStopTime() )
- {
- timer_start_set = (t.getStartHour() * 60 * 60) + (t.getStartMinute() * 60) + t.getStartSecond();
- timer_stop_set = (t.getStopHour() * 60 * 60) + (t.getStopMinute() * 60) + t.getStopSecond();
- Serial.println(String("Start Time: ") +
- t.getStartHour() + ":" +
- t.getStartMinute() + ":" +
- t.getStartSecond());
- Serial.println(String("Stop Time: ") +
- t.getStopHour() + ":" +
- t.getStopMinute() + ":" +
- t.getStopSecond());
- for (int i = 1; i <= 7; i++)
- {
- if (t.isWeekdaySelected(i))
- {
- week_day |= (0x01 << (i - 1));
- Serial.println(String("Day ") + i + " is selected");
- }
- else
- {
- week_day &= (~(0x01 << (i - 1)));
- }
- }
- weekday_set = week_day;
- }
- else
- {
- timer_start_set = 0xFFFF;
- timer_stop_set = 0xFFFF;
- }
- }
- BLYNK_WRITE(InternalPinRTC)
- {
- const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
- unsigned long blynkTime = param.asLong();
- if (blynkTime >= DEFAULT_TIME)
- {
- setTime(blynkTime);
- day_of_week = weekday();
- if ( day_of_week == 1 )
- day_of_week = 7;
- else
- day_of_week -= 1;
- rtc_sec = (hour() * 60 * 60) + (minute() * 60) + second();
- Serial.println(blynkTime);
- Serial.println(String("RTC Server: ") + hour() + ":" + minute() + ":" + second());
- Serial.println(String("Day of Week: ") + weekday());
- }
- }
- // #########################################################################################################
- BLYNK_CONNECTED()
- {
- Blynk.sendInternal("rtc", "sync");
- }
- // #########################################################################################################
- void checkFlame() {
- FlameStatus = !digitalRead(FlamePin);
- if (FlameStatus > 0) {
- led_status = 0;
- Blynk.virtualWrite(V0, 0);
- Blynk.syncVirtual(V0);
- digitalWrite(OvenRelayPin, 0);
- digitalWrite(buzzerPin, 1);
- Serial.print("Flame Status:");
- Serial.println(FlameStatus);
- Serial.println("ALERT!!!!!");
- Blynk.logEvent("fire");
- Blynk.virtualWrite(V3, FlameStatus);
- }
- else{
- digitalWrite(buzzerPin, 0);
- }
- Blynk.virtualWrite(V3, FlameStatus);
- }
- void checkTime() {
- Blynk.sendInternal("rtc", "sync");
- Temp = thermocouple.readCelsius();
- Serial.print("C = ");
- Serial.println(Temp);
- }
- // #########################################################################################################
- void led_mng()
- {
- bool time_set_overflow;
- bool led_status_buf;
- led_status_buf = led_status;
- time_set_overflow = 0;
- if ( timer_start_set != 0xFFFF && timer_stop_set != 0xFFFF)
- {
- if ( timer_stop_set < timer_start_set ) time_set_overflow = 1;
- if ((((time_set_overflow == 0 && (rtc_sec >= timer_start_set) && (rtc_sec < timer_stop_set)) ||
- (time_set_overflow && ((rtc_sec >= timer_start_set) || (rtc_sec < timer_stop_set)))) &&
- (weekday_set == 0x00 || (weekday_set & (0x01 << (day_of_week - 1) )))) )
- {
- led_timer_on_set = 1;
- }
- else
- led_timer_on_set = 0;
- }
- else
- led_timer_on_set = 0;
- if ( led_timer_on_set )
- {
- led_status = 1;
- led_set = 0;
- }
- else
- {
- led_status = led_set;
- }
- if ( led_status_buf != led_status ) {
- update_blynk_status = 1;
- }
- // HARDWARE CONTROL
- //digitalWrite(OvenRelayPin, led_status);
- if (setTemp > Temp && led_set == 1) {
- digitalWrite(OvenRelayPin, led_status);
- }
- else if(setTemp < Temp && led_set == 1){
- digitalWrite(OvenRelayPin, 0);
- }
- else{
- digitalWrite(OvenRelayPin, led_status);
- }
- }
- // #########################################################################################################
- void blynk_update()
- {
- if ( update_blynk_status )
- {
- update_blynk_status = 0;
- Blynk.virtualWrite(V0, led_status);
- }
- }
- // #########################################################################################################
- void setup()
- {
- Serial.begin(115200);
- delay(100);
- pinMode(OvenRelayPin, OUTPUT);
- pinMode(buzzerPin, OUTPUT);
- pinMode(FlamePin, INPUT);
- digitalWrite(buzzerPin, 0);
- digitalWrite(OvenRelayPin, 0);
- BlynkEdgent.begin();
- timer.setInterval(10000L, checkTime);
- timer2.setInterval(100L, checkFlame);
- }
- void loop() {
- BlynkEdgent.run();
- timer.run();
- timer2.run();
- led_mng();
- blynk_update();
- }
Advertisement
Add Comment
Please, Sign In to add comment