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.

290 lines
7.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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 "tetris.h"
  11. #include "secrets.h"
  12. #define SCREEN_WIDTH 128
  13. #define SCREEN_HEIGHT 64
  14. #define OLED_RESET -1
  15. #define ALARM_PIN 12
  16. ESP8266WebServer server(80); //Server on port 80
  17. WiFiUDP ntpUDP;
  18. NTPClient timeClient(ntpUDP, "asia.pool.ntp.org", 3600 * 3, 60000);
  19. char daysOfTheWeek[7][12] = { "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  20. int days[7];
  21. int alarm;
  22. int counter = 0;
  23. bool reading_light = false;
  24. int RGB[4] = {0,0,0,0};
  25. unsigned long previousMillis = 0;
  26. int day_prev = -1;
  27. bool alarm_ran = false;
  28. // ESP8266 Network Variables
  29. IPAddress staticIP(192, 168, 2, 7);
  30. IPAddress gateway(192, 168, 2, 1);
  31. IPAddress subnet(255, 255, 255, 0);
  32. IPAddress dns(192, 168, 2, 3);
  33. const char* deviceName = "yeetclock.xyz";
  34. void setup() {
  35. // Set Pins
  36. pinMode(14,OUTPUT);
  37. pinMode(13,OUTPUT);
  38. digitalWrite(13,LOW);
  39. pinMode(15, OUTPUT);
  40. pinMode(0,OUTPUT);
  41. pinMode(2,OUTPUT);
  42. pinMode(ALARM_PIN, OUTPUT);
  43. digitalWrite(ALARM_PIN,LOW);
  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. while(!timeClient.update()){
  71. Serial.println("Receiving NTP time for the first time");
  72. }
  73. Serial.println(timeClient.getFormattedTime());
  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. digitalWrite(ALARM_PIN, HIGH);
  121. double duration;
  122. double freq;
  123. for (int i = 0; i < server.args(); i++) {
  124. if(server.argName(i) == "duration"){
  125. duration=server.arg(i).toDouble();
  126. }
  127. if(server.argName(i) == "freq"){
  128. freq=server.arg(i).toDouble();
  129. }
  130. }
  131. tone(15,freq,duration*1000);
  132. server.send(200, "text/plain", "OK");
  133. digitalWrite(ALARM_PIN, LOW);
  134. }
  135. void setColor(){
  136. bool ON;
  137. for (int i = 0; i < server.args(); i++) {
  138. switch(server.argName(i).charAt(0)){
  139. case 'R':
  140. analogWrite(0,server.arg(i).toInt());
  141. RGB[0] = server.arg(i).toInt();
  142. case 'G':
  143. analogWrite(2,server.arg(i).toInt());
  144. RGB[1] = server.arg(i).toInt();
  145. case 'B':
  146. analogWrite(14,server.arg(i).toInt());
  147. RGB[2] = server.arg(i).toInt();
  148. case 'O':
  149. ON = server.arg(i).toInt();
  150. RGB[3] = server.arg(i).toInt();
  151. }
  152. }
  153. if(!ON){
  154. Serial.println("OFF");
  155. analogWrite(14,0);
  156. analogWrite(0,0);
  157. analogWrite(2,0);
  158. }
  159. server.send(200, "text/plain", "OK"); //Response to the HTTP request
  160. }
  161. void print_arr(int arr[],int size, String name){
  162. Serial.println("SHOWING ARRAY " + name);
  163. for(int i=0;i<size;i++){
  164. Serial.println(arr[i]);
  165. }
  166. Serial.println("DONE");
  167. }
  168. void setAlarm(){
  169. for (int i = 0; i < server.args(); i++) {
  170. if(server.argName(i)=="time"){
  171. alarm = server.arg(i).toInt();
  172. int h = floor(alarm/3600);
  173. int m = floor((alarm%3600)/60);
  174. int s = alarm%60;
  175. EEPROM.put(0,h);
  176. EEPROM.commit();
  177. EEPROM.put(5,m);
  178. EEPROM.commit();
  179. EEPROM.put(7,s);
  180. EEPROM.commit();
  181. }
  182. if(server.argName(i)=="days"){
  183. EEPROM.put(10,server.arg(i).toInt());
  184. }
  185. }
  186. EEPROM.commit();
  187. server.send(200,"text/plain","OK");
  188. }
  189. void toggleReadingLight(){
  190. digitalWrite(13,!reading_light);
  191. reading_light = !reading_light;
  192. server.send(200,"text/plain","OK");
  193. }
  194. void getReadingLight(){
  195. if(reading_light){
  196. server.send(200,"text/plain","ON");
  197. } else{
  198. server.send(200,"text/plain","OFF");
  199. }
  200. }
  201. void getAlarm(){
  202. int days[7];
  203. read_alarm_date(days);
  204. int time = read_alarm_time();
  205. String response = "{\"days\":[";
  206. for(int i=0;i<7;i++){
  207. response += String(days[i]);
  208. if(i != 6){
  209. response += ",";
  210. }
  211. }
  212. response += "],\"time\":\"";
  213. response += String(time) + "\"}";
  214. server.send(200,"application/json",response);
  215. }
  216. void getColor(){
  217. server.send(200, "application/json","{\"RGB\":[" + String(RGB[0]) + "," + String(RGB[1]) + "," + String(RGB[2]) + "], \"ON\":" + String(RGB[3]) + "}");
  218. }
  219. void geteeprom(){
  220. int data;
  221. data = EEPROM.read(server.arg(0).toInt());
  222. server.send(200, "application/text",String(data));
  223. }
  224. void loop() {
  225. unsigned long currentMillis = millis();
  226. if (currentMillis - previousMillis >= 1000) {
  227. int day = timeClient.getDay();
  228. if(day!=day_prev){
  229. day_prev = day;
  230. alarm_ran=false;
  231. }
  232. previousMillis = currentMillis;
  233. int time = timeClient.getHours()*3600 + timeClient.getMinutes()*60 + timeClient.getSeconds();
  234. Serial.println("TIME");
  235. Serial.println(time);
  236. Serial.println(alarm);
  237. Serial.println(timeClient.getDay());
  238. if (time > alarm && days[timeClient.getDay()] && !alarm_ran){
  239. alarm_ran = true;
  240. analogWrite(0,255);
  241. analogWrite(2,255);
  242. analogWrite(14,255);
  243. Serial.println("ALARM");
  244. digitalWrite(ALARM_PIN, HIGH);
  245. playTetris();
  246. digitalWrite(ALARM_PIN, LOW);
  247. analogWrite(0,0);
  248. analogWrite(2,0);
  249. analogWrite(14,0);
  250. }
  251. counter += 1;
  252. }
  253. server.handleClient();
  254. }