Advertisement
CyanBlob

Untitled

Nov 2nd, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2.  
  3. #include <ESP8266WiFi.h>
  4.  
  5. #define MAX_BRIGHTNESS 48
  6. #define MIN_BRIGHTNESS 8
  7.  
  8. // WiFi username and password
  9. const char* ssid     = "Bluffs_Resident";
  10. const char* password = "bluffsresident";
  11.  
  12. const char* host = "time.nist.gov";
  13. const int port = 13;
  14.  
  15. const int numLeds = 64;
  16. const int pin = D6;
  17.  
  18. Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLeds, pin, NEO_GRB + NEO_KHZ800);
  19.  
  20. WiFiClient client;
  21.  
  22. void setup() {
  23.  
  24.     Serial.begin(115200);
  25.  
  26.     strip.begin();
  27.  
  28.     int i;
  29.  
  30.     // initialize matrix
  31.     for (i = 0; i < numLeds; i++)
  32.     {
  33.         strip.setPixelColor(i, 0, 0, 0);
  34.     }
  35.  
  36.     strip.show();
  37.     strip.setBrightness(MIN_BRIGHTNESS);
  38.  
  39.     WiFi.begin(ssid, password);
  40.  
  41.     Serial.println("Wifi started");
  42.  
  43.     while (WiFi.status() != WL_CONNECTED)
  44.     {
  45.         delay(500);
  46.         Serial.print(".");
  47.     }
  48.     Serial.println(WiFi.localIP());
  49.     Serial.println(WiFi.macAddress());
  50.  
  51. }
  52.  
  53. void loop() {
  54.     int i;
  55.  
  56.     if (!client.connect(host, port))
  57.     {
  58.         Serial.println("Failed to connect to host");
  59.     }
  60.  
  61.     // API call to get datetime
  62.     client.print("HEAD / HTTP/1.1\r\nAccept: */*\r\nUser-Agent: Mozilla/4.0 (compatible; ESP8266 NodeMcu Lua;)\r\n\r\n");
  63.  
  64.     delay(100);
  65.  
  66.     // read all the lines of the reply from server and print them to serial
  67.     // expected line is like : Date: Thu, 01 Jan 2015 22:00:14 GMT
  68.     while (client.available())
  69.     {
  70.         String line = client.readStringUntil('\r');
  71.         Serial.println("RESPONSE: ");
  72.         Serial.println(line);
  73.  
  74.         if (line.indexOf("Date") != -1)
  75.         {
  76.             Serial.println("Error: Malformed resonse");
  77.         }
  78.         // poorly parse the response into hours and minutes
  79.         // if the response format ever changes, this will break horribly
  80.         else
  81.         {
  82.             String buf = line.substring(16, 18);
  83.             Serial.print("HOUR: ");
  84.             Serial.println(buf);
  85.  
  86.             int timeHour = buf.toInt();
  87.  
  88.             buf = line.substring(19, 21);
  89.             Serial.print("MINUTE: ");
  90.             Serial.println(buf);
  91.  
  92.             int timeMinute = buf.toInt();
  93.  
  94.             int i;
  95.  
  96.             // every minute, light up the next LED and increase brightness
  97.             if (timeHour == 10) // 5am
  98.             {
  99.                 for (i = 0; i < map(timeMinute, 0, 60, 0, numLeds); i++)
  100.                 {
  101.                     strip.setPixelColor(i, 255, 100, 0);
  102.                 }
  103.                 strip.setBrightness(map(timeMinute, 0, 60, MIN_BRIGHTNESS,
  104.                             MAX_BRIGHTNESS));
  105.                 strip.show();
  106.             }
  107.             // reset LEDs
  108.             else if (timeHour > 13) // > 8am
  109.             {
  110.                 for (i = 0; i < numLeds; i++)
  111.                 {
  112.                     strip.setPixelColor(i, 0, 0, 0);
  113.                 }
  114.                 strip.setBrightness(MIN_BRIGHTNESS);
  115.                 strip.show();
  116.             }
  117.         }
  118.     }
  119.     // only send API request a max of once a minute
  120.     delay(60000);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement