Advertisement
pleasedontcode

**Web Server** rev_21

Oct 20th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Web Server**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-20 16:11:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* merge code. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <TinyGPSPlus.h>
  27. #include "ACS712.h"
  28. #include <WiFi.h>
  29. #include <ESPAsyncWebServer.h>
  30. #include <SPIFFS.h>
  31. #include <ArduinoJson.h>
  32. #include <EEPROM.h>
  33. #include <OneWire.h>
  34. #include <DallasTemperature.h>
  35. #include "X9C10X.h"
  36.  
  37. /****** FUNCTION PROTOTYPES *****/
  38. void setup(void);
  39. void loop(void);
  40.  
  41. // Access Point credentials
  42. char ssid[] = "pleasedontcode.com";        // your network SSID (name)
  43. char pass[] = "prova1234";        // your network password (use for WPA, or use as key for WEP)
  44.  
  45. // Create an instance of the web server
  46. AsyncWebServer server(80);
  47.  
  48. // Instantiate library objects
  49. TinyGPSPlus gps;
  50. ACS712 ACS(current_pin, 3.3, 4095, 40); // pin for current sensor
  51. X9C10X pot(10000);  // 10KΩ digital potentiometer
  52. OneWire oneWire1(ONE_WIRE_BUS_Temp1);
  53. OneWire oneWire2(ONE_WIRE_BUS_Temp2);
  54. DallasTemperature sensor1(&oneWire1);
  55. DallasTemperature sensor2(&oneWire2);
  56.  
  57. /****** FUNCTION DEFINITIONS *****/
  58. void printWiFiStatus();
  59. void printWebPage();
  60. void checkClientRequest(String currentLine);
  61.  
  62. bool ReplyWebPageContent = true;
  63. bool ReplyResetTime = false;
  64. bool postUpdateData = false;
  65.  
  66. static const int GPSBaud = 9600;
  67. #define ss Serial2
  68.  
  69. unsigned long TEMPO_ATTESA_VISUALIZZAZIONE_VELOCITA = 300;
  70. unsigned long SpeedShowTimer = 0;
  71.  
  72. // Counter to track seconds
  73. unsigned long secondsCounter            = 0;
  74. unsigned long previousMillis            = millis(); // Variable to store the previous millis value
  75. unsigned long currentMillis             = millis();
  76. unsigned long minutesCounter            = 0;
  77.  
  78. // Initialize SPIFFS for file storage
  79. void initSPIFFS() {
  80.   if (!SPIFFS.begin(true)) {
  81.     Serial.println("An error occurred while mounting SPIFFS");
  82.     return;
  83.   }
  84.   Serial.println("SPIFFS mounted successfully");
  85. }
  86.  
  87. void setup() {
  88.   // Start Serial for debugging
  89.   Serial.begin(115200);
  90.   delay(1000);
  91.   Serial.println("ciao");
  92.  
  93.   pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS);  // pulse, direction, select
  94.  
  95.   //NON TOGLIERE - SERVE PER NON FAR SBARELLARE LA MACCHINA
  96.   for(int i=0; i<10;i++) {
  97.     setCurrentOutput(0);
  98.     delay(20);
  99.     setCurrentOutput(100);
  100.     delay(20);
  101.   }
  102.  
  103.   setCurrentOutput(PWM_output_percentage);
  104.   readMinutesCounterFromEEPROM();
  105.  
  106.   ss.begin(GPSBaud);
  107.   Serial.println("ciao2");
  108.  
  109.   sensor1.begin();
  110.   sensor2.begin();
  111.  
  112.   smartDelay(1000);
  113.   ACS.setMidPoint(ADC_Offset);
  114.  
  115.   Serial.println(F("Access Point Web Server"));
  116.  
  117.   // Initialize SPIFFS
  118.   initSPIFFS();
  119.  
  120.   WiFi.mode(WIFI_AP); // Set the ESP32 to access point mode
  121.   if (!WiFi.softAP(ssid, pass)) {
  122.     Serial.println("Soft AP creation failed.");
  123.     while(1);
  124.   }
  125.  
  126.   // Print the IP address of the access point
  127.   IPAddress IP = WiFi.softAPIP();
  128.   Serial.print("Access Point IP Address: ");
  129.   Serial.println(IP);
  130.  
  131.   // Serve the JPEG image
  132.   server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
  133.     Serial.println("0");
  134.     request->send(SPIFFS, "/background.jpg", "image/jpeg");
  135.   });
  136.  
  137.   // Serve the HTML page
  138.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  139.     Serial.println("1");
  140.     request->send(SPIFFS, "/index.html", "text/html");
  141.   });
  142.  
  143.   server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
  144.     // Log post request handling
  145.     //Serial.println("postUpdateData");
  146.  
  147.     // Allocate a temporary JsonDocument
  148.     StaticJsonDocument<512> doc;
  149.  
  150.     // Gather data and populate JSON
  151.     updateData(doc);
  152.  
  153.     // Convert the JSON document to string and send it in response
  154.     String response;
  155.     serializeJsonPretty(doc, response);
  156.  
  157.     request->send(200, "application/json", response);
  158.   });
  159.  
  160.   // Start the server
  161.   server.begin();
  162. }
  163.  
  164. void loop() {
  165.   // No logic in the loop; the server works asynchronously
  166.   readGPSAndCheckSpeed();
  167.   readCurrent();
  168.   readVoltage();
  169.   readWaterTank();
  170.   readTemp1();
  171.   readTemp2();
  172.   updateMinutesCounter();
  173.   checkRegeneration();
  174. }
  175.  
  176. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement