Simple ESP8266 fan controller to take over PWM fans in our servers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.5 KiB

1 year ago
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESPAsyncTCP.h>
  4. #include <ESPAsyncWebServer.h>
  5. #include <AsyncElegantOTA.h>
  6. #include <Wire.h>
  7. AsyncWebServer server(80);
  8. #define FAN_PIN 2
  9. #define DELAY_TIME 1000 // time between measurements [ms]
  10. // This next section specifies variables and constants needed by the code snippet to convert serial input from ASCII to int
  11. #define numberOfDigits 3
  12. char theNumberString[numberOfDigits + 1];
  13. int input;
  14. // HTML based manual Fan speed input
  15. const char* PARAM_INPUT_1 = "input1";
  16. // WiFi settings
  17. const char* ssid = "LILiK_WiFi"; //replace with your SSID
  18. const char* password = "pippopippo"; //password
  19. String hostname = "Ventole_Einstein";
  20. // Default Fan Speed
  21. int fanSpeedPercent = 20;
  22. // HTML WEB interface
  23. const char index_html[] PROGMEM = R"rawliteral(
  24. <!DOCTYPE HTML><html><head>
  25. <title>ControllerVentoleEinstein</title>
  26. <meta name="viewport" content="width=device-width, initial-scale=1">
  27. </head><body>
  28. <h2>Ciao! Sono l'ESP8266 che gestisce le ventole di Einstein (HP DL380p G8)</h2>
  29. <form action="/get">
  30. % manuale: <input type="number" name="input1">
  31. <input type="submit" value="Submit">
  32. </form><br>
  33. <style>
  34. html {font-family: Times New Roman; display: inline-block; text-align: center;}
  35. h2 {font-size: 2.0rem;}
  36. h3 {font-size: 2.0rem; color: #FF0000;}
  37. </style>
  38. <h2>Clicca qui sotto per aggiornare il firmware, o il secondo link per il sorgente.</h2>
  39. <a href='/update'>&#11014;&#65039; OTA Firmware_Update &#11014;&#65039;</a>
  40. <a href='https://github.com/francescocy/ESP_fan_controller'>&#128049;GitHub&#128025;</a><br><br>
  41. </body></html>)rawliteral";
  42. void notFound(AsyncWebServerRequest *request) {
  43. request->send(404, "text/plain", "Not found");
  44. }
  45. // the setup function runs once when you press reset or power the board
  46. void setup(void) {
  47. pinMode(FAN_PIN, OUTPUT);
  48. analogWriteFreq(25000);
  49. Serial.begin(115200);
  50. WiFi.mode(WIFI_STA);
  51. // WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
  52. WiFi.setHostname(hostname.c_str()); //define hostname
  53. WiFi.begin(ssid, password);
  54. Serial.println("");
  55. // Wait for connection
  56. while (WiFi.status() != WL_CONNECTED) {
  57. delay(500);
  58. Serial.print(".");
  59. }
  60. Serial.println("");
  61. Serial.print("Connected to ");
  62. Serial.println(ssid);
  63. Serial.print("IP address: ");
  64. Serial.println(WiFi.localIP());
  65. // Send web page with input fields to client
  66. server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
  67. request->send(200, "text/html", index_html);
  68. });
  69. // Send a GET request to <ESP_IP>/get?input1=<inputMessage>
  70. server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
  71. String inputMessage;
  72. String inputParam;
  73. // GET input1 value on <ESP_IP>/get?input1=<inputMessage>
  74. if (request->hasParam(PARAM_INPUT_1)) {
  75. inputMessage = request->getParam(PARAM_INPUT_1)->value();
  76. inputParam = inputMessage;
  77. if (inputMessage.toFloat() > 5 && inputMessage.toFloat() < 101) {
  78. fanSpeedPercent = inputMessage.toFloat();
  79. }
  80. }
  81. else {
  82. inputMessage = "No message sent";
  83. inputParam = "none";
  84. }
  85. Serial.print("Speed received from HTML page: " );
  86. Serial.println(inputMessage);
  87. request->send(200, "text/html", "HTTP GET request sent to your ESP on input field ("
  88. + inputParam + ") with value: " + inputMessage +
  89. "<br><a href=\"/\">Return to Home Page</a>");
  90. });
  91. server.onNotFound(notFound);
  92. AsyncElegantOTA.begin(&server); // Start ElegantOTA
  93. server.begin();
  94. Serial.println("HTTP server started");
  95. }
  96. // the loop function runs over and over again forever
  97. void loop() {
  98. // Check if a serial interface is running
  99. if(Serial.available()){
  100. // Check the first character: if it is "S" it will be followed by 3 digits for the fan speed
  101. // S025 = 25%
  102. if(Serial.read() == 'S')
  103. {
  104. for (int i = 0; i < numberOfDigits; theNumberString[i++] = Serial.read());
  105. theNumberString[numberOfDigits] = 0x00;
  106. input = atoi(theNumberString);
  107. Serial.print("You typed: " );
  108. Serial.println(input);
  109. if (input > 4 && input < 101) {
  110. fanSpeedPercent = input;
  111. }
  112. else {
  113. Serial.print("Fan Speed out of Range!" );
  114. }
  115. }
  116. }
  117. Serial.print("Fan speed is ");
  118. Serial.print(fanSpeedPercent);
  119. Serial.println(" %");
  120. Serial.println();
  121. analogWrite(FAN_PIN, ((100 - fanSpeedPercent) / 100.0) * 255);
  122. delay(1000);
  123. }