-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWebServerCreator.py
90 lines (78 loc) · 3.14 KB
/
WebServerCreator.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
import pathlib, json
with open('config.json', 'r') as jsonFile:
config = json.loads(jsonFile.read())
result = open("webserver.cpp","w+")
pages = []
# Writing required variables and setting up library for result file
ssid=config["SSID"]
password = config["Password"]
port = config["Port"]
result.write(
"""#include <Arduino.h>
#include <ESP8266WebServer.h>
const char* ssid = \"%s\";
const char* password = \"%s\";
ESP8266WebServer server(%d);\n\n\n
""" % (ssid,password,port))
# Searching for every file in pages folder.
try:
for path in pathlib.Path('pages').iterdir():
if path.is_file():
if path.name.endswith('.html'): # Checking if file is a HTML file.
currentFile = open(path, "a+") # Opening file
pageName = currentFile.name.rsplit('\\')[1].rsplit('.')[0] # Getting file name without directory and "/".
pages.append(pageName) # Adding file name to a list for final declaration.
result.write("void "+ pageName +"(){\nchar *" + pageName + " = ") # Converting file to a char variable
try:
x = 1
currentFile.seek(0)
fileLength = sum(1 for line in currentFile)
currentFile.seek(0)
for line in currentFile: # A loop for every line
strippedLine = line.strip() # Clearing empty lines
if strippedLine != "": # Checking if the line is empty
editedLine = strippedLine.replace("\"","\'")
result.write("\"" + editedLine)
currentLine = result.tell()
result.seek(currentLine)
if x == fileLength: # Checking if the line is the last line
result.write("\";\n\nserver.send(200, \"text/html\", "+ pageName +");\n}\n\n")
else: # If not, it won't add server.send(...).
result.write("\"\n")
x = x+1
else:
x = x+1
finally:
currentFile.close()
print(pageName + ' page is finished.')
else:
print("Skipped non HTML file")
finally:
print("Proccess ended, Web Server complete.")
# Setup of result file
result.write(
'void setup() {'
+ '\n\tSerial.begin('
+ config["BaudRate"] + ');'
+ '\n\tdelay(10);'
+ '\n\tWiFi.begin(ssid,password);'
+ '\n\twhile(WiFi.status() != WL_CONNECTED){'
+ '\n\t\tSerial.print(\"Connection failed, trying again.\\n\");'
+ '\n\t\tdelay(500);'
+ '\n\t}'
+ '\n\tSerial.println(\"Connected Succesfully.\\n\");'
+ '\n\tserver.begin();'
+ '\n\tSerial.print("Server is live use http://");'
+ '\n\tSerial.print(WiFi.localIP());'
+ '\n\tSerial.print("/");'
+ '\n\n\tserver.begin();\n'
)
# Declare every page
for x in pages:
result.write("\tserver.on("+ "\"/" + x + "\", " + x +");\n")
result.write("}\n\n\n")
# Loop of result file
result.write("void loop() {"
+ "\n\tserver.handleClient();"
+ "\n\tdelay(1000);"
+ "\n}")