Python Forum
Some random (and probably dumb) questions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some random (and probably dumb) questions
#1
Hello,

I've just gotten into learning more about both linux and python, mostly via a raspberry pi. Ive got some questions that I don't really know how to word for googling. I thought I would post here and just sort of talk and see if anyone wanted to comment.

I am currently mostly wanting to do some home automation tasks with the pi and python.

1. If I want to use the pi to say, control some lights, act as a thermostat and turn a fan off and on, would this generally be best done with a separate program for each running simultaneously? Or is there a way to do this all in one giant program? I've read some about threading, but currently its above my head.

2. If I write a program to for example read a temp probe and return the value as x and have it running constantly in the background, is there a way I can reference my variable x in separate programs? For example could I have a script running that turns an LED off and on depending on the value of x, but in another script have x printed to the screen when condition z happens or something? I really don't know how to word this one to search for it.

3. As I understand programs are executed from top to bottom. If I have a condition that lets say I want to make an LED flash when true, how is this normally done?
Reply
#2
Quote:1. If I want to use the pi to say, control some lights, act as a thermostat and turn a fan off and on, would this generally be best done with a separate program for each running simultaneously? Or is there a way to do this all in one giant program? I've read some about threading, but currently its above my head.
If you are going to be turning on lights, etc, you will need some sort of power switcher, a relay for example,
or one of these: https://www.adafruit.com/product/3051
which will allow controlling devices remotely. These devices usually come with instructions on how to program.
You can also get some ideas here: https://hackaday.com/
and here: https://makezine.com/ there are many more sites another good one: https://forum.allaboutcircuits.com/

I think by spending time in some of these sites, you're other questions will be addressed just by the content of various projects.

Flashing an led is usually done by assigning some GPIO pins for that purpose

Quote:2. If I write a program to for example read a temp probe and return the value as x and have it running constantly in the background, is there a way I can reference my variable x in separate programs? For example could I have a script running that turns an LED off and on depending on the value of x, but in another script have x printed to the screen when condition z happens or something? I really don't know how to word this one to search for it.
this code is about reading sensors on pi: https://stackoverflow.com/questions/4203...-raspberry

Quote:3. As I understand programs are executed from top to bottom. If I have a condition that lets say I want to make an LED flash when true, how is this normally done?
this shows you how to do that: http://www.instructables.com/id/Blinking...erry-Pi-1/
Reply
#3
(Mar-12-2018, 01:31 AM)Larz60+ Wrote:
Quote:1. If I want to use the pi to say, control some lights, act as a thermostat and turn a fan off and on, would this generally be best done with a separate program for each running simultaneously? Or is there a way to do this all in one giant program? I've read some about threading, but currently its above my head.
If you are going to be turning on lights, etc, you will need some sort of power switcher, a relay for example,
or one of these: https://www.adafruit.com/product/3051
which will allow controlling devices remotely. These devices usually come with instructions on how to program.
You can also get some ideas here: https://hackaday.com/
and here: https://makezine.com/ there are many more sites another good one: https://forum.allaboutcircuits.com/

I think by spending time in some of these sites, you're other questions will be addressed just by the content of various projects.

Flashing an led is usually done by assigning some GPIO pins for that purpose

Quote:2. If I write a program to for example read a temp probe and return the value as x and have it running constantly in the background, is there a way I can reference my variable x in separate programs? For example could I have a script running that turns an LED off and on depending on the value of x, but in another script have x printed to the screen when condition z happens or something? I really don't know how to word this one to search for it.
this code is about reading sensors on pi: https://stackoverflow.com/questions/4203...-raspberry

Quote:3. As I understand programs are executed from top to bottom. If I have a condition that lets say I want to make an LED flash when true, how is this normally done?
this shows you how to do that: http://www.instructables.com/id/Blinking...erry-Pi-1/

Thanks for the reply.

As far as relays go, that part I am very familiar with. In this case I am using an opto isolator to interface with a line voltage relay. Ive done quite a bit of this type stuff in the past, its the code that I am so green on.


As for blinking an LED, I know how to simply blink one. Maybe a better way to phrase my question would be lets say I have 3 LEDs. I want one to blink once a second, one to blink twice a second, and one to flash 3 times and pause before repeating that pattern. Is this simple? I can't quite think of how I would do all of this at once.

I have read the sensors I have. That part is working. I am more abstractly asking if I can read the variables of one program in another as opposed to doing the same read sensor>do some math>set a variable process twice if I want to use the output in more than one place. I am thinking possibly "import" can be used but don't know enough yet.
Reply
#4
you could do it as a server, or you could communicate through an sql table.
I found examples of both.
(this is a web server, but I don't see why it can't be used wit localhost): https://wingoodharry.wordpress.com/2014/...on-script/
using sql as intermediate communications devise: http://raspberrywebserver.com/cgiscripti...ogger.html
Reply
#5
The Linux way is making the programs simple, short, doing one thing but very well. As you've probably already seen. And it is easier for maintenance. One program for one task. A big program for everything... I can imagine the mess after a period of time. Monolith programs are more time-consuming.

Python shares this philosophy: simple is better than complex.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020