-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
249 lines (226 loc) · 6.87 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import network
import socket
from machine import Pin
from utime import sleep
import uasyncio as asyncio
#===========================cw=======================
# setup LED
redLED = Pin(16, Pin.OUT)
power_reset = Pin(17, Pin.OUT)
# create needed sleep times for 10 words per minute dot = 0.12 seconds
# 5 words per minute is 0.24 seconds
dot = 0.06
dash = 3 * dot
withinChar = dot
betChar = 3 * dot
betWord = 7 * dot
beacon_delay = 10
def speed(newdot):
global dot
global dash
global withinChar
global betChar
global betWord
global beacon_delay
dot = newdot
dash = 3 * dot
withinChar = dot
betChar = 3 * dot
betWord = 7 * dot
# make sure LED is off
redLED.off()
power_reset.off()
# dictionary for letters and numbers linked to the Morse code for that character
morseCode = {
'a' : '.-', 'b' : '-...', 'c' : '-.-.',
'd' : '-..', 'e' : '.', 'f' : '..-.',
'g' : '--.', 'h' : '....', 'i' : '..',
'j' : '.---', 'k' : '-.-', 'l' : '.-..',
'm' : '--', 'n' : '-.', 'o' : '---',
'p' : '.--.', 'q' : '--.-', 'r' : '.-.',
's' : '...', 't' : '-', 'u' : '..-',
'v' : '...-', 'w' : '.--', 'x' : '-..-',
'y' : '-.--', 'z' : '--..', '1' : '.----',
'2' : '..---', '3' : '...--', '4' : '....-',
'5' : '.....', '6' : '-....', '7' : '--...',
'8' : '---..', '9' : '----.', '0' : '-----',
'/' : '-..-.', '?' : '..--..'
}
lstmsg = 'test'
# function for blinking the LED for a particular letter or number
def charBlinks(char):
global lstmsg
global dot
# if the character is a space, sleep the between word time
if char == ' ':
# assuming that the space is inside the message (not the first character)
# for a space, we need to sleep "betWord - 3*dot" since the blinking code always sleeps
# betChar (=3*dot) at the end of each character
sleep(betWord - 3*dot)
if char == 'P':
power_reset.on()
sleep(8*dot)
power_reset.off()
lstmsg = 'P'
if char == 'S':
speed(dot+0.005)
lstmsg = 'S'
print(str(dot))
return
if char == 'F':
speed(dot-0.005)
lstmsg = 'F'
return
elif char == 'D':
if lstmsg == 'D':
return
redLED.on()
lstmsg = 'D'
print('D')
return
elif char == 'U':
if lstmsg == 'U':
return
redLED.off()
lstmsg = 'U'
print('U')
return
else:
lstmsg = 'M'
# look up character in morseCode dictionary - make lowercase if needed
mCode = morseCode.get(char.lower())
# if the code is found - blink the code
if mCode:
print(char, mCode)
# need to know number of dot/dashes to do the between character timing
lenCode = len(mCode)
# counter to know when we get to the last dot/dash in mCode
count = 0
for symbol in mCode:
# tract place in mCode
count += 1
if symbol == '.':
# blink a dot
redLED.on()
sleep(dot)
redLED.off()
if count == lenCode:
# character blinks finished - sleep the between character time
sleep(betChar)
else:
sleep(withinChar)
if symbol == '-':
# blink a dash
redLED.on()
sleep(dash)
redLED.off()
if count == lenCode:
# character blinks finished - sleep the between character time
sleep(betChar)
else:
sleep(withinChar)
else:
# print this if the code for the character is not found
print('No Morse code for this character:', char)
#===========================cw======================
led = machine.Pin('LED', machine.Pin.OUT)
html = """<!DOCTYPE html>
<html>
<head> <title>Pico W</title>
</head>
<body> <h1>Pico W</h1>
<p>%s</p>
</body>
</html>
"""
def webpage(temperature, state):
#Template HTML
html = f"""
<!DOCTYPE html>
<html>
<head>
</head>
<form action="./on">
<input type="text" id="msg" name="msg"><br><br>
<input type="submit" value="Light on" /><br>
</form>
<form action="./off">
<input type="submit" value="Light off" />
</form>
<p>LED is {state}</p>
<p>Temperature is {temperature}</p>
</body>
</html>
"""
return(html)
async def send_msg(msg):
for char in msg:
charBlinks(char)
async def send_sk(cwmsg):
keylist = cwmsg.split(" ")
cw = 1
for pdl in keylist:
if cw == 1:
redLED.on()
sleep(float(pdl)/1000)
cw = 0
else:
redLED.off()
sleep(float(pdl)/1000)
cw = 1
redLED.off()
ssid = 'picok'
password = ''
# Just making our internet connection
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
while ap.active() == False:
pass
print('AP Mode Is Active, You can Now Connect')
print('IP Address To Connect to:: ' + ap.ifconfig()[0])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', 80))
s.listen(5)
asyncio.run(send_msg("connected"))
# Listen for connections
while True:
try:
cl, addr = s.accept()
print('client connected from', addr)
request = cl.recv(2048)
print(request)
request = str(request)
led_on = request.find('/light/on')
sk_go = request.find('/light/skgo')
led_off = request.find('/light/off')
m_start = request.find('msg')
m_end = request.find(' HTTP')
msg = ""
if(m_end != -1):
msg = request[m_start+4:m_end]
msg = msg.replace("%2F", "/")
msg = msg.replace("+", " ")
msg = msg.replace("%3F", "?")
if led_on == 6:
#output morse code
response = webpage("0", msg)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
asyncio.run(send_msg(msg))
led.value(1)
msg = ""
elif sk_go == 6:
response = webpage("0", msg)
cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
cl.send(response)
cl.close()
asyncio.run(send_sk(msg[:-1]))
led.value(1)
msg = ""
except OSError as e:
#s.close()
cl.close()
print('connection closed ' + str(e))