Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't import usmbus SMBus #2

Open
neuberfran opened this issue Jun 4, 2019 · 16 comments
Open

I can't import usmbus SMBus #2

neuberfran opened this issue Jun 4, 2019 · 16 comments

Comments

@neuberfran
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

neuberfran commented Jun 5, 2019

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
Copy link
Author

Can you help me more

@geoffklee
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

neuberfran commented Sep 14, 2019

@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
Copy link
Owner

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
Copy link
Author

neuberfran commented Sep 17, 2019

@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
Copy link
Owner

geoffklee commented Sep 17, 2019

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
Copy link
Author

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

any PR will be welcome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants