Created
December 7, 2022 02:19
-
-
Save Akkiesoft/66a2331bca3705b078f990557b74061d to your computer and use it in GitHub Desktop.
玄関の電気のスイッチをオン・オフしたかった.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import board | |
import busio | |
from digitalio import DigitalInOut | |
# for Web Server | |
from adafruit_wsgi.wsgi_app import WSGIApp | |
# for ESP32 SPI | |
from adafruit_esp32spi import adafruit_esp32spi | |
import adafruit_esp32spi.adafruit_esp32spi_wsgiserver as server | |
import adafruit_esp32spi.adafruit_esp32spi_wifimanager as wifimanager | |
# for motor | |
import time | |
import pwmio | |
from adafruit_motor import servo | |
# Get wifi details and more from a secrets.py file | |
try: | |
from secrets import secrets | |
except ImportError: | |
print("WiFi secrets are kept in secrets.py, please add them there!") | |
raise | |
esp32_cs = DigitalInOut(board.GP7) | |
esp32_ready = DigitalInOut(board.GP10) | |
esp32_reset = DigitalInOut(board.GP11) | |
spi = busio.SPI(board.GP18, board.GP19, board.GP16) | |
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) | |
ssid_list = esp.scan_networks() | |
for secret in secrets: | |
print("Checking SSID:" + secret["ssid"]) | |
for i in ssid_list: | |
if secret["ssid"] in i["ssid"]: | |
wifi = wifimanager.ESPSPI_WiFiManager(esp, secret) | |
wifi.connect() | |
print("open this IP in your browser: ", esp.pretty_ip(esp.ip_address)) | |
break | |
status = { | |
200: "200 OK", | |
404: "404 Not Found", | |
405: "405 Method Not Allowed" | |
} | |
content_type = { | |
"html": "text/html", | |
"text": "text/plain" | |
} | |
indexhtml = """ | |
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><title>entrance room light</title></head><body><h1>entrance room light</h1></body></html> | |
""" | |
pwm = pwmio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50) | |
# Create a servo object, my_servo. | |
my_servo = servo.Servo(pwm) | |
my_servo.angle = 90 | |
class SimpleWSGIApplication: | |
def __init__(self): | |
self._listeners = {} | |
self._start_response = None | |
def __call__(self, environ, start_response): | |
self._start_response = start_response | |
path = environ["PATH_INFO"] | |
# Method not allowed except GET | |
if environ["REQUEST_METHOD"] is not 'GET': | |
status_code = 405 | |
resp_data = "Method Not Allowed" | |
# define urls. | |
elif path == "/": | |
status_code = 200 | |
filetype = "html" | |
resp_data = indexhtml | |
elif path == "/off": | |
status_code = 200 | |
filetype = "text" | |
my_servo.angle = 50 | |
time.sleep(0.5) | |
my_servo.angle = 90 | |
resp_data = "off" | |
elif path == "/on": | |
status_code = 200 | |
filetype = "text" | |
my_servo.angle = 130 | |
time.sleep(0.5) | |
my_servo.angle = 90 | |
resp_data = "on" | |
else: | |
status_code = 404 | |
filetype = "text" | |
resp_data = "Not Found." | |
# show log | |
length = len(resp_data) | |
print('"%s %s %s" %i %i' % ( | |
environ["REQUEST_METHOD"], | |
environ["PATH_INFO"], | |
environ["SERVER_PROTOCOL"], | |
status_code, | |
length)) | |
headers = [("Content-Type", content_type[filetype]), ("Content-Length", length)] | |
self._start_response(status[status_code], headers) | |
return resp_data | |
server.set_interface(esp) | |
wsgiServer = server.WSGIServer(80, application=SimpleWSGIApplication()) | |
wsgiServer.start() | |
while True: | |
# Our main loop where we have the server poll for incoming requests | |
try: | |
wsgiServer.update_poll() | |
# Could do any other background tasks here, like reading sensors | |
except (ValueError, RuntimeError) as e: | |
print("Failed to update server, restarting ESP32\n", e) | |
wifi.reset() | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment