Port from Avnet's Internet Of Things full WiGo demo: SmartConfig - WebServer - Exosite - Android sensor Fusion App

Dependencies:   NVIC_set_all_priorities mbed cc3000_hostdriver_mbedsocket TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger

Wi-Go Reference Design Overview


For additional information on Wi-Go, please visit http://www.em.avnet.com/wi-go
For additional information on Freescale eCompass, please visit
http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=E-Compass
Ported from Avnet's Wi-Go KEIL code.
Special thanks to Jim Carver from Avnet for providing the Wi-Go board and for his assistance.


Multiple Wi-Fi applications are provided within the latest version of Wi-Go software:

  • SmartConfig App for auto-setup of Wi-Go network parameters.
  • WebServer display of live sensor data.
  • Exosite portal sensor data feed by Wi-Go.
  • Freescale's Sensor Fusion App data feed by Wi-Go.

Wi-Go is intended for "untethered" portable operation (using it's high-capacity Lithium-Polymer battery). The serial terminal text interface is only required for initial setup, thereafter selection of an application from those available is via finger position on the Touch Slider during the initial 6 second startup period.

Running the Wi-Go Demo Suite

Warning

The on-board Firmware must be updated to mbed enable a Wi-Go system.
Goto the Component page to get the FirmwareUpdate tool (scroll down to the FirmwareUpdate topic).

MAG3110 sensor and eCompass Calibration!

As with the other sensor applications, the eCompass function requires quality calibration data to achieve best accuracy.
For the first 15 seconds after power-up it is recommended that "Figure 8" movements with Wi-Go be done in a smooth, repetitive pattern. Don't touch the slider pad during calibration.

Startup
The RGB LED blinks in a GREEN-ORANGE sequence to inform the user the module is waiting for input.
The RGB LED color designates which of the following Apps to launch.

RGB LED ColorApplication to Launch
OrangeErase all wireless profiles
PurpleSmartConfig
BlueWebServer
RedExosite Data Client
GreenAndroid Server

Swipe your index finger across the slider pad, the RGB LED color will change at approximately 20% intervals.
Removing your finger latches the last color displayed. After about 3 seconds, the selected app will start.
Another app can be selected when the slider pad is touched again within the 3 seconds timeout.

After launch of Exosite or Android Server Apps, the eCompass function then controls the RGB LED.
(not in WebServer mode where RGB LEDs are manually controlled by the User).

RGB LED ColorDirection Indication
BlueNear to North
GreenNorth
RedEast / West
PurpleSouth

__Note!__ The D1, D2 and D3 User LEDs on Wi-Go adhere to the following convention for the different Apps

User LED#Description of function controlling the LED
D1is the board heartbeat, derived from the timer interrupt
D2indicates network activity as follows:
Web Server Wi-Go webpage is being served.
Exosite Client Wi-Go is sending data.
Android App Wi-Go is sending data
D3WLAN Network is Connected

Detail of Wi-Go Applications

App #1: SmartConfig
See TI's pages on how to use the SmartConfig tool:

  • Preferred method : Configuration using the SmartConfig tool
  • SmartConfig download: Smart Config and Home Automation
    • iOS app : available at Apple app store.
    • Android app : download and install the Android SmartConfig Application on a PC.
      This file contains the source code as well as the compiled APK file.
      The APK file is stored in ti\CC3000AndroidApp\SmartConfigCC3X\bin.

App #2: WebServer display of live sensor data
__Note!__
When using the WebServer for the first time on a Wi-Fi network you will need to determine the IP address that's assigned to Wi-Go by the DHCP Server. To do this, it is recommended you use one of the following two methods:

  • While Wi-Go is initially tethered to a laptop via USB, launch of the WebServer Application and note the IP address that is reported on the terminal screen immediately after selection of this App.
  • Alternatively, use a 3rd party LAN SCAN type tool to view Wi-Go's IP address.
    eg. FING, - available for free download from Google Play or iTunes App Stores…

Wi-Go's WebServer Application is selected as follows:

  • Press RESET, followed by the eCompass Calibration (mentioned at the top of this page).
    Then use index finger on slider to select the WebServer App (RGB LED = BLUE).
    At end of the 3 second selection period the WebServer App shall launch.
  • If you are tethered to a laptop and have a terminal open the Wi-Fi network connection confirmation will be seen, eg.

'*** Wi-Go board DHCP assigned IP Address = 192.168.43.102
  • Once you have noted Wi-Go's reported IP address, the USB cable may be disconnected and Wi-Go then used as intended, running on it's own battery power.
  • Use an Internet Browser on SmartPhone/Tablet/Laptop (connected to same Hot-Spot/Wireless Router subnet), to now connect to the noted Wi-Go IP address and view the WebServer output: /media/uploads/frankvnk/wi-go_webserver.png
  • the Webserver sensor data is auto-updated every 2 seconds a manual refresh (F5 on laptop).
  • In the event of an error, press refresh to regenerate the screen.
  • Use the mouse (or touch-screen) to exercise the RGB LED output.

App #3: Exosite Data Client
Wi-Go's sensor data gets transmitted via Wi-Fi to a cloud-based Exosite portal where the sensor measurements are displayed graphically on a "dashboard". Users can create unique customized dashboards using drag and drop GUI widgets from the library provided on the Exosite website.
__Note!__ For the Exosite application a "live" connection to the Internet is required !!!

  • Press RESET, followed by the eCompass Calibration (mentioned at the top of this page).
    Then use index finger on slider to select the Exosite Client App (RGB LED = RED)
  • On launching this App, note Wi-Go's MAC address displayed on your terminal
    (if not running a terminal use FING or other WLAN Scan tool to determine Wi-Go's MAC address) /media/uploads/frankvnk/mac_address.png
  • Using your computer's internet browser, go to avnet.exosite.com and sign-up for a free Avnet Trial Exosite Account: /media/uploads/frankvnk/avnet_trial_exosite.png
  • On the next screen, click on the Sign-Up Now button in the displayed Avnet Trial account option.
  • Complete the Account Info and Contact Info then click on Create Account (make sure to use a valid email address!).
  • Check for new incoming email from avnet.exosite.com to the address you provided and click on the link in this email to activate your new Exosite account.
  • Once activated, login using the email address and password that you chose in your registration. Your Exosite Portal and Dashboard should now display. The first time you log-in to your new account, the default Home dashboard will be displayed, pre-configured with two widgets. On the left is the Welcome widget for tips and information. On the right is the Device List widget.
    Dashboards are configurable, so at any time this default dashboard can be changed, widgets deleted and added (Clicking the upside-down triangle icon in a widget's Title bar will allow you to edit it).
  • Before going further with the Dashboard, you need to connect your Wi-Go device to your Exosite account. Do this by going to the left sidebar and selecting Devices followed by selecting the +Add Device link (on right of screen). /media/uploads/frankvnk/add_device.png
  • In the Setup screens that follow, enter the following
Select a supported deviceWi-Go
Enter device MAC Addressnn:nn:nn:nn:nn:nn [your Wi-Go's MAC address including colons]
Enter device Name[choose a descriptive name]
Enter device Location[description of your location]
  • Once completed, under Devices the name chosen for the added Wi-Go device should now be listed.
  • Click on this new Wi-Go device to examine (and edit if necessary) it's Device Information screen.
    /media/uploads/frankvnk/device_information.png
  • Click the CLOSE button to exit the Device Information screen.
  • On your Wi-Go kit now press RESET, followed by the eCompass Calibration (mentioned at the top of this page)
    and again select the Exosite Client App (RGB LED = RED) using your index finger.
  • Refresh your browser (press F5) a couple've times until the Active indicator changes to On (Green).
    /media/uploads/frankvnk/active_indicator.png
  • From the left sidebar click on Home and click on the recently named Wi-Go device which is located under the Device List.
    This will bring-up a default dashboard display similar to what's shown below.
    (Dashboards are typically accessed via the Dashboards menu entry). Check the dashboard is updating with live data by moving your Wi-Go Kit through different orientations.
    /media/uploads/frankvnk/dashboard.png
  • To create a custom dashboard, select Dashboards from the sidebar menu, followed by +Add Dashboard (on right side of Your Dashboards title bar). After completion of the initial configuration screen you will then be able to add Widgets to display the various Wi-Go data sources as well as pictures and text to support your application.
  • More guidance on the creation, editing and sharing of custom dashboards is available under the Exosite support pages

App #4: Android Sensor Fusion App

  • Press RESET, followed by the eCompass Calibration (mentioned at the top of this page)
    , then use index finger on slider to select the Android App (RGB LED = GREEN)
  • Freescale's ''Xtrinsic Sensor Fusion Toolbox'" will run on Android 3.0 or above phone or tablet. Free to download from Google Play, type Sensor fusion in the search box to find it. freescale.sensors.sfusion /media/uploads/frankvnk/sensor_fusion_toolbox.png
  • The Freescale App is well documented. To access the built-in documentation, press the NAV button at top of screen followed by Documentation from the scroll-down menu:
    /media/uploads/frankvnk/sensor_fusion_doc.png
  • Freescale's sensors site provides additional resources such as this overview: free-android-app-teaches-sensor-fusion-basics
  • Go to the Options Menu and select Preferences… /media/uploads/frankvnk/sensor_fusion_preferences.png
  • The following items need to be taken care of:
Enter WiGo's IP address
Enter the SSID (of the Hot-Spot or Wireless Access Point used by Wi-Go)
  • Press Save and Exit!
    /media/uploads/frankvnk/sensor_fusion_save_and_exit.png
  • Exit the Application completely then re-launch the Sensor Fusion Application.
  • Select the ''Source/Algorithm'" menu and change the data source to Wi-Go mag/accel /media/uploads/frankvnk/sensor_fusion_wigo_mag_accel.png
  • The Android App should now be displaying a 3-D image of Wi-Go that you can rotate and flip-over by moving the Wi-Go board accordingly…
  • Use NAV > Device View to display if this view does not come-up by default. /media/uploads/frankvnk/sensor_fusion_nav_device_view.png
  • A Serial Terminal connection is not necessary but if you happen to have one open you should see the following messages as Wi-Go connects to the Android App:
    "Server waiting for connection" followed by
    "connected, transmit buffer size= 96", and then
    "input = 0123456789"
    at which time Wi-Go starts streaming data to the Android App.
Committer:
frankvnk
Date:
Sun Dec 01 15:07:20 2013 +0000
Revision:
5:bd9705c7cf51
Child:
7:9d86d022fa68
Solved sensor read errors.
; Added i2c unlock.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frankvnk 5:bd9705c7cf51 1
frankvnk 5:bd9705c7cf51 2 /*****************************************************************************
frankvnk 5:bd9705c7cf51 3 * Dynamic HTML string handlers:
frankvnk 5:bd9705c7cf51 4 * Nine dynamic HTML fields are updated on browser refresh or button press:
frankvnk 5:bd9705c7cf51 5 * Acceleration
frankvnk 5:bd9705c7cf51 6 * Magnetometer
frankvnk 5:bd9705c7cf51 7 * eCompass
frankvnk 5:bd9705c7cf51 8 * Altitude
frankvnk 5:bd9705c7cf51 9 * Battery Voltage
frankvnk 5:bd9705c7cf51 10 * Ambient Light
frankvnk 5:bd9705c7cf51 11 * Temperature
frankvnk 5:bd9705c7cf51 12 * Slider Position
frankvnk 5:bd9705c7cf51 13 * Page Views
frankvnk 5:bd9705c7cf51 14 *
frankvnk 5:bd9705c7cf51 15 * CGI HTML forms:
frankvnk 5:bd9705c7cf51 16 * Three CGI form inputs are used to submit data from browser to the Server:
frankvnk 5:bd9705c7cf51 17 * On screen buttons: -Red-, Green, Blue (for control of RGB LED output color)
frankvnk 5:bd9705c7cf51 18 *
frankvnk 5:bd9705c7cf51 19 * myindex[] contains the HTML string that defines the webpage that is served
frankvnk 5:bd9705c7cf51 20 * Use an online HTML validator to verify HTML code before running it on the MCU
frankvnk 5:bd9705c7cf51 21 * eg.
frankvnk 5:bd9705c7cf51 22 * www.w3schools.com/tags/tryit.asp?filename=tryhtml_div_test
frankvnk 5:bd9705c7cf51 23 * www.onlinewebcheck.com/check.php?adv=1
frankvnk 5:bd9705c7cf51 24 * Note: Before checking the HTML in one of these validators,
frankvnk 5:bd9705c7cf51 25 * strip-out all “\” backslash characters (using search & replace)
frankvnk 5:bd9705c7cf51 26 *
frankvnk 5:bd9705c7cf51 27 * Webserver code is based on TI's CC3000 Simple HTTP Webserver:
frankvnk 5:bd9705c7cf51 28 * http://processors.wiki.ti.com/index.php/CC3000_Wi-Fi_for_MCU
frankvnk 5:bd9705c7cf51 29 *
frankvnk 5:bd9705c7cf51 30 * More detail on implementation of this Webserver App is available here:
frankvnk 5:bd9705c7cf51 31 * http://processors.wiki.ti.com/index.php/CC3000_HTTP_Server_Demo_Session
frankvnk 5:bd9705c7cf51 32 *
frankvnk 5:bd9705c7cf51 33 * A more advanced Webserver and Client App is also available from TI for the CC3000:
frankvnk 5:bd9705c7cf51 34 * http://processors.wiki.ti.com/index.php/CC3000_Web_Server_Client_Application
frankvnk 5:bd9705c7cf51 35 * (at this time not yet ported to Kinetis-L as the host processor)
frankvnk 5:bd9705c7cf51 36 *
frankvnk 5:bd9705c7cf51 37 ******************************************************************************
frankvnk 5:bd9705c7cf51 38 *
frankvnk 5:bd9705c7cf51 39 * demo.c - CC3000 Main Demo Application
frankvnk 5:bd9705c7cf51 40 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
frankvnk 5:bd9705c7cf51 41 *
frankvnk 5:bd9705c7cf51 42 * Redistribution and use in source and binary forms, with or without
frankvnk 5:bd9705c7cf51 43 * modification, are permitted provided that the following conditions
frankvnk 5:bd9705c7cf51 44 * are met:
frankvnk 5:bd9705c7cf51 45 *
frankvnk 5:bd9705c7cf51 46 * Redistributions of source code must retain the above copyright
frankvnk 5:bd9705c7cf51 47 * notice, this list of conditions and the following disclaimer.
frankvnk 5:bd9705c7cf51 48 *
frankvnk 5:bd9705c7cf51 49 * Redistributions in binary form must reproduce the above copyright
frankvnk 5:bd9705c7cf51 50 * notice, this list of conditions and the following disclaimer in the
frankvnk 5:bd9705c7cf51 51 * documentation and/or other materials provided with the
frankvnk 5:bd9705c7cf51 52 * distribution.
frankvnk 5:bd9705c7cf51 53 *
frankvnk 5:bd9705c7cf51 54 * Neither the name of Texas Instruments Incorporated nor the names of
frankvnk 5:bd9705c7cf51 55 * its contributors may be used to endorse or promote products derived
frankvnk 5:bd9705c7cf51 56 * from this software without specific prior written permission.
frankvnk 5:bd9705c7cf51 57 *
frankvnk 5:bd9705c7cf51 58 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
frankvnk 5:bd9705c7cf51 59 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
frankvnk 5:bd9705c7cf51 60 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
frankvnk 5:bd9705c7cf51 61 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
frankvnk 5:bd9705c7cf51 62 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
frankvnk 5:bd9705c7cf51 63 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
frankvnk 5:bd9705c7cf51 64 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
frankvnk 5:bd9705c7cf51 65 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
frankvnk 5:bd9705c7cf51 66 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
frankvnk 5:bd9705c7cf51 67 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
frankvnk 5:bd9705c7cf51 68 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
frankvnk 5:bd9705c7cf51 69 *
frankvnk 5:bd9705c7cf51 70 *****************************************************************************/
frankvnk 5:bd9705c7cf51 71
frankvnk 5:bd9705c7cf51 72 #include "mbed.h"
frankvnk 5:bd9705c7cf51 73 #include "defLED.h"
frankvnk 5:bd9705c7cf51 74 #include "demo.h"
frankvnk 5:bd9705c7cf51 75 #include "AvnetHTML.h"
frankvnk 5:bd9705c7cf51 76 #include "TSISensor.h"
frankvnk 5:bd9705c7cf51 77 #include "Wi-Go_eCompass_Lib_V3.h"
frankvnk 5:bd9705c7cf51 78
frankvnk 5:bd9705c7cf51 79 extern DigitalOut ledr;
frankvnk 5:bd9705c7cf51 80 extern DigitalOut ledg;
frankvnk 5:bd9705c7cf51 81 extern DigitalOut ledb;
frankvnk 5:bd9705c7cf51 82 extern DigitalOut led1;
frankvnk 5:bd9705c7cf51 83 extern DigitalOut led2;
frankvnk 5:bd9705c7cf51 84 extern DigitalOut led3;
frankvnk 5:bd9705c7cf51 85 extern TSISensor tsi;
frankvnk 5:bd9705c7cf51 86
frankvnk 5:bd9705c7cf51 87 // Setup the functions to handle our CGI parameters
frankvnk 5:bd9705c7cf51 88 cgi_handler pHandlers;
frankvnk 5:bd9705c7cf51 89 dyn_html_handler htmlHandlers;
frankvnk 5:bd9705c7cf51 90
frankvnk 5:bd9705c7cf51 91 extern tNetappIpconfigRetArgs ipinfo2;
frankvnk 5:bd9705c7cf51 92
frankvnk 5:bd9705c7cf51 93 extern axis6_t axis6;
frankvnk 5:bd9705c7cf51 94 extern int server_running;
frankvnk 5:bd9705c7cf51 95 extern unsigned char newData;
frankvnk 5:bd9705c7cf51 96 extern unsigned short adc_sample3;
frankvnk 5:bd9705c7cf51 97
frankvnk 5:bd9705c7cf51 98 /** \brief Pointer to the index HTML page */
frankvnk 5:bd9705c7cf51 99 char * indexPage;
frankvnk 5:bd9705c7cf51 100
frankvnk 5:bd9705c7cf51 101 /** \brief Pointer to CGI handler structure */
frankvnk 5:bd9705c7cf51 102 cgi_handler * chList;
frankvnk 5:bd9705c7cf51 103
frankvnk 5:bd9705c7cf51 104 /** \brief Pointer to Dynamic HTML handler structure */
frankvnk 5:bd9705c7cf51 105 dyn_html_handler * htmlList;
frankvnk 5:bd9705c7cf51 106
frankvnk 5:bd9705c7cf51 107 /** \brief Page view counter */
frankvnk 5:bd9705c7cf51 108 int viewCounter = 1;
frankvnk 5:bd9705c7cf51 109 #define REQ_BUFFER_SIZE 400
frankvnk 5:bd9705c7cf51 110 #define HTTP_TX_BLOCK_SIZE 256
frankvnk 5:bd9705c7cf51 111 //#define HTTP_TX_BLOCK_SIZE 1024
frankvnk 5:bd9705c7cf51 112 //#define HTTP_TX_BLOCK_SIZE 512
frankvnk 5:bd9705c7cf51 113
frankvnk 5:bd9705c7cf51 114
frankvnk 5:bd9705c7cf51 115 // Setup the functions to handle our CGI parameters
frankvnk 5:bd9705c7cf51 116 char requestBuffer[REQ_BUFFER_SIZE];
frankvnk 5:bd9705c7cf51 117
frankvnk 5:bd9705c7cf51 118
frankvnk 5:bd9705c7cf51 119
frankvnk 5:bd9705c7cf51 120
frankvnk 5:bd9705c7cf51 121
frankvnk 5:bd9705c7cf51 122 /*
frankvnk 5:bd9705c7cf51 123 // ---------- HTML Webpage Content is defined here ----------
frankvnk 5:bd9705c7cf51 124 // Caution! Field labels and spaces in the HTML should not be edited without making corresponding changes in the C-code!
frankvnk 5:bd9705c7cf51 125 char index[] = {
frankvnk 5:bd9705c7cf51 126 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"><html lang=\"en-US\">"
frankvnk 5:bd9705c7cf51 127 "<head>"
frankvnk 5:bd9705c7cf51 128 "<META content=\"text/html;charset=ISO-8859-1\" http-equiv=\"content-type\">"
frankvnk 5:bd9705c7cf51 129 "<title>Wi-Go WebServer</title>"
frankvnk 5:bd9705c7cf51 130 //"<META HTTP-EQUIV=\"refresh\" content=\"2\">" // Uncomment for auto-refresh every 2 seconds
frankvnk 5:bd9705c7cf51 131 "</head>"
frankvnk 5:bd9705c7cf51 132
frankvnk 5:bd9705c7cf51 133 "<body><div style=\"text-align: left\"><font size=\"6\" color=\"Red\" face=\"Tahoma\">"
frankvnk 5:bd9705c7cf51 134 "<b>Avnet Wi-Go Webserver</b></font>"
frankvnk 5:bd9705c7cf51 135 "<hr size=3 width=600 align=left>"
frankvnk 5:bd9705c7cf51 136 "<font size=\"5\" color=\"Red\" face=\"Tahoma\"><b>LED RGB color select...</b></font>"
frankvnk 5:bd9705c7cf51 137
frankvnk 5:bd9705c7cf51 138 //"<font size=\"5\" color=\"Black\" face=\"monospace\"></font>"
frankvnk 5:bd9705c7cf51 139 "<form method=\"get\" action=\"index.html\" name=\"server\">"
frankvnk 5:bd9705c7cf51 140 "<input name=\"ledCon\" type=\"submit\" value=\"-Red-\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
frankvnk 5:bd9705c7cf51 141 "<input name=\"ledCon\" type=\"submit\" value=\"Green\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
frankvnk 5:bd9705c7cf51 142 "<input name=\"ledCon\" type=\"submit\" value=\"Blue-\">"
frankvnk 5:bd9705c7cf51 143 "</form></div>"
frankvnk 5:bd9705c7cf51 144
frankvnk 5:bd9705c7cf51 145 "<hr size=3 width=600 align=left>"
frankvnk 5:bd9705c7cf51 146 "<div style=\"text-align: left\"><font size=\"5\" color=\"Red\" face=\"Tahoma\">"
frankvnk 5:bd9705c7cf51 147 "<b>Navigation Sensors</b></font></div>"
frankvnk 5:bd9705c7cf51 148
frankvnk 5:bd9705c7cf51 149 "<div style=\"text-align:left\"><font size=\"4\" color=\"Black\" face=\"monospace\"><b>"
frankvnk 5:bd9705c7cf51 150 "Acceleration(G).. <br>"
frankvnk 5:bd9705c7cf51 151 "Magnetometer(uT). <br>"
frankvnk 5:bd9705c7cf51 152 "eCompass......... <br>"
frankvnk 5:bd9705c7cf51 153 "Altitude......... <br></b></font></div>"
frankvnk 5:bd9705c7cf51 154
frankvnk 5:bd9705c7cf51 155 "<hr size=3 width=600 align=left>"
frankvnk 5:bd9705c7cf51 156 "<div style=\"text-align: left\"><font size=\"5\" color=\"Red\" face=\"Tahoma\">"
frankvnk 5:bd9705c7cf51 157 "<b>Status and Control</b></font></div>"
frankvnk 5:bd9705c7cf51 158
frankvnk 5:bd9705c7cf51 159 "<div style=\"text-align:left\"><font size=\"4\" color=\"Black\" face=\"monospace\"><b>"
frankvnk 5:bd9705c7cf51 160 "Battery Voltage.. <br>"
frankvnk 5:bd9705c7cf51 161 "Ambient Light.... <br>"
frankvnk 5:bd9705c7cf51 162 "Temperature...... <br>"
frankvnk 5:bd9705c7cf51 163 "Slider Position.. <br>"
frankvnk 5:bd9705c7cf51 164 "Page Views....... </b></font>"
frankvnk 5:bd9705c7cf51 165 "<hr size=3 width=600 align=left>"
frankvnk 5:bd9705c7cf51 166 "</body></html>"}; // delete this line if adding the SVG code below...
frankvnk 5:bd9705c7cf51 167 */
frankvnk 5:bd9705c7cf51 168 // Optional section 1: Full SVG graphic example: Uncomment this section to display the standard Wi-Fi logo in lower area of webpage
frankvnk 5:bd9705c7cf51 169 // Browsers supporting SVG: Chrome and FireFox (all versions), Android (3.0 onwards), Safari (5.0 onwards), IE (9.0 onwards?)
frankvnk 5:bd9705c7cf51 170 /*
frankvnk 5:bd9705c7cf51 171 "<!--[if !IE]><!-->" // This prevents SVG content being sent to Internet Explorer
frankvnk 5:bd9705c7cf51 172 "<svg>"
frankvnk 5:bd9705c7cf51 173 "xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" height=\"175.49\" width=\"400\""
frankvnk 5:bd9705c7cf51 174 "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
frankvnk 5:bd9705c7cf51 175 "xmlns:cc=\"http://creativecommons.org/ns#\""
frankvnk 5:bd9705c7cf51 176 "xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
frankvnk 5:bd9705c7cf51 177 "<defs></defs>"
frankvnk 5:bd9705c7cf51 178 "<metadata>"
frankvnk 5:bd9705c7cf51 179 "<rdf:RDF>"
frankvnk 5:bd9705c7cf51 180 "<cc:Work rdf:about=\"\">"
frankvnk 5:bd9705c7cf51 181 "<dc:format>image/svg+xml</dc:format>"
frankvnk 5:bd9705c7cf51 182 "<dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>"
frankvnk 5:bd9705c7cf51 183 "<dc:title/>"
frankvnk 5:bd9705c7cf51 184 "</cc:Work>"
frankvnk 5:bd9705c7cf51 185 "</rdf:RDF>"
frankvnk 5:bd9705c7cf51 186 "</metadata>"
frankvnk 5:bd9705c7cf51 187 "<g transform=\"translate(-309.85919,-343.20843)\">"
frankvnk 5:bd9705c7cf51 188 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,576.05074,474.82936)\">"
frankvnk 5:bd9705c7cf51 189 "<path fill-rule=\"nonzero\" fill=\"#231f20\" d=\"m0,0c4.977,0,9.049,4.077,9.049,9.049v5.389c0,4.973-4.072,9.048-9.049,9.048h-35.433c-4.973,0-9.049-4.075-9.049-9.048v-5.389c0-4.972,4.076-9.049,9.049-9.049\"/>"
frankvnk 5:bd9705c7cf51 190 "</g>"
frankvnk 5:bd9705c7cf51 191 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,514.3818,441.01715)\">"
frankvnk 5:bd9705c7cf51 192 "<path fill-rule=\"nonzero\" fill=\"#FFF\" d=\"m0,0,0,5.389c0,4.072,3.314,7.32,7.32,7.32h9.187c4.007,0,7.253-3.248,7.253-7.32v-5.389c0-4.005-3.246-7.32-7.253-7.32h-20.239c2.281,1.656,3.732,4.284,3.732,7.32\"/>"
frankvnk 5:bd9705c7cf51 193 "</g>"
frankvnk 5:bd9705c7cf51 194 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,469.99257,451.60916)\">"
frankvnk 5:bd9705c7cf51 195 "<path fill-rule=\"nonzero\" fill=\"#FFF\" d=\"m0,0-3.043,0-0.55,2.56c-0.345,1.794-0.692,4.005-0.761,4.833-0.069-0.828-0.416-3.039-0.825-4.833l-0.555-2.56h-2.968l-2.767,11.748h3.317l0.343-2.004c0.276-1.66,0.556-3.659,0.695-5.044,0.136,1.385,0.481,3.384,0.896,5.044l0.412,2.004h2.972l0.413-2.004c0.348-1.66,0.693-3.659,0.833-5.044,0.136,1.385,0.482,3.384,0.757,5.044l0.278,2.004h3.313\"/>"
frankvnk 5:bd9705c7cf51 196 "</g>"
frankvnk 5:bd9705c7cf51 197 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,491.40819,416.23898)\">"
frankvnk 5:bd9705c7cf51 198 "<path fill-rule=\"nonzero\" fill=\"#FFF\" d=\"m0,0c-0.968,0-1.727,0.553-1.727,1.451,0,0.899,0.759,1.45,1.727,1.45,1.036,0,1.796-0.551,1.796-1.45,0-0.898-0.76-1.451-1.796-1.451m-1.521-0.968,3.0401,0,0-8.4984-3.0401,0,0,8.4984z\"/>"
frankvnk 5:bd9705c7cf51 199 "</g>"
frankvnk 5:bd9705c7cf51 200 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,541.9845,418.05849)\">"
frankvnk 5:bd9705c7cf51 201 "<path fill-rule=\"nonzero\" fill=\"#231f20\" d=\"m0,0,0-2.618,6.22,0,0-2.767-6.22,0,0-3.593-3.247,0,0,11.748,10.156,0,0-2.77\"/>"
frankvnk 5:bd9705c7cf51 202 "</g>"
frankvnk 5:bd9705c7cf51 203 "<g transform=\"matrix(3.7361528,0,0,-3.7361528,580.44446,416.23898)\">"
frankvnk 5:bd9705c7cf51 204 "<path fill-rule=\"nonzero\" fill=\"#231f20\" d=\"m0,0c-0.97,0-1.727,0.553-1.727,1.451,0,0.899,0.757,1.45,1.727,1.45,1.035,0,1.797-0.551,1.797-1.45,0-0.898-0.762-1.451-1.797-1.451m-1.521-0.968,3.0371,0,0-8.4984-3.0371,0,0,8.4984z\"/>"
frankvnk 5:bd9705c7cf51 205 "</g>"
frankvnk 5:bd9705c7cf51 206 "</g>"
frankvnk 5:bd9705c7cf51 207 "</svg>"
frankvnk 5:bd9705c7cf51 208 "<!--<![endif]-->"
frankvnk 5:bd9705c7cf51 209 */ // end of conditional inclusion of SVG graphic (excludes Internet Explorer browser)
frankvnk 5:bd9705c7cf51 210 // ---- End of Wi-Fi logo SVG image definition ----
frankvnk 5:bd9705c7cf51 211
frankvnk 5:bd9705c7cf51 212 // Optional section 2: Simple SVG graphic example: Uncomment this section to generate a filled circle in lower area of webpage
frankvnk 5:bd9705c7cf51 213 // eg. Could use to indicate RGB LED color (with addition of applicable C-code to dynamically update specified "fill=" color)
frankvnk 5:bd9705c7cf51 214 /*
frankvnk 5:bd9705c7cf51 215 "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">"
frankvnk 5:bd9705c7cf51 216 "<circle cx=\"155\" cy=\"22\" r=\"20\" stroke=\"DimGrey\" stroke-width=\"2\" fill=\"Red\"/>"
frankvnk 5:bd9705c7cf51 217 "</svg>"
frankvnk 5:bd9705c7cf51 218 "</body></html>"};
frankvnk 5:bd9705c7cf51 219 */
frankvnk 5:bd9705c7cf51 220
frankvnk 5:bd9705c7cf51 221 // ----------------------------------------------------------
frankvnk 5:bd9705c7cf51 222
frankvnk 5:bd9705c7cf51 223 //char ssid_name[] = SSID;
frankvnk 5:bd9705c7cf51 224 char testString[20];
frankvnk 5:bd9705c7cf51 225 //int vcc = 0;
frankvnk 5:bd9705c7cf51 226 //*****************************************************************************
frankvnk 5:bd9705c7cf51 227 //
frankvnk 5:bd9705c7cf51 228 //! demo_wi-fi_main
frankvnk 5:bd9705c7cf51 229 //!
frankvnk 5:bd9705c7cf51 230 //! \param None
frankvnk 5:bd9705c7cf51 231 //!
frankvnk 5:bd9705c7cf51 232 //! \return none
frankvnk 5:bd9705c7cf51 233 //!
frankvnk 5:bd9705c7cf51 234 //! \brief The main loop is executed here
frankvnk 5:bd9705c7cf51 235 //
frankvnk 5:bd9705c7cf51 236 //*****************************************************************************
frankvnk 5:bd9705c7cf51 237
frankvnk 5:bd9705c7cf51 238 int demo_wifi_main(void)
frankvnk 5:bd9705c7cf51 239 {
frankvnk 5:bd9705c7cf51 240 server_running = 1;
frankvnk 5:bd9705c7cf51 241 while(1)
frankvnk 5:bd9705c7cf51 242 {
frankvnk 5:bd9705c7cf51 243 /* Configure dynamic HTML string handlers
frankvnk 5:bd9705c7cf51 244 0 : Acceleration
frankvnk 5:bd9705c7cf51 245 1 : Magnetometer
frankvnk 5:bd9705c7cf51 246 2 : eCompass
frankvnk 5:bd9705c7cf51 247 3 : Altitude
frankvnk 5:bd9705c7cf51 248 -------------------
frankvnk 5:bd9705c7cf51 249 4 : Battery Voltage
frankvnk 5:bd9705c7cf51 250 5 : Ambient Light
frankvnk 5:bd9705c7cf51 251 6 : Temperature
frankvnk 5:bd9705c7cf51 252 7 : Slider Position
frankvnk 5:bd9705c7cf51 253 8 : Page Views
frankvnk 5:bd9705c7cf51 254 */
frankvnk 5:bd9705c7cf51 255 (htmlHandlers.dynHtmlFunc[0]) = getAccelXYZ_Str;
frankvnk 5:bd9705c7cf51 256 memcpy(htmlHandlers.dynHtmlParamName[0],"Acceleration(G).. ",strlen("Acceleration(G).. "));
frankvnk 5:bd9705c7cf51 257 htmlHandlers.dynHtmlParamName[0][strlen("Acceleration(G).. ")] ='\0';
frankvnk 5:bd9705c7cf51 258
frankvnk 5:bd9705c7cf51 259 (htmlHandlers.dynHtmlFunc[1]) = getM3110Str;
frankvnk 5:bd9705c7cf51 260 memcpy(htmlHandlers.dynHtmlParamName[1],"Magnetometer(uT). ",strlen("Magnetometer(uT). "));
frankvnk 5:bd9705c7cf51 261 htmlHandlers.dynHtmlParamName[1][strlen("Magnetometer(uT). ")] ='\0';
frankvnk 5:bd9705c7cf51 262
frankvnk 5:bd9705c7cf51 263 (htmlHandlers.dynHtmlFunc[2]) = getCompassStr;
frankvnk 5:bd9705c7cf51 264 memcpy(htmlHandlers.dynHtmlParamName[2],"eCompass......... ",strlen("eCompass......... "));
frankvnk 5:bd9705c7cf51 265 htmlHandlers.dynHtmlParamName[2][strlen("eCompass......... ")] ='\0';
frankvnk 5:bd9705c7cf51 266
frankvnk 5:bd9705c7cf51 267 (htmlHandlers.dynHtmlFunc[3]) = getAltitudeStr;
frankvnk 5:bd9705c7cf51 268 memcpy(htmlHandlers.dynHtmlParamName[3],"Altitude......... ",strlen("Altitude......... "));
frankvnk 5:bd9705c7cf51 269 htmlHandlers.dynHtmlParamName[3][strlen("Altitude......... ")] ='\0';
frankvnk 5:bd9705c7cf51 270
frankvnk 5:bd9705c7cf51 271 (htmlHandlers.dynHtmlFunc[4]) = getBatteryVoltageStr;
frankvnk 5:bd9705c7cf51 272 memcpy(htmlHandlers.dynHtmlParamName[4],"Battery Voltage.. ",strlen("Battery Voltage.. "));
frankvnk 5:bd9705c7cf51 273 htmlHandlers.dynHtmlParamName[4][strlen("Battery Voltage.. ")] ='\0';
frankvnk 5:bd9705c7cf51 274
frankvnk 5:bd9705c7cf51 275 (htmlHandlers.dynHtmlFunc[5]) = getLightVoltageStr;
frankvnk 5:bd9705c7cf51 276 memcpy(htmlHandlers.dynHtmlParamName[5],"Ambient Light.... ",strlen("Ambient Light.... "));
frankvnk 5:bd9705c7cf51 277 htmlHandlers.dynHtmlParamName[5][strlen("Ambient Light.... ")] ='\0';
frankvnk 5:bd9705c7cf51 278
frankvnk 5:bd9705c7cf51 279 (htmlHandlers.dynHtmlFunc[6]) = getTemperatureStr;
frankvnk 5:bd9705c7cf51 280 memcpy(htmlHandlers.dynHtmlParamName[6],"Temperature...... ",strlen("Temperature...... "));
frankvnk 5:bd9705c7cf51 281 htmlHandlers.dynHtmlParamName[6][strlen("Temperature...... ")] ='\0';
frankvnk 5:bd9705c7cf51 282
frankvnk 5:bd9705c7cf51 283 (htmlHandlers.dynHtmlFunc[7]) = getTSI_sliderStr;
frankvnk 5:bd9705c7cf51 284 memcpy(htmlHandlers.dynHtmlParamName[7],"Slider Position.. ",strlen("Slider Position.. "));
frankvnk 5:bd9705c7cf51 285 htmlHandlers.dynHtmlParamName[7][strlen("Slider Position.. ")] ='\0';
frankvnk 5:bd9705c7cf51 286
frankvnk 5:bd9705c7cf51 287 (htmlHandlers.dynHtmlFunc[8]) = getViewsNum;
frankvnk 5:bd9705c7cf51 288 memcpy(htmlHandlers.dynHtmlParamName[8],"Page Views....... ",strlen("Page Views....... "));
frankvnk 5:bd9705c7cf51 289 htmlHandlers.dynHtmlParamName[8][strlen("Page Views....... ")] ='\0';
frankvnk 5:bd9705c7cf51 290
frankvnk 5:bd9705c7cf51 291 // Configure CGI Handler
frankvnk 5:bd9705c7cf51 292 (pHandlers.cgiHandlerFunc[0]) = testFunc;
frankvnk 5:bd9705c7cf51 293
frankvnk 5:bd9705c7cf51 294 serverMain(HTTP_PORT,(char *)index, &pHandlers, &htmlHandlers);
frankvnk 5:bd9705c7cf51 295 }
frankvnk 5:bd9705c7cf51 296 }
frankvnk 5:bd9705c7cf51 297
frankvnk 5:bd9705c7cf51 298
frankvnk 5:bd9705c7cf51 299 void testFunc(char * str)
frankvnk 5:bd9705c7cf51 300 {
frankvnk 5:bd9705c7cf51 301 memcpy(testString,str,strlen(str));
frankvnk 5:bd9705c7cf51 302 if(strcmp(str, "-Red-") == 0)
frankvnk 5:bd9705c7cf51 303 {
frankvnk 5:bd9705c7cf51 304 RED_ON; GREEN_OFF; BLUE_OFF;
frankvnk 5:bd9705c7cf51 305 }
frankvnk 5:bd9705c7cf51 306 else if(strcmp(str, "Green") == 0)
frankvnk 5:bd9705c7cf51 307 {
frankvnk 5:bd9705c7cf51 308 RED_OFF; GREEN_ON; BLUE_OFF;
frankvnk 5:bd9705c7cf51 309 }
frankvnk 5:bd9705c7cf51 310 else if (strcmp(str, "Blue-") == 0)
frankvnk 5:bd9705c7cf51 311 {
frankvnk 5:bd9705c7cf51 312 RED_OFF; GREEN_OFF; BLUE_ON;
frankvnk 5:bd9705c7cf51 313 }
frankvnk 5:bd9705c7cf51 314 }
frankvnk 5:bd9705c7cf51 315
frankvnk 5:bd9705c7cf51 316 void getBatteryVoltageStr(char * str)
frankvnk 5:bd9705c7cf51 317 {
frankvnk 5:bd9705c7cf51 318 sprintf(str," "); //clears field (needed if previous string had more characters)
frankvnk 5:bd9705c7cf51 319 sprintf(str, "%d %%", adc_sample3);
frankvnk 5:bd9705c7cf51 320 }
frankvnk 5:bd9705c7cf51 321
frankvnk 5:bd9705c7cf51 322 void getLightVoltageStr(char * str)
frankvnk 5:bd9705c7cf51 323 {
frankvnk 5:bd9705c7cf51 324 int LightPercent = 0;
frankvnk 5:bd9705c7cf51 325 LightPercent = (axis6.light * 100) / 4096;
frankvnk 5:bd9705c7cf51 326 sprintf(str," "); //clears field (needed if previous string had more characters)
frankvnk 5:bd9705c7cf51 327 sprintf(str, "%d %%", LightPercent);
frankvnk 5:bd9705c7cf51 328 }
frankvnk 5:bd9705c7cf51 329
frankvnk 5:bd9705c7cf51 330 void getAccelXYZ_Str(char * str) // MMA8451Q accelerometer - report axis with highest value
frankvnk 5:bd9705c7cf51 331 {
frankvnk 5:bd9705c7cf51 332 sprintf(str," "); //clears field (needed if previous string had more characters)
frankvnk 5:bd9705c7cf51 333 sprintf(str, "X= %1.2f, Y= %1.2f, Z= %1.2f", axis6.fGax, axis6.fGay, axis6.fGaz);;
frankvnk 5:bd9705c7cf51 334 }
frankvnk 5:bd9705c7cf51 335
frankvnk 5:bd9705c7cf51 336 void getTemperatureStr(char * str) //
frankvnk 5:bd9705c7cf51 337 {
frankvnk 5:bd9705c7cf51 338 sprintf(str, "%+d C", axis6.temp);
frankvnk 5:bd9705c7cf51 339 }
frankvnk 5:bd9705c7cf51 340
frankvnk 5:bd9705c7cf51 341 void getTSI_sliderStr(char * str) // TSI Slider position
frankvnk 5:bd9705c7cf51 342 {
frankvnk 5:bd9705c7cf51 343 uint8_t slider_position;
frankvnk 5:bd9705c7cf51 344
frankvnk 5:bd9705c7cf51 345 slider_position = tsi.readPercentage() * 100; // Slider position as percentage
frankvnk 5:bd9705c7cf51 346 sprintf(str," "); //clears field (needed if previous string had more characters)
frankvnk 5:bd9705c7cf51 347 sprintf(str, "%d %%", slider_position);
frankvnk 5:bd9705c7cf51 348 }
frankvnk 5:bd9705c7cf51 349
frankvnk 5:bd9705c7cf51 350 void getCompassStr(char * str) // Mag3110 generated Compass bearing
frankvnk 5:bd9705c7cf51 351 {
frankvnk 5:bd9705c7cf51 352 char *compass_points[9] = {"North", "N-East", "East", "S-East", "South", "S-West", "West", "N-West", "North"};
frankvnk 5:bd9705c7cf51 353 signed short compass_bearing = (axis6.compass + 23) / 45;
frankvnk 5:bd9705c7cf51 354 sprintf(str," "); //clears field (needed if previous string had more characters)
frankvnk 5:bd9705c7cf51 355 sprintf(str, "Roll=%-d Pitch=%-d Yaw=%-d [%s]", axis6.roll, axis6.pitch, axis6.yaw, compass_points[compass_bearing]); //
frankvnk 5:bd9705c7cf51 356 }
frankvnk 5:bd9705c7cf51 357
frankvnk 5:bd9705c7cf51 358 void getM3110Str(char * str) // Mag3110 displayed in units of UT
frankvnk 5:bd9705c7cf51 359 {
frankvnk 5:bd9705c7cf51 360 sprintf(str," "); //clears field (needed if previous string had more characters)
frankvnk 5:bd9705c7cf51 361 sprintf(str, "X= %3.1f, Y= %3.1f, Z= %3.1f", axis6.fUTmx, axis6.fUTmy, axis6.fUTmz);
frankvnk 5:bd9705c7cf51 362 }
frankvnk 5:bd9705c7cf51 363
frankvnk 5:bd9705c7cf51 364 void getAltitudeStr(char * str) // Get Altitude
frankvnk 5:bd9705c7cf51 365 {
frankvnk 5:bd9705c7cf51 366 sprintf(str, "%+d meters", axis6.alt); // str = integer portion of result
frankvnk 5:bd9705c7cf51 367 }
frankvnk 5:bd9705c7cf51 368
frankvnk 5:bd9705c7cf51 369
frankvnk 5:bd9705c7cf51 370
frankvnk 5:bd9705c7cf51 371 //*****************************************************************************
frankvnk 5:bd9705c7cf51 372 //
frankvnk 5:bd9705c7cf51 373 //! \brief Main HTTP Server
frankvnk 5:bd9705c7cf51 374 //!
frankvnk 5:bd9705c7cf51 375 //! \param none
frankvnk 5:bd9705c7cf51 376 //!
frankvnk 5:bd9705c7cf51 377 //! \return none
frankvnk 5:bd9705c7cf51 378 //!
frankvnk 5:bd9705c7cf51 379 //
frankvnk 5:bd9705c7cf51 380 //*****************************************************************************
frankvnk 5:bd9705c7cf51 381 void serverMain(int port,
frankvnk 5:bd9705c7cf51 382 char * ipage,
frankvnk 5:bd9705c7cf51 383 cgi_handler * handleList,
frankvnk 5:bd9705c7cf51 384 dyn_html_handler * dhList)
frankvnk 5:bd9705c7cf51 385 {
frankvnk 5:bd9705c7cf51 386 static TCPSocketServer server;
frankvnk 5:bd9705c7cf51 387 static TCPSocketConnection client;
frankvnk 5:bd9705c7cf51 388
frankvnk 5:bd9705c7cf51 389 indexPage = ipage;
frankvnk 5:bd9705c7cf51 390 chList = handleList;
frankvnk 5:bd9705c7cf51 391 htmlList = dhList;
frankvnk 5:bd9705c7cf51 392
frankvnk 5:bd9705c7cf51 393 server.bind(port);
frankvnk 5:bd9705c7cf51 394
frankvnk 5:bd9705c7cf51 395 printf("Main HTTP server\r\n");
frankvnk 5:bd9705c7cf51 396
frankvnk 5:bd9705c7cf51 397 // Start Listening
frankvnk 5:bd9705c7cf51 398 if(server.listen() != 0);
frankvnk 5:bd9705c7cf51 399
frankvnk 5:bd9705c7cf51 400 // Handle Clients and Data
frankvnk 5:bd9705c7cf51 401 while(1)
frankvnk 5:bd9705c7cf51 402 {
frankvnk 5:bd9705c7cf51 403 int32_t status = server.accept(client);
frankvnk 5:bd9705c7cf51 404 if (status >= 0)
frankvnk 5:bd9705c7cf51 405 {
frankvnk 5:bd9705c7cf51 406 LED_D2_ON;
frankvnk 5:bd9705c7cf51 407 // Connection Accepted, Send Data
frankvnk 5:bd9705c7cf51 408 // Wait for a data update
frankvnk 5:bd9705c7cf51 409 client.set_blocking(true);
frankvnk 5:bd9705c7cf51 410 printf("Connection\r\n");
frankvnk 5:bd9705c7cf51 411 // printf("Connection from: %s \r\n", client.get_address());
frankvnk 5:bd9705c7cf51 412 if(newData) handleHTTPRequest(&client);
frankvnk 5:bd9705c7cf51 413 newData = 0;
frankvnk 5:bd9705c7cf51 414 client.close();
frankvnk 5:bd9705c7cf51 415 LED_D2_OFF;
frankvnk 5:bd9705c7cf51 416 }
frankvnk 5:bd9705c7cf51 417 else if(status == -57)
frankvnk 5:bd9705c7cf51 418 {
frankvnk 5:bd9705c7cf51 419 // BUG: Socket inactive so reopen socket
frankvnk 5:bd9705c7cf51 420 // Inactive Socket, close and reopen it
frankvnk 5:bd9705c7cf51 421 printf("Oops!!!\r\n");
frankvnk 5:bd9705c7cf51 422 server.close();
frankvnk 5:bd9705c7cf51 423 indexPage = ipage;
frankvnk 5:bd9705c7cf51 424 chList = handleList;
frankvnk 5:bd9705c7cf51 425 htmlList = dhList;
frankvnk 5:bd9705c7cf51 426 server.bind(port);
frankvnk 5:bd9705c7cf51 427
frankvnk 5:bd9705c7cf51 428 // Start Listening
frankvnk 5:bd9705c7cf51 429 if (server.listen() != 0);
frankvnk 5:bd9705c7cf51 430 }
frankvnk 5:bd9705c7cf51 431 }
frankvnk 5:bd9705c7cf51 432 }
frankvnk 5:bd9705c7cf51 433
frankvnk 5:bd9705c7cf51 434 //*****************************************************************************
frankvnk 5:bd9705c7cf51 435 //
frankvnk 5:bd9705c7cf51 436 //! \brief Handles HTTP Requests
frankvnk 5:bd9705c7cf51 437 //!
frankvnk 5:bd9705c7cf51 438 //! \param cnum is the client socket handle to be used
frankvnk 5:bd9705c7cf51 439 //!
frankvnk 5:bd9705c7cf51 440 //! \return none
frankvnk 5:bd9705c7cf51 441 //!
frankvnk 5:bd9705c7cf51 442 //
frankvnk 5:bd9705c7cf51 443 //*****************************************************************************
frankvnk 5:bd9705c7cf51 444 void handleHTTPRequest(TCPSocketConnection *client)
frankvnk 5:bd9705c7cf51 445 {
frankvnk 5:bd9705c7cf51 446 char * reqline[3];
frankvnk 5:bd9705c7cf51 447 char * cgiTok;
frankvnk 5:bd9705c7cf51 448
frankvnk 5:bd9705c7cf51 449 int i = 0;
frankvnk 5:bd9705c7cf51 450 char paramBuf[20];
frankvnk 5:bd9705c7cf51 451 int bytesRecvd;
frankvnk 5:bd9705c7cf51 452 char tempStr[40]; //PF was 26
frankvnk 5:bd9705c7cf51 453
frankvnk 5:bd9705c7cf51 454 memset(requestBuffer,0,sizeof (requestBuffer));
frankvnk 5:bd9705c7cf51 455 bytesRecvd = client->receive(requestBuffer, sizeof(requestBuffer));
frankvnk 5:bd9705c7cf51 456
frankvnk 5:bd9705c7cf51 457 printf("handleHTTPRequest\r\n");
frankvnk 5:bd9705c7cf51 458
frankvnk 5:bd9705c7cf51 459 if(bytesRecvd > 0)
frankvnk 5:bd9705c7cf51 460 {
frankvnk 5:bd9705c7cf51 461 // Received some data, check it and send data back
frankvnk 5:bd9705c7cf51 462 reqline[0] = strstr(requestBuffer, "GET");
frankvnk 5:bd9705c7cf51 463 if ( reqline[0] != NULL )
frankvnk 5:bd9705c7cf51 464 {
frankvnk 5:bd9705c7cf51 465 if (strstr (requestBuffer, "HTTP/1.0") != NULL && strstr (requestBuffer, "HTTP/1.1") != NULL )
frankvnk 5:bd9705c7cf51 466 {
frankvnk 5:bd9705c7cf51 467 client->send_all("HTTP/1.0 400 Bad Request\n", 25);
frankvnk 5:bd9705c7cf51 468 }
frankvnk 5:bd9705c7cf51 469 else
frankvnk 5:bd9705c7cf51 470 {
frankvnk 5:bd9705c7cf51 471
frankvnk 5:bd9705c7cf51 472 #ifdef HTTP_CGI_ENABLED
frankvnk 5:bd9705c7cf51 473 // Do we have CGI parameters we need to parse?
frankvnk 5:bd9705c7cf51 474 if(strchr(requestBuffer, '?') != NULL)
frankvnk 5:bd9705c7cf51 475 {
frankvnk 5:bd9705c7cf51 476 // Decode URL and handle each parameter sequentially
frankvnk 5:bd9705c7cf51 477 // according to table previously setup.
frankvnk 5:bd9705c7cf51 478 cgiTok = strstr(requestBuffer,"=");
frankvnk 5:bd9705c7cf51 479 if(cgiTok != NULL)
frankvnk 5:bd9705c7cf51 480 {
frankvnk 5:bd9705c7cf51 481 memset(paramBuf,0,sizeof(paramBuf));
frankvnk 5:bd9705c7cf51 482 memcpy(paramBuf,cgiTok+1,5); // hard-coded for demo: 5 character parameter (-Red-/Green/Blue-)
frankvnk 5:bd9705c7cf51 483 chList->cgiHandlerFunc[0](paramBuf);
frankvnk 5:bd9705c7cf51 484
frankvnk 5:bd9705c7cf51 485 }
frankvnk 5:bd9705c7cf51 486 }
frankvnk 5:bd9705c7cf51 487 #endif
frankvnk 5:bd9705c7cf51 488
frankvnk 5:bd9705c7cf51 489 #ifdef HTTP_DYN_HTML_ENABLED
frankvnk 5:bd9705c7cf51 490 // The code below replaces data in the HTML page
frankvnk 5:bd9705c7cf51 491 // with that generated by the specified functions.
frankvnk 5:bd9705c7cf51 492 for(i = 0; i < 9; i++) // change the range here for more dynamic fields on webpage
frankvnk 5:bd9705c7cf51 493 {
frankvnk 5:bd9705c7cf51 494 memset(tempStr,0,sizeof(tempStr));
frankvnk 5:bd9705c7cf51 495 htmlList->dynHtmlFunc[i](tempStr);
frankvnk 5:bd9705c7cf51 496 tempStr[strlen(tempStr)]= ' ';
frankvnk 5:bd9705c7cf51 497 pageReplace((char *)indexPage,
frankvnk 5:bd9705c7cf51 498 (char *)htmlList->dynHtmlParamName[i],
frankvnk 5:bd9705c7cf51 499 (char *)tempStr);
frankvnk 5:bd9705c7cf51 500 }
frankvnk 5:bd9705c7cf51 501 #endif
frankvnk 5:bd9705c7cf51 502 viewCounter++;
frankvnk 5:bd9705c7cf51 503 sendHTTPData(HTTP_RESP, strlen(HTTP_RESP), client);
frankvnk 5:bd9705c7cf51 504
frankvnk 5:bd9705c7cf51 505 for(i = 0; i < strlen(indexPage); i += HTTP_TX_BLOCK_SIZE)
frankvnk 5:bd9705c7cf51 506 {
frankvnk 5:bd9705c7cf51 507 if(strlen(indexPage) - i < HTTP_TX_BLOCK_SIZE)
frankvnk 5:bd9705c7cf51 508 {
frankvnk 5:bd9705c7cf51 509 sendHTTPData(&indexPage[i], strlen(indexPage) - i, client);
frankvnk 5:bd9705c7cf51 510 }
frankvnk 5:bd9705c7cf51 511 else
frankvnk 5:bd9705c7cf51 512 {
frankvnk 5:bd9705c7cf51 513 sendHTTPData(&indexPage[i], HTTP_TX_BLOCK_SIZE, client);
frankvnk 5:bd9705c7cf51 514 }
frankvnk 5:bd9705c7cf51 515 }
frankvnk 5:bd9705c7cf51 516 }
frankvnk 5:bd9705c7cf51 517 }
frankvnk 5:bd9705c7cf51 518 }
frankvnk 5:bd9705c7cf51 519 }
frankvnk 5:bd9705c7cf51 520
frankvnk 5:bd9705c7cf51 521 //*****************************************************************************
frankvnk 5:bd9705c7cf51 522 //
frankvnk 5:bd9705c7cf51 523 //! \brief Inserts characters in page that appear after an indicator ind
frankvnk 5:bd9705c7cf51 524 //! with the value from val
frankvnk 5:bd9705c7cf51 525 //!
frankvnk 5:bd9705c7cf51 526 //! \param page is a pointer to the array holding the page's HTML code
frankvnk 5:bd9705c7cf51 527 //! \param ind is a pointer to a string that has the name of the parameter on the page to modify
frankvnk 5:bd9705c7cf51 528 //! \param val is the pointer to a string holding the string to insert in the XXX
frankvnk 5:bd9705c7cf51 529 //!
frankvnk 5:bd9705c7cf51 530 //! \return none
frankvnk 5:bd9705c7cf51 531 //!
frankvnk 5:bd9705c7cf51 532 //
frankvnk 5:bd9705c7cf51 533 //*****************************************************************************
frankvnk 5:bd9705c7cf51 534 void pageReplace(char * page, char * ind, char * val)
frankvnk 5:bd9705c7cf51 535 {
frankvnk 5:bd9705c7cf51 536 char * indicLoc;
frankvnk 5:bd9705c7cf51 537 indicLoc = strstr (page,ind);
frankvnk 5:bd9705c7cf51 538 memcpy(indicLoc+strlen(ind), val, strlen(val));
frankvnk 5:bd9705c7cf51 539 }
frankvnk 5:bd9705c7cf51 540
frankvnk 5:bd9705c7cf51 541 //*****************************************************************************
frankvnk 5:bd9705c7cf51 542 //
frankvnk 5:bd9705c7cf51 543 //! \brief Returns a string with the number of views of the page
frankvnk 5:bd9705c7cf51 544 //!
frankvnk 5:bd9705c7cf51 545 //! \param str is a pointer to the array where the number of views will be put
frankvnk 5:bd9705c7cf51 546 //!
frankvnk 5:bd9705c7cf51 547 //! \return none
frankvnk 5:bd9705c7cf51 548 //!
frankvnk 5:bd9705c7cf51 549 //
frankvnk 5:bd9705c7cf51 550 //*****************************************************************************
frankvnk 5:bd9705c7cf51 551 void getViewsNum(char * str)
frankvnk 5:bd9705c7cf51 552 {
frankvnk 5:bd9705c7cf51 553 sprintf(str, "%d", viewCounter);
frankvnk 5:bd9705c7cf51 554 }
frankvnk 5:bd9705c7cf51 555
frankvnk 5:bd9705c7cf51 556 //*****************************************************************************
frankvnk 5:bd9705c7cf51 557 //
frankvnk 5:bd9705c7cf51 558 //! \brief Sends HTTP Data
frankvnk 5:bd9705c7cf51 559 //!
frankvnk 5:bd9705c7cf51 560 //! \param sdesc is the socket descriptor of the socket used for sending data
frankvnk 5:bd9705c7cf51 561 //! \param buf is a pointer to the buffer with the data to be sent
frankvnk 5:bd9705c7cf51 562 //! \param len is the number of bytes to send
frankvnk 5:bd9705c7cf51 563 //!
frankvnk 5:bd9705c7cf51 564 //! \return none
frankvnk 5:bd9705c7cf51 565 //!
frankvnk 5:bd9705c7cf51 566 //
frankvnk 5:bd9705c7cf51 567 //*****************************************************************************
frankvnk 5:bd9705c7cf51 568 void sendHTTPData(char * buf, long len, TCPSocketConnection *client)
frankvnk 5:bd9705c7cf51 569 {
frankvnk 5:bd9705c7cf51 570 int bytesSent = -2;
frankvnk 5:bd9705c7cf51 571 while(bytesSent == -2) bytesSent = client->send_all(buf, len);
frankvnk 5:bd9705c7cf51 572 }
frankvnk 5:bd9705c7cf51 573
frankvnk 5:bd9705c7cf51 574
frankvnk 5:bd9705c7cf51 575