# Getting Started with Onion Omega2 and Losant Using Python

Taron Foxworth \| 04.05.17 \| 3 minute read

The [Onion Omega2](http://onion.io/) is a small, Wi-Fi enabled Linux computer. With the Onion Cloud platform and [dock ecosystem](https://onion.io/store/), it's a great device to dive into hardware and IoT. Check out the Omega2 [Kickstarter](https://www.kickstarter.com/projects/onion/omega2-5-iot-computer-with-wi-fi-powered-by-linux) video:

Onion Omega2 Kickstarter Video - YouTube

Tap to unmute

[Onion Omega2 Kickstarter Video](https://www.youtube.com/watch?v=gGdhzmfIYtI) [Onion](https://www.youtube.com/channel/UCpEodACApRpp8kvf3qv4tMw)

Onion1.88K subscribers

Once you have your Omega2,  you’ll likely want to do two things from the internet:

- **Control GPIO -** With GPIO, we can do things like control lights or servomotors. In this example, we are going to control an LED.
- **React to Input -** We might also want to listen for input. This input could be from things such as a button or motion sensor. In this example, we are going to react to a button press.

We can easily do this with [Losant](https://www.losant.com/). Losant is an excellent tool to control and collect data from internet-connected hardware.

This tutorial will explain how to set up the Omega2 with Losant using Python.

## Setup

### Dock

The simplest way to use the Omega2 is to use a [dock](https://onion.io/store/). For this example, I used the Arduino Dock.

### Storage

Out of the box, the Omega2 doesn't have much storage to do things with Node.js or Python. Once you have the respective platforms installed, you'll have little storage to do anything else.  However, if you have an [Omeaga2+](https://docs.onion.io/omega2-docs/omega2p.html), storage won't be a problem because it has [expandable memory via an SD slot](https://docs.onion.io/omega2-docs/using-a-microsd-card.html#using-a-microsd-card).

If you have the base Omega2, I recommend you use a USB flash drive to extend the storage on the device.

### Internet

The Omega2 setup guide is simple. Follow their [first time set up guide](https://docs.onion.io/omega2-docs/first-time-setup.html) to get connected.

For the rest of this tutorial, [you will need SSH access](https://docs.onion.io/omega2-docs/connecting-to-the-omega-terminal.html#connecting-to-the-omega-terminal-ssh) to the Omega2.

### Python

Once you can use the terminal, we'll need to install Python. To control the GPIO, we are going to use the [Omega2](https://github.com/OnionIoT/onion-gpio-sysfs) Python GPIO library.

Here are the commands you need to run to set up the environment:

```
opkg update
opkg install python git git-http ca-bundle python-pip
pip install paho-mqtt
git clone https://github.com/Losant/losant-mqtt-python.git
cd losant-mqtt-python
python setup.py install
```

### Wiring

For this tutorial, you'll need an [LED](https://www.adafruit.com/products/845) and a [button](https://www.adafruit.com/products/367). However, the ideas are the same for other hardware, like a [servomotor](https://en.wikipedia.org/wiki/Servomotor).

Here is how you wire everything:

- First, connect GND to the negative column of the breadboard and 5V to the positive column of the breadboard
- Connect the LED to GPIO 0
- Using a [pull-down resistor](https://medium.freecodecamp.com/a-simple-explanation-of-pull-down-and-pull-up-resistors-660b308f116a), connect the button to GPIO 1

## Download example project

Using a terminal, SSH into the Omega2 and download the example project. This project is available for you on [GitHub](https://github.com/Losant/example-omega2-python). Here are the commands:

```
cd ~ && git clone git@github.com:Losant/example-omega2-python.git
```

Here is the code in `index.py`:
```
import time

import onionGpio # See Library: https://github.com/OnionIoT/onion-gpio

from losantmqtt import Device

ledPin = 0 # GPIO 0

buttonPin = 1 # GPIO 1

lightStatus = 0

led = onionGpio.OnionGpio(ledPin)

led.setOutputDirection(0) # make LED output and init to 0

button = onionGpio.OnionGpio(buttonPin)

button.setInputDirection() # make Button input

# Construct device

device = Device("my-device-id", "my-access-key", "my-access-secret")

# Called when a Losant Device Command is received.
def on_command(device, command):

global lightStatus

print("Command received.")

print(command["name"])

print(command["payload"])

if command["name"] == "toggle":

lightStatus = int(not lightStatus)

led.setValue(lightStatus)

# Listen for commands.
device.add_event_observer("command", on_command)

# Connect to Losant.
device.connect(blocking=False)

print("Here we go! Press CTRL+C to exit")

try:
    while 1:
        device.loop()
        if device.is_connected():
            if int(button.getValue()):
                print("Button pressed!")
                device.send_state({"button": 1})
                time.sleep(3) # Debounce button press
        time.sleep(0.03)
except KeyboardInterrupt: # If CTRL+C is pressed, exit cleanly:
    led._freeGpio()
    button._freeGpio()# cleanup all GPIO
```

Now, you may see that three things are missing from the code: `my-device-id`, `my-access-key`, and `my-access-secret`. We get this information from Losant.

To run the code:
```
cd example-omega-python
python index.py
```

## Set up Losant

If you haven't already, [create an account](https://accounts.losant.com/create-account) and an [application](https://app.losant.com/#/applications/new) in Losant. Next, [add a device](https://docs.losant.com/devices/overview/) to your Losant application for the Omega2.

Once we create a device, we can get the Device ID (`my-device-id`) from the dashboard.

We also need to give our device an [attribute](https://docs.losant.com/devices/overview/). This tells Losant what data to expect from the device. The attribute should be called `button` and be of type `Boolean`.

Now, we just need an [access key/secret](https://docs.losant.com/applications/access-keys/) (`my-access-key` & `my-access-secret`). Go to the Security tab in Losant to create a pair:

### WORKFLOW

Finally, we'll use a [Workflow](https://docs.losant.com/workflows/overview/) to control the LED and respond to the button press.

**Control GPIO**

In the Workflow Editor, we can use a [Virtual Button](https://docs.losant.com/workflows/triggers/virtual-button/) and [Device Command](https://docs.losant.com/devices/commands/) to control the LED remotely.

In the example firmware, it is listening for a command called `toggle`. When the Virtual Button is clicked, it will send the `toggle` command to the Omega2, toggling the LED on and off.

**React to Input**

To react to the button press, we need to listen for a device payload by using the [Device Trigger node](https://docs.losant.com/workflows/triggers/device/). In the firmware, we are sending state when the button is pressed. When Losant gets this state, it can trigger a workflow.

In this Workflow, we are sending an SMS message every time the button is pressed. However, the output doesn't have to be a SMS message, it could be an email, Tweet, etc. or even many things at once.

## WHAT'S NEXT?

Now that you can control your Omega2 over the internet, the power is yours! You can even start to [visualize data](https://docs.losant.com/dashboards/overview/) that you're collecting.

This tutorial is in a series about controlling different kinds of compute modules and microcontrollers from the internet using Losant. Check out the others:

- [Getting Started with the Sparkfun ESP32](https://www.losant.com/blog/getting-started-with-esp32-and-platformio)
- [Getting Started with the Raspberry Pi](https://www.losant.com/blog/how-to-access-your-raspberry-pis-gpio-over-the-internet)
- [Getting Started with the C.H.I.P.](https://www.losant.com/blog/getting-started-with-chip-and-losant)

If you have any ideas/request, let us know! Follow us on [Twitter](http://twitter.com/losanthq) if you want to keep up with the series.
