gionni
January 20, 2018, 9:41am
1
hi, please, I would like help with this project… see below
#define BLYNK_PRINT Serial
#define relePIN D1
#define DHTTYPE DHT22
#define DHTPIN D6
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
BlynkTimer timer;
WidgetRTC rtc;
WidgetLED led1(V6);
WidgetLED led2(V7);
DHT dht(DHTPIN, DHTTYPE);
char currentTime[9];
char auth[] = "";
char ssid[] = "";
char pass[] = "";
char server[] = "blynk-cloud.com";
bool clockSync = false;
const int rele = D1;
int Avvio;
int manuale;
float TempDes;
float Tempmin;
float Isteresi = 0.3;
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V16, h);
Blynk.virtualWrite(V17, t);
}
void setup() {
Serial.begin(9600);
Serial.println();
dht.begin();
timer.setInterval(10000L, sendSensor);
pinMode(5,OUTPUT);
Blynk.begin(auth, ssid, pass, server);
ArduinoOTA.setHostname("pippo");
ArduinoOTA.begin();
timer.setInterval(1000L, activetoday);
timer.setInterval(1000L, clockDisplay);
}
BLYNK_CONNECTED() {
rtc.begin();
}
void activetoday(){
if(year() != 1970){
Blynk.syncVirtual(V1);
Blynk.syncVirtual(V2);
}
}
void clockDisplay(){
if((year() != 1970) && (clockSync == false)){
sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
Serial.println(currentTime);
clockSync = true;
}
}
BLYNK_WRITE(V12){
TempDes = param.asInt();
Blynk.virtualWrite(13,TempDes);
}
BLYNK_WRITE(V14){
Tempmin = param.asInt();
Blynk.virtualWrite(15,Tempmin);
}
BLYNK_WRITE(V1) {
TimeInputParam t(param);
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if(weekday() == 1){
dayadjustment = 6;
}
if(t.isWeekdaySelected((weekday() + dayadjustment))){
//Schedule is ACTIVE today
if(nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ){ is sent
//led1.on();
Avvio = 1;
Serial.println("Schedule 1 started");
}
if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){
led1.off();
Avvio = 0;
Serial.println("Schedule 1 finished");
}
}
}
BLYNK_WRITE(V2) {
TimeInputParam t(param);
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if(weekday() == 1){
dayadjustment = 6;
}
if(t.isWeekdaySelected((weekday() + dayadjustment))){
//Schedule is ACTIVE today
if(nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ){ t
led2.on(); // turn on virtual LED
Serial.println("Schedule 2 started");
}
if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){
led2.off(); // turn OFF virtual LED
Serial.println("Schedule 2 finished");
float t = dht.readTemperature();
//Serial.println(Avvio);
if (Avvio == 1){
if (t < TempDes);
digitalWrite(rele,HIGH);
led1.on();
}
else
digitalWrite(rele,LOW);
}
}
}
void loop()
{
if(Blynk.connected()){
Blynk.syncAll();
Blynk.run();
}
ArduinoOTA.handle();
timer.run();
}
gionni
January 20, 2018, 9:42am
2
hi, please, I would like help with this project.
the variable “Avvio” changes state correctly at the set time, but the “rele” and the LED widget remain off.
while if the code below is inserted, the relay is activated. but I would like to set two conditions for this to happen, time and temperature, with the first as a priority
BLYNK_WRITE(V1) { // Scheduler #1 Time Input widget
TimeInputParam t(param);
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if(weekday() == 1){
dayadjustment = 6; // needed for Sunday Time library is day 1 and Blynk is day 7
}
if(t.isWeekdaySelected((weekday() + dayadjustment))){ //Time library starts week on Sunday, Blynk on Monday
//Schedule is ACTIVE today
if(nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ){ // 62s on 60s timer ensures 1 trigger command is sent
led1.on(); // turn on virtual LED
digitalWrite(rele,HIGH);
Serial.println("Schedule 1 started");
}
if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){ // 62s on 60s timer ensures 1 trigger command is sent
led1.off(); // turn OFF virtual LED
digitalWrite(rele,LOW);
Serial.println("Schedule 1 finished");
sorry google translator
Costas
January 20, 2018, 9:47am
3
@gionni can you post the QR code image for a clone of the project.
Costas
January 20, 2018, 11:04am
5
@gionni just running your project here.
You can’t have the sync in the loop, move it to BLYNK_CONNECTED()
V1 scheduler is working fine after tweaking your sketch.
I will check V2 now.
Costas
January 20, 2018, 11:12am
6
In your first sketch you have //led1.on();
so that will never turn on the LED.
Are your relays triggered HIGH or LOW, the vast majority are LOW.
You have D1 defined twice in your sketch, comment out the first one.
I don’t like to see “D” in sketches so my version has the real GPIO references.
Are you connecting your relay to the correct pin.
Also I don’t like to see "bad " pins being used when there are still “good” pins available.
gionni
January 20, 2018, 11:25am
7
thanks for now. I’m leaving for week end.ti update in the next days
Costas
January 20, 2018, 12:11pm
8
Ok, when you get back to your code the other things to look at are:
I don’t like to see single letter variables like h and t especially when t is already used as a Time Input variable.
Unless you are really short of memory then h and t should be declared globally.
With the code you have at the moment schedule 2 will only trigger the LED and relay when schedule 1 is also active (Avvio == 1).
I think you have schedule 2 code in the OFF section and you probably need it in the ON section. Maybe led1.on referred to in schedule 2 should be led2.on and you don’t have an off in the else. It depends what you are trying to do and if your relays are active HIGH or LOW etc.
1 Like
gionni
January 22, 2018, 12:08pm
9
hello.o took note of your advice, more or studied a bit, and I think I have achieved a fair result.now or in mind some change …
however it now works.
thanks again
1 Like
Costas
January 22, 2018, 12:12pm
10
Format your sketch post with backticks.
gionni
January 22, 2018, 12:47pm
11
#define BLYNK_PRINT Serial
#define relePIN 5
#define DHTTYPE DHT22
#define DHTPIN 12
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
BlynkTimer timer;
WidgetRTC rtc;
WidgetLED led1(V6);
WidgetLED led2(V7);
DHT dht(DHTPIN, DHTTYPE);
char currentTime[9];
char auth[] = "";
char ssid[] = "";
char pass[] = "";
char server[] = "blynk-cloud.com";
bool clockSync = false;
const int rele = 5;
int Avvio;
int manuale;
float TempDes;
float Tempmin;
float Isteresi = 0.3;
float tempnow;
void sendSensor()
{
float hum = dht.readHumidity();
float temp = dht.readTemperature();
tempnow = temp;
if (isnan(hum) || isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V16, hum);
Blynk.virtualWrite(V17, temp);
}
void setup() {
Serial.begin(9600);
Serial.println();
dht.begin();
timer.setInterval(10000L, sendSensor);
pinMode(5,OUTPUT);
Blynk.begin(auth, ssid, pass, server);
timer.setInterval(1000L, activetoday);
timer.setInterval(1000L, clockDisplay);
timer.setInterval(5000L, reconnectBlynk);
}
BLYNK_CONNECTED() {
rtc.begin();
Blynk.syncAll();
}
void activetoday(){ // check if schedule #1 or #2 should run today
if(year() != 1970){
Blynk.syncVirtual(V1); // sync scheduler #1
Blynk.syncVirtual(V2); // sync scheduler #2
}
}
void clockDisplay(){
if((year() != 1970) && (clockSync == false)){
sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
Serial.println(currentTime);
clockSync = true;
}
}
BLYNK_WRITE(V12){
TempDes = param.asInt();
Blynk.virtualWrite(13,TempDes);
}
BLYNK_WRITE(V14){
Tempmin = param.asInt();
Blynk.virtualWrite(15,Tempmin);
}
BLYNK_WRITE(V1) {
TimeInputParam t(param);
int Avvio1;
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if(weekday() == 1){
dayadjustment = 6;
}
if(t.isWeekdaySelected((weekday() + dayadjustment))){
//Schedule is ACTIVE today
if(nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ){
led1.on();
Avvio1=Avvio=1;
Serial.println("Schedule 1 started");
}
if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){
led1.off();
Avvio = 0;
Serial.println("Schedule 1 finished");
//Serial.println(Avvio);
}
}
}
BLYNK_WRITE(V2) { // Scheduler #2 Time Input widget
TimeInputParam t(param);
int Avvio1;
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if(weekday() == 1){
dayadjustment = 6;
}
if(t.isWeekdaySelected((weekday() + dayadjustment))){
//Schedule is ACTIVE today
if(nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ){
led2.on(); // turn on virtual LED
Avvio1=Avvio=1;
Serial.println("Schedule 2 started");
}
if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){
led2.off();
Avvio = 0;
Serial.println("Schedule 2 finished");
//Serial.println(Avvio);
}
}
if (Avvio == 1){
if (tempnow < (TempDes-Isteresi))
digitalWrite(rele,1);
else if (tempnow > (TempDes+Isteresi)){
digitalWrite(rele,0);
}
}
Serial.print("Stato AVVIO: ");
Serial.println(Avvio);
Serial.print("Temperatua attuale: ");
Serial.println(tempnow);
Serial.print("TempDes-Isteresi: ");
Serial.println(TempDes-Isteresi);
Serial.println("TempDes+Isteresi");
Serial.println(TempDes+Isteresi);
}
void reconnectBlynk() {
if (!Blynk.connected()) {
if (Blynk.connect()) {
BLYNK_LOG("Reconnected");
}
else {
BLYNK_LOG("Not reconnected");
}
}
}
BLYNK_WRITE(V8) {
manuale = param.asInt();
digitalWrite(5, manuale);
}
void loop()
{
if
(Blynk.connected()){
Blynk.run();
}
timer.run();
}
Guacomo
December 2, 2019, 5:34am
12
Ciao Gionni sono molto interessato al tuo progetto. Ho copiato tutto ma ci sono diversi problemi. Sei riuscito a fare funzionare tutto per bene. Gradirei tanto sapere come. Grazie
What issue do you have?
The first thing I will do is to stage timers because at 10 seconds , they all work
timer.setInterval(10000L, sendSensor);
timer.setInterval(1000L, activetoday);
timer.setInterval(1000L, clockDisplay);
timer.setInterval(5000L, reconnectBlynk);
Guacomo
December 2, 2019, 1:29pm
15
Il bottone manuale non funziona e non funziona il led relè on
gionni
December 2, 2019, 10:34pm
16
include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SSD1306_LCDHEIGHT 64
#define OLED_RESET LED_BUILTIN
Adafruit_SSD1306 display(OLED_RESET);
#define BLYNK_PRINT Serial
#define relePIN 12
#define DHTTYPE DHT22
#define DHTPIN 2
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
BlynkTimer timer;
WidgetRTC rtc;
WidgetLED led1(V6);
WidgetLED led2(V7);
WidgetLED led3(V9);
WidgetLED led4(V10);
WidgetLED led5(V11);
WidgetLED led6(V18);
DHT dht(DHTPIN, DHTTYPE);
char currentTime[9];
char auth[] =
char ssid[] =
char pass[] =
char server[] = "blynk-cloud.com";
bool clockSync = false;
const int rele = 12;
int Avvio;
//int manuale;
float TempDes;
float Tempmin;
float Isteresi = 0.3;
float tempnow;
void sendSensor()
{
float hum = dht.readHumidity();
float temp = dht.readTemperature();
tempnow = temp;
if (isnan(hum) || isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V16, hum);
Blynk.virtualWrite(V17, temp);
}
void setup() {
display.begin(SSD1306_SWITCHCAPVCC,0x3c);
Serial.begin(9600);
Serial.println();
dht.begin();
timer.setInterval(2000L, sendSensor);
pinMode(12, OUTPUT);
Blynk.begin(auth, ssid, pass, server);
timer.setInterval(1000L, activetoday);
timer.setInterval(2000L, clockDisplay);
timer.setInterval(2000L, reconnectBlynk);
timer.setInterval(2000L, ledStatus);
}
BLYNK_CONNECTED() {
rtc.begin();
Blynk.syncAll();
}
void activetoday() {
if (year() != 1970) {
Blynk.syncVirtual(V1); // sync scheduler #1
Blynk.syncVirtual(V2); // sync scheduler #2
Blynk.syncVirtual(V3); // sync scheduler #3
Blynk.syncVirtual(V4); // sync scheduler #4
Blynk.syncVirtual(V5); // sync scheduler #5
}
}
void clockDisplay() { // only needs to be done once after time sync
if ((year() != 1970) && (clockSync == false)) {
sprintf(currentTime, "%02d:%02d:%02d", hour(), minute(), second());
Serial.println(currentTime);
clockSync = true;
}
}
void displayTempHumid(){
// float hum = dht.readHumidity();
//float temp = dht.readTemperature();
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(0,0);
display.print("T:");
display.print(tempnow);
display.setCursor(0,44);
display.print("D:");
display.print(TempDes);
display.setCursor(0,15);
display.print(" ... ");
}
BLYNK_WRITE(V12) {
TempDes = param.asFloat();
Blynk.virtualWrite(13, TempDes);
}
BLYNK_WRITE(V14) {
Tempmin = param.asFloat();
Blynk.virtualWrite(15, Tempmin);
}
BLYNK_WRITE(V1) {
TimeInputParam t(param);
int Avvio1;
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6;
}
if (t.isWeekdaySelected((weekday() + dayadjustment))) {
//Schedule is ACTIVE today
if (nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ) {
led1.on(); // turn on virtual LED
Avvio1 = Avvio = 1;
Serial.println("Schedule 1 started");
}
if (nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ) {
led1.off(); // turn OFF virtual LED
Avvio = 0;
Serial.println("Schedule 1 finished");
//Serial.println(Avvio);
}
}
}
BLYNK_WRITE(V2) { // Scheduler #2 Time Input widget
TimeInputParam t(param);
int Avvio1;
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6;
}
if (t.isWeekdaySelected((weekday() + dayadjustment))) {
//Schedule is ACTIVE today
if (nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ) {
led2.on(); // turn on virtual LED
Avvio1 = Avvio = 1;
Serial.println("Schedule 2 started");
}
if (nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ) {
led2.off(); // turn OFF virtual LED
Avvio = 0;
Serial.println("Schedule 2 finished");
//Serial.println(Avvio);
}
}
}
BLYNK_WRITE(V3) { // Scheduler #3 Time Input widget
TimeInputParam t(param);
int Avvio1;
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6;
}
if (t.isWeekdaySelected((weekday() + dayadjustment))) {
//Schedule is ACTIVE today
if (nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ) {
led3.on(); // turn on virtual LED
Avvio1 = Avvio = 1;
Serial.println("Schedule 3 started");
}
if (nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ) {
led3.off(); // turn OFF virtual LED
Avvio = 0;
Serial.println("Schedule 3 finished");
//Serial.println(Avvio);
}
}
}
BLYNK_WRITE(V4) { // Scheduler #4 Time Input widget
TimeInputParam t(param);
int Avvio1;
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6;
}
if (t.isWeekdaySelected((weekday() + dayadjustment))) {
//Schedule is ACTIVE today
if (nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ) {
led4.on(); // turn on virtual LED
Avvio1 = Avvio = 1;
Serial.println("Schedule 4 started");
}
if (nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ) {
led4.off(); // turn OFF virtual LED
Avvio = 0;
Serial.println("Schedule 4 finished");
//Serial.println(Avvio);
}
}
}
BLYNK_WRITE(V5) { // Scheduler #5 Time Input widget
TimeInputParam t(param);
int Avvio1;
unsigned int nowseconds = ((hour() * 3600) + (minute() * 60) + second());
unsigned int startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
unsigned int stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
int dayadjustment = -1;
if (weekday() == 1) {
dayadjustment = 6;
}
if (t.isWeekdaySelected((weekday() + dayadjustment))) {
//Schedule is ACTIVE today
if (nowseconds >= startseconds - 31 && nowseconds <= startseconds + 31 ) {
led5.on(); // turn on virtual LED
Avvio1 = Avvio = 1;
Serial.println("Schedule 5 started");
}
if (nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ) {
led5.off(); // turn OFF virtual LED
Avvio = 0;
Serial.println("Schedule 5 finished");
//Serial.println(Avvio);
}
}
if (Avvio == 1) {
if (tempnow < (TempDes - Isteresi))
digitalWrite(rele, 1);
else if (tempnow > (TempDes + Isteresi)) {
digitalWrite(rele, 0);
}
}
if (Avvio == 0) {
if (tempnow < (Tempmin - Isteresi))
digitalWrite(rele, 1);
else if (tempnow > (Tempmin + Isteresi)) {
digitalWrite(rele, 0);
}
}
//Serial.print("Stato AVVIO: ");
//Serial.println(Avvio);
//Serial.print("Temperatua attuale: ");
//Serial.println(tempnow);
//Serial.print("TempDes-Isteresi: ");
//Serial.println(TempDes - Isteresi);
//Serial.println("TempDes+Isteresi");
//Serial.println(TempDes + Isteresi);
}
void ledStatus() {
int ledState;
ledState = digitalRead(rele);
if (ledState == HIGH)
//Blynk.virtualWrite(V8, 1);
led6.on();
else
// Blynk.virtualWrite(V8, 0);
led6.off();
}
BLYNK_WRITE(V6){
if (param.asInt() == 1){
Tempmin = 50,0;
//led6.on();
Blynk.virtualWrite(V8, 1);
}
}
void reconnectBlynk() {
if (!Blynk.connected()) {
if (Blynk.connect()) {
BLYNK_LOG("Reconnected");
}
else {
BLYNK_LOG("Not reconnected");
}
}
}
BLYNK_WRITE(V8){
if (param.asInt() == 1){
Tempmin = 50,0;
//led6.on();
Blynk.virtualWrite(V8, 1);
}
else{
Tempmin = 0;
//led6.off();
Blynk.virtualWrite(V8, 0);
}
}
//}
void loop()
{
displayTempHumid();
display.display();
if
(Blynk.connected()) {
Blynk.run();
}
timer.run();
}