geoffklee / micropython-smbus

A wrapper to provide methods of the CPython 'smbus' module on micropython
MIT License
24 stars 5 forks source link

I can't import usmbus SMBus #2

Open neuberfran opened 5 years ago

neuberfran commented 5 years ago

Hi,

I need transpose this project:https://github.com/neuberfran/OpenElectrons_i2c/blob/master/OpenElectrons_i2c.py

from python to micropython, but I have issue:

Captura de Tela 2019-06-04 às 11 04 55

geoffklee commented 5 years ago

Hi there,

It looks like your python interpreter just can't find the usmbus module: how did you go about installing it?

You should be able to see the list of places python is looking for modules with:

import sys
print(sys.path)

I don't have micropython handy but having the usmbus directory in the same directory as the main script, or in a subdirectory called lib usually works.

neuberfran commented 5 years ago

I did not give any installation commands, I thought they were already part of the micropython kernel. Do you look at the OpenEletronics_i2c.py module? Will I have to make many changes to the code to stop using your smbus? Do you think I'd better adapt OpenEletronics_i2c.py with the micropython library machine?

geoffklee commented 5 years ago

Hi,

There are different ways you could tackle this, but it would help to know what you're trying to do: is there a particular sensor you're trying to use?

neuberfran commented 5 years ago

first of all, thanks for the help.

this is the sensor in case: http://www.mindsensors.com/rpi/76-smartdrive-high-current-motor-controller the original driver is python.

I made a driver for it in java: https://github.com/neuberfran/SmartDrive/blob/master/SmartDrive/src/main/java/com/neuberfran/androidthings/driver/SmartDrive/SmartDrive.java for android things,

and thought of doing in c ++: https://forum.arduino.cc/index.php?topic=619218.new#new

would be specifically pro esp32, but I prefer micropython

neuberfran commented 5 years ago

Can you help me more

geoffklee commented 5 years ago

I think this module can help you here - all you need to do is install it alongside the OpenElectrons_i2c.py on your micropython board.

If you call it smbus rather than usmbus the code should run more or less without modification, I think (I'm by no means an expert). Depending on which pins you're using, you may need to modify the line that initialises the SMBus object:

https://github.com/neuberfran/OpenElectrons_i2c/blob/e993f3d05ff94a0b0cdb27564348a1933a3917ec/OpenElectrons_i2c.py#L60

I'm also not sure whether the ctypes module will behave exactly as expected, but we can cross that bridge when we come to it :-)

neuberfran commented 5 years ago

the main issue is still at the beginning. how to install your module so I can import it? Captura de Tela 2019-06-10 às 09 57 21

geoffklee commented 5 years ago

I’m not sure how your environment is set up, so unsure what to suggest. Are you using a python board of some kind? How are you getting the openelectrons code onto it? As a brute force method you could just copy the SMBus class into your file above the openelectrons class

neuberfran commented 5 years ago

I'm trying to use usmbus SMBus on an esp32 with 30 pin espressif running micropython.

I already rode OpenElectrons_i2c on (and run motors with i2c smartdrive-drive) a raspberry pi 3 and an intel edison, but these two platforms ran normal python and not micropython, so I could use the modules smbus (python normal) and ctypes.

You said:"As a brute force method you could just copy the SMBus class into your file above the openelectrons class"

How can I do this? (pls)

geoffklee commented 5 years ago

How are you getting the openelectrons file onto your esp32 board at the moment? Do you have it connected via USB? When I'm developing on an esp8266 I have it connected via USB, and use the micropython IDE extension: https://marketplace.visualstudio.com/items?itemName=dphans.micropython-ide-vscode

This allows you to copy files onto the board, so to use usmbus.SMBus you git clone this project, and copy the usmbus directory from this project into the /lib directory on your board.

geoffklee commented 5 years ago

You said:"As a brute force method you could just copy the SMBus class into your file above the openelectrons class" How can I do this? (pls)

Something like this is what I was imagining: https://gist.github.com/gkluoe/83b11673484c1a8a4e555607ad52c362

neuberfran commented 5 years ago

@gkluoe

hi

https://github.com/neuberfran/SmartMPython

I would appreciate it if you continued to help me solve this project and make engines move. I created the file main.py, but it does one only error Captura de Tela 2019-09-14 às 15 54 19

image

geoffklee commented 5 years ago

Hi - the problem in this picture, I think, is that you're using python 2 syntax, and micropython is (roughly) Python 3. In python 3, you need to use the print() function instead:

print("Batt: " + str(SmartDrive.getBattVoltage())

Should work. If you fix up all the print statements, hopefully that will help!

neuberfran commented 5 years ago

@gkluoe Tks I am evolving. While I bring more screenshots with updated issues:

Look:

(base) iMac:SmartMPython neuberfran$ ampy  --port /dev/tty.SLAB_USBtoUART ls
/OpenElectrons_i2c.py
/SmartDrive.py
/boot.py

https://github.com/neuberfran/SmartMPython/blob/master/OpenElectrons_i2c.py

Captura de Tela 2019-09-16 às 21 42 20

Captura de Tela 2019-09-16 às 21 42 40

geoffklee commented 5 years ago

Hmm, OK, now I think your issue is here:

https://github.com/neuberfran/SmartMPython/blob/c1fcb3629c27d172440e82ddabfc3004a1555523/OpenElectrons_i2c.py#L3

The ctypes module doesn't exist on micropython. There is a uctypes module, but as far as I can tell, it doesn't offer the .c_int() and .c_long() functions that this code is using.

You could re-implement these functions - in theory that's quite simple - all they do is convert a python int or long into c-style signed number, but this is getting a bit over my head!

To be honest, at this point it's probably going to be easier to just rewrite the OpenElectrons_i2c.py module to work natively in micropython. I'd love to help with that but without your hardware to test on, I don't think I can.

neuberfran commented 5 years ago

https://github.com/neuberfran/SmartMPython/invitations

any PR will be welcome