<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[My motion / dallas / light sensor]]></title><description><![CDATA[<p dir="auto">Hello,<br />
I was looking for a good working non-battery powered 3 in 1 sketch but couln't find one, that worked the way i wanted it to work. My wishlist:</p>
<ul>
<li>Instant update's on temp and light changes</li>
<li>Only send motion stop if motion stopped for at least 10 seconds</li>
<li>Only send motion start if motion stopped</li>
<li>No delays</li>
<li>Make it easy to add other sensors / remove excisting ones *</li>
<li>Enable / disable serial debug by define *</li>
</ul>
<p dir="auto">I have made this sketch, hope it can be usefull to someone else. Have tried to document the code as good as possible. I'm planning to add defines in the future to be able to add / remove a temp sensor without altering the whole sketch. So just define light / temp / hum / dhtxx etc, and the sketch will work. The same goes for serial debugging,</p>
<p dir="auto">What i used: Dallas DS18b20 , PIR, LDR</p>
<pre><code>// Include libaries
#include &lt;SPI.h&gt;
#include &lt;MySensor.h&gt;  
#include &lt;DallasTemperature.h&gt;    // For Dallas temp sensor
#include &lt;OneWire.h&gt;              // For Dallas temp sensor

// Define mysensors data
#define CHILD_ID_LIGHT 0             // LDR child ID
#define CHILD_ID_TEMP 1              // Temp child ID
#define CHILD_ID_MOTION 2            // MOTION child ID

#define LIGHT_SENSOR_ANALOG_PIN A2   // LDR PIN
#define MOTION_INPUT_SENSOR 2        // Motion input pin
#define ONE_WIRE_BUS 3               // DALLAS pin
#define MAX_ATTACHED_DS18B20 16      // DALLAS max nr. of sensors

// Initialize mysensor
MySensor gw;

// Initialize dallas
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&amp;oneWire);

// Store variables
long previousMillis = 0;        // last time the sensors are updated
long sensorInterval = 5000;     // interval at which we will take a measurement ( 5 seconds)
int lastLightLevel;             // Holds last light level
float lastTemperature[MAX_ATTACHED_DS18B20]; // Holds DALLAS temperatures
int numSensors=0;               // Holds number of sensors
boolean metric = true;          // Celcius or fahrenheid
int calibrationTime = 30;       // the time we give the sensor to calibrate (10-60 secs according to the datasheet)        
long unsigned int lowIn;        // the time when the sensor outputs a low impulse
long unsigned int pause = 10000;//the amount of milliseconds the sensor has to be low before we assume all motion has stopped
boolean lockLow = true;
boolean takeLowTime;  

// Initialize childs
MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
MyMessage msgTemp(CHILD_ID_TEMP,V_TEMP);
MyMessage msgMot(CHILD_ID_MOTION, V_TRIPPED);

void setup() {
  sensors.begin();
  gw.begin();
  // Send sketch info to controller
  gw.sendSketchInfo("Light sensor without delay", "1.0");
  // Register child sensors
  gw.present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
  gw.present(CHILD_ID_MOTION, S_MOTION);
  // Set pinmode for motion sensor
  pinMode(MOTION_INPUT_SENSOR, INPUT);
  // Pull down motion pin
  digitalWrite(MOTION_INPUT_SENSOR, LOW);
  numSensors = sensors.getDeviceCount();
  // Present all sensors to controller
   for (int i=0; i&lt;numSensors &amp;&amp; i&lt;MAX_ATTACHED_DS18B20; i++) {   
      gw.present(i, S_TEMP);
   }
  delay(calibrationTime); // Give pir time to calibrate
  
}

void loop()
{
  // Check if sensors have changed
  sensorCheck();
  
  if(digitalRead(MOTION_INPUT_SENSOR) == HIGH){
    if(lockLow){  
      //makes sure we wait for a transition to LOW before any further output is made:
      lockLow = false;            
      Serial.print("motion detected at ");
      Serial.print(millis()/1000);
      Serial.println(" sec"); 
      gw.send(msgMot.set("1"));  // Send tripped value to gw
    }         
    takeLowTime = true;
  }
  if(digitalRead(MOTION_INPUT_SENSOR) == LOW){       
     if(takeLowTime){
      lowIn = millis();          //save the time of the transition from high to LOW
      takeLowTime = false;       //make sure this is only done at the start of a LOW phase
      }
     //if the sensor is low for more than the given pause, we assume that no more motion is going to happen
     if(!lockLow &amp;&amp; millis() - lowIn &gt; pause){  
         //makes sure this block of code is only executed again after new motion sequence has been detected
         lockLow = true;                        
         Serial.print("motion ended at ");      //output
         Serial.print((millis() - pause)/1000);
         Serial.println(" sec");
         gw.send(msgMot.set("0"));  // Send tripped value to gw
     }
   }
}

void sensorCheck(){
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis &gt; sensorInterval) {
    // Save the current millis 
    previousMillis = currentMillis;   
    // take action here:
    
    // Begin light measurement
    int lightLevel = (1023-analogRead(LIGHT_SENSOR_ANALOG_PIN))/10.23;
    Serial.print("Lightlevel: ");
    Serial.println(analogRead(LIGHT_SENSOR_ANALOG_PIN));
    if (lightLevel != lastLightLevel) {
      gw.send(msgLight.set(lightLevel));
      lastLightLevel = lightLevel;
    }
    // end light measurement
    
    // Begin dallas temperature
    // Fetch temperatures from Dallas sensors
    sensors.requestTemperatures();
     // Read temperatures and send them to controller 
    for (int i=0; i&lt;numSensors &amp;&amp; i&lt;MAX_ATTACHED_DS18B20; i++) {
      // Fetch and round temperature to one decimal
      float temperature = static_cast&lt;float&gt;(static_cast&lt;int&gt;((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
      Serial.print("Temperature ");
      Serial.print(i);
      Serial.print(" ");
      Serial.println(temperature);
     // Only send data if temperature has changed and no error
     if (lastTemperature[i] != temperature &amp;&amp; temperature != -127.00) {
        // Send in the new temperature
        gw.send(msgTemp.setSensor(i).set(temperature,1));
        lastTemperature[i]=temperature;
      }
    } 
    // End dallas temperarures
  }
}
</code></pre>
]]></description><link>https://forum.mysensors.org/topic/1006/my-motion-dallas-light-sensor</link><generator>RSS for Node</generator><lastBuildDate>Wed, 11 Nov 2026 09:11:33 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/1006.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 18 Feb 2015 21:34:16 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to My motion / dallas / light sensor on Wed, 27 Jan 2016 06:28:18 GMT]]></title><description><![CDATA[<p dir="auto">Are you using a wifi gateway? Others reported strange values too: <a href="http://forum.mysensors.org/topic/2934/wifi-gateway-motion-sensor-value-1107296256-instead-of-tripped/2" rel="nofollow ugc">http://forum.mysensors.org/topic/2934/wifi-gateway-motion-sensor-value-1107296256-instead-of-tripped/2</a></p>
]]></description><link>https://forum.mysensors.org/post/29793</link><guid isPermaLink="true">https://forum.mysensors.org/post/29793</guid><dc:creator><![CDATA[ericvdb]]></dc:creator><pubDate>Wed, 27 Jan 2016 06:28:18 GMT</pubDate></item><item><title><![CDATA[Reply to My motion / dallas / light sensor on Tue, 26 Jan 2016 23:51:08 GMT]]></title><description><![CDATA[<p dir="auto">Actually - it gets weirder.</p>
<p dir="auto">Does the DS18b20 contain a barometer? Not according to the datasheet. But this node, using the sketch above, seems to create additional sensors, some with garbage data, and some with what appear to be barometric pressure readings. I have some other (BMP180) mysensors which report the same value for pressure as the sketch above.</p>
<p dir="auto">I can't understand what's causing the garbage data though. For example, MySensorsDevice#33 has a reading of 1149829168. Some are null, and there are a few other random integers there too. Each with their own MySensorsDevice#.</p>
<p dir="auto">Can anyone point me in the right direction as to what's going on? It's getting annoying having to remove all these fake sensors daily! :)</p>
]]></description><link>https://forum.mysensors.org/post/29786</link><guid isPermaLink="true">https://forum.mysensors.org/post/29786</guid><dc:creator><![CDATA[velkrosmaak]]></dc:creator><pubDate>Tue, 26 Jan 2016 23:51:08 GMT</pubDate></item><item><title><![CDATA[Reply to My motion / dallas / light sensor on Fri, 22 Jan 2016 00:50:20 GMT]]></title><description><![CDATA[<p dir="auto">Perfect! Thank you!</p>
]]></description><link>https://forum.mysensors.org/post/29212</link><guid isPermaLink="true">https://forum.mysensors.org/post/29212</guid><dc:creator><![CDATA[velkrosmaak]]></dc:creator><pubDate>Fri, 22 Jan 2016 00:50:20 GMT</pubDate></item><item><title><![CDATA[Reply to My motion / dallas / light sensor on Tue, 19 Jan 2016 05:00:33 GMT]]></title><description><![CDATA[<p dir="auto">The 0 before 65.30 is the index number of the temperature sensor.</p>
<p dir="auto">And to get Celsius, you should make the controller return true for gw.getConfig().isMetric or change</p>
<pre><code>float temperature = static_cast&lt;float&gt;(static_cast&lt;int&gt;((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
</code></pre>
<p dir="auto">to</p>
<pre><code>float temperature = static_cast&lt;float&gt;(static_cast&lt;int&gt;((metric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
</code></pre>
]]></description><link>https://forum.mysensors.org/post/28958</link><guid isPermaLink="true">https://forum.mysensors.org/post/28958</guid><dc:creator><![CDATA[ericvdb]]></dc:creator><pubDate>Tue, 19 Jan 2016 05:00:33 GMT</pubDate></item><item><title><![CDATA[Reply to My motion / dallas / light sensor on Tue, 19 Jan 2016 00:40:20 GMT]]></title><description><![CDATA[<p dir="auto">I just gave this a try and it works great! I can't believe nobody else has commented yet.</p>
<p dir="auto">The only issue I have is that there doesn't appear to be a Celsius reading, despite the fact that metric is set to true at the top of the code.</p>
<p dir="auto">This is what I see over the console:</p>
<pre><code>Temperature 0 65.30
Lightlevel: 290
Temperature 0 65.30
Lightlevel: 324
send: 0-0-0-0 s=10,c=1,t=23,pt=2,l=2,sg=0,st=fail:69
Temperature 0 65.30
</code></pre>
<p dir="auto">I'm guessing the 0 before the 65.30 is where the Celsius reading is supposed to be, but I can't see in the code where the temperature is being calculated.</p>
<p dir="auto">Excuse a noob! Thanks for any help! :)</p>
]]></description><link>https://forum.mysensors.org/post/28954</link><guid isPermaLink="true">https://forum.mysensors.org/post/28954</guid><dc:creator><![CDATA[velkrosmaak]]></dc:creator><pubDate>Tue, 19 Jan 2016 00:40:20 GMT</pubDate></item></channel></rss>