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!
#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();
}