OK thanks everyone for help, this is code witch works very well:
Only I must add the line where the light will be turn "off" when light level will be > 350 after 5 minutes.
#include <SPI.h>
#include <Encoder.h>
#include <MySensor.h>
#include <Bounce2.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
#define ONE_WIRE_BUS 19 // Pin where dallase sensor is connected
#define MAX_ATTACHED_DS18B20 1
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature.
#define LED_PIN 3 // Arduino pin attached to MOSFET Gate pin
#define KNOB_ENC_PIN_1 4 // Rotary encoder input pin 1
#define KNOB_ENC_PIN_2 5 // Rotary encoder input pin 2
#define KNOB_BUTTON_PIN 6 // Rotary encoder button pin
#define photoPin A6 // Fotorezystor
#define FADE_DELAY 10 // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
#define SEND_THROTTLE_DELAY 400 // Number of milliseconds before sending after user stops turning knob
#define SN "DimmableLED /w button"
#define SV "1.2"
#define CHILD_ID_LIGHT 16
#define ID 15
#define EEPROM_DIM_LEVEL_LAST 1
#define EEPROM_DIM_LEVEL_SAVE 2
#define LIGHT_OFF 0
#define LIGHT_ON 1
//WENT
//#define WENT_RELAY_PIN 17 // Arduino Digital I/O pin number for relay
//#define WENT_BUTTON_PIN 18 // Arduino Digital I/O pin number for button
//#define CHILD_ID_WENT 17 // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0
#define noRelays 1
const int relayPin[] = {17}; // switch around pins to your desire
const int buttonPin[] = {18}; // switch around pins to your desire
class Relay // relay class, store all relevant data (equivalent to struct)
{
public:
int buttonPin; // physical pin number of button
int relayPin; // physical pin number of relay
byte oldValue; // last Values for key (debounce)
boolean relayState; // relay status (also stored in EEPROM)
};
//END
int dimValue;
int fadeTo;
int fadeDelta;
int persons;
byte oldButtonVal;
bool changedByKnob=false;
bool sendDimValue=false;
unsigned long lastFadeStep;
unsigned long sendDimTimeout;
char convBuffer[10];
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
boolean receivedConfig = false;
boolean metric = true;
unsigned long on = 0;
unsigned long onT = 0;
MySensor gw;
// Initialize temperature message
MyMessage msg(0,V_TEMP);
MyMessage dimmerMsg(CHILD_ID_LIGHT, V_DIMMER);
MyMessage msgl(0, V_LIGHT_LEVEL);
//WENT
Relay Relays[noRelays];
Bounce debouncer1[noRelays];
MyMessage msg1[noRelays];
//END
Encoder knob(KNOB_ENC_PIN_1, KNOB_ENC_PIN_2);
Bounce debouncer = Bounce();
int lastLightLevel;
void setup()
{
// The third argument enables repeater mode.
// gw.begin(NULL, AUTO, true);
//Send the sensor node sketch version information to the gateway
// gw.sendSketchInfo("Repeater Node", "1.0");
// Set knob button pin as input (with debounce)
pinMode(KNOB_BUTTON_PIN, INPUT);
pinMode(photoPin, INPUT);
digitalWrite(KNOB_BUTTON_PIN, HIGH);
debouncer.attach(KNOB_BUTTON_PIN);
debouncer.interval(5);
oldButtonVal = debouncer.read();
// Set analog led pin to off
analogWrite( LED_PIN, 0);
// Init mysensors library
gw.begin(incomingMessage, ID, true);
// Send the Sketch Version Information to the Gateway
gw.present(CHILD_ID_LIGHT, S_DIMMER);
gw.sendSketchInfo(SN, SV);
gw.sendSketchInfo("Repeater Node", "1.0");
// Retreive our last dim levels from the eprom
fadeTo = dimValue = 0;
byte oldLevel = 0;
Serial.print("Sending in last known light level to controller: ");
Serial.println(oldLevel);
gw.send(dimmerMsg.set(oldLevel), true);
Serial.println("Ready to receive messages...");
// Startup up the OneWire library
sensors.begin();
// requestTemperatures() will not block current thread
sensors.setWaitForConversion(false);
// Fetch the number of attached temperature sensors
numSensors = sensors.getDeviceCount();
// Present all sensors to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
gw.present(i, S_TEMP);
}
gw.present(15, S_LIGHT_LEVEL);
//WENT
gw.sendSketchInfo("MultiRelayButton", "0.9b");
delay(250);
// Initialize Relays with corresponding buttons
for (int i = 0; i < noRelays; i++){
Relays[i].buttonPin = buttonPin[i]; // assign physical pins
Relays[i].relayPin = relayPin[i];
msg1[i].sensor = i; // initialize messages
msg1[i].type = V_LIGHT;
debouncer1[i] = Bounce(); // initialize debouncer
debouncer1[i].attach(buttonPin[i]);
debouncer1[i].interval(5);
pinMode(Relays[i].buttonPin, INPUT_PULLUP);
pinMode(Relays[i].relayPin, OUTPUT);
Relays[i].relayState = gw.loadState(i); // retrieve last values from EEPROM
digitalWrite(Relays[i].relayPin, Relays[i].relayState? RELAY_ON:RELAY_OFF); // and set relays accordingly
gw.send(msg1[i].set(Relays[i].relayState? true : false)); // make controller aware of last status
gw.present(i, S_LIGHT); // present sensor to gateway
delay(250);
}
//END
}
void loop()
{
// Process incoming messages (like config and light state from controller)
gw.process();
//WENT
for (byte i = 0; i < noRelays; i++){
debouncer1[i].update();
byte value = debouncer1[i].read();
if (value != Relays[i].oldValue && value == 0){
Relays[i].relayState = !Relays[i].relayState;
digitalWrite(Relays[i].relayPin, Relays[i].relayState?RELAY_ON:RELAY_OFF);
gw.send(msg1[i].set(Relays[i].relayState? true : false));
gw.saveState( i, Relays[i].relayState );} // save sensor state in EEPROM (location == sensor number)
Relays[i].oldValue = value;
}
//END
// Sprawdzenie temperatury
CheckTemp();
onT = onT + 1;
on = on + 1;
if (on > 401 + 2*ID) {
// Poziom swiata
lightLevel();
on = 0;
}
if (analogRead(photoPin) > 350) { persons = 0;} else {persons = 1;}
// Check if someone has pressed the knob button
checkButtonClick();
// Fade light to new dim value
fadeStep();
}
void lightLevel()
{
int lightLevel = (analogRead(photoPin));
Serial.println(lightLevel);
if (lightLevel != lastLightLevel) {
gw.send(msgl.set(lightLevel));
lastLightLevel = lightLevel;
}
}
void CheckTemp()
{
// Fetch temperatures from Dallas sensors
sensors.requestTemperatures();
// Read temperatures and send them to controller
for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
// Fetch and round temperature to one decimal
float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
// Only send data if temperature has changed and no error
#if COMPARE_TEMP == 1
if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
#else
if (temperature != -127.00 && temperature != 85.00) {
#endif
if (onT > 400 + 2*ID) {
// Send in the new temperature
gw.send(msg.setSensor(i).set(temperature,1));
onT = 0; }
// Save new temperatures for next compare
lastTemperature[i]=temperature;
}
}
}
void incomingMessage(const MyMessage &message)
{// if (message.sensor == 17){
//} else {
if (analogRead(photoPin) > 350) { persons = 0;} else {persons = 1;}
if (message.type == V_LIGHT && persons == 1 && message.sensor == 16) {
// Incoming on/off command sent from controller ("1" or "0")
int lightState = message.getString()[0] == '1';
int newLevel = 0;
if (lightState==LIGHT_ON) {
// Pick up last saved dimmer level from the eeprom
newLevel = loadLevelState(EEPROM_DIM_LEVEL_SAVE);
}
// Send dimmer level back to controller with ack enabled
gw.send(dimmerMsg.set(newLevel), true);
// We do not change any levels here until ack comes back from gateway
return;
} else if (message.type == V_DIMMER && persons == 1 && message.sensor == 16) {
// Incoming dim-level command sent from controller (or ack message)
fadeTo = atoi(message.getString(convBuffer));
// Save received dim value to eeprom (unless turned off). Will be
// retreived when a on command comes in
if (fadeTo != 0)
saveLevelState(EEPROM_DIM_LEVEL_SAVE, fadeTo);
}
if (persons == 1 && message.sensor == 16){
saveLevelState(EEPROM_DIM_LEVEL_LAST, fadeTo);
Serial.print("New light level received: ");
Serial.println(fadeTo);
if (!changedByKnob)
knob.write(fadeTo);
// Cancel send if user turns knob while message comes in
changedByKnob = false;
sendDimValue = false;
// Stard fading to new light level
startFade();
} else if (persons == 0){
if (message.getString() != 0)
{gw.send(dimmerMsg.set(0), true);}
else
{return;}
}
//WENT
if (message.type == V_LIGHT){
if (message.sensor <noRelays){ // check if message is valid for relays..... previous line [[[ if (message.sensor <=noRelays){ ]]]
Relays[message.sensor].relayState = message.getBool();
digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState? RELAY_ON:RELAY_OFF); // and set relays accordingly
gw.saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
}
}
//END
}//}
void checkButtonClick()
{
if ( persons == 1 ) {
debouncer.update();
byte buttonVal = debouncer.read();
byte newLevel = 0;
if (buttonVal != oldButtonVal && buttonVal == LOW) {
if (dimValue==0) {
// Turn on light. Set the level to last saved dim value
// int saved = loadLevelState(EEPROM_DIM_LEVEL_SAVE);
int saved = 100;
newLevel = saved > 0 ? saved : 100;
}
gw.send(dimmerMsg.set(newLevel),true);
}
oldButtonVal = buttonVal;
}
}
void startFade() {
fadeDelta = ( fadeTo - dimValue ) < 0 ? -1 : 1;
lastFadeStep = millis();
}
// This method provides a graceful none-blocking fade up/down effect
void fadeStep() {
unsigned long currentTime = millis();
if ( dimValue != fadeTo && currentTime > lastFadeStep + FADE_DELAY) {
dimValue += fadeDelta;
analogWrite( LED_PIN, (int)(dimValue / 100. * 255) );
lastFadeStep = currentTime;
Serial.print("Fading level: ");
Serial.println(dimValue);
if (fadeTo == dimValue && changedByKnob) {
sendDimValue = true;
sendDimTimeout = currentTime;
}
}
// Wait a few millisecs before sending in new value (if user still turns the knob)
if (sendDimValue && currentTime > sendDimTimeout + SEND_THROTTLE_DELAY) {
// We're done fading.. send in new dim-value to controller.
// Send in new dim value with ack (will be picked up in incomingMessage)
gw.send(dimmerMsg.set(dimValue), true); // Send new dimmer value and request ack back
sendDimValue = false;
}
}
// Make sure only to store/fetch values in the range 0-100 from eeprom
int loadLevelState(byte pos) {
return min(max(gw.loadState(pos),0),100);
}
void saveLevelState(byte pos, byte data) {
gw.saveState(pos,min(max(data,0),100));
}