Last active
May 8, 2016 18:14
-
-
Save Jotschi/40934ae480c63c1aeef9c65360fcbd40 to your computer and use it in GitHub Desktop.
This file contains 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 <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
const char* ssid = "1234"; | |
const char* password = ""; | |
#define GREEN 12 | |
#define RED 13 | |
#define BLUE 15 | |
const int httpPort = 80; | |
HTTPClient http; | |
void initWLAN() { | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println(WiFi.localIP()); | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
analogWriteFreq(500); | |
analogWriteRange(255); | |
pinMode(RED, OUTPUT); | |
pinMode(GREEN, OUTPUT); | |
pinMode(BLUE, OUTPUT); | |
digitalWrite(RED, 0); | |
digitalWrite(GREEN, 0); | |
digitalWrite(BLUE, 0); | |
initWLAN(); | |
} | |
String cmd; | |
int targetRed = 0; | |
int targetBlue = 0; | |
int targetGreen = 0; | |
int fromRed = 0; | |
int fromBlue = 0; | |
int fromGreen = 0; | |
int toRed = 0; | |
int toBlue = 0; | |
int toGreen = 0; | |
int red = 0; | |
int blue = 0; | |
int green = 0; | |
void execute(void) { | |
if (cmd == "fadeTo" || cmd == "fadeBetween") { | |
if (red < targetRed) { | |
red++; | |
} | |
if (red > targetRed) { | |
red--; | |
} | |
if (green < targetGreen) { | |
green++; | |
} | |
if (green > targetGreen) { | |
green--; | |
} | |
if (blue < targetBlue) { | |
blue++; | |
} | |
if (blue > targetBlue) { | |
blue--; | |
} | |
} | |
if (cmd == "set") { | |
red = targetRed; | |
blue = targetBlue; | |
green = targetGreen; | |
} | |
if (red < 0) { | |
red = 0; | |
} | |
if (blue < 0) { | |
blue = 0; | |
} | |
if (green < 0) { | |
green = 0; | |
} | |
analogWrite(RED, red); | |
analogWrite(BLUE, blue); | |
analogWrite(GREEN, green); | |
} | |
void parseCommand(String response) { | |
int cstart= response.indexOf("cmd\":\"") + 6; | |
int cend = response.indexOf("\"", cstart); | |
cmd = response.substring(cstart, cend); | |
targetBlue = parseJSONField(response, "blue"); | |
targetRed = parseJSONField(response, "red"); | |
targetGreen = parseJSONField(response, "green"); | |
fromBlue = parseJSONField(response, "fromBlue"); | |
fromRed = parseJSONField(response, "fromRed"); | |
fromGreen = parseJSONField(response, "fromGreen"); | |
toBlue = parseJSONField(response, "toBlue"); | |
toRed = parseJSONField(response, "toRed"); | |
toGreen = parseJSONField(response, "toGreen"); | |
} | |
int parseJSONField(String response, String name) { | |
int start= response.indexOf(name + "\":") + name.length() + 2; | |
int end = response.indexOf("}", start); | |
String value = response.substring(start, end); | |
return value.toInt(); | |
} | |
void pullCommand() { | |
// Serial.print("[HTTP] begin...\n"); | |
http.begin("http://dweet.io/get/latest/dweet/for/aboard-curve"); | |
// Serial.print("[HTTP] GET...\n"); | |
int httpCode = http.GET(); | |
if (httpCode > 0) { | |
// Serial.printf("[HTTP] GET... code: %d\n", httpCode); | |
if (httpCode == HTTP_CODE_OK) { | |
String response = http.getString(); | |
parseCommand(response); | |
//Serial.println(response); | |
} | |
} else { | |
// Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
} | |
int fadeState = HIGH; | |
void loop() { | |
pullCommand(); | |
if (cmd == "fadeBetween") { | |
if (fadeState == LOW) { | |
targetRed = fromRed; | |
targetBlue = fromBlue; | |
targetGreen = fromGreen; | |
fadeState = HIGH; | |
} else { | |
targetRed = toRed; | |
targetBlue = toBlue; | |
targetGreen = toGreen; | |
fadeState = LOW; | |
} | |
} | |
if(true) { | |
Serial.print("Command: "); | |
Serial.println(cmd); | |
Serial.println("------------"); | |
Serial.print("red: "); | |
Serial.println(red); | |
Serial.print("blue: "); | |
Serial.println(blue); | |
Serial.print("green: "); | |
Serial.println(green); | |
Serial.println("------------"); | |
Serial.print("targetRed: "); | |
Serial.println(targetRed); | |
Serial.print("targetBlue: "); | |
Serial.println(targetBlue); | |
Serial.print("targetGreen: "); | |
Serial.println(targetGreen); | |
Serial.println("------------"); | |
Serial.print("toRed: "); | |
Serial.println(toRed); | |
Serial.print("toBlue: "); | |
Serial.println(toBlue); | |
Serial.print("toGreen: "); | |
Serial.println(toGreen); | |
Serial.println("------------"); | |
Serial.print("fromRed: "); | |
Serial.println(fromRed); | |
Serial.print("fromBlue: "); | |
Serial.println(fromBlue); | |
Serial.print("fromGreen: "); | |
Serial.println(fromGreen); | |
} | |
for(int i=0;i<255;i++) { | |
execute(); | |
delay(10); | |
} | |
delay(100); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment