I use a Arduino Mega with the ENC28J60 ethernet port.
I use a seperate routine because I need to do more logic than just a simple write.
1st I need to see of the timer function is actually on (vPin 3), then check of the timer is actually running (vPin 4) and if so, switch all on. BUT, in case of manual On button, the relays also need to go on.
I will try and check with a simple program, but the timer it self is just wrong. If I press it to see how much time remains before it’s engaged it’s just wrong. I set it, for example, for 6 in the morning untill 22.30 in the evening, but when I press it at ten past six, it says “14 hours remaining or more”. Or at least something which is all wrong and way off.
Besides, I tried to output some serial data like “timer running” on vPin 4, that also didn’t work. It just looks like the whole timer is just doing nothing or is in the wrong timezone.
The whole code and nothing but the code as it is running now:
/****************************************************************************************
* Changelog:
* v0.1 initial setup
* v0.2 using simple timer instead of calling functions from main loop
*/
// Physical Pin assignments
/*
* 6 = not used, linked to port on backside
* 7 = not used, linked to port on backside
* 8 = not used, linked to port on backside
* 9 = Infrared, linked to port on backside, 1 / 2 (VCC / GND)
* 10 = CE NRF
* 11 = CSN NRF
* 20 = I2C Display SDA (Data)
* 21 = I2C Display SCK (Clock)
* 22 t/m 30 = 230v Relays (same as virtual pins)
* 50 = (MI)SO Ethernet / NRF
* 51 = (MO)SI Ethernet / NRF
* 52 = SCK Ethernet / NRF
* 53 = CS Ethernet / NRF
*/
// Virual Pin assignments
/*
* 1 = Audio/video off
* 2 = Audio/video on
* 3 = Lights timer on/off (Doesn't work yet)
* 4 = Lights timer (Doesn't work yet)
* 7 = LCD backlight on/off
* 22 = Override Tree
* 23 = Override Lamp
*/
// Basic
#define BLYNK_PRINT Serial
// Includes
#include <UIPEthernet.h> // Ethernet lib
#include <BlynkSimpleUIPEthernet.h> // Blynk Ethernet add-ons
#include <IRLib.h> // Infrared for audio/video
#include <Wire.h> // Wire for I2C communications
#include <LiquidCrystal_I2C.h> // Matrix display
#include <RF24Network.h> // NRF communications
#include <RF24.h> // NRF communications
#include <SPI.h> // NRF communications
#include <SimpleTimer.h> // Timer lib
/****************************************************************************************
* Blynk Stuff
*/
char auth[] = "bla";
/****************************************************************************************
* IR stuff for turning on/off Audio/Video equipment (IR Led on Pin3, hardcoded!)
*/
IRsend irsend;
/****************************************************************************************
* End IR Stuff
*/
/****************************************************************************************
* Timer and light stuff
*/
int enableTimer; // Defaults to disable timer (button on vPin 3)
int timer1Running; // Defaults to timer is running (timer widget on vPin 4)
int manualTree; // Tree lamp thingie override (right of couch), default via timer
int manualLamp; // Normal lamp override (left of couch), default via timer
/****************************************************************************************
* End timer and light stuff
*/
/****************************************************************************************
* LCD Matrix display
*/
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
char text1[] = {'I','n','i','t','i','a','l','i','z','i','n','g'};
char text2[] = {'M','e','g','a','D','o','m','o',' ','v','0','.','2'};
const char* server = "cloud.blynk.cc";
bool backLight = 1;
/****************************************************************************************
* End LCD Matrix Stuff
*/
/****************************************************************************************
* NRF24 Radio
*/
RF24 radio(10, 11);
RF24Network network(radio);
struct controlMessage
{
// int stores 2 bytes, adjust payload if this grows
float messageData;
};
/****************************************************************************************
* End NRF24 Radio Stuff
*/
SimpleTimer timerOne;
void setup()
{
// Serial output for debugs
Serial.begin(9600);
delay(10);
timerOne.setInterval(1000, timerStuff);
// Init pins for output (230v relays)
for(int i=22;i<30;i++)
{
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
// Start Blynk
Blynk.begin(auth);
// Init LCD display (16x2)
lcd.begin(16,2);
lcd.backlight();
initLCD();
// Init NRF24 Components
SPI.begin();
radio.begin();
radio.setDataRate(RF24_250KBPS); // Lowest speed for best transmission
radio.setPayloadSize(32); // Smallest payload for best performance
network.begin(90, 0); // Get networking running on this controller
}
// Do audio/video stuff when pressing button to vPin 1
BLYNK_WRITE(1)
{
for(int i=0;i<4;i++)
{
irsend.send(RC5,0x140F, 13); // AMP off
irsend.send(SONY,0xA90, 12); // TV off
}
}
// Do audio/video stuff when pressing button to vPin 2
BLYNK_WRITE(2)
{
for(int i=0;i<4;i++)
{
irsend.send(RC5,0x1C0E, 13); // AMP on
irsend.send(SONY,0xA90, 12); // TV on
}
}
/*
* Set lights on/off with respect to timer on vPin 4
*/
BLYNK_WRITE(3) // Timer enable/disable for lights
{
enableTimer = param.asInt();
lcd.clear();
lcd.setCursor(0,0);
if(enableTimer == 1)
{
lcd.print("Timer on");
}
else
{
lcd.print("Timer off");
}
}
BLYNK_WRITE(4) // Actual timer to vPin for lights
{
timer1Running = param.asInt();
lcd.clear();
lcd.setCursor(0,0);
if(timer1Running == 1)
{
lcd.print("Timer running");
}
else
{
lcd.print("Timer n/ running");
}
}
BLYNK_WRITE(7) // Backlight on/off
{
if(backLight)
{
lcd.noBacklight();
backLight = 0;
}
else
{
lcd.backlight();
backLight = 1;
}
}
BLYNK_WRITE(22) // Manual override for Tree
{
manualTree = param.asInt();
lcd.clear();
lcd.setCursor(0,0);
if(manualTree == 1)
{
lcd.print("Tree on");
}
else
{
lcd.print("Tree off");
}
}
BLYNK_WRITE(23) // Manual override for Lamp
{
manualLamp = param.asInt();
lcd.clear();
lcd.setCursor(0,0);
if(manualLamp == 1)
{
lcd.print("Lamp on");
}
else
{
lcd.print("Lamp off");
}
}
void loop()
{
// Run Blynk
Blynk.run();
timerOne.run();
/*
// Keep NRF network going and fetch data
network.update();
getNetworkData();
// Check timer for lights, not functional yet
checkTimer();
// Display message on LCD if connected or not
checkConnect();
*/
}
void timerStuff()
{
// Keep NRF network going and fetch data
network.update();
getNetworkData();
// Check timer for lights, not functional yet
checkTimer();
// Display message on LCD if connected or not
checkConnect();
}
void checkTimer()
{
// If timer is on and timer is running, turn lights on or if manual override is on
if(((enableTimer == 1) && (timer1Running == 1)) || (manualTree == 1))
{
// Timer is running, so lights on!
digitalWrite(22, HIGH);
}
else
{
// Timer is not running, so lights off!
digitalWrite(22, LOW);
}
if(((enableTimer == 1) && (timer1Running == 1)) || (manualLamp == 1))
{
// Timer is running, so lights on!
digitalWrite(23, HIGH);
}
else
{
// Timer is not running, so lights off!
digitalWrite(23, LOW);
}
}
void checkConnect()
{
if(Blynk.connected())
{
lcd.setCursor(0,1);
lcd.print("Connected! ");
}
else
{
lcd.setCursor(0,1);
lcd.print("Disconnected!");
}
}
void initLCD()
{
lcd.clear();
lcd.setCursor(0,0);
for(int i = 0; i< 4; i++)
{
lcd.noBacklight();
delay(100);
lcd.backlight();
delay(100);
}
for(int i=0;i<sizeof(text1);i++)
{
lcd.print(text1[i]);
//Serial.println(text1[i]);
delay(100);
}
for(int i=0;i<sizeof(text1);i++)
{
lcd.setCursor(i,0);
lcd.print(" ");
delay(100);
}
lcd.setCursor(0,0);
for(int i=0;i<sizeof(text2);i++)
{
lcd.print(text2[i]);
//Serial.println(text2[i]);
delay(100);
}
for(int i=0;i<sizeof(text2);i++)
{
lcd.setCursor(i,0);
lcd.print(" ");
delay(100);
}
for(int i = 0; i< 4; i++)
{
lcd.noBacklight();
delay(100);
lcd.backlight();
delay(100);
}
lcd.clear();
}
void getNetworkData()
{
// Receive network data
while(network.available())
{
RF24NetworkHeader header; // Create header
controlMessage message; // Define message
bool receiveOK = network.read(header,&message,sizeof(message)); // Read message from network
// If message is received, return data and exit function
if(receiveOK)
{
lcd.setCursor(0,0);
lcd.print("Temp: " );
lcd.setCursor(6,0);
lcd.print((int)message.messageData);
Blynk.virtualWrite(0, message.messageData);
}
}
}