RF24Ethernet - TCP/IP over RF24Network v1.6.17
TMRh20 - Pushing the practical limits of RF24 modules
Loading...
Searching...
No Matches
HTML.h
Go to the documentation of this file.
1/**
2 * @file examples/InteractiveServer_Mesh_ESPWifi/HTML.h
3 * This file is used for html code in the
4 * InteractiveServer_Mesh_ESPWifi example
5 */
6#if !defined(ARDUINO_ARCH_AVR)
7#define strncpy_P strncpy
8#endif
9
10bool led_state = 0;
11
12/**
13* This page stores the actual HTML code that will be presented.
14* The data is stored in program memory as a single long string, and is presented
15* below in a manageable format
16*/
17
18/***************************************************************/
19
20// The basic beginning of an HTML connection, plus
21// a style (CSS) section and header to be used on every page
22static const PROGMEM char begin_html[] = "HTTP/1.1 200 OK\r\n"
23 "Content-Type: text/html\r\n" //40b
24 "Connection: close\r\n\n" //59
25 "<!DOCTYPE HTML>\n" //75
26 "<html><head>" //87
27 "<style>\n"
28 "body{background-color:linen; text-align: center}"
29 "table.center{margin-left:auto;margin-right:auto;}"
30 "</style>"
31 "</head>";
32
33/***************************************************************/
34
35// The main HTML page, broken into 2 parts
36// It is broken up so some variables can be printed manually in the middle
37/** main html part 1 */
38static const PROGMEM char main_html_p1[] =
39
40 "<body>"
41 "<img src='http://arduino.cc/en/uploads/Trademark/ArduinoCommunityLogo.png'"
42 "style='width:383px;height:162px'>"
43 "<br><b>Hello From Arduino!</b><br>\n"
44 "<br><br> LED/Digital Pin Control:"
45 "<br><br>\n<table class = 'center'>";
46
47/***************************************************************/
48/** main html part 2 */
49static const PROGMEM char main_html_p2[] =
50
51 "<tr><td><a href='/ON'>Turn LED On</a>"
52
53 "<br></td><td><a href='/OF'>Turn LED Off</a>"
54
55 "<br></td></tr></table><br><a href='/ST'>"
56
57 "Stats</a> <a href='/CR'>Credits</a>"
58
59 "</body></html>";
60
61/***************************************************************/
62
63/** The HTML for the credits page */
64static const PROGMEM char credits_html[] = "<body>"
65 "<img src='http://arduino.cc/en/uploads/Trademark/ArduinoCommunityLogo.png'"
66 "style='width:383px;height:162px'>"
67 "<br><b>Credits:</b><br><table class='center'><tr>"
68 "<td>RF24Ethernet by </td>"
69 "<td><a href='https://github.com/tmrh20'> TMRh20</a></td>"
70 "</tr><tr>"
71 "<td>uIP by</td>"
72 "<td><a href='https://github.com/adamdunkels/uip'> Adam Dunkels</a></td>"
73 "</tr><tr>"
74 "<td>Based on</td>"
75 "<td><a href='https://github.com/ntruchsess/arduino_uip'> UIPEthernet</a></td>"
76 "</tr><tr>"
77 "<td>Documentation:</td><td> <a href='http://nRF24.github.io/RF24Ethernet/'>github.io</a></td>"
78 "</tr>"
79 "</tr><tr>"
80 "<td>RF24toTUN creator:</td><td> <a href='https://github.com/reixd/'>Reixd</a></td>"
81 "</tr>"
82 "</table>"
83 "<br>And everybody who contributed to RF24 and RF24Network"
84 "<br><br><a href='/'>Home</a>"
85 "</body>"
86 "</html>";
87
88/***************************************************************/
89
90/**
91 * This function reads from a specified program memory buffer, and sends the data to the client
92 * in chunks equal to the max output buffer size or less
93 * This allows the HTML code to be modified as desired, with no need to change any other code
94 */
95void sendPage(EthernetClient& _client, const char* _pointer, size_t size) {
96 for (uint16_t i = 0; i < size; i++) {
97 _client.print((char)pgm_read_byte(_pointer++));
98 }
99}
100
101/***************************************************************/
102
103// Function to send the main page
104void main_page(EthernetClient& _client) {
105
106 // Send the connection info and header
107 const char* html_pointer = begin_html;
108 sendPage(_client, html_pointer, sizeof(begin_html));
109
110 //Send the first part of the page
111 html_pointer = main_html_p1;
112 sendPage(_client, html_pointer, sizeof(main_html_p1));
113
114 // Include some variables, print them into the page manually
115 const char* lState = led_state ? "ON" : "OFF";
116 const char* lColor = led_state ? "darkseagreen 1" : "lightpink";
117
118 char bf[OUTPUT_BUFFER_SIZE];
119
120 if (!led_state) {
121 sprintf_P(bf, PSTR("<tr><td bgcolor=%s>\n"), lColor);
122 _client.print(bf);
123 sprintf_P(bf, PSTR("LED is %s</td></tr>\n"), lState);
124 } else {
125 sprintf_P(bf, PSTR("<tr><td> </td><td bgcolor=%s>\n"), lColor);
126 _client.print(bf);
127 sprintf_P(bf, PSTR("LED is %s</td></tr>\n"), lState);
128 }
129 _client.print(bf);
130
131 // Send the 2nd half of the page
132 static const char* html_pointer2 = main_html_p2;
133 sendPage(_client, html_pointer2, sizeof(main_html_p2));
134}
135
136/***************************************************************/
137
139 //Set the pointer to the HTML connection data + header
140 const char* html_pointer = begin_html;
141 sendPage(_client, html_pointer, sizeof(begin_html));
142
143 //Set the pointer to the HTML page data and send it
144 html_pointer = credits_html;
145 sendPage(_client, html_pointer, sizeof(credits_html));
146}
147
148/***************************************************************/
149
150// The stats page is sent differently, just to demonstrate a different method of handling pages
152
153 uint32_t seconds = millis() / 1000UL;
154 uint32_t minutes = seconds / 60UL;
155 uint32_t hours = minutes / 60UL;
156 uint8_t days = hours / 24UL;
157 seconds %= 60;
158 minutes %= 60;
159 hours %= 24;
160
161 char buffer[45];
162
163 strncpy_P(buffer, PSTR("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n"), 45);
164 _client.print(buffer);
165 strncpy_P(buffer, PSTR("Connection: close\r\n\n<!DOCTYPE HTML>\n<html>\n"), 45);
166 _client.print(buffer);
167 strncpy_P(buffer, PSTR("<head><style>body{background-color:linen;}\n"), 45);
168 _client.print(buffer);
169 strncpy_P(buffer, PSTR("td{border: 1px solid black;}</style></head>\n"), 45);
170 _client.print(buffer);
171 strncpy_P(buffer, PSTR("<body><table><tr><td> Uptime</td><td>\n"), 45);
172 _client.print(buffer);
173 sprintf_P(buffer, PSTR("%u days, %lu hours %lu minutes %lu"), days, hours, minutes, seconds);
174 _client.print(buffer);
175 strncpy_P(buffer, PSTR("seconds</td></tr><tr><td>UIP Buffer Size"), 45);
176 _client.print(buffer);
177 sprintf_P(buffer, PSTR("</td><td>%u bytes</td></tr><tr><td>User "), UIP_BUFSIZE);
178 _client.print(buffer);
179 sprintf_P(buffer, PSTR("Output<br>Buffer Size</td><td>%u bytes"), OUTPUT_BUFFER_SIZE);
180 _client.print(buffer);
181 strncpy_P(buffer, PSTR("</td></tr></table><br><br>"), 45);
182 _client.print(buffer);
183 strncpy_P(buffer, PSTR("<a href='/'>Home</a></body></html>"), 45);
184 _client.print(buffer);
185}
186
187/***************************************************************/
188
189/**
190 * An example of a very basic HTML page
191 */
192static const PROGMEM char html_page[] = "HTTP/1.1 200 OK\r\n"
193 "Content-Type: text/html\r\n"
194 "Connection: close\r\n\n"
195 "<!DOCTYPE HTML>"
196 "<html>"
197 "<body>"
198 "<b>Hello From Arduino!</b>"
199 "</body>"
200 "</html>";
bool led_state
Definition HTML.h:10
#define strncpy_P
Definition HTML.h:7
void main_page(EthernetClient &_client)
Definition HTML.h:104
void stats_page(EthernetClient &_client)
Definition HTML.h:151
void sendPage(EthernetClient &_client, const char *_pointer, size_t size)
Definition HTML.h:95
void credits_page(EthernetClient &_client)
Definition HTML.h:138
#define EthernetClient
#define OUTPUT_BUFFER_SIZE
Definition uip-conf.h:153