Zizico2
Posts: 11
Joined: Sun Nov 18, 2018 11:11 pm

Solved: GPIO Zero on PiCore

Tue Nov 20, 2018 10:56 pm

Hi guys. Since PiCore ships with MicroPython, will GPIO Zero work out of the box? (after installed, so not quite out of the box but you get it xd)
Last edited by Zizico2 on Wed Nov 21, 2018 11:08 am, edited 1 time in total.

User avatar
Gavinmc42
Posts: 4296
Joined: Wed Aug 28, 2013 3:31 am

Re: GPIO Zero on PiCore

Wed Nov 21, 2018 12:34 am

Micropython works fine, been using it for years on PiCore.
There is some old micropython bit banging result posted on the forum.
They may need testing on release 10.? http://tinycorelinux.net/10.x/
Try the PiCore forum for help, the guys there are pretty active.

Get PiCore working on a B+ first then swap SD card to Zero.
If you are using the Zero W then use the 3B as it has the same RF chip.

I have managed to USB boot a Zero without a SD card with PiCore but it did not load the tce extensions.
I imagine with some more fiddling around PiCore on Zero could boot further via the USB.
Not sure I would want to run the X11 Desktop that way ;)

Why do it? Well there is this Pi Cluster hat that takes 4 Zero's.
Normally you would need 5 SD cards, but if PiCore could boot via USB, you save 4 SD or 5 if you boot from a network :D

These days I use Ultibo which makes it even easier/faster to USB boot a Zero.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Zizico2
Posts: 11
Joined: Sun Nov 18, 2018 11:11 pm

Re: GPIO Zero on PiCore

Wed Nov 21, 2018 12:55 am

Gavinmc42 wrote:
Wed Nov 21, 2018 12:34 am
Micropython works fine, been using it for years on PiCore.
There is some old micropython bit banging result posted on the forum.
They may need testing on release 10.? http://tinycorelinux.net/10.x/
Try the PiCore forum for help, the guys there are pretty active.

Get PiCore working on a B+ first then swap SD card to Zero.
If you are using the Zero W then use the 3B as it has the same RF chip.

I have managed to USB boot a Zero without a SD card with PiCore but it did not load the tce extensions.
I imagine with some more fiddling around PiCore on Zero could boot further via the USB.
Not sure I would want to run the X11 Desktop that way ;)

Why do it? Well there is this Pi Cluster hat that takes 4 Zero's.
Normally you would need 5 SD cards, but if PiCore could boot via USB, you save 4 SD or 5 if you boot from a network :D

These days I use Ultibo which makes it even easier/faster to USB boot a Zero.
By GPIO Zero I meant this https://gpiozero.readthedocs.io/en/stable/ :)

User avatar
scruss
Posts: 2824
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: GPIO Zero on PiCore

Wed Nov 21, 2018 1:22 am

Zizico2 wrote:
Wed Nov 21, 2018 12:55 am
By GPIO Zero I meant this https://gpiozero.readthedocs.io/en/stable/ :)
GPIO Zero doesn't run on MicroPython - it uses full CPython 3.x
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.

User avatar
Gavinmc42
Posts: 4296
Joined: Wed Aug 28, 2013 3:31 am

Re: GPIO Zero on PiCore

Wed Nov 21, 2018 7:27 am

Oh, that GPIO Zero, mea culpa :oops:

This is how I used micropython in PiCore, but full Python can be installed too.

Code: Select all

fp = open('/sys/class/gpio/gpio24/value', 'w')

while True:
    fp.write('1\n')
    fp.write('0\n')
Another way is

Code: Select all

import _os as os

while True:
    os.system("echo 1 > /sys/class/gpio/gpio22/value")
    os.system("echo 0 > /sys/class/gpio/gpio22/value")
It was good enough for a single stepper motor controller a few years ago.
The control parameters are written by some shell script via webpage interface.
Most of the web page was shell script, only the stepper pulses needed to be micropython for speed.
I think I have also used Joan's pigpio daemon in PiCore.

Code: Select all

import _os as os
import activate as run
import stepconfig as config

print(run.onoff, config.Direction, config.Rate, config.Pulse, config.Steps, config.Pause, config.Maxmoves)

def wait(delay):
    for i in range(0, delay):
	i=i+1
	
trigger = open('/sys/class/gpio/gpio22/value', 'w')
step = open('/sys/class/gpio/gpio27/value', 'w')
direct = open('/sys/class/gpio/gpio18/value', 'w')
enable = open('/sys/class/gpio/gpio17/value', 'w')
#angle = open('/home/tc/steps', 'w')

totalsteps= 0
maxsteps = 0
triggerwait = 10000

#print("activated", activated.read())


def sleep():
	sleeptime = config.Pause
	os.system('sleep ' '%s' %(sleeptime) )

def stepping():
	global totalsteps
	stepcount = 0	
	while stepcount < config.Steps:
		step.write('1\n')
		wait(config.Pulse)
		step.write('0\n')
		stepcount = stepcount + 1
		wait(config.Rate)
	totalsteps = totalsteps + stepcount		
	angle = open('/home/tc/steps', 'w')
	angle.write('%s' %(totalsteps))
	angle.close()

def running():
    maxsteps = 0
    while maxsteps < config.Maxmoves:
		stepping()
		trigger.write('1\n')
		wait(triggerwait)
		trigger.write('0\n')
		sleep()
  
while True:
	if config.Direction == 1:
		direct.write('1\n')
	else:
		direct.write('0\n')
		
	activated = open('/home/tc/activate','r')
	run = activated.read(1)
    #print("activated", run)

	if  run == "1" :
		running()
		#print("running")
		#wait(1000000)
	else:
		wait(1000000)
		print("waiting")
A simpleweb.py using micropython, as micropython comes preinstalled with PiCore it is very useful.

Code: Select all

try:
    import usocket as socket
except:
    import socket


CONTENT = """\
HTTP/1.0 200 OK
Hello #{} from MicroPython!
"""

s = socket.socket()

ai = socket.getaddrinfo("10.36.34.49", 8080)
print("Bind address info:", ai)
addr = ai[0][4]

s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print("Listening, connect your browser to http://127.0.0.1:8080/")

counter = 0
while True:
    res = s.accept()
    client_s = res[0]
    client_addr = res[1]
    print("Client address:", client_addr)
    print("Client socket:", client_s)
    print("Request:")
    if 0:
        # MicroPython rawsocket module supports file interface directly
        print(client_s.read(4096))
        #print(client_s.readall())
        client_s.write(CONTENT.format(counter))
    else:
        print(client_s.recv(4096))
        client_s.send(bytes(CONTENT.format(counter), "ascii"))
    client_s.close()
    counter += 1

Not hard to use micropython in a similar style as GPIO Zero.
I think GPIO Zero came out after I had done all this stuff as most of this code is 2015?

Code: Select all

import _os as os
import sys

# red 24, green 22, blue 23
# 1 green, 2 blue, 3 red

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()
    
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Return to “General discussion”