View: 35684|Reply: 25

python-periphery and 2G-IOT

[Copy link]

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
Published in 2017-6-20 20:55:34 | Show all floors |Read mode
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 ----

  1. from periphery import GPIO
  2. import time
  3. import signal
  4. import sys

  5. flag = True

  6. def handler(signal, frame):
  7.   global flag
  8.   print('handler')
  9.   flag = False


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

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

  15. while flag:
  16.   value = gpio_in.read()
  17.   print value
  18.   gpio_out.write(value)
  19.   time.sleep(1.0)


  20. gpio_out.write(False)
  21. gpio_in.close()
  22. gpio_out.close()
Copy code


--- led.py ---
  1. from periphery import LED
  2. import time
  3. import signal
  4. import sys

  5. flag = True

  6. def handler(signal, frame):
  7.   global flag
  8.   print('handler')
  9.   flag = False

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

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

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

  18. led0.close()
Copy code









10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
 Author| Published in 2017-9-7 20:56:31 | Show all floors
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 ---
  1. '''
  2. python-periphery hc595 sample

  3. 2G_IOT---HC595
  4. --------------
  5. Pin#29---SER(SerialDataInput)
  6. Pin#31---SRCLK
  7. Pin#33---RCLK
  8. ---------Qa----- LED1
  9. ---------Qb----- LED2
  10. ---------Qc----- LED3
  11. ---------Qd----- LED4
  12. ---------Qe----- LED5
  13. ---------Qf----- LED6
  14. ---------Qg----- LED7
  15. ---------Qh----- LED8
  16. 3.3V-----Vcc
  17. 3.3V-----BAR(SRCLR)
  18. GND------GND
  19. GND------BAR(OE)

  20. '''

  21. from periphery import GPIO
  22. import time
  23. import sys

  24. def hc595_write(data):
  25.   print 'hc595_write date=',data
  26.   mask=0x80
  27.   latch_out.write(False)
  28.   for x in range(0,8):
  29.     flag = data & mask
  30.     if (flag == 0):
  31.       data_out.write(False)
  32.     if (flag != 0):
  33.       data_out.write(True)
  34.     clock_out.write(True)
  35.     clock_out.write(False)
  36.     mask = mask >> 1
  37.   latch_out.write(True)

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

  41. data=0x01
  42. for x in range(0,8):
  43.   hc595_write(data)
  44.   data = data << 1
  45.   time.sleep(2)
  46.   
  47. data_out.close()
  48. clock_out.close()
  49. latch_out.close()
Copy code


10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
 Author| Published in 2017-9-7 21:34:52 | Show all floors
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 ---
  1. '''
  2. python-periphery mcp23017 sample

  3. 2G_IOT---MCP23017
  4. -----------------
  5. Pin#3----SDA
  6. Pin#5----SCK
  7. ---------GPA0--- LED1
  8. ---------GPA1--- LED2
  9. ---------GPA2--- LED3
  10. ---------GPA3--- LED4
  11. ---------GPA4--- LED5
  12. ---------GPA5--- LED6
  13. ---------GPA6--- LED7
  14. ---------GPA7--- LED8
  15. GND------A0
  16. GND------A1
  17. GND------A2
  18. 3.3V-----RESET
  19. 3.3V-----VDD
  20. GND------VSS
  21. '''

  22. from periphery import I2C
  23. import time
  24. import sys

  25. argv = sys.argv
  26. argc = len(argv)
  27. if (argc == 1):
  28.   device = "/dev/i2c-0"
  29. if (argc == 2 and int(argv[1]) == 0):
  30.   device = "/dev/i2c-0"
  31. if (argc == 2 and int(argv[1]) == 1):
  32.   device = "/dev/i2c-1"
  33. if (argc == 2 and int(argv[1]) == 2):
  34.   device = "/dev/i2c-2"

  35. print device
  36. # Open i2c-0 controller
  37. #i2c = I2C("/dev/i2c-0")
  38. i2c = I2C(device)

  39. # Write data to MCP23017
  40. msgs = [I2C.Message([0x00]), I2C.Message([0x00], read=False)]
  41. i2c.transfer(0x20, msgs)
  42. msgs = [I2C.Message([0x01]), I2C.Message([0x00], read=False)]
  43. i2c.transfer(0x20, msgs)
  44. msgs = [I2C.Message([0x12]), I2C.Message([0x00], read=False)]
  45. i2c.transfer(0x20, msgs)
  46. msgs = [I2C.Message([0x13]), I2C.Message([0x00], read=False)]
  47. i2c.transfer(0x20, msgs)

  48. byte=0
  49. for var in range(0,8):
  50.   byte=byte << 1
  51.   byte=byte+1
  52.   print "byte=",byte
  53.   msgs = [I2C.Message([0x12]), I2C.Message([byte], read=False)]
  54.   i2c.transfer(0x20, msgs)
  55.   time.sleep(1.0);

  56. for var in range(0,8):
  57.   byte=byte >> 1
  58.   print "byte=",byte
  59.   msgs = [I2C.Message([0x12]), I2C.Message([byte], read=False)]
  60.   i2c.transfer(0x20, msgs)
  61.   time.sleep(1.0);

  62. i2c.close()
Copy code

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



10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
 Author| Published in 2017-9-7 23:37:34 | Show all floors
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 ---
  1. '''
  2. python-periphery pcf8591 sample

  3. 2G_IOT---PCF8591
  4. -----------------
  5. Pin#3----SDA
  6. Pin#5----SCK
  7. ---------AIN0(Analog Channel 0)---3.3V
  8. ---------AIN1(Analog Channel 1)---1.65V
  9. ---------AIN2(Analog Channel 2)---0V
  10. GND------A0
  11. GND------A1
  12. GND------A2
  13. GND------EXT
  14. GND------AGND
  15. 3.3V-----Vref
  16. 3.3V-----VDD
  17. GND------VSS
  18. '''

  19. from periphery import I2C
  20. import time
  21. import signal
  22. import sys

  23. flag = True

  24. def handler(signal, frame):
  25.   global flag
  26.   print('handler')
  27.   flag = False

  28. argv = sys.argv
  29. argc = len(argv)
  30. if (argc == 1):
  31.   device = "/dev/i2c-0"
  32. if (argc == 2 and int(argv[1]) == 0):
  33.   device = "/dev/i2c-0"
  34. if (argc == 2 and int(argv[1]) == 1):
  35.   device = "/dev/i2c-1"
  36. if (argc == 2 and int(argv[1]) == 2):
  37.   device = "/dev/i2c-2"

  38. print device
  39. # Open i2c-0 controller
  40. #i2c = I2C("/dev/i2c-0")
  41. i2c = I2C(device)

  42. signal.signal(signal.SIGINT, handler)
  43. # Read data to PCF8591
  44. while flag:
  45.   msgs = [I2C.Message([0x40]), I2C.Message([0x00,0x00], read=True)]
  46.   i2c.transfer(0x48, msgs)
  47. #  print "value0=",msgs[1].data[0],msgs[1].data[1]
  48.   data = int(msgs[1].data[1])
  49.   volt0 = data * 3.3 / 255
  50. #  print "volt(Ch0)={0:.2f}".format(volt0)

  51.   msgs = [I2C.Message([0x41]), I2C.Message([0x00,0x00], read=True)]
  52.   i2c.transfer(0x48, msgs)
  53. #  print "value1=",msgs[1].data[0],msgs[1].data[1]
  54.   data = int(msgs[1].data[1])
  55.   volt1 = data * 3.3 / 255
  56. #  print "volt(Ch1)={0:.2f}".format(volt1)

  57.   msgs = [I2C.Message([0x42]), I2C.Message([0x00,0x00], read=True)]
  58.   i2c.transfer(0x48, msgs)
  59. #  print "value1=",msgs[1].data[0],msgs[1].data[1]
  60.   data = int(msgs[1].data[1])
  61.   volt2 = data * 3.3 / 255
  62. #  print "volt(Ch2)={0:.2f}".format(volt2)
  63.   print "volt(Ch0)={0:.2f} (Ch1)={1:.2f} (Ch2)={2:.2f}".format(volt0,volt1,volt2)

  64.   time.sleep(2)

  65. i2c.close()
Copy code


0

threads

2

posts

44

credits

Novice

Rank: 1

credits
44
Published in 2017-9-8 02:55:28 | Show all floors
Hi Nopnop2002, great work! I could test different pins cool.
Have tested ADC directly from OPi 2G-IOT, without using pcf8591.
Thanks
Rodri

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
 Author| Published in 2017-9-8 04:56:09 | Show all floors
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?

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
 Author| Published in 2017-9-8 10:48:39 | Show all floors
Edited by nopnop2002 at 2017-9-8 10:50

You can use 1602 LCD.

  1. '''
  2. python-periphery 1602 LCD sample

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

  5. '''
  6. #!/usr/bin/python
  7. #-*- encoding: utf-8 -*-
  8. #import
  9. from periphery import GPIO
  10. import time

  11. # Define GPIO to LCD mapping
  12. GPIO.LCD_RS = GPIO(101, "out")
  13. GPIO.LCD_E  = GPIO(121, "out")
  14. GPIO.LCD_D4 = GPIO(122, "out")
  15. GPIO.LCD_D5 = GPIO(123, "out")
  16. GPIO.LCD_D6 = GPIO(124, "out")
  17. GPIO.LCD_D7 = GPIO(125, "out")

  18. # Define some device constants
  19. LCD_WIDTH = 16    # Maximum characters per line
  20. LCD_CHR = True
  21. LCD_CMD = False

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

  24. # Timing constants
  25. E_PULSE = 0.0005
  26. E_DELAY = 0.0005

  27. def main():
  28.   # Main program block
  29. #  GPIO_setwarnings(False)
  30. #  GPIO_setmode(GPIO_BCM)       # Use BCM GPIO numbers
  31. #  GPIO_setup(LCD_E, GPIO_OUT)  # E
  32. #  GPIO_setup(LCD_RS, GPIO_OUT) # RS
  33. #  GPIO_setup(LCD_D4, GPIO_OUT) # DB4
  34. #  GPIO_setup(LCD_D5, GPIO_OUT) # DB5
  35. #  GPIO_setup(LCD_D6, GPIO_OUT) # DB6
  36. #  GPIO_setup(LCD_D7, GPIO_OUT) # DB7

  37.   # Initialise display
  38.   lcd_init()

  39.   while True:

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

  43.     time.sleep(3) # 3 second delay

  44.     # Send some text
  45.     lcd_string("1234567890123456",LCD_LINE_1)
  46.     lcd_string("abcdefghijklmnop",LCD_LINE_2)

  47.     time.sleep(3) # 3 second delay

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

  57. def lcd_byte(bits, mode):
  58.   # Send byte to data pins
  59.   # bits = data
  60.   # mode = True  for character
  61.   #        False for command

  62.   GPIO.LCD_RS.write(mode) # RS

  63.   # High bits
  64.   GPIO.LCD_D4.write(False)
  65.   GPIO.LCD_D5.write(False)
  66.   GPIO.LCD_D6.write(False)
  67.   GPIO.LCD_D7.write(False)
  68.   if bits&0x10==0x10:
  69.     GPIO.LCD_D4.write(True)
  70.   if bits&0x20==0x20:
  71.     GPIO.LCD_D5.write(True)
  72.   if bits&0x40==0x40:
  73.     GPIO.LCD_D6.write(True)
  74.   if bits&0x80==0x80:
  75.     GPIO.LCD_D7.write(True)

  76.   # Toggle 'Enable' pin
  77.   lcd_toggle_enable()

  78.   # Low bits
  79.   GPIO.LCD_D4.write(False)
  80.   GPIO.LCD_D5.write(False)
  81.   GPIO.LCD_D6.write(False)
  82.   GPIO.LCD_D7.write(False)
  83.   if bits&0x01==0x01:
  84.     GPIO.LCD_D4.write(True)
  85.   if bits&0x02==0x02:
  86.     GPIO.LCD_D5.write(True)
  87.   if bits&0x04==0x04:
  88.     GPIO.LCD_D6.write(True)
  89.   if bits&0x08==0x08:
  90.     GPIO.LCD_D7.write(True)

  91.   # Toggle 'Enable' pin
  92.   lcd_toggle_enable()

  93. def lcd_toggle_enable():
  94.   # Toggle enable
  95.   time.sleep(E_DELAY)
  96.   GPIO.LCD_E.write(True)
  97.   time.sleep(E_PULSE)
  98.   GPIO.LCD_E.write(False)
  99.   time.sleep(E_DELAY)

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

  102.   message = message.ljust(LCD_WIDTH," ")

  103.   lcd_byte(line, LCD_CMD)

  104.   for i in range(LCD_WIDTH):
  105.     lcd_byte(ord(message[i]),LCD_CHR)

  106. if __name__ == '__main__':

  107.   try:
  108.     main()
  109.   except KeyboardInterrupt:
  110.     pass
  111.   finally:
  112.     lcd_byte(0x01, LCD_CMD)
  113.     lcd_string("Goodbye!",LCD_LINE_1)
  114. #    GPIO_cleanup()
Copy code

This thread contains more resources

You need to Log in to download or view,No account?    Register

x

0

threads

2

posts

44

credits

Novice

Rank: 1

credits
44
Published in 2017-9-8 18:40:49 | Show all floors
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

This thread contains more resources

You need to Log in to download or view,No account?    Register

x

10

threads

218

posts

2040

credits

Gold member

Rank: 6Rank: 6

credits
2040
 Author| Published in 2017-9-8 22:02:05 | Show all floors
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.

6

threads

70

posts

370

credits

Intermediate member

Rank: 3Rank: 3

credits
370
Published in 2017-9-9 15:14:50 | Show all floors
Is there a way for work witch DHT22 sensor?

Thanks for your work
You need to log in before you can reply login | Register

Points Rule

Quick reply Top Return list