Automatic LED light - high WAF inside :)
-
Hi guys
A new project came on top of the others : ceiling light
The wife kinda likes the idea behind MySensors and openHab, as home automation and security system
But i had to hit hard to get it fully accepted. So here comes the "cat shaped ceiling light" !!!(
(Ok I know, ceiling is not finished yet... it has to be spotless as this kind of light reveals everything...)
LED strip is glued on the edge of the board, powered via a 12V power supply which is controlled via an OMRON SSR relaySome AutoCad and CNC later... :
This is highly inspired by Aproxx's work
https://www.openhardware.io/view/77/AC-DC-double-solid-state-relay-moduleWhat I want it to do :
- switch on the light when motion (PIR sensor)
- switch itself off with delay (PIR kind enough to do that as well)
- measure room light level so as to switch light on only when needed
- have 3 modes controlled via openHab : fully automatic, always ON, always OFF
So far i have managed to create a 3-state selector in openhab with this:
- ".items" file:
String HouseMode "House Mode [%s]"
- ".sitemap" file:
Switch item=LightMode mappings=[ON=1, AUTO=2, OFF=1]
Now I am having some trouble with the sketch...
I guess I should be using S_CUSTOM and V_CUSTOM (instead of S_BINARY and V_STATUS) as the "LightMode" variable can take up to 3 states...
How do i then retreive that value?void receive(const MyMessage &message) int lightMode = message.getInt();
Thanks for your input and comments
-
Maybe you could add a second binary switch to toggle AUTO mode and leave light as binary, otherwise you probably need to use something like S_LEVEL or similar
-
Thank you for your suggestion
It does work the way you suggested, with 2 binary switches
But that is not what i would like to achieve
-
Guys
Based on
- ".items file:"
String lightMode "lightMode [%s]
- ".sitemap" file:
Switch item=lightMode mappings=[ON=1, AUTO=2, OFF=1]
I would like to retreive the value of "lightMode" at the node side
This is what I am doing at the moment :
void receive(const MyMessage &message) { if (message.type == V_VAR1) { char lightMode[MAX_MESSAGE_LENGTH]; strncpy(lightMode, message.data, MAX_MESSAGE_LENGTH); }
Compiles and uploads fine
But state of lightMode doesn't change in node serial monitor...
Nothing is returned (empty field) when doing aSerial.println(lightMode)"
Could you please point me towards my mistake ? Am I (one more time) mixing data types (int and char and so on) ?
Thanks a lot for your time
- ".items file:"
-
Sorted
It is probably a bad idea to send data to node while sleeping...
Complete sketch for the record
// Enable debug prints #define MY_DEBUG #define MY_RADIO_NRF24 #define MY_PARENT_ID 0 #define MY_NODE_ID 19 #include <MySensors.h> // (Only input 2 and 3 generates interrupt!) #define PIN_MOTION 2 // Motion sensor input #define PIN_RELAY 5 // Omron relay output #define MOTION_ID 1 // Id of the motion sensor child #define RELAY_ID 2 // Id of the relay child int lightMode = 2; // 0=OFF - 1=ON - 2=auto bool prevTripped = 0; // previous state of motion sensor MyMessage msgMotion(MOTION_ID, V_TRIPPED); // Initialize motion message void setup() { pinMode(PIN_MOTION, INPUT); delay(3000); // wait for motion sensor voltage to stabilize pinMode(PIN_RELAY, OUTPUT); // Then set relay pins in output mode digitalWrite(PIN_RELAY, LOW); // Make sure relays are off when starting up } void presentation() { sendSketchInfo("Toilet Light", "27.10.2017"); present(MOTION_ID, S_MOTION); present(RELAY_ID, S_CUSTOM); } void loop() { bool tripped = digitalRead(PIN_MOTION) == HIGH; if ( tripped =! prevTripped ) { //only send data when there is a change send(msgMotion.set(tripped ? "1" : "0")); // Send tripped value to gw prevTripped = tripped; } if ( lightMode == 0 ) { digitalWrite(PIN_RELAY, LOW); } if ( lightMode == 1 ) { digitalWrite(PIN_RELAY, HIGH); } if ( (tripped == 1) && ( lightMode == 2 ) ) { digitalWrite(PIN_RELAY, HIGH); } if ( (tripped == 0) && ( lightMode == 2 ) ) { digitalWrite(PIN_RELAY, LOW); } } void receive(const MyMessage &message) { if (message.type == V_VAR1) { lightMode = atoi(message.data); } }
Thanks for reading ! Next project !!!
-
Hi guys
Could you please comment on this :
bool tripped = digitalRead(PIN_MOTION) == HIGH; if ( tripped =! prevTripped ) { //only send data when there is a change send(msgMotion.set(tripped ? "1" : "0")); // Send tripped value to gw prevTripped = tripped; }
Goal is to send update to gateway when state of PIR sensor changes
Trouble is that node sends state of sensor every cycle and floods karaf console...
Any clue ?
Also can anyone explain thisbool tripped = digitalRead(PIN_MOTION) == HIGH;
I dont quite understand.... read, store, and compare... all at once?
Thanks a lot for your help
Cheers
-
@ben999,
As for your last question, this line is simply converting the pin state (either HIGH or LOW) to a boolean. The right side expression evaluates to a boolean that is true when the pin is HIGH. This is assigned to the boolean variable "tripped". This makes it logically easy to use in the following IF statement.First question: I'm not sure why it would send every cycle. The logic of the IF statement seems correct, and since prevTripped is global, its state will be preserved between loops.
-
@ben999 Nice CNC work on the PCB.
-
@ben999 I would rewrite some of the logic in your code to shorten it up a bit. I would change this:
if ( lightMode == 0 ) { digitalWrite(PIN_RELAY, LOW); } if ( lightMode == 1 ) { digitalWrite(PIN_RELAY, HIGH); } if ( (tripped == 1) && ( lightMode == 2 ) ) { digitalWrite(PIN_RELAY, HIGH); } if ( (tripped == 0) && ( lightMode == 2 ) ) { digitalWrite(PIN_RELAY, LOW); }
To this:
if ( ( (tripped == 1) && ( lightMode == 2 ) ) || lightMode == 1 ) { digitalWrite(PIN_RELAY, HIGH); } if ( ( (tripped == 0) && ( lightMode == 2 ) ) || lightMode == 0 ) { digitalWrite(PIN_RELAY, LOW); }
-
@dbemowsk thanks a lot i like that!
Do you rekon that would "speed" things up? Not that i need more speed to switch that light on but would this simplify and shorten the sketch processing time?
-
@billgoolsby thanks a lot for your comments that make more sense now
Trouble is that if sketch remains as above it doesn't get into the
if ( tripped =! prevTripped )
loop (i have putSerial.print
here and there and i never get to it)
On top of thattripped
stays locked on HIGH but light is off...Any clue? Sketch seems fine to you?
Thanks for your time
-
@dbemowsk said in Automatic LED light - high WAF inside :
@ben999 Nice CNC work on the PCB.
I have some neat little tools in my workshop:
3x1.5m working area
500x300mm working area
-
@ben999 OK, NO FAIR. Is the bottom one a laser cutter/engraver??
-
@dbemowsk i know i'm lucky to have all those toys handy
This is a 2.2kW spindle... other one is 13kW in case i need more
-
@ben999 .oO{ JEALOUS }Oo.
-
@ben999 just for the record...
=!
is wrong. Should have been!=
Makes quite a difference