Created
May 30, 2019 15:14
-
-
Save benevpi/7020e38c621d12f4aa315a9689ebd54c 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 <Wire.h> | |
#include <SPI.h> | |
#include <WiFi101.h> | |
#include <Adafruit_NeoPixel.h> | |
const char* ssid = "yourwifissid"; // The SSID (name) of your Wi-Fi network | |
const char* pass = "yourwifipassword"; // The password of the Wi-Fi network | |
String payload; // Variables to accept data | |
String nowday; | |
String nowmonth; | |
String nowyear; | |
String nowhour; | |
String nowmin; | |
String nowsec; | |
int d; // Variables to hold data as integers | |
int mo; | |
int y; | |
int h; | |
int mi; | |
int s; | |
unsigned long prevMillis = millis(); | |
unsigned long interval = 30000; | |
#define LED_PIN 6 | |
Adafruit_NeoPixel strip(104, 6, NEO_GRB + NEO_KHZ800); | |
int itis[] = {8,9,11,12}; | |
int five[] = {35,36,37,38}; | |
int ten[] = {4,5,6}; | |
int half[] = {0,1,2,3}; | |
int quarter[] = {13,14,15,16,17,18,19 }; | |
int twenty[] = {20,21,22,23,24,25}; | |
int minutes[] = {27,28,29,30,31,32,33}; | |
int past[] = {39, 40, 41, 42}; | |
int to[] = {43,44}; | |
int h_one[] = {46,47,48}; | |
int h_two[] = {49,50,51}; | |
int h_three[] = {60, 61, 62, 63, 64}; | |
int h_four[] = {59, 58, 57, 56}; | |
int h_five[] = {52, 53, 54, 55}; | |
int h_six[] = {65, 66, 67}; | |
int h_seven[] = {68, 69,70,71,72}; | |
int h_eight[] = {73, 74, 75, 76, 77}; | |
int h_nine[] = {87,88,89,90}; | |
int h_ten[] = {86,85,84}; | |
int h_eleven[]={83,82,81,80,79,78}; | |
int h_twelve[] = {91,92,93,94,95,96}; | |
int oclock[] = {103, 102, 101, 100, 99, 98}; | |
int hour_red = 150; | |
int hour_green = 0; | |
int hour_blue = 150; | |
int mins_red = 0; | |
int mins_green = 100; | |
int mins_blue = 100; | |
uint32_t magenta = strip.Color(150, 0, 150); | |
uint32_t yellow = strip.Color(100, 100, 0); | |
WiFiClient client; | |
char server[] = "api.timezonedb.com"; | |
int status = WL_IDLE_STATUS; | |
void setup() { | |
Serial.begin(115200); | |
if (WiFi.status() == WL_NO_SHIELD) { | |
Serial.println("WiFi shield not present"); | |
// don't continue: | |
while (true); | |
} | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
} | |
void loop() { | |
status = WiFi.begin(ssid, pass); | |
while (status != WL_CONNECTED) { | |
Serial.print("Attempting to connect to SSID: "); | |
Serial.println(ssid); | |
// Connect to WPA/WPA2 network. Change this line if using open or WEP network: | |
status = WiFi.begin(ssid, pass); | |
delay(10000); | |
} | |
Serial.println("Connected to wifi"); | |
printWiFiStatus(); | |
Serial.println("\nStarting connection to server..."); | |
// if you get a connection, report back via serial: | |
if (client.connect(server, 80)) { | |
Serial.println("connected to server"); | |
// Make a HTTP request: | |
client.println("GET /v2/get-time-zone?key=IAIFGXNLMT2Z&format=xml&fields=formatted&by=zone&zone=Europe/London HTTP/1.1"); | |
client.println("Host: api.timezonedb.com"); | |
client.println("Connection: close"); | |
client.println(); | |
} | |
delay(10000); | |
payload = ""; | |
Serial.println("stand by for data"); | |
while (client.available()) { | |
char c = client.read(); | |
Serial.write(c); | |
if (c == '\n') { | |
payload = ""; | |
} | |
payload += c; | |
if(payload.endsWith("</result>")) { | |
parse_response(); | |
} | |
} | |
makewords(); | |
strip.show(); | |
//shutdown the wifi connection | |
client.stop(); | |
WiFi.end(); | |
delay(60000); | |
} | |
void makewords() { | |
//blank strip | |
strip.fill(); | |
lightup(itis, 4, 100,100,0); | |
// what hour | |
int hour = h; | |
if (mi > 33) { | |
hour+=1; | |
} | |
if (hour > 12) { hour -= 12;} | |
if (hour==1) { lightup(h_one, 3, hour_red, hour_green, hour_blue); } | |
if (hour==2) { lightup(h_two, 3, hour_red, hour_green, hour_blue); } | |
if (hour==3) { lightup(h_three, 5, hour_red, hour_green, hour_blue); } | |
if (hour==4) { lightup(h_four, 4, hour_red, hour_green, hour_blue); } | |
if (hour==5) { lightup(h_five, 4, hour_red, hour_green, hour_blue); } | |
if (hour==6) { lightup(h_six, 3, hour_red, hour_green, hour_blue); } | |
if (hour==7) { lightup(h_seven, 5, hour_red, hour_green, hour_blue); } | |
if (hour==8) { lightup(h_eight, 5, hour_red, hour_green, hour_blue); } | |
if (hour==9) { lightup(h_nine, 4, hour_red, hour_green, hour_blue); } | |
if (hour==10) { lightup(h_ten, 3, hour_red, hour_green, hour_blue); } | |
if (hour==11) { lightup(h_eleven, 6, hour_red, hour_green, hour_blue); } | |
if (hour==12) { lightup(h_twelve, 6, hour_red, hour_green, hour_blue); } | |
//past or to? | |
if (mi > 3 && mi < 34) { lightup(past, 4,0, 150, 0); } | |
if (mi > 33 && mi < 58) {lightup(to,2,0,150,0);} | |
if (mi > 57 || mi < 4) {lightup(oclock,6,50, 50, 100);} | |
// minutes | |
if (mi > 3 && mi < 8) {lightup(five, 4, mins_red, mins_green, mins_blue); lightup(minutes, 7,mins_red,mins_green, mins_blue);} | |
if (mi > 7 && mi < 14) {lightup(ten, 3, mins_red, mins_green, mins_blue); lightup(minutes, 7,mins_red,mins_green, mins_blue);} | |
if (mi > 13 && mi < 18) { lightup(quarter, 7,mins_red, mins_green, mins_blue);} | |
if (mi > 17 && mi < 24) { lightup(twenty, 6,mins_red, mins_green, mins_blue); lightup(minutes, 7,mins_red,mins_green, mins_blue);} | |
if (mi > 23 && mi < 28) { lightup(twenty, 6,mins_red, mins_green, mins_blue); lightup(five, 4, mins_red, mins_green, mins_blue); lightup(minutes, 7,mins_red,mins_green, mins_blue);} | |
if (mi > 27 && mi < 34) { lightup(half, 4, mins_red, mins_green, mins_blue);} | |
if (mi > 53 && mi < 58) {lightup(five, 4, mins_red, mins_green, mins_blue); lightup(minutes, 7,mins_red,mins_green, mins_blue);} | |
if (mi > 47 && mi < 54) {lightup(ten, 3, mins_red, mins_green, mins_blue); lightup(minutes, 7,mins_red,mins_green, mins_blue);} | |
if (mi > 43 && mi < 48) { lightup(quarter, 7,mins_red, mins_green, mins_blue);} | |
if (mi > 37 && mi < 44) { lightup(twenty, 6,mins_red, mins_green, mins_blue); lightup(minutes, 7,mins_red,mins_green, mins_blue);} | |
if (mi > 33 && mi < 38) { lightup(twenty, 6,mins_red, mins_green, mins_blue); lightup(five, 4, mins_red, mins_green, mins_blue); lightup(minutes, 7,mins_red,mins_green, mins_blue);} | |
} | |
void lightup(int letters[], int letters_len, int red, int green, int blue) { | |
int arraylen = sizeof(letters); | |
for(int i = 0; i<letters_len; i++) { | |
strip.setPixelColor(letters[i], red, green, blue); | |
} | |
strip.show(); | |
} | |
//stolen from https://forum.arduino.cc/index.php?topic=527662.0 | |
void parse_response() { | |
int colon = payload.indexOf(':'); // Set the first colon in time as reference point | |
Serial.println(payload); | |
Serial.println("colon"); | |
Serial.println(colon); | |
nowday = payload.substring(colon - 5, colon - 3); // Get data as substring | |
Serial.println(nowday); | |
d = nowday.toInt(); // and convert to int | |
nowmonth = payload.substring(colon - 8, colon - 6); | |
mo = nowmonth.toInt(); | |
nowyear = payload.substring(colon - 13, colon - 9); | |
y = nowyear.toInt(); | |
nowhour = payload.substring(colon - 2, colon); | |
h = nowhour.toInt(); | |
nowmin = payload.substring(colon + 1, colon + 3); | |
mi = nowmin.toInt(); | |
nowsec = payload.substring(colon + 4, colon + 6); | |
s = nowsec.toInt(); | |
// Print integers without leading zeros - use in calculations | |
Serial.print("Today's date is: "); | |
Serial.print(y); | |
Serial.print("-"); | |
Serial.print(mo); | |
Serial.print("-"); | |
Serial.println(d); | |
Serial.print("Current local time is: "); | |
Serial.print(h); | |
Serial.print(":"); | |
Serial.print(mi); | |
Serial.print(":"); | |
Serial.println(s); | |
Serial.println(); | |
} | |
///from arduino wifi sketch | |
void printWiFiStatus() { | |
// print the SSID of the network you're attached to: | |
Serial.print("SSID: "); | |
Serial.println(WiFi.SSID()); | |
Serial.println("status"); | |
Serial.println(status); | |
// print your WiFi shield's IP address: | |
IPAddress ip = WiFi.localIP(); | |
Serial.print("IP Address: "); | |
Serial.println(ip); | |
// print the received signal strength: | |
long rssi = WiFi.RSSI(); | |
Serial.print("signal strength (RSSI):"); | |
Serial.print(rssi); | |
Serial.println(" dBm"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment