python smbus/i2c frequency - python

I would like to change i2c bus frequency in order to allow for slightly longer cables.
I am using python-smbus package and it does work very well, however, I am unable to find how to set the bus frequency.
I have looked through the docs but was unable to find anything even remotely related to setting bus parameters.
Is that anything that could be done in python or do I need something lower level?
I am using Raspberry PI, which is an ARM architecture.

On the Raspberry Pi with the latest Jessie image, you can use this to check the current I2C frequency:
sudo cat /sys/module/i2c_bcm2708/parameters/baudrate.
To change the frequency, you can add/change this parameter:
dtparam=i2c_baudrate=50000
(replace 50000 with the desired frequency) in:
/boot/config.txt
and reboot to change the frequency.

You'll have to do something at a lower level. Typically this stuff is setup by the board file in the kernel. I didn't see anything specifically being done with the i2c, other than allocating the resources, so it's likely just using the default clock divisor. If you look on page 28 of the datasheet, you'll see that the default is 0x5dc. You'll need to setup that register with a different value (probably bigger) to cope with the longer cables.

I have now spent some significant amount of time researching all the options. It turns out that there are indeed low lever registers as specified in the other post, however, the Raspberry-Pi's driver resets their value on its every use, making any modification to them pretty much useless. The solution is to either write a custom i2c driver or simply wait for an updated version.
Some lower-level information could be found in byval forum.

Related

Get current network usage with Python

I'm writing a python script which consists of checking the current network usage of the computer, when downloading something. I've done a lot of research, and most of the things I find online are getting the MAX speed of the PC's NIC. In this case I want the current speed (like in mbps or something) only. The most promising solution I have come across yet is with the library "psutils". So the piece of code goes like this:
import psutil
download = psutil.net_io_counters(pernic=True)["Ethernet"][1]
print(download)
The output I get is '1392877555' which means it is definitely giving me something, but no matter what I have tried to alter this number, it is ALWAYS very close to this number. Only the last 3 digits vary. If I download something at my max speed my ISP allows me to, I get this number. If I reduce network usage to a minimum (I can monitor it in task manager for testing), I still get this number.
Any ideas why this is happening, or do I need to do something else with this data?
To get current network speed you can use speedtest-cli library. Using this library can give you the detailed info on your network speed and it's configurations. For more details you can refer to this article.

Capture image from a Mightex usb camera with opencv-python

I am trying to capture images from a Mightex cmos usb camera (SCE-B013-U) with Python 3.6.5 and opencv-python 3.4.3.18. The software came along confirms the camera works fine. But, cv2.VideoCapture(0).isOpened() is false. I am sure I am missing something, but I do not know what. Please help. Thanks a lot in advance.
For testing I have 1 camera installed on my setup that I know is opencv compatible with the command below:
print(cv2.VideoCapture(0).isOpened())
returns True for me, but if I unplug the camera, it returns False. False can imply that there is no camera available....
If the other answer (above) yields no result, perhaps the installed driver may not be compatible with opencv?
From the product description:
In addition, a user-friendly GUI based application software and an SDK
are provided for custom software development. A USB command set
protocol is also provided for non-Windows based application
Cannot help but to think that this company may be following a different standard to the one opencv typically uses. For example, many USB3Vision cameras will not work out of the box in the way you are attempting and require additional programming to return a NumPy style array image.
Additional support for my thinking is in the "EXAMPLE OF GRAPHICAL USER INTERFACE"
Many of the options I've seen in the GeniCam standard (USB3Vision).
Camera Mode (Continuous / Trigger), Exposure Control, Autoexposure Enable, etc
This is not to say that the camera is definitely is or is not one or the other, but the symptoms suggest the camera is not compliant to run out of the box using opencv in the way you want to.
You could try this 3rd party SDK instead to help verify.
I have no affiliation with them, but the ability to use their program for a (free) trial is very useful for helping to troubleshoot this issue.
Best of luck with getting your camera to work.
Maybe this will help, the official documentation states:
Sometimes, cap may not have initialized the capture. In that case,
this code shows error. You can check whether it is initialized or not
by the method cap.isOpened(). If it is True, OK. Otherwise open it
using cap.open().

How to read the status of the Power-LED on Raspberry Pi 3 with Python

How to read the status of the Power-LED (or the rainbow square - if you like that better) on Raspberry Pi 3 to detect an low-voltage-condition in a python script? Since the wiring of the Power-LED has changed since Raspberry Pi 2, it seems that GPIO 35 cannot longer be used for that purpose.
Update:
Since it seems to be non-trivial to detect a low-power-condition in code on Raspberry Pi 3, i solved it with a quick hardware hack. I soldered a wire between the Output of the APX803 (the power-monitoring device used on Pi 3) and GPIO26 and that way I can simply read GPIO26 to get the power status. Works like a charm.
Because of Pi3's BT/wifi support Power LED is controlled directly from the GPU through a GPIO expander.
I believe that there's no way to do what you want
I have dig into the topic. Found a very good discussion. In RPi 3 control of power LED from GPIO has been removed as OP mentioned.
The probable reason:
The power LED is different on Pi3. It is controlled from GPU through a
GPIO expander which is configured as an input. It may not be possible
to drive it from the arm (certainly it is not currently). Pi3's
BT/wifi support required a number of additional GPIOs compared to Pi2.
So, the ACT and PWR leds had to move to make that possible.
Also:
Since the B+ the functionality of PWR LED has doubled, power indicator
and as well as an under-voltage indicator. Since GPIOs are in short
supply, as well as being an indicator it is also the under-voltage
detector - there is some external circuitry that can detect the GPIO
direction so that it is either an input with an automatic LED or an
output. This circuitry is only present on the red LED. The ability to
change the "PWR" LED pin to an output, bypasses the under-voltage detection, which is permitted but
not recommended.
These GPIOs may never be available as fully
featured GPIOs because they are too slow to use in many circumstances.
There is a simple GPIO driver that can efficiently control the ACT
LED, but more effort is needed to make it work with the PWR LED and
change its direction to an output. This is something we may address,
but it isn't high on the list of priorities at the moment.
The possible wayout: Didn't find any way of using Python to access GPU, but found some discussions regarding accessing GPU configuration - sharing it as looking into it could be helpful for you in replicating an equivalent functionality in Python.
There is also the mailbox service that would allow userspace to fiddle
with the outputs on the expander, but not set direction - see
https://www.raspberrypi.org/forums/viewtopic.php?f=43&t=109137&start=100#p990152
and https://github.com/6by9/rpi3-gpiovirtbuf
Also:
For the complete list of GPIO allocations, look at the dt-blob.dts
that configures the GPU side -
https://github.com/raspberrypi/firmware/blob/master/extra/dt-blob.dts#L1175
Documentation is at
https://www.raspberrypi.org/documentation/configuration/pin-configuration.md
If I get something helpful 'll post it as update 2.

Control PWR Led on Raspberry 3

I read quite a few articels that say that the power led is now hardwired on
a raspy 3 device but some say it is somehow possible (but do not give specific
answers).
My question(s):
1.) is it possible to control the led via python and how.
2.) if not then can I permanently disable it?
The power LED is now also acting as a low-voltage indicator now. It is possible (however not trivial) to use it as an output but then the low-voltage indicaton is not working anymore. If I understand it correctly, the devs need to come up with a solution, so not much "normal" users can do for now.
There is an open issue on their bugtracker where you can get a bit more info: https://github.com/raspberrypi/linux/issues/1332
In general, as soon as the devs make it possible to control the power LED, there should be a file named /boot/overlays/pi3-pwr-led.dtbo. Currently, there isn't.
You can also have a look at one of the bare-metal environments and see if they have come up with a work-around.
https://github.com/rsta2/circle
https://github.com/vanvught/rpidmx512

Controlling the real-time clock module PCF8563 with python over i2c - understanding smbus

I am trying to control a real-time clock module PCF8563 with python. I connected the module to my Raspberry Pi and I am able to read the clock from the module. I would like to use the internal alarm functionality of the module and control this feature from python.
There are two Python libraries I know, one here and one here.
The first one works great but lacks the support for the alarm functionality. The second one cover most of the functions but rely on quick2wire library which I do not know and do not want to use. I actually would like to improve the first one.
I found also a good documented how-to related to this module, but sadly it is for Arduino.
My current problem is, how can I read and set specific bits from the bytes I get from the registries over the i2c bus. With:
import smbus
bus = smbus.SMBus(0)
returndata = bus.read_byte_data(0x51, 0x01)
I get one byte from registry 0x01. I need to translate the return vale from bcd to decimal, but how can I get the third bit of this byte ? And how can I change it.
Taken from the Arudino how-to:
... check bit 3 of the register at 0x01 (the “AF” alarm flag bit). If it’s 1 – it’s alarm time! Then you can turn the alarm off by setting that bit to zero. Using hardware, first set bit 1 of register 0x01 to 1 – then whenever an alarm occurs, current can flow into pin 3 of the PCF8563.
The short answer was: learning how to use the bit-masks and how bit-shifting works. The German Wikipedia site was quite helpful. After checking the data-sheet for the PCF8563 I was able to implement some of the missing functionality. For further progress see my GitHub repository.
I found probably an error in the Arduino related How-To, I noticed the author, see the comments under the linked article.

Categories

Resources