Skip to content

Commit 17af90b

Browse files
authored
Merge pull request #2234 from adafruit/telegraph
Adding code for two way telegraph guide
2 parents 271b3fe + d3755d0 commit 17af90b

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

Two_Way_Servo_Telegraph/code.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import time
5+
import ssl
6+
import board
7+
import touchio
8+
import pwmio
9+
from analogio import AnalogIn
10+
import adafruit_requests
11+
import socketpool
12+
import wifi
13+
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
14+
from simpleio import map_range
15+
from adafruit_motor import servo
16+
17+
# select which display is running the code
18+
servo_one = True
19+
# servo_two = True
20+
21+
try:
22+
from secrets import secrets
23+
except ImportError:
24+
print("WiFi secrets are kept in secrets.py, please add them there!")
25+
raise
26+
# connect to adafruitio
27+
aio_username = secrets["aio_username"]
28+
aio_key = secrets["aio_key"]
29+
30+
print("Connecting to %s" % secrets["ssid"])
31+
wifi.radio.connect(secrets["ssid"], secrets["password"])
32+
print("Connected to %s!" % secrets["ssid"])
33+
34+
pool = socketpool.SocketPool(wifi.radio)
35+
requests = adafruit_requests.Session(pool, ssl.create_default_context())
36+
# Initialize an Adafruit IO HTTP API object
37+
io = IO_HTTP(aio_username, aio_key, requests)
38+
39+
# pylint: disable=undefined-variable
40+
# disabling undefined-variable for ease of comment/uncomment
41+
# servo_one or servo_two at top for user
42+
43+
# setup for display 1
44+
if servo_one:
45+
# servo calibration values
46+
CALIB_MIN = 15708
47+
CALIB_MAX = 43968
48+
# create feeds
49+
try:
50+
# get feed
51+
out_feed = io.get_feed("touch-1")
52+
in_feed = io.get_feed("touch-2")
53+
except AdafruitIO_RequestError:
54+
# if no feed exists, create one
55+
out_feed = io.create_new_feed("touch-1")
56+
in_feed = io.create_new_feed("touch-2")
57+
# setup for display 2
58+
if servo_two:
59+
CALIB_MIN = 15668
60+
CALIB_MAX = 43550
61+
try:
62+
# get feed
63+
out_feed = io.get_feed("touch-2")
64+
in_feed = io.get_feed("touch-1")
65+
except AdafruitIO_RequestError:
66+
# if no feed exists, create one
67+
out_feed = io.create_new_feed("touch-2")
68+
in_feed = io.create_new_feed("touch-1")
69+
70+
received_data = io.receive_data(in_feed["key"])
71+
72+
# Pin setup
73+
SERVO_PIN = board.A1
74+
FEEDBACK_PIN = board.A2
75+
touch = touchio.TouchIn(board.TX)
76+
77+
# angles for servo
78+
ANGLE_MIN = 0
79+
ANGLE_MAX = 180
80+
81+
# servo setup
82+
pwm = pwmio.PWMOut(SERVO_PIN, duty_cycle=2 ** 15, frequency=50)
83+
servo = servo.Servo(pwm)
84+
servo.angle = None
85+
86+
# setup feedback
87+
feedback = AnalogIn(FEEDBACK_PIN)
88+
89+
# position finder function for servo
90+
def get_position():
91+
return map_range(feedback.value, CALIB_MIN, CALIB_MAX, ANGLE_MIN, ANGLE_MAX)
92+
93+
# touch debounce
94+
touch_state = False
95+
# new_msg value
96+
new_msg = None
97+
# last_msg value
98+
last_msg = None
99+
# time.monotonic() holder for pinging IO
100+
clock = 5
101+
102+
while True:
103+
# check IO for new data every 5 seconds
104+
if (time.monotonic() - clock) > 2:
105+
# get data
106+
received_data = io.receive_data(in_feed["key"])
107+
# reset clock
108+
clock = time.monotonic()
109+
# if touched...
110+
if touch.value and touch_state is False:
111+
touch_state = True
112+
# when touch is released...
113+
if not touch.value and touch_state is True:
114+
# get position of servo
115+
pos = get_position()
116+
# send position to IO
117+
io.send_data(out_feed["key"], float(pos))
118+
# delay to settle
119+
time.sleep(1)
120+
# reset touch state
121+
touch_state = False
122+
# if a new value is detected
123+
if float(received_data["value"]) != last_msg:
124+
# assign value to new_msg
125+
new_msg = float(received_data["value"])
126+
# set servo angle
127+
servo.angle = new_msg
128+
# quick delay to settle
129+
time.sleep(1)
130+
# release servo
131+
servo.angle = None
132+
# log msg
133+
last_msg = new_msg

0 commit comments

Comments
 (0)