Navigation

    • Register
    • Login
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular
    1. Home
    2. chickey
    3. Best
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Best posts made by chickey

    • Dimable Security light with PIR/Temp/Humidity and Voltage sense.

      I've mashed together a few sketches to create a kind of security light. I've not put any logic for when the light should come on in the sketch and instead let my Domoticz controller decide when and what intensity the light should come on. In this way i have it set to a very low level of around %15 from 30minutes after sunset till around 11 but if motion is detected i bump it up to %75 for 1 minute. I've done it this way as i can control it remotely and also modify the delay/intensity etc without having to open up the case. I'm also sensing the battery voltage of a 12v battery. I'm using a 1M and 470k resistor although i had to play with the resistor values, the 470k one in particular in the code to get it to be accurate. I am sending the temp every 2 mins and the battery voltage every 5 mins using a timer and the main loop monitors the motion sensor and sends that it's been triggered otherwise it just loops around again. I had looked at interrupts and sleeping inbetwen but then i couldn't see a way to control it remotely as Domoticz wouldn't know when it was awake. It seems to be working ok for me so hope it helps some people.

      I housed it all in a small project box which is secured to the underside of my garage and the light then connects directly to the circuit. If anyone is interested in a fritzing diagram then please let me know.

      0_1462452224492_IMG_4579.JPG
      0_1462452234108_IMG_4576.JPG
      0_1462452213582_IMG_4580.JPG
      0_1462452143142_IMG_4582.JPG

      /**
       * The MySensors Arduino library handles the wireless radio link and protocol
       * between your home built sensors/actuators and HA controller of choice.
       * The sensors forms a self healing radio network with optional repeaters. Each
       * repeater and gateway builds a routing tables in EEPROM which keeps track of the
       * network topology allowing messages to be routed to nodes.
       *
       * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
       * Copyright (C) 2013-2015 Sensnology AB
       * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
       *
       * Documentation: http://www.mysensors.org
       * Support Forum: http://forum.mysensors.org
       *
       * This program is free software; you can redistribute it and/or
       * modify it under the terms of the GNU General Public License
       * version 2 as published by the Free Software Foundation.
       *
       *******************************
      
       *
       * DESCRIPTION
       * This sketch provides a Dimmable LED Light along with Temp/Humidity/voltage sensing as well as Motion sensing.
       */
      
      #define SN "SecurityLED"
      #define SV "1.2"
      #define CHILD_ID_HUM 0
      #define CHILD_ID_TEMP 1
      #define CHILD_ID_VOLTAGE 2
      #define CHILD_ID_PIR 3
      
      #define HUMIDITY_SENSOR_DIGITAL_PIN 6
      #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.
      #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
      
      #include <MySensor.h> 
      #include <SPI.h>
      #include <DHT.h> 
      #include <SimpleTimer.h>
      
      // the timer object
      SimpleTimer timer;
      
      #define LED_PIN 5      // Arduino pin attached to MOSFET Gate pin
      //#define 
      int FADE_DELAY=50;  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)
      
      MySensor gw;
      
      DHT dht;
      float lastTemp;
      boolean metric = true;
      float lastHum;
      float lastVolt;
      int oldBatteryPcnt = 0;
      int analogInput = A0;
      float vout = 0.0;
      float vin = 0.0;
      int value = 0;
      float R1 = 1000000.0;  
      float R2 = 454500.0;
      boolean sensordelay;
      
      static int currentLevel = 0;  // Current dim level...
      MyMessage dimmerMsg(0, V_DIMMER);
      MyMessage lightMsg(0, V_LIGHT);
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      MyMessage msgVolt(CHILD_ID_VOLTAGE, V_VOLTAGE); 
      MyMessage msg(CHILD_ID_PIR, V_TRIPPED);
      
      /***
       * Dimmable LED initialization method
       */
      void setup()  
      { 
        Serial.println( SN ); 
        gw.begin( incomingMessage );
        dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
        
        pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
        // Register the LED Dimmable Light with the gateway
        gw.present( 0, S_DIMMER );
       
        gw.sendSketchInfo(SN, SV);
        // Pull the gateway's current dim level - restore light level upon sendor node power-up
        //gw.request( 0, V_DIMMER );
        gw.present(CHILD_ID_HUM, S_HUM);
        gw.present(CHILD_ID_TEMP, S_TEMP);
        gw.present(CHILD_ID_VOLTAGE, V_VOLTAGE);
        gw.present(CHILD_ID_PIR, S_MOTION);
      
        pinMode(analogInput, INPUT);
        timer.setInterval(120000,readTempHum);  // Send Temp every 2 mins
        timer.setInterval(300000,measureBattery);  // Send voltage every 5 mins
        fadeToLevel( 0 );
        readTempHum();
        measureBattery();
        sensordelay=0;
      }
      
      void ResetTripped(){
            sensordelay=0;
            FADE_DELAY=50;
      }
      
      /***
       *  Main processing loop 
       */
      void loop() 
      {
        gw.process();  
      
        // Read digital motion value
        boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        if (tripped ==1 && sensordelay==0) {
          FADE_DELAY=0;
          Serial.print("Security Sensor state : ");
          Serial.println(tripped);
          gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
          tripped=0;
          gw.send(msg.set(tripped?"1":"0"));  // Send tripped value to gw
          sensordelay=1;
          timer.setTimeout(10000,ResetTripped);
        }
        timer.run();
      }
      
      
      
      void incomingMessage(const MyMessage &message) {
        if (message.type == V_LIGHT || message.type == V_DIMMER) {
          
          //  Retrieve the power or dim level from the incoming request message
          int requestedLevel = atoi( message.data );
          
          // Adjust incoming level if this is a V_LIGHT variable update [0 == off, 1 == on]
          requestedLevel *= ( message.type == V_LIGHT ? 100 : 1 );
          
          // Clip incoming level to valid range of 0 to 100
          requestedLevel = requestedLevel > 100 ? 100 : requestedLevel;
          requestedLevel = requestedLevel < 0   ? 0   : requestedLevel;
          
          Serial.print( "Changing level to " );
          Serial.print( requestedLevel );
          Serial.print( ", from " ); 
          Serial.println( currentLevel );
      
          fadeToLevel( requestedLevel );
          
          // Inform the gateway of the current DimmableLED's SwitchPower1 and LoadLevelStatus value...
          gw.send(lightMsg.set(currentLevel > 0 ? 1 : 0));
      
          // hek comment: Is this really nessesary?
          gw.send( dimmerMsg.set(currentLevel) );
          
          }
      }
      
      /***
       *  This method provides a graceful fade up/down effect
       */
      void fadeToLevel( int toLevel ) {
      
        int delta = ( toLevel - currentLevel ) < 0 ? -1 : 1;
        
        while ( currentLevel != toLevel ) {
          currentLevel += delta;
          analogWrite( LED_PIN, (int)(currentLevel / 100. * 255) );
          delay( FADE_DELAY );
        }
      }
      
      void readTempHum() {
       delay(dht.getMinimumSamplingPeriod());
        float temperature = dht.getTemperature();
        if (isnan(temperature)) {
            Serial.println("Failed reading temperature from DHT");
        } else if (temperature != lastTemp) {
          lastTemp = temperature;
          if (!metric) {
            temperature = dht.toFahrenheit(temperature);
          }
          gw.send(msgTemp.set(temperature, 1));
          Serial.print("T: ");
          Serial.println(temperature);
        }
        
        float humidity = dht.getHumidity();
        if (isnan(humidity)) {
            Serial.println("Failed reading humidity from DHT");
        } else if (humidity != lastHum) {
            lastHum = humidity;
            gw.send(msgHum.set(humidity, 1));
            Serial.print("H: ");
            Serial.println(humidity);
        } 
      }
      
      void measureBattery() {
         // read the value at analog input
         value = analogRead(analogInput);
         vout = (value * 5.0) / 1024.0; // see text
         vin = vout / (R2/(R1+R2)); 
         int batteryPcnt = value / 10;
            
         #ifdef DEBUG
         Serial.print("Battery Voltage: ");
         Serial.print(vin);
         Serial.println(" V");
      
         Serial.print("Battery percent: ");
         Serial.print(batteryPcnt);
         Serial.println(" %");
         #endif
         
         if (oldBatteryPcnt != batteryPcnt) {
           // Power up radio after sleep
           gw.sendBatteryLevel(batteryPcnt);
           gw.send(msgVolt.set(vin, 1));
           oldBatteryPcnt = batteryPcnt;
         }
      }
      
      posted in My Project
      chickey
      chickey
    • RE: Universal board bought from from Slovenia Radio Woes. (solved)

      @mfalkvidd @MikeF I've now resolved the issue. With this board you have to set D3 high so that it powers the radio, shame as that really ties pin 3 up then but i guess he's done it to make the radio optional as he supports bluetooth as well. It's a nice lower power board but i think i've been spoilt by the otherboards i've used which have used the same pinouts etc. I think the solar one which you have may have been of more use thinking about it but hey ho. It's trivial but just incase anyone else is as simple minded as me i put the following in setup

      pinMode(3, OUTPUT);
      digitalWrite(3,HIGH);

      Thanks
      Col

      posted in Hardware
      chickey
      chickey
    • RE: Dimmer issues.

      Rude to reply to your own posts or so i'm told but for the benefit of others i was being an idiot and pin 4 isn't pwm (facepalm) i was looking at this for a while last night as well. Ah well never mind it's working brilliantly now after switching to pin 5 🙂

      posted in Troubleshooting
      chickey
      chickey
    • RE: Dimable Security light with PIR/Temp/Humidity and Voltage sense.

      @koen01 I spent some time after work and updated the code so i've updated the first post with the code in. It now works for the PIR part.

      posted in My Project
      chickey
      chickey
    • RE: Is there a mysensor thread for this "mysensor" pro mini adapter?

      In case anyone is interested this is what the assembled board looks like on mine minus any case. You could get it a lot thinner if you skipped the connectors and soldered directly but i've an aversion to that as i keep tinkering.

      1_1463478114699_IMG_4690.JPG
      2_1463478114699_IMG_4689.JPG
      0_1463478114698_IMG_4688.JPG

      posted in Hardware
      chickey
      chickey
    • RE: Is there a mysensor thread for this "mysensor" pro mini adapter?

      If anyone is thinking of using this board Cory produced a PDF which has build instructions for 3.3 and 5v, I've uploaded it so it's easy for people to find.

      0_1463498990546_MySensor Pro Mini V2 - Google Docs.pdf

      posted in Hardware
      chickey
      chickey
    • RE: PWM frequencies on pin 9,10,11

      You my friend are a genius, i was having flickering on my dimmer and couldn't get to the bottom of it. I was using pin 6 for controlling my LED strip.

      @gregl said:

      I was having some probs with the led dimmer sketch the other night beacuse i wanted to use PIN6 rather than PIN3.
      My LED strip would flicker....had me puzzled for a bit until i remembered the mysensors v2 beta ( im using quite an old cut of it ) uses pin 6 for the RX/TX/ERR leds...
      So i just reassiged them in my sketch...and problem solved!
      #define MY_DEFAULT_TX_LED_PIN A2
      #define MY_DEFAULT_RX_LED_PIN A3
      #define MY_DEFAULT_ERR_LED_PIN A4

      This may not be relevant to your posts... but i thought it was a PWM frequency issue initially!

      posted in Hardware
      chickey
      chickey
    • RE: RF Nano = Nano + NRF24, for just $3,50 on Aliexpress

      How are people getting on with these boards, mine just arrived today so doing a bit of testing. I wanted one as a replacement gateway for home assistant. I've got it working at 1mb and the pin changes suggested above, just wondered how they worked out for people in practice over the long term.

      I also wanted to mention that there is another option although it would work out more expensive

      https://www.aliexpress.com/item/Keywish-Nano-Terminal-Expansion-Adapter-Board-for-Arduino-Nano-V3-0-AVR-ATMEGA328P-with-NRF2401-Expansion/32957033159.html?spm=a2g0s.9042311.0.0.6df84c4dcMws6c

      posted in Hardware
      chickey
      chickey