123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- /*
- Home Dynamic System v3
- Desktop Control Panel Firmware
- Author: tobychui
- [Compile Options]
- Wemos D1 R2 & Mini
- CPU: 160Mhz
- IwIP: High Bandwidth v2
- [Wiring Definations]
- D0 -> Button 4
- D5 -> Button 3
- D6 -> Button 2
- D7 -> Button 1
- [HMI Element Definations]
- page: home
- p0 -> background wallpaper (night: 0, day: 1)
- time -> current time
- ampm -> am / pm string
- date -> current date
- dow -> Date of Week, e.g. MON
- temp -> Current temp, max 2 digit
- humd -> Current humditiy, max 2 digit
- rain -> Current rainfall in mm, max 2 digit
- wicon -> Weather icon (large)
- 6: Sunny Day
- 7: Clear Night
- 8: Cloudy
- 9: Rainy
- 10: Cloudy Day
- 11: Heavy Rain
- 12: Thunder
- 13: Cloudy Night
- */
- //Include libraries
- #include <ESP8266WiFi.h>
- #include <NTPClient.h>
- #include <WiFiUdp.h>
- #include <SoftwareSerial.h>
- #include <WiFiManager.h>
- #include <ArduinoJson.h>
- #include <TaskScheduler.h>
- //WiFi Related
- WiFiManager wifiManager;
- //UART HMI
- SoftwareSerial HMISerial(D2, D1); // RX, TX
- bool debugMode = true;
- //Clock & NTP
- WiFiUDP UDPconn;
- NTPClient ntpClient(UDPconn, "pool.ntp.org");
- const String weekDays[7] = {"Sun", "Mon", "Tues", "Wed", "Thu", "Fri", "Sat"};
- const String months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
- //Global Variables
- int currentPage = 0; //0 = home, 1 = forcast, 2 = iot
- bool buttonState[4] = {false, false, false, false};
- //Current weather
- int currentTemp = 25;
- int currentHumd = 50;
- int currentRain = 0;
- bool currentIsRaining = false;
- //Schedulers
- void keyboardListenerCallback();
- void datetimeUpdateCallback();
- void todayWeatherUpdateCallback();
- void weatherForcastCallback();
- Task t_kb(50, TASK_FOREVER, &keyboardListenerCallback);
- Task t_dt(10000, TASK_FOREVER, &datetimeUpdateCallback);
- Task t_wt(3600000, TASK_FOREVER, &todayWeatherUpdateCallback);
- Task t_wf(3600000, TASK_FOREVER, &weatherForcastCallback);
- Scheduler runner;
- // the setup function runs once when you press reset or power the board
- void setup() {
- //Serial
- Serial.begin(9600);
- HMISerial.begin(9600);
- delay(100);
- sendCommand("page init");
- sendCommand("t0.txt=\"Waiting WiFi\"");
-
- // Connect to Wi-Fi
- wifiManager.setClass("invert");
- wifiManager.autoConnect("HomeDynamic Panel");
- if (!debugMode) {
- wifiManager.setDebugOutput(false);
- }
-
- sendCommand("t0.txt=\"Setting up NTP conn\"");
- delay(1000);
-
- //Start NTP Client
- ntpClient.begin();
- ntpClient.setTimeOffset(28800);
- // setup button pins
- pinMode(D7, INPUT); //1
- pinMode(D6, INPUT); //2
- pinMode(D5, INPUT); //3
- pinMode(D0, INPUT); //4
- sendCommand("t0.txt=\"Starting scheduler\"");
- // setup schedulers
- runner.init();
- Serial.println("Initialized scheduler");
- runner.addTask(t_kb); //Keyboard update task
- runner.addTask(t_dt); //Date-time update task
- runner.addTask(t_wt); //Weather update task
- runner.addTask(t_wf); //Weather forcast task
- //Enable all the backtground tasks
- delay(300);
- t_kb.enable();
- delay(300);
- t_dt.enable();
- delay(300);
- t_wt.enable();
- delay(300);
- t_wf.enable();
- //Switch over to homepage
- sendCommand("page home");
- }
- // the loop function runs over and over again forever
- void loop() {
- runner.execute();
- }
|