Advertisement
pleasedontcode

**Asynchronous Initialization** rev_07

Oct 20th, 2024
78
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: **Asynchronous Initialization**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-20 10:20:56
  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. // Instantiate library objects
  42. TinyGPSPlus gps; // GPS object
  43. ACS712 ACS(A0, 5.0, 1023, 100); // Current sensor object
  44. OneWire oneWire(2); // OneWire object for temperature sensors
  45. DallasTemperature sensors(&oneWire); // Temperature sensor object
  46. X9C10X pot(10000); // Digital potentiometer object
  47.  
  48. void setup(void)
  49. {
  50.     // put your setup code here, to run once:
  51.     // Start Serial for debugging
  52.     Serial.begin(115200);
  53.     delay(1000);
  54.     Serial.println("ciao");
  55.  
  56.     // Access Point credentials
  57.     char ssid[] = "pleasedontcode.com";        // your network SSID (name)
  58.     char pass[] = "prova1234";        // your network password (use for WPA, or use as key for WEP)
  59.  
  60.     // Create an instance of the web server
  61.     AsyncWebServer server(80);
  62.  
  63.     pot.begin(4, 5, 15);  //  pulse, direction, select // INC = 4, UD = 5, CS = 15
  64.  
  65.     //NON TOGLIERE - SERVE PER NON FAR SBARELLARE LA MACCHINA
  66.     for(int i=0; i<10; i++)
  67.     {
  68.         setCurrentOutput(0);
  69.         delay(20);
  70.         setCurrentOutput(100);
  71.         delay(20);
  72.     }
  73.  
  74.     setCurrentOutput(PWM_output_percentage);
  75.     readMinutesCounterFromEEPROM();
  76.  
  77.     // Initialize GPS
  78.     Serial.println("Initializing GPS...");
  79.     gps.begin(GPSBaud);
  80.     Serial.println("ciao2");
  81.  
  82.     // Initialize temperature sensors
  83.     sensors.begin();
  84.  
  85.     smartDelay(1000);
  86.  
  87.     ACS.setMidPoint(ADC_Offset);
  88.  
  89.     Serial.println(F("Access Point Web Server"));
  90.  
  91.     // Initialize SPIFFS
  92.     if (!SPIFFS.begin(true)) {
  93.         Serial.println("An Error has occurred while mounting SPIFFS");
  94.         return;
  95.     }
  96.  
  97.     WiFi.mode(WIFI_AP); // Set the ESP32 to access point mode
  98.     if (!WiFi.softAP(ssid, pass)) {
  99.         Serial.println("Soft AP creation failed.");
  100.         while(1);
  101.     }
  102.  
  103.     // Print the IP address of the access point
  104.     IPAddress IP = WiFi.softAPIP();
  105.     Serial.print("Access Point IP Address: ");
  106.     Serial.println(IP);
  107.  
  108.     // Serve the JPEG image
  109.     server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
  110.         Serial.println("Serving background image");
  111.         request->send(SPIFFS, "/background.jpg", "image/jpeg");
  112.     });
  113.  
  114.     // Serve the HTML page
  115.     server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  116.         Serial.println("Serving index page");
  117.         request->send(SPIFFS, "/index.html", "text/html");
  118.     });
  119.  
  120.     server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
  121.         // Log post request handling
  122.         Serial.println("Handling postUpdateData");
  123.  
  124.         // Allocate a temporary JsonDocument
  125.         StaticJsonDocument<512> doc;
  126.  
  127.         // Gather data and populate JSON
  128.         updateData(doc);
  129.  
  130.         // Convert the JSON document to string and send it in response
  131.         String response;
  132.         serializeJsonPretty(doc, response);
  133.  
  134.         request->send(200, "application/json", response);
  135.     });
  136.  
  137.     // Start the server
  138.     server.begin();
  139. }
  140.  
  141. void loop(void)
  142. {
  143.     // put your main code here, to run repeatedly:
  144.     // No logic in the loop; the server works asynchronously
  145.  
  146.     readGPSAndCheckSpeed();
  147.     readCurrent();
  148.     readVoltage();
  149.     readWaterTank();
  150.     readTemp1();
  151.     readTemp2();
  152.     updateMinutesCounter();
  153.     checkRegeneration();
  154. }
  155.  
  156. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement