Author Topic: Raspberry Pi and Python  (Read 1899 times)

Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Raspberry Pi and Python
« on: March 29, 2019, 10:46:23 AM »
I tried searching the forum for Raspberry Pi related info but found none.  About a month ago I started playing with a Pi, at first to learn Python programming. It wasn't long before a list of possible uses was staggering.  Alarms, weather monitoring, encryption (which started this new journey), and automated garden irrigation to mention a few.  I am still at my beginning stages, connecting sensors, recording their readings, and using outputs to control a relay. Soon, putting my little modules together will begin to make something useful.  Connecting a SDR to the Pi is another future project.  The list keeps growing!

Another project I'll eventually get to is an air gapped Pi dedicated to encryption.  So, are there any Pi users out there? 


Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Re: Raspberry Pi and Python
« Reply #1 on: March 29, 2019, 10:49:10 AM »
I don't know why, but a new search pulled up a number of Pi related posts I'll have to go through.

Offline Smurf Hunter

  • Survival Veteran
  • ********
  • Posts: 7172
  • Karma: 334
Re: Raspberry Pi and Python
« Reply #2 on: March 29, 2019, 04:10:02 PM »
Is there a particular problem you want to solve?

In general terms, a Raspi can do what a PC can do.  e.g. python would work the same in either case.
Are you running Linux (Ubuntu or similar) or something else?

If you get into technical questions, it's free to ask :D

Offline fritz_monroe

  • The Defenestrator
  • Administrator
  • Survival Veteran
  • *******
  • Posts: 8375
  • Karma: 151
    • The Homestead Fritz
Re: Raspberry Pi and Python
« Reply #3 on: March 29, 2019, 05:23:41 PM »
I have several Raspberry Pis around the house.  I build stuff that other people have already done, I don't have the time or expertise to build from scratch.

My network is mostly Ubiquiti's Unifi gear.  The way to manage it is to use Unifi Controller software.  I have this running on a RasPi.

I have a Raspberry Pi running PiHole.  It's basically a DNS server that blocks ads.

I have another running RasPlex, but since I've moved over to GoogleTV, it doesn't get much use.

Also have one running straight Raspian.  It will eventually run AcuParse.  This will have my weather station connected to it to send the data to Wunderground.

That's it at the moment.

I recommend listening to the Mini PC Podcast to get some ideas of what to do with them.

Offline surfivor

  • Survival Veteran
  • ********
  • Posts: 7124
  • Karma: 105
Re: Raspberry Pi and Python
« Reply #4 on: March 29, 2019, 07:09:07 PM »
 Python can also be used for big data analysis and machine learning

 Here are some books in PDF format on Raspberry Pi

http://www.stilson.net/documentation/raspberrypi/Raspberry%20Pi%20Cookbook.pdf

https://www.raspberrypi.org/magpi-issues/Beginners_Guide_v1.pdf

https://tentacle.net/~prophet/raspberrypi/Raspberry%20Pi%20Projects%20For%20Dummies.pdf

https://people.cs.clemson.edu/~yfeaste/cybersecurity/CPSC424/project/RaspberryPiTutorial.pdf


==================

other python books:

natural language processing with Python:

https://theswissbay.ch/pdf/Gentoomen%20Library/Programming/Python/Natural%20Language%20Processing%20with%20Python%20%282009%29.pdf

Big Data with Python:

https://tanthiamhuat.files.wordpress.com/2018/04/pythondatasciencehandbook.pdf


Machine learning with Python:

https://pythonizame.s3.amazonaws.com/media/Book/machine-learning-python-essential-techniques-predictive-analysis/file/008c0aac-9784-11e5-964d-04015fb6ba01.pdf


===================

Raspberry Pi is apparently designed as a learning tool

https://en.wikipedia.org/wiki/Raspberry_Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote teaching of basic computer science in schools and in developing countries


Offline fritz_monroe

  • The Defenestrator
  • Administrator
  • Survival Veteran
  • *******
  • Posts: 8375
  • Karma: 151
    • The Homestead Fritz
Re: Raspberry Pi and Python
« Reply #5 on: March 29, 2019, 07:19:40 PM »
Raspberry Pi is apparently designed as a learning tool

https://en.wikipedia.org/wiki/Raspberry_Pi

The Raspberry Pi is a series of small single-board computers developed in the United Kingdom by the Raspberry Pi Foundation to promote teaching of basic computer science in schools and in developing countries
Without a doubt, the Raspberry Pi was designed to be a learning tool.  To teach children (and their teachers) about how computers work.  It's been around for several years.

Offline surfivor

  • Survival Veteran
  • ********
  • Posts: 7124
  • Karma: 105
Re: Raspberry Pi and Python
« Reply #6 on: March 29, 2019, 11:57:11 PM »
Python coding examples

https://www.pythonforbeginners.com/code-snippets-source-code/python-code-examples

Python GUI examples and concepts using tkinter

https://www.google.com/amp/s/likegeeks.com/python-gui-examples-tkinter-tutorial/amp/

https://python-textbok.readthedocs.io/en/1.0/Introduction_to_GUI_Programming.html

Python versus ruby:

https://learn.onemonth.com/ruby-vs-python/

Tiobe index, a ranking of popular languages.
Python is ranked 3rd for March. I’ve always liked ruby but python is popular with beginners and is something to keep an eye on. I’m also a fan of JavaScript

https://www.tiobe.com/tiobe-index/



Django, web development framework for python

https://steelkiwi.com/blog/why-django-best-web-framework-your-project/

https://www.pythonforbeginners.com/learn-python/what-is-django/

« Last Edit: March 30, 2019, 12:15:45 AM by surfivor »

Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Re: Raspberry Pi and Python
« Reply #7 on: April 01, 2019, 09:22:55 AM »
Thanks, everyone for the replies.  I have more reading ahead.  My current issue is getting an Adafruit DHT22 temperature humidity sensor working.  I tried using some Python code I downloaded being sure to change the GPIO pin to the one I was using, and set for the correct sensor.  As usual trying to search for what was wrong I found conflicting information, so I tried a 'standard' GPIO pin, then used one that would accept serial data, powered it from 3, then 5 volts, all with no luck.  I suspected a bad sensor, so I tried a second one on a different Pi with no results. 

I know, I jumped into this too fast and am finding basic things I don't know yet.  Often I learn best that way but it can take a while.  I used to write stuff in Turbo Pascal so with Python I know what I want it to do, but don't always know how to get it done.  My company lets us use Lynda.com for free so I am going to start taking the Python courses- but I can't help playing in the mean time!

One of my 2 Pis is destined to live in the basement to monitor the temperature and humidity, check for water leaks, maybe control the dehumidifier.  At some point I do want the basement Pi to be able to send me a text or email warning but there are too many things to learn before I jump into something else. 

Offline Smurf Hunter

  • Survival Veteran
  • ********
  • Posts: 7172
  • Karma: 334
Re: Raspberry Pi and Python
« Reply #8 on: April 01, 2019, 04:21:06 PM »
My $0.02 (biased as a software engineer), I prefer a microcontroller like an Arduino for GPIO stuff like thermostats or actuating a relaying.
Sure, the C syntax is a little more cryptic than python, but you also don't have an entire linux OS to think about.  It's also $10 instead of $40, boots faster and is generally a little more durable as there's a whole let less involved. 

What's in the C-like Arduino program is the entire system (well mostly).

Where the RasPi shines is when you need to integrate with a network, and send telemetry to some cloud service.  So if you really need to track and potentially alert your humidity over time, I can see how having the TCP/IP stack built-in would be convenient. 

though I still think for round one, you might instead have a red and green LED.  Illuminate red when some "bad" sensor threshold is reached and green when the opposite condition is met.  Whatever you code for that will be a foundation for the integrated application that sends data over the wire.

Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Re: Raspberry Pi and Python
« Reply #9 on: April 02, 2019, 10:07:39 AM »
I forgot to mention I am using the Raspbian OS on the Pi.  Similar to looking into the Temp/ humidity sensor, when I started looking around for how to use the relay board there was conflicting information, a Pi can drive it direct, it will blow out the Pi... It turned out the relay I have has diodes for EMF spikes and opto isolators and could be driven directly.  Now that I know the relay can work I may replace it with LEDs for testing- a good idea. The relay board is is awkward carrying around and at least for now is not controlling anything.

Someone at work also mentioned the Arduino.  From bits and pieces I read looking for sensors it sounds like the Arduino had analog to digital converter(s) built in.  I assume an Arduino output can be fed into a Pi.  The A/D ability sounds interesting since at least for now I didn't want to get into a rat's nest of wires using a chip with the Pi.  Although I can see the use of plugin breadboards for prototype testing I could not see using one that was bigger than the Pi.  I bought pre-made jumpers but some of them did not get a tight grip on pins, especially sensors which seem to be smaller than the GPIO pins.  I went back to an old tried and true method (probably antiquated) from years ago, wire wrap.  Works like a charm plus I still had my wrap/ unwrap tools.

I downloaded the PDF links supplied (thanks for those) for documents and have to start reading through them.  I can see many hours of reading ahead, and probably learning another language in the process. 

Offline outoforder2day

  • Survivalist Mentor
  • *****
  • Posts: 399
  • Karma: 37
  • Semper Ubi Sub Ubi
    • The End Of The Tunnel
Re: Raspberry Pi and Python
« Reply #10 on: April 05, 2019, 07:59:09 AM »
Kudos on teaching yourself this stuff. The art of self-driven learning sets people apart, imho, and it's great to see folks getting into this here.

I'm a huge fan of the pi as a light-weight server for home use and learning. I have 3 at home running consul, vault, and DNSMasq. I initially did this as a prototype for work, but liked the setup so much that I decided to keep it. I wrote it up here: https://theendofthetunnel.org/2018/08/29/rebuilding-the-homesteads-dns-with-consul-dnsmasq-and-ansible/

As for your specific issues, I would recommend that you follow a good tutorial (adafruit has a lot) and get the exact parts from it. That way you can learn but have enough success a long the way so as not to get discouraged.

For sensor work long term, I'd also suggest you look at arduino. They're much simpler devices and don't need a full linux environment to run. They can send their data back via wireless (wifi and other types). I'm a huge fan of moteino's for this. They're arduino-compatible boards with RF transmitters built in. They're great for distributed sensor networks and don't rely on WiFi (which is complicated to implement on a chip like that). https://lowpowerlab.com/guide/moteino/

My plan, if I can ever make enough time, is to put moteino sensors all around the homestead, to monitor light, humidity, temperature, air quality, etc in places like my barn, coop, basement, etc. The moteinos would then send data to a Pi gateway that would graph it using something... I'm thinking influxDB and grafana, but am not sold on that stack yet.

Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Re: Raspberry Pi and Python
« Reply #11 on: April 09, 2019, 11:23:17 PM »
So far I have 2 Pis.  The original idea was to have each be a duplicate of the other, one running at home the other I could carry around to program, mostly while at work.  It has become too much of a problem keeping both in sync, plus at some point one will become a stay at home dedicated device.  I took whatever source code I had on both, merged them to a USB stick and eliminated all duplicate code.  Does anyone know if there would be an issue keeping the majority of code on an external USB stick and share that between the Pis?  I am guessing that any downloaded modules/ libraries are best kept residing on the Pis.

For the most part I've kept to sensors that seem to be the most commonly used versions, and decided not to buy any more stuff until I get what I have already working.  Of cause after I disconnected the temperature humidity sensor (DHT22) which I haven't got to work yet I realized that I never set the gpio pins for input/ output.  I did have all the interfaces enabled, so I am hoping that may be why I couldn't get any outputs from the sensor.  Tonight I did some reading on gpiozero and found out it has a lot of features built in and pretty much ready to go.  I also did some reading from the PDFs suggested and started learning some of he basic stuff I didn't know, like = vs ==.  Lots of reading to go!

Somewhere, hopefully not in the too distant future I would like to link up a handful of remote sensors wireless.  What seemed interesting was a number of devices would run off a single 18650 battery making them free of any power adapters.  The Arduino keeps popping up too, and after I get a good handle on the Pi I'll probably look into them.  I have Pi 3B+ which among other things I believe is the most power hungry.  Not the best choice to run off battery, although I have managed to get over 8 hours run time off a 4x18650 battery box without the Pi doing very much.  The battery box would make a nice battery backup in case power fails since it actually does function as a UPS by design.  It does give some voltage low warnings but the Pi ran just fine.

I took a quick look at End of the Tunnel and noticed Pi Hole mentioned at the end.  I've run across that mentioned online quite a few times.  Hopefully in the next couple days I'll get the DHT22 hooked up again and take another shot at getting it working.  That is one sensor I can put to good use and eventually have send warning notifications to my phone.  Does anyone have a Pi that resides outside?  I am curious what type of enclosure would be reasonably weather proof. 

Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Re: Raspberry Pi and Python
« Reply #12 on: April 17, 2019, 10:49:11 AM »
I think I found my problem with that sensor.  I disconnected it to try a microwave motion sensor and was unable to get it working.  I am pretty sure the GPIO mode (board vs bcm) was my problem.  Once I set the mode and referenced the gpio accordingly the sensor worked.  For now I have a microwave motion sensor connected and working.  I like it better than the PIR I was playing with.  The microwave does sense through doors and walls, plus it has a 360 degree field of view.

Is it possible to run 2 different Python scripts at the same time with one set for BOARD and the other BCM?  Would that cause a conflict or are the 2 scripts isolated?

Aside from going back to the temp/ humidity sensor I need to look into a permanent mounting scheme to hold the Pi, a relay board, and probably another sensor or 2.  I currently have a case that has a clear plastic top and bottom with standoffs holding everything together, and a built in fan.  I've thought of adding another set of standoffs and adding another level to the existing case, or maybe mounting the original setup on top of a box.  I have to check around to see what's out there, but if anyone has any suggestions it would be appreciated.

Offline creuzerm

  • Survivalist Mentor
  • *****
  • Posts: 422
  • Karma: 33
    • My Blog
Re: Raspberry Pi and Python
« Reply #13 on: April 17, 2019, 04:07:39 PM »
I am running half a dozen rasbperry pi zero w and a few more. Look into Node Red. Great stuff for automating things. Most of my automations 'talk' via my google home and alexa so when things are out of bounds, it verbally tells me I need to pay attention to my things.

I built an aquaponics monitor that monitors my ebb & flow bed for when the siphon stops working. It operates with 2 different sensors. An ultrasonic sensor and a pressure sensor in a pipe that watches for the rising water to slightly pressurize the air in the pipe.  It also feeds my fish on a timer or by the kids pushing a button. http://mike.creuzer.com/2018/02/sump-pump-aquaponics-aquarium-water-level-monitoring.html

I've built a sump pump monitor that works on the same principle as the aquaponics monitor so I know what my current sump pump cadence is. https://github.com/creuzerm/rasppi_node-red_Sump_Pump_Monitor

I have an indoor air quality sensor https://github.com/creuzerm/rasppi_node-red_Indoor_Environment_Monitor which surprised the daylights out of me how bad the air can get. I have this monitor control a HEPA air purifier by one of those wifi outlets. This one is evolving into a full on air quality monitor to use in conjunction with a weather station to create a system that rivals NOAA data.

I had a fridge go on the fritz, and bodged together a monitor out of a temp sensor and a raspberry pi to tell me when I needed to reset the temperature in the fridge. http://mike.creuzer.com/2018/03/a-raspberry-pi-post-for-pi-day.html

I've connected  a raspi to my shower head so I can turn off the shower on a timer to train myself and my kids towards water conservation. This one is fun as turning the light off will turn the water off. Great prankage potential.


my TODOs

I've dozens of long wired temperature probes and water moisture sensors that will get buried at various depths so I can do some analysis on ground temperature. Also my new pond on the property I bought last week.

Trying to get my lightning sensor to work correctly

Trying to get a dozen different air quality sensors to all work together. Than do the testing and analysis so I can isolate particular air contaminates because each sensor is sensitive to a few different chemicals and there is a lot of overlap so 4 sensors may be sensitive to the same thing.

Lots of cool things that can be done. I am starting learning MQTT so I can have things publish and subscribe to events and act independently. The goal would be silly things like if there is a window open on the windward side of the house and it will be about to rain, or I turned on the car and look to be leaving, or the AC/Heat just kicked on, let me know about the open window. Without really programming those behaviors explicitly. Just by letting a bunch of simple rules happen and getting complex behaviors out of them.

Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Re: Raspberry Pi and Python
« Reply #14 on: April 19, 2019, 09:47:24 AM »
That's really interesting, I took a quick look through your links.  I have the same ultrasonic sensor but haven't gotten to play with it yet- trying to get other things to work first.  Which wifi outlet did you use?  That is one thing I do have plans to use but haven't gotten around to yet.  Also, Node Red looks like a home automation kit I ran across being sold.  I was tempted to get their kit (can't recall the site selling it)  because it looked so easy to use, expand, and customize. 

I've been trying to get my existing stuff to work before adding more things to my to do list.  I think I sorted most of the issues to getting the pins/ gpio#s correctly.  From now on the 'pinout' command will see a lot of use.  I still can't help but search around and seeing all the sensors and gadgets available for the Pi. The latest thing I got working is a radar motion sensor.  The PIR sensor worked well, but the so called radar sensor senses 360 degrees and really does sense through doors and walls.  I can see that making a pretty effective intruder detector.  I still have to try to see how it works through an outside wall from the inside.  Another 'if I ever get to it project' is to use one of those outside to trigger a security camera.  If it works as I hope I can use it to trigger a security camera.  Now there are a lot of false triggers from car headlights and other things I don't care about.  The camera has connections for external triggers, and the sensor is pretty small and can work from inside a plastic case which will make it possible to close it into a weatherproof box.  Although I haven't tried, I believe the microwaves can be focused to exclude areas by shielding with aluminum foil.  So many projects, so little time.

I also need to look into the various models of Pi.  I have two  Pi3b+s now but using smaller, cheaper, less power hungry versions will be a plus in many uses.  I now see many things don't need all the features of the 3b+.  I believe a reduced size, 1 USB2 port version was recently released. 

Probably my first standalone working project will be used to monitor temperature, humidity, motion controlled lights, water leak detector, and CO in the basement.  I currently have a really old X10 setup automating the lights there, and I am surprised it is still working after all these years.  Another thing I've purpously avoided for now is getting the Pi to wireless connect with something- except the internet for updates etc.  I have a standalone LAN with a NAS connected that I can eventually experiment with.

Anyway, thanks for the links, I will have to look at them more closely.  I still want to use Python and learn it better- one of the reasons I started using the Pi, but in the mean time it looks like Node Red can get some projects off the drawing board.

Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Re: Raspberry Pi and Python
« Reply #15 on: May 07, 2019, 09:42:38 AM »
This is probably a simple issue to fix, but so far it has eluded me.  I wrote 2 small Python programs that I want to start automatically on boot.  I've tried using the cron task scheduler from the main menu (I am using a version 3b+ Raspberry Pi) and set it up to start on boot.  I use the full path,     /home/pi/Fan_Control.py     in the setup.

After saving, a window pops up saying it will run from the home directory.  I've tried putting my program in /home  and /home/pi and even created a 'cron scripts' directory but it will not start on boot.  I know there are other ways to auto start something, but using the main menu chron scheduler seems to be simplest- if I can get it to work. 

I starting running 2 projects under BOINC so the Pi is doing something productive while idle, a side effect being the CPU starts getting hot.  When I start the fan control program manually it keeps temperature under control nicely, but without the fan it heats up too much too fast.  Any ideas would be appreciated.

Offline Skunkeye

  • Dedicated Contributor
  • ******
  • Posts: 1609
  • Karma: 90
Re: Raspberry Pi and Python
« Reply #16 on: June 18, 2019, 12:48:10 AM »
The startup scripts on Raspbian are in /etc/init.d.  If you look at the scripts in there, you can find one that's pretty simple (fake-hwclock is pretty basic) to use as a template for a startup script for your fan control program.

I have a Pi with a relay board and some sensors that I use as a greenhouse controller/weather station.  Weather instruments are outside the greenhouse, plus I can check the greenhouse indoor temperature remotely via wifi, check if the fans are running, etc.  The relay board kicks the fans on and off based on temperature/humidity levels.  I plan on adding some intelligence so that the fan control takes into account the outside temperature (i.e., if it's below freezing, only ventilate in short bursts to avoid sucking in too much freezing air).  I have extra relays I'm not using now, which I plan to use to add automated watering at a later date.  I've had a lot of fun toying with the Pi and building the controller.

Offline Smurf Hunter

  • Survival Veteran
  • ********
  • Posts: 7172
  • Karma: 334
Re: Raspberry Pi and Python
« Reply #17 on: June 18, 2019, 03:43:55 PM »
A python programmer walks into a bar and orders a root beer.
Bartender explains they only sell root beer floats.
Programmer says "make it a double".

Offline Ralph

  • Senior Survivalist
  • ****
  • Posts: 242
  • Karma: 5
Re: Raspberry Pi and Python
« Reply #18 on: July 15, 2019, 07:32:24 PM »
Thanks for the autostart help,  I'll give it a try after I log out of here. 

I have added a Pi A1+ with a sense hat and have been playing around with the temperature, humidity, and barometer.  So far I just have the HAT scrolling values and changing colors of text and/ or background as values move in and out of set ranges and write them to a file.  I want this small script to autostart as well.

Playing around with the HAT's other sensors and watching the raw numbers change as I moved it around I had an idea to use all that data to generate 'random' numbers for encryption keys.  I put it on the seat of my car while driving home as an experiment.  Since I also capture the date and time for each set of readings no 2 lines will ever be the same, and even if someone were to drive the same path they could never duplicate the way I hit all the pot holes, bumps, steering corrections etc.  Hashing each line of data would further mix things up and output fixed length strings.  One more thing to add to the 'to do' list.