Cheerlights client using WiFiDIPCortex and WS2801 RGB LED strip
Dependencies: Adafruit_WS2801 HTTPClient cc3000_hostdriver_mbedsocket mbed
main.cpp@3:7410e3231e7a, 2014-11-26 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Wed Nov 26 14:02:36 2014 +0000
- Revision:
- 3:7410e3231e7a
- Parent:
- 1:40027344b249
Latest updates of WiFi Cheerlights
Who changed what in which revision?
User | Revision | Line number | New 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 | |
SomeRandomBloke | 3:7410e3231e7a | 72 | // Some local defines |
SomeRandomBloke | 0:98d83f5b309f | 73 | #define SERIAL_BAUD_RATE 115200 |
SomeRandomBloke | 0:98d83f5b309f | 74 | |
SomeRandomBloke | 3:7410e3231e7a | 75 | #define NUM_PIXELS 50 |
SomeRandomBloke | 3:7410e3231e7a | 76 | #define NUM_COL_HISTORY 5 |
SomeRandomBloke | 3:7410e3231e7a | 77 | |
SomeRandomBloke | 0:98d83f5b309f | 78 | #define WIGO 1 |
SomeRandomBloke | 0:98d83f5b309f | 79 | #define WIFI_DIPCORTEX 2 |
SomeRandomBloke | 0:98d83f5b309f | 80 | #define UNDEFINED 3 |
SomeRandomBloke | 0:98d83f5b309f | 81 | |
SomeRandomBloke | 0:98d83f5b309f | 82 | #define MY_BOARD WIFI_DIPCORTEX |
SomeRandomBloke | 0:98d83f5b309f | 83 | |
SomeRandomBloke | 0:98d83f5b309f | 84 | using namespace mbed_cc3000; |
SomeRandomBloke | 0:98d83f5b309f | 85 | |
SomeRandomBloke | 3:7410e3231e7a | 86 | // LED to indicate SmartConfig is running |
SomeRandomBloke | 0:98d83f5b309f | 87 | DigitalOut LedSC(P0_1); |
SomeRandomBloke | 3:7410e3231e7a | 88 | // Button to hold down during reset to enter SmartConfig mode |
SomeRandomBloke | 0:98d83f5b309f | 89 | DigitalIn SCButton(P1_31); |
SomeRandomBloke | 0:98d83f5b309f | 90 | |
SomeRandomBloke | 1:40027344b249 | 91 | // For Bit-Banging SPI use Pins 40 and 39 |
SomeRandomBloke | 1:40027344b249 | 92 | PinName dataPin(p40); // PIN 40 - Yellow wire on Adafruit Pixels |
SomeRandomBloke | 1:40027344b249 | 93 | PinName clockPin(p39); // PIN 39 - Green wire on Adafruit Pixels |
SomeRandomBloke | 1:40027344b249 | 94 | |
SomeRandomBloke | 1:40027344b249 | 95 | // For hardware SPI use Pin 16 for Data and pin 13 for Clk |
SomeRandomBloke | 0:98d83f5b309f | 96 | |
SomeRandomBloke | 0:98d83f5b309f | 97 | /* cc3000 module declaration specific for user's board. Check also init() */ |
SomeRandomBloke | 0:98d83f5b309f | 98 | #if (MY_BOARD == WIGO) |
SomeRandomBloke | 0:98d83f5b309f | 99 | cc3000 wifi(PTA16, PTA13, PTD0, SPI(PTD2, PTD3, PTC5), PORTA_IRQn); |
SomeRandomBloke | 0:98d83f5b309f | 100 | Serial uart(USBTX,USBRX); |
SomeRandomBloke | 0:98d83f5b309f | 101 | #elif (MY_BOARD == WIFI_DIPCORTEX) |
SomeRandomBloke | 0:98d83f5b309f | 102 | cc3000 wifi(p28, p27, p30, SPI(p21, p14, p37)); |
SomeRandomBloke | 0:98d83f5b309f | 103 | Serial uart(p19, p20); |
SomeRandomBloke | 0:98d83f5b309f | 104 | #else |
SomeRandomBloke | 0:98d83f5b309f | 105 | |
SomeRandomBloke | 0:98d83f5b309f | 106 | #endif |
SomeRandomBloke | 0:98d83f5b309f | 107 | |
SomeRandomBloke | 0:98d83f5b309f | 108 | #ifndef CC3000_UNENCRYPTED_SMART_CONFIG |
SomeRandomBloke | 3:7410e3231e7a | 109 | //const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; |
SomeRandomBloke | 0:98d83f5b309f | 110 | #else |
SomeRandomBloke | 3:7410e3231e7a | 111 | //const uint8_t smartconfigkey = 0; |
SomeRandomBloke | 0:98d83f5b309f | 112 | #endif |
SomeRandomBloke | 0:98d83f5b309f | 113 | |
SomeRandomBloke | 0:98d83f5b309f | 114 | tNetappIpconfigRetArgs ipinfo; |
SomeRandomBloke | 0:98d83f5b309f | 115 | extern char tmpBuffer[512]; |
SomeRandomBloke | 0:98d83f5b309f | 116 | |
SomeRandomBloke | 0:98d83f5b309f | 117 | bool Connected = false; |
SomeRandomBloke | 0:98d83f5b309f | 118 | bool UsingSmartConfig = false; |
SomeRandomBloke | 0:98d83f5b309f | 119 | char _deviceName[] = "CC3000"; |
SomeRandomBloke | 0:98d83f5b309f | 120 | |
SomeRandomBloke | 0:98d83f5b309f | 121 | HTTPClient http; |
SomeRandomBloke | 0:98d83f5b309f | 122 | |
SomeRandomBloke | 0:98d83f5b309f | 123 | // Set the first variable to the number of rows, the second to number of pixels. 32 = 32 pixels in a row |
SomeRandomBloke | 3:7410e3231e7a | 124 | Adafruit_WS2801 strip = Adafruit_WS2801(NUM_PIXELS, dataPin, clockPin); |
SomeRandomBloke | 0:98d83f5b309f | 125 | |
SomeRandomBloke | 0:98d83f5b309f | 126 | // Setup the colour table and mappings |
SomeRandomBloke | 3:7410e3231e7a | 127 | #define NUM_COLOURS 13 |
SomeRandomBloke | 0:98d83f5b309f | 128 | struct ColourTable { |
SomeRandomBloke | 0:98d83f5b309f | 129 | char name[12]; |
SomeRandomBloke | 0:98d83f5b309f | 130 | uint32_t value; |
SomeRandomBloke | 0:98d83f5b309f | 131 | } colTable[NUM_COLOURS] = { |
SomeRandomBloke | 1:40027344b249 | 132 | { "red", 0xff0000 }, |
SomeRandomBloke | 0:98d83f5b309f | 133 | { "green", 0x008000 }, |
SomeRandomBloke | 1:40027344b249 | 134 | { "blue", 0x0000ff }, |
SomeRandomBloke | 1:40027344b249 | 135 | { "cyan", 0x00ffff }, |
SomeRandomBloke | 1:40027344b249 | 136 | { "white", 0xffffff }, |
SomeRandomBloke | 1:40027344b249 | 137 | { "warmwhite", 0xfdf5e6 }, |
SomeRandomBloke | 0:98d83f5b309f | 138 | { "purple", 0x800080 }, |
SomeRandomBloke | 1:40027344b249 | 139 | { "magenta", 0xff00ff }, |
SomeRandomBloke | 1:40027344b249 | 140 | { "yellow", 0xffff00 }, |
SomeRandomBloke | 1:40027344b249 | 141 | { "orange", 0xffa500 }, |
SomeRandomBloke | 0:98d83f5b309f | 142 | { "pink", 0xff69b4 }, |
SomeRandomBloke | 3:7410e3231e7a | 143 | { "oldlace", 0xfd5e56 }, |
SomeRandomBloke | 3:7410e3231e7a | 144 | { "black", 0x000000 } |
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 | 3:7410e3231e7a | 204 | /** Convert name to colour and set it on LEDs. With colour history, strip will be |
SomeRandomBloke | 3:7410e3231e7a | 205 | * changing colour for every new colour detected producing an almost random effect |
SomeRandomBloke | 3:7410e3231e7a | 206 | * @param colNum Number of the received colour, starts 0 to NUM_COL_HISTORY with highest being latest colour |
SomeRandomBloke | 0:98d83f5b309f | 207 | * @param colStr Received colour name |
SomeRandomBloke | 0:98d83f5b309f | 208 | */ |
SomeRandomBloke | 3:7410e3231e7a | 209 | void setColour( int colNum, char *colStr ) |
SomeRandomBloke | 0:98d83f5b309f | 210 | { |
SomeRandomBloke | 0:98d83f5b309f | 211 | // uart.printf("received %s\r\n",colStr); |
SomeRandomBloke | 0:98d83f5b309f | 212 | |
SomeRandomBloke | 0:98d83f5b309f | 213 | for( int i=0; i < NUM_COLOURS; i++ ) { |
SomeRandomBloke | 0:98d83f5b309f | 214 | if( strncmp( colTable[i].name, colStr, strlen(colTable[i].name) ) == 0 ) { |
SomeRandomBloke | 3:7410e3231e7a | 215 | for (int n=colNum; n < strip.numPixels()+NUM_COL_HISTORY; n += NUM_COL_HISTORY) { |
SomeRandomBloke | 3:7410e3231e7a | 216 | strip.setPixelColor(n, colTable[i].value); |
SomeRandomBloke | 0:98d83f5b309f | 217 | } |
SomeRandomBloke | 3:7410e3231e7a | 218 | // Update strip after colour has been set |
SomeRandomBloke | 3:7410e3231e7a | 219 | strip.show(); |
SomeRandomBloke | 3:7410e3231e7a | 220 | // Slight pause |
SomeRandomBloke | 3:7410e3231e7a | 221 | wait_ms(250); |
SomeRandomBloke | 0:98d83f5b309f | 222 | return; |
SomeRandomBloke | 0:98d83f5b309f | 223 | } |
SomeRandomBloke | 0:98d83f5b309f | 224 | } |
SomeRandomBloke | 0:98d83f5b309f | 225 | uart.printf("No colour found\r\n"); |
SomeRandomBloke | 0:98d83f5b309f | 226 | |
SomeRandomBloke | 0:98d83f5b309f | 227 | } |
SomeRandomBloke | 0:98d83f5b309f | 228 | |
SomeRandomBloke | 3:7410e3231e7a | 229 | #define CL_BUFFSIZE 1024 |
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 | 3:7410e3231e7a | 235 | char responseStr[CL_BUFFSIZE]; |
SomeRandomBloke | 0:98d83f5b309f | 236 | //GET data |
SomeRandomBloke | 0:98d83f5b309f | 237 | uart.printf("\r\nTrying to fetch page...\r\n"); |
SomeRandomBloke | 3:7410e3231e7a | 238 | int ret = http.get("http://api.thingspeak.com/channels/1417/feed.csv?results=5", responseStr, CL_BUFFSIZE); |
SomeRandomBloke | 0:98d83f5b309f | 239 | if (!ret) { |
SomeRandomBloke | 3:7410e3231e7a | 240 | uart.printf("Page fetched successfully - read %d characters\r\n", strlen(responseStr)); |
SomeRandomBloke | 3:7410e3231e7a | 241 | //uart.printf("Result: %s\r\n", responseStr); |
SomeRandomBloke | 3:7410e3231e7a | 242 | // Parse CSV and set colours |
SomeRandomBloke | 3:7410e3231e7a | 243 | // Get a line |
SomeRandomBloke | 3:7410e3231e7a | 244 | char *ptr = responseStr; |
SomeRandomBloke | 3:7410e3231e7a | 245 | // Skip header line |
SomeRandomBloke | 3:7410e3231e7a | 246 | while(*ptr++ != 0x0a ); // Header |
SomeRandomBloke | 3:7410e3231e7a | 247 | // 5 colours |
SomeRandomBloke | 3:7410e3231e7a | 248 | for( int i=0; i<5; i++ ) { |
SomeRandomBloke | 3:7410e3231e7a | 249 | while(*ptr++ != ','); // Column 1 - Date, Skip |
SomeRandomBloke | 3:7410e3231e7a | 250 | while(*ptr++ != ','); // Column 2 - ID, Skip |
SomeRandomBloke | 3:7410e3231e7a | 251 | char col[10]; |
SomeRandomBloke | 3:7410e3231e7a | 252 | char *cPtr = col; |
SomeRandomBloke | 3:7410e3231e7a | 253 | while( *ptr != 0x0a ) { // Column 3 - colour |
SomeRandomBloke | 3:7410e3231e7a | 254 | *cPtr++ = *ptr++; |
SomeRandomBloke | 3:7410e3231e7a | 255 | } |
SomeRandomBloke | 3:7410e3231e7a | 256 | *cPtr = '\0'; |
SomeRandomBloke | 3:7410e3231e7a | 257 | uart.printf("%d %s\r\n",i,col); |
SomeRandomBloke | 3:7410e3231e7a | 258 | setColour( i, col ); |
SomeRandomBloke | 3:7410e3231e7a | 259 | } |
SomeRandomBloke | 0:98d83f5b309f | 260 | } else { |
SomeRandomBloke | 0:98d83f5b309f | 261 | uart.printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); |
SomeRandomBloke | 0:98d83f5b309f | 262 | } |
SomeRandomBloke | 0:98d83f5b309f | 263 | |
SomeRandomBloke | 3:7410e3231e7a | 264 | uart.printf("End of readCheerlights\n\r"); |
SomeRandomBloke | 0:98d83f5b309f | 265 | } |
SomeRandomBloke | 0:98d83f5b309f | 266 | |
SomeRandomBloke | 0:98d83f5b309f | 267 | |
SomeRandomBloke | 0:98d83f5b309f | 268 | /** Initialisations |
SomeRandomBloke | 0:98d83f5b309f | 269 | * Hardware initialisations and any other setup needed |
SomeRandomBloke | 0:98d83f5b309f | 270 | */ |
SomeRandomBloke | 0:98d83f5b309f | 271 | void init() |
SomeRandomBloke | 0:98d83f5b309f | 272 | { |
SomeRandomBloke | 0:98d83f5b309f | 273 | LedSC = 0; |
SomeRandomBloke | 0:98d83f5b309f | 274 | SCButton.mode(PullUp); |
SomeRandomBloke | 0:98d83f5b309f | 275 | NVIC_SetPriority(SSP1_IRQn, 0x0); |
SomeRandomBloke | 0:98d83f5b309f | 276 | NVIC_SetPriority(PIN_INT0_IRQn, 0x1); |
SomeRandomBloke | 0:98d83f5b309f | 277 | |
SomeRandomBloke | 0:98d83f5b309f | 278 | // SysTick set to lower priority than Wi-Fi SPI bus interrupt |
SomeRandomBloke | 0:98d83f5b309f | 279 | NVIC_SetPriority(SysTick_IRQn, 0x2); |
SomeRandomBloke | 0:98d83f5b309f | 280 | |
SomeRandomBloke | 0:98d83f5b309f | 281 | // Enable RAM1 |
SomeRandomBloke | 0:98d83f5b309f | 282 | LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26); |
SomeRandomBloke | 0:98d83f5b309f | 283 | |
SomeRandomBloke | 0:98d83f5b309f | 284 | uart.baud(SERIAL_BAUD_RATE); |
SomeRandomBloke | 3:7410e3231e7a | 285 | |
SomeRandomBloke | 1:40027344b249 | 286 | strip.updatePins(); // Switch to Hardware SPI |
SomeRandomBloke | 0:98d83f5b309f | 287 | strip.begin(); |
SomeRandomBloke | 0:98d83f5b309f | 288 | |
SomeRandomBloke | 0:98d83f5b309f | 289 | // Update LED contents, to start they are all 'off' |
SomeRandomBloke | 0:98d83f5b309f | 290 | strip.show(); |
SomeRandomBloke | 0:98d83f5b309f | 291 | } |
SomeRandomBloke | 0:98d83f5b309f | 292 | |
SomeRandomBloke | 0:98d83f5b309f | 293 | /** Main loop, handle WiFi connection, check for button press to start SmartConfig process |
SomeRandomBloke | 0:98d83f5b309f | 294 | * |
SomeRandomBloke | 0:98d83f5b309f | 295 | */ |
SomeRandomBloke | 0:98d83f5b309f | 296 | int main( void ) |
SomeRandomBloke | 0:98d83f5b309f | 297 | { |
SomeRandomBloke | 0:98d83f5b309f | 298 | // Initalise the WiFi Module |
SomeRandomBloke | 0:98d83f5b309f | 299 | init(); |
SomeRandomBloke | 0:98d83f5b309f | 300 | |
SomeRandomBloke | 0:98d83f5b309f | 301 | uart.printf("WiFiDIPCortex Smartconfig Cheerlights\r\n"); |
SomeRandomBloke | 0:98d83f5b309f | 302 | wifi.start(0); |
SomeRandomBloke | 0:98d83f5b309f | 303 | |
SomeRandomBloke | 0:98d83f5b309f | 304 | // Check if button pressed during startup, if so then go into SmartConfig mode |
SomeRandomBloke | 0:98d83f5b309f | 305 | // otherwise just start wifi |
SomeRandomBloke | 0:98d83f5b309f | 306 | if(!SCButton) { |
SomeRandomBloke | 0:98d83f5b309f | 307 | uart.printf("Smartconfig button pressed\r\n"); |
SomeRandomBloke | 0:98d83f5b309f | 308 | |
SomeRandomBloke | 0:98d83f5b309f | 309 | //SmartConfig(); |
SomeRandomBloke | 0:98d83f5b309f | 310 | uart.printf("\r\nStarting Smart config, waiting for message from smartphone app ....\r\n"); |
SomeRandomBloke | 0:98d83f5b309f | 311 | LedSC = 1; |
SomeRandomBloke | 0:98d83f5b309f | 312 | // We dont want to auto reconnect to an access point |
SomeRandomBloke | 0:98d83f5b309f | 313 | wifi._wlan.ioctl_set_connection_policy(0, 0, 0); |
SomeRandomBloke | 0:98d83f5b309f | 314 | |
SomeRandomBloke | 0:98d83f5b309f | 315 | // start smart config will disconnect, set the prefix |
SomeRandomBloke | 0:98d83f5b309f | 316 | // wait for a message via a SmartConfig app, store it to the profile list |
SomeRandomBloke | 0:98d83f5b309f | 317 | // finally it will reenable auto connection, triggering the module to connect to the new access point |
SomeRandomBloke | 0:98d83f5b309f | 318 | wifi.start_smart_config(0); |
SomeRandomBloke | 0:98d83f5b309f | 319 | LedSC = 0; |
SomeRandomBloke | 0:98d83f5b309f | 320 | UsingSmartConfig = true; |
SomeRandomBloke | 0:98d83f5b309f | 321 | |
SomeRandomBloke | 0:98d83f5b309f | 322 | uart.printf("Back from SmartConfig\r\n"); |
SomeRandomBloke | 0:98d83f5b309f | 323 | |
SomeRandomBloke | 0:98d83f5b309f | 324 | wait(2); // for dhcp to configure |
SomeRandomBloke | 3:7410e3231e7a | 325 | // TODO: Add this into start_smart_config |
SomeRandomBloke | 0:98d83f5b309f | 326 | if ( wifi.is_dhcp_configured() ) { |
SomeRandomBloke | 0:98d83f5b309f | 327 | if (!Connected) { |
SomeRandomBloke | 0:98d83f5b309f | 328 | // We have just connected |
SomeRandomBloke | 0:98d83f5b309f | 329 | Connected = true; |
SomeRandomBloke | 3:7410e3231e7a | 330 | // This might need calling for mdsn advertiser to work. |
SomeRandomBloke | 3:7410e3231e7a | 331 | // uint32_t ip_addr[4] = {0,0,0,0}; |
SomeRandomBloke | 3:7410e3231e7a | 332 | // wifi._socket.gethostbyname((uint8_t *)_deviceName, strlen(_deviceName), uint32_t *ip_addr); |
SomeRandomBloke | 0:98d83f5b309f | 333 | |
SomeRandomBloke | 0:98d83f5b309f | 334 | // Start the mdns service, this tells any smart config apps listening we have succeeded |
SomeRandomBloke | 0:98d83f5b309f | 335 | wifi._socket.mdns_advertiser(1, (uint8_t *)_deviceName, strlen(_deviceName)); |
SomeRandomBloke | 0:98d83f5b309f | 336 | |
SomeRandomBloke | 0:98d83f5b309f | 337 | UsingSmartConfig = false; |
SomeRandomBloke | 0:98d83f5b309f | 338 | } |
SomeRandomBloke | 0:98d83f5b309f | 339 | } else { |
SomeRandomBloke | 0:98d83f5b309f | 340 | Connected = false; |
SomeRandomBloke | 0:98d83f5b309f | 341 | |
SomeRandomBloke | 0:98d83f5b309f | 342 | } |
SomeRandomBloke | 0:98d83f5b309f | 343 | } else { |
SomeRandomBloke | 0:98d83f5b309f | 344 | uart.printf("Normal startup\r\n"); |
SomeRandomBloke | 0:98d83f5b309f | 345 | } |
SomeRandomBloke | 0:98d83f5b309f | 346 | |
SomeRandomBloke | 0:98d83f5b309f | 347 | wait_ms(750); |
SomeRandomBloke | 0:98d83f5b309f | 348 | |
SomeRandomBloke | 0:98d83f5b309f | 349 | LedSC = 0; |
SomeRandomBloke | 0:98d83f5b309f | 350 | print_cc3000_info(); |
SomeRandomBloke | 3:7410e3231e7a | 351 | |
SomeRandomBloke | 3:7410e3231e7a | 352 | // Check if we're connected to WiFi and have an IP address, if not then just flash LED until reset |
SomeRandomBloke | 0:98d83f5b309f | 353 | uint32_t status = getWiFiStatus(); |
SomeRandomBloke | 0:98d83f5b309f | 354 | if( status != 3 || !wifi.is_dhcp_configured() ) { |
SomeRandomBloke | 0:98d83f5b309f | 355 | while( 1 ) { |
SomeRandomBloke | 0:98d83f5b309f | 356 | LedSC = !LedSC; |
SomeRandomBloke | 0:98d83f5b309f | 357 | wait_ms(500); |
SomeRandomBloke | 3:7410e3231e7a | 358 | } |
SomeRandomBloke | 0:98d83f5b309f | 359 | } |
SomeRandomBloke | 0:98d83f5b309f | 360 | |
SomeRandomBloke | 0:98d83f5b309f | 361 | while (1) { |
SomeRandomBloke | 3:7410e3231e7a | 362 | if( getWiFiStatus() == 0 ) { |
SomeRandomBloke | 3:7410e3231e7a | 363 | // Not connected, attempt reconnect |
SomeRandomBloke | 3:7410e3231e7a | 364 | ; |
SomeRandomBloke | 3:7410e3231e7a | 365 | } |
SomeRandomBloke | 0:98d83f5b309f | 366 | readCheerlight(); |
SomeRandomBloke | 0:98d83f5b309f | 367 | // Pause for a minute before checking again |
SomeRandomBloke | 0:98d83f5b309f | 368 | wait(60); |
SomeRandomBloke | 0:98d83f5b309f | 369 | } |
SomeRandomBloke | 0:98d83f5b309f | 370 | } |