Cheerlights client using WiFiDIPCortex and WS2801 RGB LED strip
Dependencies: Adafruit_WS2801 HTTPClient cc3000_hostdriver_mbedsocket mbed
Diff: main.cpp
- Revision:
- 3:7410e3231e7a
- Parent:
- 1:40027344b249
diff -r 8de3eccd527e -r 7410e3231e7a main.cpp --- a/main.cpp Sat Feb 22 13:25:44 2014 +0000 +++ b/main.cpp Wed Nov 26 14:02:36 2014 +0000 @@ -68,10 +68,13 @@ // Library to drive the LED strip #include "Adafruit_WS2801.h" -// Some local defines +// Some local defines #define SERIAL_BAUD_RATE 115200 +#define NUM_PIXELS 50 +#define NUM_COL_HISTORY 5 + #define WIGO 1 #define WIFI_DIPCORTEX 2 #define UNDEFINED 3 @@ -80,9 +83,9 @@ using namespace mbed_cc3000; -// LED to indicate smart config is running +// LED to indicate SmartConfig is running DigitalOut LedSC(P0_1); -// +// Button to hold down during reset to enter SmartConfig mode DigitalIn SCButton(P1_31); // For Bit-Banging SPI use Pins 40 and 39 @@ -103,9 +106,9 @@ #endif #ifndef CC3000_UNENCRYPTED_SMART_CONFIG -const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; +//const uint8_t smartconfigkey[] = {0x73,0x6d,0x61,0x72,0x74,0x63,0x6f,0x6e,0x66,0x69,0x67,0x41,0x45,0x53,0x31,0x36}; #else -const uint8_t smartconfigkey = 0; +//const uint8_t smartconfigkey = 0; #endif tNetappIpconfigRetArgs ipinfo; @@ -116,14 +119,12 @@ char _deviceName[] = "CC3000"; HTTPClient http; -//char str[128]; -uint32_t lastColour = 0; // Set the first variable to the number of rows, the second to number of pixels. 32 = 32 pixels in a row -Adafruit_WS2801 strip = Adafruit_WS2801(32, dataPin, clockPin); +Adafruit_WS2801 strip = Adafruit_WS2801(NUM_PIXELS, dataPin, clockPin); // Setup the colour table and mappings -#define NUM_COLOURS 12 +#define NUM_COLOURS 13 struct ColourTable { char name[12]; uint32_t value; @@ -139,11 +140,10 @@ { "yellow", 0xffff00 }, { "orange", 0xffa500 }, { "pink", 0xff69b4 }, - { "oldlace", 0xfd5e56 } + { "oldlace", 0xfd5e56 }, + { "black", 0x000000 } }; - - /** Get status of WiFi connection * displays and returns value */ @@ -201,25 +201,24 @@ } -/** Convert name to colour +/** Convert name to colour and set it on LEDs. With colour history, strip will be + * changing colour for every new colour detected producing an almost random effect + * @param colNum Number of the received colour, starts 0 to NUM_COL_HISTORY with highest being latest colour * @param colStr Received colour name */ -void setColour( char *colStr ) +void setColour( int colNum, char *colStr ) { // uart.printf("received %s\r\n",colStr); for( int i=0; i < NUM_COLOURS; i++ ) { if( strncmp( colTable[i].name, colStr, strlen(colTable[i].name) ) == 0 ) { - if( colTable[i].value != lastColour ) { - for (int n=0; n < strip.numPixels(); n++) { - strip.setPixelColor(n, colTable[i].value); - strip.show(); - wait_ms(100); - } - lastColour = colTable[i].value; - } else { - uart.printf("Same as previous colour\r\n"); + for (int n=colNum; n < strip.numPixels()+NUM_COL_HISTORY; n += NUM_COL_HISTORY) { + strip.setPixelColor(n, colTable[i].value); } + // Update strip after colour has been set + strip.show(); + // Slight pause + wait_ms(250); return; } } @@ -227,25 +226,42 @@ } +#define CL_BUFFSIZE 1024 /** Read Cheerlights colour * Use http call to get last Cheerlights colour */ void readCheerlight( void ) { - char str[128]; + char responseStr[CL_BUFFSIZE]; //GET data uart.printf("\r\nTrying to fetch page...\r\n"); - int ret = http.get("http://api.thingspeak.com/channels/1417/field/1/last.txt", str, 128); - // Just a local test url, saves having to tweet the cheerlights colour every time -// int ret = http.get("http://192.168.1.3/last.txt", str, 128); + int ret = http.get("http://api.thingspeak.com/channels/1417/feed.csv?results=5", responseStr, CL_BUFFSIZE); if (!ret) { - uart.printf("Page fetched successfully - read %d characters\r\n", strlen(str)); - uart.printf("Result: %s\r\n", str); - setColour( str ); + uart.printf("Page fetched successfully - read %d characters\r\n", strlen(responseStr)); + //uart.printf("Result: %s\r\n", responseStr); + // Parse CSV and set colours + // Get a line + char *ptr = responseStr; + // Skip header line + while(*ptr++ != 0x0a ); // Header + // 5 colours + for( int i=0; i<5; i++ ) { + while(*ptr++ != ','); // Column 1 - Date, Skip + while(*ptr++ != ','); // Column 2 - ID, Skip + char col[10]; + char *cPtr = col; + while( *ptr != 0x0a ) { // Column 3 - colour + *cPtr++ = *ptr++; + } + *cPtr = '\0'; + uart.printf("%d %s\r\n",i,col); + setColour( i, col ); + } } else { uart.printf("Error - ret = %d - HTTP return code = %d\r\n", ret, http.getHTTPResponseCode()); } + uart.printf("End of readCheerlights\n\r"); } @@ -266,9 +282,8 @@ LPC_SYSCON->SYSAHBCLKCTRL |= (0x1 << 26); uart.baud(SERIAL_BAUD_RATE); - + strip.updatePins(); // Switch to Hardware SPI - strip.begin(); // Update LED contents, to start they are all 'off' @@ -307,11 +322,14 @@ uart.printf("Back from SmartConfig\r\n"); wait(2); // for dhcp to configure - +// TODO: Add this into start_smart_config if ( wifi.is_dhcp_configured() ) { if (!Connected) { // We have just connected Connected = true; + // This might need calling for mdsn advertiser to work. + // uint32_t ip_addr[4] = {0,0,0,0}; + // wifi._socket.gethostbyname((uint8_t *)_deviceName, strlen(_deviceName), uint32_t *ip_addr); // Start the mdns service, this tells any smart config apps listening we have succeeded wifi._socket.mdns_advertiser(1, (uint8_t *)_deviceName, strlen(_deviceName)); @@ -330,18 +348,21 @@ LedSC = 0; print_cc3000_info(); - - // Check if we're connected to WiFi and have an IP address, if not then just flash LED + + // Check if we're connected to WiFi and have an IP address, if not then just flash LED until reset uint32_t status = getWiFiStatus(); if( status != 3 || !wifi.is_dhcp_configured() ) { while( 1 ) { LedSC = !LedSC; wait_ms(500); - } + } } while (1) { - getWiFiStatus(); + if( getWiFiStatus() == 0 ) { + // Not connected, attempt reconnect + ; + } readCheerlight(); // Pause for a minute before checking again wait(60);