Skip to content

tangowhisky37/RaspiPythonProjects

Repository files navigation

RaspiPythonProjects

This folder contains a list of all the Raspberry Pi projects I have been working on. For a listing of all these projects including detailed HowTo's for the Raspberry pi please visit - https://tangowhisky37.github.io/RasPiSetupGuide/. Feel free to drop the author an email with any feedback or input you might have.

  • This folder includes programs that uses the Raspberry Pi's GPIO pins to interact with multiple LED's
  • This folder also includes programs that use the Raspberry Pi's GPIO pins to interact with a simple push button
  • Components required for the programs in this folder include
    • 1 x Raspberry Pi 3 (I have used a 3, Model B. You can use whatever you have at your disposal.)
    • 3 x 50 Ohm Resistors
    • 3 x LED's
    • LED's connected to the following GPIO ports : led1 on GPIO17, led2 on GPIO27, led3 on GPIO22
    • 1 x Breadboard
    • 1 x Extension cable (Suggested) to extend the GPIO ports of the Raspberry Pi and bring them closed to the breadboard
    • 6 x Jumper cables - Female (connect to Rasberry Pi GPIO) to Male (Connect to the Breadboard)
    • 1 x Tactile Push button
    • 1 x Raspberry Pi Cobbler board (Recommended, to breakout the GPIO's) and make working easier
  • For circuit and connectivity details please refer to the images in the respective directories
  • For additional reading please visit
  • This project has evolved to Log data to CSV on the RaspberryPi and also upload data to the ThingSpeak/IoT Platform
  • Components required for the programs in this folder include
  • Python Adafruit DHT Library - https://github.com/adafruit/Adafruit_Python_DHT
  • Data directory to log captured data in CSV form in /opt/data.
    • The folder /opt/data will need to be created.
    • Permissions on the folder to be granted to user executing the program.
  • To log data at the ThingSpeak IoT platform, create an account at ThingSpeak (https://thingspeak.com).
    • This program uses the DHT11 and logs both Temperatury, Humidity
    • Create a new channel at Thingspeak. Create two new fields at ThingSpeak i.e. field1, field2
    • Copy your API key to the program and update the WriteDataThinkSpeak() function.
    • This program has now been updated to pull data from a local Arduino Mega 2560 over http. The Arduino has a DHT11 connected to it.
    • The Arduino also includes an Ethernet module based on the enc28j60 chip allowing it to connect over the network.
    • For details on connecting up the Arduino and configuring the ethernet module please refer to my contribution at ArduinoProjects here on Github.
  • While the Sense_Temp_Humidity project (above) was designed as a simple IoT (Internet Of Things) project to pull data from my DHT11 on the Raspberry Pi to be uploaded to ThingSpeak, Weather Reporting on the other hand pulls data from OpenWeatherMap and uploads the data to Thingspeak.
  • The objective of this project was to be able to compare the data for temperature, humidity being collected by my sensors at home with the data for the city i live in provided by Open Weather Map.
  • The project uses the Open Weather Map python library which you will need to download and install on the Raspberry Pi
  • This project has evolved to log data to CSV including upload of data to the ThingSpeak/IoT Platform
  • Components required include -
  • Python module for OWM from https://github.com/csparpa/pyowm
  • 1 x Raspberry Pi 3 (I have used a 3, Model B. You can use whatever you have at your disposal.
  • While the Sense_Temp_Humidity project (above) was designed as a simple IoT (Internet Of Things) project to pull data from my DHT11 on the Raspberry Pi to be uploaded to ThingSpeak, Weather Reporting on the other hand pulls data from OpenWeatherMap and uploads the data to Thingspeak. The Weather Reporting project (above) was also updated to log information to the local file system for purposes of further analysis.
  • The objective of this project was to obtain the last value of the temperature, humidity (inside the house, in the city) and display it to the screen.
  • The program builds upon another LCD library that was downloaded from the internet and is made available in the same folder
  • The code for obtaining the temperature, humidity and other information from the OWM service using python is described above. Please reference the previous project.
  • This project simply pulls the data from the log file and displays it to the screen refreshing it regularly.
  • Updates (030617) -
    • I ran an upgrade (dist-upgrade) to upgrade Raspbian to the latest version. For some reason i found an additional entry in the "/boot/config.txt" file called "dtparam=i2c=on1=on"
    • This entry was causing me a lot of grief and as a result the LCD wasn't being seen on the i2c bus by the Raspberry Pi 3.
    • I would recommend looking for this entry if you have issues finding your LCD on the i2c bus and disabling it.
    • Please note the other entry "dtparam=i2c_arm=on" is required.

IoT - Read Analog Sensors using MCP 3008 & Upload data (https://github.com/tangowhisky37/RaspiPythonProjects/tree/master/ReadingAnalogSensors)

  • This program reads analog sensor values using an MCP 3008. An MCP 3008 is required since the Raspberry Pi does not have an ADC or Analog to Digital converter and is not in a position to read analog signals by itself.
  • This project has evolved to Log data to CSV including upload of data to the ThingSpeak/IoT Platform
  • Components required include
    • 1 x Raspberry Pi 3 (I have used a 3, Model B. You can use whatever you have at your disposal.)
    • 1 x PIR Motion Sensor
    • 1 x Breadboard
    • 1 x Extension cable (Suggested) to extend the GPIO ports of the Raspberry Pi and bring them closed to the breadboard
    • 10 x Jumper cables - Female (connect to Rasberry Pi GPIO) to Male (Connect to the Breadboard)
    • 1 x Raspberry Pi Cobbler board (Recommended, to breakout the GPIO's) and make working easier
  • To setup the project you will need to -

Alexa on the Raspberry Pi 3

Mopidy (Audio Server) on the Raspberry Pi 3

  • Mopidy is an extensible music server that plays music from local disk, Spotify, SoundCloud, Google Play Music, and more. You edit the playlist from any phone, tablet, or computer using a range of MPD and web clients. Head off to https://www.mopidy.com to learn more about the Mopidy music server.
  • Let's step through the commands to download and install the base Mopidy player on your RaspberryPi
    • Download and install git on Raspbian using "sudo apt-get install git". This will install the git client on your RaspberryPi. You will need the git client to clone a lot of the repositories included in this tutorial.
    • Clone the github repository for Mopidy from https://github.com/mopidy/mopidy using "git clone https://github.com/mopidy/mopidy"
    • Change into downloaded Mopidy directory to build and install using the following commands, "sudo python setup.py install". This should build and install mopidy.
    • Clone the github repository for Mopidy-AlsaMixer from https://github.com/mopidy/mopidy-alsamixer using "git clone https://github.com/mopidy/mopidy-alsamixer"
    • Change into the downloaded mopidy-alsamixer directory to build and install using the following commands, "sudo python setup.py install". This should build and install mopidy-alsamixer.
  • Now that we have installed Mopidy we need to review some of the other non-documented dependencies we will need to install. If we had not installed Mopidy from source (github) and rather installed Mopidy using the Raspbian packages (sudo apt-get install mopidy) the system would have auto resolved the dependencies.
  • However, we've chosen not to install the stock packages since they are really outdated. We prefer the latest stable codebase from Github.
  • Lets proceed now and manually install the following packages. These are part of the pre-requisites which i found missing and spent sometime/effort understanding what was required. The commands are -
    • sudo apt-get install python-gst-1.0 gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-tools
    • sudo apt-get install gir1.2-gstreamer-1.0 gir1.2-gst-plugins-base-1.0
  • You will now need to open up your Mopidy configuration file at /etc/mopidy/mopidy.conf and edit it to suit your requirements. For details on each of the configuration options please visit - https://docs.mopidy.com/en/latest/config/
  • Please including configuration for the Mopidy AlsaMixer module into /etc/mopidy/mopidy.conf file. You can refer to a sample config at https://github.com/mopidy/mopidy-alsamixer
  • Now that we've got this far we'll need to add the local repository to the configuration. See your [local] configuration section and add the location path to your music store. This one needs to be configured with a simple local unix path.
  • Start up Mopidy using "sudo /path/to/mopidy --config /etc/mopidy/mopidy.conf" and you should review the errors on the screen. Work through the errors.
  • Best case outcome would be that the only error is that the local plugin configuration has not found any files. Run the following command to initiate creation of a local cache, "sudo /path/to/mopidy --config /etc/mopidy/mopidy.conf local scan". This should run for a while depending on how much content you've got in your local music repository. I use my usb drive mounted on /mnt/usb0 and it took a while for Mopidy to scan through the content.
  • Once you've setup mopidy you should confirm if you are able to connect to the User Interface. Start mopidy with the command, "sudo /path/to/mopidy --config /etc/mopidy/mopidy.conf". Then connect to the user interface via a web browser http://RaspberryPI_IP_Address_Here:6680/
  • If you have the musicbox client installed you will see a link for it at the above page. Else you'll need to clone the git repository for Mopidy Musicbox Web client using "git clone https://github.com/pimusicbox/mopidy-musicbox-webclient"
    • Change into the downloaded mopidy-musicbox directory to build and install using the following commands, "sudo python setup.py install". This will build and install the mopidy-musicbox package onto the RaspberryPi.
    • Once you've installed mopidy music box you should confirm if you are able to connect to the User Interface.
    • Start mopidy with the command, "sudo /path/to/mopidy --config /etc/mopidy/mopidy.conf".
    • Then connect to the user interface via a web browser http://RaspberryPI_IP_Address_Here:6680/musicbox_webclient/index.html
  • Check out these URL's if you are looking for Audio Streams to add to Musicbox
  • Obviously you might consider automating the startup of Mopidy. Like everything on unix/linux there's tons of ways of doing this. You could consider using daemon (daemontools) or simply /etc/rc.d/rc.local. Key in the startup command into rc.local and reboot the machine to find mopidy running in the background.
  • Enjoy listening to your music!!!

Hacking the Disk Layout on a Raspberry Pi Model A

  • Update (180517) -
    • I've been doing a bit of reading and realized that after all re-partitioning might not be really required to increase SWAP
    • On the Raspberry Pi one can edit /etc/dphys-swapfile and set the swap file size
    • Then initilize SWAP using "sudo dphys-swapfile setup"
    • Once initialization is complete start SWAP using "sudo dphys-swapfile swapon" and "sudo dphys-swapfile swapoff" to run off SWAP.
    • Some sites suggests setting up SWAP or large amounts of SWAP is not a good idea since it reduces the life of the SD CARD.
  • This guide was put together while i was assembling 4 Raspberry Pi's for the local Raspberry Pi Hackers group i run.
  • I've had some challenges building the Raspberry Pi A. I used the stock Raspbian distro on a 8 GB SDHC card. The default file system is around ~4GB in size with SWAP around ~128 MB in size.
  • The updates ran really slow and i couldn't get any software loaded on it. So I decided to re-do the partitions and throw in some additinoal swap space. The memory on the device was 128MB with SWAP as 128 MB created part of the default install.
  • The steps to extending the file system and creating a new one are as follows -
    • Backup your system in case of a mistake!
    • Use "fdisk /dev/mmcblk0" to view your partitions. Note down the start and end sectors for your main partition.
    • Make sure you have cross checked and double cross checked the sector start/end information.
    • use fdisk to delete the partition, but do not reboot.
    • Recreate a new partition (similar type i.e. ext4 Linux) but with a larger size starting at the same location as the previous one. I extended mine by another 2 GB to give me a total of 6 GB.
    • Create a new SWAP file system. I gave mine ~1GB of space.
    • Reboot to activate the partition changes. You should check "fdisk -l" to see the changes.
    • On rebooting use "resize2fs /dev/mmclk0p2" to enlarge the root Linux file system.
    • Some sites recommended using, "e2fsck -f /dev/mmcblk0p2" to perform a file system check. This however didn't work for me hence i used the next step.
    • If you perform the following, "touch /forcefsck" you will force an fsck at every boot. Just make sure you have the following entry in your /etc/fstab, "/dev/mmcblk0p2 / ext4 defaults, noatime 0 1"
    • Create the new SWAP file system by using, "mkswap /dev/mmcblk0p3".
    • Configure the /etc/fstab file with the following, "/dev/mmcblk0p3 swap swap defaults 0 0"
    • Reboot and you should see SWAP auto mounted.
    • Links to read -

Miscellaneous