Last active
December 8, 2016 20:24
-
-
Save jasoncoon/5e066ab1c7906f6f58d52d5eddf7594e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FastLED.h> | |
FASTLED_USING_NAMESPACE | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <ArduinoJson.h> | |
#define DATA_PIN D7 | |
#define LED_TYPE WS2812B | |
#define COLOR_ORDER GRB | |
#define NUM_LEDS 24 | |
#define BRIGHTNESS 8 | |
#define MILLI_AMPS 1000 // IMPORTANT: set the max milli-Amps of your power supply (4A = 4000mA) | |
#define FRAMES_PER_SECOND 240 // here you can control the speed. With the Access Point / Web Server the animations run a bit slower. | |
CRGB leds[NUM_LEDS]; | |
// WiFi network parameters | |
const char* ssid = "<your SSID>"; | |
const char* password = "<your passphrase>"; | |
int maxVotes = 538; | |
int dElectoral = 0; | |
int rElectoral = 0; | |
uint8_t dElectoralCount = 0; | |
uint8_t rElectoralCount = 0; | |
bool firstRequest = true; | |
void setup() { | |
Serial.begin(115200); | |
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS); // for WS2812 (Neopixel) | |
//FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS); // for APA102 (Dotstar) | |
FastLED.setDither(false); | |
FastLED.setCorrection(TypicalLEDStrip); | |
FastLED.setBrightness(BRIGHTNESS); | |
FastLED.setMaxPowerInVoltsAndMilliamps(5, MILLI_AMPS); | |
fill_solid(leds, NUM_LEDS, CRGB::Black); | |
FastLED.show(); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(100); | |
} | |
} | |
void parsePayload(String payload) { | |
DynamicJsonBuffer jsonbuffer; | |
JsonObject& root = jsonbuffer.parseObject(payload); | |
if (root.success()) { | |
maxVotes = root["maxVotes"]; | |
dElectoral = root["dElectoral"]; | |
rElectoral = root["rElectoral"]; | |
Serial.print("maxVotes: "); | |
Serial.println(maxVotes); | |
Serial.print("dElectoral: "); | |
Serial.println(dElectoral); | |
Serial.print("rElectoral: "); | |
Serial.println(rElectoral); | |
if(maxVotes > 0) { | |
dElectoralCount = map(dElectoral, 0, maxVotes, 0, NUM_LEDS - 1); | |
rElectoralCount = map(rElectoral, 0, maxVotes, 0, NUM_LEDS - 1); | |
Serial.print("dElectoralCount: "); | |
Serial.println(dElectoralCount); | |
Serial.print("rElectoralCount: "); | |
Serial.println(rElectoralCount); | |
} | |
} | |
} | |
void loop() { | |
if(firstRequest) | |
requestElectionData(); | |
firstRequest = false; | |
EVERY_N_SECONDS(10) { | |
requestElectionData(); | |
} | |
election(); | |
FastLED.show(); | |
// insert a delay to keep the framerate modest | |
FastLED.delay(1000 / FRAMES_PER_SECOND); | |
} | |
void requestElectionData() { | |
if (WiFi.status() == WL_CONNECTED) { | |
HTTPClient http; | |
http.begin("http://35.162.231.64/election2016.json"); | |
int httpCode = http.GET(); | |
if (httpCode > 0) { | |
if (httpCode == HTTP_CODE_OK) { | |
String payload = http.getString(); | |
parsePayload(payload); | |
} | |
} | |
http.end(); | |
} | |
} | |
void election() { | |
fill_solid(leds, NUM_LEDS, CRGB::Black); | |
uint8_t doffset = beatsin8(10, 0, (NUM_LEDS - 1) - dElectoralCount); | |
uint8_t roffset = beatsin8(13, 0, (NUM_LEDS - 1) - rElectoralCount); | |
for(uint8_t i = doffset; i <= doffset + dElectoralCount; i++) { | |
leds[i] = CRGB::Blue; | |
} | |
for(uint8_t i = roffset; i <= roffset + rElectoralCount; i++) { | |
leds[i] += CRGB::Red; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment