Cheerlights client using WiFiDIPCortex and WS2801 RGB LED strip

Dependencies:   Adafruit_WS2801 HTTPClient cc3000_hostdriver_mbedsocket mbed

Committer:
SomeRandomBloke
Date:
Wed Feb 12 22:18:52 2014 +0000
Revision:
1:40027344b249
Parent:
0:98d83f5b309f
Child:
3:7410e3231e7a
updates to use hardware SPI and checking for last colour set

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 1:40027344b249 88 // For Bit-Banging SPI use Pins 40 and 39
SomeRandomBloke 1:40027344b249 89 PinName dataPin(p40); // PIN 40 - Yellow wire on Adafruit Pixels
SomeRandomBloke 1:40027344b249 90 PinName clockPin(p39); // PIN 39 - Green wire on Adafruit Pixels
SomeRandomBloke 1:40027344b249 91
SomeRandomBloke 1:40027344b249 92 // For hardware SPI use Pin 16 for Data and pin 13 for Clk
SomeRandomBloke 0:98d83f5b309f 93
SomeRandomBloke 0:98d83f5b309f 94 /* cc3000 module declaration specific for user's board. Check also init() */
SomeRandomBloke 0:98d83f5b309f 95 #if (MY_BOARD == WIGO)
SomeRandomBloke 0:98d83f5b309f 96 cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn);
SomeRandomBloke 0:98d83f5b309f 97 Serial uart(USBTX,USBRX);
SomeRandomBloke 0:98d83f5b309f 98 #elif (MY_BOARD == WIFI_DIPCORTEX)
SomeRandomBloke 0:98d83f5b309f 99 cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37));
SomeRandomBloke 0:98d83f5b309f 100 Serial uart(p19, p20);
SomeRandomBloke 0:98d83f5b309f 101 #else
SomeRandomBloke 0:98d83f5b309f 102
SomeRandomBloke 0:98d83f5b309f 103 #endif
SomeRandomBloke 0:98d83f5b309f 104
SomeRandomBloke 0:98d83f5b309f 105 #ifndef CC3000_UNENCRYPTED_SMART_CONFIG
SomeRandomBloke 0:98d83f5b309f 106 const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36};
SomeRandomBloke 0:98d83f5b309f 107 #else
SomeRandomBloke 0:98d83f5b309f 108 const uint8_t smartconfigkey = 0;
SomeRandomBloke 0:98d83f5b309f 109 #endif
SomeRandomBloke 0:98d83f5b309f 110
SomeRandomBloke 0:98d83f5b309f 111 tNetappIpconfigRetArgs ipinfo;
SomeRandomBloke 0:98d83f5b309f 112 extern char tmpBuffer[512];
SomeRandomBloke 0:98d83f5b309f 113
SomeRandomBloke 0:98d83f5b309f 114 bool Connected = false;
SomeRandomBloke 0:98d83f5b309f 115 bool UsingSmartConfig = false;
SomeRandomBloke 0:98d83f5b309f 116 char _deviceName[] = "CC3000";
SomeRandomBloke 0:98d83f5b309f 117
SomeRandomBloke 0:98d83f5b309f 118 HTTPClient http;
SomeRandomBloke 0:98d83f5b309f 119 //char str[128];
SomeRandomBloke 0:98d83f5b309f 120 uint32_t lastColour = 0;
SomeRandomBloke 0:98d83f5b309f 121
SomeRandomBloke 0:98d83f5b309f 122 // Set the first variable to the number of rows, the second to number of pixels. 32 = 32 pixels in a row
SomeRandomBloke 1:40027344b249 123 Adafruit_WS2801 strip = Adafruit_WS2801(32, dataPin, clockPin);
SomeRandomBloke 0:98d83f5b309f 124
SomeRandomBloke 0:98d83f5b309f 125 // Setup the colour table and mappings
SomeRandomBloke 0:98d83f5b309f 126 #define NUM_COLOURS 12
SomeRandomBloke 0:98d83f5b309f 127 struct ColourTable {
SomeRandomBloke 0:98d83f5b309f 128 char name[12];
SomeRandomBloke 0:98d83f5b309f 129 uint32_t value;
SomeRandomBloke 0:98d83f5b309f 130 } colTable[NUM_COLOURS] = {
SomeRandomBloke 1:40027344b249 131 { "red", 0xff0000 },
SomeRandomBloke 0:98d83f5b309f 132 { "green", 0x008000 },
SomeRandomBloke 1:40027344b249 133 { "blue", 0x0000ff },
SomeRandomBloke 1:40027344b249 134 { "cyan", 0x00ffff },
SomeRandomBloke 1:40027344b249 135 { "white", 0xffffff },
SomeRandomBloke 1:40027344b249 136 { "warmwhite", 0xfdf5e6 },
SomeRandomBloke 0:98d83f5b309f 137 { "purple", 0x800080 },
SomeRandomBloke 1:40027344b249 138 { "magenta", 0xff00ff },
SomeRandomBloke 1:40027344b249 139 { "yellow", 0xffff00 },
SomeRandomBloke 1:40027344b249 140 { "orange", 0xffa500 },
SomeRandomBloke 0:98d83f5b309f 141 { "pink", 0xff69b4 },
SomeRandomBloke 0:98d83f5b309f 142 { "oldlace", 0xfd5e56 }
SomeRandomBloke 0:98d83f5b309f 143 };
SomeRandomBloke 0:98d83f5b309f 144
SomeRandomBloke 0:98d83f5b309f 145
SomeRandomBloke 0:98d83f5b309f 146
SomeRandomBloke 0:98d83f5b309f 147 /** Get status of WiFi connection
SomeRandomBloke 0:98d83f5b309f 148 * displays and returns value
SomeRandomBloke 0:98d83f5b309f 149 */
SomeRandomBloke 0:98d83f5b309f 150 int32_t getWiFiStatus(void)
SomeRandomBloke 0:98d83f5b309f 151 {
SomeRandomBloke 0:98d83f5b309f 152 int32_t status = 0;
SomeRandomBloke 0:98d83f5b309f 153 const char * WIFI_STATUS[] = {"Disconnected", "Scanning", "Connecting", "Connected"};
SomeRandomBloke 0:98d83f5b309f 154
SomeRandomBloke 0:98d83f5b309f 155 status = wifi._wlan.ioctl_statusget();
SomeRandomBloke 0:98d83f5b309f 156 if (( status > -1 ) && ( status < 4 )) {
SomeRandomBloke 0:98d83f5b309f 157 uart.printf(" Wifi Status : %s\r\n", WIFI_STATUS[status]);
SomeRandomBloke 0:98d83f5b309f 158 } else {
SomeRandomBloke 0:98d83f5b309f 159 uart.printf(" Wifi Status : %d\r\n", status);
SomeRandomBloke 0:98d83f5b309f 160 }
SomeRandomBloke 0:98d83f5b309f 161
SomeRandomBloke 0:98d83f5b309f 162 return status;
SomeRandomBloke 0:98d83f5b309f 163 }
SomeRandomBloke 0:98d83f5b309f 164
SomeRandomBloke 0:98d83f5b309f 165 /** Print info from CC3000
SomeRandomBloke 0:98d83f5b309f 166 *
SomeRandomBloke 0:98d83f5b309f 167 */
SomeRandomBloke 0:98d83f5b309f 168 void print_cc3000_info()
SomeRandomBloke 0:98d83f5b309f 169 {
SomeRandomBloke 0:98d83f5b309f 170 uint8_t myMAC[8];
SomeRandomBloke 0:98d83f5b309f 171 uint8_t buffer[2];
SomeRandomBloke 0:98d83f5b309f 172 tNetappIpconfigRetArgs ipinfo2;
SomeRandomBloke 0:98d83f5b309f 173 tUserFS cc_user_info;
SomeRandomBloke 0:98d83f5b309f 174
SomeRandomBloke 0:98d83f5b309f 175 wifi.get_user_file_info((uint8_t *)&cc_user_info, sizeof(cc_user_info));
SomeRandomBloke 0:98d83f5b309f 176 wifi.get_mac_address(myMAC);
SomeRandomBloke 0:98d83f5b309f 177 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 178
SomeRandomBloke 0:98d83f5b309f 179 if (! wifi._nvmem.read_sp_version( (unsigned char*)&buffer ) ) {
SomeRandomBloke 0:98d83f5b309f 180 uart.printf(" CC3000 Firmware Version : %u.%u \r\n", buffer[0], buffer[1]);
SomeRandomBloke 0:98d83f5b309f 181 } else {
SomeRandomBloke 0:98d83f5b309f 182 uart.printf(" CC3000 Read nvmem failed!");
SomeRandomBloke 0:98d83f5b309f 183 }
SomeRandomBloke 0:98d83f5b309f 184 getWiFiStatus();
SomeRandomBloke 0:98d83f5b309f 185
SomeRandomBloke 0:98d83f5b309f 186 if ( wifi.is_dhcp_configured() ) {
SomeRandomBloke 0:98d83f5b309f 187 wifi.get_ip_config(&ipinfo2);
SomeRandomBloke 0:98d83f5b309f 188 uart.printf(" Connected to : %s \r\n", ipinfo2.uaSSID);
SomeRandomBloke 0:98d83f5b309f 189 uart.printf(" IP : %d.%d.%d.%d \r\n", ipinfo2.aucIP[3], ipinfo2.aucIP[2], ipinfo2.aucIP[1], ipinfo2.aucIP[0]);
SomeRandomBloke 0:98d83f5b309f 190 uart.printf(" Gateway : %d.%d.%d.%d \r\n", ipinfo2.aucDefaultGateway[3], ipinfo2.aucDefaultGateway[2], ipinfo2.aucDefaultGateway[1], ipinfo2.aucDefaultGateway[0]);
SomeRandomBloke 0:98d83f5b309f 191 uart.printf(" Subnet : %d.%d.%d.%d \r\n", ipinfo2.aucSubnetMask[3], ipinfo2.aucSubnetMask[2], ipinfo2.aucSubnetMask[1], ipinfo2.aucSubnetMask[0]);
SomeRandomBloke 0:98d83f5b309f 192 uart.printf(" DNS : %d.%d.%d.%d \r\n", ipinfo2.aucDNSServer[3], ipinfo2.aucDNSServer[2], ipinfo2.aucDNSServer[1], ipinfo2.aucDNSServer[0]);
SomeRandomBloke 0:98d83f5b309f 193
SomeRandomBloke 0:98d83f5b309f 194 uart.printf(" Cached IP : %s \r\n", wifi.getIPAddress());
SomeRandomBloke 0:98d83f5b309f 195 uart.printf(" Cached Gateway : %s \r\n", wifi.getGateway());
SomeRandomBloke 0:98d83f5b309f 196 uart.printf(" Cached Subnet : %s \r\n", wifi.getNetworkMask());
SomeRandomBloke 0:98d83f5b309f 197
SomeRandomBloke 0:98d83f5b309f 198 } else {
SomeRandomBloke 0:98d83f5b309f 199 uart.printf(" Not connected \r\n");
SomeRandomBloke 0:98d83f5b309f 200 }
SomeRandomBloke 0:98d83f5b309f 201 }
SomeRandomBloke 0:98d83f5b309f 202
SomeRandomBloke 0:98d83f5b309f 203
SomeRandomBloke 0:98d83f5b309f 204 /** Convert name to colour
SomeRandomBloke 0:98d83f5b309f 205 * @param colStr Received colour name
SomeRandomBloke 0:98d83f5b309f 206 */
SomeRandomBloke 0:98d83f5b309f 207 void setColour( char *colStr )
SomeRandomBloke 0:98d83f5b309f 208 {
SomeRandomBloke 0:98d83f5b309f 209 // uart.printf("received %s\r\n",colStr);
SomeRandomBloke 0:98d83f5b309f 210
SomeRandomBloke 0:98d83f5b309f 211 for( int i=0; i < NUM_COLOURS; i++ ) {
SomeRandomBloke 0:98d83f5b309f 212 if( strncmp( colTable[i].name, colStr, strlen(colTable[i].name) ) == 0 ) {
SomeRandomBloke 1:40027344b249 213 if( colTable[i].value != lastColour ) {
SomeRandomBloke 1:40027344b249 214 for (int n=0; n < strip.numPixels(); n++) {
SomeRandomBloke 1:40027344b249 215 strip.setPixelColor(n, colTable[i].value);
SomeRandomBloke 1:40027344b249 216 strip.show();
SomeRandomBloke 1:40027344b249 217 wait_ms(100);
SomeRandomBloke 1:40027344b249 218 }
SomeRandomBloke 1:40027344b249 219 lastColour = colTable[i].value;
SomeRandomBloke 1:40027344b249 220 } else {
SomeRandomBloke 1:40027344b249 221 uart.printf("Same as previous colour\r\n");
SomeRandomBloke 0:98d83f5b309f 222 }
SomeRandomBloke 0:98d83f5b309f 223 return;
SomeRandomBloke 0:98d83f5b309f 224 }
SomeRandomBloke 0:98d83f5b309f 225 }
SomeRandomBloke 0:98d83f5b309f 226 uart.printf("No colour found\r\n");
SomeRandomBloke 0:98d83f5b309f 227
SomeRandomBloke 0:98d83f5b309f 228 }
SomeRandomBloke 0:98d83f5b309f 229
SomeRandomBloke 0:98d83f5b309f 230 /** Read Cheerlights colour
SomeRandomBloke 0:98d83f5b309f 231 * Use http call to get last Cheerlights colour
SomeRandomBloke 0:98d83f5b309f 232 */
SomeRandomBloke 0:98d83f5b309f 233 void readCheerlight( void )
SomeRandomBloke 0:98d83f5b309f 234 {
SomeRandomBloke 0:98d83f5b309f 235 char str[128];
SomeRandomBloke 0:98d83f5b309f 236 //GET data
SomeRandomBloke 0:98d83f5b309f 237 uart.printf("\r\nTrying to fetch page...\r\n");
SomeRandomBloke 0:98d83f5b309f 238 int ret = http.get("http://api.thingspeak.com/channels/1417/field/1/last.txt", str, 128);
SomeRandomBloke 1:40027344b249 239 // Just a local test url, saves having to tweet the cheerlights colour every time
SomeRandomBloke 1:40027344b249 240 // int ret = http.get("http://192.168.1.3/last.txt", str, 128);
SomeRandomBloke 0:98d83f5b309f 241 if (!ret) {
SomeRandomBloke 0:98d83f5b309f 242 uart.printf("Page fetched successfully - read %d characters\r\n", strlen(str));
SomeRandomBloke 0:98d83f5b309f 243 uart.printf("Result: %s\r\n", str);
SomeRandomBloke 0:98d83f5b309f 244 setColour( str );
SomeRandomBloke 0:98d83f5b309f 245 } else {
SomeRandomBloke 0:98d83f5b309f 246 uart.printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode());
SomeRandomBloke 0:98d83f5b309f 247 }
SomeRandomBloke 0:98d83f5b309f 248
SomeRandomBloke 0:98d83f5b309f 249 }
SomeRandomBloke 0:98d83f5b309f 250
SomeRandomBloke 0:98d83f5b309f 251
SomeRandomBloke 0:98d83f5b309f 252 /** Initialisations
SomeRandomBloke 0:98d83f5b309f 253 * Hardware initialisations and any other setup needed
SomeRandomBloke 0:98d83f5b309f 254 */
SomeRandomBloke 0:98d83f5b309f 255 void init()
SomeRandomBloke 0:98d83f5b309f 256 {
SomeRandomBloke 0:98d83f5b309f 257 LedSC = 0;
SomeRandomBloke 0:98d83f5b309f 258 SCButton.mode(PullUp);
SomeRandomBloke 0:98d83f5b309f 259 NVIC_SetPriority(SSP1_IRQn, 0x0);
SomeRandomBloke 0:98d83f5b309f 260 NVIC_SetPriority(PIN_INT0_IRQn, 0x1);
SomeRandomBloke 0:98d83f5b309f 261
SomeRandomBloke 0:98d83f5b309f 262 // SysTick set to lower priority than Wi-Fi SPI bus interrupt
SomeRandomBloke 0:98d83f5b309f 263 NVIC_SetPriority(SysTick_IRQn, 0x2);
SomeRandomBloke 0:98d83f5b309f 264
SomeRandomBloke 0:98d83f5b309f 265 // Enable RAM1
SomeRandomBloke 0:98d83f5b309f 266 LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26);
SomeRandomBloke 0:98d83f5b309f 267
SomeRandomBloke 0:98d83f5b309f 268 uart.baud(SERIAL_BAUD_RATE);
SomeRandomBloke 1:40027344b249 269
SomeRandomBloke 1:40027344b249 270 strip.updatePins(); // Switch to Hardware SPI
SomeRandomBloke 0:98d83f5b309f 271
SomeRandomBloke 0:98d83f5b309f 272 strip.begin();
SomeRandomBloke 0:98d83f5b309f 273
SomeRandomBloke 0:98d83f5b309f 274 // Update LED contents, to start they are all 'off'
SomeRandomBloke 0:98d83f5b309f 275 strip.show();
SomeRandomBloke 0:98d83f5b309f 276 }
SomeRandomBloke 0:98d83f5b309f 277
SomeRandomBloke 0:98d83f5b309f 278 /** Main loop, handle WiFi connection, check for button press to start SmartConfig process
SomeRandomBloke 0:98d83f5b309f 279 *
SomeRandomBloke 0:98d83f5b309f 280 */
SomeRandomBloke 0:98d83f5b309f 281 int main( void )
SomeRandomBloke 0:98d83f5b309f 282 {
SomeRandomBloke 0:98d83f5b309f 283 // Initalise the WiFi Module
SomeRandomBloke 0:98d83f5b309f 284 init();
SomeRandomBloke 0:98d83f5b309f 285
SomeRandomBloke 0:98d83f5b309f 286 uart.printf("WiFiDIPCortex Smartconfig Cheerlights\r\n");
SomeRandomBloke 0:98d83f5b309f 287 wifi.start(0);
SomeRandomBloke 0:98d83f5b309f 288
SomeRandomBloke 0:98d83f5b309f 289 // Check if button pressed during startup, if so then go into SmartConfig mode
SomeRandomBloke 0:98d83f5b309f 290 // otherwise just start wifi
SomeRandomBloke 0:98d83f5b309f 291 if(!SCButton) {
SomeRandomBloke 0:98d83f5b309f 292 uart.printf("Smartconfig button pressed\r\n");
SomeRandomBloke 0:98d83f5b309f 293
SomeRandomBloke 0:98d83f5b309f 294 //SmartConfig();
SomeRandomBloke 0:98d83f5b309f 295 uart.printf("\r\nStarting Smart config, waiting for message from smartphone app ....\r\n");
SomeRandomBloke 0:98d83f5b309f 296 LedSC = 1;
SomeRandomBloke 0:98d83f5b309f 297 // We dont want to auto reconnect to an access point
SomeRandomBloke 0:98d83f5b309f 298 wifi._wlan.ioctl_set_connection_policy(0, 0, 0);
SomeRandomBloke 0:98d83f5b309f 299
SomeRandomBloke 0:98d83f5b309f 300 // start smart config will disconnect, set the prefix
SomeRandomBloke 0:98d83f5b309f 301 // wait for a message via a SmartConfig app, store it to the profile list
SomeRandomBloke 0:98d83f5b309f 302 // finally it will reenable auto connection, triggering the module to connect to the new access point
SomeRandomBloke 0:98d83f5b309f 303 wifi.start_smart_config(0);
SomeRandomBloke 0:98d83f5b309f 304 LedSC = 0;
SomeRandomBloke 0:98d83f5b309f 305 UsingSmartConfig = true;
SomeRandomBloke 0:98d83f5b309f 306
SomeRandomBloke 0:98d83f5b309f 307 uart.printf("Back from SmartConfig\r\n");
SomeRandomBloke 0:98d83f5b309f 308
SomeRandomBloke 0:98d83f5b309f 309 wait(2); // for dhcp to configure
SomeRandomBloke 0:98d83f5b309f 310
SomeRandomBloke 0:98d83f5b309f 311 if ( wifi.is_dhcp_configured() ) {
SomeRandomBloke 0:98d83f5b309f 312 if (!Connected) {
SomeRandomBloke 0:98d83f5b309f 313 // We have just connected
SomeRandomBloke 0:98d83f5b309f 314 Connected = true;
SomeRandomBloke 0:98d83f5b309f 315
SomeRandomBloke 0:98d83f5b309f 316 // Start the mdns service, this tells any smart config apps listening we have succeeded
SomeRandomBloke 0:98d83f5b309f 317 wifi._socket.mdns_advertiser(1, (uint8_t *)_deviceName, strlen(_deviceName));
SomeRandomBloke 0:98d83f5b309f 318
SomeRandomBloke 0:98d83f5b309f 319 UsingSmartConfig = false;
SomeRandomBloke 0:98d83f5b309f 320 }
SomeRandomBloke 0:98d83f5b309f 321 } else {
SomeRandomBloke 0:98d83f5b309f 322 Connected = false;
SomeRandomBloke 0:98d83f5b309f 323
SomeRandomBloke 0:98d83f5b309f 324 }
SomeRandomBloke 0:98d83f5b309f 325 } else {
SomeRandomBloke 0:98d83f5b309f 326 uart.printf("Normal startup\r\n");
SomeRandomBloke 0:98d83f5b309f 327 }
SomeRandomBloke 0:98d83f5b309f 328
SomeRandomBloke 0:98d83f5b309f 329 wait_ms(750);
SomeRandomBloke 0:98d83f5b309f 330
SomeRandomBloke 0:98d83f5b309f 331 LedSC = 0;
SomeRandomBloke 0:98d83f5b309f 332 print_cc3000_info();
SomeRandomBloke 0:98d83f5b309f 333
SomeRandomBloke 0:98d83f5b309f 334 // Check if we're connected to WiFi and have an IP address, if not then just flash LED
SomeRandomBloke 0:98d83f5b309f 335 uint32_t status = getWiFiStatus();
SomeRandomBloke 0:98d83f5b309f 336 if( status != 3 || !wifi.is_dhcp_configured() ) {
SomeRandomBloke 0:98d83f5b309f 337 while( 1 ) {
SomeRandomBloke 0:98d83f5b309f 338 LedSC = !LedSC;
SomeRandomBloke 0:98d83f5b309f 339 wait_ms(500);
SomeRandomBloke 0:98d83f5b309f 340 }
SomeRandomBloke 0:98d83f5b309f 341 }
SomeRandomBloke 0:98d83f5b309f 342
SomeRandomBloke 0:98d83f5b309f 343 while (1) {
SomeRandomBloke 0:98d83f5b309f 344 getWiFiStatus();
SomeRandomBloke 0:98d83f5b309f 345 readCheerlight();
SomeRandomBloke 0:98d83f5b309f 346 // Pause for a minute before checking again
SomeRandomBloke 0:98d83f5b309f 347 wait(60);
SomeRandomBloke 0:98d83f5b309f 348 }
SomeRandomBloke 0:98d83f5b309f 349 }