Cheerlights client using WiFiDIPCortex and WS2801 RGB LED strip

Dependencies:   Adafruit_WS2801 HTTPClient cc3000_hostdriver_mbedsocket mbed

Committer:
SomeRandomBloke
Date:
Tue Feb 11 21:30:21 2014 +0000
Revision:
0:98d83f5b309f
Child:
1:40027344b249
Cheerlights using WiFiDIPCortex

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:98d83f5b309f 1 /** WiFiDIPCortex Cheerlights
SomeRandomBloke 0:98d83f5b309f 2 *
SomeRandomBloke 0:98d83f5b309f 3 * @author Andrew Lindsay
SomeRandomBloke 0:98d83f5b309f 4 *
SomeRandomBloke 0:98d83f5b309f 5 * @section LICENSE
SomeRandomBloke 0:98d83f5b309f 6 *
SomeRandomBloke 0:98d83f5b309f 7 * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk)
SomeRandomBloke 0:98d83f5b309f 8 *
SomeRandomBloke 0:98d83f5b309f 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
SomeRandomBloke 0:98d83f5b309f 10 * of this software and associated documentation files (the "Software"), to deal
SomeRandomBloke 0:98d83f5b309f 11 * in the Software without restriction, including without limitation the rights
SomeRandomBloke 0:98d83f5b309f 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
SomeRandomBloke 0:98d83f5b309f 13 * copies of the Software, and to permit persons to whom the Software is
SomeRandomBloke 0:98d83f5b309f 14 * furnished to do so, subject to the following conditions:
SomeRandomBloke 0:98d83f5b309f 15
SomeRandomBloke 0:98d83f5b309f 16 * The above copyright notice and this permission notice shall be included in
SomeRandomBloke 0:98d83f5b309f 17 * all copies or substantial portions of the Software.
SomeRandomBloke 0:98d83f5b309f 18 *
SomeRandomBloke 0:98d83f5b309f 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
SomeRandomBloke 0:98d83f5b309f 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
SomeRandomBloke 0:98d83f5b309f 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
SomeRandomBloke 0:98d83f5b309f 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
SomeRandomBloke 0:98d83f5b309f 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
SomeRandomBloke 0:98d83f5b309f 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
SomeRandomBloke 0:98d83f5b309f 25 * THE SOFTWARE.
SomeRandomBloke 0:98d83f5b309f 26 *
SomeRandomBloke 0:98d83f5b309f 27 * @section DESCRIPTION
SomeRandomBloke 0:98d83f5b309f 28 *
SomeRandomBloke 0:98d83f5b309f 29 * This is a basic cheerlights client, http://www.cheerlights.com/ It uses the
SomeRandomBloke 0:98d83f5b309f 30 * API url http://api.thingspeak.com/channels/1417/field/1/last.txt to read the last
SomeRandomBloke 0:98d83f5b309f 31 * colour selected.
SomeRandomBloke 0:98d83f5b309f 32 *
SomeRandomBloke 0:98d83f5b309f 33 * The hardware is the WiFiDIPCortex from SolderSplash Labs http://www.soldersplash.co.uk/products/wifi-dipcortex/
SomeRandomBloke 0:98d83f5b309f 34 * This is a small, yet powerful LPC1347 Cortex M3 dev board with built in CC3000 WiFi module.
SomeRandomBloke 0:98d83f5b309f 35 * The CC3000 uses the TI SmartConfig to setup the WiFi connection without having to update any code.
SomeRandomBloke 0:98d83f5b309f 36 *
SomeRandomBloke 0:98d83f5b309f 37 * The WiFiDIPCortex requires 2 pushbuttons connected between:
SomeRandomBloke 0:98d83f5b309f 38 * Reset: Pin 1 (Reset) and GND, plus 10K resistor between Pin 1 and 3.3V, Pin 11
SomeRandomBloke 0:98d83f5b309f 39 * Config: Pin 6 (P1_31) and GND, plus 10K resistor between Pin 1 and 3.3V, Pin 11
SomeRandomBloke 0:98d83f5b309f 40 *
SomeRandomBloke 0:98d83f5b309f 41 * The LED strip used in this example is based on the WS2801 chips and requires a CLK and DATA to use it.
SomeRandomBloke 0:98d83f5b309f 42 * Pin 27 (P0_7) LED strip Data
SomeRandomBloke 0:98d83f5b309f 43 * Pin 28 (P1_28) LED strip Clk.
SomeRandomBloke 0:98d83f5b309f 44 * Ideally strip should be powered from a external 3.3V source and the GNDs connected between Power supply and
SomeRandomBloke 0:98d83f5b309f 45 * WiFiDIPCortex.
SomeRandomBloke 0:98d83f5b309f 46 *
SomeRandomBloke 0:98d83f5b309f 47 * Debug output is sent to UART connected to pins 19 and 20.
SomeRandomBloke 0:98d83f5b309f 48 *
SomeRandomBloke 0:98d83f5b309f 49 * To use SmartConfig you'll need either the Android or iOS app from http://www.ti.com/tool/SmartConfig.
SomeRandomBloke 0:98d83f5b309f 50 * The Java version hasnt worked so far, but could also be used.
SomeRandomBloke 0:98d83f5b309f 51 * To enter SmartConfig mode, hold down the Config button, then press and release Reset, release Config.
SomeRandomBloke 0:98d83f5b309f 52 * You then use the SmartConfig app to set your network parameters, when you get the notification that
SomeRandomBloke 0:98d83f5b309f 53 * it was successful the WiFiDIPCortex is configured. The settings are saved and available next time it is powered up.
SomeRandomBloke 0:98d83f5b309f 54 *
SomeRandomBloke 0:98d83f5b309f 55 * If SmartConfig fails, try again.
SomeRandomBloke 0:98d83f5b309f 56 *
SomeRandomBloke 0:98d83f5b309f 57 * After starting the WiFiDIPCortex will connect to cheerlights.com and retrieve the latest colour and
SomeRandomBloke 0:98d83f5b309f 58 * set the lights to the colour.
SomeRandomBloke 0:98d83f5b309f 59 * Every minute the colour is retrieved and if its different to the last one the new colour is shown.
SomeRandomBloke 0:98d83f5b309f 60 *
SomeRandomBloke 0:98d83f5b309f 61 * The basic framework can be changed to use different LEDs to suit your available hardware.
SomeRandomBloke 0:98d83f5b309f 62 *
SomeRandomBloke 0:98d83f5b309f 63 */
SomeRandomBloke 0:98d83f5b309f 64
SomeRandomBloke 0:98d83f5b309f 65 #include "mbed.h"
SomeRandomBloke 0:98d83f5b309f 66 #include "cc3000.h"
SomeRandomBloke 0:98d83f5b309f 67 #include "HTTPClient.h"
SomeRandomBloke 0:98d83f5b309f 68 // Library to drive the LED strip
SomeRandomBloke 0:98d83f5b309f 69 #include "Adafruit_WS2801.h"
SomeRandomBloke 0:98d83f5b309f 70
SomeRandomBloke 0:98d83f5b309f 71 // Some local defines
SomeRandomBloke 0:98d83f5b309f 72
SomeRandomBloke 0:98d83f5b309f 73 #define SERIAL_BAUD_RATE 115200
SomeRandomBloke 0:98d83f5b309f 74
SomeRandomBloke 0:98d83f5b309f 75 #define WIGO 1
SomeRandomBloke 0:98d83f5b309f 76 #define WIFI_DIPCORTEX 2
SomeRandomBloke 0:98d83f5b309f 77 #define UNDEFINED 3
SomeRandomBloke 0:98d83f5b309f 78
SomeRandomBloke 0:98d83f5b309f 79 #define MY_BOARD WIFI_DIPCORTEX
SomeRandomBloke 0:98d83f5b309f 80
SomeRandomBloke 0:98d83f5b309f 81 using namespace mbed_cc3000;
SomeRandomBloke 0:98d83f5b309f 82
SomeRandomBloke 0:98d83f5b309f 83 // LED to indicate smart config is running
SomeRandomBloke 0:98d83f5b309f 84 DigitalOut LedSC(P0_1);
SomeRandomBloke 0:98d83f5b309f 85 //
SomeRandomBloke 0:98d83f5b309f 86 DigitalIn SCButton(P1_31);
SomeRandomBloke 0:98d83f5b309f 87
SomeRandomBloke 0:98d83f5b309f 88 PinName dataPin(P1_28); // Yellow wire on Adafruit Pixels
SomeRandomBloke 0:98d83f5b309f 89 PinName clockPin(P0_7); // Green wire on Adafruit Pixels
SomeRandomBloke 0:98d83f5b309f 90
SomeRandomBloke 0:98d83f5b309f 91 /* cc3000 module declaration specific for user's board. Check also init() */
SomeRandomBloke 0:98d83f5b309f 92 #if (MY_BOARD == WIGO)
SomeRandomBloke 0:98d83f5b309f 93 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn);
SomeRandomBloke 0:98d83f5b309f 94 Serial uart(USBTX,USBRX);
SomeRandomBloke 0:98d83f5b309f 95 #elif (MY_BOARD == WIFI_DIPCORTEX)
SomeRandomBloke 0:98d83f5b309f 96 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37));
SomeRandomBloke 0:98d83f5b309f 97 Serial uart(p19, p20);
SomeRandomBloke 0:98d83f5b309f 98 #else
SomeRandomBloke 0:98d83f5b309f 99
SomeRandomBloke 0:98d83f5b309f 100 #endif
SomeRandomBloke 0:98d83f5b309f 101
SomeRandomBloke 0:98d83f5b309f 102 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
SomeRandomBloke 0:98d83f5b309f 103 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
SomeRandomBloke 0:98d83f5b309f 104 #else
SomeRandomBloke 0:98d83f5b309f 105 const uint8_t smartconfigkey = 0;
SomeRandomBloke 0:98d83f5b309f 106 #endif
SomeRandomBloke 0:98d83f5b309f 107
SomeRandomBloke 0:98d83f5b309f 108 tNetappIpconfigRetArgs ipinfo;
SomeRandomBloke 0:98d83f5b309f 109 extern char tmpBuffer[512];
SomeRandomBloke 0:98d83f5b309f 110
SomeRandomBloke 0:98d83f5b309f 111 bool Connected = false;
SomeRandomBloke 0:98d83f5b309f 112 bool UsingSmartConfig = false;
SomeRandomBloke 0:98d83f5b309f 113 char _deviceName[] = "CC3000";
SomeRandomBloke 0:98d83f5b309f 114
SomeRandomBloke 0:98d83f5b309f 115 HTTPClient http;
SomeRandomBloke 0:98d83f5b309f 116 //char str[128];
SomeRandomBloke 0:98d83f5b309f 117 uint32_t lastColour = 0;
SomeRandomBloke 0:98d83f5b309f 118
SomeRandomBloke 0:98d83f5b309f 119 // Set the first variable to the number of rows, the second to number of pixels. 32 = 32 pixels in a row
SomeRandomBloke 0:98d83f5b309f 120 Adafruit_WS2801 strip = Adafruit_WS2801(1,32, dataPin, clockPin);
SomeRandomBloke 0:98d83f5b309f 121
SomeRandomBloke 0:98d83f5b309f 122 // Setup the colour table and mappings
SomeRandomBloke 0:98d83f5b309f 123 #define NUM_COLOURS 12
SomeRandomBloke 0:98d83f5b309f 124 struct ColourTable {
SomeRandomBloke 0:98d83f5b309f 125 char name[12];
SomeRandomBloke 0:98d83f5b309f 126 uint32_t value;
SomeRandomBloke 0:98d83f5b309f 127 } colTable[NUM_COLOURS] = {
SomeRandomBloke 0:98d83f5b309f 128 { "red", 0xFF0000 },
SomeRandomBloke 0:98d83f5b309f 129 { "green", 0x008000 },
SomeRandomBloke 0:98d83f5b309f 130 { "blue", 0x0000FF },
SomeRandomBloke 0:98d83f5b309f 131 { "cyan", 0x00FFFF },
SomeRandomBloke 0:98d83f5b309f 132 { "white", 0xFFFFFF },
SomeRandomBloke 0:98d83f5b309f 133 { "warmwhite", 0xFDF5E6 },
SomeRandomBloke 0:98d83f5b309f 134 { "purple", 0x800080 },
SomeRandomBloke 0:98d83f5b309f 135 { "magenta", 0xFF00FF },
SomeRandomBloke 0:98d83f5b309f 136 { "yellow", 0xFFFF00 },
SomeRandomBloke 0:98d83f5b309f 137 { "orange", 0xFFA500 },
SomeRandomBloke 0:98d83f5b309f 138 { "pink", 0xff69b4 },
SomeRandomBloke 0:98d83f5b309f 139 { "oldlace", 0xfd5e56 }
SomeRandomBloke 0:98d83f5b309f 140 };
SomeRandomBloke 0:98d83f5b309f 141
SomeRandomBloke 0:98d83f5b309f 142
SomeRandomBloke 0:98d83f5b309f 143
SomeRandomBloke 0:98d83f5b309f 144 /** Get status of WiFi connection
SomeRandomBloke 0:98d83f5b309f 145 * displays and returns value
SomeRandomBloke 0:98d83f5b309f 146 */
SomeRandomBloke 0:98d83f5b309f 147 int32_t getWiFiStatus(void)
SomeRandomBloke 0:98d83f5b309f 148 {
SomeRandomBloke 0:98d83f5b309f 149 int32_t status = 0;
SomeRandomBloke 0:98d83f5b309f 150 const char * WIFI_STATUS[] = {"Disconnected", "Scanning", "Connecting", "Connected"};
SomeRandomBloke 0:98d83f5b309f 151
SomeRandomBloke 0:98d83f5b309f 152 status = wifi._wlan.ioctl_statusget();
SomeRandomBloke 0:98d83f5b309f 153 if (( status > -1 ) && ( status < 4 )) {
SomeRandomBloke 0:98d83f5b309f 154 uart.printf(" Wifi Status : %s\r\n", WIFI_STATUS[status]);
SomeRandomBloke 0:98d83f5b309f 155 } else {
SomeRandomBloke 0:98d83f5b309f 156 uart.printf(" Wifi Status : %d\r\n", status);
SomeRandomBloke 0:98d83f5b309f 157 }
SomeRandomBloke 0:98d83f5b309f 158
SomeRandomBloke 0:98d83f5b309f 159 return status;
SomeRandomBloke 0:98d83f5b309f 160 }
SomeRandomBloke 0:98d83f5b309f 161
SomeRandomBloke 0:98d83f5b309f 162 /** Print info from CC3000
SomeRandomBloke 0:98d83f5b309f 163 *
SomeRandomBloke 0:98d83f5b309f 164 */
SomeRandomBloke 0:98d83f5b309f 165 void print_cc3000_info()
SomeRandomBloke 0:98d83f5b309f 166 {
SomeRandomBloke 0:98d83f5b309f 167 uint8_t myMAC[8];
SomeRandomBloke 0:98d83f5b309f 168 uint8_t buffer[2];
SomeRandomBloke 0:98d83f5b309f 169 tNetappIpconfigRetArgs ipinfo2;
SomeRandomBloke 0:98d83f5b309f 170 tUserFS cc_user_info;
SomeRandomBloke 0:98d83f5b309f 171
SomeRandomBloke 0:98d83f5b309f 172 wifi.get_user_file_info((uint8_t *)&cc_user_info, sizeof(cc_user_info));
SomeRandomBloke 0:98d83f5b309f 173 wifi.get_mac_address(myMAC);
SomeRandomBloke 0:98d83f5b309f 174 uart.printf(" MAC address : %02x:%02x:%02x:%02x:%02x:%02x\r\n", myMAC[0], myMAC[1], myMAC[2], myMAC[3], myMAC[4], myMAC[5]);
SomeRandomBloke 0:98d83f5b309f 175
SomeRandomBloke 0:98d83f5b309f 176 if (! wifi._nvmem.read_sp_version( (unsigned char*)&buffer ) ) {
SomeRandomBloke 0:98d83f5b309f 177 uart.printf(" CC3000 Firmware Version : %u.%u \r\n", buffer[0], buffer[1]);
SomeRandomBloke 0:98d83f5b309f 178 } else {
SomeRandomBloke 0:98d83f5b309f 179 uart.printf(" CC3000 Read nvmem failed!");
SomeRandomBloke 0:98d83f5b309f 180 }
SomeRandomBloke 0:98d83f5b309f 181 getWiFiStatus();
SomeRandomBloke 0:98d83f5b309f 182
SomeRandomBloke 0:98d83f5b309f 183 if ( wifi.is_dhcp_configured() ) {
SomeRandomBloke 0:98d83f5b309f 184 wifi.get_ip_config(&ipinfo2);
SomeRandomBloke 0:98d83f5b309f 185 uart.printf(" Connected to : %s \r\n", ipinfo2.uaSSID);
SomeRandomBloke 0:98d83f5b309f 186 uart.printf(" IP : %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);
SomeRandomBloke 0:98d83f5b309f 187 uart.printf(" Gateway : %d.%d.%d.%d \r\n", ipinfo2.aucDefaultGateway[3], ipinfo2.aucDefaultGateway[2], ipinfo2.aucDefaultGateway[1], ipinfo2.aucDefaultGateway[0]);
SomeRandomBloke 0:98d83f5b309f 188 uart.printf(" Subnet : %d.%d.%d.%d \r\n", ipinfo2.aucSubnetMask[3], ipinfo2.aucSubnetMask[2], ipinfo2.aucSubnetMask[1], ipinfo2.aucSubnetMask[0]);
SomeRandomBloke 0:98d83f5b309f 189 uart.printf(" DNS : %d.%d.%d.%d \r\n", ipinfo2.aucDNSServer[3], ipinfo2.aucDNSServer[2], ipinfo2.aucDNSServer[1], ipinfo2.aucDNSServer[0]);
SomeRandomBloke 0:98d83f5b309f 190
SomeRandomBloke 0:98d83f5b309f 191 uart.printf(" Cached IP : %s \r\n", wifi.getIPAddress());
SomeRandomBloke 0:98d83f5b309f 192 uart.printf(" Cached Gateway : %s \r\n", wifi.getGateway());
SomeRandomBloke 0:98d83f5b309f 193 uart.printf(" Cached Subnet : %s \r\n", wifi.getNetworkMask());
SomeRandomBloke 0:98d83f5b309f 194
SomeRandomBloke 0:98d83f5b309f 195 } else {
SomeRandomBloke 0:98d83f5b309f 196 uart.printf(" Not connected \r\n");
SomeRandomBloke 0:98d83f5b309f 197 }
SomeRandomBloke 0:98d83f5b309f 198 }
SomeRandomBloke 0:98d83f5b309f 199
SomeRandomBloke 0:98d83f5b309f 200
SomeRandomBloke 0:98d83f5b309f 201 /** Convert name to colour
SomeRandomBloke 0:98d83f5b309f 202 * @param colStr Received colour name
SomeRandomBloke 0:98d83f5b309f 203 */
SomeRandomBloke 0:98d83f5b309f 204 void setColour( char *colStr )
SomeRandomBloke 0:98d83f5b309f 205 {
SomeRandomBloke 0:98d83f5b309f 206 // uart.printf("received %s\r\n",colStr);
SomeRandomBloke 0:98d83f5b309f 207
SomeRandomBloke 0:98d83f5b309f 208 for( int i=0; i < NUM_COLOURS; i++ ) {
SomeRandomBloke 0:98d83f5b309f 209 if( strncmp( colTable[i].name, colStr, strlen(colTable[i].name) ) == 0 ) {
SomeRandomBloke 0:98d83f5b309f 210 for (int n=0; n < strip.numPixels(); n++) {
SomeRandomBloke 0:98d83f5b309f 211 strip.setPixelColor(n, colTable[i].value);
SomeRandomBloke 0:98d83f5b309f 212 strip.show();
SomeRandomBloke 0:98d83f5b309f 213 wait_ms(50);
SomeRandomBloke 0:98d83f5b309f 214 }
SomeRandomBloke 0:98d83f5b309f 215 return;
SomeRandomBloke 0:98d83f5b309f 216 }
SomeRandomBloke 0:98d83f5b309f 217 }
SomeRandomBloke 0:98d83f5b309f 218 uart.printf("No colour found\r\n");
SomeRandomBloke 0:98d83f5b309f 219
SomeRandomBloke 0:98d83f5b309f 220 }
SomeRandomBloke 0:98d83f5b309f 221
SomeRandomBloke 0:98d83f5b309f 222 /** Read Cheerlights colour
SomeRandomBloke 0:98d83f5b309f 223 * Use http call to get last Cheerlights colour
SomeRandomBloke 0:98d83f5b309f 224 */
SomeRandomBloke 0:98d83f5b309f 225 void readCheerlight( void )
SomeRandomBloke 0:98d83f5b309f 226 {
SomeRandomBloke 0:98d83f5b309f 227 char str[128];
SomeRandomBloke 0:98d83f5b309f 228 //GET data
SomeRandomBloke 0:98d83f5b309f 229 uart.printf("\r\nTrying to fetch page...\r\n");
SomeRandomBloke 0:98d83f5b309f 230 int ret = http.get("http://api.thingspeak.com/channels/1417/field/1/last.txt", str, 128);
SomeRandomBloke 0:98d83f5b309f 231 if (!ret) {
SomeRandomBloke 0:98d83f5b309f 232 uart.printf("Page fetched successfully - read %d characters\r\n", strlen(str));
SomeRandomBloke 0:98d83f5b309f 233 uart.printf("Result: %s\r\n", str);
SomeRandomBloke 0:98d83f5b309f 234 setColour( str );
SomeRandomBloke 0:98d83f5b309f 235 } else {
SomeRandomBloke 0:98d83f5b309f 236 uart.printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
SomeRandomBloke 0:98d83f5b309f 237 }
SomeRandomBloke 0:98d83f5b309f 238
SomeRandomBloke 0:98d83f5b309f 239 }
SomeRandomBloke 0:98d83f5b309f 240
SomeRandomBloke 0:98d83f5b309f 241
SomeRandomBloke 0:98d83f5b309f 242 /** Initialisations
SomeRandomBloke 0:98d83f5b309f 243 * Hardware initialisations and any other setup needed
SomeRandomBloke 0:98d83f5b309f 244 */
SomeRandomBloke 0:98d83f5b309f 245 void init()
SomeRandomBloke 0:98d83f5b309f 246 {
SomeRandomBloke 0:98d83f5b309f 247 LedSC = 0;
SomeRandomBloke 0:98d83f5b309f 248 SCButton.mode(PullUp);
SomeRandomBloke 0:98d83f5b309f 249 NVIC_SetPriority(SSP1_IRQn, 0x0);
SomeRandomBloke 0:98d83f5b309f 250 NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
SomeRandomBloke 0:98d83f5b309f 251
SomeRandomBloke 0:98d83f5b309f 252 // SysTick set to lower priority than Wi-Fi SPI bus interrupt
SomeRandomBloke 0:98d83f5b309f 253 NVIC_SetPriority(SysTick_IRQn, 0x2);
SomeRandomBloke 0:98d83f5b309f 254
SomeRandomBloke 0:98d83f5b309f 255 // Enable RAM1
SomeRandomBloke 0:98d83f5b309f 256 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
SomeRandomBloke 0:98d83f5b309f 257
SomeRandomBloke 0:98d83f5b309f 258 uart.baud(SERIAL_BAUD_RATE);
SomeRandomBloke 0:98d83f5b309f 259
SomeRandomBloke 0:98d83f5b309f 260 strip.begin();
SomeRandomBloke 0:98d83f5b309f 261
SomeRandomBloke 0:98d83f5b309f 262 // Update LED contents, to start they are all 'off'
SomeRandomBloke 0:98d83f5b309f 263 strip.show();
SomeRandomBloke 0:98d83f5b309f 264 }
SomeRandomBloke 0:98d83f5b309f 265
SomeRandomBloke 0:98d83f5b309f 266 /** Main loop, handle WiFi connection, check for button press to start SmartConfig process
SomeRandomBloke 0:98d83f5b309f 267 *
SomeRandomBloke 0:98d83f5b309f 268 */
SomeRandomBloke 0:98d83f5b309f 269 int main( void )
SomeRandomBloke 0:98d83f5b309f 270 {
SomeRandomBloke 0:98d83f5b309f 271 // Initalise the WiFi Module
SomeRandomBloke 0:98d83f5b309f 272 init();
SomeRandomBloke 0:98d83f5b309f 273
SomeRandomBloke 0:98d83f5b309f 274 uart.printf("WiFiDIPCortex Smartconfig Cheerlights\r\n");
SomeRandomBloke 0:98d83f5b309f 275 wifi.start(0);
SomeRandomBloke 0:98d83f5b309f 276
SomeRandomBloke 0:98d83f5b309f 277 // Check if button pressed during startup, if so then go into SmartConfig mode
SomeRandomBloke 0:98d83f5b309f 278 // otherwise just start wifi
SomeRandomBloke 0:98d83f5b309f 279 if(!SCButton) {
SomeRandomBloke 0:98d83f5b309f 280 uart.printf("Smartconfig button pressed\r\n");
SomeRandomBloke 0:98d83f5b309f 281
SomeRandomBloke 0:98d83f5b309f 282 //SmartConfig();
SomeRandomBloke 0:98d83f5b309f 283 uart.printf("\r\nStarting Smart config, waiting for message from smartphone app ....\r\n");
SomeRandomBloke 0:98d83f5b309f 284 LedSC = 1;
SomeRandomBloke 0:98d83f5b309f 285 // We dont want to auto reconnect to an access point
SomeRandomBloke 0:98d83f5b309f 286 wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
SomeRandomBloke 0:98d83f5b309f 287
SomeRandomBloke 0:98d83f5b309f 288 // start smart config will disconnect, set the prefix
SomeRandomBloke 0:98d83f5b309f 289 // wait for a message via a SmartConfig app, store it to the profile list
SomeRandomBloke 0:98d83f5b309f 290 // finally it will reenable auto connection, triggering the module to connect to the new access point
SomeRandomBloke 0:98d83f5b309f 291 wifi.start_smart_config(0);
SomeRandomBloke 0:98d83f5b309f 292 LedSC = 0;
SomeRandomBloke 0:98d83f5b309f 293 UsingSmartConfig = true;
SomeRandomBloke 0:98d83f5b309f 294
SomeRandomBloke 0:98d83f5b309f 295 uart.printf("Back from SmartConfig\r\n");
SomeRandomBloke 0:98d83f5b309f 296
SomeRandomBloke 0:98d83f5b309f 297 wait(2); // for dhcp to configure
SomeRandomBloke 0:98d83f5b309f 298
SomeRandomBloke 0:98d83f5b309f 299 if ( wifi.is_dhcp_configured() ) {
SomeRandomBloke 0:98d83f5b309f 300 if (!Connected) {
SomeRandomBloke 0:98d83f5b309f 301 // We have just connected
SomeRandomBloke 0:98d83f5b309f 302 Connected = true;
SomeRandomBloke 0:98d83f5b309f 303
SomeRandomBloke 0:98d83f5b309f 304 // Start the mdns service, this tells any smart config apps listening we have succeeded
SomeRandomBloke 0:98d83f5b309f 305 wifi._socket.mdns_advertiser(1, (uint8_t *)_deviceName, strlen(_deviceName));
SomeRandomBloke 0:98d83f5b309f 306
SomeRandomBloke 0:98d83f5b309f 307 UsingSmartConfig = false;
SomeRandomBloke 0:98d83f5b309f 308 }
SomeRandomBloke 0:98d83f5b309f 309 } else {
SomeRandomBloke 0:98d83f5b309f 310 Connected = false;
SomeRandomBloke 0:98d83f5b309f 311
SomeRandomBloke 0:98d83f5b309f 312 }
SomeRandomBloke 0:98d83f5b309f 313 } else {
SomeRandomBloke 0:98d83f5b309f 314 uart.printf("Normal startup\r\n");
SomeRandomBloke 0:98d83f5b309f 315 }
SomeRandomBloke 0:98d83f5b309f 316
SomeRandomBloke 0:98d83f5b309f 317 wait_ms(750);
SomeRandomBloke 0:98d83f5b309f 318
SomeRandomBloke 0:98d83f5b309f 319 LedSC = 0;
SomeRandomBloke 0:98d83f5b309f 320 print_cc3000_info();
SomeRandomBloke 0:98d83f5b309f 321
SomeRandomBloke 0:98d83f5b309f 322 // Check if we're connected to WiFi and have an IP address, if not then just flash LED
SomeRandomBloke 0:98d83f5b309f 323 uint32_t status = getWiFiStatus();
SomeRandomBloke 0:98d83f5b309f 324 if( status != 3 || !wifi.is_dhcp_configured() ) {
SomeRandomBloke 0:98d83f5b309f 325 while( 1 ) {
SomeRandomBloke 0:98d83f5b309f 326 LedSC = !LedSC;
SomeRandomBloke 0:98d83f5b309f 327 wait_ms(500);
SomeRandomBloke 0:98d83f5b309f 328 }
SomeRandomBloke 0:98d83f5b309f 329 }
SomeRandomBloke 0:98d83f5b309f 330
SomeRandomBloke 0:98d83f5b309f 331 while (1) {
SomeRandomBloke 0:98d83f5b309f 332 getWiFiStatus();
SomeRandomBloke 0:98d83f5b309f 333 readCheerlight();
SomeRandomBloke 0:98d83f5b309f 334 // Pause for a minute before checking again
SomeRandomBloke 0:98d83f5b309f 335 wait(60);
SomeRandomBloke 0:98d83f5b309f 336 }
SomeRandomBloke 0:98d83f5b309f 337 }