def callback_function(gpio_pin, trigger):
    print("Interrupt occured...")
    led = iot_hw.gpio(
        iot_hw.gpio_pin.GPIO_LED_BLUE)  # Allocate Blue LED GPIO pin
    led.set_dir(iot_hw.gpio_direction.GPIO_DIR_OUTPUT
                )  # Set Blue LED GPIO pin to "output"
    led.write(iot_hw.gpio_level.GPIO_LEVEL_HIGH)  # Turn on Blue LED
    led.close()
    return 0
def test_leds():
    print('Test LEDs')
    #leds = {"Red":iot_hw.gpio_pin.GPIO_LED_RED, "Green":iot_hw.gpio_pin.GPIO_LED_GREEN, "Blue":iot_hw.gpio_pin.GPIO_LED_BLUE}
    leds = {
        "Blue": iot_hw.gpio_pin.GPIO_LED_BLUE,
        "Pmod95": iot_hw.gpio_pin.GPIO_PIN_95
    }

    for key in leds:
        myled = iot_hw.gpio(leds[key])
        myled.set_dir(iot_hw.gpio_direction.GPIO_DIR_OUTPUT)
        print('  {} on'.format(key))
        myled.write(iot_hw.gpio_level.GPIO_LEVEL_HIGH)
        time.sleep(1)
        print('  {} off'.format(key))
        myled.write(iot_hw.gpio_level.GPIO_LEVEL_LOW)
        time.sleep(1)
        myled.close()
Exemple #3
0
def Blink_GPIO():
    print('\nBlink PMOD GPIO 95')

    leds = {"Pmod95": iot_hw.gpio_pin.GPIO_PIN_95}  # List of GPIOs to toggle
    myled = 0  # Object returned from GPIO open API

    for key in leds:  # Sequence through all GPIO in 'leds' list
        myled = iot_hw.gpio(leds[key])  # Open each GPIO key
        myled.set_dir(
            iot_hw.gpio_direction.GPIO_DIR_OUTPUT)  # Set direction to output
        print('  {} on'.format(key))

        myled.write(iot_hw.gpio_level.GPIO_LEVEL_HIGH)  # Set GPIO high
        time.sleep(0.5)  # Sleep for 1/2 second
        print('  {} off'.format(key))

        myled.write(iot_hw.gpio_level.GPIO_LEVEL_LOW)  # Set GPIO low
        time.sleep(0.5)  # Sleep for 1/2 second

        myled.close()  # Close the driver for that GPIO key

# A callback function that will turn on the blue LED
def callback_function(gpio_pin, trigger):
    print("Interrupt occured...")
    led = iot_hw.gpio(
        iot_hw.gpio_pin.GPIO_LED_BLUE)  # Allocate Blue LED GPIO pin
    led.set_dir(iot_hw.gpio_direction.GPIO_DIR_OUTPUT
                )  # Set Blue LED GPIO pin to "output"
    led.write(iot_hw.gpio_level.GPIO_LEVEL_HIGH)  # Turn on Blue LED
    led.close()
    return 0


print("Starting up...")
button = iot_hw.gpio(
    iot_hw.gpio_pin.GPIO_PIN_98)  # Allocate User Button GPIO pin
button.set_dir(
    iot_hw.gpio_direction.GPIO_DIR_INPUT)  # Configure User BUtton as "input"
button.interrupt_request(
    iot_hw.gpio_irq_trig.
    GPIO_IRQ_TRIG_FALLING,  # Trigger interrupt when pressing down on button
    callback_function
)  # Run callback_function() after the interrupt is generated

print("Ready to sleep...")
time.sleep(30)  # Sleep for 30 seconds
# Press the button here, during the 30 second wait

print("Freeing resources...")
button.interrupt_free()  # Turn off button interrupt and free IRQ resources
button.close()  # Free up User Button's GPIO pin
Exemple #5
0
import iot_hw
import time


# Initialize all LEDs
red_led = iot_hw.gpio(iot_hw.gpio_pin.GPIO_LED_RED)
red_led.set_dir(iot_hw.gpio_direction.GPIO_DIR_OUTPUT)

green_led = iot_hw.gpio(iot_hw.gpio_pin.GPIO_LED_GREEN)
green_led.set_dir(iot_hw.gpio_direction.GPIO_DIR_OUTPUT)

blue_led = iot_hw.gpio(iot_hw.gpio_pin.GPIO_LED_BLUE)
blue_led.set_dir(iot_hw.gpio_direction.GPIO_DIR_OUTPUT)

# Cycle all LEDs on and off
# Red on
red_led.write(iot_hw.gpio_level.GPIO_LEVEL_HIGH)
time.sleep(1)

# Green on, Red off
green_led.write(iot_hw.gpio_level.GPIO_LEVEL_HIGH)
red_led.write(iot_hw.gpio_level.GPIO_LEVEL_LOW)
time.sleep(1)

# Blue on, Green off
blue_led.write(iot_hw.gpio_level.GPIO_LEVEL_HIGH)
green_led.write(iot_hw.gpio_level.GPIO_LEVEL_LOW)
time.sleep(1)

# Red on (Purple)
red_led.write(iot_hw.gpio_level.GPIO_LEVEL_HIGH)
        if button_held >= 3:
            loop = False  # Change while loop's exit condition since button was held long enough
            print(
                "Exiting... because button was held down for more than 3 seconds ("
                + str(button_held) + " to be exact)\n")
        else:
            print("Button was held down for " + str(button_held) +
                  " seconds\n")

    return 0


# Main code starts here
print("\nStarting up... please wait")
led = iot_hw.gpio(iot_hw.gpio_pin.GPIO_LED_BLUE)  # Allocate Blue LED GPIO pin
led.set_dir(
    iot_hw.gpio_direction.GPIO_DIR_OUTPUT)  # Set Blue LED GPIO pin to "output"
led.write(iot_hw.gpio_level.GPIO_LEVEL_LOW)  # Turn off Blue LED

button = iot_hw.gpio(
    iot_hw.gpio_pin.GPIO_PIN_98)  # Allocate User Button GPIO pin
button.set_dir(
    iot_hw.gpio_direction.GPIO_DIR_INPUT)  # Configure User BUtton as "input"
button.interrupt_request(
    iot_hw.gpio_irq_trig.
    GPIO_IRQ_TRIG_BOTH,  # Trigger interrupt when pressing down on button
    my_callback_fcn)  # Run my_callback_fcn() after the interrupt is generated

w = wwan()  # Create instances of WWAN LED class and set equal to 'w'
w.off()  # Turn off WWAN LED
TWILIO_URL = r'https://api.twilio.com/2010-04-01/Accounts//Messages.json'
TWILIO_SID = r'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
TWILIO_AUTH = r'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SMS_FROM = r'+12165551212'
SMS_TO = r'+12145551212'

#### Setup the MAL and data connection ########################################
network_handler = iot_mal.network()  # Connect to MAL.network
network_handler.set_connection_mode(
    1,  # Mode: 0 - Always, 1 - On-demand
    10,  # On-demand Timeout: Disconnect in mins if no access
    2)  # Manual Mode: 0 - Disconnect, 1 - Connect (Always/on-demand),
#              2 - Connect once

#### Initialize the GPIO connected to the SK2 User Button #####################
button = iot_hw.gpio(iot_hw.gpio_pin.GPIO_USER_BUTTON)
button.set_dir(iot_hw.gpio_direction.GPIO_DIR_INPUT)

if button.read():
    button_state = 'up'
else:
    button_state = 'down'

data = 'Body="button": ' + button_state

curl_url = " " + TWILIO_URL + "/" + TWILIO_SID + "/Messages.json"
curl_cmd = " -X POST"
curl_to = " --data-urlencode 'To=" + SMS_TO + "'"
curl_from = " --data-urlencode 'From=" + SMS_FROM + "'"
curl_data = " --data-urlencode '" + data + "'"
curl_auth = " -u " + TWILIO_SID + ":" + TWILIO_AUTH
#red_led_blink.py

import time  # Needed for time.sleep() function
import iot_hw  # Allows access to SK2 hardware resources

# Define 'ON' and 'OFF'
ON = iot_hw.gpio_level.GPIO_LEVEL_HIGH
OFF = iot_hw.gpio_level.GPIO_LEVEL_LOW

# Allocate and configure GPIO as output for Red LED
red_led = iot_hw.gpio(iot_hw.gpio_pin.GPIO_LED_RED)
red_led.set_dir(iot_hw.gpio_direction.GPIO_DIR_OUTPUT)

# Red on
red_led.write(ON)
time.sleep(1)

# Red off
red_led.write(OFF)

# Release Red LED resource
red_led.close()