RF 433 Mhz on virtual pin doesn't want to send (with scheduling)

Hi!
(First) I made a simple sketch with 4 buttons in switch-mode to control my power plugs, and it worked without any problem. Then I tried to implement scheduling with SimpleTimer, but the transmitter refuses to send anything! I basically copied Costas code from TIme input widget and modified it with the code from my “4 button” sketch.
I’m probably missing something fundamental in the coding structure… All help appreciated! :smiley:

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>
#include <Time.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
// https://github.com/JoakimWesslen/Tx433/
#include <tx433.h>

#define W5100_CS 10
#define SDCARD_CS 4

// For my receivers
// Tx433::Tx433(int digitalpin, String transmittercode, String channelcode)
String txAnslut = "1010101010010101100110100110010101010101010101010110";
String chAnslut="0101";
int txPin = 8;

Tx433 Anslut(txPin, txAnslut, chAnslut);

byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xCC };
IPAddress arduino_ip (192, 168,   0,  20);
IPAddress dns_ip     (192, 168,   0,   1);
IPAddress gateway_ip (192, 168,   0,   1);
IPAddress subnet_mask(255, 255, 255,   0);

char auth[] = "";

char Date[16];
char Time[16];

long startseconds;
long stopseconds;
long nowseconds;

SimpleTimer timer;
WidgetRTC rtc;

void setup(){
	pinMode(SDCARD_CS, OUTPUT);
	digitalWrite(SDCARD_CS, HIGH);

	Serial.begin(9600);
	Serial.println("\nStarted");
	Blynk.begin(auth, "cloud.blynk.cc", 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);

	while (Blynk.connect() == false) {
		// Wait until connected
	}
	Blynk.notify("Connected successfully");
	Serial.println("Done");
	rtc.begin();
	timer.setInterval(60000L, activetoday);
	timer.setInterval(30000L, reconnectBlynk);
}

void activetoday(){
	if(year() != 1970){
		Blynk.syncVirtual(V10);
		sprintf(Date, "%02d/%02d/%04d",  day(), month(), year());
		sprintf(Time, "%02d:%02d:%02d", hour(), minute(), second());
	}
}

BLYNK_WRITE(V10) {

	TimeInputParam t(param);
	Serial.print("Checked schedule at: ");
	Serial.println(Time);
	int dayadjustment = -1;

	if(weekday() == 1){
		dayadjustment =  6;
	}
	if(t.isWeekdaySelected((weekday() + dayadjustment))) {
		Serial.println("Schedule ACTIVE today");
		nowseconds = ((hour() * 3600) + (minute() * 60) + second());
		startseconds = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
		if(nowseconds >= startseconds){
			if(nowseconds <= startseconds + 90) {
				Anslut.Device_On(0);			// Set Device 0 to ON (send code) -- Doesnt work :(
				Blynk.virtualWrite(V0, 1);		// Update app
				Serial.println("RC 1 ON");		// This message gets printed! (RC (RemoteControl) 1 = Device 0)
			}
		}

		else {
			Serial.println("Relay not on");
		}

		stopseconds = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
		if(nowseconds >= stopseconds) {
			if(nowseconds <= stopseconds + 90) {
				Anslut.Device_Off(0);			// Set Device 0 to OFF (send code) -- Doesnt work 
				Blynk.virtualWrite(V0, 0);		// Update app
				Serial.println("RC 1 OFF");		// This message gets printed!
			}
		}
		else {
			if(nowseconds >= startseconds) {
				Serial.println("Relay is still ON");
			}
		}
	}
	else {
		Serial.println("Schedule INACTIVE today");
	}
	Serial.println();
}

void reconnectBlynk() {
	if (!Blynk.connected()) {
		if(Blynk.connect()) {
			BLYNK_LOG("Reconnected");
		}
		else {
			BLYNK_LOG("Not reconnected");
		}
	}
}

BLYNK_WRITE(V0) {                   	// Device 0
	if ( param.asInt() == 1 ) {     	// Button in switch-mode
		Anslut.Device_On(0);			// Doesnt work..
		Serial.println("RC 1 ON");		// ..but this message gets printed
	}
	else {
		Anslut.Device_Off(0);			// Doesnt work..
		Serial.println("RC 1 OFF");		// ..but this message gets printed
	}
}
// Same code for V1 and V2 except device numbers

// V3 doesnt work either.
BLYNK_WRITE(V3) {
	if ( param.asInt() == 1 ) {
		Anslut.Device_On(3);			// Device 3 is for the hole group 0, 1, 2
		Blynk.virtualWrite(V0, 1);		// Change all buttons in app to ON
		Blynk.virtualWrite(V1, 1);
		Blynk.virtualWrite(V2, 1);
		Serial.println("ALL RC ON");
	}
	else {
		Anslut.Device_Off(3);
		Blynk.virtualWrite(V0, 0);		// Change all buttons in app to OFF
		Blynk.virtualWrite(V1, 0);
		Blynk.virtualWrite(V2, 0);
		Serial.println("ALL RC OFF");
	}
}

void loop()
{
	if (Blynk.connected()) {
		Blynk.run();
	}
	timer.run();
}

I didn’t look all the way through your code, but there is MUCH easier way to achieve scheduled on/off. Use the Eventor Widget and make some rules.

Try that with some basic scheduling. You can also add buttons to the Vpins in conjunction with the Eventor Widget. E.g. I turn on all my lights at 18.00, and turn them off at 22.30. But when I’m in the mood for a late nighter, I can simply use the buttons to set them to on anyway. It’ll save you lots of code and works much better in my experience. The only thing you’d have to code is the BLYNK_WRITE for the vPin button on your dashboard.

Somewhat annoyed after a frustrating week with my sketch, but Eventor WAS much easier :laughing: so I’ll use that instead! But I’m still curious to know why my sketch doesn’t work. In short, I can’t call
Anslut.Device_On(0) inside a BLYNK_WRITE(pin)

That’ll require some debugging. Take it step by step and start from scratch, if you want to debug. But good to know eventor is working. I myself found the light only this week to, credits to @Gunner :wink:

@distans I recently revamped my scheduling code to make it easier to add more schedules i.e. variables are all local now so you can simply copy the function over and over again, plus a minor mod to one other function.

Test the following sketch with 2 schedulers and once you now it works incorporate into your project.

/****************************************************************************** 
  EziScheduler.ino   by Costas, 7 Sept 2017
  Scheduler with Time Input and obligatory OTA update facility for ESP's
  RTC widget (no pin allocation)
  LED V0
  Time Input V1
  Time Input V2
  To add more schedules simply copy BLYNK_WRITE(V1) function and change V1 to V3
  and in activetoday() function add: Blynk.syncVirtual(V3); // sync scheduler #3 
*******************************************************************************/
#define BLYNK_PRINT Serial 
#include <ArduinoOTA.h>             
#include <ESP8266WiFi.h>        
#include <BlynkSimpleEsp8266.h> 
BlynkTimer timer;

#include <TimeLib.h>
#include <WidgetRTC.h>
WidgetRTC rtc;
char currentTime[9];

char auth[]   = "**************"; 
char ssid[]   = "GargoyleTest";                     
char pass[]   = "**************";                     
char server[] = "blynk-cloud.com"; 
bool clockSync = false;    

void setup() {
  Serial.begin(115200);
  Serial.println();
  Blynk.begin(auth, ssid, pass, server);
  ArduinoOTA.setHostname("EziScheduler");       
  ArduinoOTA.begin();    
  timer.setInterval(60000L, activetoday);  // check every 60s if ON / OFF trigger time has been reached
  timer.setInterval(1000L, clockDisplay);  // check every second if time has been obtained from the server
}

BLYNK_CONNECTED() {
  rtc.begin();
}

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(){  // 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;
  } 
}    

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
      Blynk.virtualWrite(V0, 255);  // turn on virtual LED
      Serial.println("Schedule 1 started");
    }                  
    if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){   // 62s on 60s timer ensures 1 trigger command is sent
      Blynk.virtualWrite(V0, 0);   // turn OFF virtual LED
      Serial.println("Schedule 1 finished");
    }               
  }
}

BLYNK_WRITE(V2) {   // Scheduler #2 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
      Blynk.virtualWrite(V0, 255);  // turn on virtual LED
      Serial.println("Schedule 2 started");
    }                  
    if(nowseconds >= stopseconds - 31 && nowseconds <= stopseconds + 31 ){   // 62s on 60s timer ensures 1 trigger command is sent
      Blynk.virtualWrite(V0, 0);    // turn OFF virtual LED
      Serial.println("Schedule 2 finished");
    }              
  }
}

void loop() {
  if(Blynk.connected()){ 
    Blynk.run();        
  }                     
  ArduinoOTA.handle(); 
  timer.run();
}
1 Like

Much obliged @Costas! I’ll see if I can get the new code running. But as @Lichtsignaal mentioned, Eventor is so much easier to use - almost too easy actually. I don’t learn anything if I don’t have to write any code. :face_with_raised_eyebrow:

I noticed in your sketch that you added a comment that even pressing V3 button didn’t trigger the required functions. This suggests you have a more fundamental issue than time triggered events.

That doesn’t sound very promising… :grimacing: There are some other libraries that should work with my receivers that I could try (with fundamental issue I assume you mean the lib?!). In other sketches the lib has worked without any problem and since @Lichtsignaal’s reply earlier today with Eventor :slight_smile: (basically the same code as above minus V10 and the timer functions)

I’ve might have been a little bit unclear with my comment on V3. When I press the button all virtualWrite is triggered correctly and updates the app, and println sends its message to my console. It’s only Anslut.Device_On(3) that doesn’t work.

Can you provide details of what you are including in Eventor and confirm Anslut.Device_On(x) is working with Eventor.

Some comments are in Swedish, but the code is universal :wink:

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <tx433.h>
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <SimpleTimer.h>

#define W5100_CS 10
#define SDCARD_CS 4

// Sändar-ID och kanal för Anslut-enheter
String txAnslut = "1010101010010101100110100110010101010101010101010110";
String chAnslut="0101";
int txPin = 8;

// Tx433::Tx433(int digitalpin, String transmittercode, String channelcode)
Tx433 Anslut(txPin, txAnslut, chAnslut);

byte arduino_mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xCC };
IPAddress arduino_ip (192, 168,   0,  20);        
IPAddress dns_ip     (192, 168,   0,   1);
IPAddress gateway_ip (192, 168,   0,   1);
IPAddress subnet_mask(255, 255, 255,   0);

char auth[] = ""; // Token till Blynk

WidgetTerminal terminal(V4);		
SimpleTimer timer;

void setup() {
	pinMode(SDCARD_CS, OUTPUT);		
	digitalWrite(SDCARD_CS, HIGH);
	Serial.begin(9600);
	Serial.println("\nStarted");

	Blynk.begin(auth, "cloud.blynk.cc", 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
	while (Blynk.connect() == false) {
		// Wait until connected
	}
	Blynk.notify("Connected successfully");
	Serial.println("Done");
	terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
	terminal.println(F("-------------"));
	terminal.flush();
	timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server
}

BLYNK_WRITE(V0) {                   // 1

	if ( param.asInt() == 1 ) {     
		Anslut.Device_On(0);
		Serial.println("RC 1 ON");
		terminal.println("RC 1 ON");
		terminal.flush();
	}
	else {                          
		Anslut.Device_Off(0);
		Serial.println("RC 1 OFF");
		terminal.println("RC 1 OFF");
		terminal.flush();
	}
}

BLYNK_WRITE(V1) {                    // 2

	if ( param.asInt() == 1 ) {
		Anslut.Device_On(1);
		Serial.println("RC 2 ON");
		terminal.println("RC 2 ON");
		terminal.flush();
	}
	else {
		Anslut.Device_Off(1);
		Serial.println("RC 2 OFF");
		terminal.println("RC 2 OFF");
		terminal.flush();
	}
}

BLYNK_WRITE(V2) {                    // 3

	if ( param.asInt() == 1 ) {
		Anslut.Device_On(2);
		Serial.println("RC 3 ON");
		terminal.println("RC 3 ON");
		terminal.flush();
	}
	else {
		Anslut.Device_Off(2);
		Serial.println("RC 3 OFF");
		terminal.println("RC 3 OFF");
		terminal.flush();
	}
}

BLYNK_WRITE(V3) {                    // Group

	if ( param.asInt() == 1 ) {
		Anslut.Device_On(3);
		Blynk.virtualWrite(V0, 1);    
		Blynk.virtualWrite(V1, 1);    
		Blynk.virtualWrite(V2, 1);
		Serial.println("ALL IS ON");
		terminal.println("ALL IS ON");
		terminal.flush();
	}
	else {
		Anslut.Device_Off(3);
		Blynk.virtualWrite(V0, 0);    
		Blynk.virtualWrite(V1, 0);
		Blynk.virtualWrite(V2, 0);
		Serial.println("ALL IS OFF");
		terminal.println("ALL IS OFF");
		terminal.flush();
	}

}

void reconnectBlynk() {
	if (!Blynk.connected()) {
		if(Blynk.connect()) {
			BLYNK_LOG("Reconnected");
		}
		else {
			BLYNK_LOG("Not reconnected");
		}
	}
}

void loop() {
	if (Blynk.connected()) {
		Blynk.run();
	}
	timer.run();
}

Eventor Event 1 & 2:
![Screenshot_2017-09-08-03-21-22|281x500](upload://nCo27Sp70hs4TJBXJ5zYKDbCDr9.png)
https://imgur.com/a/d8CI6

V0 changes to 1/ON
![Screenshot_2017-09-08-03-22-10|281x500](upload://6eh66GyxPdBHLh41jAfhUk5UdXk.png)
https://imgur.com/a/DmCHF

V0 changes to 0/OFF
![Screenshot_2017-09-08-03-23-01|281x500](upload://8c4ax7ZrLUcV6Yo7ankYZALby1S.png)
http://imgur.com/a/cRkdL

The only problem I've had with my RF sketches is when I send ON/OFF fast and repeatedly. Sort of a simple "stress test".

_Edit: cant get my images to upload_

I’m not seeing the all important Eventor screenshots.

That’s probably an RF / Blynk issue, how fast is fast?

Sorry for that, edited my post a couple of times but the images wont stick :weary:

Possibly a new user restriction so we don’t see to much p*rn.
I have upgraded you to a regular Blynker. Can you try one last time.

Using Blynk, about 1 / sec is the highest rate. Actually, 1 / sec is probably the highest rate you want to use anyhow. (I fell asleep for a couple of minutes haha) Almost all transmitters (or libraries) send the code burst 3-5 times with small pauses inbetween, and then you have to update status on pins, print stuff to terminal, the relay itself is “slow” etc etc. I could make a more serious test if you want.

Thx! :slight_smile: Did it work?

Still no screenshots of Eventor.


Now?

Yes I see the screenshots. I will study them now.

OK so Eventor is triggering V0 at specified time as requested.

When the event is triggered does it also process Anslut.Device_On(0) and turn on your RF device?

Yes! Everything works in that sketch as far as i know. Perhaps worth mentioning, I used two different radio scanners to check If any signal was transmitted when I pressed the buttons in my first sketch. But there wasnt.
I tried to put Anslut.Device_On(0) in setup () and that actually worked… I’m lost! :grin:

With your new code and another lib for the transmitter everything seems to be working just perfectly! :+1: I will test the original lib and see if that works as well and post a working code.