You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
7.8 KiB

5 years ago
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <EEPROM.h>
  5. #include <ArduinoJson.h>
  6. #include <NTPClient.h>
  7. #include <WiFiUdp.h>
  8. #include <SPI.h>
  9. #include <Wire.h>
  10. #include <Adafruit_GFX.h>
  11. #include <Adafruit_SSD1306.h>
  12. #include "tetris.h"
  13. #include "secrets.h"
  14. #define SCREEN_WIDTH 128
  15. #define SCREEN_HEIGHT 64
  16. #define OLED_RESET -1
  17. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  18. ESP8266WebServer server(80); //Server on port 80
  19. WiFiUDP ntpUDP;
  20. NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 10800);
  21. char daysOfTheWeek[7][12] = { "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  22. int days[7];
  23. int alarm;
  24. int counter = 0;
  25. bool reading_light = false;
  26. int RGB[4] = {0,0,0,0};
  27. unsigned long previousMillis = 0;
  28. int day_prev = -1;
  29. bool alarm_ran = false;
  30. // ESP8266 Network Variables
  31. IPAddress staticIP(192, 168, 1, 4);
  32. IPAddress gateway(192, 168, 1, 1);
  33. IPAddress subnet(255, 255, 255, 0);
  34. IPAddress dns(8, 8, 8, 8);
  35. const char* deviceName = "yeetclock.xyz";
  36. void setup() {
  37. // Set Pins
  38. pinMode(14,OUTPUT);
  39. pinMode(13,OUTPUT);
  40. digitalWrite(13,LOW);
  41. pinMode(15,OUTPUT);
  42. pinMode(0,OUTPUT);
  43. pinMode(2,OUTPUT);
  44. // Start WiFi
  45. WiFi.hostname(deviceName);
  46. WiFi.config(staticIP, subnet, gateway, dns);
  47. WiFi.begin(ssid, password);
  48. Serial.begin(9600); // Start serial connection (Baudrate 9600)
  49. while (WiFi.status() != WL_CONNECTED) { // Halt the program until WiFi connects
  50. delay(500);
  51. Serial.println("Waiting to connect…");
  52. }
  53. Serial.print("IP address: "); // Print IP
  54. Serial.println(WiFi.localIP());
  55. // Server settings
  56. server.on("/setcolor", setColor);
  57. server.on("/getcolor", getColor);
  58. server.on("/setalarm", setAlarm);
  59. server.on("/getalarm", getAlarm);
  60. server.on("/getreading", getReadingLight);
  61. server.on("/togglereading", toggleReadingLight);
  62. server.on("/geteeprom", geteeprom);
  63. server.on("/tone", playTone);
  64. server.begin(); // Server begin
  65. timeClient.begin(); // Start UDP time client
  66. EEPROM.begin(16); // Start EEPROM with 16 bytes of space
  67. alarm = read_alarm_time(); // Save alarm days & time to memory from EEPROM
  68. read_alarm_date(days);
  69. Serial.println("test"); // ESP8266 gives errors without this line idk why
  70. if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Test i2c connection
  71. Serial.println(F("SSD1306 allocation failed"));
  72. }
  73. timeClient.update(); // Sync with time server
  74. Serial.println("test2"); // Again, gives errors idk why
  75. }
  76. int bits_to_int(int bits[64]){ // Converts a binary integer to decimal int (Used to store the days that alarm is enabled)
  77. int num;
  78. for(int i=0;i<64;i++){
  79. num += pow(2,i)*bits[i];
  80. }
  81. return num;
  82. }
  83. int read_alarm_time(){ // Reads alarm time from memory (ESP8266 gives errors without the debug lines)
  84. int h = EEPROM.read(0); // Get h,m,s from eeprom
  85. int m = EEPROM.read(5);
  86. int s = EEPROM.read(7);
  87. int alarm = 0;
  88. Serial.println("\naaa");
  89. Serial.println(h);
  90. Serial.println("bbb");
  91. Serial.println(m);
  92. Serial.println("ccc");
  93. Serial.println(s);
  94. alarm += h*3600;
  95. alarm += m*60;
  96. alarm += s;
  97. return alarm;
  98. }
  99. void read_alarm_date(int days[]){
  100. int day_int;
  101. day_int = EEPROM.read(10);
  102. decimal_to_binary(day_int,days);
  103. }
  104. void decimal_to_binary(int decimal, int binary[]){
  105. int result = decimal;
  106. Serial.print("Date decimal: ");
  107. Serial.println(decimal);
  108. int binary_length = 0;
  109. for(int i=0; result>0; i++) {
  110. binary_length += 1;
  111. binary[i]=result%2;
  112. result= result/2;
  113. }
  114. while(binary_length<7){
  115. binary[binary_length] = 0;
  116. binary_length += 1;
  117. }
  118. }
  119. void playTone(){
  120. double duration;
  121. double freq;
  122. for (int i = 0; i < server.args(); i++) {
  123. if(server.argName(i) == "duration"){
  124. duration=server.arg(i).toDouble();
  125. }
  126. if(server.argName(i) == "freq"){
  127. freq=server.arg(i).toDouble();
  128. }
  129. }
  130. tone(15,freq,duration*1000);
  131. server.send(200, "text/plain", "OK");
  132. }
  133. void setColor(){
  134. bool ON;
  135. for (int i = 0; i < server.args(); i++) {
  136. switch(server.argName(i).charAt(0)){
  137. case 'R':
  138. analogWrite(0,server.arg(i).toInt());
  139. RGB[0] = server.arg(i).toInt();
  140. case 'G':
  141. analogWrite(2,server.arg(i).toInt());
  142. RGB[1] = server.arg(i).toInt();
  143. case 'B':
  144. analogWrite(14,server.arg(i).toInt());
  145. RGB[2] = server.arg(i).toInt();
  146. case 'O':
  147. ON = server.arg(i).toInt();
  148. RGB[3] = server.arg(i).toInt();
  149. }
  150. }
  151. if(!ON){
  152. Serial.println("OFF");
  153. analogWrite(14,0);
  154. analogWrite(0,0);
  155. analogWrite(2,0);
  156. }
  157. server.send(200, "text/plain", "OK"); //Response to the HTTP request
  158. }
  159. void print_arr(int arr[],int size, String name){
  160. Serial.println("SHOWING ARRAY " + name);
  161. for(int i=0;i<size;i++){
  162. Serial.println(arr[i]);
  163. }
  164. Serial.println("DONE");
  165. }
  166. void setAlarm(){
  167. for (int i = 0; i < server.args(); i++) {
  168. if(server.argName(i)=="time"){
  169. alarm = server.arg(i).toInt();
  170. int h = floor(alarm/3600);
  171. int m = floor((alarm%3600)/60);
  172. int s = alarm%60;
  173. EEPROM.put(0,h);
  174. EEPROM.commit();
  175. EEPROM.put(5,m);
  176. EEPROM.commit();
  177. EEPROM.put(7,s);
  178. EEPROM.commit();
  179. }
  180. if(server.argName(i)=="days"){
  181. EEPROM.put(10,server.arg(i).toInt());
  182. }
  183. }
  184. EEPROM.commit();
  185. server.send(200,"text/plain","OK");
  186. }
  187. void toggleReadingLight(){
  188. digitalWrite(13,!reading_light);
  189. reading_light = !reading_light;
  190. server.send(200,"text/plain","OK");
  191. }
  192. void getReadingLight(){
  193. if(reading_light){
  194. server.send(200,"text/plain","ON");
  195. } else{
  196. server.send(200,"text/plain","OFF");
  197. }
  198. }
  199. void getAlarm(){
  200. int days[7];
  201. read_alarm_date(days);
  202. int time = read_alarm_time();
  203. String response = "{\"days\":{";
  204. for(int i=0;i<7;i++){
  205. response += String(days[i]);
  206. if(i != 6){
  207. response += ",";
  208. }
  209. }
  210. response += "],\"time\":\"";
  211. response += String(time) + "\"}";
  212. server.send(200,"application/json",response);
  213. }
  214. void getColor(){
  215. server.send(200, "application/json","{\"RGB\":[" + String(RGB[0]) + "," + String(RGB[1]) + "," + String(RGB[2]) + "], \"ON\":" + String(RGB[3]) + "}");
  216. }
  217. void show_time(String time, String day){
  218. display.clearDisplay();
  219. display.setCursor(0,0);
  220. display.setTextSize(2);
  221. display.setTextColor(WHITE);
  222. display.setCursor(16,0);
  223. display.println(time);
  224. display.setCursor(18,24);
  225. display.println(day);
  226. display.setTextSize(1);
  227. display.setFont();
  228. display.setTextColor(WHITE);
  229. display.setCursor(32,48);
  230. display.println(WiFi.localIP());
  231. display.display();
  232. }
  233. void geteeprom(){
  234. int data;
  235. data = EEPROM.read(server.arg(0).toInt());
  236. server.send(200, "application/text",String(data));
  237. }
  238. void loop() {
  239. unsigned long currentMillis = millis();
  240. if (currentMillis - previousMillis >= 1000) {
  241. int day = timeClient.getDay();
  242. if(day!=day_prev){
  243. day_prev = day;
  244. alarm_ran=false;
  245. }
  246. previousMillis = currentMillis;
  247. if(counter==300){
  248. counter=0;
  249. Serial.println("test3");
  250. timeClient.update();
  251. Serial.println("test4");
  252. }
  253. show_time(timeClient.getFormattedTime(),daysOfTheWeek[timeClient.getDay()]);
  254. int time = timeClient.getHours()*3600 + timeClient.getMinutes()*60 + timeClient.getSeconds();
  255. Serial.println("TIME");
  256. Serial.println(time);
  257. Serial.println(alarm);
  258. Serial.println(timeClient.getDay());
  259. if (time > alarm && days[timeClient.getDay()] && !alarm_ran){
  260. alarm_ran = true;
  261. analogWrite(0,255);
  262. analogWrite(2,255);
  263. analogWrite(14,255);
  264. Serial.println("ALARM");
  265. playTetris();
  266. analogWrite(0,0);
  267. analogWrite(2,0);
  268. analogWrite(14,0);
  269. }
  270. counter += 1;
  271. }
  272. server.handleClient();
  273. }