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)
author | makerhacks |
---|---|
permlink | responding-to-http-events-with-physical-results |
category | electronics |
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"} |
created | 2018-06-17 00:57:48 |
last_update | 2018-06-17 00:57:48 |
depth | 0 |
children | 2 |
last_payout | 2018-06-24 00:57:48 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 16.828 HBD |
curator_payout_value | 4.772 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 2,683 |
author_reputation | 156,977,359,570,955 |
root_title | "Responding to HTTP Events with Physical Results" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 61,037,506 |
net_rshares | 8,918,856,646,843 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
blhz | 0 | 18,992,799,988 | 100% | ||
shaunmza | 0 | 6,310,121,825 | 90% | ||
pbock | 0 | 9,354,678,718 | 100% | ||
thatsweeneyguy | 0 | 6,268,755,940 | 50% | ||
rawpride | 0 | 7,293,998,191 | 10% | ||
saywha | 0 | 90,336,570,507 | 100% | ||
ratticus | 0 | 5,028,959,174 | 10% | ||
taukproung85 | 0 | 53,900,492 | 100% | ||
techtek | 0 | 46,432,169,257 | 100% | ||
jefpatat | 0 | 54,317,557,644 | 100% | ||
podanrj | 0 | 7,658,584,053 | 45% | ||
bssman | 0 | 3,197,545,918 | 63% | ||
makerhacks | 0 | 112,169,110,113 | 100% | ||
utopian-io | 0 | 7,611,888,274,650 | 5% | ||
drmake | 0 | 118,155,191,261 | 100% | ||
mytechtrail | 0 | 564,024,687 | 100% | ||
steemmakers | 0 | 33,968,157,272 | 100% | ||
knfitaly | 0 | 4,724,288,942 | 50% | ||
anonymee | 0 | 728,981,876 | 100% | ||
salvadorcrg | 0 | 561,410,719 | 50% | ||
kaking | 0 | 272,722,811 | 50% | ||
ipromote | 0 | 761,903,916,626 | 8.82% | ||
flash4yard | 0 | 894,630,770 | 100% | ||
dmxmaster | 0 | 601,597,152 | 100% | ||
geekahol | 0 | 7,292,532,108 | 100% | ||
builduino | 0 | 2,896,206,001 | 100% | ||
davincibot | 0 | 123,017,347 | 100% | ||
loveyourstyle | 0 | 122,731,201 | 100% | ||
canuckbot | 0 | 122,731,183 | 100% | ||
tpvoter1 | 0 | 102,831,736 | 100% | ||
sbi3 | 0 | 2,231,945,267 | 1% | ||
share4every1 | 0 | 109,970,009 | 100% | ||
electricswine | 0 | 574,759,441 | 50% | ||
electronicsworld | 0 | 3,380,690,369 | 100% | ||
microoo | 0 | 221,283,595 | 50% |
<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.
author | steemmakers |
---|---|
permlink | re-makerhacks-responding-to-http-events-with-physical-results-20180617t073349691z |
category | electronics |
json_metadata | "" |
created | 2018-06-17 07:33:42 |
last_update | 2018-06-17 07:33:42 |
depth | 1 |
children | 0 |
last_payout | 2018-06-24 07:33:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,045 |
author_reputation | 1,907,312,584,548 |
root_title | "Responding to HTTP Events with Physical Results" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 61,064,549 |
net_rshares | 0 |
#### 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**
author | utopian-io |
---|---|
permlink | 20180617t073518450z |
category | electronics |
json_metadata | {"tags":["utopian.tip"],"app":"utopian-io"} |
created | 2018-06-17 07:35:18 |
last_update | 2018-06-17 07:35:18 |
depth | 1 |
children | 0 |
last_payout | 2018-06-24 07:35:18 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 426 |
author_reputation | 152,955,367,999,756 |
root_title | "Responding to HTTP Events with Physical Results" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 61,064,658 |
net_rshares | 0 |