MAIN_ALL.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include <HardwareSerial.h>
  2. #include <Preferences.h>
  3. #include <WiFi.h>
  4. #include <MacroDebugger.h>
  5. #include "WS_Bluetooth.h"
  6. #include "WS_GPIO.h"
  7. //#define ENABLE_DEBUG /* <-- Commenting this line will remove any trace of debug printing */
  8. /******************************************************** Data Analysis ********************************************************/
  9. unsigned long timers[6] = { 0, 0, 0, 0, 0, 0 };
  10. bool isOverride = false;
  11. Preferences preferences;
  12. String macAddress;
  13. String devicename;
  14. String equipmentid;
  15. String warningnotice;
  16. String contactinfo;
  17. bool btConnected;
  18. void Restart()
  19. {
  20. ESP.restart();
  21. }
  22. void Configure(String name, String warning, String contact, String id)
  23. {
  24. preferences.begin("prs-digital-key", false);
  25. devicename = name;
  26. preferences.putString("device-name", name.c_str());
  27. warningnotice = warning;
  28. preferences.putString("warning-notice", warning.c_str());
  29. contactinfo = contact;
  30. preferences.putString("contact-info", contact.c_str());
  31. equipmentid = id;
  32. preferences.putString("id", id.c_str());
  33. preferences.end();
  34. }
  35. String GetMacAddress()
  36. {
  37. return macAddress;
  38. }
  39. String GetDeviceName()
  40. {
  41. return devicename;
  42. }
  43. String GetWarningNotice()
  44. {
  45. return warningnotice;
  46. }
  47. String GetContactInfo()
  48. {
  49. return contactinfo;
  50. }
  51. String GetEquipmentId()
  52. {
  53. return equipmentid;
  54. }
  55. void SetRelay(int relay, long time)
  56. {
  57. if (time < 0L)
  58. {
  59. TriggerRelay(relay,true);
  60. timers[relay] = 0;
  61. }
  62. else if (time==0L)
  63. {
  64. TriggerRelay(relay,false);
  65. timers[relay] = 0;
  66. }
  67. else
  68. {
  69. TriggerRelay(relay,true);
  70. timers[relay] = millis() + time;
  71. }
  72. }
  73. bool GetRelay(int relay)
  74. {
  75. return RelayStatus(relay);
  76. }
  77. int i = 0;
  78. /******************************************************** Initializing ********************************************************/
  79. void setup()
  80. {
  81. DEBUG_BEGIN();
  82. WiFi.mode(WIFI_STA);
  83. macAddress = WiFi.macAddress().c_str();
  84. preferences.begin("prs-digital-key", true);
  85. devicename = preferences.getString("device-name","PRS Digital Key");
  86. warningnotice = preferences.getString("warning-notice","(No information available)");
  87. contactinfo = preferences.getString("contact-info","(Not available)");
  88. equipmentid = preferences.getString("id","00000000-0000-0000-0000-000000000000");
  89. preferences.end();
  90. GPIO_Init();
  91. Bluetooth_Init();
  92. }
  93. /********************************************************** While **********************************************************/
  94. void loop()
  95. {
  96. if (btConnected && !IsConnected())
  97. {
  98. RGB_Light(0, 0, 0);
  99. btConnected = false;
  100. } else if (!btConnected && IsConnected())
  101. {
  102. RGB_Light(0, 0, 60);
  103. btConnected = true;
  104. }
  105. unsigned long ms = millis();
  106. bool bChanged = false;
  107. for (int relay=0; relay<6; relay++)
  108. {
  109. if ((timers[relay] > 0L) && (timers[relay] <= ms))
  110. {
  111. SetRelay(relay,0L);
  112. bChanged = true;
  113. }
  114. }
  115. if (bChanged && !IsConnected())
  116. UpdateAdvertising();
  117. }