6.4 WuClass Examples for Grove Modules

Having introduced the template of writing a WuClass in Python, we will show several WuClass examples so that you can use and expand them according to your needs.

We use a sensor kit called Grove modules (http://www.seeedstudio.com/wiki/Grove_System). These modules can be assembled easily on Edison, Galileo and Raspberry Pi using individual shields. Also, they provide abundant sensors, actuators and libraries for IoT developers.

For more information of Grove starter kit, please refer to the following websites:
Using Intel Edison or Galileo: https://software.intel.com/iot/hardware/devkit
Using Raspberry Pi: http://www.dexterindustries.com/grovepi/

IO Interface

As we mentioned in the last section, in order to be compatible to all libraries of Edison, Galileo and Raspberry Pi, we have added an io interface to bridge individual library. For Edison and Galileo, we have to import mraa library; for Raspberry Pi, we have to import rpi or grovepi library. To achieve this, we add a device_type definition on line 12 of udpwkpf_io_interface.py. Currently, there are three options: DEVICE_TYPE_MRAA, DEVICE_TYPE_GPI and DEVICE_TYPE_RPI. You need to configure this definition before running a WuClass of Grove modules on the IoT board.

GPIO USAGE

After specifying which library we want to import for the WuClass, we also use this type definition to manage how the code uses the gpio function. For the time being, we have defined six common functions for gpio usage. These functions are as follows.

pin_mode(pin, pin_type, pin_mode, **kwargs)  
# this function declares whether the pin is digital/analog, input/output, etc.

digital_read(pin_obj)   
# if  pin_obj is digital and input,  we can use this to retrieve data from gpio

digital_write(pin_obj, val)   
# the usage is opposite to digital_read. 

analog_read(pin_obj)  
# since rpi library doesn't support anaglog reading, 
# we can only use this function when we import mraa or grovepi library. 

analog_write(pin_obj, val)
# only grovepi supports analog writing, 
# so we cannot use this function when we import mraa or rpi library. 

temp_read(pin_obj)  
# this function is specific for reading value from temperature sensor 
# of Grove starter kit.

We have used the above functions throughout the WuClasses for the Grove kit. However, before you use these functions, you need to check updwkpf_io_interface.py to see whether it supports the "device type" you have selected.

Examples

According to the connection types of Grove modules, we can divide them into following categories: Digital IO, Analog IO, PWM, I2C, SPI, and UART. Sometimes, one device handles certain connection type using individual library which is quite different than others. In this case, the function of io interface could be difficult to be defined. Therefore, for the time being, we only apply io interface to WuClass of Grove starter kit.

  • Sensor with digital input

  • Actuator with digital output

  • Sensor with Analog input

  • Sensor with individual library

All of these WuClasses for Grove starter kit can be found in

https://github.com/wukong-m2m/wukong-darjeeling/tree/release0.4/wukong/gateway/udpwkpf

Pin Numbering

While selecting a device type, we have to define the pin numbering for each sensor and actuator. The following figures are the pin assignments for grove starter kit on Edison base board, Raspberry Pi and GrovePi shield. In our code, the pin number used in each WuClass is defined at the beginning of the code so that you can change it easily.