#include #include #include #include #include #include #include #include #include #include #include #include "tetris.h" #include "secrets.h" #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define OLED_RESET -1 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); ESP8266WebServer server(80); //Server on port 80 WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 10800); char daysOfTheWeek[7][12] = { "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; int days[7]; int alarm; int counter = 0; bool reading_light = false; int RGB[4] = {0,0,0,0}; unsigned long previousMillis = 0; int day_prev = -1; bool alarm_ran = false; // ESP8266 Network Variables IPAddress staticIP(192, 168, 1, 4); IPAddress gateway(192, 168, 1, 1); IPAddress subnet(255, 255, 255, 0); IPAddress dns(8, 8, 8, 8); const char* deviceName = "yeetclock.xyz"; void setup() { // Set Pins pinMode(14,OUTPUT); pinMode(13,OUTPUT); digitalWrite(13,LOW); pinMode(15,OUTPUT); pinMode(0,OUTPUT); pinMode(2,OUTPUT); // Start WiFi WiFi.hostname(deviceName); WiFi.config(staticIP, subnet, gateway, dns); WiFi.begin(ssid, password); Serial.begin(9600); // Start serial connection (Baudrate 9600) while (WiFi.status() != WL_CONNECTED) { // Halt the program until WiFi connects delay(500); Serial.println("Waiting to connect…"); } Serial.print("IP address: "); // Print IP Serial.println(WiFi.localIP()); // Server settings server.on("/setcolor", setColor); server.on("/getcolor", getColor); server.on("/setalarm", setAlarm); server.on("/getalarm", getAlarm); server.on("/getreading", getReadingLight); server.on("/togglereading", toggleReadingLight); server.on("/geteeprom", geteeprom); server.on("/tone", playTone); server.begin(); // Server begin timeClient.begin(); // Start UDP time client EEPROM.begin(16); // Start EEPROM with 16 bytes of space alarm = read_alarm_time(); // Save alarm days & time to memory from EEPROM read_alarm_date(days); Serial.println("test"); // ESP8266 gives errors without this line idk why if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Test i2c connection Serial.println(F("SSD1306 allocation failed")); } timeClient.update(); // Sync with time server Serial.println("test2"); // Again, gives errors idk why } int bits_to_int(int bits[64]){ // Converts a binary integer to decimal int (Used to store the days that alarm is enabled) int num; for(int i=0;i<64;i++){ num += pow(2,i)*bits[i]; } return num; } int read_alarm_time(){ // Reads alarm time from memory (ESP8266 gives errors without the debug lines) int h = EEPROM.read(0); // Get h,m,s from eeprom int m = EEPROM.read(5); int s = EEPROM.read(7); int alarm = 0; Serial.println("\naaa"); Serial.println(h); Serial.println("bbb"); Serial.println(m); Serial.println("ccc"); Serial.println(s); alarm += h*3600; alarm += m*60; alarm += s; return alarm; } void read_alarm_date(int days[]){ int day_int; day_int = EEPROM.read(10); decimal_to_binary(day_int,days); } void decimal_to_binary(int decimal, int binary[]){ int result = decimal; Serial.print("Date decimal: "); Serial.println(decimal); int binary_length = 0; for(int i=0; result>0; i++) { binary_length += 1; binary[i]=result%2; result= result/2; } while(binary_length<7){ binary[binary_length] = 0; binary_length += 1; } } void playTone(){ double duration; double freq; for (int i = 0; i < server.args(); i++) { if(server.argName(i) == "duration"){ duration=server.arg(i).toDouble(); } if(server.argName(i) == "freq"){ freq=server.arg(i).toDouble(); } } tone(15,freq,duration*1000); server.send(200, "text/plain", "OK"); } void setColor(){ bool ON; for (int i = 0; i < server.args(); i++) { switch(server.argName(i).charAt(0)){ case 'R': analogWrite(0,server.arg(i).toInt()); RGB[0] = server.arg(i).toInt(); case 'G': analogWrite(2,server.arg(i).toInt()); RGB[1] = server.arg(i).toInt(); case 'B': analogWrite(14,server.arg(i).toInt()); RGB[2] = server.arg(i).toInt(); case 'O': ON = server.arg(i).toInt(); RGB[3] = server.arg(i).toInt(); } } if(!ON){ Serial.println("OFF"); analogWrite(14,0); analogWrite(0,0); analogWrite(2,0); } server.send(200, "text/plain", "OK"); //Response to the HTTP request } void print_arr(int arr[],int size, String name){ Serial.println("SHOWING ARRAY " + name); for(int i=0;i= 1000) { int day = timeClient.getDay(); if(day!=day_prev){ day_prev = day; alarm_ran=false; } previousMillis = currentMillis; if(counter==300){ counter=0; Serial.println("test3"); timeClient.update(); Serial.println("test4"); } show_time(timeClient.getFormattedTime(),daysOfTheWeek[timeClient.getDay()]); int time = timeClient.getHours()*3600 + timeClient.getMinutes()*60 + timeClient.getSeconds(); Serial.println("TIME"); Serial.println(time); Serial.println(alarm); Serial.println(timeClient.getDay()); if (time > alarm && days[timeClient.getDay()] && !alarm_ran){ alarm_ran = true; analogWrite(0,255); analogWrite(2,255); analogWrite(14,255); Serial.println("ALARM"); playTetris(); analogWrite(0,0); analogWrite(2,0); analogWrite(14,0); } counter += 1; } server.handleClient(); }