Advertisement
pleasedontcode

**System Initialization** rev_25

Oct 20th, 2024
74
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: **System Initialization**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-20 23:16:10
  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); // Current sensor
  51. X9C10X pot(10000);  // Digital potentiometer
  52.  
  53. // Define constants and global variables...
  54. #define VELOCITA_SOGLIA_HIGH 75.0 //km/h
  55. #define VELOCITA_SOGLIA_LOW 65.0 //km/h
  56. #define VELOCITA_SOGLIA_0A_HIGH  20.0 //km/h
  57. #define VELOCITA_SOGLIA_0A_LOW  10.0 //km/h
  58.  
  59. // Pins and global variables...
  60. const int water_level_pin = 18;
  61. const int voltage_pin     = 35;
  62. const int current_pin     = 34;
  63. const int tempSensor1_pin = 33;
  64. const int tempSensor2_pin = 32;
  65. const int ONE_WIRE_BUS_Temp1 = 19; // Temperature sensor 1
  66. const int ONE_WIRE_BUS_Temp2 = 21; // Temperature sensor 2
  67. const int DIGPOT_INC      = 4;    // pin INC - X9C103S
  68. const int DIGPOT_UD       = 5;    // pin UD - X9C103S
  69. const int DIGPOT_CS       = 15;   // pin CS - X9C103S
  70.  
  71. const float PWM_output_percentage_0A = 0.0; //0%
  72. const float PWM_output_percentage_9A = 20.0; //20% --> 1V/5V
  73. const float PWM_output_percentage_5A = 13.0; //13% --> 0,65V/5V
  74.  
  75. // Global data
  76. float PWM_output_percentage = 0.0;
  77. float velocita = 0.0;
  78. float voltage_output_value = 0.0;
  79. bool waterLevelEmpty = true;
  80. float voltage = 0.0;
  81. float current = 0.0;
  82. float temperature1 = 0.0;
  83. float temperature2 = 0.0;
  84. unsigned long powerOnTime = 0; //minutes
  85.  
  86. boolean startRegeneration = false;
  87. unsigned int set_current  = 0;
  88. unsigned int set_timer    = 0;
  89.  
  90. // Configuration for ACS712
  91. unsigned int ADC_Offset = 1930;
  92.  
  93. // Setup a OneWire instance for temperature sensors
  94. OneWire oneWire1(ONE_WIRE_BUS_Temp1);
  95. OneWire oneWire2(ONE_WIRE_BUS_Temp2);
  96. DallasTemperature sensor1(&oneWire1);
  97. DallasTemperature sensor2(&oneWire2);
  98.  
  99. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  100. const uint8_t SEGMENT_POINTS_voltage_Temperature = 10;
  101. const float voltage_Temperature_lookup[2][SEGMENT_POINTS_voltage_Temperature] =
  102. {
  103.     {0.0, 1.2, 1.9, 5.0, 10.0, 14.5, 17.0, 20.0, 30.0, 35.0}, // voltage [V]
  104.     {0.0, 5.0, 7.0, 13.0, 22.0, 29.0, 33.0, 37.0, 56.0, 65.0} // percentage [°C]
  105. };
  106.  
  107. // Initialize SPIFFS for file storage
  108. void initSPIFFS() {
  109.   if (!SPIFFS.begin(true)) {
  110.     Serial.println("An error occurred while mounting SPIFFS");
  111.     return;
  112.   }
  113.   Serial.println("SPIFFS mounted successfully");
  114. }
  115.  
  116. void setup() {
  117.   // Start Serial for debugging
  118.   Serial.begin(115200);
  119.  
  120.   delay(1000);
  121.   Serial.println("ciao");
  122.  
  123.   pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS);  // Initialize digital potentiometer
  124.  
  125.   // Initialize ACS712 sensor
  126.   ACS.setMidPoint(ADC_Offset);
  127.  
  128.   // Initialize temperature sensors
  129.   sensor1.begin();
  130.   sensor2.begin();
  131.  
  132.   // Initialize SPIFFS
  133.   initSPIFFS();
  134.  
  135.   // Set up WiFi Access Point
  136.   WiFi.mode(WIFI_AP); // Set the ESP32 to access point mode
  137.   if (!WiFi.softAP(ssid, pass)) {
  138.     Serial.println("Soft AP creation failed.");
  139.     while(1);
  140.   }
  141.  
  142.   // Print the IP address of the access point
  143.   IPAddress IP = WiFi.softAPIP();
  144.   Serial.print("Access Point IP Address: ");
  145.   Serial.println(IP);
  146.  
  147.   // Serve the JPEG image
  148.   server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
  149.     request->send(SPIFFS, "/background.jpg", "image/jpeg");
  150.   });
  151.  
  152.   // Serve the HTML page
  153.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  154.     request->send(SPIFFS, "/index.html", "text/html");
  155.   });
  156.  
  157.   // Start the server
  158.   server.begin();
  159. }
  160.  
  161. void loop() {
  162.   // No logic in the loop; the server works asynchronously
  163.   readGPSAndCheckSpeed();
  164.   readCurrent();
  165.   readVoltage();
  166.   readWaterTank();
  167.   readTemp1();
  168.   readTemp2();
  169.   updateMinutesCounter();
  170.   checkRegeneration();
  171. }
  172.  
  173. // Function implementations...
  174.  
  175. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement