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 ----

  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()


--- 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()









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()


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()

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



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()


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