MAIN_ALL.ino 2.8 KB

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