create account

Responding to HTTP Events with Physical Results by makerhacks

View this thread on: hive.blogpeakd.comecency.com
· @makerhacks ·
$21.60
Responding to HTTP Events with Physical Results
https://i.imgur.com/13j4CCO.gif

[Previously we have blinked LED lights](https://steemit.com/programming/@makerhacks/running-python-on-the-esp8266-nodemcu-from-the-command-line) 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](https://steemit.com/programming/@makerhacks/adafruit-circuit-playground-crickit-servos-with-micro-circuit-python) 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
1. Their example code is for http (port 80), and does not handle redirects
1. 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)
```


<hr>

[![makerhacks.png](https://cdn.steemitimages.com/DQmQt85v2egWPFwnp55DneenrwKCR6sQLinEfq3JAo68FwR/makerhacks.png)](/@makerhacks)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authormakerhacks
permlinkresponding-to-http-events-with-physical-results
categoryelectronics
json_metadata{"tags":["electronics","arduino","python","programming","steemmakers"],"users":["utopian-io"],"image":["https://i.imgur.com/13j4CCO.gif","https://cdn.steemitimages.com/DQmQt85v2egWPFwnp55DneenrwKCR6sQLinEfq3JAo68FwR/makerhacks.png"],"links":["https://steemit.com/programming/@makerhacks/running-python-on-the-esp8266-nodemcu-from-the-command-line","https://steemit.com/programming/@makerhacks/adafruit-circuit-playground-crickit-servos-with-micro-circuit-python","/@makerhacks"],"app":"steemit/0.1","format":"markdown"}
created2018-06-17 00:57:48
last_update2018-06-17 00:57:48
depth0
children2
last_payout2018-06-24 00:57:48
cashout_time1969-12-31 23:59:59
total_payout_value16.828 HBD
curator_payout_value4.772 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,683
author_reputation156,977,359,570,955
root_title"Responding to HTTP Events with Physical Results"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,037,506
net_rshares8,918,856,646,843
author_curate_reward""
vote details (35)
@steemmakers ·
<div class='pull-right'><center><a href='http://www.steemmakers.com'><img src='https://www.steemmakers.com/img/comment_logo_makers.png' /></a></center></div><b>Congratulations</b> 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. <br/><br/>Join our <a href='https://discord.gg/EFGbRuW'>Discord Channel</a> to connect with us and nominate your own or somebody else's posts in our review channel.<br/><br/><b>Help us to reward you for making it !</b> Join <a href='https://www.steemmakers.com/steemmakerstrail.php'>our voting trail</a> or <a href='https://www.steemmakers.com/steemmakersdelegation.php'>delegate steem power</a> to the community account. <br/><br/>Your post is also presented on the community website <a href='http://www.steemmakers.com'>www.steemmakers.com</a> where you can find other selected content. <br/><br/>If you like our work, please consider upvoting this comment to support the growth of our community. Thank you.
properties (22)
authorsteemmakers
permlinkre-makerhacks-responding-to-http-events-with-physical-results-20180617t073349691z
categoryelectronics
json_metadata""
created2018-06-17 07:33:42
last_update2018-06-17 07:33:42
depth1
children0
last_payout2018-06-24 07:33:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,045
author_reputation1,907,312,584,548
root_title"Responding to HTTP Events with Physical Results"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,064,549
net_rshares0
@utopian-io ·
#### 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 <a href="https://join.utopian.io">our website</a> and join the new open source economy.

**Want to chat? Join the Utopian Community on Discord https://discord.gg/h52nFrV**
properties (22)
authorutopian-io
permlink20180617t073518450z
categoryelectronics
json_metadata{"tags":["utopian.tip"],"app":"utopian-io"}
created2018-06-17 07:35:18
last_update2018-06-17 07:35:18
depth1
children0
last_payout2018-06-24 07:35:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length426
author_reputation152,955,367,999,756
root_title"Responding to HTTP Events with Physical Results"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,064,658
net_rshares0