webcamp 2016: python_Илья Батозский _python of things

19
ILYA BATOZSKIY @vz10 PYTHON

Upload: webcamp

Post on 15-Feb-2017

158 views

Category:

Technology


1 download

TRANSCRIPT

ILYA BATOZSKIY @vz10

PYTHON

ILYA BATOZSKIY @vz10

PYTHON

What is IoT?

The internet of things is the network of physical devices, vehicles, buildings and other items - embedded with electronics, sof t ware , sensors, ac t uators, and network connectivity that enable these objects to collect and exchange data.

What do we have for Python?

PyBoard (MicroPython) intel

GALILEO

Raspberry/Orange/Banana Pis

Connect Raspberry to the real world

Little example: connecting switches and LEDs

GPIO initialiseGPIO - general purpose input/output import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) # or GPIO.setmode(GPIO.BCM)

GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) # or pull_up_down=GPIO.PUD_DOWN

GPIO.setup(16, GPIO.OUT)

GPIO lifecycle

try: while True: if GPIO.input(12) == False: GPIO.output(16, GPIO.HIGH) else GPIO.output(16, False)

finally: GPIO.cleanup()

GPIO with callbackimport RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD) # or GPIO.setmode(GPIO.BCM)

GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP) # or pull_up_down=GPIO.PUD_DOWN

GPIO.setup(16, GPIO.OUT)

pwm_led = GPIO.PWM(16, 500) pwm_led.start(100)

GPIO.add_event_detect(12, GPIO.FALLING, callback=press, bouncetime=100)

GPIO.add_event_detect(12, GPIO.RISING, callback=unpress, bouncetime=100)

GPIO with callback lifecycle

def press(): pwm_led.ChangeDutyCycle(50)

def unpress(): pwm_led.ChangeDutyCycle(100)

try: while True: pass

finally: GPIO.cleanup()

No analog inputsLadyada

https://www.adafruit.com

Little prehistory

Little demo…

Dealing with SPISPI - Serial Peripheral Interface SCLK : Serial Clock (output from master).

MOSI : Master Output, Slave Input (output from master).

MISO : Master Input, Slave Output (output from slave).

SS : Slave Select (active low, output from master).

Initialize SPIimport Adafruit_GPIO as GPIOimport Adafruit_GPIO.SPI as SPI

CS = 18MOSI = 23MISO = 24SCLK = 25

gpio = GPIO.get_platform_gpio()gpio.setup(CS, GPIO.OUT) # Initialize CS line.gpio.set_high(CS)# Using software SPI or just init hardware SPI if it presentsspi = SPI.BitBang(gpio, SCLK, MOSI, MISO)# Set SPI mode and LSB first bit order.spi.set_mode(0)spi.set_bit_order(SPI.LSBFIRST)

Write datadef wait_ms(self, ms): start = time.time() delta = ms/1000.0 while (time.time() - start) <= delta: pass

frame = bytearray(l0) # Create bytearray of control commands to get firmwareframe[0] = PN532_SPI_DATAWRITEframe[1] = PN532_PREAMBLE. . . .gpio.set_low(CS)wait_ms(2) # Not recommended to use just time.sleep(0.002)spi.write(frame)gpio.set_high(CS)

response = Nonewhile response[1] != PN532_SPI_READY: gpio.set_low(CS) wait_ms(2) response = spi.transfer([PN532_SPI_STATREAD, 0x00]) gpio.set_high(CS)

Read data# Send frame that we a ready to read and wait for positive response

frame = bytearray(7)frame[0] = PN532_SPI_DATAREADgpio.set_low(self._cs)wait_ms(2)response = spi.transfer(frame)gpio.set_high(CS)

# Remove leading bytes equal 0x00 and one more byte equal 0xffprint('Found PN532 with firmware version: {0}.{1}'.format(response[6], response[7]))

One more thing…

Easy connection to internet services

The open data platform for the Internet of Things

https://thingspeak.comhttps://ifttt.com

Thanks for listening.

Q?