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:
Sat Feb 28 20:23:30 2015 +0000
Revision:
7:9d86d022fa68
Parent:
4:8f8d0d19e6f1
Fixed Webserver lockup by skipping all code in the SysTick_Handler while HTTP data is sent.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frankvnk 4:8f8d0d19e6f1 1 /*****************************************************************************
frankvnk 4:8f8d0d19e6f1 2 *
frankvnk 4:8f8d0d19e6f1 3 * demo.c - CC3000 Main Demo Application
frankvnk 4:8f8d0d19e6f1 4 * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
frankvnk 4:8f8d0d19e6f1 5 *
frankvnk 4:8f8d0d19e6f1 6 * Redistribution and use in source and binary forms, with or without
frankvnk 4:8f8d0d19e6f1 7 * modification, are permitted provided that the following conditions
frankvnk 4:8f8d0d19e6f1 8 * are met:
frankvnk 4:8f8d0d19e6f1 9 *
frankvnk 4:8f8d0d19e6f1 10 * Redistributions of source code must retain the above copyright
frankvnk 4:8f8d0d19e6f1 11 * notice, this list of conditions and the following disclaimer.
frankvnk 4:8f8d0d19e6f1 12 *
frankvnk 4:8f8d0d19e6f1 13 * Redistributions in binary form must reproduce the above copyright
frankvnk 4:8f8d0d19e6f1 14 * notice, this list of conditions and the following disclaimer in the
frankvnk 4:8f8d0d19e6f1 15 * documentation and/or other materials provided with the
frankvnk 4:8f8d0d19e6f1 16 * distribution.
frankvnk 4:8f8d0d19e6f1 17 *
frankvnk 4:8f8d0d19e6f1 18 * Neither the name of Texas Instruments Incorporated nor the names of
frankvnk 4:8f8d0d19e6f1 19 * its contributors may be used to endorse or promote products derived
frankvnk 4:8f8d0d19e6f1 20 * from this software without specific prior written permission.
frankvnk 4:8f8d0d19e6f1 21 *
frankvnk 4:8f8d0d19e6f1 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
frankvnk 4:8f8d0d19e6f1 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
frankvnk 4:8f8d0d19e6f1 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
frankvnk 4:8f8d0d19e6f1 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
frankvnk 4:8f8d0d19e6f1 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
frankvnk 4:8f8d0d19e6f1 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
frankvnk 4:8f8d0d19e6f1 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
frankvnk 4:8f8d0d19e6f1 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
frankvnk 4:8f8d0d19e6f1 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
frankvnk 4:8f8d0d19e6f1 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
frankvnk 4:8f8d0d19e6f1 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
frankvnk 4:8f8d0d19e6f1 33 *
frankvnk 4:8f8d0d19e6f1 34 *****************************************************************************/
frankvnk 4:8f8d0d19e6f1 35
frankvnk 4:8f8d0d19e6f1 36 #include "run_exosite.h"
frankvnk 4:8f8d0d19e6f1 37 #include "Wi-Go_eCompass_Lib_V3.h"
frankvnk 4:8f8d0d19e6f1 38 #include "mbed.h"
frankvnk 4:8f8d0d19e6f1 39 #include "defLED.h"
frankvnk 4:8f8d0d19e6f1 40 #include "strlib.h"
frankvnk 4:8f8d0d19e6f1 41
frankvnk 4:8f8d0d19e6f1 42 extern DigitalOut ledr;
frankvnk 4:8f8d0d19e6f1 43 extern DigitalOut ledg;
frankvnk 4:8f8d0d19e6f1 44 extern DigitalOut ledb;
frankvnk 4:8f8d0d19e6f1 45 extern DigitalOut led1;
frankvnk 4:8f8d0d19e6f1 46 extern DigitalOut led2;
frankvnk 4:8f8d0d19e6f1 47 extern DigitalOut led3;
frankvnk 4:8f8d0d19e6f1 48
frankvnk 4:8f8d0d19e6f1 49 // local defines
frankvnk 4:8f8d0d19e6f1 50 #define WRITE_INTERVAL 5
frankvnk 4:8f8d0d19e6f1 51 #define EXO_BUFFER_SIZE 300 //reserve 300 bytes for our output buffer
frankvnk 4:8f8d0d19e6f1 52
frankvnk 4:8f8d0d19e6f1 53 typedef struct
frankvnk 4:8f8d0d19e6f1 54 {
frankvnk 4:8f8d0d19e6f1 55 float *p;
frankvnk 4:8f8d0d19e6f1 56 char *s;
frankvnk 4:8f8d0d19e6f1 57 } exo_data_ft;
frankvnk 4:8f8d0d19e6f1 58
frankvnk 4:8f8d0d19e6f1 59 typedef struct
frankvnk 4:8f8d0d19e6f1 60 {
frankvnk 4:8f8d0d19e6f1 61 int16_t *p;
frankvnk 4:8f8d0d19e6f1 62 char *s;
frankvnk 4:8f8d0d19e6f1 63 } exo_data_it;
frankvnk 4:8f8d0d19e6f1 64
frankvnk 4:8f8d0d19e6f1 65 //extern tUserFS user_info;
frankvnk 4:8f8d0d19e6f1 66
frankvnk 4:8f8d0d19e6f1 67 extern char requestBuffer[];
frankvnk 4:8f8d0d19e6f1 68
frankvnk 4:8f8d0d19e6f1 69 extern unsigned int compass_type, seconds;
frankvnk 4:8f8d0d19e6f1 70
frankvnk 4:8f8d0d19e6f1 71 char exo_meta[META_SIZE];
frankvnk 4:8f8d0d19e6f1 72
frankvnk 4:8f8d0d19e6f1 73 #define RX_SIZE 60
frankvnk 4:8f8d0d19e6f1 74 #define MAC_LEN 6
frankvnk 4:8f8d0d19e6f1 75
frankvnk 4:8f8d0d19e6f1 76 typedef enum
frankvnk 4:8f8d0d19e6f1 77 {
frankvnk 4:8f8d0d19e6f1 78 CIK_LINE,
frankvnk 4:8f8d0d19e6f1 79 HOST_LINE,
frankvnk 4:8f8d0d19e6f1 80 CONTENT_LINE,
frankvnk 4:8f8d0d19e6f1 81 ACCEPT_LINE,
frankvnk 4:8f8d0d19e6f1 82 LENGTH_LINE,
frankvnk 4:8f8d0d19e6f1 83 GETDATA_LINE,
frankvnk 4:8f8d0d19e6f1 84 POSTDATA_LINE,
frankvnk 4:8f8d0d19e6f1 85 VENDOR_LINE,
frankvnk 4:8f8d0d19e6f1 86 EMPTY_LINE
frankvnk 4:8f8d0d19e6f1 87 } lineTypes;
frankvnk 4:8f8d0d19e6f1 88
frankvnk 4:8f8d0d19e6f1 89 #define STR_CIK_HEADER "X-Exosite-CIK: c3c675e0601551c6d4d3230e162d62cc8bba3311\r\n"
frankvnk 4:8f8d0d19e6f1 90 #define STR_CONTENT_LENGTH "Content-Length: "
frankvnk 4:8f8d0d19e6f1 91 #define STR_GET_URL "GET /api:v1/stack/alias?"
frankvnk 4:8f8d0d19e6f1 92 #define STR_HTTP " HTTP/1.1\r\n"
frankvnk 4:8f8d0d19e6f1 93 #define STR_HOST "Host: avnet.m2.exosite.com\r\n"
frankvnk 4:8f8d0d19e6f1 94 #define STR_ACCEPT "Accept: application/x-www-form-urlencoded; charset=utf-8\r\n"
frankvnk 4:8f8d0d19e6f1 95 #define STR_CONTENT "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"
frankvnk 4:8f8d0d19e6f1 96 #define STR_VENDOR "vendor=avnet&model=wigosmartconfig&sn="
frankvnk 4:8f8d0d19e6f1 97 #define STR_CRLF "\r\n"
frankvnk 4:8f8d0d19e6f1 98 #define MY_CIK "27b9684751332589be52c8e5819e7c438f7e7479"
frankvnk 4:8f8d0d19e6f1 99 char myCIK[] = "c3c675e0601551c6d4d3230e162d62cc8bba3311";
frankvnk 4:8f8d0d19e6f1 100
frankvnk 4:8f8d0d19e6f1 101 // local functions
frankvnk 4:8f8d0d19e6f1 102 void activate_device(TCPSocketConnection *socket);
frankvnk 4:8f8d0d19e6f1 103 int readResponse(TCPSocketConnection *socket, char * expectedCode);
frankvnk 4:8f8d0d19e6f1 104 long connect_to_exosite(TCPSocketConnection *socket);
frankvnk 4:8f8d0d19e6f1 105 void sendLine(TCPSocketConnection *socket, unsigned char LINE, char * payload);
frankvnk 4:8f8d0d19e6f1 106 void exosite_meta_write(unsigned char * write_buffer, unsigned short srcBytes, unsigned char element);
frankvnk 4:8f8d0d19e6f1 107 void exosite_meta_read(unsigned char * read_buffer, unsigned short destBytes, unsigned char element);
frankvnk 4:8f8d0d19e6f1 108 int Exosite_Write(TCPSocketConnection *socket, char * pbuf, unsigned char bufsize);
frankvnk 4:8f8d0d19e6f1 109 int Exosite_Read(char * palias, char * pbuf, unsigned char bufsize);
frankvnk 4:8f8d0d19e6f1 110 int Exosite_Init(TCPSocketConnection *socket);
frankvnk 4:8f8d0d19e6f1 111
frankvnk 4:8f8d0d19e6f1 112 // global variables
frankvnk 4:8f8d0d19e6f1 113 static unsigned char exositeWriteFailures = 0;
frankvnk 4:8f8d0d19e6f1 114
frankvnk 4:8f8d0d19e6f1 115 // exported functions
frankvnk 4:8f8d0d19e6f1 116
frankvnk 4:8f8d0d19e6f1 117 // externs
frankvnk 4:8f8d0d19e6f1 118 extern char *itoa(int n, char *s, int b);
frankvnk 4:8f8d0d19e6f1 119
frankvnk 4:8f8d0d19e6f1 120 extern axis6_t axis6;
frankvnk 4:8f8d0d19e6f1 121 extern int secondFlag;
frankvnk 4:8f8d0d19e6f1 122 extern int server_running;
frankvnk 4:8f8d0d19e6f1 123
frankvnk 4:8f8d0d19e6f1 124 /** \brief Definition of data packet to be sent by server */
frankvnk 4:8f8d0d19e6f1 125 unsigned char dataPacket[] = { '\r', 0xBE, 128, 128, 128, 70, 36, 0xEF };
frankvnk 4:8f8d0d19e6f1 126 char activeCIK[41];
frankvnk 4:8f8d0d19e6f1 127
frankvnk 4:8f8d0d19e6f1 128 exo_data_ft exo_fdata[] = {
frankvnk 4:8f8d0d19e6f1 129 &axis6.fGax, "acc_x=%f&",
frankvnk 4:8f8d0d19e6f1 130 &axis6.fGay, "acc_y=%f&",
frankvnk 4:8f8d0d19e6f1 131 &axis6.fGaz, "acc_z=%f&",
frankvnk 4:8f8d0d19e6f1 132 &axis6.fUTmx, "mag_x=%f&",
frankvnk 4:8f8d0d19e6f1 133 &axis6.fUTmy, "mag_y=%f&",
frankvnk 4:8f8d0d19e6f1 134 &axis6.fUTmz, "mag_z=%f&",
frankvnk 4:8f8d0d19e6f1 135 &axis6.q0, "q_w=%f&",
frankvnk 4:8f8d0d19e6f1 136 &axis6.q1, "q_x=%f&",
frankvnk 4:8f8d0d19e6f1 137 &axis6.q2, "q_y=%f&",
frankvnk 4:8f8d0d19e6f1 138 &axis6.q3, "q_z=%f&",
frankvnk 4:8f8d0d19e6f1 139 0, 0 };
frankvnk 4:8f8d0d19e6f1 140
frankvnk 4:8f8d0d19e6f1 141 exo_data_it exo_idata[] = {
frankvnk 4:8f8d0d19e6f1 142 &axis6.roll, "roll=%d&",
frankvnk 4:8f8d0d19e6f1 143 &axis6.pitch, "pitch=%d&",
frankvnk 4:8f8d0d19e6f1 144 &axis6.yaw, "yaw=%d&",
frankvnk 4:8f8d0d19e6f1 145 &axis6.alt, "alt=%d&",
frankvnk 4:8f8d0d19e6f1 146 &axis6.temp, "temp=%d&",
frankvnk 4:8f8d0d19e6f1 147 &axis6.light, "light=%d&",
frankvnk 4:8f8d0d19e6f1 148 0, 0
frankvnk 4:8f8d0d19e6f1 149 };
frankvnk 4:8f8d0d19e6f1 150
frankvnk 4:8f8d0d19e6f1 151 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 152 //
frankvnk 4:8f8d0d19e6f1 153 //! main
frankvnk 4:8f8d0d19e6f1 154 //!
frankvnk 4:8f8d0d19e6f1 155 //! \param None
frankvnk 4:8f8d0d19e6f1 156 //!
frankvnk 4:8f8d0d19e6f1 157 //! \return none
frankvnk 4:8f8d0d19e6f1 158 //!
frankvnk 4:8f8d0d19e6f1 159 //! \brief The main loop is executed here
frankvnk 4:8f8d0d19e6f1 160 //
frankvnk 4:8f8d0d19e6f1 161 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 162 void run_exosite(TCPSocketConnection *socket)
frankvnk 4:8f8d0d19e6f1 163 {
frankvnk 4:8f8d0d19e6f1 164 int value;
frankvnk 4:8f8d0d19e6f1 165 exo_data_it *tsp;
frankvnk 4:8f8d0d19e6f1 166 exo_data_ft *tspf;
frankvnk 4:8f8d0d19e6f1 167
frankvnk 4:8f8d0d19e6f1 168 printf("\nConnecting to Exosite\n");
frankvnk 4:8f8d0d19e6f1 169 //user_info.validCIK = 0; // uncomment this to force provisioning every time, only used for debug
frankvnk 4:8f8d0d19e6f1 170
frankvnk 4:8f8d0d19e6f1 171 if(!user_info.validCIK)
frankvnk 4:8f8d0d19e6f1 172 {
frankvnk 4:8f8d0d19e6f1 173 printf("Activating Wi-Go System on Exosite\n");
frankvnk 4:8f8d0d19e6f1 174 uint8_t myMAC[8];
frankvnk 4:8f8d0d19e6f1 175 wifi.get_mac_address(myMAC);
frankvnk 4:8f8d0d19e6f1 176 printf(" MAC address %02x:%02x:%02x:%02x:%02x:%02x \r\n \r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
frankvnk 4:8f8d0d19e6f1 177 wait_ms(100);
frankvnk 4:8f8d0d19e6f1 178 printf("Enter the device MAC on your Exosite device portal to prepare\n");
frankvnk 4:8f8d0d19e6f1 179 wait_ms(100);
frankvnk 4:8f8d0d19e6f1 180 printf("the system for provisioning your Wi-Go system onto Exosite\n");
frankvnk 4:8f8d0d19e6f1 181 wait_ms(100);
frankvnk 4:8f8d0d19e6f1 182 printf("Hit any key to continue.....\n");
frankvnk 4:8f8d0d19e6f1 183 getchar();
frankvnk 4:8f8d0d19e6f1 184 Exosite_Init(socket);
frankvnk 4:8f8d0d19e6f1 185 }
frankvnk 4:8f8d0d19e6f1 186
frankvnk 4:8f8d0d19e6f1 187 for(value=0 ;value < CIK_LENGTH ; value++) activeCIK[value] = user_info.CIK[value];
frankvnk 4:8f8d0d19e6f1 188 activeCIK[value] = NULL;
frankvnk 4:8f8d0d19e6f1 189 // Main Loop
frankvnk 4:8f8d0d19e6f1 190 while (1)
frankvnk 4:8f8d0d19e6f1 191 {
frankvnk 4:8f8d0d19e6f1 192 while(!secondFlag) /*__wfi()*/;
frankvnk 4:8f8d0d19e6f1 193 secondFlag = 0;
frankvnk 4:8f8d0d19e6f1 194 LED_D2_ON;
frankvnk 4:8f8d0d19e6f1 195
frankvnk 4:8f8d0d19e6f1 196 // Build string
frankvnk 4:8f8d0d19e6f1 197 requestBuffer[0] = 0;
frankvnk 4:8f8d0d19e6f1 198 tsp = &exo_idata[0];
frankvnk 4:8f8d0d19e6f1 199 tspf = &exo_fdata[0];
frankvnk 4:8f8d0d19e6f1 200 while(tsp->p)
frankvnk 4:8f8d0d19e6f1 201 {
frankvnk 4:8f8d0d19e6f1 202 sprintf( requestBuffer + strlen(requestBuffer), tsp->s, *tsp->p);
frankvnk 4:8f8d0d19e6f1 203 tsp++;
frankvnk 4:8f8d0d19e6f1 204 }
frankvnk 4:8f8d0d19e6f1 205 while(tspf->p)
frankvnk 4:8f8d0d19e6f1 206 {
frankvnk 4:8f8d0d19e6f1 207 sprintf( requestBuffer + strlen(requestBuffer), tspf->s, *tspf->p);
frankvnk 4:8f8d0d19e6f1 208 tspf++;
frankvnk 4:8f8d0d19e6f1 209 }
frankvnk 4:8f8d0d19e6f1 210 sprintf( requestBuffer + strlen(requestBuffer), "time=%d\r\n", axis6.timestamp);
frankvnk 4:8f8d0d19e6f1 211
frankvnk 4:8f8d0d19e6f1 212 if(strlen(requestBuffer) > 300) printf("Buffer size= %d too small!!!", strlen(requestBuffer));
frankvnk 4:8f8d0d19e6f1 213 //printf("Buffer size= %d\n", strlen(requestBuffer));
frankvnk 4:8f8d0d19e6f1 214 value = Exosite_Write(socket, requestBuffer, strlen(requestBuffer)); //write all sensor values to the cloud
frankvnk 4:8f8d0d19e6f1 215 LED_D2_OFF;
frankvnk 4:8f8d0d19e6f1 216 if(value == -1)
frankvnk 4:8f8d0d19e6f1 217 {
frankvnk 4:8f8d0d19e6f1 218 LED_D3_OFF;
frankvnk 4:8f8d0d19e6f1 219 wifi.stop();
frankvnk 4:8f8d0d19e6f1 220 if (wifi.connect() == -1)
frankvnk 4:8f8d0d19e6f1 221 printf("Failed to connect. Please verify connection details and try again. \r\n");
frankvnk 4:8f8d0d19e6f1 222 else
frankvnk 4:8f8d0d19e6f1 223 {
frankvnk 4:8f8d0d19e6f1 224 printf("Connected - IP address: %s \r\n",wifi.getIPAddress());
frankvnk 4:8f8d0d19e6f1 225 LED_D3_ON;
frankvnk 4:8f8d0d19e6f1 226 }
frankvnk 4:8f8d0d19e6f1 227 }
frankvnk 4:8f8d0d19e6f1 228 }
frankvnk 4:8f8d0d19e6f1 229 }
frankvnk 4:8f8d0d19e6f1 230
frankvnk 4:8f8d0d19e6f1 231 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 232 //
frankvnk 4:8f8d0d19e6f1 233 //! Exosite_Init
frankvnk 4:8f8d0d19e6f1 234 //!
frankvnk 4:8f8d0d19e6f1 235 //! \param None
frankvnk 4:8f8d0d19e6f1 236 //!
frankvnk 4:8f8d0d19e6f1 237 //! \return 0 success; -1 failure
frankvnk 4:8f8d0d19e6f1 238 //!
frankvnk 4:8f8d0d19e6f1 239 //! \brief The function initializes the cloud connection to Exosite
frankvnk 4:8f8d0d19e6f1 240 //
frankvnk 4:8f8d0d19e6f1 241 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 242 int Exosite_Init(TCPSocketConnection *socket)
frankvnk 4:8f8d0d19e6f1 243 {
frankvnk 4:8f8d0d19e6f1 244 char strBuf[META_MARK_SIZE];
frankvnk 4:8f8d0d19e6f1 245 const unsigned char meta_server_ip[6] = {173,255,209,28,0,80};
frankvnk 4:8f8d0d19e6f1 246 uint8_t mac_status = 1;
frankvnk 4:8f8d0d19e6f1 247 uint8_t myMAC[8];
frankvnk 4:8f8d0d19e6f1 248 int i;
frankvnk 4:8f8d0d19e6f1 249 char tempCIK[CIK_LENGTH];
frankvnk 4:8f8d0d19e6f1 250
frankvnk 4:8f8d0d19e6f1 251 //check our meta mark - if it isn't there, we wipe the meta structure
frankvnk 4:8f8d0d19e6f1 252 exosite_meta_read((unsigned char *)strBuf, META_MARK_SIZE, META_MARK);
frankvnk 4:8f8d0d19e6f1 253 if (strncmp(strBuf, EXOMARK, META_MARK_SIZE))
frankvnk 4:8f8d0d19e6f1 254 {
frankvnk 4:8f8d0d19e6f1 255 memset(exo_meta, 0, META_SIZE); //erase the information currently in meta
frankvnk 4:8f8d0d19e6f1 256 exosite_meta_write((unsigned char *)meta_server_ip, 6, META_SERVER); //store server IP
frankvnk 4:8f8d0d19e6f1 257 exosite_meta_write((unsigned char *)EXOMARK, META_MARK_SIZE, META_MARK); //store exosite mark
frankvnk 4:8f8d0d19e6f1 258 }
frankvnk 4:8f8d0d19e6f1 259
frankvnk 4:8f8d0d19e6f1 260 while(mac_status) mac_status = wifi.get_mac_address(myMAC);
frankvnk 4:8f8d0d19e6f1 261 exosite_meta_write((unsigned char *)myMAC, 17, META_UUID);
frankvnk 4:8f8d0d19e6f1 262
frankvnk 4:8f8d0d19e6f1 263 //setup some of our globals for operation
frankvnk 4:8f8d0d19e6f1 264 exositeWriteFailures = 0;
frankvnk 4:8f8d0d19e6f1 265
frankvnk 4:8f8d0d19e6f1 266 //exosite Re-init : Called after Init has been ran in the past, but maybe
frankvnk 4:8f8d0d19e6f1 267 // comms were down and we have to keep trying...
frankvnk 4:8f8d0d19e6f1 268 activate_device(socket); //the moment of truth - can this device provision with the Exosite cloud?...
frankvnk 4:8f8d0d19e6f1 269 exosite_meta_read((unsigned char *)tempCIK, CIK_LENGTH, META_CIK); //sanity check on the CIK
frankvnk 4:8f8d0d19e6f1 270 for (i = 0; i < CIK_LENGTH; i++)
frankvnk 4:8f8d0d19e6f1 271 {
frankvnk 4:8f8d0d19e6f1 272 if (!(tempCIK[i] >= 'a' && tempCIK[i] <= 'f' || tempCIK[i] >= '0' && tempCIK[i] <= '9'))
frankvnk 4:8f8d0d19e6f1 273 {
frankvnk 4:8f8d0d19e6f1 274 return -1;
frankvnk 4:8f8d0d19e6f1 275 }
frankvnk 4:8f8d0d19e6f1 276 }
frankvnk 4:8f8d0d19e6f1 277 for(i=0;i<CIK_LENGTH;i++) user_info.CIK[i] = tempCIK[i];
frankvnk 4:8f8d0d19e6f1 278 user_info.validCIK = 1;
frankvnk 4:8f8d0d19e6f1 279 wifi._nvmem.write( NVMEM_USER_FILE_1_FILEID, sizeof(user_info), 0, (unsigned char *) &user_info);
frankvnk 4:8f8d0d19e6f1 280 return 0;
frankvnk 4:8f8d0d19e6f1 281 }
frankvnk 4:8f8d0d19e6f1 282
frankvnk 4:8f8d0d19e6f1 283 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 284 //
frankvnk 4:8f8d0d19e6f1 285 //! Exosite_Write
frankvnk 4:8f8d0d19e6f1 286 //!
frankvnk 4:8f8d0d19e6f1 287 //! \param pbuf - string buffer containing data to be sent
frankvnk 4:8f8d0d19e6f1 288 //! bufsize - number of bytes to send
frankvnk 4:8f8d0d19e6f1 289 //!
frankvnk 4:8f8d0d19e6f1 290 //! \return 0 success; -1 failure
frankvnk 4:8f8d0d19e6f1 291 //!
frankvnk 4:8f8d0d19e6f1 292 //! \brief The function writes data to Exosite
frankvnk 4:8f8d0d19e6f1 293 //
frankvnk 4:8f8d0d19e6f1 294 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 295 int Exosite_Write(TCPSocketConnection *socket, char * pbuf, unsigned char bufsize)
frankvnk 4:8f8d0d19e6f1 296 {
frankvnk 4:8f8d0d19e6f1 297 char strBuf[10];
frankvnk 4:8f8d0d19e6f1 298 long sock = -1;
frankvnk 4:8f8d0d19e6f1 299
frankvnk 4:8f8d0d19e6f1 300 sock = connect_to_exosite(socket);
frankvnk 4:8f8d0d19e6f1 301 if(sock == -1) return(sock);
frankvnk 4:8f8d0d19e6f1 302
frankvnk 4:8f8d0d19e6f1 303 // This is an example write POST...
frankvnk 4:8f8d0d19e6f1 304 // s.send('POST /api:v1/stack/alias HTTP/1.1\r\n')
frankvnk 4:8f8d0d19e6f1 305 // s.send('Host: m2.exosite.com\r\n')
frankvnk 4:8f8d0d19e6f1 306 // s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n')
frankvnk 4:8f8d0d19e6f1 307 // s.send('Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n')
frankvnk 4:8f8d0d19e6f1 308 // s.send('Content-Length: 6\r\n\r\n')
frankvnk 4:8f8d0d19e6f1 309 // s.send('temp=2')
frankvnk 4:8f8d0d19e6f1 310
frankvnk 4:8f8d0d19e6f1 311 sprintf(strBuf, "%d", (int)bufsize); //make a string for length
frankvnk 4:8f8d0d19e6f1 312
frankvnk 4:8f8d0d19e6f1 313 sendLine(socket, POSTDATA_LINE, "/onep:v1/stack/alias");
frankvnk 4:8f8d0d19e6f1 314 sendLine(socket, HOST_LINE, NULL);
frankvnk 4:8f8d0d19e6f1 315 sendLine(socket, CIK_LINE, NULL);
frankvnk 4:8f8d0d19e6f1 316 sendLine(socket, CONTENT_LINE, NULL);
frankvnk 4:8f8d0d19e6f1 317 sendLine(socket, LENGTH_LINE, strBuf);
frankvnk 4:8f8d0d19e6f1 318 //printf("Data=%s\n", pbuf);
frankvnk 4:8f8d0d19e6f1 319 socket->send_all(pbuf, bufsize); //alias=value
frankvnk 4:8f8d0d19e6f1 320 wifi._event.hci_unsolicited_event_handler();
frankvnk 4:8f8d0d19e6f1 321
frankvnk 4:8f8d0d19e6f1 322 if (0 == readResponse(socket, "204"))
frankvnk 4:8f8d0d19e6f1 323 {
frankvnk 4:8f8d0d19e6f1 324 exositeWriteFailures = 0;
frankvnk 4:8f8d0d19e6f1 325 }
frankvnk 4:8f8d0d19e6f1 326 else
frankvnk 4:8f8d0d19e6f1 327 {
frankvnk 4:8f8d0d19e6f1 328 exositeWriteFailures++;
frankvnk 4:8f8d0d19e6f1 329 }
frankvnk 4:8f8d0d19e6f1 330
frankvnk 4:8f8d0d19e6f1 331 socket->close();
frankvnk 4:8f8d0d19e6f1 332
frankvnk 4:8f8d0d19e6f1 333 if (exositeWriteFailures > 5)
frankvnk 4:8f8d0d19e6f1 334 {
frankvnk 4:8f8d0d19e6f1 335 // sometimes transport connect works even if no connection...
frankvnk 4:8f8d0d19e6f1 336 printf("ERROR %d\r\n", EXO_ERROR_WRITE);
frankvnk 4:8f8d0d19e6f1 337 }
frankvnk 4:8f8d0d19e6f1 338
frankvnk 4:8f8d0d19e6f1 339 if (!exositeWriteFailures)
frankvnk 4:8f8d0d19e6f1 340 {
frankvnk 4:8f8d0d19e6f1 341 //printf("ShowUIcode %d\n", EXO_CLIENT_RW);
frankvnk 4:8f8d0d19e6f1 342 return 0; // success
frankvnk 4:8f8d0d19e6f1 343 }
frankvnk 4:8f8d0d19e6f1 344 return 0;
frankvnk 4:8f8d0d19e6f1 345 }
frankvnk 4:8f8d0d19e6f1 346
frankvnk 4:8f8d0d19e6f1 347
frankvnk 4:8f8d0d19e6f1 348 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 349 //
frankvnk 4:8f8d0d19e6f1 350 //! activate_device
frankvnk 4:8f8d0d19e6f1 351 //!
frankvnk 4:8f8d0d19e6f1 352 //! \param none
frankvnk 4:8f8d0d19e6f1 353 //!
frankvnk 4:8f8d0d19e6f1 354 //! \return none
frankvnk 4:8f8d0d19e6f1 355 //!
frankvnk 4:8f8d0d19e6f1 356 //! \brief Calls activation API - if successful, it saves the returned
frankvnk 4:8f8d0d19e6f1 357 //! CIK to non-volatile
frankvnk 4:8f8d0d19e6f1 358 //
frankvnk 4:8f8d0d19e6f1 359 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 360 void activate_device(TCPSocketConnection *socket)
frankvnk 4:8f8d0d19e6f1 361 {
frankvnk 4:8f8d0d19e6f1 362 long sock = -1;
frankvnk 4:8f8d0d19e6f1 363 volatile int length;
frankvnk 4:8f8d0d19e6f1 364 char strLen[5];
frankvnk 4:8f8d0d19e6f1 365
frankvnk 4:8f8d0d19e6f1 366 while (sock < 0)
frankvnk 4:8f8d0d19e6f1 367 sock = connect_to_exosite(socket);
frankvnk 4:8f8d0d19e6f1 368 printf("Activating Device\n");
frankvnk 4:8f8d0d19e6f1 369
frankvnk 4:8f8d0d19e6f1 370 length = strlen(STR_VENDOR) + META_UUID_SIZE;
frankvnk 4:8f8d0d19e6f1 371 itoa(length, strLen, 10); //make a string for length
frankvnk 4:8f8d0d19e6f1 372
frankvnk 4:8f8d0d19e6f1 373 sendLine(socket, POSTDATA_LINE, "/provision/activate");
frankvnk 4:8f8d0d19e6f1 374 sendLine(socket, HOST_LINE, NULL);
frankvnk 4:8f8d0d19e6f1 375 sendLine(socket, CONTENT_LINE, NULL);
frankvnk 4:8f8d0d19e6f1 376 sendLine(socket, LENGTH_LINE, strLen);
frankvnk 4:8f8d0d19e6f1 377 sendLine(socket, VENDOR_LINE, NULL);
frankvnk 4:8f8d0d19e6f1 378
frankvnk 4:8f8d0d19e6f1 379 if (0 == readResponse(socket, "200"))
frankvnk 4:8f8d0d19e6f1 380 {
frankvnk 4:8f8d0d19e6f1 381 char strBuf[RX_SIZE];
frankvnk 4:8f8d0d19e6f1 382 unsigned char strLen, len;
frankvnk 4:8f8d0d19e6f1 383 char *p;
frankvnk 4:8f8d0d19e6f1 384 unsigned char crlf = 0;
frankvnk 4:8f8d0d19e6f1 385 unsigned char ciklen = 0;
frankvnk 4:8f8d0d19e6f1 386 char NCIK[CIK_LENGTH];
frankvnk 4:8f8d0d19e6f1 387
frankvnk 4:8f8d0d19e6f1 388 do
frankvnk 4:8f8d0d19e6f1 389 {
frankvnk 4:8f8d0d19e6f1 390 strLen = socket->receive(strBuf, RX_SIZE);
frankvnk 4:8f8d0d19e6f1 391 len = strLen;
frankvnk 4:8f8d0d19e6f1 392 p = strBuf;
frankvnk 4:8f8d0d19e6f1 393 // Find 4 consecutive \r or \n - should be: \r\n\r\n
frankvnk 4:8f8d0d19e6f1 394 while (0 < len && 4 > crlf)
frankvnk 4:8f8d0d19e6f1 395 {
frankvnk 4:8f8d0d19e6f1 396 if ('\r' == *p || '\n' == *p)
frankvnk 4:8f8d0d19e6f1 397 {
frankvnk 4:8f8d0d19e6f1 398 ++crlf;
frankvnk 4:8f8d0d19e6f1 399 }
frankvnk 4:8f8d0d19e6f1 400 else
frankvnk 4:8f8d0d19e6f1 401 {
frankvnk 4:8f8d0d19e6f1 402 crlf = 0;
frankvnk 4:8f8d0d19e6f1 403 }
frankvnk 4:8f8d0d19e6f1 404 ++p;
frankvnk 4:8f8d0d19e6f1 405 --len;
frankvnk 4:8f8d0d19e6f1 406 }
frankvnk 4:8f8d0d19e6f1 407
frankvnk 4:8f8d0d19e6f1 408 // The body is the CIK
frankvnk 4:8f8d0d19e6f1 409 if (0 < len && 4 == crlf && CIK_LENGTH > ciklen)
frankvnk 4:8f8d0d19e6f1 410 {
frankvnk 4:8f8d0d19e6f1 411 // TODO, be more robust - match Content-Length header value to CIK_LENGTH
frankvnk 4:8f8d0d19e6f1 412 unsigned char need, part;
frankvnk 4:8f8d0d19e6f1 413 need = CIK_LENGTH - ciklen;
frankvnk 4:8f8d0d19e6f1 414 part = need < len ? need : len;
frankvnk 4:8f8d0d19e6f1 415 strncpy(NCIK + ciklen, p, part);
frankvnk 4:8f8d0d19e6f1 416 ciklen += part;
frankvnk 4:8f8d0d19e6f1 417 }
frankvnk 4:8f8d0d19e6f1 418 } while (RX_SIZE == strLen);
frankvnk 4:8f8d0d19e6f1 419
frankvnk 4:8f8d0d19e6f1 420 if (CIK_LENGTH == ciklen)
frankvnk 4:8f8d0d19e6f1 421 {
frankvnk 4:8f8d0d19e6f1 422 exosite_meta_write((unsigned char *)NCIK, CIK_LENGTH, META_CIK);
frankvnk 4:8f8d0d19e6f1 423 }
frankvnk 4:8f8d0d19e6f1 424 }
frankvnk 4:8f8d0d19e6f1 425 else
frankvnk 4:8f8d0d19e6f1 426 {
frankvnk 4:8f8d0d19e6f1 427 printf("Activation failed\n");
frankvnk 4:8f8d0d19e6f1 428 getchar();
frankvnk 4:8f8d0d19e6f1 429 }
frankvnk 4:8f8d0d19e6f1 430 socket->close();
frankvnk 4:8f8d0d19e6f1 431 }
frankvnk 4:8f8d0d19e6f1 432
frankvnk 4:8f8d0d19e6f1 433 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 434 //
frankvnk 4:8f8d0d19e6f1 435 //! connect_to_exosite
frankvnk 4:8f8d0d19e6f1 436 //!
frankvnk 4:8f8d0d19e6f1 437 //! \param None
frankvnk 4:8f8d0d19e6f1 438 //!
frankvnk 4:8f8d0d19e6f1 439 //! \return socket handle
frankvnk 4:8f8d0d19e6f1 440 //!
frankvnk 4:8f8d0d19e6f1 441 //! \brief Establishes a connection with the Exosite API server
frankvnk 4:8f8d0d19e6f1 442 //
frankvnk 4:8f8d0d19e6f1 443 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 444 long connect_to_exosite(TCPSocketConnection *socket)
frankvnk 4:8f8d0d19e6f1 445 {
frankvnk 4:8f8d0d19e6f1 446 static unsigned char connectRetries = 0;
frankvnk 4:8f8d0d19e6f1 447 long sock;
frankvnk 4:8f8d0d19e6f1 448
frankvnk 4:8f8d0d19e6f1 449 if (connectRetries++ > 5)
frankvnk 4:8f8d0d19e6f1 450 {
frankvnk 4:8f8d0d19e6f1 451 connectRetries = 0;
frankvnk 4:8f8d0d19e6f1 452 printf("ERROR %d\r\n", EXO_ERROR_CONNECT);
frankvnk 4:8f8d0d19e6f1 453 }
frankvnk 4:8f8d0d19e6f1 454
frankvnk 4:8f8d0d19e6f1 455 const char* ECHO_SERVER_ADDRESS = "173.255.209.28"; //TODO - use DNS or check m2.exosite.com/ip to check for updates
frankvnk 4:8f8d0d19e6f1 456 const int ECHO_SERVER_PORT = 80;
frankvnk 4:8f8d0d19e6f1 457
frankvnk 4:8f8d0d19e6f1 458 unsigned char server[META_SERVER_SIZE];
frankvnk 4:8f8d0d19e6f1 459 exosite_meta_read(server, META_SERVER_SIZE, META_SERVER);
frankvnk 4:8f8d0d19e6f1 460
frankvnk 4:8f8d0d19e6f1 461 sock = socket->connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
frankvnk 4:8f8d0d19e6f1 462 if(sock < 0)
frankvnk 4:8f8d0d19e6f1 463 {
frankvnk 4:8f8d0d19e6f1 464 printf("Connection to server failed\n");
frankvnk 4:8f8d0d19e6f1 465 socket->close();
frankvnk 4:8f8d0d19e6f1 466 wait_ms(200);
frankvnk 4:8f8d0d19e6f1 467 return -1;
frankvnk 4:8f8d0d19e6f1 468 }
frankvnk 4:8f8d0d19e6f1 469 else
frankvnk 4:8f8d0d19e6f1 470 {
frankvnk 4:8f8d0d19e6f1 471 connectRetries = 0;
frankvnk 4:8f8d0d19e6f1 472 //printf("ShowUIcode %d\n", EXO_SERVER_CONNECTED);
frankvnk 4:8f8d0d19e6f1 473 }
frankvnk 4:8f8d0d19e6f1 474
frankvnk 4:8f8d0d19e6f1 475 // Success
frankvnk 4:8f8d0d19e6f1 476 //printf("success\n");
frankvnk 4:8f8d0d19e6f1 477 return sock;
frankvnk 4:8f8d0d19e6f1 478 }
frankvnk 4:8f8d0d19e6f1 479
frankvnk 4:8f8d0d19e6f1 480
frankvnk 4:8f8d0d19e6f1 481 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 482 //
frankvnk 4:8f8d0d19e6f1 483 //! readResponse
frankvnk 4:8f8d0d19e6f1 484 //!
frankvnk 4:8f8d0d19e6f1 485 //! \param socket handle, pointer to expected HTTP response code
frankvnk 4:8f8d0d19e6f1 486 //!
frankvnk 4:8f8d0d19e6f1 487 //! \return 0 if match, -1 if no match
frankvnk 4:8f8d0d19e6f1 488 //!
frankvnk 4:8f8d0d19e6f1 489 //! \brief Reads first 12 bytes of HTTP response and extracts the 3 byte code
frankvnk 4:8f8d0d19e6f1 490 //
frankvnk 4:8f8d0d19e6f1 491 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 492 int readResponse(TCPSocketConnection *socket, char * code)
frankvnk 4:8f8d0d19e6f1 493 {
frankvnk 4:8f8d0d19e6f1 494 char rxBuf[12];
frankvnk 4:8f8d0d19e6f1 495 unsigned char rxLen;
frankvnk 4:8f8d0d19e6f1 496 rxLen = socket->receive(rxBuf, 12);
frankvnk 4:8f8d0d19e6f1 497 //PROBLEM : more than 12 chars are printed
frankvnk 4:8f8d0d19e6f1 498 printf("rec %s\n", rxBuf);
frankvnk 4:8f8d0d19e6f1 499 if (12 == rxLen && code[0] == rxBuf[9] && code[1] == rxBuf[10] && code[2] == rxBuf[11])
frankvnk 4:8f8d0d19e6f1 500 {
frankvnk 4:8f8d0d19e6f1 501 return 0;
frankvnk 4:8f8d0d19e6f1 502 }
frankvnk 4:8f8d0d19e6f1 503
frankvnk 4:8f8d0d19e6f1 504 return -1;
frankvnk 4:8f8d0d19e6f1 505 }
frankvnk 4:8f8d0d19e6f1 506
frankvnk 4:8f8d0d19e6f1 507
frankvnk 4:8f8d0d19e6f1 508 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 509 //
frankvnk 4:8f8d0d19e6f1 510 //! sendLine
frankvnk 4:8f8d0d19e6f1 511 //!
frankvnk 4:8f8d0d19e6f1 512 //! \param Which line type
frankvnk 4:8f8d0d19e6f1 513 //!
frankvnk 4:8f8d0d19e6f1 514 //! \return socket handle
frankvnk 4:8f8d0d19e6f1 515 //!
frankvnk 4:8f8d0d19e6f1 516 //! \brief Sends data out the socket
frankvnk 4:8f8d0d19e6f1 517 //
frankvnk 4:8f8d0d19e6f1 518 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 519 void sendLine(TCPSocketConnection *socket, unsigned char LINE, char * payload)
frankvnk 4:8f8d0d19e6f1 520 {
frankvnk 4:8f8d0d19e6f1 521 char strBuf[80];
frankvnk 4:8f8d0d19e6f1 522 unsigned char strLen = 0;
frankvnk 4:8f8d0d19e6f1 523
frankvnk 4:8f8d0d19e6f1 524 switch(LINE)
frankvnk 4:8f8d0d19e6f1 525 {
frankvnk 4:8f8d0d19e6f1 526 case CIK_LINE:
frankvnk 4:8f8d0d19e6f1 527 sprintf(strBuf, "X-Exosite-CIK: %s\r\n", activeCIK);
frankvnk 4:8f8d0d19e6f1 528 strLen = strlen(strBuf);
frankvnk 4:8f8d0d19e6f1 529 //strLen = 57;
frankvnk 4:8f8d0d19e6f1 530 //memcpy(strBuf,STR_CIK_HEADER,strLen);
frankvnk 4:8f8d0d19e6f1 531 //exosite_meta_read((unsigned char *)&strBuf[strLen], CIK_LENGTH, META_CIK);
frankvnk 4:8f8d0d19e6f1 532 //strLen += CIK_LENGTH;
frankvnk 4:8f8d0d19e6f1 533 //memcpy(&strBuf[strLen],STR_CRLF, 2);
frankvnk 4:8f8d0d19e6f1 534 //strLen += 2;
frankvnk 4:8f8d0d19e6f1 535 break;
frankvnk 4:8f8d0d19e6f1 536 case HOST_LINE:
frankvnk 4:8f8d0d19e6f1 537 strLen = 28;
frankvnk 4:8f8d0d19e6f1 538 memcpy(strBuf,STR_HOST,strLen);
frankvnk 4:8f8d0d19e6f1 539 strBuf[strLen] = NULL;
frankvnk 4:8f8d0d19e6f1 540 break;
frankvnk 4:8f8d0d19e6f1 541 case CONTENT_LINE:
frankvnk 4:8f8d0d19e6f1 542 strLen = 64;
frankvnk 4:8f8d0d19e6f1 543 memcpy(strBuf,STR_CONTENT,strLen);
frankvnk 4:8f8d0d19e6f1 544 strBuf[strLen] = NULL;
frankvnk 4:8f8d0d19e6f1 545 break;
frankvnk 4:8f8d0d19e6f1 546 case ACCEPT_LINE:
frankvnk 4:8f8d0d19e6f1 547 strLen = 58;
frankvnk 4:8f8d0d19e6f1 548 memcpy(strBuf,STR_ACCEPT,strLen);
frankvnk 4:8f8d0d19e6f1 549 memcpy(&strBuf[strLen],payload, strlen(payload));
frankvnk 4:8f8d0d19e6f1 550 strLen += strlen(payload);
frankvnk 4:8f8d0d19e6f1 551 break;
frankvnk 4:8f8d0d19e6f1 552 case LENGTH_LINE: // Content-Length: NN
frankvnk 4:8f8d0d19e6f1 553 strLen = 16;
frankvnk 4:8f8d0d19e6f1 554 memcpy(strBuf,STR_CONTENT_LENGTH,strLen);
frankvnk 4:8f8d0d19e6f1 555 memcpy(&strBuf[strLen],payload, strlen(payload));
frankvnk 4:8f8d0d19e6f1 556 strLen += strlen(payload);
frankvnk 4:8f8d0d19e6f1 557 memcpy(&strBuf[strLen],STR_CRLF, 2);
frankvnk 4:8f8d0d19e6f1 558 strLen += 2;
frankvnk 4:8f8d0d19e6f1 559 memcpy(&strBuf[strLen],STR_CRLF, 2);
frankvnk 4:8f8d0d19e6f1 560 strLen += 2;
frankvnk 4:8f8d0d19e6f1 561 strBuf[strLen] = NULL;
frankvnk 4:8f8d0d19e6f1 562 break;
frankvnk 4:8f8d0d19e6f1 563 case GETDATA_LINE:
frankvnk 4:8f8d0d19e6f1 564 strLen = 24;
frankvnk 4:8f8d0d19e6f1 565 memcpy(strBuf,STR_GET_URL,strLen);
frankvnk 4:8f8d0d19e6f1 566 memcpy(&strBuf[strLen],payload, strlen(payload));
frankvnk 4:8f8d0d19e6f1 567 strLen += strlen(payload);
frankvnk 4:8f8d0d19e6f1 568 memcpy(&strBuf[strLen],STR_HTTP, 12);
frankvnk 4:8f8d0d19e6f1 569 strLen += 12;
frankvnk 4:8f8d0d19e6f1 570 break;
frankvnk 4:8f8d0d19e6f1 571 case VENDOR_LINE:
frankvnk 4:8f8d0d19e6f1 572 strLen = strlen(STR_VENDOR);
frankvnk 4:8f8d0d19e6f1 573 memcpy(strBuf, STR_VENDOR, strLen);
frankvnk 4:8f8d0d19e6f1 574 exosite_meta_read((unsigned char *)&strBuf[strLen], META_UUID_SIZE, META_UUID);
frankvnk 4:8f8d0d19e6f1 575 strLen += META_UUID_SIZE;
frankvnk 4:8f8d0d19e6f1 576 strBuf[strLen] = NULL;
frankvnk 4:8f8d0d19e6f1 577 break;
frankvnk 4:8f8d0d19e6f1 578 case POSTDATA_LINE:
frankvnk 4:8f8d0d19e6f1 579 strLen = 5;
frankvnk 4:8f8d0d19e6f1 580 memcpy(strBuf,"POST ", strLen);
frankvnk 4:8f8d0d19e6f1 581 memcpy(&strBuf[strLen],payload, strlen(payload));
frankvnk 4:8f8d0d19e6f1 582 strLen += strlen(payload);
frankvnk 4:8f8d0d19e6f1 583 memcpy(&strBuf[strLen],STR_HTTP, 12);
frankvnk 4:8f8d0d19e6f1 584 strLen += 12;
frankvnk 4:8f8d0d19e6f1 585 strBuf[strLen] = NULL;
frankvnk 4:8f8d0d19e6f1 586 break;
frankvnk 4:8f8d0d19e6f1 587 case EMPTY_LINE:
frankvnk 4:8f8d0d19e6f1 588 strLen = 2;
frankvnk 4:8f8d0d19e6f1 589 memcpy(strBuf,STR_CRLF,strLen);
frankvnk 4:8f8d0d19e6f1 590 break;
frankvnk 4:8f8d0d19e6f1 591 default:
frankvnk 4:8f8d0d19e6f1 592 break;
frankvnk 4:8f8d0d19e6f1 593 }
frankvnk 4:8f8d0d19e6f1 594 //printf("sendLine: %s\n", strBuf);
frankvnk 4:8f8d0d19e6f1 595 socket->send_all(strBuf, strLen);
frankvnk 4:8f8d0d19e6f1 596 wifi._event.hci_unsolicited_event_handler();
frankvnk 4:8f8d0d19e6f1 597 return;
frankvnk 4:8f8d0d19e6f1 598 }
frankvnk 4:8f8d0d19e6f1 599
frankvnk 4:8f8d0d19e6f1 600 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 601 //
frankvnk 4:8f8d0d19e6f1 602 //! exosite_meta_write
frankvnk 4:8f8d0d19e6f1 603 //!
frankvnk 4:8f8d0d19e6f1 604 //! \param write_buffer - string buffer containing info to write to meta;
frankvnk 4:8f8d0d19e6f1 605 //! srcBytes - size of string in bytes; element - item from
frankvnk 4:8f8d0d19e6f1 606 //! MetaElements enum.
frankvnk 4:8f8d0d19e6f1 607 //!
frankvnk 4:8f8d0d19e6f1 608 //! \return None
frankvnk 4:8f8d0d19e6f1 609 //!
frankvnk 4:8f8d0d19e6f1 610 //! \brief Writes specific meta information to meta memory.
frankvnk 4:8f8d0d19e6f1 611 //
frankvnk 4:8f8d0d19e6f1 612 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 613 void exosite_meta_write(unsigned char * write_buffer, unsigned short srcBytes, unsigned char element)
frankvnk 4:8f8d0d19e6f1 614 {
frankvnk 4:8f8d0d19e6f1 615 exosite_meta * meta_info = 0;
frankvnk 4:8f8d0d19e6f1 616
frankvnk 4:8f8d0d19e6f1 617 //TODO - do not write if the data already there is identical...
frankvnk 4:8f8d0d19e6f1 618
frankvnk 4:8f8d0d19e6f1 619 switch (element)
frankvnk 4:8f8d0d19e6f1 620 {
frankvnk 4:8f8d0d19e6f1 621 case META_CIK:
frankvnk 4:8f8d0d19e6f1 622 if (srcBytes > META_CIK_SIZE) return;
frankvnk 4:8f8d0d19e6f1 623 memcpy((char *)(exo_meta + (int)meta_info->cik), write_buffer, srcBytes); //store CIK
frankvnk 4:8f8d0d19e6f1 624 break;
frankvnk 4:8f8d0d19e6f1 625 case META_SERVER:
frankvnk 4:8f8d0d19e6f1 626 if (srcBytes > META_SERVER_SIZE) return;
frankvnk 4:8f8d0d19e6f1 627 memcpy((char *)(exo_meta + (int)meta_info->server), write_buffer, srcBytes); //store server IP
frankvnk 4:8f8d0d19e6f1 628 break;
frankvnk 4:8f8d0d19e6f1 629 case META_MARK:
frankvnk 4:8f8d0d19e6f1 630 if (srcBytes > META_MARK_SIZE) return;
frankvnk 4:8f8d0d19e6f1 631 memcpy((char *)(exo_meta + (int)meta_info->mark), write_buffer, srcBytes); //store exosite mark
frankvnk 4:8f8d0d19e6f1 632 break;
frankvnk 4:8f8d0d19e6f1 633 case META_UUID:
frankvnk 4:8f8d0d19e6f1 634 if (srcBytes > META_UUID_SIZE) return;
frankvnk 4:8f8d0d19e6f1 635 memcpy((char *)(exo_meta + (int)meta_info->uuid), write_buffer, srcBytes); //store UUID
frankvnk 4:8f8d0d19e6f1 636 break;
frankvnk 4:8f8d0d19e6f1 637 case META_MFR:
frankvnk 4:8f8d0d19e6f1 638 if (srcBytes > META_MFR_SIZE) return;
frankvnk 4:8f8d0d19e6f1 639 memcpy((char *)(exo_meta + (int)meta_info->mfr), write_buffer, srcBytes); //store manufacturing info
frankvnk 4:8f8d0d19e6f1 640 break;
frankvnk 4:8f8d0d19e6f1 641 case META_NONE:
frankvnk 4:8f8d0d19e6f1 642 default:
frankvnk 4:8f8d0d19e6f1 643 break;
frankvnk 4:8f8d0d19e6f1 644 }
frankvnk 4:8f8d0d19e6f1 645 return;
frankvnk 4:8f8d0d19e6f1 646 }
frankvnk 4:8f8d0d19e6f1 647
frankvnk 4:8f8d0d19e6f1 648
frankvnk 4:8f8d0d19e6f1 649 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 650 //
frankvnk 4:8f8d0d19e6f1 651 //! exosite_meta_read
frankvnk 4:8f8d0d19e6f1 652 //!
frankvnk 4:8f8d0d19e6f1 653 //! \param read_buffer - string buffer to receive element data; destBytes -
frankvnk 4:8f8d0d19e6f1 654 //! size of buffer in bytes; element - item from MetaElements enum.
frankvnk 4:8f8d0d19e6f1 655 //!
frankvnk 4:8f8d0d19e6f1 656 //! \return None
frankvnk 4:8f8d0d19e6f1 657 //!
frankvnk 4:8f8d0d19e6f1 658 //! \brief Writes specific meta information to meta memory.
frankvnk 4:8f8d0d19e6f1 659 //
frankvnk 4:8f8d0d19e6f1 660 //*****************************************************************************
frankvnk 4:8f8d0d19e6f1 661 void exosite_meta_read(unsigned char * read_buffer, unsigned short destBytes, unsigned char element)
frankvnk 4:8f8d0d19e6f1 662 {
frankvnk 4:8f8d0d19e6f1 663 exosite_meta * meta_info = 0;
frankvnk 4:8f8d0d19e6f1 664
frankvnk 4:8f8d0d19e6f1 665 switch (element)
frankvnk 4:8f8d0d19e6f1 666 {
frankvnk 4:8f8d0d19e6f1 667 case META_CIK:
frankvnk 4:8f8d0d19e6f1 668 if (destBytes < META_CIK_SIZE) return;
frankvnk 4:8f8d0d19e6f1 669 memcpy(read_buffer, (char *)(exo_meta + (int)meta_info->cik), destBytes); //read CIK
frankvnk 4:8f8d0d19e6f1 670 break;
frankvnk 4:8f8d0d19e6f1 671 case META_SERVER:
frankvnk 4:8f8d0d19e6f1 672 if (destBytes < META_SERVER_SIZE) return;
frankvnk 4:8f8d0d19e6f1 673 memcpy(read_buffer, (char *)(exo_meta + (int)meta_info->server), destBytes); //read server IP
frankvnk 4:8f8d0d19e6f1 674 break;
frankvnk 4:8f8d0d19e6f1 675 case META_MARK:
frankvnk 4:8f8d0d19e6f1 676 if (destBytes < META_MARK_SIZE) return;
frankvnk 4:8f8d0d19e6f1 677 memcpy(read_buffer, (char *)(exo_meta + (int)meta_info->mark), destBytes); //read exosite mark
frankvnk 4:8f8d0d19e6f1 678 break;
frankvnk 4:8f8d0d19e6f1 679 case META_UUID:
frankvnk 4:8f8d0d19e6f1 680 if (destBytes < META_UUID_SIZE) return;
frankvnk 4:8f8d0d19e6f1 681 memcpy(read_buffer, (char *)(exo_meta + (int)meta_info->uuid), destBytes); //read exosite mark
frankvnk 4:8f8d0d19e6f1 682 break;
frankvnk 4:8f8d0d19e6f1 683 case META_MFR:
frankvnk 4:8f8d0d19e6f1 684 if (destBytes < META_MFR_SIZE) return;
frankvnk 4:8f8d0d19e6f1 685 memcpy(read_buffer, (char *)(exo_meta + (int)meta_info->mfr), destBytes); //read exosite mark
frankvnk 4:8f8d0d19e6f1 686 break;
frankvnk 4:8f8d0d19e6f1 687 case META_NONE:
frankvnk 4:8f8d0d19e6f1 688 default:
frankvnk 4:8f8d0d19e6f1 689 break;
frankvnk 4:8f8d0d19e6f1 690 }
frankvnk 4:8f8d0d19e6f1 691 return;
frankvnk 4:8f8d0d19e6f1 692 }