I’m looking to send data from my physical RTC connected to my Arduino to my Blynk app. It is important that my program rely on a physical clock to ensure fluid time keeping in the event of network disconnections. Is it possible to send this kind of data to one of the widgets? My thought was to use terminal.println, but after compile failures and research, I’m lead to believe this is just for generic prewritten words and numbers. Any insights?
#include "SPI.h"
#include "Ethernet.h"
#include "BlynkSimpleEthernet.h"
#include "Wire.h"
#include "DHT.h"
#include "SimpleTimer.h"
#include "RTClib.h" // RealTimeClock Library for DS1307 and DS3231
//**********DEFINE PINS****************************
#define DHTPIN 13 // DHT22 sensor connected to digital pin 13, there is a 1k pullup resistor from +5V to pin 13
#define Relay_A 30 // relay 1 - Mega Pin 30 // Lights
#define Relay_B 31 // relay 2 - Mega Pin 31 // Feed Pump(s)
#define Relay_C 32 // relay 3 - Mega Pin 32 // ------------
#define Relay_D 33 // relay 4 - Mega Pin 33 // ------------
#define Relay_E 34 // relay 5 - Mega Pin 34 // Exhaust Fan
#define Relay_F 35 // relay 6 - Mega Pin 35 // Heater
#define Relay_G 37 // relay 7 - Mega Pin 36 // Dehumidifier
#define Relay_H 36 // relay 8 - Mega Pin 37 // ------------
//*******Sensor Model********************************
#define DHTTYPE DHT22
//*******Sets Relays to Off Position*****************
#define TURN_ON 0 // TURN_ON and TURN_OFF are defined to account for Active High relays
#define TURN_OFF 1 // Used to switch relay states for on/off of AC devices
//*******DHT Reference Values - CHANGE THESE TO MANAGE YOUR ROOM'S CLIMATE - //
byte hiMaxTemp = 90; // temp that triggers heat removal fan on
byte lowMaxTemp = 75; // temp that triggers heat removal fan off
byte hiMinTemp = 70; // temp that triggers heater on
byte lowMinTemp = 60; // temp that triggers heater off
byte hiHum = 75; // High humidity value that triggers dehumidifier on
byte lowHum = 70; // Low humidity value that triggers dehumidifier off
RTC_DS1307 RTC;
float UTCOffset = -5.0; // Your timezone relative to UTC (http://en.wikipedia.org/wiki/UTC_offset)
char auth[] = "abc123";
DHT dht(DHTPIN, DHTTYPE);
byte h;
byte f;
//WidgetTerminal terminal(V3);
SimpleTimer timer;
void setup() {
Serial.begin(9600);
//Serial2.begin() // For other baud rates
//Serial3.begin() // For other baud rates
Blynk.begin(auth);
pinMode(DHTPIN, OUTPUT);
dht.begin();
timer.setInterval(10000L, climateCheck); // 10 seconds between sensor readings
//---(Set pins 30-37 as outputs )----
pinMode(Relay_A, OUTPUT);
pinMode(Relay_B, OUTPUT);
pinMode(Relay_C, OUTPUT);
pinMode(Relay_D, OUTPUT);
pinMode(Relay_E, OUTPUT);
pinMode(Relay_F, OUTPUT);
pinMode(Relay_G, OUTPUT);
pinMode(Relay_H, OUTPUT);
digitalWrite(Relay_A, TURN_OFF); // This ensures relays do not actuate until called upon
digitalWrite(Relay_B, TURN_OFF);
digitalWrite(Relay_C, TURN_OFF);
digitalWrite(Relay_D, TURN_OFF);
digitalWrite(Relay_E, TURN_OFF);
digitalWrite(Relay_F, TURN_OFF);
digitalWrite(Relay_G, TURN_OFF);
digitalWrite(Relay_H, TURN_OFF);
RTC.begin();
RTC.adjust(DateTime(__DATE__, __TIME__)); /* This captures the time from the computer that is uploading sketch to
//Arduino, so ensure that the uploading computer has the correct time. */
delay(1000);
}
void loop()
{
Blynk.run();
timer.run();
}
void climateCheck()
{
h = dht.readHumidity();
f = dht.readTemperature(true);
Blynk.virtualWrite(V1, f); // Set Virtual Pin 1 frequency to PUSH in Blynk app
Blynk.virtualWrite(V2, h); // Set Virtual Pin 2 frequency to PUSH in Blynk app
}
void RTCdisplay()
{
//**********************************************************************************//
//****** Setting Time and testing to display 24 hour format as 12 hour format ******//
//**********************************************************************************//
DateTime now = RTC.now(); // reads time at beginning of loop
byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
byte zeroHour = 12; // Variable use to convert "0" zero hour to display it as 12:00+
if (now.hour() == 0) // First we test if the hour reads "0"
{
//terminal.println((zeroHour) ":" (now.minute) ":" (now.second) "AM");
//terminal.flush();
Serial.print(zeroHour); // if yes, serial print a "12" instead
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.println (F("AM"));
}
else if (now.hour() >= 13) // if no, Second we test if the hour reads "13 or more"
{
//terminal.println((twelveHour) ":" (now.minute) ":" (now.second) "PM");
//terminal.flush();
Serial.print(twelveHour); // if yes, serial print that current hour minus 12
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.println (F("PM"));
}
else
{
//terminal.println((now.hour) ":" (now.minute) ":" (now.second) "AM");
//terminal.flush();
Serial.print(now.hour(), DEC); // if no, Third we conclude that the am hours are being displayed correctly.
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.print(' ');
Serial.println (F("AM"));
}
{
//terminal.println((now.month, DEC) "/" (now.day, DEC) "/" (now.year, DEC));
//terminal.flush();
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print('/');
Serial.println(now.year(), DEC);
Serial.println();
}
//******************************************************************************************************//
//***** Alarms for Relays A-D. Adjust hours and minutes in accordance with 24 hour time format. *******//
//***** Create tests where true is ON time and false is OFF time. **************************************//
//***** 18/6 Light Cycle - RELAY_A *********************************************************************//
boolean relayAstate = false;
if (now.hour() >= 6) relayAstate = true; // The test is true from 6-23, and false from 0-5 (18/6)
if (relayAstate == true)
{
digitalWrite(Relay_A, TURN_ON);
Serial.print("\t");
Serial.print(F("Vegetative Lights On")); // Text printed to serial monitor
Serial.print("\t");
Serial.println();
}
else
{
digitalWrite(Relay_A, TURN_OFF);
Serial.print("\t");
Serial.println(F("Vegitative Lights OFf")); // Text printed to serial monitor
Serial.print("\t");
Serial.println();
}
//*********************************** FEED TIMEs - RELAY_B ******************************************//
// By testing hour once and minute twice, we isolate the "on" time and the duration it is "on". *****//
// If the Boolean becomes true, D31 is HIGH until Boolean becomes false again. **********************//
boolean relayBstate = false;
if (now.hour() == 9 && now.minute() >= 0 && now.minute() < 10) relayBstate = true; //9:00 am - 10mins
if (now.hour() == 15 && now.minute() >= 30 && now.minute() < 40) relayBstate = true; //3:30 pm - 10mins
if (now.hour() == 22 && now.minute() >= 30 && now.minute() < 33) relayBstate = true; //10:30pm - 03mins
if (relayBstate == true)
{
digitalWrite(Relay_B, TURN_ON);
Serial.print("\t");
Serial.println(F("Feeding Plants")); // Text printed to serial monitor
Serial.print("\t");
Serial.println();
}
else
{
digitalWrite(Relay_B, TURN_OFF);
}
}
//********************** Unassigned - RELAY_C *******************************//
//********************** Unassigned - RELAY_D *******************************//
//******************************** RELAYS E^F^G^H ****************************************//
//******* Testing the DHT22 sensor temperature in Fahrenheit and humidity in Percent and *//
//******* actuating Relays based upon those values ***************************************//
//****************************************************************************************//
void climaticEvents()
{
h = dht.readHumidity();
f = dht.readTemperature(true);
if (isnan(f) || isnan(h))
{
Serial.println("Failed to read from DHT");
}
else
{
float hi = dht.computeHeatIndex(f, h);
Serial.print(F("Fahrenheit: "));
Serial.print(f);
Serial.println("*\t");
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.println("%\t");
}
//********************************************************************************//
// Tests temp & humidity to see if preset values are exceed.
// If exceeded, a relay is trigger high to power an exhaust fan or heater.
//**************************** REMOVING HEAT *************************************//
if (f >= hiMaxTemp) //if "f" is greater than or equal to hiMaxTemp,
{
digitalWrite(Relay_E, TURN_ON); // TURN_ON relayE (fan).
Serial.print("\t");
Serial.println(F("Exhausting the heat!")); // Text printed to serial monitor
Serial.print("\t");
Serial.println();
}
else if (f <= lowMaxTemp) // or else if "f" is less than or equal to lowMaxTemp
{
digitalWrite(Relay_E, TURN_OFF); // TURN_OFF relay E.
}
//****************************** ADDING HEAT *******************************//
if (f <= lowMinTemp)
{
digitalWrite(Relay_F, TURN_ON); // (heater)
Serial.print("\t");
Serial.println(F("Warming the room!"));
Serial.print("\t");
Serial.println();
}
else if(f >= hiMinTemp);
{
digitalWrite(Relay_F, TURN_OFF);
}
//**************************** REMOVING HUMIDITY *******************//
if (h >= hiHum)
{
digitalWrite(Relay_G, TURN_ON); // (dehumidifier)
Serial.print("\t");
Serial.println(F("Drying the air!"));
Serial.print("\t");
Serial.println();
}
else if (h <= lowHum)
{
digitalWrite(Relay_G, TURN_OFF);
}
}