Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h>
- #include <ESP8266WiFi.h>
- #define MAX_BRIGHTNESS 48
- #define MIN_BRIGHTNESS 8
- // WiFi username and password
- const char* ssid = "Bluffs_Resident";
- const char* password = "bluffsresident";
- const char* host = "time.nist.gov";
- const int port = 13;
- const int numLeds = 64;
- const int pin = D6;
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLeds, pin, NEO_GRB + NEO_KHZ800);
- WiFiClient client;
- void setup() {
- Serial.begin(115200);
- strip.begin();
- int i;
- // initialize matrix
- for (i = 0; i < numLeds; i++)
- {
- strip.setPixelColor(i, 0, 0, 0);
- }
- strip.show();
- strip.setBrightness(MIN_BRIGHTNESS);
- WiFi.begin(ssid, password);
- Serial.println("Wifi started");
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- Serial.println(WiFi.localIP());
- Serial.println(WiFi.macAddress());
- }
- void loop() {
- int i;
- if (!client.connect(host, port))
- {
- Serial.println("Failed to connect to host");
- }
- // API call to get datetime
- client.print("HEAD / HTTP/1.1\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; ESP8266 NodeMcu Lua;)\r\n\r\n");
- delay(100);
- // read all the lines of the reply from server and print them to serial
- // expected line is like : Date: Thu, 01 Jan 2015 22:00:14 GMT
- while (client.available())
- {
- String line = client.readStringUntil('\r');
- Serial.println("RESPONSE: ");
- Serial.println(line);
- if (line.indexOf("Date") != -1)
- {
- Serial.println("Error: Malformed resonse");
- }
- // poorly parse the response into hours and minutes
- // if the response format ever changes, this will break horribly
- else
- {
- String buf = line.substring(16, 18);
- Serial.print("HOUR: ");
- Serial.println(buf);
- int timeHour = buf.toInt();
- buf = line.substring(19, 21);
- Serial.print("MINUTE: ");
- Serial.println(buf);
- int timeMinute = buf.toInt();
- int i;
- // every minute, light up the next LED and increase brightness
- if (timeHour == 10) // 5am
- {
- for (i = 0; i < map(timeMinute, 0, 60, 0, numLeds); i++)
- {
- strip.setPixelColor(i, 255, 100, 0);
- }
- strip.setBrightness(map(timeMinute, 0, 60, MIN_BRIGHTNESS,
- MAX_BRIGHTNESS));
- strip.show();
- }
- // reset LEDs
- else if (timeHour > 13) // > 8am
- {
- for (i = 0; i < numLeds; i++)
- {
- strip.setPixelColor(i, 0, 0, 0);
- }
- strip.setBrightness(MIN_BRIGHTNESS);
- strip.show();
- }
- }
- }
- // only send API request a max of once a minute
- delay(60000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement