BcSensor Mosfet

  • 2x connectors for Dallas temperature sensor
  • 1 - photocell connector (light sensor)
  • 1 DHT-22 connector (humidity and temperature)
  • 1 connector for pir sensor (HC-SR501 PIR Motion Sensor Module)
  • 1 connector for PWM IRF3205 (led light controller )
  • 1 conenctor for Magnetic Door Switch Sensor
  • 1 conenctor for power out (5v)

(this version has a 1117 3.3v regulator and 7805 5.0v)

		Partlist
Exported from node-mosfet-v2.8.sch at 22.02.2015 20:49:44
Assembly variant: 
Part       Value          Device        Package        Library             Sheet

C2         10uF           CPOL-USE2.5-6 E2,5-6         adafruit            1
C3         10uF           CPOL-USE2.5-6 E2,5-6         adafruit            1
C4         4.7uF          CPOL-USE2.5-6 E2,5-6         adafruit            1
C5         0.1uF          C-USC1206     C1206          adafruit            1
C6         10uF/25v       CPOL-USE2.5-6 E2,5-6         adafruit            1
C7         100uF/6v       CPOL-USE2.5-6 E2,5-6         adafruit            1
C8         0.1uF          C-USC1206     C1206          adafruit            1
D1         1N4004         1N4004        DO41-10        adafruit            1
DOOR                      JST_2PIN-THM  JST-PH-2-THM   adafruit            1
DS18B20                   PINHD-1X3     1X03           adafruit            1
IC1        REG1117        REG1117       SOT223         burr-brown          1
IC2        7805           78MXXS        78MXXS         v-reg               1
JP1        DHT-22         PINHD-1X3     1X03           adafruit            1
JP2        PWR IN         M02           02P            con-amp-quick       1
JP3                       PINHD-1X2     1X02           pinhead             1
JP4        PIR            PINHD-1X3     1X03           adafruit            1
JP5                       M03POLAR_LOCK MOLEX-1X3_LOCK SparkFun-Connectors 1
NRF                       PINHD-2X4     2X04           adafruit            1
PHOTOCELL1                JST_2PIN-THM  JST-PH-2-THM   adafruit            1
PWR                       M02           02P            con-amp-quick       1
Q1         IRLZ44Z        IRLZ44Z       TO220BH        transistor-power    1
R1         4k7            RESISTOR1206  1206           digital-toy         1
R2         0              RESISTOR1206  1206           digital-toy         1
R4         10k            RESISTOR1206  1206           digital-toy         1
R5         1k             RESISTOR1206  1206           digital-toy         1
R6         0              RESISTOR1206  1206           digital-toy         1
R7         0              RESISTOR1206  1206           digital-toy         1
R8         0              RESISTOR1206  1206           digital-toy         1
R9         10k            RESISTOR1206  1206           digital-toy         1
U$1        ARDUINO_MINI   ARDUINO_MINI  ARDUINO_MINI   SparkFun-Boards     1
------------------------------------------------------------------------------------------------------------------------
#define SN "BcSensor Mosfet"
#define SV "1.1"

#include <MySensor.h> 
#include <SPI.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <DHT.h>  


#define NUMBER_OF_LIGHTS 1
//#define NUMBER_DS 1
//#define NUMBER_DHT 1
#define NUMBER_PIR 1

#define LED_PIN 5      // Arduino pin attached to MOSFET Gate pin
#define FADE_DELAY 5  // Delay in ms for each percentage fade up/down (10ms = 1s full-range dim)

#define ONE_WIRE_BUS 2           
#define MAX_ATTACHED_DS18B20 16

#define LIGHT_SENSOR_ANALOG_PIN A2
int lastLightLevel;
static int currentLevel = 0;  // Current dim level...
unsigned long SLEEP_TIME = 30000;
unsigned long lastRequest, now;
MySensor gw;
#ifdef NUMBER_DS // dallas sensor
  OneWire oneWire(ONE_WIRE_BUS);
  DallasTemperature sensors(&oneWire);
  float lastTemperature[MAX_ATTACHED_DS18B20];
  int numSensors = 0;
  MyMessage msg(0, V_TEMP);
#endif

#ifdef NUMBER_DHT // DTH sensor
  #define HUMIDITY_SENSOR_DIGITAL_PIN 4
  #define CHILD_ID_HUM 1
  #define CHILD_ID_TEMP 1
  DHT dht;
  float lastTemp;
  float lastHum;
  MyMessage msgHum(CHILD_ID_HUM, V_HUM);
  MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
#endif

#ifdef NUMBER_PIR //PIR Sensor
  #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
  #define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)
  #define CHILD_ID 0   // Id of the sensor child
  boolean lastTrippedState;
  MyMessage msgMotion(CHILD_ID, V_TRIPPED);
#endif
boolean metric = true;

MyMessage dimmerMsg(0, V_DIMMER);
MyMessage lightMsg(0, V_LIGHT);

#ifdef NUMBER_OF_LIGHTS // light sensor
  MyMessage msgL(0, V_LIGHT_LEVEL);
#endif
void setup()  
{ 
//  analogReference(INTERNAL);
  Serial.println( SN ); 
  gw.begin( incomingMessage );
  
  gw.present( 0, S_DIMMER );
  gw.sendSketchInfo(SN, SV);
  gw.request( 0, V_DIMMER );
  #ifdef NUMBER_DS //dallas sensor
    sensors.begin();
    numSensors = sensors.getDeviceCount();
    for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) {
      gw.present(i, S_TEMP);
    }
  #endif
  #ifdef NUMBER_OF_LIGHTS // light sensor
    pinMode(LIGHT_SENSOR_ANALOG_PIN, INPUT);
    gw.present(0, S_LIGHT_LEVEL);
  #endif
  #ifdef NUMBER_DHT // DHT sensor
    dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    gw.present(CHILD_ID_HUM, S_HUM);
    gw.present(CHILD_ID_TEMP, S_TEMP);
  #endif
  #ifdef NUMBER_PIR //PIR Sensor
    pinMode(DIGITAL_INPUT_SENSOR, INPUT); 
    gw.present(CHILD_ID, S_MOTION);
  #endif
}


void loop() 
{
  gw.process();
   now = millis();
  if (now - lastRequest >= SLEEP_TIME || lastRequest < 1) {
    Serial.println("loop");
    lastRequest = now;
    #ifdef NUMBER_DS // DALLAS
      sensors.requestTemperatures();                                                    
      for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) {
        float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric ? sensors.getTempCByIndex(i) : sensors.getTempFByIndex(i)) * 10.)) / 10.;
        if (abs(lastTemperature[i] - temperature) >= 0.5 && temperature != -127.00) {
          gw.send(msg.setSensor(i).set(temperature, 1));
          lastTemperature[i] = temperature;
        }
      }
    #endif //end DALLAS
    #ifdef NUMBER_OF_LIGHTS // senzor lumina
      int lightLevel = (1023 - analogRead(LIGHT_SENSOR_ANALOG_PIN)) / 10.23;
      if (lightLevel != lastLightLevel) {
        gw.send(msgL.set(lightLevel));
        lastLightLevel = lightLevel;
      }
      Serial.println(lightLevel);
    #endif // end senzor lumina
    
    #ifdef NUMBER_DHT
      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);
    }
    #endif
  }
   #ifdef NUMBER_PIR //PIR Sensor
    boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
    if (tripped != lastTrippedState){  
      Serial.println(tripped? "tripped" : "not tripped");
      gw.send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw//
      lastTrippedState = tripped;
    }
   #endif
}



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 );
  }
}