2023-03-16 09:53:18 +00:00
|
|
|
import usocket as socket
|
|
|
|
import ssl
|
2023-03-23 21:37:43 +00:00
|
|
|
from machine import Pin
|
|
|
|
import time
|
2023-03-16 09:53:18 +00:00
|
|
|
|
2023-03-16 11:47:45 +00:00
|
|
|
with open('token') as f:
|
|
|
|
token = f.readline().strip('\n')
|
2023-03-16 09:53:18 +00:00
|
|
|
|
2023-03-16 11:47:45 +00:00
|
|
|
|
|
|
|
def send_msg(text, title=""):
|
2023-03-16 09:53:18 +00:00
|
|
|
s = socket.socket()
|
|
|
|
|
|
|
|
host = "ntfy.acorneroftheweb.com"
|
|
|
|
port = 443
|
|
|
|
|
|
|
|
ai = socket.getaddrinfo(host, port)
|
|
|
|
print("Address infos:", ai)
|
|
|
|
addr = ai[0][-1]
|
|
|
|
|
|
|
|
print("Connect address:", addr)
|
|
|
|
s.connect(addr)
|
|
|
|
|
|
|
|
s = ssl.wrap_socket(s)
|
|
|
|
|
|
|
|
headers = """\
|
|
|
|
POST / HTTP/1.1\r
|
|
|
|
Host: {host}\r
|
|
|
|
Content-Type: application/json\r
|
|
|
|
Authorization: Bearer {token}
|
|
|
|
Accept: */*\r
|
2023-03-16 11:47:45 +00:00
|
|
|
Connection: close\r
|
2023-03-16 09:53:18 +00:00
|
|
|
Content-Length: {content_length}\r
|
|
|
|
\r\n"""
|
2023-03-16 11:47:45 +00:00
|
|
|
body = '{"topic": "d1", "message": "' + text + '", "title": "' + title + '"}'
|
2023-03-16 09:53:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
body_bytes = body.encode('ascii')
|
|
|
|
header_bytes = headers.format(
|
|
|
|
content_length=len(body_bytes),
|
|
|
|
token=token,
|
|
|
|
host=str(host)
|
|
|
|
).encode('iso-8859-1')
|
|
|
|
|
|
|
|
payload = header_bytes + body_bytes
|
|
|
|
|
|
|
|
print(payload)
|
|
|
|
|
|
|
|
s.write(payload)
|
2023-03-16 11:47:45 +00:00
|
|
|
print(s.read())
|
2023-03-16 09:53:18 +00:00
|
|
|
|
|
|
|
s.close()
|
|
|
|
|
2023-03-23 21:37:43 +00:00
|
|
|
def simple_msg():
|
|
|
|
send_msg("Hello there from the button")
|
2023-03-16 09:53:18 +00:00
|
|
|
|
2023-03-16 11:47:45 +00:00
|
|
|
def main():
|
2023-03-23 21:37:43 +00:00
|
|
|
p2 = Pin(2, Pin.IN)
|
|
|
|
while True:
|
|
|
|
cur_value = p2.value()
|
|
|
|
active = 0
|
|
|
|
while active < 20:
|
|
|
|
if p2.value() != cur_value:
|
|
|
|
active += 1
|
|
|
|
else:
|
|
|
|
active = 0
|
|
|
|
time.sleep_ms(1)
|
|
|
|
simple_msg()
|
2023-03-16 11:47:45 +00:00
|
|
|
|
2023-03-16 09:53:18 +00:00
|
|
|
main()
|