Help Read GPIO pin in Python

Hello everyone. I have been attempting to read the analog signal from the GPIO pin of the MetamotionR board. I am using the pymetawear library on a raspberry pi.

This is the sesnor I am attempting to read: https://www.sparkfun.com/products/12650?_ga=2.112552772.1984315370.1584943763-165751714.1579836749

Reading the sesnor from the GPIO pin with the MetaWear android app does work. However when I try to test it in python I am stuck.

I have been modifying the gpio.py script in the pymetawear modules folder. Here is my code:

https://github.com/booman171/gpio/blob/master/gpio.py

Note, this file is a copy of the the gpio.py file from the modules folder within a clone of the pymetawear library, in which I am editing. I have been playing around with the code around line 100 but nothing seems to work.

There error message is as follows:

Traceback (most recent call last):
  File "gpio.py", line 217, in <module>
    gp.notifications(handle_gpio_notification)
  File "gpio.py", line 197, in notifications
    super(GpioModule, self).notifications(data_handler(callback))
  File "/usr/local/lib/python3.7/dist-packages/pymetawear/modules/base.py", line 135, in notifications
    data_signal = self.data_signal
  File "gpio.py", line 100, in data_signal
    return libmetawear.mbl_mw_gpio_get_pin_monitor_data_signal(self.board, '0')
ctypes.ArgumentError: argument 2: <class 'TypeError'>: wrong type

Any help is welcome.

Comments

  • I have been looking through that example, thank you. I managed to get passed my initial issue, but still can't access the signal.

    This is my code:

    # usage: python stream_acc.py [mac1] [mac2] ... [mac(n)]
    from __future__ import print_function
    from mbientlab.metawear import MetaWear, libmetawear, parse_value
    from mbientlab.metawear.cbindings import *
    from time import sleep
    from threading import Event
    from ctypes import byref, create_string_buffer
    import platform
    import sys
    
    if sys.version_info[0] == 2:
        range = xrange
    
    class State:
        def __init__(self, device):
            self.device = device
            self.samples = 0
            self.callback = FnVoid_VoidP_DataP(self.data_handler)
    
        def data_handler(self, ctx, data):
            print("%s -> %s" % (self.device.address, parse_value(data)))
            self.samples+= 1
    
    states = []
    d = MetaWear("CC:3E:36:3A:4B:50")
    d.connect()
    print("Connected to " + d.address)
    states.append(State(d))
    
    for s in states:
        print("Configuring device")
        libmetawear.mbl_mw_settings_set_connection_parameters(s.device.board, 7.5, 7.5, 0, 6000)
        sleep(1.5)
        an_signal = libmetawear.mbl_mw_gpio_get_analog_input_data_signal(s.device.board, 2, GpioAnalogReadMode.ADC)
        libmetawear.mbl_mw_datasignal_subscribe(an_signal, None, s.callback)
        libmetawear.mbl_mw_gpio_start_pin_monitoring(s.device.board, 2)
        libmetawear.mbl_mw_datasignal_read(an_signal)
    
    sleep(15.0)
    
    for s in states:
        libmetawear.mbl_mw_gpio_stop_pin_monitoring(s.device.board, 2)
        an_signal = libmetawear.mbl_mw_gpio_get_analog_input_data_signal(s.device.board, 2, GpioAnalogReadMode.ADC)
        libmetawear.mbl_mw_datasignal_unsubscribe(an_signal)
        libmetawear.mbl_mw_debug_disconnect(s.device.board)
    
    print("Total Samples Received")
    for s in states:
        print("%s -> %d" % (s.device.address, s.samples))
    
    

    The output is:

    Connected
    Services disconvered
    Characteristics discovered
    Descriptors found
    Connected to CC:3E:36:3A:4B:50
    Configuring device
    CC:3E:36:3A:4B:50 -> 401
    Total Samples Received
    CC:3E:36:3A:4B:50 -> 1
    

    There should be some output here. Any thoughts?

  • This looks all correct. You only read the signal once so you got one sample. Please read our tutorials, some signal need timers to be read more than once:
    https://mbientlab.com/tutorials/CPPDevelopment.html#reading

Sign In or Register to comment.