WiFiPCRemote.ino 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // pre compiletime config
  2. #define SPIFFS_DBG
  3. #define SPIFFS_USE_MAGIC
  4. #define FIRMWARE_NAME "WiFiPCRemote"
  5. #define FIRMWARE_VERSION "0.1.1"
  6. #define FIRMWARE_URL "https://git.flokra.at/flo/WiFiPCRemote"
  7. #define FIRMWARE_COPYRIGHT "FloKra"
  8. #define FIRMWARE_COPYRIGHT_URL "https://www.flokra.at/"
  9. // default values, can later be overridden via configuration
  10. #define DEVICE_NAME "WiFiPCRemote-1"
  11. #define MQTT_SERVER "mqtt.lan"
  12. #define MQTT_PORT 1883
  13. #define MQTT_TOPIC_IN "Test/WiFiPCRemote1/cmd"
  14. #define MQTT_TOPIC_OUT "Test/WiFiPCRemote1/status"
  15. #define PIN_OUT_POWERSW 5
  16. #define PIN_OUT_RESETSW 4
  17. #define PIN_IN_PWRLED 14
  18. // default values that can only be configured at compile time / hardware configuration
  19. #define MQTT_HEARTBEAT_MAXAGE 180000 // max interval for MQTT heartbeat message. only applicable if MQTT IN-topic is defined. after this timeout MQTT reconnect is forced
  20. #define MQTT_HEARTBEAT_MAXAGE_REBOOT 1800000 // max interval for MQTT heartbeat message. only applicable if MQTT IN-topic is defined. after this timeout the ESP will reboot
  21. // default logic levels
  22. #define OUT_POWERSW_ONSTATE HIGH
  23. #define OUT_RESETSW_ONSTATE HIGH
  24. #define IN_PWRLED_ONSTATE LOW
  25. #include "PersWiFiManager.h"
  26. #include <ArduinoJson.h>
  27. #include <ESP8266WiFi.h>
  28. #include <WiFiClient.h>
  29. #include <ESP8266WebServer.h>
  30. #include <ESP8266mDNS.h>
  31. #include <ESP8266HTTPUpdateServer.h>
  32. #include <PubSubClient.h>
  33. #include <DNSServer.h>
  34. #include <FS.h>
  35. boolean serialdebug = true;
  36. boolean mqttdebug = true;
  37. // config variables - do not change here!
  38. // confWeb
  39. char deviceName[31];
  40. char http_user[31];
  41. char http_pass[31];
  42. boolean http_user_auth = false;
  43. char http_token[31];
  44. char http_user1[31];
  45. char http_pass1[31];
  46. char http_user2[31];
  47. char http_pass2[31];
  48. // confMqtt
  49. boolean mqtt_enable = false;
  50. char mqtt_server[41];
  51. int mqtt_port = MQTT_PORT;
  52. char mqtt_user[31];
  53. char mqtt_pass[31];
  54. char mqtt_topic_in[51];
  55. char mqtt_topic_out[51];
  56. char mqtt_willTopic[51];
  57. int mqtt_willQos = 2;
  58. boolean mqtt_willRetain = false;
  59. char mqtt_willMsg[31];
  60. char mqtt_connMsg[31];
  61. boolean mqtt_outRetain = false;
  62. boolean mqtt_enable_heartbeat = true;
  63. // global variables
  64. byte mqttMode = 0;
  65. unsigned long mqttLastReconnectAttempt = 0;
  66. int mqttReconnectAttempts = 0;
  67. int mqttReconnects = 0;
  68. boolean mqttConnected = false;
  69. unsigned long mqttLastHeartbeat;
  70. boolean mqttInTopicSubscribed = false;
  71. char cmdPayload[101]; // buffer for commands
  72. boolean cmdInQueue = false; // command is queued and will be processed next loop() run
  73. bool saveConfigWebToFlash = false;
  74. bool saveConfigMqttToFlash = false;
  75. //bool saveConfigHwToFlash = false;
  76. char mqtt_topic_in_pcpwr[64];
  77. char mqtt_topic_in_pcpwrh[64];
  78. char mqtt_topic_in_pcrst[64];
  79. char mqtt_topic_in_pconsleep[64];
  80. unsigned long mqtt_heartbeat_maxage_reconnect = MQTT_HEARTBEAT_MAXAGE;
  81. unsigned long mqtt_heartbeat_maxage_reboot = MQTT_HEARTBEAT_MAXAGE_REBOOT;
  82. char tmp_topic_pub[51];
  83. boolean pwrSwitchStateOn;
  84. unsigned long pwrSwitchOnAt;
  85. unsigned int pwrSwitchOffAfter;
  86. boolean resSwitchStateOn;
  87. unsigned long resSwitchOnAt;
  88. unsigned int resSwitchOffAfter;
  89. boolean in_pwrled_lastState = LOW;
  90. boolean in_pwrled_currState = LOW;
  91. unsigned long in_pwrled_millis = 0;
  92. unsigned long PC_standby_pending_millis = 0;
  93. int PCstate = 0;
  94. int PCstate_lastPublished = 0;
  95. unsigned int sysUptime_days = 0;
  96. unsigned int sysUptime_hours = 0;
  97. unsigned int sysUptime_mins = 0;
  98. char uptimeStr[15];
  99. WiFiClient espClient;
  100. void mqttCallback(char* topic, byte* payload, unsigned int length);
  101. PubSubClient mqttclient(espClient);
  102. ESP8266WebServer httpServer(80);
  103. DNSServer dnsServer;
  104. PersWiFiManager persWM(httpServer, dnsServer);
  105. ESP8266HTTPUpdateServer httpUpdater;
  106. void setup() {
  107. Serial.begin(115200);
  108. delay(500);
  109. Serial.println();
  110. Serial.print(FIRMWARE_NAME);
  111. Serial.print(" v");
  112. Serial.print(FIRMWARE_VERSION);
  113. Serial.println(FIRMWARE_COPYRIGHT);
  114. Serial.println("starting...");
  115. pinMode(PIN_OUT_POWERSW, OUTPUT);
  116. digitalWrite(PIN_OUT_POWERSW, !OUT_POWERSW_ONSTATE);
  117. pinMode(PIN_OUT_RESETSW, OUTPUT);
  118. digitalWrite(PIN_OUT_RESETSW, !OUT_RESETSW_ONSTATE);
  119. pinMode(PIN_IN_PWRLED, INPUT_PULLUP);
  120. strlcpy(deviceName, DEVICE_NAME, 31);
  121. strlcpy(mqtt_server, MQTT_SERVER, 41);
  122. strlcpy(mqtt_topic_in, MQTT_TOPIC_IN, 51);
  123. strlcpy(mqtt_topic_out, MQTT_TOPIC_OUT, 51);
  124. Serial.println("default config values loaded..");
  125. Serial.println("Mounting FS...");
  126. if (!SPIFFS.begin()) {
  127. Serial.println("Failed to mount file system");
  128. return;
  129. }
  130. //SPIFFS.format();
  131. //Serial.print("Format SPIFFS complete.");
  132. if (!SPIFFS.exists("/formatted")) {
  133. Serial.println("Please wait 30 secs for SPIFFS to be formatted");
  134. SPIFFS.format();
  135. Serial.println("Spiffs formatted");
  136. File f = SPIFFS.open("/formatted", "w");
  137. if (!f) {
  138. Serial.println("file open failed");
  139. } else {
  140. f.println("Format Complete");
  141. }
  142. f.close();
  143. } else {
  144. Serial.println("SPIFFS is formatted. Moving along...");
  145. }
  146. Serial.println("files in SPIFFS:");
  147. Dir dir = SPIFFS.openDir("/");
  148. while (dir.next()) {
  149. Serial.print(dir.fileName());
  150. File f = dir.openFile("r");
  151. Serial.print(" ");
  152. Serial.println(f.size());
  153. f.close();
  154. }
  155. Serial.println("---");
  156. // load config from SPIFFS if files exist
  157. if (!loadConfigWeb()) {
  158. Serial.println("Failed to load confWeb.json");
  159. } else {
  160. Serial.println("confWeb.json loaded");
  161. }
  162. if (!loadConfigMqtt()) {
  163. Serial.println("Failed to load confWeb.json");
  164. } else {
  165. Serial.println("confWeb.json loaded");
  166. }
  167. delay(500);
  168. //optional code handlers to run everytime wifi is connected...
  169. persWM.onConnect([]() {
  170. Serial.print("wifi connected to ");
  171. Serial.print(WiFi.SSID());
  172. Serial.print(", IP: ");
  173. Serial.println(WiFi.localIP());
  174. });
  175. //...or AP mode is started
  176. persWM.onAp([]() {
  177. Serial.print("AP MODE: ");
  178. Serial.println(persWM.getApSsid());
  179. });
  180. //sets network name for AP mode
  181. persWM.setApCredentials(DEVICE_NAME);
  182. //persWM.setApCredentials(DEVICE_NAME, "password"); optional password
  183. //make connecting/disconnecting non-blocking
  184. persWM.setConnectNonBlock(true);
  185. //in non-blocking mode, program will continue past this point without waiting
  186. persWM.begin();
  187. delay(500);
  188. httpServerInit();
  189. if(mqtt_enable) {
  190. mqttPrepareConnection();
  191. mqttClientInit();
  192. }
  193. buildUptimeString();
  194. Serial.println("setup complete.");
  195. delay(1000);
  196. } //void setup
  197. void loop() {
  198. checkMillis();
  199. persWM.handleWiFi(); //in non-blocking mode, handleWiFi must be called in the main loop
  200. yield();
  201. dnsServer.processNextRequest();
  202. httpServer.handleClient();
  203. yield();
  204. if(mqtt_enable) {
  205. mqttHandleConnection();
  206. yield();
  207. }
  208. evalCmd();
  209. handleOutputs_loop();
  210. if (Serial.available()) {
  211. serialEvent();
  212. yield();
  213. }
  214. } //void loop