-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.py
More file actions
204 lines (151 loc) · 5.61 KB
/
boot.py
File metadata and controls
204 lines (151 loc) · 5.61 KB
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
# _____ _ _ _
# | ____| _ __ ___ (_)| |_ | |_ ___ _ __
# | _| | '_ ` _ \ | || __|| __|/ _ \| '__|
# | |___ | | | | | || || |_ | |_| __/| |
# |_____||_| |_| |_||_| \__| \__|\___||_|
#
from machine import Pin, Timer
import time
import network
import Webserver
import _thread
print("Booting Emitter")
#=============Preamble=================
# Transmit Options
dtr = 8 # Data Transfer Rate in bit/s
END_SYMBOLE = int('00000100', 2) # 00000100
is_emitting = False
adjust_mode = False
time_remaining = 0
transmitting_text = ""
# Pins
onboard_led = Pin("LED", Pin.OUT)
indicator_led = Pin(13, Pin.OUT)
laser = Pin(14, Pin.OUT)
# Network
SSID = 'Emitter'
#==============Functions==============
# Function that sends an Byte over the Laser Module
def send(recived_bytes):
global laser, time, onboard_led, indicator_led, is_emitting, send_thread, time_remaining
is_emitting = True
print("Start sending Message over Laser. Aproximate Duration: {0}s".format(len(recived_bytes) * 8 /dtr))
#onboard_led.on() // Cant Turn on the Onboard LED on becouse the Web Server would crash
print("\n")
# Cycle through bytes
for byte in recived_bytes:
# Send first Byte to indicate new Package containing one byte
print("New Packadge:\t1")
laser.value(1)
indicator_led.value(1)
time.sleep(1/dtr)
print("Byte: '{1}' ({0})".format(byte, chr(byte)))
# Cycle through byte, start by highest
for i in range(8):
# Sees if bit i is set, returns true if set
bit = (byte & 1<<(7 - i)) != 0
print("{0}".format(int(bit)), end='')
# Sed Laser
laser.value(bit)
indicator_led.value(bit)
#
time_remaining = time_remaining - 1/dtr
time.sleep(1/dtr)
# Pull low to ensure Package bit is send
laser.off()
indicator_led.off()
time.sleep(1/dtr)
print('\n')
is_emitting = False
print("\nFinished sending the Message.")
#onboard_led.off()
return
def webResponseGenerator(header, path, properties):
global dtr, is_emitting, send_thread, adjust_mode, time_remaining, transmitting_text
# Response Page
if path is '/response':
if is_emitting:
# Redirekt to Status Page
print("Allready Transmitting, Redirektion to Status Page")
return 'HTTP/1.1 303 Allready Transmitting\r\nLocation: /status\r\n\r\n'
# If there is no Data to be Send there is no response Site to display, ignoring Request
if('data' not in properties.keys()):
print("No Data! Ignoring Request!")
return ''
print('Got following Data: "{0}"'.format(properties['data']))
transmitting_text = properties['data']
# Convert to byte Array
msg_bytes = bytearray(properties['data'], 'utf-8')
print(msg_bytes)
# Append End Symbol
msg_bytes.append(END_SYMBOLE)
# Send Message via Laser
_thread.start_new_thread(send, [msg_bytes])
# Send HTML with Answer
time_remaining = len(properties['data']) * 8 / dtr
page_data = {
"dtr":str(dtr),
"time":str(time_remaining) + ' Sekunden',
"text":properties['data'],
}
response_html_file = open("response.html")
response_html = response_html_file.read()
response_html_file.close()
response = Webserver.generateResponse(response_html, page_data)
return response
# Settings Page
if path is '/settings':
setSettings = properties.keys()
# Set all Settings
if('adjust_mode' in setSettings): # Ajustier Modus
state = properties['adjust_mode'] is 'on'
adjust_mode = state
laser.value(state)
indicator_led.value(state)
print("Setting Adjust Mode to {0}".format(state))
if('dtr' in setSettings and properties['dtr'] != ''): # Data Transfer Rate
value = float(properties['dtr'])
# Data Transfer Rate must be grater than 0
if value > 0:
dtr = value
print("Set Data Transfer Rate to {0}".format(dtr))
# Read settings.html
settings_file = open('settings.html')
response = settings_file.read()
settings_file.close()
page_data = {
"dtr":str(dtr),
}
# Return Connection
return Webserver.generateResponse(response, page_data)
# Status Page
if path is '/status':
page_data = {
"is_emitting":str(is_emitting),
"dtr":str(dtr),
"text": transmitting_text,
"time_remaining": str(time_remaining) + ' seconds',
}
status_file = open("status.html")
status_html = status_file.read()
status_file.close()
return Webserver.generateResponse(status_html, page_data)
# Default
index_file = open("index.html")
index_html = index_file.read()
index_file.close()
return Webserver.generateResponse(index_html)
#================Start================
laser.off()
onboard_led.off()
# Setup
ap = network.WLAN(network.AP_IF)
ap.config(essid=SSID, password="emitterAP")
ap.active(True)
print("\n")
# Waiting for AP to be ready
while ap.active() == False:
pass
print ('AP Mode Is Active, You can Now Connect')
print('IP Address To Connect to:: ' + ap.ifconfig () [0])
Webserver.start(webResponseGenerator)