Taron Foxworth
Published

Building an API for Your Raspberry Pi Zero W

This tutorial will walk through creating an API for your Pi Zero W (or others). This API allows you to read/control GPIO remotely with code.

BeginnerProtip1 hour19,772
Building an API for Your Raspberry Pi Zero W

Things used in this project

Hardware components

Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
MicroSD Card
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×5
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
LED (generic)
LED (generic)
×4
Resistor 2.21k ohm
Resistor 2.21k ohm
×4

Software apps and online services

Losant Platform
Losant Platform

Story

Read more

Schematics

Raspberry PI Zero W API Wiring

Wiring Raspberry PI Zero W to 4 LEDs

pi-zero-led-wiring_S5uNJ3nRST.png

Code

index.py

Python
Example Python application listening for Device Commands from Losant
import json
from gpiozero import LED # Import GPIO library: https://gpiozero.readthedocs.io/en/stable/
from time import sleep
from losantmqtt import Device # Import Losant library: https://github.com/Losant/losant-mqtt-python

# We need to describe what GPIO is available to control.
# This key is the GPIO number and the value is the peripheral to control.
availableGPIO = {"6": LED(6), "13": LED(13), "19": LED(19), "26": LED(26)}

# Construct Losant device
device = Device("my-device-id", "my-app-access-key", "my-app-access-secret")

def on_command(device, command):
    print(command["name"] + " command received.")

    # Listen for the gpioControl. This name configured in Losant
    if command["name"] == "gpioControl":
        # The gpio that's passed in from the path parameter
        currentGpio = int(command["payload"]["gpio"])
        currentGpioText = str(currentGpio)

        # Get the LED at that physical GPIO number
        # from our availableGPIO
        led = availableGPIO.get(currentGpioText)

        # If found, toggle the LED
        # If not, display a message
        if led:
            print("Toggling LED " + str(currentGpioText))
            led.toggle()
        else:
            print("GPIO not configured " + str(currentGpioText))



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

print("Listening for device commands")

# Connect to Losant and leave the connection open
device.connect(blocking=True)

Credits

Taron Foxworth

Taron Foxworth

0 projects • 6 followers

Comments