Ideas for the project

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
raszga
Posts: 10
Joined: Sat Sep 10, 2016 2:06 pm

Re: Ideas for the project

Postby raszga » Wed Sep 14, 2016 2:10 am

Hello,
I'm able to read the MPU6050 thru I2C and funnel the data WIFI to a pyton program running on my Debian 8 station.
It works fine but I have a question regarding the use of the bytearray ..
When I program the scales for accelerometers and gyros I have to write a value in a memory location thru a bytearray..
I'm using the next code:
------------------------------------------------------------
buf=bytearray(b'\x00')
i2c.writeto_mem(address,PWR_MGMT_1,buf)
i2c.writeto_mem(address,FS_SEL,buf)
i2c.writeto_mem(address,AFS_SEL,buf)
------------------------------------------------------------
I suppose (hope) that b'\x00' means "0x00 but I really don't know how to write the equivalent of 1 or 2 ( integers)
ii is \x01 and \x02 or \x10 ?...

Help would be appreciated.
Thanks.

If it is interest for both sides of the programing I can publish the sources .. I don't want to use so much space in a post, so let me know.
Thanks

User avatar
pythoncoder
Posts: 1382
Joined: Fri Jul 18, 2014 8:01 am

Re: Ideas for the project

Postby pythoncoder » Wed Sep 14, 2016 6:00 am

The syntax b'\x00' creates a bytes instance containing a single byte of value 0. A bytes object is an immutable array of bytes, whereas a bytearray is mutable. There is no need to specify hex unless you want to:

Code: Select all

>>> b =bytearray(b'\0')
>>> len(b)
1
>>> b
bytearray(b'\x00')
>>> b =bytearray(b'\0\1\2\0xf0')
>>> len(b), b
(7, bytearray(b'\x00\x01\x02\x00xf0'))
>>>
If you're writing data out there's no need to convert it to a bytearray - you're not going to change the data - so you can write:

Code: Select all

i2c.writeto_mem(address,PWR_MGMT_1,b'\0')

Incidentally there is a driver for the MPU9150 here https://github.com/micropython-IMU/micropython-mpu9150.git. I believe the MPU6050 is very similar but lacks the magnetometer.
Peter Hinch

raszga
Posts: 10
Joined: Sat Sep 10, 2016 2:06 pm

Re: Ideas for the project

Postby raszga » Fri Sep 16, 2016 1:15 am

Thanks for the help and clarification.
to write "1" I'll write b'\1' if I understand correctly.

User avatar
pythoncoder
Posts: 1382
Joined: Fri Jul 18, 2014 8:01 am

Re: Ideas for the project

Postby pythoncoder » Fri Sep 16, 2016 6:49 am

Yes, or b'\x01', whichever you prefer.
Peter Hinch

raszga
Posts: 10
Joined: Sat Sep 10, 2016 2:06 pm

Re: Ideas for the project

Postby raszga » Thu Sep 29, 2016 12:27 am

Tanks for your responses.,

I try to find the instructions to load the mpy program in the ESP 8266-12 but I'm not too successful.

In simple terms the program loaded with ESPlorer works on ESP board but if I reset or cut the power the program is lost.

I do not understand how I can make the main.py to start after I write it on the ESP .

I guess i have a lot of missing info here ( I'm used with Microchip and PicKit 3 flashing the programs from a compiled Hex file.)

I would appreciate if you can direct me to some documentation which is readable in more simple terms and procedures...

Thanks,

raszga
Posts: 10
Joined: Sat Sep 10, 2016 2:06 pm

Re: Ideas for the project: how to write main.py

Postby raszga » Fri Sep 30, 2016 3:04 am

Hello,
I Think I found a solution to writing the main.py in the ESP 8266 12 (AI thinker) (I'm sure is not the best one but it worked)

I 'm using ESPlorer under Windows 7 .

(under debian 8 I din not have the boot.py)

1. Delete and re-flash your ESP. ( I used mp-esp8266firmware2016_25_2016.bin loaded with NODEMCU)
2. in ESPlorer start a new program in the left panel - call it loader.py if you want
3. create a multi line variable in which you put your program which you want to load in the main.py
4. for inside quotes use ' '
5. for control char you need to double the \ (see below)
6. The template would be:
a=''''
# you put your program here
import machine
print('Hell..ooo')
print('\\r\\n')
"""
f=open('main.py','w')
f.close()
f=open('main.py','a')
f.write(a)
f.close()
print(a)
# end of template

7. Run the program by pressing the SEND to ESP button
8. Reset the ESP
9. Print a will give you program listing how it is written in esp main.py
10. After you run the program the main.py should be present if you press the listdir button.
11. After reset main.py will start automatically.

Thanks in advance for comments.


Return to “Hardware Projects”

Who is online

Users browsing this forum: No registered users and 2 guests