Browse Source

Added stylesheet and favicon. Static data put into own auto-generated header.

Thomas Buck 7 years ago
parent
commit
63082b2a12
9 changed files with 332 additions and 18 deletions
  1. 43
    8
      ESP-Weather.ino
  2. 2
    0
      README.md
  3. 14
    1
      client-script.js
  4. 12
    0
      client-style.css
  5. 1
    6
      config.h
  6. 64
    3
      convert-static.py
  7. BIN
      favicon.ico
  8. 194
    0
      static.h
  9. 2
    0
      template.html

+ 43
- 8
ESP-Weather.ino View File

@@ -25,8 +25,11 @@
25 25
 #include <WiFiManager.h>
26 26
 #include "config.h"
27 27
 #include "ntp.h"
28
+#include "static.h"
28 29
 #include "storage.h"
29 30
 
31
+#define DEBUG
32
+
30 33
 SHT21 sensor;
31 34
 ESP8266WebServer server(WEB_PORT);
32 35
 WiFiServer serverSocket(WEBSOCKET_PORT);
@@ -41,8 +44,10 @@ byte storeAtBoot = 1;
41 44
 unsigned long lastTime = 0;
42 45
 bool waitingForReplies = false;
43 46
 
44
-void handleRoot() {
47
+static void handleRoot() {
48
+#ifdef DEBUG
45 49
     Serial.println("Sending UDP Broadcast...");
50
+#endif // DEBUG
46 51
 
47 52
     // Send UDP broadcast to other modules
48 53
     udp.beginPacket(broadcastIP, BROADCAST_PORT);
@@ -54,12 +59,21 @@ void handleRoot() {
54 59
     waitingForReplies = true;
55 60
 }
56 61
 
57
-void handleJS() {
62
+static void handleJS() {
58 63
     String message = F(JS_FILE);
59 64
     server.send(200, "text/javascript", message);
60 65
 }
61 66
 
62
-void handleNotFound() {
67
+static void handleCSS() {
68
+    String message = F(CSS_FILE);
69
+    server.send(200, "text/css", message);
70
+}
71
+
72
+static void handleFavicon() {
73
+    server.send_P(200, faviconMimeType, (PGM_P)favicon, faviconSize);
74
+}
75
+
76
+static void handleNotFound() {
63 77
     String message = "File Not Found\n\n";
64 78
     message += "URI: ";
65 79
     message += server.uri();
@@ -75,10 +89,12 @@ void handleNotFound() {
75 89
 }
76 90
 
77 91
 void setup(void) {
78
-    // Debugging
79
-    Serial.begin(115200);
92
+    Serial.begin(DEBUG_BAUDRATE);
93
+
94
+#ifdef DEBUG
80 95
     Serial.println();
81 96
     Serial.println("ESP-Weather init...");
97
+#endif // DEBUG
82 98
 
83 99
     //sensor.begin();
84 100
     // The SHT library is simpy calling Wire.begin(), but the default
@@ -100,28 +116,36 @@ void setup(void) {
100 116
     if (WiFi.status() != WL_CONNECTED) {
101 117
         while (WiFi.status() != WL_CONNECTED) {
102 118
             delay(500);
119
+#ifdef DEBUG
103 120
             Serial.print(".");
121
+#endif // DEBUG
104 122
         }
123
+#ifdef DEBUG
105 124
         Serial.println("");
125
+#endif // DEBUG
106 126
     }
107 127
 
128
+#ifdef DEBUG
108 129
     Serial.print("IP address: ");
109 130
     Serial.println(WiFi.localIP());
131
+#endif // DEBUG
110 132
 
111 133
     broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP();
112 134
 
113 135
     server.on("/", handleRoot);
114 136
     server.on("/index.html", handleRoot);
115 137
     server.on("/view.js", handleJS);
138
+    server.on("/style.css", handleCSS);
139
+    server.on("/favicon.ico", handleFavicon);
116 140
     server.onNotFound(handleNotFound);
117 141
     server.begin();
118
-
119 142
     serverSocket.begin();
120
-
121 143
     ntpInit();
122
-
123 144
     udp.begin(BROADCAST_PORT);
145
+
146
+#ifdef DEBUG
124 147
     Serial.println("ESP-Weather ready!");
148
+#endif // DEBUG
125 149
 }
126 150
 
127 151
 void loop(void) {
@@ -152,8 +176,10 @@ void loop(void) {
152 176
         }
153 177
         json += "]}";
154 178
 
179
+#ifdef DEBUG
155 180
         Serial.println("WebSocket Response:");
156 181
         Serial.println(json);
182
+#endif // DEBUG
157 183
 
158 184
         webSocketServer.sendData(json);
159 185
         client.flush();
@@ -163,7 +189,9 @@ void loop(void) {
163 189
     // EEPROM-Schreiben jede Stunde
164 190
     if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
165 191
             && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
192
+#ifdef DEBUG
166 193
         Serial.println("Storing new data packet...");
194
+#endif // DEBUG
167 195
         lastStorageTime = millis();
168 196
         storeAtBoot = 0;
169 197
         if (storage.header.count < MAX_STORAGE) {
@@ -188,11 +216,15 @@ void loop(void) {
188 216
             packetBuffer[len] = 0;
189 217
         }
190 218
 
219
+#ifdef DEBUG
191 220
         Serial.print("Got UDP packet: ");
192 221
         Serial.println(packetBuffer);
222
+#endif // DEBUG
193 223
 
194 224
         if (strcmp(packetBuffer, UDP_PING_CONTENTS) == 0) {
225
+#ifdef DEBUG
195 226
             Serial.println("Broadcast");
227
+#endif // DEBUG
196 228
             udp.beginPacket(udp.remoteIP(), udp.remotePort());
197 229
             udp.print(UDP_ECHO_CONTENTS);
198 230
             udp.endPacket();
@@ -202,7 +234,10 @@ void loop(void) {
202 234
     }
203 235
 
204 236
     if (((millis() - lastTime) >= MAX_BROADCAST_WAIT_TIME) && (waitingForReplies == true)) {
237
+#ifdef DEBUG
205 238
         Serial.println("Timeout, sending response...");
239
+#endif // DEBUG
240
+
206 241
         waitingForReplies = false;
207 242
         String message = F(HTML_BEGIN);
208 243
         message += "var clients = Array(";

+ 2
- 0
README.md View File

@@ -4,6 +4,8 @@ This [started out as a project](https://github.com/g40st/Systemadministration) f
4 4
 
5 5
 ## Dependencies
6 6
 
7
+The included Favicon is ['weather' by 'jkeks'](http://www.favicon.cc/?action=icon&file_id=757061).
8
+
7 9
 ### ESP8266
8 10
 
9 11
 Download and install the following libraries to your `Arduino/libraries` folder. Of course, you also need the [Arduino core for the ESP8266 WiFi chip](https://github.com/esp8266/Arduino) installed from the IDE Board Manager.

+ 14
- 1
client-script.js View File

@@ -1,3 +1,15 @@
1
+// This is the client-side Javascript that receives the IPs of all available
2
+// local ESP-Weather modules, reads their values using the Websocket interface
3
+// and renders the graphs to visualize them.
4
+//
5
+// ----------------------------------------------------------------------------
6
+// "THE BEER-WARE LICENSE" (Revision 42):
7
+// <xythobuz@xythobuz.de> & <ghost-ghost@web.de> wrote this file.  As long as
8
+// you retain this notice you can do whatever you want with this stuff. If we
9
+// meet some day, and you think this stuff is worth it, you can buy us a beer
10
+// in return.                                   Thomas Buck & Christian Högerle
11
+// ----------------------------------------------------------------------------
12
+
1 13
 var arrSensor = Array();
2 14
 
3 15
 textAvailableSensors = "Available Sensors";
@@ -8,7 +20,7 @@ $(document).ready(function() {
8 20
         <div class="row" id="contentDiv">
9 21
             <div class="col-md-5 col-lg-5">
10 22
                 <div class="panel panel-primary">
11
-                    <div class="panel-heading" id="listSensorsHeading" style="font-size: 18px;">
23
+                    <div class="panel-heading" id="listSensorsHeading">
12 24
                         ` + textAvailableSensors + ` (0/0)
13 25
                     </div>
14 26
                     <div class="panel-body">
@@ -232,3 +244,4 @@ function getRandomColor() {
232 244
     }
233 245
     return color;
234 246
 }
247
+

+ 12
- 0
client-style.css View File

@@ -0,0 +1,12 @@
1
+// ----------------------------------------------------------------------------
2
+// "THE BEER-WARE LICENSE" (Revision 42):
3
+// <xythobuz@xythobuz.de> & <ghost-ghost@web.de> wrote this file.  As long as
4
+// you retain this notice you can do whatever you want with this stuff. If we
5
+// meet some day, and you think this stuff is worth it, you can buy us a beer
6
+// in return.                                   Thomas Buck & Christian Högerle
7
+// ----------------------------------------------------------------------------
8
+
9
+#listSensorsHeading {
10
+    font-size: 18px;
11
+}
12
+

+ 1
- 6
config.h
File diff suppressed because it is too large
View File


+ 64
- 3
convert-static.py View File

@@ -13,6 +13,8 @@
13 13
 # in return.                                   Thomas Buck & Christian Högerle
14 14
 # ----------------------------------------------------------------------------
15 15
 
16
+import binascii
17
+
16 18
 def fileToString(filename, js = False):
17 19
     f = open(filename, "r")
18 20
     sf = ""
@@ -44,21 +46,80 @@ def minify(text, js = False):
44 46
     text = text.replace("> <", "><")
45 47
     if js == True:
46 48
         text = text.replace("\\n ", "\\n")
49
+        text = text.replace("\\n\\n", "\\n")
47 50
     if (text[-1:] == ' '):
48 51
         text = text[:-1]
52
+    if (text[-2:] == '\\n'):
53
+        text = text[:-2]
54
+    if (text[0] == ' '):
55
+        text = text[1:]
56
+    if (text[0] == '\\') and (text[1] == 'n'):
57
+        text = text[2:]
49 58
     return text
50 59
 
60
+def getBinaryFile(filename, id):
61
+    f = open(filename, "rb")
62
+    s = "const static unsigned char " + id + "[] PROGMEM = {\n";
63
+    i = 0
64
+    c = 0
65
+    while True:
66
+        d = f.read(1)
67
+        if not d:
68
+            break
69
+        if i == 0:
70
+            s += "    "
71
+        s += "0x" + binascii.hexlify(d).decode("utf-8") + ", "
72
+        i += 1
73
+        c += 1
74
+        if i >= 8:
75
+            i = 0
76
+            s += "\n"
77
+    if i == 0:
78
+        s = s[:-3]
79
+    else:
80
+        s = s[:-2]
81
+    s += "\n"
82
+    s += "};"
83
+    s = "const static unsigned int " + id + "Size = " + str(c) + ";\n" + "const static char faviconMimeType[] PROGMEM = \"image/x-icon\";\n" + s;
84
+    return s
85
+
51 86
 def getAsDefine(name, text):
52 87
     return "#define " + name + " \"" + text.replace("\"", "\\\"") + "\""
53 88
 
89
+print("Preparing static.h output file...")
90
+f = open("static.h", "w")
91
+f.write("// !!DO NOT EDIT, AUTO-GENERATED FILE!!\n")
92
+f.write("// Use convert-static.py to recreate this.\n")
93
+f.write("\n")
94
+f.write("#ifndef __STATIC_H__\n")
95
+f.write("#define __STATIC_H__\n")
96
+f.write("\n")
97
+
98
+print("Processing template.html...")
54 99
 template = fileToString("template.html")
55 100
 template = minify(template)
56 101
 templates = template.split(" /* %% INSERT_CLIENT_LIST_HERE %% */ ")
57 102
 
58
-print(getAsDefine("HTML_BEGIN", templates[0]))
59
-print(getAsDefine("HTML_END", templates[1]))
103
+f.write(getAsDefine("HTML_BEGIN", templates[0]) + "\n")
104
+f.write(getAsDefine("HTML_END", templates[1]) + "\n")
60 105
 
106
+print("Processing client-script.js...")
61 107
 js = fileToString("client-script.js", True)
62 108
 js = minify(js, True)
63
-print(getAsDefine("JS_FILE", js))
109
+f.write(getAsDefine("JS_FILE", js) + "\n")
110
+
111
+print("Processing client-style.css...")
112
+css = fileToString("client-style.css", True)
113
+css = minify(css)
114
+f.write(getAsDefine("CSS_FILE", css) + "\n")
115
+f.write("\n")
116
+
117
+print("Processing favicon.ico...")
118
+f.write(getBinaryFile("favicon.ico", "favicon") + "\n")
119
+
120
+print("Done!")
121
+f.write("\n")
122
+f.write("#endif // __STATIC_H__\n")
123
+f.write("\n")
124
+f.close()
64 125
 

BIN
favicon.ico View File


+ 194
- 0
static.h
File diff suppressed because it is too large
View File


+ 2
- 0
template.html View File

@@ -25,6 +25,8 @@
25 25
             href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
26 26
             integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
27 27
             crossorigin="anonymous">
28
+        <link rel="stylesheet" href="style.css" type="text/css">
29
+        <link rel="icon" href="favicon.ico" type="image/x-icon">
28 30
     </head>
29 31
     <body>
30 32
         <script type="text/javascript">

Loading…
Cancel
Save