hdspanel.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Home Dynamic System v3
  3. Desktop Control Panel Firmware
  4. Author: tobychui
  5. [Compile Options]
  6. Wemos D1 R2 & Mini
  7. CPU: 160Mhz
  8. IwIP: High Bandwidth v2
  9. [Wiring Definations]
  10. D0 -> Button 4
  11. D5 -> Button 3
  12. D6 -> Button 2
  13. D7 -> Button 1
  14. [HMI Element Definations]
  15. page: home
  16. p0 -> background wallpaper (night: 0, day: 1)
  17. time -> current time
  18. ampm -> am / pm string
  19. date -> current date
  20. dow -> Date of Week, e.g. MON
  21. temp -> Current temp, max 2 digit
  22. humd -> Current humditiy, max 2 digit
  23. rain -> Current rainfall in mm, max 2 digit
  24. wicon -> Weather icon (large)
  25. 6: Sunny Day
  26. 7: Clear Night
  27. 8: Cloudy
  28. 9: Rainy
  29. 10: Cloudy Day
  30. 11: Heavy Rain
  31. 12: Thunder
  32. 13: Cloudy Night
  33. */
  34. //Include libraries
  35. #include <ESP8266WiFi.h>
  36. #include <NTPClient.h>
  37. #include <WiFiUdp.h>
  38. #include <SoftwareSerial.h>
  39. #include <WiFiManager.h>
  40. #include <ArduinoJson.h>
  41. #include <TaskScheduler.h>
  42. //WiFi Related
  43. WiFiManager wifiManager;
  44. //UART HMI
  45. SoftwareSerial HMISerial(D2, D1); // RX, TX
  46. bool debugMode = true;
  47. //Clock & NTP
  48. WiFiUDP UDPconn;
  49. NTPClient ntpClient(UDPconn, "pool.ntp.org");
  50. const String weekDays[7] = {"Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"};
  51. const String months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  52. //Global Variables
  53. int currentPage = 0; //0 = home, 1 = forcast, 2 = iot
  54. bool buttonState[4] = {false, false, false, false};
  55. //Current weather
  56. int currentTemp = 25;
  57. int currentHumd = 50;
  58. int currentRain = 0;
  59. bool currentIsRaining = false;
  60. //Schedulers
  61. void keyboardListenerCallback();
  62. void datetimeUpdateCallback();
  63. void todayWeatherUpdateCallback();
  64. void weatherForcastCallback();
  65. Task t_kb(50, TASK_FOREVER, &keyboardListenerCallback);
  66. Task t_dt(10000, TASK_FOREVER, &datetimeUpdateCallback);
  67. Task t_wt(3600000, TASK_FOREVER, &todayWeatherUpdateCallback);
  68. Task t_wf(3600000, TASK_FOREVER, &weatherForcastCallback);
  69. Scheduler runner;
  70. // the setup function runs once when you press reset or power the board
  71. void setup() {
  72. //Serial
  73. Serial.begin(9600);
  74. HMISerial.begin(9600);
  75. delay(100);
  76. sendCommand("page init");
  77. sendCommand("t0.txt=\"Waiting WiFi\"");
  78. // Connect to Wi-Fi
  79. wifiManager.setClass("invert");
  80. wifiManager.autoConnect("HomeDynamic Panel");
  81. if (!debugMode) {
  82. wifiManager.setDebugOutput(false);
  83. }
  84. sendCommand("t0.txt=\"Setting up NTP conn\"");
  85. delay(1000);
  86. //Start NTP Client
  87. ntpClient.begin();
  88. ntpClient.setTimeOffset(28800);
  89. // setup button pins
  90. pinMode(D7, INPUT); //1
  91. pinMode(D6, INPUT); //2
  92. pinMode(D5, INPUT); //3
  93. pinMode(D0, INPUT); //4
  94. sendCommand("t0.txt=\"Starting scheduler\"");
  95. // setup schedulers
  96. runner.init();
  97. Serial.println("Initialized scheduler");
  98. runner.addTask(t_kb); //Keyboard update task
  99. runner.addTask(t_dt); //Date-time update task
  100. runner.addTask(t_wt); //Weather update task
  101. runner.addTask(t_wf); //Weather forcast task
  102. //Enable all the backtground tasks
  103. delay(300);
  104. t_kb.enable();
  105. delay(300);
  106. t_dt.enable();
  107. delay(300);
  108. t_wt.enable();
  109. delay(300);
  110. t_wf.enable();
  111. //Switch over to homepage
  112. sendCommand("page home");
  113. }
  114. // the loop function runs over and over again forever
  115. void loop() {
  116. runner.execute();
  117. }