WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: Micropython + GPIO  (Read 12989 times)

Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Micropython + GPIO
« on: August 31, 2015, 12:40:33 AM »
Been testing the new 7 releases which have Micropython built in.

Was trying to turn GPIO leds on/off, found one solution that works.
Thought someone might be interested.
You also need to make the GPIO class values outputs and writable.
I do this with a separate shell script on startup.

With the ESA taking an interest in Micropython and a Astro RPi + hat going into space maybe they will get the i2c/SPI/GPIO in Micropython working for the Raspberry Pi.


import _os as os
import sys

# red 24, green 22, blue 23

os.system("date")

def led22on():
    os.system("echo 1 > /sys/class/gpio/gpio22/value")
   
def led22off():
    os.system("echo 0 > /sys/class/gpio/gpio22/value")
   
def led23on():
    os.system("echo 1 > /sys/class/gpio/gpio23/value")
   
def led23off():
    os.system("echo 0 > /sys/class/gpio/gpio23/value")
   
def led24on():
    os.system("echo 1 > /sys/class/gpio/gpio24/value")
   
def led24off():
    os.system("echo 0 > /sys/class/gpio/gpio24/value")

led22off()
led23off()
led24off()

led24on()






Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Re: Micropython + GPIO
« Reply #1 on: August 31, 2015, 06:14:01 PM »
Scripting i2c etc.

Been looking at other scripting languages, Lua and Micropython to access GPIO, i2c
i2c requires i2c-tools.tcz installed.
GPIO needs /sys/class/gpio/gpioxx/value to be writable.

Lua
dump = os.execute("sudo i2cdump -y 1 0x08")
print(dump)

Python
dump = os.system("sudo i2cdump -y 1 0x08")
print(dump)

i2cget, i2cset work fine to read/write i2c.

It will be interesting to compare Busybox Shell, Lua, Micropython and Luajit to see how fast the GPIO pins can toggle.




Offline Greg Erskine

  • Sr. Member
  • ****
  • Posts: 402
Re: Micropython + GPIO
« Reply #2 on: September 02, 2015, 12:15:22 AM »
hi gavinmc42,

I am following this thread so keep posting information.

http://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/

regards
Greg

Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Re: Micropython + GPIO
« Reply #3 on: September 03, 2015, 09:11:26 PM »
OK Shell is faster than Micropython or Lua calling system commands

Shell script

while true
do
    echo 1 > /sys/class/gpio/gpio22/value
    echo 0 > /sys/class/gpio/gpio22/value
done


Micropython

import _os as os

while True:
    os.system("echo 1 > /sys/class/gpio/gpio22/value")
    os.system("echo 0 > /sys/class/gpio/gpio22/value")


Lua

while true
do
    os.execute("echo 1 > /sys/class/gpio/gpio22/value")
    os.execute("echo 0 > /sys/class/gpio/gpio22/value")
end


Shell 2KHz, Python 42Hz, Lua 42Hz - 50 times slower.

Hey but at least it worked:)
If you only want to read and log a temperature every 5 minutes this is no issue.
Native hardware access to GPIO, I2C, SPI in Micropython, Lua will be better?

Shell is kind of hard to read when you get to a few hundred lines.
Calling Sed and Awk could be slow:)

Hmm call the shell scripts from Lua?
os.execute("./flashled.sh")   no surprise gives 2KHz

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Micropython + GPIO
« Reply #4 on: September 03, 2015, 10:18:33 PM »
In the shell version you are using file operations only, while in Python and Lua calling another program with shell to do the same. Equivalent solution in Python:


Code: [Select]
while True:
    fp = open('/sys/class/gpio/gpio22/value', 'w')
    fp.write('1\n')
    fp.close()

    fp = open('/sys/class/gpio/gpio22/value', 'w')
    fp.write('0\n')
    fp.close()


   
« Last Edit: September 03, 2015, 10:20:34 PM by bmarkus »
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Re: Micropython + GPIO
« Reply #5 on: September 04, 2015, 01:30:04 AM »
Thanks Bela,

That gives 2.8KHz, a bit faster than Shell.
Lua is 1.3KHz

Might try more file I/O coding.
So much still to learn.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Micropython + GPIO
« Reply #6 on: September 04, 2015, 01:55:44 AM »
Can you try the Python code with the normal 2.7 version instead of Micropython?
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Re: Micropython + GPIO
« Reply #7 on: September 04, 2015, 02:14:25 AM »
Will have to install the real python 1st.
The point was to try to do everything with the standard SSH piCore to keep the OS size down.
Lua is not to bad at 128KB

With Shell, Micropython and Lua I have three scripting languages I can use without bloating the OS.
Python is 9MB + libs, may as well install Go 29MB and learn another language:)
Most of my stuff is sensor logging with web serving the data via busybox-httpd.
RPi is over kill so I have spare cpu %.

Maintaining OS's is a pain, backing up Raspbian or Ubuntu Mate is 8GB at a time.
I try to fit everything on my smallest SD cards, 128MB, quick, easy to backup.
That's why I love piCore.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Micropython + GPIO
« Reply #8 on: September 04, 2015, 02:30:46 AM »
If you get some time, please try Python just to see the performance.

GO creates static executables so expect larger applications, which are running nearly as fast as native C code.

You can try LUAJIT too, which significantly speeds up execution of LUA scripts. Just for testing.


Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline curaga

  • Administrator
  • Hero Member
  • *****
  • Posts: 10957
Re: Micropython + GPIO
« Reply #9 on: September 04, 2015, 02:31:28 AM »
The latest Go released just recently finally supports shared executables. Also gccgo supported them for a while.
The only barriers that can stop you are the ones you create yourself.

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Micropython + GPIO
« Reply #10 on: September 04, 2015, 06:54:44 AM »
gavinmc42:

Are you testing on RPi or RPi2?
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Micropython + GPIO
« Reply #11 on: September 04, 2015, 06:56:38 AM »
The latest Go released just recently finally supports shared executables. Also gccgo supported them for a while.

Thanks, I haven't recognized the new 1.5 which is a major new release with interesting changes. However at first and fast reading of  change list have seen nothing about shared libs.
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline bmarkus

  • Administrator
  • Hero Member
  • *****
  • Posts: 7183
    • My Community Forum
Re: Micropython + GPIO
« Reply #12 on: September 04, 2015, 09:02:40 AM »
Go 1.5 build fails on Raspberry Pi and x86. Maybe better to wait for 1.5.2 or so...
Béla
Ham Radio callsign: HA5DI

"Amateur Radio: The First Technology-Based Social Network."

Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Re: Micropython + GPIO
« Reply #13 on: September 06, 2015, 06:35:41 PM »
Hi Bela,

Using up stock of my old Model B's.
Latest Luajit and Go not working any more, think they may already be compiled for ARM7, Pi2.
Get an ARM6 expecting ARM7 warning.
Come to think of it maybe I only tested Go on the Pi 2.
Only reason for learning Go is the multicore capability, no need on old Pi's.

Will try the real python.tcz, stay tuned.


Offline gavinmc42

  • Sr. Member
  • ****
  • Posts: 301
Re: Micropython + GPIO
« Reply #14 on: September 06, 2015, 07:10:38 PM »
Bela,

Using your file method, python 2.7 flashes the led at 980Hz, Micropython does 2.8KHz, three times faster.
Maybe because of all the bloat stuff the normal python loads.

Just remembered GoBot, runs GO so it must work on old models.
http://gobot.io/
Uses version 1.4?
Made one attempt to install it and failed, will wait it someone smarter or with more time figures it out.
Wanted to make some bots for the kid, teach him Go at the same time.