Setting up Adafruit PDM with Rapsberry Pi 4 - python

I am relatively new to working with Raspberry pi, Adafruit products and python coding. I have been working on setting up a Adafruit PDM Microphone connected to my RPi 4 and am attempting to running it on python 3. Adafruits tutorials have been amazing so far but I am having some issues getting all the required modules and libraries installed for this one. Is there a way to install Adafruit_zeroPDM and Adafruit_zeroDMA into python? I followed the github download links and tried several methods for installing them using pip3 however I am repeatedly getting errors about missing modules that should be in Adafruit's circuit python library. Is the Adafruit PDM microphone not compatible with RPi?
I have been using the following python code, which is directly taken form the adafruit website(https://learn.adafruit.com/adafruit-pdm-microphone-breakout/circuitpython):
import time
import array
import math
import board
import audiobusio
def mean(values):
return sum(values) /len(values)
def normalized_rms(values):
minbuf = int(mean(values))
samples_sum = sum(
float(sample - minbuf) * (sample - minbuf)
for sample in values
)
return math.sqrt(samples_sum / len(values))
#Main program
mic = audiobusio.PDMIn(board.TX, board.D12, sample_rate=16000, bit_depth=16)
samples = array.array('H', [0] * 160)
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print((magnitude,))
print("Decible Quired")
time.sleep(10)
I am unable to solve a library and module problem. I have downloaded Adafruit_circuitpython library and Adafruit_Blinka library as well as the required Adafruit_ZeroPDM and Adafruit_ZeroDMA however I keep getting the following error.
ModuleNotFoundError: No module named 'audiobusio'
Any help would be greatly appreciated.
Best

Related

Problems reading a .pcap file in Python using scapy

I'm trying to create a program where I have to read a pcap file and then count the number of packets related to some IPs. I'm not used to program in Python but I have to use it because I'm using it on a Raspberry Pi and depending of the output I have to control several pins.
Right now I have this, but I have an error and I donĀ“t know how to solve it.
from scapy.all import *
from scapy.utils import RawPcapReader
from scapy.layers.l2 import Ether
from scapy.layers.inet import IP, TCP
def read_pcap(name_pcap):
print("Opening", name_pcap)
client_1 = '192.168.4.4:48878'
server = '10.0.0.2:80'
(client_1_ip, client_1_port) = client_1.split(':')
(server_ip, server_port) = server.split(':')
counter = 0
for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap):
counter += 1
ether_pkt = Ether(pkt_data)
# Below here are functions to filter the data
read_pcap("captura.pcap")
And the error is this one:
NameError: name 'Packet' is not defined
The error apears to be in this (for(pkt_data, pkt_metadata,) in RawPcapReader(name_pcap):) line.
Someone knows how to solve it?
Thnak you :)
As Carcigenicate pointed out, that's a known bug. It's fixed in https://github.com/secdev/scapy/commit/ff644181d9bee35979a84671690d8cd1aa1971fa
You can use the development version (over https://scapy.readthedocs.io/en/latest/installation.html#current-development-version) in the meantime
Uninstall previous version & Install Latest version from https://pypi.org/project/scapy/
pip install scapy==2.5.0rc1
This should fix the error

Get CPU and GPU Temp using Python Windows

I was wondering if there was a way to get the CPU and the GPU temperature in python. I have already found a way for Linux (using psutil.sensors_temperature()), and I wanted to find a way for Windows.
A way to find the temperatures for Mac OS would also be appreciated, but I mainly want a way for windows.
I prefer to only use python modules, but DLL and C/C++ extensions are also completely acceptable!
When I try doing the below, I get None:
import wmi
w = wmi.WMI()
prin(w.Win32_TemperatureProbe()[0].CurrentReading)
When I try doing the below, I get an error:
import wmi
w = wmi.WMI(namespace="root\wmi")
temperature_info = w.MSAcpi_ThermalZoneTemperature()[0]
print(temperature_info.CurrentTemperature)
Error:
wmi.x_wmi: <x_wmi: Unexpected COM Error (-2147217396, 'OLE error 0x8004100c', None, None)>
I have heard of OpenHardwareMoniter, but this requires me to install something that is not a python module. I would also prefer to not have to run the script as admin to get the results.
I am also fine with running windows cmd commands with python, but I have not found one that returns the CPU temp.
Update: I found this: https://stackoverflow.com/a/58924992/13710015.
I can't figure out how to use it though.
When I tried doing: print(OUTPUT_temp._fields_), I got
[('Board Temp', <class 'ctypes.c_ulong'>), ('CPU Temp', <class 'ctypes.c_ulong'>), ('Board Temp2', <class 'ctypes.c_ulong'>), ('temp4', <class 'ctypes.c_ulong'>), ('temp5', <class 'ctypes.c_ulong'>)]
Note: I really do not want to run this as admin. If I absolutely have to, I can, but I prefer not to.
I think there doesn't have a directly way to achieve that. Some CPU producers wouldn't provide wmi to let your know the temperature directly.
You could use OpenHardwareMoniter.dll. Use the dynamic library.
Firstly, Download the OpenHardwareMoniter. It contains a file called OpenHardwareMonitorLib.dll (version 0.9.6, December 2020).
Install the module pythonnet:
pip install pythonnet
Below code works fine on my PC (Get the CPU temperature):
import clr # the pythonnet module.
clr.AddReference(r'YourdllPath')
# e.g. clr.AddReference(r'OpenHardwareMonitor/OpenHardwareMonitorLib'), without .dll
from OpenHardwareMonitor.Hardware import Computer
c = Computer()
c.CPUEnabled = True # get the Info about CPU
c.GPUEnabled = True # get the Info about GPU
c.Open()
while True:
for a in range(0, len(c.Hardware[0].Sensors)):
# print(c.Hardware[0].Sensors[a].Identifier)
if "/temperature" in str(c.Hardware[0].Sensors[a].Identifier):
print(c.Hardware[0].Sensors[a].get_Value())
c.Hardware[0].Update()
To Get the GPU temperature, change the c.Hardware[0] to c.Hardware[1].
Compare the result with :
Attention: If you want to get the CPU temperature, you need to run it as Administrator. If not, you will only get the value of Load. For GPU temperature, it can work without Admin permissions (as on Windows 10 21H1).
I did some changes from a Chinese Blog
I found a pretty good module for getting the temperature of NVIDIA GPUs.
pip install gputil
Code to print out temperature
import GPUtil
gpu = GPUtil.getGPUs()[0]
print(gpu.temperature)
I found it here
so you can use gpiozero to get the temp
first pip install gpiozero and from gpiozero import CPUTemperature to import it and cpu = CPUTemperature() print(cpu.temperature)
code:
from gpiozero import CPUTemperature
cpu = CPUTemperature()
print(cpu.temperature)
hope you enjoy. :)

append time difference values in a list

I am using Raspberry pi with python for a project and i am also a beginner to Python.
I have tried this code in Spyder IDE in windows and it was working fine but now i am facing the error for the same in raspberry pi inbuilt python IDE. I know that i made a small mistake somewhere but unfortunately, i could not find. Can someone please help me to rectify?
import time
import matplotlib.pyplot as plt
a = list(range(150))
t=[]
str_time = time.time()
for x in range(150):
time.sleep(0.001)
t.append((time.time() - str_time))
y= [x * 1000 for x in t]
plt.plot(y,a)
plt.show()
Error:

FTDI specific - libftd2xx.so is not loaded when using python wrapper to libMPSSE.so

I have installed the FTDI libftd2xx.so following the FTDI installation guide. I also has the libMPSSE.so downloaded. I am writing a python wrapper to use the I2C functions in the libMPSSE.so
However, I got the following error when trying to run my code:
../../TopLayer/I2C/src/ftdi_i2c.c:239:I2C_InitChannel(): NULL expression encountered
According to https://stackoverflow.com/a/22994703/8406938, looks like it cannot find the libftd2xx.so
Here is my python wrapper:
filepath = 'my path to the libMPSSE.so'
mpsse = CDLL(os.path.join(filepath, 'libMPSSE.so')
ChannelConf = ChannelConfig()
ChannelConf.clock_rate = 100000
ChannelConf.LatencyTimer = 3
ChannelConf.Options = 0
status = mpsse.I2C_InitChannel(c_void_p()), byref(ChannelConf))
The exact same code work in my Windows machine with libMPSSE.dll with the windows driver. Any suggestion how to solve this?

sending serial data to r305 biometric module interfacing with raspberry Pi using python 2.7

I had interfaced R305 biometric module with microcontrollers before using embedded C. But when I tried it using python, I am having error in sending hex array to it. Here is my code:
import serial
adrport = serial.Serial(port="/dev/tty0",baudrate=9600)
genimg = [0xEF,0x01,0xFF,0xFF,0xFF,0xFF,0x01,0x00,0x03,0x01,0x00,0x05]
also I tried it declaring like this:
genimg = "\xEF\x01\xFF\xFF\xFF\xFF\x01\x00\x03\x01\x00\x05"
I used to transfer the above mentioned array using the following function:
txd(genimg)
def txd(tx):
adrport.write(bytearray(tx))
Also I tried using
adrport.write(bytes(tx))
It doesnot show any errors to post the traceback, but the biometric module is not responding.
Okay, I changed the serail port to "/ttyAMA0" & now I can see the data flowing.But it also includes both "[,]" along with commas","; Can anybody help.?
Finally got the answer,defined array as
genimg = "\xEF\x01\xFF\xFF\xFF\xFF\x01\x00\x03\x01\x00\x05"
and used this.
adrport.write(bytes(tx))
Note: I tried this combination earlier also, but got loop iteration error because of using same name for both array and function. My bad,sorry everyone -;)

Categories