RF24Ethernet - TCP/IP over RF24Network v1.6.14
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/SLIP_InteractiveServer/HTML.h
3 * This file is used for html code in the
4 * [SLIP_InteractiveServer example](SLIP_InteractiveServer_8ino-example.html)
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 char begin_html[] PROGMEM = "HTTP/1.1 200 OK\n"
23 "Content-Type: text/html\n" //40b
24 "Connection: close\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
37static const char main_html_p1[] PROGMEM =
38
39 "<body>"
40 "<img src='http://arduino.cc/en/uploads/Trademark/ArduinoCommunityLogo.png'"
41 "style='width:383px;height:162px'>"
42 "<br><b>Hello From Arduino!</b><br>\n"
43 "<br><br> LED/Digital Pin Control:"
44 "<br><br>\n<table class = 'center'>";
45
46/***************************************************************/
47
48static const char main_html_p2[] PROGMEM =
49
50 "<tr><td><a href='/ON'>Turn LED On</a>"
51
52 "<br></td><td><a href='/OF'>Turn LED Off</a>"
53
54 "<br></td></tr></table><br><a href='/ST'>"
55
56 "Stats</a> <a href='/CR'>Credits</a>"
57
58 "</body></html>";
59
60/***************************************************************/
61
62// The HTML for the credits page
63static const char credits_html[] PROGMEM = "<body>"
64 "<img src='http://arduino.cc/en/uploads/Trademark/ArduinoCommunityLogo.png'"
65 "style='width:383px;height:162px'>"
66 "<br><b>Credits:</b><br><table class='center'><tr>"
67 "<td>RF24Ethernet by </td>"
68 "<td><a href='https://github.com/tmrh20'> TMRh20</a></td>"
69 "</tr><tr>"
70 "<td>uIP by</td>"
71 "<td><a href='https://github.com/adamdunkels/uip'> Adam Dunkels</a></td>"
72 "</tr><tr>"
73 "<td>Based on</td>"
74 "<td><a href='https://github.com/ntruchsess/arduino_uip'> UIPEthernet</a></td>"
75 "</tr><tr>"
76 "<td>Documentation:</td><td> <a href='http://nRF24.github.io/RF24Ethernet/'>github.io</a></td>"
77 "</tr>"
78 "</tr><tr>"
79 "<td>RF24toTUN creator:</td><td> <a href='https://github.com/reixd/'>Reixd</a></td>"
80 "</tr>"
81 "</table>"
82 "<br>And everybody who contributed to RF24 and RF24Network"
83 "<br><br><a href='/'>Home</a>"
84 "</body>"
85 "</html>";
86
87/***************************************************************/
88
89/**
90* This function reads from a specified program memory buffer, and sends the data to the client
91* in chunks equal to the max output buffer size or less
92* This allows the HTML code to be modified as desired, with no need to change any other code
93*/
94void sendPage(EthernetClient& _client, const char* _pointer, size_t size) {
95 for (uint16_t i = 0; i < size; i++) {
96 char c = pgm_read_byte(_pointer++);
97 _client.write(&c, 1);
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.write(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.write(bf);
127 sprintf_P(bf, PSTR("LED is %s</td></tr>\n"), lState);
128 }
129 _client.write(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\nContent-Type: text/html\n"), 45);
164 _client.write(buffer);
165 strncpy_P(buffer, PSTR("Connection: close\n\n<!DOCTYPE HTML>\n<html>\n"), 45);
166 _client.write(buffer);
167 strncpy_P(buffer, PSTR("<head><style>body{background-color:linen;}\n"), 45);
168 _client.write(buffer);
169 strncpy_P(buffer, PSTR("td{border: 1px solid black;}</style></head>\n"), 45);
170 _client.write(buffer);
171 strncpy_P(buffer, PSTR("<body><table><tr><td> Uptime</td><td>\n"), 45);
172 _client.write(buffer);
173 sprintf_P(buffer, PSTR("%u days, %lu hours %lu minutes %lu"), days, hours, minutes, seconds);
174 _client.write(buffer);
175 strncpy_P(buffer, PSTR("seconds</td></tr><tr><td>UIP Buffer Size"), 45);
176 _client.write(buffer);
177 sprintf_P(buffer, PSTR("</td><td>%u bytes</td></tr><tr><td>User "), UIP_BUFSIZE);
178 _client.write(buffer);
179 sprintf_P(buffer, PSTR("Output<br>Buffer Size</td><td>%u bytes"), OUTPUT_BUFFER_SIZE);
180 _client.write(buffer);
181 strncpy_P(buffer, PSTR("</td></tr></table><br><br>"), 45);
182 _client.write(buffer);
183 strncpy_P(buffer, PSTR("<a href='/'>Home</a></body></html>"), 45);
184 _client.write(buffer);
185}
186
187/***************************************************************/
188
189/**
190* An example of a very basic HTML page
191*/
192static const char html_page[] PROGMEM = "HTTP/1.1 200 OK\n"
193 "Content-Type: text/html\n"
194 "Connection: close\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
Definition: ethernet_comp.h:5
#define OUTPUT_BUFFER_SIZE
Definition: uip-conf.h:153