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

+ 2
- 0
README.md View File

4
 
4
 
5
 ## Dependencies
5
 ## Dependencies
6
 
6
 
7
+The included Favicon is ['weather' by 'jkeks'](http://www.favicon.cc/?action=icon&file_id=757061).
8
+
7
 ### ESP8266
9
 ### ESP8266
8
 
10
 
9
 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.
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
+// 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
 var arrSensor = Array();
13
 var arrSensor = Array();
2
 
14
 
3
 textAvailableSensors = "Available Sensors";
15
 textAvailableSensors = "Available Sensors";
8
         <div class="row" id="contentDiv">
20
         <div class="row" id="contentDiv">
9
             <div class="col-md-5 col-lg-5">
21
             <div class="col-md-5 col-lg-5">
10
                 <div class="panel panel-primary">
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
                         ` + textAvailableSensors + ` (0/0)
24
                         ` + textAvailableSensors + ` (0/0)
13
                     </div>
25
                     </div>
14
                     <div class="panel-body">
26
                     <div class="panel-body">
232
     }
244
     }
233
     return color;
245
     return color;
234
 }
246
 }
247
+

+ 12
- 0
client-style.css View File

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
 # in return.                                   Thomas Buck & Christian Högerle
13
 # in return.                                   Thomas Buck & Christian Högerle
14
 # ----------------------------------------------------------------------------
14
 # ----------------------------------------------------------------------------
15
 
15
 
16
+import binascii
17
+
16
 def fileToString(filename, js = False):
18
 def fileToString(filename, js = False):
17
     f = open(filename, "r")
19
     f = open(filename, "r")
18
     sf = ""
20
     sf = ""
44
     text = text.replace("> <", "><")
46
     text = text.replace("> <", "><")
45
     if js == True:
47
     if js == True:
46
         text = text.replace("\\n ", "\\n")
48
         text = text.replace("\\n ", "\\n")
49
+        text = text.replace("\\n\\n", "\\n")
47
     if (text[-1:] == ' '):
50
     if (text[-1:] == ' '):
48
         text = text[:-1]
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
     return text
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
 def getAsDefine(name, text):
86
 def getAsDefine(name, text):
52
     return "#define " + name + " \"" + text.replace("\"", "\\\"") + "\""
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
 template = fileToString("template.html")
99
 template = fileToString("template.html")
55
 template = minify(template)
100
 template = minify(template)
56
 templates = template.split(" /* %% INSERT_CLIENT_LIST_HERE %% */ ")
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
 js = fileToString("client-script.js", True)
107
 js = fileToString("client-script.js", True)
62
 js = minify(js, True)
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
             href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
25
             href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
26
             integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
26
             integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
27
             crossorigin="anonymous">
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
     </head>
30
     </head>
29
     <body>
31
     <body>
30
         <script type="text/javascript">
32
         <script type="text/javascript">

Loading…
Cancel
Save