Before you can start working with UPER1 you have to make sure that it was successfully recognized by the host. To do so, please choose your platform and follow the steps:
First of all, even though UPER1 is controlled with SFP messages sent over the command port, this will not be a tutorial about SFP messages. If you really want to send SFP messages by yourself, you should refer to the UPER1 function list to see the available functions and SFP wiki to learn how the messages are encoded.
Instead it will be shown how to create an LED blinky program by using IoTPy API, which is the recommended way of developing applications not only because it's much easier to use, but it also gives a lot of power and flexibility in all development stages. In order to run the code using IoTPy you will need to install two things:
The IoTPy API has its own concept and to better understand it you should read the full IoTPY API reference. But for this example it will suffice to know that the main idea behind IoTPy is that you have a device object from which you can get certain modules, for example a GPIO module. Each module has it's own capabilities, which usually manifest in a form of a function. One of the GPIO modules capability is to output certain voltage (write digital signal) on the physical pin, which, as you may have guessed it, will be used to turn on and off an LED.
So the short theory is here, lets jump to the code:
from time import sleep from IoTPy.core.gpio import GPIO from IoTPy.pyuper.uper import UPER1 with UPER1() as board, board.GPIO(27) as redPin: redPin.setup(GPIO.OUTPUT) # set GPIO pin to be output while True: redPin.write(0) # Turn led ON (LED on board is common anode - therefore inverted) sleep(0.5) redPin.write(1) # Turn led OFF sleep(0.5)
Copy and paste the code into a file (for example blinky.py) and run it.
In UNIX-like systems type python blinky.py
and in Windows you can open the file with IDLE and press F5 key or simply double click it.
For those who are new to Python or are unclear about the code, here's some details:
with
statement is one of the core IoTPy elements. Not only we create and initialize a certain module in it, but we also define the lifespan of this object - once the code exits from a with
block, all modules are de-initialized. Although in many cases de-initialization is not important, in others it can result in undefined behaviour. That's why with
usage is not only highly recommended, but demanded too. It's also a good Python's programming practice!with
statement we initialize UPER1 board with UPER1()
call, which automatically detects a connected UPER1 board, and then from this board we extract a GPIO module, which corresponds to (controls) the 27th pin.setup()
call and enter an infinite loop in which we toggle the pin level from LOW to HIGH (to LOW …) with a bit of delay. That's it!