Added corrected Lab11 directory and code
This commit is contained in:
parent
a37409e5e7
commit
3d2736ac57
48 changed files with 8548 additions and 1 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -15,7 +15,8 @@ dist/
|
||||||
downloads/
|
downloads/
|
||||||
eggs/
|
eggs/
|
||||||
.eggs/
|
.eggs/
|
||||||
lib/
|
# Commented out for CircuitPython 2026-0418
|
||||||
|
#lib/
|
||||||
lib64/
|
lib64/
|
||||||
parts/
|
parts/
|
||||||
sdist/
|
sdist/
|
||||||
|
|
|
||||||
204
Lab10/MicroPython/Code/lib/umqtt/simple.py
Normal file
204
Lab10/MicroPython/Code/lib/umqtt/simple.py
Normal file
|
|
@ -0,0 +1,204 @@
|
||||||
|
import usocket as socket
|
||||||
|
import ustruct as struct
|
||||||
|
from ubinascii import hexlify
|
||||||
|
|
||||||
|
class MQTTException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class MQTTClient:
|
||||||
|
|
||||||
|
def __init__(self, client_id, server, port=0, user=None, password=None, keepalive=0,
|
||||||
|
ssl=False, ssl_params={}):
|
||||||
|
if port == 0:
|
||||||
|
port = 8883 if ssl else 1883
|
||||||
|
self.client_id = client_id
|
||||||
|
self.sock = None
|
||||||
|
self.server = server
|
||||||
|
self.port = port
|
||||||
|
self.ssl = ssl
|
||||||
|
self.ssl_params = ssl_params
|
||||||
|
self.pid = 0
|
||||||
|
self.cb = None
|
||||||
|
self.user = user
|
||||||
|
self.pswd = password
|
||||||
|
self.keepalive = keepalive
|
||||||
|
self.lw_topic = None
|
||||||
|
self.lw_msg = None
|
||||||
|
self.lw_qos = 0
|
||||||
|
self.lw_retain = False
|
||||||
|
|
||||||
|
def _send_str(self, s):
|
||||||
|
self.sock.write(struct.pack("!H", len(s)))
|
||||||
|
self.sock.write(s)
|
||||||
|
|
||||||
|
def _recv_len(self):
|
||||||
|
n = 0
|
||||||
|
sh = 0
|
||||||
|
while 1:
|
||||||
|
b = self.sock.read(1)[0]
|
||||||
|
n |= (b & 0x7f) << sh
|
||||||
|
if not b & 0x80:
|
||||||
|
return n
|
||||||
|
sh += 7
|
||||||
|
|
||||||
|
def set_callback(self, f):
|
||||||
|
self.cb = f
|
||||||
|
|
||||||
|
def set_last_will(self, topic, msg, retain=False, qos=0):
|
||||||
|
assert 0 <= qos <= 2
|
||||||
|
assert topic
|
||||||
|
self.lw_topic = topic
|
||||||
|
self.lw_msg = msg
|
||||||
|
self.lw_qos = qos
|
||||||
|
self.lw_retain = retain
|
||||||
|
|
||||||
|
def connect(self, clean_session=True):
|
||||||
|
self.sock = socket.socket()
|
||||||
|
addr = socket.getaddrinfo(self.server, self.port)[0][-1]
|
||||||
|
self.sock.connect(addr)
|
||||||
|
if self.ssl:
|
||||||
|
import ussl
|
||||||
|
self.sock = ussl.wrap_socket(self.sock, **self.ssl_params)
|
||||||
|
premsg = bytearray(b"\x10\0\0\0\0\0")
|
||||||
|
msg = bytearray(b"\x04MQTT\x04\x02\0\0")
|
||||||
|
|
||||||
|
sz = 10 + 2 + len(self.client_id)
|
||||||
|
msg[6] = clean_session << 1
|
||||||
|
if self.user is not None:
|
||||||
|
sz += 2 + len(self.user) + 2 + len(self.pswd)
|
||||||
|
msg[6] |= 0xC0
|
||||||
|
if self.keepalive:
|
||||||
|
assert self.keepalive < 65536
|
||||||
|
msg[7] |= self.keepalive >> 8
|
||||||
|
msg[8] |= self.keepalive & 0x00FF
|
||||||
|
if self.lw_topic:
|
||||||
|
sz += 2 + len(self.lw_topic) + 2 + len(self.lw_msg)
|
||||||
|
msg[6] |= 0x4 | (self.lw_qos & 0x1) << 3 | (self.lw_qos & 0x2) << 3
|
||||||
|
msg[6] |= self.lw_retain << 5
|
||||||
|
|
||||||
|
i = 1
|
||||||
|
while sz > 0x7f:
|
||||||
|
premsg[i] = (sz & 0x7f) | 0x80
|
||||||
|
sz >>= 7
|
||||||
|
i += 1
|
||||||
|
premsg[i] = sz
|
||||||
|
|
||||||
|
self.sock.write(premsg, i + 2)
|
||||||
|
self.sock.write(msg)
|
||||||
|
#print(hex(len(msg)), hexlify(msg, ":"))
|
||||||
|
self._send_str(self.client_id)
|
||||||
|
if self.lw_topic:
|
||||||
|
self._send_str(self.lw_topic)
|
||||||
|
self._send_str(self.lw_msg)
|
||||||
|
if self.user is not None:
|
||||||
|
self._send_str(self.user)
|
||||||
|
self._send_str(self.pswd)
|
||||||
|
resp = self.sock.read(4)
|
||||||
|
assert resp[0] == 0x20 and resp[1] == 0x02
|
||||||
|
if resp[3] != 0:
|
||||||
|
raise MQTTException(resp[3])
|
||||||
|
return resp[2] & 1
|
||||||
|
|
||||||
|
def disconnect(self):
|
||||||
|
self.sock.write(b"\xe0\0")
|
||||||
|
self.sock.close()
|
||||||
|
|
||||||
|
def ping(self):
|
||||||
|
self.sock.write(b"\xc0\0")
|
||||||
|
|
||||||
|
def publish(self, topic, msg, retain=False, qos=0):
|
||||||
|
pkt = bytearray(b"\x30\0\0\0")
|
||||||
|
pkt[0] |= qos << 1 | retain
|
||||||
|
sz = 2 + len(topic) + len(msg)
|
||||||
|
if qos > 0:
|
||||||
|
sz += 2
|
||||||
|
assert sz < 2097152
|
||||||
|
i = 1
|
||||||
|
while sz > 0x7f:
|
||||||
|
pkt[i] = (sz & 0x7f) | 0x80
|
||||||
|
sz >>= 7
|
||||||
|
i += 1
|
||||||
|
pkt[i] = sz
|
||||||
|
#print(hex(len(pkt)), hexlify(pkt, ":"))
|
||||||
|
self.sock.write(pkt, i + 1)
|
||||||
|
self._send_str(topic)
|
||||||
|
if qos > 0:
|
||||||
|
self.pid += 1
|
||||||
|
pid = self.pid
|
||||||
|
struct.pack_into("!H", pkt, 0, pid)
|
||||||
|
self.sock.write(pkt, 2)
|
||||||
|
self.sock.write(msg)
|
||||||
|
if qos == 1:
|
||||||
|
while 1:
|
||||||
|
op = self.wait_msg()
|
||||||
|
if op == 0x40:
|
||||||
|
sz = self.sock.read(1)
|
||||||
|
assert sz == b"\x02"
|
||||||
|
rcv_pid = self.sock.read(2)
|
||||||
|
rcv_pid = rcv_pid[0] << 8 | rcv_pid[1]
|
||||||
|
if pid == rcv_pid:
|
||||||
|
return
|
||||||
|
elif qos == 2:
|
||||||
|
assert 0
|
||||||
|
|
||||||
|
def subscribe(self, topic, qos=0):
|
||||||
|
assert self.cb is not None, "Subscribe callback is not set"
|
||||||
|
pkt = bytearray(b"\x82\0\0\0")
|
||||||
|
self.pid += 1
|
||||||
|
struct.pack_into("!BH", pkt, 1, 2 + 2 + len(topic) + 1, self.pid)
|
||||||
|
#print(hex(len(pkt)), hexlify(pkt, ":"))
|
||||||
|
self.sock.write(pkt)
|
||||||
|
self._send_str(topic)
|
||||||
|
self.sock.write(qos.to_bytes(1, "little"))
|
||||||
|
while 1:
|
||||||
|
op = self.wait_msg()
|
||||||
|
if op == 0x90:
|
||||||
|
resp = self.sock.read(4)
|
||||||
|
#print(resp)
|
||||||
|
assert resp[1] == pkt[2] and resp[2] == pkt[3]
|
||||||
|
if resp[3] == 0x80:
|
||||||
|
raise MQTTException(resp[3])
|
||||||
|
return
|
||||||
|
|
||||||
|
# Wait for a single incoming MQTT message and process it.
|
||||||
|
# Subscribed messages are delivered to a callback previously
|
||||||
|
# set by .set_callback() method. Other (internal) MQTT
|
||||||
|
# messages processed internally.
|
||||||
|
def wait_msg(self):
|
||||||
|
res = self.sock.read(1)
|
||||||
|
self.sock.setblocking(True)
|
||||||
|
if res is None:
|
||||||
|
return None
|
||||||
|
if res == b"":
|
||||||
|
raise OSError(-1)
|
||||||
|
if res == b"\xd0": # PINGRESP
|
||||||
|
sz = self.sock.read(1)[0]
|
||||||
|
assert sz == 0
|
||||||
|
return None
|
||||||
|
op = res[0]
|
||||||
|
if op & 0xf0 != 0x30:
|
||||||
|
return op
|
||||||
|
sz = self._recv_len()
|
||||||
|
topic_len = self.sock.read(2)
|
||||||
|
topic_len = (topic_len[0] << 8) | topic_len[1]
|
||||||
|
topic = self.sock.read(topic_len)
|
||||||
|
sz -= topic_len + 2
|
||||||
|
if op & 6:
|
||||||
|
pid = self.sock.read(2)
|
||||||
|
pid = pid[0] << 8 | pid[1]
|
||||||
|
sz -= 2
|
||||||
|
msg = self.sock.read(sz)
|
||||||
|
self.cb(topic, msg)
|
||||||
|
if op & 6 == 2:
|
||||||
|
pkt = bytearray(b"\x40\x02\0\0")
|
||||||
|
struct.pack_into("!H", pkt, 2, pid)
|
||||||
|
self.sock.write(pkt)
|
||||||
|
elif op & 6 == 4:
|
||||||
|
assert 0
|
||||||
|
|
||||||
|
# Checks whether a pending message from server is available.
|
||||||
|
# If not, returns immediately with None. Otherwise, does
|
||||||
|
# the same processing as wait_msg.
|
||||||
|
def check_msg(self):
|
||||||
|
self.sock.setblocking(False)
|
||||||
|
return self.wait_msg()
|
||||||
Binary file not shown.
Binary file not shown.
BIN
Lab11/CircuitPython/Code.tar
Normal file
BIN
Lab11/CircuitPython/Code.tar
Normal file
Binary file not shown.
32
Lab11/CircuitPython/Code/blink_external2.py
Executable file
32
Lab11/CircuitPython/Code/blink_external2.py
Executable file
|
|
@ -0,0 +1,32 @@
|
||||||
|
# Program for CircuitPython on a Pico W
|
||||||
|
|
||||||
|
import board
|
||||||
|
import digitalio
|
||||||
|
import time
|
||||||
|
|
||||||
|
led = digitalio.DigitalInOut(board.LED)
|
||||||
|
led15 = digitalio.DigitalInOut(board.GP15)
|
||||||
|
led16 = digitalio.DigitalInOut(board.GP16)
|
||||||
|
|
||||||
|
led.direction = digitalio.Direction.OUTPUT
|
||||||
|
led15.direction = digitalio.Direction.OUTPUT
|
||||||
|
led16.direction = digitalio.Direction.OUTPUT
|
||||||
|
|
||||||
|
print("\nCircuitPython example code")
|
||||||
|
print("Blink the onboard LED and LED's connected to GP15 & GP16\n")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
print("Start")
|
||||||
|
led.value = True #turn on the LED
|
||||||
|
led15.value = False #turn on the LED
|
||||||
|
led16.value = True #turn on the LED
|
||||||
|
time.sleep(1) #wait for one second
|
||||||
|
print("Off")
|
||||||
|
led.value = False #turn off the LED
|
||||||
|
led15.value = True #turn off the LED
|
||||||
|
led16.value = False #turn off the LED
|
||||||
|
time.sleep(1) #wait for one second
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
21
Lab11/CircuitPython/Code/blink_internal.py
Executable file
21
Lab11/CircuitPython/Code/blink_internal.py
Executable file
|
|
@ -0,0 +1,21 @@
|
||||||
|
# Program for CircuitPython on a Pico W
|
||||||
|
|
||||||
|
import board
|
||||||
|
import digitalio
|
||||||
|
import time
|
||||||
|
|
||||||
|
led = digitalio.DigitalInOut(board.LED)
|
||||||
|
led.direction = digitalio.Direction.OUTPUT
|
||||||
|
|
||||||
|
print("Hello, CircuitPython!")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
print("On!")
|
||||||
|
led.value = True
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
print("Off!")
|
||||||
|
led.value = False
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
11
Lab11/CircuitPython/Code/certificates/certificate-chain.pem
Executable file
11
Lab11/CircuitPython/Code/certificates/certificate-chain.pem
Executable file
|
|
@ -0,0 +1,11 @@
|
||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIBozCCAQwCCQCwlOkfTi8J9zANBgkqhkiG9w0BAQsFADAWMRQwEgYDVQQDDAtw
|
||||||
|
aWNvdy5sb2NhbDAeFw0yMzAyMjgwNTQyNTZaFw0yNDAyMjgwNTQyNTZaMBYxFDAS
|
||||||
|
BgNVBAMMC3BpY293LmxvY2FsMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDq
|
||||||
|
UQiuhGBzHCLaesEjk6e6Kx7iNAKneaPht0Q6TsvF+IgOpuQFNkvQTU5S7POKDl2c
|
||||||
|
UGkMT9Q1x4xRdM3u3qT3OmutyAtfL94RkK1qsblJwIH8xop5p7P+gwHTT4UOEicj
|
||||||
|
MOSYBmkI6YvO/QImGdEoezWBqxKjOzyYLnv4UrwtYQIDAQABMA0GCSqGSIb3DQEB
|
||||||
|
CwUAA4GBABuzaLw1tcFNYZ0ViWnii1j97otemOFkM31TW8Ohm3zRS6f/aQi+hujN
|
||||||
|
BYMWIhxlK2cr0zsN+mXCVZN5u2BY0Q/w/7cNMBPNcGG7dJCGoBJRKRbEoBb/AMaO
|
||||||
|
vjp0giQ8kFOtRcf0Gun5gPdDcmZayZwU1n/V1Q7UjwKXcqe0+eXu
|
||||||
|
-----END CERTIFICATE-----
|
||||||
16
Lab11/CircuitPython/Code/certificates/key.pem
Executable file
16
Lab11/CircuitPython/Code/certificates/key.pem
Executable file
|
|
@ -0,0 +1,16 @@
|
||||||
|
-----BEGIN PRIVATE KEY-----
|
||||||
|
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAOpRCK6EYHMcItp6
|
||||||
|
wSOTp7orHuI0Aqd5o+G3RDpOy8X4iA6m5AU2S9BNTlLs84oOXZxQaQxP1DXHjFF0
|
||||||
|
ze7epPc6a63IC18v3hGQrWqxuUnAgfzGinmns/6DAdNPhQ4SJyMw5JgGaQjpi879
|
||||||
|
AiYZ0Sh7NYGrEqM7PJgue/hSvC1hAgMBAAECgYEAvZ05s0/4ZO493iM8LDgOoP7I
|
||||||
|
DTEdfL1Yuw19LtoY2GmYYJL5LqaTj0sfuMd7BRs+8YG4oHfxOFv01u34v/Z38vRa
|
||||||
|
E0tzSMVz29CRF0LUcko7c8Q2TzeTVsk3tEb3Kuf3tfVh5bhhjSakhUIY7D+Gb6LT
|
||||||
|
GRIUg72tP9wjayTDsCkCQQD6oyJqxUhxuuiL5D+e0ssp582YruEJK4f5CzRsjmVV
|
||||||
|
COBbqzRkIMCxPMCJCB+DbiMPlu/6CuxGh6i8rczDlkdfAkEA71SAt9PFx3hLW0hu
|
||||||
|
OBR9w7BF6B2YTR3cG5+SQXOiF1feMUIaAn/dDr1OxqAQ/hQ/QKUk5JDwquy6phYT
|
||||||
|
9qWDPwJBAPMqGLccBkgI/ZrTbIILov5aHdcXO88ow7f0jf0QPfG9NebZ+G94c1rB
|
||||||
|
RU7taZ2a2jtCxjqCJG/dJ/E+cZ4Ei+MCQFu3O3i2/FU7wU0jDbIKEEQc2j1gkgwD
|
||||||
|
hGVFmovgn15ouuqPlV4d1/4dCAJQNxLXeYHxh5jb/o7SF5ksXswnk4sCQA7Jlj5M
|
||||||
|
8+7qQV9DrOieFGpXMqlc9J6u9L0Tev2P9uE7a7z61NvLIdskGDpiaYOmADob5nHo
|
||||||
|
Duv3lSMQg6ql7EE=
|
||||||
|
-----END PRIVATE KEY-----
|
||||||
BIN
Lab11/CircuitPython/Code/lib/adafruit_connection_manager.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_connection_manager.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_dht.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_dht.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/__init__.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/__init__.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/authentication.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/authentication.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/exceptions.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/exceptions.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/headers.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/headers.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/interfaces.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/interfaces.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/methods.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/methods.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/mime_types.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/mime_types.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/request.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/request.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/response.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/response.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/route.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/route.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/server.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/server.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/status.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_httpserver/status.mpy
Executable file
Binary file not shown.
0
Lab11/CircuitPython/Code/lib/adafruit_io/__init__.py
Executable file
0
Lab11/CircuitPython/Code/lib/adafruit_io/__init__.py
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_io/adafruit_io.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_io/adafruit_io.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_io/adafruit_io_errors.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_io/adafruit_io_errors.mpy
Executable file
Binary file not shown.
0
Lab11/CircuitPython/Code/lib/adafruit_minimqtt/__init__.py
Executable file
0
Lab11/CircuitPython/Code/lib/adafruit_minimqtt/__init__.py
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_minimqtt/adafruit_minimqtt.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_minimqtt/adafruit_minimqtt.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_minimqtt/matcher.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_minimqtt/matcher.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_motor/__init__.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_motor/__init__.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_motor/motor.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_motor/motor.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_motor/servo.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_motor/servo.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_motor/stepper.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_motor/stepper.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_ntp.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_ntp.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_requests.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_requests.mpy
Executable file
Binary file not shown.
BIN
Lab11/CircuitPython/Code/lib/adafruit_ticks.mpy
Executable file
BIN
Lab11/CircuitPython/Code/lib/adafruit_ticks.mpy
Executable file
Binary file not shown.
177
Lab11/CircuitPython/Code/picowServo1.py
Executable file
177
Lab11/CircuitPython/Code/picowServo1.py
Executable file
|
|
@ -0,0 +1,177 @@
|
||||||
|
# SPDX-FileCopyrightText: 2021 jedgarpark for Adafruit Industries
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
# Pico servo demo
|
||||||
|
# Hardware setup:
|
||||||
|
# Servo on GP0 with external 5V power supply
|
||||||
|
# Button on GP3 and ground
|
||||||
|
|
||||||
|
import time
|
||||||
|
import board
|
||||||
|
from digitalio import DigitalInOut, Direction, Pull
|
||||||
|
import pwmio
|
||||||
|
from adafruit_motor import servo
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import ssl
|
||||||
|
import wifi
|
||||||
|
import socketpool
|
||||||
|
import microcontroller
|
||||||
|
import board
|
||||||
|
import busio
|
||||||
|
import adafruit_requests
|
||||||
|
import adafruit_dht
|
||||||
|
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
|
||||||
|
from microcontroller import cpu
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
print("Servo test")
|
||||||
|
mode = -1 # track state of button mode
|
||||||
|
|
||||||
|
def setupLED():
|
||||||
|
led = DigitalInOut(board.LED)
|
||||||
|
led.direction = Direction.OUTPUT
|
||||||
|
led.value = True
|
||||||
|
return led
|
||||||
|
|
||||||
|
def setupButton():
|
||||||
|
# Mode button setup
|
||||||
|
button = DigitalInOut(board.GP3)
|
||||||
|
button.direction = Direction.INPUT
|
||||||
|
button.pull = Pull.UP
|
||||||
|
return button
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def blink(led,count):
|
||||||
|
for _ in range(count):
|
||||||
|
led.value = False
|
||||||
|
time.sleep(0.1)
|
||||||
|
led.value = True
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
def setupServo():
|
||||||
|
# Servo setup
|
||||||
|
pwm_servo = pwmio.PWMOut(board.GP0, duty_cycle=2 ** 15, frequency=50)
|
||||||
|
servo1 = servo.Servo(pwm_servo, min_pulse=500, max_pulse=2200) # tune pulse for specific servo
|
||||||
|
|
||||||
|
return servo1
|
||||||
|
|
||||||
|
def setServoAngle(servo1,angle):
|
||||||
|
# Servo setup
|
||||||
|
print("servo set angle =",angle)
|
||||||
|
servo1.angle = angle
|
||||||
|
servoData = angle
|
||||||
|
data = [servoData]
|
||||||
|
# send sensor data to respective feeds
|
||||||
|
# io.send_data(picowServo,servoData)
|
||||||
|
io.send_data("myservo",angle)
|
||||||
|
print("sent")
|
||||||
|
|
||||||
|
return servo1
|
||||||
|
|
||||||
|
# Servo test
|
||||||
|
def servo_direct_test1():
|
||||||
|
print("Servo Direct Test 1")
|
||||||
|
setServoAngle(servo1,90)
|
||||||
|
time.sleep(2)
|
||||||
|
setServoAngle(servo1,0)
|
||||||
|
time.sleep(2)
|
||||||
|
setServoAngle(servo1,90)
|
||||||
|
time.sleep(2)
|
||||||
|
setServoAngle(servo1,180)
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
|
# Servo test
|
||||||
|
def servo_direct_test():
|
||||||
|
print("Servo Direct Test")
|
||||||
|
print("servo test: 90")
|
||||||
|
servo1.angle = 90
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 0")
|
||||||
|
servo1.angle = 0
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 90")
|
||||||
|
servo1.angle = 90
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 180")
|
||||||
|
servo1.angle = 180
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
|
# Servo smooth test
|
||||||
|
def servo_smooth_test():
|
||||||
|
counter = 1
|
||||||
|
print("servo smooth test: 180 - 0, -1º steps")
|
||||||
|
for angle in range(180, 0, -1): # 180 - 0 degrees, -1º at a time.
|
||||||
|
servo1.angle = angle
|
||||||
|
|
||||||
|
if(counter > 10): # This is used to rate limit the data to Adafruit
|
||||||
|
servoData = angle
|
||||||
|
data = [servoData]
|
||||||
|
# send sensor data to respective feeds
|
||||||
|
# io.send_data(picowServo,servoData)
|
||||||
|
io.send_data("myservo",angle)
|
||||||
|
print("sent ", angle)
|
||||||
|
counter = 1
|
||||||
|
else:
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
time.sleep(0.1)
|
||||||
|
# time.sleep(0.01)
|
||||||
|
time.sleep(1)
|
||||||
|
print("servo smooth test: 0 - 180, 1º steps")
|
||||||
|
for angle in range(0, 180, 1): # 0 - 180 degrees, 1º at a time.
|
||||||
|
servo1.angle = angle
|
||||||
|
time.sleep(0.01)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
def run_test(testnum):
|
||||||
|
if testnum is 0:
|
||||||
|
print("servo direct test")
|
||||||
|
servo_direct_test1()
|
||||||
|
elif testnum is 1:
|
||||||
|
print("servo direct test")
|
||||||
|
servo_smooth_test()
|
||||||
|
|
||||||
|
led = setupLED()
|
||||||
|
button = setupButton()
|
||||||
|
servo1 = setupServo()
|
||||||
|
print("Type of servo = ",type(servo))
|
||||||
|
|
||||||
|
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
|
||||||
|
|
||||||
|
aio_username = os.getenv('ADAFRUIT_IO_USERNAME')
|
||||||
|
aio_key = os.getenv('ADAFRUIT_IO_KEY')
|
||||||
|
|
||||||
|
pool = socketpool.SocketPool(wifi.radio)
|
||||||
|
requests = adafruit_requests.Session(pool, ssl.create_default_context())
|
||||||
|
# Initialize an Adafruit IO HTTP API object
|
||||||
|
io = IO_HTTP(aio_username, aio_key, requests)
|
||||||
|
print("connected to io")
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
# get feed
|
||||||
|
picowServo = io.get_feed("myservo")
|
||||||
|
except AdafruitIO_RequestError:
|
||||||
|
# if no feed exists, create one
|
||||||
|
print("Feed error")
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
# pack feed names into an array for the loop
|
||||||
|
feed_names = [picowServo]
|
||||||
|
print("Connected to feed.")
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if not button.value:
|
||||||
|
blink(led,2)
|
||||||
|
mode = (mode + 1) % 2
|
||||||
|
print("switch to mode %d" % (mode))
|
||||||
|
time.sleep(0.8) # big debounce
|
||||||
|
run_test(mode)
|
||||||
68
Lab11/CircuitPython/Code/picowServo2CloudTest.py
Executable file
68
Lab11/CircuitPython/Code/picowServo2CloudTest.py
Executable file
|
|
@ -0,0 +1,68 @@
|
||||||
|
# SPDX-FileCopyrightText: 2022 Liz Clark for Adafruit Industries
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import ssl
|
||||||
|
import wifi
|
||||||
|
import socketpool
|
||||||
|
import microcontroller
|
||||||
|
import board
|
||||||
|
import busio
|
||||||
|
import adafruit_requests
|
||||||
|
import adafruit_dht
|
||||||
|
from adafruit_io.adafruit_io import IO_HTTP, AdafruitIO_RequestError
|
||||||
|
from microcontroller import cpu
|
||||||
|
|
||||||
|
|
||||||
|
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD'))
|
||||||
|
|
||||||
|
aio_username = os.getenv('ADAFRUIT_IO_USERNAME')
|
||||||
|
aio_key = os.getenv('ADAFRUIT_IO_KEY')
|
||||||
|
|
||||||
|
pool = socketpool.SocketPool(wifi.radio)
|
||||||
|
requests = adafruit_requests.Session(pool, ssl.create_default_context())
|
||||||
|
# Initialize an Adafruit IO HTTP API object
|
||||||
|
io = IO_HTTP(aio_username, aio_key, requests)
|
||||||
|
print("connected to io")
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
# get feed
|
||||||
|
picowServo = io.get_feed("myservo")
|
||||||
|
except AdafruitIO_RequestError:
|
||||||
|
# if no feed exists, create one
|
||||||
|
print("Feed error")
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
# pack feed names into an array for the loop
|
||||||
|
feed_names = [picowServo]
|
||||||
|
print("Connected to feed.")
|
||||||
|
|
||||||
|
clock = 11
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# when the clock runs out..
|
||||||
|
print("") # Prints a blank line after clock print.
|
||||||
|
# read sensor
|
||||||
|
servoData = clock
|
||||||
|
data = [servoData]
|
||||||
|
# send sensor data to respective feeds
|
||||||
|
# io.send_data(picowServo,servoData)
|
||||||
|
io.send_data("myservo",servoData)
|
||||||
|
print("sent")
|
||||||
|
time.sleep(15)
|
||||||
|
# print sensor data to the REPL
|
||||||
|
print()
|
||||||
|
|
||||||
|
#time.sleep(1)
|
||||||
|
time.sleep(1)
|
||||||
|
print(clock)
|
||||||
|
clock = clock + 1
|
||||||
|
if clock >50:
|
||||||
|
clock = 11
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
88
Lab11/CircuitPython/Code/picowServoTest1.py
Executable file
88
Lab11/CircuitPython/Code/picowServoTest1.py
Executable file
|
|
@ -0,0 +1,88 @@
|
||||||
|
# SPDX-FileCopyrightText: 2021 jedgarpark for Adafruit Industries
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
# Pico servo demo
|
||||||
|
# Hardware setup:
|
||||||
|
# Servo on GP0 with external 5V power supply
|
||||||
|
# Button on GP3 and ground
|
||||||
|
|
||||||
|
import time
|
||||||
|
import board
|
||||||
|
from digitalio import DigitalInOut, Direction, Pull
|
||||||
|
import pwmio
|
||||||
|
from adafruit_motor import servo
|
||||||
|
|
||||||
|
print("Servo test")
|
||||||
|
|
||||||
|
led = DigitalInOut(board.LED)
|
||||||
|
led.direction = Direction.OUTPUT
|
||||||
|
led.value = True
|
||||||
|
|
||||||
|
|
||||||
|
def blink(times):
|
||||||
|
for _ in range(times):
|
||||||
|
led.value = False
|
||||||
|
time.sleep(0.1)
|
||||||
|
led.value = True
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
|
||||||
|
# Mode button setup
|
||||||
|
button = DigitalInOut(board.GP3)
|
||||||
|
button.direction = Direction.INPUT
|
||||||
|
button.pull = Pull.UP
|
||||||
|
mode = -1 # track state of button mode
|
||||||
|
|
||||||
|
# Servo setup
|
||||||
|
pwm_servo = pwmio.PWMOut(board.GP0, duty_cycle=2 ** 15, frequency=50)
|
||||||
|
servo1 = servo.Servo(
|
||||||
|
pwm_servo, min_pulse=500, max_pulse=2200
|
||||||
|
) # tune pulse for specific servo
|
||||||
|
|
||||||
|
|
||||||
|
# Servo test
|
||||||
|
def servo_direct_test():
|
||||||
|
print("servo test: 90")
|
||||||
|
servo1.angle = 90
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 0")
|
||||||
|
servo1.angle = 0
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 90")
|
||||||
|
servo1.angle = 90
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 180")
|
||||||
|
servo1.angle = 180
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
|
# Servo smooth test
|
||||||
|
def servo_smooth_test():
|
||||||
|
print("servo smooth test: 180 - 0, -1º steps")
|
||||||
|
for angle in range(180, 0, -1): # 180 - 0 degrees, -1º at a time.
|
||||||
|
servo1.angle = angle
|
||||||
|
time.sleep(0.01)
|
||||||
|
time.sleep(1)
|
||||||
|
print("servo smooth test: 0 - 180, 1º steps")
|
||||||
|
for angle in range(0, 180, 1): # 0 - 180 degrees, 1º at a time.
|
||||||
|
servo1.angle = angle
|
||||||
|
time.sleep(0.01)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
def run_test(testnum):
|
||||||
|
if testnum is 0:
|
||||||
|
print("servo direct test")
|
||||||
|
servo_direct_test()
|
||||||
|
elif testnum is 1:
|
||||||
|
print("servo smooth test")
|
||||||
|
servo_smooth_test()
|
||||||
|
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if not button.value:
|
||||||
|
blink(2)
|
||||||
|
mode = (mode + 1) % 2
|
||||||
|
print("switch to mode %d" % (mode))
|
||||||
|
time.sleep(0.8) # big debounce
|
||||||
|
run_test(mode)
|
||||||
117
Lab11/CircuitPython/Code/picowServoTest2.py
Executable file
117
Lab11/CircuitPython/Code/picowServoTest2.py
Executable file
|
|
@ -0,0 +1,117 @@
|
||||||
|
# SPDX-FileCopyrightText: 2021 jedgarpark for Adafruit Industries
|
||||||
|
# SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
# Pico servo demo
|
||||||
|
# Hardware setup:
|
||||||
|
# Servo on GP0 with external 5V power supply
|
||||||
|
# Button on GP3 and ground
|
||||||
|
|
||||||
|
import time
|
||||||
|
import board
|
||||||
|
from digitalio import DigitalInOut, Direction, Pull
|
||||||
|
import pwmio
|
||||||
|
from adafruit_motor import servo
|
||||||
|
|
||||||
|
print("Servo test")
|
||||||
|
mode = -1 # track state of button mode
|
||||||
|
|
||||||
|
def setupLED():
|
||||||
|
led = DigitalInOut(board.LED)
|
||||||
|
led.direction = Direction.OUTPUT
|
||||||
|
led.value = True
|
||||||
|
return led
|
||||||
|
|
||||||
|
def setupButton():
|
||||||
|
# Mode button setup
|
||||||
|
button = DigitalInOut(board.GP3)
|
||||||
|
button.direction = Direction.INPUT
|
||||||
|
button.pull = Pull.UP
|
||||||
|
return button
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def blink(led,count):
|
||||||
|
for _ in range(count):
|
||||||
|
led.value = False
|
||||||
|
time.sleep(0.1)
|
||||||
|
led.value = True
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
def setupServo():
|
||||||
|
# Servo setup
|
||||||
|
pwm_servo = pwmio.PWMOut(board.GP0, duty_cycle=2 ** 15, frequency=50)
|
||||||
|
servo1 = servo.Servo(pwm_servo, min_pulse=500, max_pulse=2200) # tune pulse for specific servo
|
||||||
|
|
||||||
|
return servo1
|
||||||
|
|
||||||
|
def setServoAngle(servo1,angle):
|
||||||
|
# Servo setup
|
||||||
|
print("servo set angle =",angle)
|
||||||
|
servo1.angle = angle
|
||||||
|
|
||||||
|
return servo1
|
||||||
|
|
||||||
|
# Servo test
|
||||||
|
def servo_direct_test1():
|
||||||
|
print("Servo Direct Test 1")
|
||||||
|
setServoAngle(servo1,90)
|
||||||
|
time.sleep(2)
|
||||||
|
setServoAngle(servo1,0)
|
||||||
|
time.sleep(2)
|
||||||
|
setServoAngle(servo1,90)
|
||||||
|
time.sleep(2)
|
||||||
|
setServoAngle(servo1,180)
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
|
# Servo test
|
||||||
|
def servo_direct_test():
|
||||||
|
print("Servo Direct Test")
|
||||||
|
print("servo test: 90")
|
||||||
|
servo1.angle = 90
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 0")
|
||||||
|
servo1.angle = 0
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 90")
|
||||||
|
servo1.angle = 90
|
||||||
|
time.sleep(2)
|
||||||
|
print("servo test: 180")
|
||||||
|
servo1.angle = 180
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
|
# Servo smooth test
|
||||||
|
def servo_smooth_test():
|
||||||
|
print("servo smooth test: 180 - 0, -1º steps")
|
||||||
|
for angle in range(180, 0, -1): # 180 - 0 degrees, -1º at a time.
|
||||||
|
servo1.angle = angle
|
||||||
|
time.sleep(0.01)
|
||||||
|
time.sleep(1)
|
||||||
|
print("servo smooth test: 0 - 180, 1º steps")
|
||||||
|
for angle in range(0, 180, 1): # 0 - 180 degrees, 1º at a time.
|
||||||
|
servo1.angle = angle
|
||||||
|
time.sleep(0.01)
|
||||||
|
time.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
def run_test(testnum):
|
||||||
|
if testnum is 0:
|
||||||
|
print("servo direct test")
|
||||||
|
servo_direct_test1()
|
||||||
|
elif testnum is 1:
|
||||||
|
print("servo smooth test")
|
||||||
|
servo_smooth_test()
|
||||||
|
|
||||||
|
led = setupLED()
|
||||||
|
button = setupButton()
|
||||||
|
servo1 = setupServo()
|
||||||
|
print("Type of servo = ",type(servo))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
if not button.value:
|
||||||
|
blink(led,2)
|
||||||
|
mode = (mode + 1) % 2
|
||||||
|
print("switch to mode %d" % (mode))
|
||||||
|
time.sleep(0.8) # big debounce
|
||||||
|
run_test(mode)
|
||||||
7
Lab11/CircuitPython/Code/settings.toml
Executable file
7
Lab11/CircuitPython/Code/settings.toml
Executable file
|
|
@ -0,0 +1,7 @@
|
||||||
|
CIRCUITPY_WIFI_SSID = "cset@stcc"
|
||||||
|
CIRCUITPY_WIFI_PASSWORD = "c1s2e3t4"
|
||||||
|
|
||||||
|
|
||||||
|
# Replace the next two lines with your Adafruit username and key.
|
||||||
|
ADAFRUIT_IO_USERNAME = "csetuser"
|
||||||
|
ADAFRUIT_IO_KEY = "aio_whil751ZM389iWH4GR20r90cTb0R"
|
||||||
10
Lab11/CircuitPython/README.md
Executable file
10
Lab11/CircuitPython/README.md
Executable file
|
|
@ -0,0 +1,10 @@
|
||||||
|
# CircuitPython Files
|
||||||
|
|
||||||
|
---
|
||||||
|
Directories & Files
|
||||||
|
---
|
||||||
|
CircuitPython: CircuitPython firmware for the Pico 2 W
|
||||||
|
|
||||||
|
Code: CircuitPython code for the lab.
|
||||||
|
|
||||||
|
Documentation: Any additional documentation files.
|
||||||
8
Lab11/Doumentation/README.md
Normal file
8
Lab11/Doumentation/README.md
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# Lab08 Documentation
|
||||||
|
Introduction to the Pico W.
|
||||||
|
|
||||||
|
Pin diagrams, web links, and other information.
|
||||||
|
---
|
||||||
|
Directories & Files
|
||||||
|
---
|
||||||
|
RP-008315-DS-1-PicoW-A4-Pinout.pdf: Pinout diagram for the Pico 2 W.
|
||||||
7763
Lab11/Doumentation/RP-008315-DS-1-PicoW-A4-Pinout.pdf
Normal file
7763
Lab11/Doumentation/RP-008315-DS-1-PicoW-A4-Pinout.pdf
Normal file
File diff suppressed because one or more lines are too long
24
Lab11/README.md
Normal file
24
Lab11/README.md
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
# Lab 11
|
||||||
|
## Servo Control Dashboard
|
||||||
|
|
||||||
|
### Overview
|
||||||
|
Use the Adafruit dashboard to control a servo motor. The dashboard can be used to push position information to remote IoT nodes.
|
||||||
|
|
||||||
|
Important: **This project uses CircuitPython.** Nuke the Pico to erase any MicroPython installation. Drag and drop the appropriate CircuitPython version to the Pico W or Pico 2W.
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
Directories & Files
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
CircuitPython: CircuitPython code and documentation. It includes the CircuitPython Versions for the Pico W and Pico 2 W.
|
||||||
|
|
||||||
|
flash_nuke.uf2: Erases all existing firmware on the Pico W.
|
||||||
|
universal_flash_nuke.uf2: Erases all existing firmware on the Pico 2 W.
|
||||||
|
|
||||||
|
|
||||||
|
CircuitPython/code: Working code for the lab. Drag and drop all code and directories to the Pico. CicuitPython provides a removeable disk access to the Pico.
|
||||||
|
|
||||||
|
Documentation: Pin diagrams, web links, and other information.
|
||||||
|
|
||||||
BIN
Lab11/adafruit-circuitpython-raspberry_pi_pico_w-en_US-9.0.2.uf2
Normal file
BIN
Lab11/adafruit-circuitpython-raspberry_pi_pico_w-en_US-9.0.2.uf2
Normal file
Binary file not shown.
BIN
Lab11/flash_nuke.uf2
Normal file
BIN
Lab11/flash_nuke.uf2
Normal file
Binary file not shown.
BIN
Lab11/universal_flash_nuke.uf2
Normal file
BIN
Lab11/universal_flash_nuke.uf2
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue