Responding to HTTP Events with Physical Results

in #electronics6 years ago

Previously we have blinked LED lights when events have taken place, but what about if we could raise a flag, wave, or some other physical action based on these IoT events?

This is where using an ESP8266 rather than a "land locked" microcontroller helps us. We can poll an online API, and detect the state that we want, then drive motors and servos in response!

We can repurpose the Circuit Playground servo code for the physical part, driving the servos through the Crickit Featherwing:

import time
import board
from busio import I2C
from adafruit_seesaw.seesaw import Seesaw
from adafruit_seesaw.pwmout import PWMOut
from adafruit_motor import servo


i2c = I2C(board.SCL, board.SDA)
seesaw = Seesaw(i2c)

# servo object
pwm = PWMOut(seesaw, 17)
pwm.frequency = 50
my_servo = servo.Servo(pwm)

for i in range(0, 3):
    my_servo.angle = 0
    time.sleep(1)
    my_servo.angle = 45
    time.sleep(1)
    my_servo.angle = 135
    time.sleep(1)
    my_servo.angle = 180
    time.sleep(1)

There is an existing API for checking the @Utopian-IO votes, but unfortunately, it returns too many. We only want the current vote.

To get around this I created an intermediate script on my dev server, with the added benefit it made testing easy (it could be weeks until I get a Utopian vote!).

For this we simply utilize urequests which is a micropython version of the Python 3 requests module.

Adafruit supply example code for HTTP gets, but there are a few issues:

  1. It's not the "python" way, one of the benefits of Micro and Circuit Python is being able to use familiar syntax
  2. Their example code is for http (port 80), and does not handle redirects
  3. We want the output as JSON

As you can see, using the request result as JSON affords us the ability to just extract the author, with no processing on our side!

The final part is to execute our earlier servo script. I use exec so I can keep modifying it independently.

from time import sleep
import urequests

while 1:
    response = urequests.get("https://steemitbots.com/api/utopian.py")
    data = response.json()

    try:
        author = data['author']
        print(author)
    except:
        print("oops")

    if author == "makerhacks":
        exec(open("servos.py").read())

    sleep(60)

makerhacks.png

Sort:  
Congratulations This post has been upvoted by SteemMakers. We are a community-based project that aims to support makers and DIYers on the blockchain in every way possible.

Join our Discord Channel to connect with us and nominate your own or somebody else's posts in our review channel.

Help us to reward you for making it ! Join our voting trail or delegate steem power to the community account.

Your post is also presented on the community website www.steemmakers.com where you can find other selected content.

If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.

Hi @makerhacks!

Your post was upvoted by utopian.io in cooperation with steemmakers - supporting knowledge, innovation and technological advancement on the Steem Blockchain.

Contribute to Open Source with utopian.io

Learn how to contribute on our website and join the new open source economy.

Want to chat? Join the Utopian Community on Discord https://discord.gg/h52nFrV

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 63688.35
ETH 3125.30
USDT 1.00
SBD 3.97