Python Programming Language

Python is a programming language that lets you work quickly and integrate systems more effectively.

Python Installation

Check Python is installed

  1. root@platform:~# python --version

Python Mraa Hello Internet of Things

Create your first Python script, the common “Hello Internet of Things” example

  1. root@platform:~# vi iot.py
  1. #!/usr/bin/python
  2. # Hello Internet of Things
  3. print 'Hello Internet of Things @ Python'
  4. # End of Python Script
  1. root@platform:~# python iot.py

Python Mraa Version

Let’s get the version of mraa library we installed

  1. root@platform:~# vi iot.py
  1. #!/usr/bin/python
  2. # Libraries
  3. import mraa
  4. # Mraa Version
  5. print (mraa.getVersion())
  6. print (mraa.getPlatformName())
  7. print (mraa.getPlatformType())
  8. # End of Python Script
  1. root@platform:~# python iot.py

Python Mraa Analog Input Output (AIO)

Let’s work with Analog Input Output

  1. root@platform:~# vi iot.py
  1. #!/usr/bin/python
  2. # Libraries
  3. import mraa
  4. # Mraa Aio
  5. aioline = mraa.Aio(0)
  6. aioline.setBit(10)
  7. aioline.read()
  8. print ("%.5f" % aioline.readFloat())
  9. # End of Python Script
  1. root@platform:~# python iot.py

Python Mraa Inter-Integrated Circuit (I2C)

Let’s work with Inter-Integrated Circuit protocol

  1. root@platform:~# vi iot.py
  1. #!/usr/bin/python
  2. # Libraries
  3. import mraa
  4. # Mraa I2C
  5. if mraa.getPlatformType() == 1:
  6. i2cline = mraa.I2c(0)
  7. if mraa.getPlatformType() == 2:
  8. i2cline = mraa.I2c(1)
  9. i2cline.address(0x6b)
  10. print i2cline.readReg(0x6b, 0x80)
  11. # End of Python Script
  1. root@platform:~# python iot.py

Python Mraa General Purpose Input Output (GPIO) Direction Output

Let’s work with General Purpose Input Output, Direction Output

  1. root@platform:~# vi iot.py
  1. #!/usr/bin/python
  2. # Libraries
  3. import mraa
  4. import time
  5. # Mraa GPIO Direction Output
  6. if mraa.getPlatformType() == 1:
  7. gpioline = mraa.Gpio(13)
  8. if mraa.getPlatformType() == 2:
  9. gpioline = mraa.Gpio(44)
  10. gpioline.dir(mraa.DIR_OUT)
  11. gpionextvalue = not gpioline.read()
  12. gpioline.write(gpionextvalue)
  13. time.sleep(1)
  14. gpioline.write(not gpionextvalue)
  15. # End of Python Script
  1. root@platform:~# python iot.py

Python Mraa Universal Asynchronous Receiver/Transmitter (UART)

Let’s work with Universal Asynchronous Receiver/Transmitter

  1. root@platform:~# vi iot.py
  1. #!/usr/bin/python
  2. # Libraries
  3. import mraa
  4. # Mraa UART
  5. uartdevice = mraa.Uart(0)
  6. print uartdevice.getDevicePath()
  7. # End of Python Script
  1. root@platform:~# python iot.py