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