Frank Vannieuwkerke / Mbed 2 deprecated Wi-Go_IOT_Demo_MKII

Dependencies:   NVIC_set_all_priorities mbed cc3000_hostdriver_mbedsocket TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger

Committer:
frankvnk
Date:
Sun Dec 01 15:06:46 2013 +0000
Revision:
4:8f8d0d19e6f1
Parent:
3:1851b5d6f69d
Child:
6:cbd7f95bbca9
Solved sensor read errors.
; Added i2c unlock.
;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frankvnk 1:99bfc8d68fd3 1 /* mbed Microcontroller Library
frankvnk 1:99bfc8d68fd3 2 * Copyright (c) 2006-2013 ARM Limited
frankvnk 1:99bfc8d68fd3 3 *
frankvnk 1:99bfc8d68fd3 4 * Licensed under the Apache License, Version 2.0 (the "License");
frankvnk 1:99bfc8d68fd3 5 * you may not use this file except in compliance with the License.
frankvnk 1:99bfc8d68fd3 6 * You may obtain a copy of the License at
frankvnk 1:99bfc8d68fd3 7 *
frankvnk 1:99bfc8d68fd3 8 * http://www.apache.org/licenses/LICENSE-2.0
frankvnk 1:99bfc8d68fd3 9 *
frankvnk 1:99bfc8d68fd3 10 * Unless required by applicable law or agreed to in writing, software
frankvnk 1:99bfc8d68fd3 11 * distributed under the License is distributed on an "AS IS" BASIS,
frankvnk 1:99bfc8d68fd3 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
frankvnk 1:99bfc8d68fd3 13 * See the License for the specific language governing permissions and
frankvnk 1:99bfc8d68fd3 14 * limitations under the License.
frankvnk 1:99bfc8d68fd3 15 */
frankvnk 1:99bfc8d68fd3 16 #include "mbed.h"
frankvnk 1:99bfc8d68fd3 17 #include "cc3000.h"
frankvnk 1:99bfc8d68fd3 18 #include "main.h"
frankvnk 4:8f8d0d19e6f1 19 #include "TCPSocketConnection.h"
frankvnk 4:8f8d0d19e6f1 20 #include "TCPSocketServer.h"
frankvnk 1:99bfc8d68fd3 21
frankvnk 1:99bfc8d68fd3 22 using namespace mbed_cc3000;
frankvnk 1:99bfc8d68fd3 23
frankvnk 1:99bfc8d68fd3 24 tUserFS user_info;
frankvnk 1:99bfc8d68fd3 25
frankvnk 1:99bfc8d68fd3 26 /* cc3000 module declaration specific for user's board. Check also init() */
frankvnk 1:99bfc8d68fd3 27 #if (MY_BOARD == WIGO)
frankvnk 1:99bfc8d68fd3 28
frankvnk 4:8f8d0d19e6f1 29 #include "I2C_busreset.h"
frankvnk 1:99bfc8d68fd3 30 #include "defLED.h"
frankvnk 1:99bfc8d68fd3 31 #include "TSISensor.h"
frankvnk 1:99bfc8d68fd3 32 #include "TEMT6200.h"
frankvnk 1:99bfc8d68fd3 33 #include "WiGo_BattCharger.h"
frankvnk 1:99bfc8d68fd3 34 #include "MMA8451Q.h"
frankvnk 1:99bfc8d68fd3 35 #include "MAG3110.h"
frankvnk 1:99bfc8d68fd3 36 #include "MPL3115A2.h"
frankvnk 1:99bfc8d68fd3 37 #include "Wi-Go_eCompass_Lib_V3.h"
frankvnk 1:99bfc8d68fd3 38 #include "demo.h"
frankvnk 4:8f8d0d19e6f1 39 #include "doTCPIP.h"
frankvnk 4:8f8d0d19e6f1 40 #include "run_exosite.h"
frankvnk 1:99bfc8d68fd3 41
frankvnk 1:99bfc8d68fd3 42 #define FCOUNTSPERG 4096.0F // sensor specific: MMA8451 provide 4096 counts / g in 2g mode
frankvnk 1:99bfc8d68fd3 43 #define FCOUNTSPERUT 10.0F // sensor specific: MAG3110 provide 10 counts / uT
frankvnk 1:99bfc8d68fd3 44
frankvnk 1:99bfc8d68fd3 45 #define BATT_0 0.53
frankvnk 1:99bfc8d68fd3 46 #define BATT_100 0.63
frankvnk 1:99bfc8d68fd3 47
frankvnk 1:99bfc8d68fd3 48 DigitalOut ledr (LED_RED);
frankvnk 1:99bfc8d68fd3 49 DigitalOut ledg (LED_GREEN);
frankvnk 1:99bfc8d68fd3 50 DigitalOut ledb (LED_BLUE);
frankvnk 1:99bfc8d68fd3 51 DigitalOut led1 (PTB8);
frankvnk 1:99bfc8d68fd3 52 DigitalOut led2 (PTB9);
frankvnk 1:99bfc8d68fd3 53 DigitalOut led3 (PTB10);
frankvnk 1:99bfc8d68fd3 54
frankvnk 4:8f8d0d19e6f1 55 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), "", "", NONE, true);
frankvnk 4:8f8d0d19e6f1 56 TCPSocketConnection socket;
frankvnk 4:8f8d0d19e6f1 57
frankvnk 1:99bfc8d68fd3 58 Serial pc(USBTX, USBRX);
frankvnk 1:99bfc8d68fd3 59
frankvnk 1:99bfc8d68fd3 60 // Slide sensor
frankvnk 1:99bfc8d68fd3 61 TSISensor tsi;
frankvnk 1:99bfc8d68fd3 62
frankvnk 1:99bfc8d68fd3 63 // Systick
frankvnk 1:99bfc8d68fd3 64 Ticker systick;
frankvnk 1:99bfc8d68fd3 65
frankvnk 1:99bfc8d68fd3 66 // Ambient light sensor : PTD5 = enable, PTB0 = analog input
frankvnk 1:99bfc8d68fd3 67 TEMT6200 ambi(PTD5, PTB0);
frankvnk 1:99bfc8d68fd3 68
frankvnk 1:99bfc8d68fd3 69 //Wi-Go battery charger control
frankvnk 1:99bfc8d68fd3 70 WiGo_BattCharger Batt(CHRG_EN1, CHRG_EN2, CHRG_SNS_EN, CHRG_SNS, CHRG_POK, CHRG_CHG);
frankvnk 1:99bfc8d68fd3 71
frankvnk 1:99bfc8d68fd3 72 // Accelerometer
frankvnk 1:99bfc8d68fd3 73 #define MMA8451_I2C_ADDRESS (0x1d<<1)
frankvnk 1:99bfc8d68fd3 74 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
frankvnk 1:99bfc8d68fd3 75
frankvnk 1:99bfc8d68fd3 76 // Magnetometer
frankvnk 4:8f8d0d19e6f1 77 #define MAG3110_I2C_ADDRESS (0x0e<<1)
frankvnk 4:8f8d0d19e6f1 78 MAG3110 mag(PTE0, PTE1, MAG3110_I2C_ADDRESS);
frankvnk 1:99bfc8d68fd3 79
frankvnk 1:99bfc8d68fd3 80 // altimeter-Pressure-Temperature (apt)
frankvnk 1:99bfc8d68fd3 81 #define MPL3115A2_I2C_ADDRESS (0x60<<1)
frankvnk 1:99bfc8d68fd3 82 MPL3115A2 apt( PTE0, PTE1, MPL3115A2_I2C_ADDRESS);
frankvnk 1:99bfc8d68fd3 83
frankvnk 1:99bfc8d68fd3 84 volatile int secondFlag;
frankvnk 1:99bfc8d68fd3 85 volatile int HsecondFlag;
frankvnk 1:99bfc8d68fd3 86 unsigned int seconds;
frankvnk 1:99bfc8d68fd3 87 unsigned int compass_type;
frankvnk 1:99bfc8d68fd3 88 unsigned short adc_sample3;
frankvnk 1:99bfc8d68fd3 89 float fcountperg = 1.0F / FCOUNTSPERG;
frankvnk 1:99bfc8d68fd3 90 float fcountperut = 1.0F / FCOUNTSPERUT;
frankvnk 1:99bfc8d68fd3 91 volatile unsigned char newData;
frankvnk 1:99bfc8d68fd3 92 volatile int server_running;
frankvnk 1:99bfc8d68fd3 93 axis6_t axis6;
frankvnk 1:99bfc8d68fd3 94 int do_mDNS = 0;
frankvnk 1:99bfc8d68fd3 95 //Device name - used for Smart config in order to stop the Smart phone configuration process
frankvnk 1:99bfc8d68fd3 96 char DevServname[] = "CC3000";
frankvnk 1:99bfc8d68fd3 97
frankvnk 1:99bfc8d68fd3 98 void initLEDs(void)
frankvnk 1:99bfc8d68fd3 99 {
frankvnk 1:99bfc8d68fd3 100 RED_OFF;
frankvnk 1:99bfc8d68fd3 101 GREEN_OFF;
frankvnk 1:99bfc8d68fd3 102 BLUE_OFF;
frankvnk 1:99bfc8d68fd3 103 LED_D1_OFF;
frankvnk 1:99bfc8d68fd3 104 LED_D2_OFF;
frankvnk 1:99bfc8d68fd3 105 LED_D3_OFF;
frankvnk 1:99bfc8d68fd3 106 }
frankvnk 1:99bfc8d68fd3 107
frankvnk 1:99bfc8d68fd3 108 void GreenStop(void)
frankvnk 1:99bfc8d68fd3 109 {
frankvnk 1:99bfc8d68fd3 110 RED_OFF; GREEN_OFF; BLUE_OFF;
frankvnk 1:99bfc8d68fd3 111 while(1)
frankvnk 1:99bfc8d68fd3 112 {
frankvnk 1:99bfc8d68fd3 113 GREEN_ON;
frankvnk 1:99bfc8d68fd3 114 secondFlag = 0;
frankvnk 1:99bfc8d68fd3 115 while(!secondFlag);
frankvnk 1:99bfc8d68fd3 116 GREEN_OFF;
frankvnk 1:99bfc8d68fd3 117 secondFlag = 0;
frankvnk 1:99bfc8d68fd3 118 while(!secondFlag);
frankvnk 1:99bfc8d68fd3 119 }
frankvnk 1:99bfc8d68fd3 120 }
frankvnk 1:99bfc8d68fd3 121
frankvnk 1:99bfc8d68fd3 122 void accel_read(void)
frankvnk 1:99bfc8d68fd3 123 {
frankvnk 1:99bfc8d68fd3 124 signed short resultx, resulty, resultz;
frankvnk 1:99bfc8d68fd3 125 if(acc.isDataAvailable())
frankvnk 1:99bfc8d68fd3 126 {
frankvnk 4:8f8d0d19e6f1 127 resultx = acc.readReg(0x01)<<8;
frankvnk 4:8f8d0d19e6f1 128 resultx |= acc.readReg(0x02);
frankvnk 4:8f8d0d19e6f1 129 resultx = resultx >> 2;
frankvnk 4:8f8d0d19e6f1 130 resulty = acc.readReg(0x03)<<8;
frankvnk 4:8f8d0d19e6f1 131 resulty |= acc.readReg(0x04);
frankvnk 4:8f8d0d19e6f1 132 resulty = resulty >> 2;
frankvnk 4:8f8d0d19e6f1 133 resultz = acc.readReg(0x05)<<8;
frankvnk 4:8f8d0d19e6f1 134 resultz |= acc.readReg(0x06);
frankvnk 4:8f8d0d19e6f1 135 resultz = resultz >> 2;
frankvnk 1:99bfc8d68fd3 136 if(compass_type == NED_COMPASS)
frankvnk 1:99bfc8d68fd3 137 {
frankvnk 1:99bfc8d68fd3 138 axis6.acc_x = resultx;
frankvnk 1:99bfc8d68fd3 139 axis6.acc_y = -1 * resulty; // multiple by -1 to compensate for PCB layout
frankvnk 1:99bfc8d68fd3 140 axis6.acc_z = resultz;
frankvnk 1:99bfc8d68fd3 141 }
frankvnk 1:99bfc8d68fd3 142 if(compass_type == ANDROID_COMPASS)
frankvnk 1:99bfc8d68fd3 143 {
frankvnk 1:99bfc8d68fd3 144 axis6.acc_x = resulty; //
frankvnk 1:99bfc8d68fd3 145 axis6.acc_y = -1 * resultx;
frankvnk 1:99bfc8d68fd3 146 axis6.acc_z = resultz;
frankvnk 1:99bfc8d68fd3 147 }
frankvnk 1:99bfc8d68fd3 148 if(compass_type == WINDOWS_COMPASS)
frankvnk 1:99bfc8d68fd3 149 {
frankvnk 1:99bfc8d68fd3 150 axis6.acc_x = -1 * resulty; //
frankvnk 1:99bfc8d68fd3 151 axis6.acc_y = resultx;
frankvnk 1:99bfc8d68fd3 152 axis6.acc_z = resultz;
frankvnk 1:99bfc8d68fd3 153 }
frankvnk 1:99bfc8d68fd3 154 axis6.fax = axis6.acc_x;
frankvnk 1:99bfc8d68fd3 155 axis6.fay = axis6.acc_y;
frankvnk 1:99bfc8d68fd3 156 axis6.faz = axis6.acc_z;
frankvnk 1:99bfc8d68fd3 157 axis6.fGax = axis6.fax * fcountperg;
frankvnk 1:99bfc8d68fd3 158 axis6.fGay = axis6.fay * fcountperg;
frankvnk 1:99bfc8d68fd3 159 axis6.fGaz = axis6.faz * fcountperg;
frankvnk 1:99bfc8d68fd3 160 }
frankvnk 1:99bfc8d68fd3 161 }
frankvnk 1:99bfc8d68fd3 162
frankvnk 1:99bfc8d68fd3 163 void readTempAlt(void) // We don't use the fractional data
frankvnk 1:99bfc8d68fd3 164 {
frankvnk 4:8f8d0d19e6f1 165 unsigned char raw_data[2];
frankvnk 4:8f8d0d19e6f1 166 if(apt.getAltimeterRaw(&raw_data[0]))
frankvnk 1:99bfc8d68fd3 167 axis6.alt = ((raw_data[0] << 8) | raw_data[1]);
frankvnk 4:8f8d0d19e6f1 168 if(apt.getTemperatureRaw(&raw_data[0]))
frankvnk 4:8f8d0d19e6f1 169 axis6.temp = raw_data[0];
frankvnk 1:99bfc8d68fd3 170 }
frankvnk 1:99bfc8d68fd3 171
frankvnk 1:99bfc8d68fd3 172 void readCompass( void )
frankvnk 1:99bfc8d68fd3 173 {
frankvnk 4:8f8d0d19e6f1 174 if(mag.isDataAvailable())
frankvnk 1:99bfc8d68fd3 175 {
frankvnk 4:8f8d0d19e6f1 176 uint8_t mx_msb, my_msb, mz_msb;
frankvnk 4:8f8d0d19e6f1 177 uint8_t mx_lsb, my_lsb, mz_lsb;
frankvnk 4:8f8d0d19e6f1 178
frankvnk 4:8f8d0d19e6f1 179 mx_msb = mag.readReg(0x01);
frankvnk 4:8f8d0d19e6f1 180 mx_lsb = mag.readReg(0x02);
frankvnk 4:8f8d0d19e6f1 181 my_msb = mag.readReg(0x03);
frankvnk 4:8f8d0d19e6f1 182 my_lsb = mag.readReg(0x04);
frankvnk 4:8f8d0d19e6f1 183 mz_msb = mag.readReg(0x05);
frankvnk 4:8f8d0d19e6f1 184 mz_lsb = mag.readReg(0x06);
frankvnk 4:8f8d0d19e6f1 185
frankvnk 4:8f8d0d19e6f1 186 if(compass_type == NED_COMPASS)
frankvnk 4:8f8d0d19e6f1 187 {
frankvnk 4:8f8d0d19e6f1 188 axis6.mag_y = (((mx_msb << 8) | mx_lsb)); // x & y swapped to compensate for PCB layout
frankvnk 4:8f8d0d19e6f1 189 axis6.mag_x = (((my_msb << 8) | my_lsb));
frankvnk 4:8f8d0d19e6f1 190 axis6.mag_z = (((mz_msb << 8) | mz_lsb));
frankvnk 4:8f8d0d19e6f1 191 }
frankvnk 4:8f8d0d19e6f1 192 if(compass_type == ANDROID_COMPASS)
frankvnk 4:8f8d0d19e6f1 193 {
frankvnk 4:8f8d0d19e6f1 194 axis6.mag_x = (((mx_msb << 8) | mx_lsb));
frankvnk 4:8f8d0d19e6f1 195 axis6.mag_y = (((my_msb << 8) | my_lsb));
frankvnk 4:8f8d0d19e6f1 196 axis6.mag_z = -1 * (((mz_msb << 8) | mz_lsb)); // negate to reverse axis of Z to conform to Android coordinate system
frankvnk 4:8f8d0d19e6f1 197 }
frankvnk 4:8f8d0d19e6f1 198 if(compass_type == WINDOWS_COMPASS)
frankvnk 4:8f8d0d19e6f1 199 {
frankvnk 4:8f8d0d19e6f1 200 axis6.mag_x = (((mx_msb << 8) | mx_lsb));
frankvnk 4:8f8d0d19e6f1 201 axis6.mag_y = (((my_msb << 8) | my_lsb));
frankvnk 4:8f8d0d19e6f1 202 axis6.mag_z = -1 * (((mz_msb << 8) | mz_lsb));
frankvnk 4:8f8d0d19e6f1 203 }
frankvnk 4:8f8d0d19e6f1 204 axis6.fmx = axis6.mag_x;
frankvnk 4:8f8d0d19e6f1 205 axis6.fmy = axis6.mag_y;
frankvnk 4:8f8d0d19e6f1 206 axis6.fmz = axis6.mag_z;
frankvnk 4:8f8d0d19e6f1 207 axis6.fUTmx = axis6.fmx * fcountperut;
frankvnk 4:8f8d0d19e6f1 208 axis6.fUTmy = axis6.fmy * fcountperut;
frankvnk 4:8f8d0d19e6f1 209 axis6.fUTmz = axis6.fmz * fcountperut;
frankvnk 1:99bfc8d68fd3 210 }
frankvnk 1:99bfc8d68fd3 211 }
frankvnk 1:99bfc8d68fd3 212
frankvnk 4:8f8d0d19e6f1 213 void axis6Print(void)
frankvnk 4:8f8d0d19e6f1 214 {
frankvnk 4:8f8d0d19e6f1 215 char *compass_points[9] = {"North", "N-East", "East", "S-East", "South", "S-West", "West", "N-West", "North"};
frankvnk 4:8f8d0d19e6f1 216 signed short compass_bearing = (axis6.compass + 23) / 45;
frankvnk 4:8f8d0d19e6f1 217 printf("Compass : Roll=%-d Pitch=%-d Yaw=%-d [%s]\r\n", axis6.roll, axis6.pitch, axis6.yaw, compass_points[compass_bearing]);
frankvnk 4:8f8d0d19e6f1 218 printf("Accel : X= %1.2f, Y= %1.2f, Z= %1.2f\r\n", axis6.fGax, axis6.fGay, axis6.fGaz);
frankvnk 4:8f8d0d19e6f1 219 printf("Magneto : X= %3.1f, Y= %3.1f, Z= %3.1f\r\n\r\n", axis6.fUTmx, axis6.fUTmy, axis6.fUTmz);
frankvnk 4:8f8d0d19e6f1 220 }
frankvnk 4:8f8d0d19e6f1 221
frankvnk 1:99bfc8d68fd3 222 void set_dir_LED(void)
frankvnk 1:99bfc8d68fd3 223 {
frankvnk 1:99bfc8d68fd3 224 RED_OFF; GREEN_OFF; BLUE_OFF;
frankvnk 1:99bfc8d68fd3 225
frankvnk 1:99bfc8d68fd3 226 if((axis6.compass >= 353) || (axis6.compass <= 7))
frankvnk 1:99bfc8d68fd3 227 {
frankvnk 1:99bfc8d68fd3 228 GREEN_ON;
frankvnk 1:99bfc8d68fd3 229 }
frankvnk 1:99bfc8d68fd3 230 else
frankvnk 1:99bfc8d68fd3 231 {
frankvnk 1:99bfc8d68fd3 232 GREEN_OFF;
frankvnk 1:99bfc8d68fd3 233 }
frankvnk 1:99bfc8d68fd3 234 if(((axis6.compass >= 348) && (axis6.compass <= 357)) || ((axis6.compass >= 3) && (axis6.compass <= 12)))
frankvnk 1:99bfc8d68fd3 235 {
frankvnk 1:99bfc8d68fd3 236 BLUE_ON;
frankvnk 1:99bfc8d68fd3 237 }
frankvnk 1:99bfc8d68fd3 238 else
frankvnk 1:99bfc8d68fd3 239 {
frankvnk 1:99bfc8d68fd3 240 BLUE_OFF;
frankvnk 1:99bfc8d68fd3 241 }
frankvnk 1:99bfc8d68fd3 242 if((axis6.compass >= 348) || (axis6.compass <= 12)) return;
frankvnk 1:99bfc8d68fd3 243 if(((axis6.compass >= 268) && (axis6.compass <= 272)) || ((axis6.compass >= 88) && (axis6.compass <= 92)))
frankvnk 1:99bfc8d68fd3 244 {
frankvnk 1:99bfc8d68fd3 245 RED_ON;
frankvnk 1:99bfc8d68fd3 246 return;
frankvnk 1:99bfc8d68fd3 247 }
frankvnk 1:99bfc8d68fd3 248 if((axis6.compass >= 178) && (axis6.compass <= 182))
frankvnk 1:99bfc8d68fd3 249 {
frankvnk 1:99bfc8d68fd3 250 BLUE_ON;
frankvnk 1:99bfc8d68fd3 251 RED_ON;
frankvnk 1:99bfc8d68fd3 252 return;
frankvnk 1:99bfc8d68fd3 253 }
frankvnk 1:99bfc8d68fd3 254 }
frankvnk 1:99bfc8d68fd3 255
frankvnk 1:99bfc8d68fd3 256 void SysTick_Handler(void)
frankvnk 1:99bfc8d68fd3 257 {
frankvnk 1:99bfc8d68fd3 258 static unsigned int ttt = 1;
frankvnk 1:99bfc8d68fd3 259 int ts;
frankvnk 1:99bfc8d68fd3 260 ts = ttt & 0x3;
frankvnk 1:99bfc8d68fd3 261 if(ts == 2) readCompass();
frankvnk 1:99bfc8d68fd3 262 if(ts == 1) accel_read();
frankvnk 1:99bfc8d68fd3 263 if(ts == 3)
frankvnk 1:99bfc8d68fd3 264 {
frankvnk 1:99bfc8d68fd3 265 run_eCompass();
frankvnk 1:99bfc8d68fd3 266 newData = 1; // a general purpose flag for things that need to synch to the ISR
frankvnk 1:99bfc8d68fd3 267 axis6.timestamp++;
frankvnk 1:99bfc8d68fd3 268 if(!server_running) set_dir_LED(); // Set the LEDs based on direction when nothing else is usng them
frankvnk 1:99bfc8d68fd3 269 }
frankvnk 4:8f8d0d19e6f1 270 if(ttt == 100)//systick = 0.005 : 100 - systick = 0.025 : 20
frankvnk 1:99bfc8d68fd3 271 {
frankvnk 1:99bfc8d68fd3 272 LED_D1_ON;
frankvnk 1:99bfc8d68fd3 273 if(seconds && (seconds < 15)) calibrate_eCompass();
frankvnk 1:99bfc8d68fd3 274 readTempAlt();
frankvnk 1:99bfc8d68fd3 275 axis6.light = ambi.readRaw(); // Light Sensor
frankvnk 1:99bfc8d68fd3 276 HsecondFlag = 1; // A general purpose flag for things that need to happen every 500ms
frankvnk 1:99bfc8d68fd3 277 }
frankvnk 4:8f8d0d19e6f1 278 if(ttt >= 200)//systick = 0.005 : 200 - systick = 0.025 : 40
frankvnk 1:99bfc8d68fd3 279 {
frankvnk 1:99bfc8d68fd3 280 LED_D1_OFF;
frankvnk 1:99bfc8d68fd3 281 ttt = 1;
frankvnk 1:99bfc8d68fd3 282 calibrate_eCompass();
frankvnk 1:99bfc8d68fd3 283 Batt.sense_en(1);
frankvnk 1:99bfc8d68fd3 284 adc_sample3 = Batt.level();
frankvnk 1:99bfc8d68fd3 285 Batt.sense_en(0);
frankvnk 1:99bfc8d68fd3 286 secondFlag = 1; // A general purpose flag for things that need to happen once a second
frankvnk 1:99bfc8d68fd3 287 HsecondFlag = 1;
frankvnk 1:99bfc8d68fd3 288 seconds++;
frankvnk 4:8f8d0d19e6f1 289 if(!(seconds & 0x1F)) do_mDNS = 1;
frankvnk 1:99bfc8d68fd3 290 } else ttt++;
frankvnk 1:99bfc8d68fd3 291 }
frankvnk 1:99bfc8d68fd3 292
frankvnk 1:99bfc8d68fd3 293 #elif (MY_BOARD == WIFI_DIPCORTEX)
frankvnk 1:99bfc8d68fd3 294 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37), PIN_INT0_IRQn);
frankvnk 1:99bfc8d68fd3 295 Serial pc(UART_TX, UART_RX);
frankvnk 1:99bfc8d68fd3 296 #else
frankvnk 1:99bfc8d68fd3 297
frankvnk 1:99bfc8d68fd3 298 #endif
frankvnk 1:99bfc8d68fd3 299
frankvnk 1:99bfc8d68fd3 300 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
frankvnk 1:99bfc8d68fd3 301 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
frankvnk 1:99bfc8d68fd3 302 #else
frankvnk 1:99bfc8d68fd3 303 const uint8_t smartconfigkey = 0;
frankvnk 1:99bfc8d68fd3 304 #endif
frankvnk 1:99bfc8d68fd3 305
frankvnk 1:99bfc8d68fd3 306 /**
frankvnk 1:99bfc8d68fd3 307 * \brief Print cc3000 information
frankvnk 1:99bfc8d68fd3 308 * \param none
frankvnk 1:99bfc8d68fd3 309 * \return none
frankvnk 1:99bfc8d68fd3 310 */
frankvnk 1:99bfc8d68fd3 311 void print_cc3000_info() {
frankvnk 1:99bfc8d68fd3 312 uint8_t myMAC[8];
frankvnk 4:8f8d0d19e6f1 313 uint8_t spVER[5];
frankvnk 4:8f8d0d19e6f1 314 wifi._nvmem.read_sp_version(spVER);
frankvnk 4:8f8d0d19e6f1 315 printf("SP Version (TI) : %d %d %d %d %d\r\n", spVER[0], spVER[1], spVER[2], spVER[3], spVER[4]);
frankvnk 1:99bfc8d68fd3 316 printf("MAC address + cc3000 info \r\n");
frankvnk 1:99bfc8d68fd3 317 wifi.get_user_file_info((uint8_t *)&user_info, sizeof(user_info));
frankvnk 1:99bfc8d68fd3 318 wifi.get_mac_address(myMAC);
frankvnk 1:99bfc8d68fd3 319 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 1:99bfc8d68fd3 320
frankvnk 1:99bfc8d68fd3 321 printf(" FTC %i \r\n",user_info.FTC);
frankvnk 1:99bfc8d68fd3 322 printf(" PP_version %i.%i \r\n",user_info.PP_version[0], user_info.PP_version[1]);
frankvnk 1:99bfc8d68fd3 323 printf(" SERV_PACK %i.%i \r\n",user_info.SERV_PACK[0], user_info.SERV_PACK[1]);
frankvnk 1:99bfc8d68fd3 324 printf(" DRV_VER %i.%i.%i \r\n",user_info.DRV_VER[0], user_info.DRV_VER[1], user_info.DRV_VER[2]);
frankvnk 1:99bfc8d68fd3 325 printf(" FW_VER %i.%i.%i \r\n",user_info.FW_VER[0], user_info.FW_VER[1], user_info.FW_VER[2]);
frankvnk 1:99bfc8d68fd3 326 }
frankvnk 1:99bfc8d68fd3 327
frankvnk 1:99bfc8d68fd3 328 /**
frankvnk 1:99bfc8d68fd3 329 * \brief Connect to SSID with a timeout
frankvnk 1:99bfc8d68fd3 330 * \param ssid Name of SSID
frankvnk 1:99bfc8d68fd3 331 * \param key Password
frankvnk 1:99bfc8d68fd3 332 * \param sec_mode Security mode
frankvnk 1:99bfc8d68fd3 333 * \return none
frankvnk 1:99bfc8d68fd3 334 */
frankvnk 1:99bfc8d68fd3 335 void connect_to_ssid(char *ssid, char *key, unsigned char sec_mode) {
frankvnk 1:99bfc8d68fd3 336 printf("Connecting to SSID: %s. Timeout is 10s. \r\n",ssid);
frankvnk 1:99bfc8d68fd3 337 if (wifi.connect_to_AP((uint8_t *)ssid, (uint8_t *)key, sec_mode) == true) {
frankvnk 1:99bfc8d68fd3 338 printf(" Connected. \r\n");
frankvnk 1:99bfc8d68fd3 339 } else {
frankvnk 1:99bfc8d68fd3 340 printf(" Connection timed-out (error). Please restart. \r\n");
frankvnk 1:99bfc8d68fd3 341 while(1);
frankvnk 1:99bfc8d68fd3 342 }
frankvnk 1:99bfc8d68fd3 343 }
frankvnk 1:99bfc8d68fd3 344
frankvnk 1:99bfc8d68fd3 345 /**
frankvnk 1:99bfc8d68fd3 346 * \brief Connect to SSID without security
frankvnk 1:99bfc8d68fd3 347 * \param ssid Name of SSID
frankvnk 1:99bfc8d68fd3 348 * \return none
frankvnk 1:99bfc8d68fd3 349 */
frankvnk 1:99bfc8d68fd3 350 void connect_to_ssid(char *ssid) {
frankvnk 1:99bfc8d68fd3 351 wifi.connect_open((uint8_t *)ssid);
frankvnk 1:99bfc8d68fd3 352 }
frankvnk 1:99bfc8d68fd3 353
frankvnk 1:99bfc8d68fd3 354 /**
frankvnk 1:99bfc8d68fd3 355 * \brief First time configuration
frankvnk 1:99bfc8d68fd3 356 * \param none
frankvnk 1:99bfc8d68fd3 357 * \return none
frankvnk 1:99bfc8d68fd3 358 */
frankvnk 1:99bfc8d68fd3 359 void do_FTC(void) {
frankvnk 1:99bfc8d68fd3 360 printf("Running First Time Configuration \r\n");
frankvnk 1:99bfc8d68fd3 361 wifi.start_smart_config(smartconfigkey);
frankvnk 1:99bfc8d68fd3 362 while (wifi.is_dhcp_configured() == false) {
frankvnk 1:99bfc8d68fd3 363 wait_ms(500);
frankvnk 1:99bfc8d68fd3 364 printf("Waiting for dhcp to be set. \r\n");
frankvnk 1:99bfc8d68fd3 365 }
frankvnk 1:99bfc8d68fd3 366 user_info.FTC = 1;
frankvnk 1:99bfc8d68fd3 367 wifi.set_user_file_info((uint8_t *)&user_info, sizeof(user_info));
frankvnk 1:99bfc8d68fd3 368 wifi._wlan.stop();
frankvnk 1:99bfc8d68fd3 369 printf("FTC finished. \r\n");
frankvnk 1:99bfc8d68fd3 370 }
frankvnk 1:99bfc8d68fd3 371
frankvnk 1:99bfc8d68fd3 372 /**
frankvnk 1:99bfc8d68fd3 373 * \brief TCP server demo
frankvnk 1:99bfc8d68fd3 374 * \param none
frankvnk 1:99bfc8d68fd3 375 * \return int
frankvnk 1:99bfc8d68fd3 376 */
frankvnk 1:99bfc8d68fd3 377 int main() {
frankvnk 1:99bfc8d68fd3 378 int loop;
frankvnk 1:99bfc8d68fd3 379 int temp;
frankvnk 1:99bfc8d68fd3 380 unsigned int oldseconds;
frankvnk 1:99bfc8d68fd3 381
frankvnk 1:99bfc8d68fd3 382 //Board dependent init
frankvnk 1:99bfc8d68fd3 383 init();
frankvnk 1:99bfc8d68fd3 384
frankvnk 1:99bfc8d68fd3 385 // Initalize global variables
frankvnk 1:99bfc8d68fd3 386 axis6.packet_id = 1;
frankvnk 1:99bfc8d68fd3 387 axis6.timestamp = 0;
frankvnk 1:99bfc8d68fd3 388 axis6.acc_x = 0;
frankvnk 1:99bfc8d68fd3 389 axis6.acc_y = 0;
frankvnk 1:99bfc8d68fd3 390 axis6.acc_z = 0;
frankvnk 1:99bfc8d68fd3 391 axis6.mag_x = 0;
frankvnk 1:99bfc8d68fd3 392 axis6.mag_y = 0;
frankvnk 1:99bfc8d68fd3 393 axis6.mag_z = 0;
frankvnk 1:99bfc8d68fd3 394 axis6.roll = 0;
frankvnk 1:99bfc8d68fd3 395 axis6.pitch = 0;
frankvnk 1:99bfc8d68fd3 396 axis6.yaw = 0;
frankvnk 1:99bfc8d68fd3 397 axis6.compass = 0;
frankvnk 1:99bfc8d68fd3 398 axis6.alt = 0;
frankvnk 1:99bfc8d68fd3 399 axis6.temp = 0;
frankvnk 1:99bfc8d68fd3 400 axis6.light = 0;
frankvnk 1:99bfc8d68fd3 401 compass_type = ANDROID_COMPASS;
frankvnk 1:99bfc8d68fd3 402 seconds = 0;
frankvnk 1:99bfc8d68fd3 403 server_running = 1;
frankvnk 1:99bfc8d68fd3 404 newData = 0;
frankvnk 1:99bfc8d68fd3 405 secondFlag = 0;
frankvnk 1:99bfc8d68fd3 406 HsecondFlag = 0;
frankvnk 1:99bfc8d68fd3 407 GREEN_ON;
frankvnk 1:99bfc8d68fd3 408
frankvnk 4:8f8d0d19e6f1 409 // Unlock I2C bus if blocked by a device
frankvnk 4:8f8d0d19e6f1 410 I2C_busreset();
frankvnk 4:8f8d0d19e6f1 411
frankvnk 1:99bfc8d68fd3 412 pc.baud(115200);
frankvnk 1:99bfc8d68fd3 413
frankvnk 1:99bfc8d68fd3 414 // set current to 500mA since we're turning on the Wi-Fi
frankvnk 1:99bfc8d68fd3 415 Batt.init(CHRG_500MA);
frankvnk 1:99bfc8d68fd3 416
frankvnk 1:99bfc8d68fd3 417 //Init LEDs
frankvnk 1:99bfc8d68fd3 418 initLEDs();
frankvnk 1:99bfc8d68fd3 419 // Read the Magnetometer a couple of times to initalize
frankvnk 1:99bfc8d68fd3 420 for(loop=0 ; loop < 5 ; loop++)
frankvnk 1:99bfc8d68fd3 421 {
frankvnk 4:8f8d0d19e6f1 422 temp = mag.readReg(0x01);
frankvnk 4:8f8d0d19e6f1 423 temp = mag.readReg(0x02);
frankvnk 4:8f8d0d19e6f1 424 temp = mag.readReg(0x03);
frankvnk 4:8f8d0d19e6f1 425 temp = mag.readReg(0x04);
frankvnk 4:8f8d0d19e6f1 426 temp = mag.readReg(0x05);
frankvnk 4:8f8d0d19e6f1 427 temp = mag.readReg(0x06);
frankvnk 4:8f8d0d19e6f1 428 wait_ms(50);
frankvnk 1:99bfc8d68fd3 429 }
frankvnk 1:99bfc8d68fd3 430
frankvnk 1:99bfc8d68fd3 431 init_eCompass();
frankvnk 1:99bfc8d68fd3 432
frankvnk 4:8f8d0d19e6f1 433 // Start Ticker
frankvnk 4:8f8d0d19e6f1 434 systick.attach(&SysTick_Handler, 0.005);
frankvnk 1:99bfc8d68fd3 435 // Trigger a WLAN device
frankvnk 4:8f8d0d19e6f1 436 wifi.init();
frankvnk 4:8f8d0d19e6f1 437 //wifi.start(0);
frankvnk 1:99bfc8d68fd3 438 printf("CC3000 Wi-Go IOT demo.\r\n");
frankvnk 1:99bfc8d68fd3 439 print_cc3000_info();
frankvnk 1:99bfc8d68fd3 440 server_running = 1;
frankvnk 1:99bfc8d68fd3 441 newData = 0;
frankvnk 1:99bfc8d68fd3 442 GREEN_ON;
frankvnk 1:99bfc8d68fd3 443
frankvnk 4:8f8d0d19e6f1 444 if(!user_info.FTC)
frankvnk 1:99bfc8d68fd3 445 {
frankvnk 1:99bfc8d68fd3 446 do_FTC(); // Call First Time Configuration if SmartConfig has not been run
frankvnk 1:99bfc8d68fd3 447 printf("Please restart your board. \r\n");
frankvnk 1:99bfc8d68fd3 448 GreenStop();
frankvnk 1:99bfc8d68fd3 449 }
frankvnk 1:99bfc8d68fd3 450
frankvnk 1:99bfc8d68fd3 451 // Wait for slider touch
frankvnk 1:99bfc8d68fd3 452 printf("\r\nUse the slider to start an application.\r\n");
frankvnk 1:99bfc8d68fd3 453 printf("Releasing the slider for more than 3 seconds\r\nwill start the chosen application.\r\n");
frankvnk 1:99bfc8d68fd3 454 printf("Touching the slider within the 3 seconds\ntimeframe allows you to re-select an application.\r\n");
frankvnk 1:99bfc8d68fd3 455 printf("\r\nThe RGB LED indicates the selection:\r\n");
frankvnk 1:99bfc8d68fd3 456 printf("ORANGE - Erase all profiles.\r\n");
frankvnk 1:99bfc8d68fd3 457 printf("PURPLE - Force SmartConfig.\r\n");
frankvnk 1:99bfc8d68fd3 458 printf("BLUE - Webserver displaying live sensor data.\r\n");
frankvnk 1:99bfc8d68fd3 459 printf("RED - Exosite data client.\r\n");
frankvnk 1:99bfc8d68fd3 460 printf("GREEN - Android sensor fusion app.\r\n");
frankvnk 1:99bfc8d68fd3 461 while( tsi.readPercentage() == 0 )
frankvnk 1:99bfc8d68fd3 462 {
frankvnk 1:99bfc8d68fd3 463 RED_ON;
frankvnk 1:99bfc8d68fd3 464 wait(0.2);
frankvnk 1:99bfc8d68fd3 465 RED_OFF;
frankvnk 1:99bfc8d68fd3 466 wait(0.2);
frankvnk 1:99bfc8d68fd3 467 }
frankvnk 1:99bfc8d68fd3 468 RED_OFF;
frankvnk 1:99bfc8d68fd3 469
frankvnk 1:99bfc8d68fd3 470 oldseconds = seconds;
frankvnk 1:99bfc8d68fd3 471 loop = 100;
frankvnk 1:99bfc8d68fd3 472 temp = 0;
frankvnk 1:99bfc8d68fd3 473 // Read slider as long as it is touched.
frankvnk 1:99bfc8d68fd3 474 // If released for more than 3 seconds, exit
frankvnk 1:99bfc8d68fd3 475 while((loop != 0) || ((seconds - oldseconds) < 3))
frankvnk 1:99bfc8d68fd3 476 {
frankvnk 1:99bfc8d68fd3 477 loop = tsi.readPercentage() * 100;
frankvnk 1:99bfc8d68fd3 478 if(loop != 0)
frankvnk 1:99bfc8d68fd3 479 {
frankvnk 1:99bfc8d68fd3 480 oldseconds = seconds;
frankvnk 1:99bfc8d68fd3 481 temp = loop;
frankvnk 1:99bfc8d68fd3 482 }
frankvnk 1:99bfc8d68fd3 483 if(temp > 80)
frankvnk 1:99bfc8d68fd3 484 {
frankvnk 1:99bfc8d68fd3 485 RED_ON; GREEN_ON; BLUE_OFF; //Orange
frankvnk 1:99bfc8d68fd3 486 }
frankvnk 1:99bfc8d68fd3 487 else if(temp > 60)
frankvnk 1:99bfc8d68fd3 488 {
frankvnk 1:99bfc8d68fd3 489 RED_ON; GREEN_OFF; BLUE_ON; //Purple
frankvnk 1:99bfc8d68fd3 490 }
frankvnk 1:99bfc8d68fd3 491 else if(temp > 40)
frankvnk 1:99bfc8d68fd3 492 {
frankvnk 1:99bfc8d68fd3 493 RED_OFF; GREEN_OFF; BLUE_ON; //Blue
frankvnk 1:99bfc8d68fd3 494 }
frankvnk 1:99bfc8d68fd3 495 else if(temp > 20)
frankvnk 1:99bfc8d68fd3 496 {
frankvnk 1:99bfc8d68fd3 497 RED_ON; GREEN_OFF; BLUE_OFF; //Red
frankvnk 1:99bfc8d68fd3 498 }
frankvnk 1:99bfc8d68fd3 499 else
frankvnk 1:99bfc8d68fd3 500 {
frankvnk 1:99bfc8d68fd3 501 RED_OFF; GREEN_ON; BLUE_OFF; //Green
frankvnk 1:99bfc8d68fd3 502 }
frankvnk 1:99bfc8d68fd3 503 }
frankvnk 1:99bfc8d68fd3 504 RED_OFF; GREEN_OFF; BLUE_OFF;
frankvnk 1:99bfc8d68fd3 505
frankvnk 1:99bfc8d68fd3 506 // Execute the user selected application
frankvnk 1:99bfc8d68fd3 507 if(temp > 80) // Erase all profiles
frankvnk 1:99bfc8d68fd3 508 {
frankvnk 1:99bfc8d68fd3 509 server_running = 1;
frankvnk 1:99bfc8d68fd3 510 RED_OFF; GREEN_OFF; BLUE_OFF;
frankvnk 1:99bfc8d68fd3 511 printf("\r\nErasing all wireless profiles. \r\n");
frankvnk 1:99bfc8d68fd3 512 wifi.delete_profiles();
frankvnk 1:99bfc8d68fd3 513 wifi.stop();
frankvnk 1:99bfc8d68fd3 514 printf("Finished. Please restart your board. \r\n");
frankvnk 1:99bfc8d68fd3 515 GreenStop();
frankvnk 1:99bfc8d68fd3 516 }
frankvnk 1:99bfc8d68fd3 517
frankvnk 1:99bfc8d68fd3 518 if(temp > 60) // Force SmartConfig
frankvnk 1:99bfc8d68fd3 519 {
frankvnk 1:99bfc8d68fd3 520 server_running = 1;
frankvnk 1:99bfc8d68fd3 521 RED_OFF; GREEN_OFF; BLUE_OFF;
frankvnk 1:99bfc8d68fd3 522 printf("\r\nStarting Smart Config configuration. \r\n");
frankvnk 1:99bfc8d68fd3 523 wifi.start_smart_config(smartconfigkey);
frankvnk 1:99bfc8d68fd3 524 while (wifi.is_dhcp_configured() == false)
frankvnk 1:99bfc8d68fd3 525 {
frankvnk 1:99bfc8d68fd3 526 wait_ms(500);
frankvnk 1:99bfc8d68fd3 527 printf("Waiting for dhcp to be set. \r\n");
frankvnk 1:99bfc8d68fd3 528 }
frankvnk 1:99bfc8d68fd3 529 printf("Finished. Please restart your board. \r\n");
frankvnk 1:99bfc8d68fd3 530 GreenStop();
frankvnk 1:99bfc8d68fd3 531 }
frankvnk 1:99bfc8d68fd3 532
frankvnk 1:99bfc8d68fd3 533 RED_OFF; GREEN_OFF; BLUE_OFF;
frankvnk 1:99bfc8d68fd3 534
frankvnk 1:99bfc8d68fd3 535 printf("\r\nAttempting SSID Connection. \r\n");
frankvnk 4:8f8d0d19e6f1 536 if (wifi.connect() == -1) {
frankvnk 4:8f8d0d19e6f1 537 printf("Failed to connect. Please verify connection details and try again. \r\n");
frankvnk 4:8f8d0d19e6f1 538 } else {
frankvnk 4:8f8d0d19e6f1 539 printf("Connected - IP address: %s \r\n",wifi.getIPAddress());
frankvnk 1:99bfc8d68fd3 540 }
frankvnk 1:99bfc8d68fd3 541 LED_D3_ON;
frankvnk 1:99bfc8d68fd3 542
frankvnk 1:99bfc8d68fd3 543 server_running = 0;
frankvnk 1:99bfc8d68fd3 544
frankvnk 1:99bfc8d68fd3 545 // Start the selected application
frankvnk 1:99bfc8d68fd3 546 if(temp > 40) // Run Webserver
frankvnk 1:99bfc8d68fd3 547 {
frankvnk 1:99bfc8d68fd3 548 compass_type = NED_COMPASS;
frankvnk 1:99bfc8d68fd3 549 init_eCompass();
frankvnk 1:99bfc8d68fd3 550 seconds = 0;
frankvnk 1:99bfc8d68fd3 551 demo_wifi_main();
frankvnk 1:99bfc8d68fd3 552 }
frankvnk 1:99bfc8d68fd3 553 if(temp > 20) // Send data to Exosite
frankvnk 1:99bfc8d68fd3 554 {
frankvnk 1:99bfc8d68fd3 555 compass_type = NED_COMPASS;
frankvnk 1:99bfc8d68fd3 556 init_eCompass();
frankvnk 1:99bfc8d68fd3 557 seconds = 0;
frankvnk 4:8f8d0d19e6f1 558 run_exosite(&socket);
frankvnk 1:99bfc8d68fd3 559 }
frankvnk 1:99bfc8d68fd3 560 init_eCompass();
frankvnk 1:99bfc8d68fd3 561 seconds = 0;
frankvnk 1:99bfc8d68fd3 562 // Run TCP/IP Connection to host - Sensor Fusion App
frankvnk 4:8f8d0d19e6f1 563 runTCPIPserver();
frankvnk 1:99bfc8d68fd3 564
frankvnk 1:99bfc8d68fd3 565 }
frankvnk 3:1851b5d6f69d 566