nopnop2002 post at 2017-6-20 20:55:34

python-periphery and 2G-IOT

Edited by nopnop2002 at 2017-9-7 21:45

I tried python-periphery using 2G-IOT.
https://github.com/vsergeev/python-periphery

$ env GIT_SSL_NO_VERIFY=true git clone https://github.com/vsergeev/python-periphery.git
$ cd python-periphery/
$ sudo python setup.py install

You can use these digital IO pin.

Pin#7:GPIO(56, "in/out")
Pin#16:GPIO(101,"in/out")
Pin#18:GPIO(121,"in/out")
Pin#19:GPIO(4, "in/out")
Pin#21:GPIO(3, "in/out")
Pin#23:GPIO(2, "in/out")
Pin#24:GPIO(5,"in/out")
Pin#26:GPIO(6,"in/out")
Pin#27:GPIO(1, "in/out")
Pin#28:GPIO(0,"in/out")
Pin#29:GPIO(122, "in/out")
Pin#31:GPIO(123, "in/out")
Pin#33:GPIO(124, "in/out")
Pin#35:GPIO(125, "in/out")
Pin#37:GPIO(126, "in/out")

Pinout is here.
http://www.orangepi.org/orangepi ... viewthread&tid=2818

GPIO & LED work fine.

--- gpio.py ----

from periphery import GPIO
import time
import signal
import sys

flag = True

def handler(signal, frame):
global flag
print('handler')
flag = False


signal.signal(signal.SIGINT, handler)
# Open GPIO 125 with input direction
gpio_in = GPIO(125, "in")

# Open GPIO 126 with output direction
gpio_out = GPIO(126, "out")

while flag:
value = gpio_in.read()
print value
gpio_out.write(value)
time.sleep(1.0)


gpio_out.write(False)
gpio_in.close()
gpio_out.close()

--- led.py ---
from periphery import LED
import time
import signal
import sys

flag = True

def handler(signal, frame):
global flag
print('handler')
flag = False

signal.signal(signal.SIGINT, handler)
# Open On Board LED "led0" with initial state off
led0 = LED("red-flash", False)

while flag:
led0.write(0)
time.sleep(1.0);

led0.write(1)
time.sleep(1.0);

led0.close()








nopnop2002 post at 2017-9-7 20:56:31

Edited by nopnop2002 at 2017-9-7 23:47

I tried python-periphery using 2G-IOT.
https://github.com/vsergeev/python-periphery

You can expand digital OUTPUT port to at most 64.
http://www.best-microcontroller-projects.com/74hc595.html

--- hc595.py ---'''
python-periphery hc595 sample

2G_IOT---HC595
--------------
Pin#29---SER(SerialDataInput)
Pin#31---SRCLK
Pin#33---RCLK
---------Qa----- LED1
---------Qb----- LED2
---------Qc----- LED3
---------Qd----- LED4
---------Qe----- LED5
---------Qf----- LED6
---------Qg----- LED7
---------Qh----- LED8
3.3V-----Vcc
3.3V-----BAR(SRCLR)
GND------GND
GND------BAR(OE)

'''

from periphery import GPIO
import time
import sys

def hc595_write(data):
print 'hc595_write date=',data
mask=0x80
latch_out.write(False)
for x in range(0,8):
    flag = data & mask
    if (flag == 0):
      data_out.write(False)
    if (flag != 0):
      data_out.write(True)
    clock_out.write(True)
    clock_out.write(False)
    mask = mask >> 1
latch_out.write(True)

data_out = GPIO(122, "out")#Pin#29
clock_out = GPIO(123, "out") #Pin#31
latch_out = GPIO(124, "out") #Pin#33

data=0x01
for x in range(0,8):
hc595_write(data)
data = data << 1
time.sleep(2)

data_out.close()
clock_out.close()
latch_out.close()

nopnop2002 post at 2017-9-7 21:34:52

Edited by nopnop2002 at 2017-9-15 06:51

I tried python-periphery using 2G-IOT.
https://github.com/vsergeev/python-periphery

You can expand digital INPUT/OUTPUT port to at most 16.
When using a cascade, it's possible to expand to at most 128 ports.

/dev/i2c-0 & /dev/i2c-2 work fine.
But /dev/i2c-1 don't work.
These pins(#27/#28) are assigned as GPIO.

--- mcp23017.py ---
'''
python-periphery mcp23017 sample

2G_IOT---MCP23017
-----------------
Pin#3----SDA
Pin#5----SCK
---------GPA0--- LED1
---------GPA1--- LED2
---------GPA2--- LED3
---------GPA3--- LED4
---------GPA4--- LED5
---------GPA5--- LED6
---------GPA6--- LED7
---------GPA7--- LED8
GND------A0
GND------A1
GND------A2
3.3V-----RESET
3.3V-----VDD
GND------VSS
'''

from periphery import I2C
import time
import sys

argv = sys.argv
argc = len(argv)
if (argc == 1):
device = "/dev/i2c-0"
if (argc == 2 and int(argv) == 0):
device = "/dev/i2c-0"
if (argc == 2 and int(argv) == 1):
device = "/dev/i2c-1"
if (argc == 2 and int(argv) == 2):
device = "/dev/i2c-2"

print device
# Open i2c-0 controller
#i2c = I2C("/dev/i2c-0")
i2c = I2C(device)

# Write data to MCP23017
msgs = ), I2C.Message(, read=False)]
i2c.transfer(0x20, msgs)
msgs = ), I2C.Message(, read=False)]
i2c.transfer(0x20, msgs)
msgs = ), I2C.Message(, read=False)]
i2c.transfer(0x20, msgs)
msgs = ), I2C.Message(, read=False)]
i2c.transfer(0x20, msgs)

byte=0
for var in range(0,8):
byte=byte << 1
byte=byte+1
print "byte=",byte
msgs = ), I2C.Message(, read=False)]
i2c.transfer(0x20, msgs)
time.sleep(1.0);

for var in range(0,8):
byte=byte >> 1
print "byte=",byte
msgs = ), I2C.Message(, read=False)]
i2c.transfer(0x20, msgs)
time.sleep(1.0);

i2c.close()
EDIT:I tried python smbus library.
It's work fine.
That's quite easier.



nopnop2002 post at 2017-9-7 23:37:34

Edited by nopnop2002 at 2017-9-7 23:41

I tried python-periphery using 2G-IOT.
https://github.com/vsergeev/python-periphery

You can read analog data using pcf8591.

--- pcf8591.py ---
'''
python-periphery pcf8591 sample

2G_IOT---PCF8591
-----------------
Pin#3----SDA
Pin#5----SCK
---------AIN0(Analog Channel 0)---3.3V
---------AIN1(Analog Channel 1)---1.65V
---------AIN2(Analog Channel 2)---0V
GND------A0
GND------A1
GND------A2
GND------EXT
GND------AGND
3.3V-----Vref
3.3V-----VDD
GND------VSS
'''

from periphery import I2C
import time
import signal
import sys

flag = True

def handler(signal, frame):
global flag
print('handler')
flag = False

argv = sys.argv
argc = len(argv)
if (argc == 1):
device = "/dev/i2c-0"
if (argc == 2 and int(argv) == 0):
device = "/dev/i2c-0"
if (argc == 2 and int(argv) == 1):
device = "/dev/i2c-1"
if (argc == 2 and int(argv) == 2):
device = "/dev/i2c-2"

print device
# Open i2c-0 controller
#i2c = I2C("/dev/i2c-0")
i2c = I2C(device)

signal.signal(signal.SIGINT, handler)
# Read data to PCF8591
while flag:
msgs = ), I2C.Message(, read=True)]
i2c.transfer(0x48, msgs)
#print "value0=",msgs.data,msgs.data
data = int(msgs.data)
volt0 = data * 3.3 / 255
#print "volt(Ch0)={0:.2f}".format(volt0)

msgs = ), I2C.Message(, read=True)]
i2c.transfer(0x48, msgs)
#print "value1=",msgs.data,msgs.data
data = int(msgs.data)
volt1 = data * 3.3 / 255
#print "volt(Ch1)={0:.2f}".format(volt1)

msgs = ), I2C.Message(, read=True)]
i2c.transfer(0x48, msgs)
#print "value1=",msgs.data,msgs.data
data = int(msgs.data)
volt2 = data * 3.3 / 255
#print "volt(Ch2)={0:.2f}".format(volt2)
print "volt(Ch0)={0:.2f} (Ch1)={1:.2f} (Ch2)={2:.2f}".format(volt0,volt1,volt2)

time.sleep(2)

i2c.close()

rodri16 post at 2017-9-8 02:55:28

Hi Nopnop2002, great work! I could test different pins cool.
Have tested ADC directly from OPi 2G-IOT, without using pcf8591.
Thanks
Rodri

nopnop2002 post at 2017-9-8 04:56:09

rodri16 replied at 2017-9-8 02:55
Hi Nopnop2002, great work! I could test different pins cool.
Have tested ADC directly from OPi 2G-IO ...

Does 2G-IOT have ADC?
Without using pcf8591, is it AD conversion possible?

nopnop2002 post at 2017-9-8 10:48:39

Edited by nopnop2002 at 2017-9-8 10:50

You can use 1602 LCD.

'''
python-periphery 1602 LCD sample

I ported from here
https://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/

'''
#!/usr/bin/python
#-*- encoding: utf-8 -*-
#import
from periphery import GPIO
import time

# Define GPIO to LCD mapping
GPIO.LCD_RS = GPIO(101, "out")
GPIO.LCD_E= GPIO(121, "out")
GPIO.LCD_D4 = GPIO(122, "out")
GPIO.LCD_D5 = GPIO(123, "out")
GPIO.LCD_D6 = GPIO(124, "out")
GPIO.LCD_D7 = GPIO(125, "out")

# Define some device constants
LCD_WIDTH = 16    # Maximum characters per line
LCD_CHR = True
LCD_CMD = False

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

def main():
# Main program block
#GPIO_setwarnings(False)
#GPIO_setmode(GPIO_BCM)       # Use BCM GPIO numbers
#GPIO_setup(LCD_E, GPIO_OUT)# E
#GPIO_setup(LCD_RS, GPIO_OUT) # RS
#GPIO_setup(LCD_D4, GPIO_OUT) # DB4
#GPIO_setup(LCD_D5, GPIO_OUT) # DB5
#GPIO_setup(LCD_D6, GPIO_OUT) # DB6
#GPIO_setup(LCD_D7, GPIO_OUT) # DB7

# Initialise display
lcd_init()

while True:

    # Send some test
    lcd_string("OrangePi 2G-IOT",LCD_LINE_1)
    lcd_string("16x2 LCD Test",LCD_LINE_2)

    time.sleep(3) # 3 second delay

    # Send some text
    lcd_string("1234567890123456",LCD_LINE_1)
    lcd_string("abcdefghijklmnop",LCD_LINE_2)

    time.sleep(3) # 3 second delay

def lcd_init():
# Initialise display
lcd_byte(0x33,LCD_CMD) # 110011 Initialise
lcd_byte(0x32,LCD_CMD) # 110010 Initialise
lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off
lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
lcd_byte(0x01,LCD_CMD) # 000001 Clear display
time.sleep(E_DELAY)

def lcd_byte(bits, mode):
# Send byte to data pins
# bits = data
# mode = Truefor character
#      False for command

GPIO.LCD_RS.write(mode) # RS

# High bits
GPIO.LCD_D4.write(False)
GPIO.LCD_D5.write(False)
GPIO.LCD_D6.write(False)
GPIO.LCD_D7.write(False)
if bits&0x10==0x10:
    GPIO.LCD_D4.write(True)
if bits&0x20==0x20:
    GPIO.LCD_D5.write(True)
if bits&0x40==0x40:
    GPIO.LCD_D6.write(True)
if bits&0x80==0x80:
    GPIO.LCD_D7.write(True)

# Toggle 'Enable' pin
lcd_toggle_enable()

# Low bits
GPIO.LCD_D4.write(False)
GPIO.LCD_D5.write(False)
GPIO.LCD_D6.write(False)
GPIO.LCD_D7.write(False)
if bits&0x01==0x01:
    GPIO.LCD_D4.write(True)
if bits&0x02==0x02:
    GPIO.LCD_D5.write(True)
if bits&0x04==0x04:
    GPIO.LCD_D6.write(True)
if bits&0x08==0x08:
    GPIO.LCD_D7.write(True)

# Toggle 'Enable' pin
lcd_toggle_enable()

def lcd_toggle_enable():
# Toggle enable
time.sleep(E_DELAY)
GPIO.LCD_E.write(True)
time.sleep(E_PULSE)
GPIO.LCD_E.write(False)
time.sleep(E_DELAY)

def lcd_string(message,line):
# Send string to display

message = message.ljust(LCD_WIDTH," ")

lcd_byte(line, LCD_CMD)

for i in range(LCD_WIDTH):
    lcd_byte(ord(message),LCD_CHR)

if __name__ == '__main__':

try:
    main()
except KeyboardInterrupt:
    pass
finally:
    lcd_byte(0x01, LCD_CMD)
    lcd_string("Goodbye!",LCD_LINE_1)
#    GPIO_cleanup()

rodri16 post at 2017-9-8 18:40:49

Well from schematics you can see 4 ADCs but I think they are not available on the header pinout

for Pi 2G-IOT schematic:
http://www.orangepi.org/download/2G-IOT-V1.4.pdf

nopnop2002 post at 2017-9-8 22:02:05

rodri16 replied at 2017-9-8 18:40
Well from schematics you can see 4 ADCs but I think they are not available on the header pinout

for ...

It's so hard for me to use ADC directly from OPi 2G-IOT.

surfero75 post at 2017-9-9 15:14:50

Is there a way for work witch DHT22 sensor?

Thanks for your work
page: [1] 2 3
View full version: python-periphery and 2G-IOT