| 1 | [[PageOutline]] |
| 2 | |
| 3 | = Adding a Webserver To OpenWrt = |
| 4 | Gateworks provides an OpenWrt board support package. By default, the LUCI admin has a default webserver uhttpd running on port 80 |
| 5 | |
| 6 | However, if one wants to add their own webserver for hosting custom content (with even CGI scripts...), it is best to install and configure one's own. OpenWrt makes it easy to add and install packages. |
| 7 | |
| 8 | For this project, we chose the webserver [http://www.lighttpd.net/ lighthttpd]. On this page we will show how to install the webserver lighthttpd and CGI-mod if desired, configure it, create an HTML file and create a CGI script. |
| 9 | |
| 10 | References: |
| 11 | * http://wiki.openwrt.org/doc/howto/http.lighttpd |
| 12 | |
| 13 | |
| 14 | == Basic Configuration == |
| 15 | Please get familiar with building the OpenWrt BSP located [wiki:OpenWrt/building here] |
| 16 | |
| 17 | Please also be familiar with using the make menuconfig command discussed in [wiki:Configuration OpenWrt Configuration] |
| 18 | |
| 19 | 1. In the make menuconfig turn on libprce (lighthttpd depends on it) |
| 20 | 2. Turn on lighthttpd (Network -> Webservers -> !LightHttpd) |
| 21 | 3. Turn on lighthttpd-mod-cgi (If CGI is desired) (Network -> Webservers -> !LightHttpd -> lighthttpd-mod-cgi) |
| 22 | 4. Exit and save |
| 23 | 5. From the trunk directory, compile with the command: make -j8 V=99 && make package/index |
| 24 | 6. Find the IPK files located in the /trunk/bin/cns3xxx/packages/ directory |
| 25 | 7. Install via IPK method as discussed here: [wiki:ipkupload] |
| 26 | 8. Configure the webserver on the Gateworks board by editing /etc/lighttpd/lighttpd.conf and making the following changes: |
| 27 | * '''server.document-root = "/www1/"''' where www1 is the root directory of the web server. Then create the www1 directory |
| 28 | * '''server.port=8000''' where 8000 is the port you want your webserver on '''(This is very important as by default the LUCI Web Admin is running on port 80. If you want to use port 80, you must re-configure LUCI to use something different)''' |
| 29 | 9. Create a simple index.html file in the www1 directory (example down towards bottom of this page) |
| 30 | 10. Start the lighthttpd server with the command |
| 31 | {{{ |
| 32 | /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf |
| 33 | }}} |
| 34 | 11. Access the server at an address like so: http://192.168.1.1:8000/index.html |
| 35 | [[Image(m2mdemolighthttpd.png, 300px)]] |
| 36 | |
| 37 | |
| 38 | == Common Gateway Interface (CG) == |
| 39 | If a Common Gateway Interface (CGI) is desired, it can be configured. |
| 40 | |
| 41 | Modify the lighthttpd config file as noted above and make sure these lines are in the file: |
| 42 | {{{ |
| 43 | server.modules = ( |
| 44 | "mod_cgi" |
| 45 | ) |
| 46 | }}} |
| 47 | |
| 48 | {{{ |
| 49 | #### CGI module |
| 50 | cgi.assign = ( ".pl" => "/usr/bin/perl", ".cgi" => "/usr/bin/perl", ".sh" => "" ) |
| 51 | |
| 52 | }}} |
| 53 | |
| 54 | Once configured, write a script to access the desired function and an appropriate index.html |
| 55 | |
| 56 | |
| 57 | === CGI Script === |
| 58 | Here is a script that reads the voltage on a Laguna board: (Find more commands on the GSC page [wiki:gsc]) |
| 59 | {{{ |
| 60 | root@OpenWrt:/www1# cat voltage.sh |
| 61 | #!/bin/sh |
| 62 | |
| 63 | CAT=/bin/cat |
| 64 | |
| 65 | header() { |
| 66 | echo Content-type: text/html |
| 67 | echo "" |
| 68 | } |
| 69 | |
| 70 | getvalue() { |
| 71 | val="$(cat /sys/class/hwmon/hwmon0/device/in0_input)" |
| 72 | let "vin = val/1000" |
| 73 | echo -e "$vin" |
| 74 | } |
| 75 | footer() { |
| 76 | echo "" |
| 77 | } |
| 78 | |
| 79 | |
| 80 | header |
| 81 | case "$REQUEST_METHOD" in |
| 82 | GET) getvalue;; |
| 83 | esac |
| 84 | footer |
| 85 | }}} |
| 86 | |
| 87 | === Index html file === |
| 88 | Here is a sample index.html file that is used in the above image. Note the library jquery is used for the javascript ajax calls to the cgi scripts: |
| 89 | {{{ |
| 90 | root@OpenWrt:/www1# cat index.html |
| 91 | <html> |
| 92 | <head> |
| 93 | <script src="jquery.js"></script> |
| 94 | |
| 95 | |
| 96 | |
| 97 | </head> |
| 98 | |
| 99 | <body style="font-family:arial;bgcolor="#BDBDBD"> |
| 100 | |
| 101 | <style> |
| 102 | table, th , td,tr |
| 103 | { |
| 104 | border: 1px solid black; |
| 105 | border-collapse:collapse; |
| 106 | text-align:center; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | </style> |
| 112 | <script> |
| 113 | function getTemp() |
| 114 | { |
| 115 | $.ajax({ |
| 116 | url:"temp.sh", |
| 117 | type: "GET", |
| 118 | success: function(data) |
| 119 | { |
| 120 | $('#temperature').html(data+"° C"); |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | |
| 125 | } |
| 126 | function getVoltage() |
| 127 | { |
| 128 | $.ajax({ |
| 129 | url:"voltage.sh", |
| 130 | type: "GET", |
| 131 | success: function(data) |
| 132 | { |
| 133 | $('#voltage').html(data+" Volts"); |
| 134 | } |
| 135 | }); |
| 136 | |
| 137 | |
| 138 | } |
| 139 | $(document).ready(function() { |
| 140 | getVoltage(); |
| 141 | getTemp(); |
| 142 | }); |
| 143 | </script> |
| 144 | <center> |
| 145 | <div style="background-color:white;border: 5px solid red; width:600px;min-height:600px;"> |
| 146 | |
| 147 | <br> |
| 148 | <img alt="Gateworks" style="width:75%" src="images/gateworks.jpg"/> |
| 149 | <br> |
| 150 | <h1 style="font-family:arial"> |
| 151 | Gateworks M2M Demo |
| 152 | </h1> |
| 153 | <br> |
| 154 | <div style="border: 0px solid blue;text-align:left; width:98%;"> |
| 155 | <h3>GSC Values:</h3><br> |
| 156 | <table style="width:98%"> |
| 157 | <tr> |
| 158 | <th> |
| 159 | Property |
| 160 | </th> |
| 161 | <th> |
| 162 | Value |
| 163 | </th> |
| 164 | </tr> |
| 165 | <tr> |
| 166 | <td> |
| 167 | Vin |
| 168 | </td> |
| 169 | <td> |
| 170 | <span id="voltage"/> |
| 171 | </td> |
| 172 | </tr> |
| 173 | |
| 174 | <tr> |
| 175 | <td> |
| 176 | Processor Temperature |
| 177 | </td> |
| 178 | <td> |
| 179 | <span id="temperature"/> |
| 180 | </td> |
| 181 | </tr> |
| 182 | |
| 183 | |
| 184 | </table> |
| 185 | |
| 186 | |
| 187 | </div> |
| 188 | </div> |
| 189 | </center> |
| 190 | </body> |
| 191 | </html> |
| 192 | }}} |
| 193 | |
| 194 | |
| 195 | == Troubleshooting == |
| 196 | To see if the server is listening on a port, use the netstat command: |
| 197 | {{{ |
| 198 | root@OpenWrt:/www1# netstat -a |
| 199 | Active Internet connections (servers and established) |
| 200 | Proto Recv-Q Send-Q Local Address Foreign Address State |
| 201 | tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN |
| 202 | tcp 0 0 0.0.0.0:www 0.0.0.0:* LISTEN |
| 203 | tcp 0 0 0.0.0.0:domain 0.0.0.0:* LISTEN |
| 204 | tcp 0 0 :::domain :::* LISTEN |
| 205 | tcp 0 0 :::ssh :::* LISTEN |
| 206 | tcp 0 0 :::telnet :::* LISTEN |
| 207 | tcp 0 0 ::ffff:192.168.1.73:telnet ::ffff:192.168.1.22:38861 ESTABLISHED |
| 208 | udp 0 0 0.0.0.0:domain 0.0.0.0:* |
| 209 | udp 0 0 192.168.1.73:ntp 0.0.0.0:* |
| 210 | udp 0 0 localhost:ntp 0.0.0.0:* |
| 211 | udp 0 0 0.0.0.0:ntp 0.0.0.0:* |
| 212 | udp 0 0 :::domain :::* |
| 213 | udp 0 0 ::1:ntp :::* |
| 214 | udp 0 0 fe80::2d0:12ff:fe9b:ede0:ntp :::* |
| 215 | udp 0 0 :::ntp :::* |
| 216 | Active UNIX domain sockets (servers and established) |
| 217 | Proto RefCnt Flags Type State I-Node Path |
| 218 | unix 2 [ ] DGRAM 1314 /var/run/hostapd-phy0/wlan0 |
| 219 | unix 8 [ ] DGRAM 254 /dev/log |
| 220 | unix 2 [ ] DGRAM 1966 |
| 221 | unix 2 [ ] DGRAM 1912 |
| 222 | unix 2 [ ] DGRAM 1907 |
| 223 | unix 2 [ ] DGRAM 1729 |
| 224 | unix 2 [ ] DGRAM 1397 |
| 225 | unix 2 [ ] DGRAM 305 |
| 226 | }}} |
| 227 | |
| 228 | Verify the process is running with the ps command: |
| 229 | {{{ |
| 230 | root@OpenWrt:/www1# ps -aef | grep light |
| 231 | 2541 root 2680 S /usr/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf |
| 232 | 2557 root 1140 S grep light |
| 233 | root@OpenWrt:/www1# |
| 234 | }}} |