gproduct
Posts: 59
Joined: Tue Aug 11, 2015 1:27 pm

Sensor working 24/7 script

Sat Oct 03, 2015 9:05 am

Hello
I've set up my raspberry pi for sending emails at certain time(cron) etc.
But I have a PIRsensor. I know that it needs a while loop to run but if I put it that way,I don't think the piwill survive long.
Please give me suggestions.

Heater
Posts: 13303
Joined: Tue Jul 17, 2012 3:02 pm

Re: Sensor working 24/7 script

Sat Oct 03, 2015 9:11 am

gproduct,

Why would you Pi not survive long?

Consider, as long as you Pi is booted up and running Raspbian or whatever operating system it is basically running a "DO FOREVER" loop all the time already. We expect that it can do this for years and decades without any problem. Like most computers can.

Of course in your program you will actually not be running your code all the time. You will have something like:

Code: Select all

DO FOREVER
   DO whatever work 
   SLEEP(some time)
END
Or perhaps you will be waiting on data to arrive from some serial port or other device.

gproduct
Posts: 59
Joined: Tue Aug 11, 2015 1:27 pm

Re: Sensor working 24/7 script

Sat Oct 03, 2015 9:14 am

Yes, I know.
But how much is that sleep?
Thx

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Sensor working 24/7 script

Sat Oct 03, 2015 9:20 am

gproduct wrote:Yes, I know.
But how much is that sleep?
Thx
As short or as long as you want.

If it's too short, your program will use more CPU cycles.

If it's too long, you might miss some data from your sensor.

You should experiment and find the best length of time for your application.

gproduct
Posts: 59
Joined: Tue Aug 11, 2015 1:27 pm

Re: Sensor working 24/7 script

Sat Oct 03, 2015 9:26 am

Okay, thank you.

Heater
Posts: 13303
Joined: Tue Jul 17, 2012 3:02 pm

Re: Sensor working 24/7 script

Sat Oct 03, 2015 9:34 am

How long do you want that sleep to be?

If your program is a shell script then sleep(x) is x number of seconds.

If you need shorter sleep times then use a language that supports millisecond or shorter sleeps.

I like to use Javascript for this kind of thing:

Code: Select all

function doStuff () {
    console.log("Hello, doing stuff...");
}

// Do stuff every 1000 milliseconds
setInterval(doStuff, 1000);
Then I can have lot's of stuff happening at all kind of intervals.

jehutting
Posts: 139
Joined: Sun Feb 15, 2015 8:37 am
Location: The Netherlands

Re: Sensor working 24/7 script

Sat Oct 03, 2015 10:04 am

Does your PIR sensor needs 'a while loop to run'?

Isn't it just connected to a GPIO input, which is False (0,zero) when there is no motion, and True (1,one) when there is motion?

If this latter is the case, you can use (python) RPi.GPIO module and use its add_event_detect() to add a callback function on a (rising/falling/both) edge of your GPIO input. In the callback function you simply send the email.

According to a GPIO can be configured as an interrupt source to the ARM I would expect that the RPI.GPIO uses this mechanism. So no time is/should be spent on looping.

tbd.pi
Posts: 34
Joined: Sat Aug 11, 2012 11:21 am
Location: NJ,USA
Contact: Website

Re: Sensor working 24/7 script

Sat Oct 03, 2015 11:50 am

jehutting even if you use the interrupt, you've still got to make the program wait in some way. How do you avoid the loop?

jehutting
Posts: 139
Joined: Sun Feb 15, 2015 8:37 am
Location: The Netherlands

Re: Sensor working 24/7 script

Sat Oct 03, 2015 12:51 pm

tbl.pl, you are right, there is always (somewhere) a MAIN loop needed while running an application. even with the GPIO interrupt handling. That is what Heater in above post is telling.

What I was trying to point out is that there is no need to check the GPIO input (PIR checking) into the (main) loop.

Heater
Posts: 13303
Joined: Tue Jul 17, 2012 3:02 pm

Re: Sensor working 24/7 script

Sat Oct 03, 2015 1:30 pm

Usually programs have a main loop. This need not always be the case though. It's a kind of old fashioned and pointless idea.

That piece of Javascript I posted above is a complete program. It has no loop! It sets up the interval timer to run doStuff() every second. Then it has nothing to do so it just stops. The program does not terminate though when it runs off the end of the code though. A Javascript program will not terminate while it is possible that some event may trigger some action. An event could be data arriving from a file, network interface, serial port, GPIO etc.

Here an example of waiting for a pin change on Javascript using the wiring-pi module:

Code: Select all

var wpi = require('wiring-pi');

wpi.setup('wpi');
wpi.pinMode(7, wpi.INPUT);
wpi.pullUpDnControl(7, wpi.PUD_UP);

doStuff(delta) {
    console.log('Pin 7 changed to LOW (', delta, ')');
}

wpi.wiringPiISR(7, wpi.INT_EDGE_FALLING, doStuff);
See documentation here: https://github.com/eugeneware/wiring-pi ... NTATION.md

Or we could imagine programming on the bare metal of the processor, no operating system at all, and just setting up interrupt handlers to handle events.

jehutting
Posts: 139
Joined: Sun Feb 15, 2015 8:37 am
Location: The Netherlands

Re: Sensor working 24/7 script

Sat Oct 03, 2015 1:41 pm

Heater, I'm old fashioned :D . Is it possible in python to have something similar (a loop-less main)?

Heater
Posts: 13303
Joined: Tue Jul 17, 2012 3:02 pm

Re: Sensor working 24/7 script

Sat Oct 03, 2015 2:40 pm

This "loopless main" approach to programming is generally called "event driven programming".

You can do it in any language given that you have some library support for it. In Python there are number of event-driven programming libraries, the most famous of which seems to be "twisted" https://twistedmatrix.com/trac/

Of course Javascript engines are written in C++ which in turn needs and event system. Node.js uses libuv https://github.com/libuv/libuv

Hmm...seems that libuv is usable in Python as well. https://github.com/saghul/pyuv

I won't make any recommendations as I have never used these things with Python.

One thing to be aware of is that JavaScript is and always has been based on the event-driven model. That means that everything in node.js is compatible with that style. For most other languages this is not the case and most libraries do not support even-driven programming. So, for example, calling some library function that takes a long time to complete will hang up your even-t handling code. Not good.

All in all this makes it easier to work in JS.

Return to “General discussion”