Advertisement
pleasedontcode

Sensor Management rev_41

Oct 21st, 2024
139
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: Sensor Management
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-21 15:10:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* merge code. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23.  
  24. /********* User code review feedback **********
  25. #### Feedback 1 ####
  26. - ESP32_project library is not included into ino project.
  27. ********* User code review feedback **********/
  28.  
  29. /* START CODE */
  30.  
  31. /****** DEFINITION OF LIBRARIES *****/
  32. #include <TinyGPSPlus.h>
  33. #include "ACS712.h"
  34. #include <WiFi.h>
  35. #include <ESPAsyncWebServer.h>
  36. #include <SPIFFS.h>
  37. #include <ArduinoJson.h>
  38. #include <EEPROM.h>
  39. #include <OneWire.h>
  40. #include <DallasTemperature.h>
  41. #include "X9C10X.h"
  42. #include "ESP32_Project.h" // Include the new library
  43.  
  44. /****** FUNCTION PROTOTYPES *****/
  45. void setup(void);
  46. void loop(void);
  47.  
  48. // Access Point credentials
  49. char ssid[] = "pleasedontcode.com";        // your network SSID (name)
  50. char pass[] = "prova1234";        // your network password (use for WPA, or use as key for WEP)
  51.  
  52. TinyGPSPlus gps;
  53.  
  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 - soglia sotto 15km/h si spegne l'erogazione corrente
  57. #define VELOCITA_SOGLIA_0A_LOW  10.0 //km/h
  58.  
  59. // PINS
  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; // sonda temp 1
  66. const int ONE_WIRE_BUS_Temp2 = 21; // sonda temp 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. // DATI globali da trasferire
  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. // configurazione ACS712 da 30A
  91. unsigned int ADC_Offset = 1930;
  92. ACS712  ACS(current_pin, 3.3, 4095, 40); // pin 34 per acquisizione sensore corrente; l'alimentazione sensore è 3.3V
  93. X9C10X pot(10000);  //  10KΩ  - potenziometro digitale X9C103S da 10kΩ
  94.  
  95. void printWiFiStatus();
  96. void printWebPage();
  97. void checkClientRequest(String currentLine);
  98.  
  99. bool ReplyWebPageContent = true;
  100. bool ReplyResetTime = false;
  101. bool postUpdateData = false;
  102.  
  103. static const int GPSBaud = 9600;
  104. #define ss Serial2
  105.  
  106. unsigned long TEMPO_ATTESA_VISUALIZZAZIONE_VELOCITA = 300;
  107. unsigned long SpeedShowTimer = 0;
  108.  
  109. // Counter to track seconds
  110. unsigned long secondsCounter            = 0;
  111. unsigned long previousMillis            = millis(); // Variable to store the previous millis value
  112. unsigned long currentMillis             = millis();
  113. unsigned long minutesCounter            = 0;
  114.  
  115. // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
  116. OneWire oneWire1(ONE_WIRE_BUS_Temp1);
  117. OneWire oneWire2(ONE_WIRE_BUS_Temp2);
  118.  
  119. // Pass our oneWire reference to Dallas Temperature.
  120. DallasTemperature sensor1(&oneWire1);
  121. DallasTemperature sensor2(&oneWire2);
  122.  
  123. /****** DEFINITION OF ANALOG INPUTS CHARACTERISTIC CURVES *****/
  124. const uint8_t SEGMENT_POINTS_voltage_Temperature = 10;
  125. const float voltage_Temperature_lookup[2][SEGMENT_POINTS_voltage_Temperature] =
  126. {
  127.     {0.0, 1.2, 1.9, 5.0, 10.0, 14.5, 17.0, 20.0, 30.0, 35.0}, // corrente [V]
  128.     {0.0, 5.0, 7.0, 13.0, 22.0, 29.0, 33.0, 37.0, 56.0, 65.0} // percentuale [°C]
  129. };
  130.  
  131. // Initialize SPIFFS for file storage
  132. void initSPIFFS() {
  133.   if (!SPIFFS.begin(true)) {
  134.     Serial.println("An error occurred while mounting SPIFFS");
  135.     return;
  136.   }
  137.   Serial.println("SPIFFS mounted successfully");
  138. }
  139.  
  140. void setup() {
  141.   // Start Serial for debugging
  142.   Serial.begin(115200);
  143.   delay(1000);
  144.   Serial.println("ciao");
  145.  
  146.   pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS);  //  pulse, direction, select // INC = 4, UD = 5, CS = 15
  147.  
  148.   // Set Up ACS712
  149.   ACS.setMidPoint(ADC_Offset);
  150.  
  151.   ss.begin(GPSBaud);
  152.   Serial.println("ciao2");
  153.  
  154.   sensor1.begin();
  155.   sensor2.begin();
  156.  
  157.   delay(1000);
  158.  
  159.   Serial.println(F("Access Point Web Server"));
  160.  
  161.   // Initialize SPIFFS
  162.   initSPIFFS();
  163.  
  164.   WiFi.mode(WIFI_AP); // Set the ESP32 to access point mode
  165.   if (!WiFi.softAP(ssid, pass)) {
  166.     Serial.println("Soft AP creation failed.");
  167.     while(1);
  168.   }
  169.  
  170.   // Print the IP address of the access point
  171.   IPAddress IP = WiFi.softAPIP();
  172.   Serial.print("Access Point IP Address: ");
  173.   Serial.println(IP);
  174.  
  175.   // URLs handling
  176.   server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
  177.     Serial.println("0");
  178.     request->send(SPIFFS, "/background.jpg", "image/jpeg");
  179.   });
  180.  
  181.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  182.     Serial.println("1");
  183.     request->send(SPIFFS, "/index.html", "text/html");
  184.   });
  185.  
  186.   server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
  187.     StaticJsonDocument<512> doc;
  188.  
  189.     // Populate JSON with user data
  190.     updateData(doc);  
  191.  
  192.     String response;
  193.     serializeJsonPretty(doc, response);
  194.  
  195.     request->send(200, "application/json", response);
  196.   });
  197.  
  198.   // Start the server
  199.   server.begin();
  200. }
  201.  
  202. void loop() {
  203.   // Non-blocking implementations updated the reading functions
  204.   readGPSAndCheckSpeed();
  205.   readCurrent();
  206.   readVoltage();
  207.   readWaterTank();
  208.   readTemp1();
  209.   readTemp2();
  210.   updateMinutesCounter();
  211.   checkRegeneration();
  212. }
  213.  
  214. // Handle updates to appending new data
  215. void updateData(JsonDocument &doc) {
  216.   if (velocita > 0.0) {
  217.     doc["speed"] = String(velocita) + String(F(" km/h"));
  218.   } else {
  219.     doc["speed"] = String(F("ERROR"));
  220.   }
  221.   doc["voltageCommandOut"] = String(F("Tensione uscita comando: ")) + String(voltage_output_value) + String(F("/3.3 [V]"));
  222.   doc["percentageCommandOut"] = String(F("Uscita comando PWM: ")) + String(PWM_output_percentage) + String(F(" [%]"));
  223.   doc["waterTankLevel"] = waterLevelEmpty ? String(F("VUOTO")) : String(F("PIENO"));
  224.   doc["powerOnTime"] = String(powerOnTime) + String(F(" min"));
  225.   doc["current"] = String(current) + String(F(" A"));
  226.   doc["voltage"] = String(voltage) + String(F(" V"));
  227.   doc["temperature1"] = String(temperature1) + String(F(" °C"));
  228.   doc["temperature2"] = String(temperature2) + String(F(" °C"));
  229.   doc["set_timer"] = String(F("Timer : ")) + String(set_timer) + String(F(" min"));
  230. }
  231.  
  232. void checkRegeneration() {
  233.   // Non-blocking check for setting timer adjustment
  234.   if(set_timer == 0) {
  235.     startRegeneration = false;
  236.   }
  237. }
  238.  
  239. ///////////////////////////////////////////////////////////////////////////
  240. // The remaining functions have been converted to non-blocking behaviours..
  241. ///////////////////////////////////////////////////////////////////////////
  242. void readGPSAndCheckSpeed() {
  243.     if(!startRegeneration) {
  244.         while (ss.available()) {
  245.             if (gps.encode(ss.read())) {
  246.                 checkSpeed();
  247.             }
  248.         }
  249.    
  250.         if (millis() > 5000 && gps.charsProcessed() < 10) {
  251.             velocita = -1.0;
  252.             PWM_output_percentage = PWM_output_percentage_0A;
  253.             setCurrentOutput(PWM_output_percentage);
  254.         }
  255.     } else {
  256.         PWM_output_percentage = lookup_phyData_from_voltage(set_current, SEGMENT_POINTS_voltage_Temperature, &(voltage_Temperature_lookup[0][0]));
  257.         setCurrentOutput(PWM_output_percentage);
  258.     }
  259. }
  260.  
  261. void readCurrent() {
  262.     int mA = ACS.mA_DC(30); // 30 acquisizioni di media
  263.     float tempCurrent = float(mA) / 1000;
  264.     current = current * 0.7 + tempCurrent * 0.3;
  265. }
  266.  
  267. void readVoltage() {
  268.     float adc_voltage = 0.0;
  269.     float R1 = 30000.0;
  270.     float R2 = 7500.0;
  271.     float ref_voltage = 3.3;
  272.  
  273.     float adc_value = analogRead(voltage_pin);
  274.     adc_voltage  = (adc_value * ref_voltage) / 4096.0;
  275.     voltage = adc_voltage * (R1 + R2) / R2;
  276. }
  277.  
  278. void readWaterTank() {
  279.     waterLevelEmpty = digitalRead(water_level_pin);
  280. }
  281.  
  282. void readTemp1() {
  283.     sensor1.requestTemperatures(); // Send the command to get temperatures
  284.     temperature1 = sensor1.getTempCByIndex(0);
  285.     if(temperature1 == DEVICE_DISCONNECTED_C) {
  286.         temperature1 = -100; // Indicator for sensor disconnect
  287.     }  
  288. }
  289.  
  290. void readTemp2() {
  291.     sensor2.requestTemperatures(); // Send the command to get temperatures
  292.     temperature2 = sensor2.getTempCByIndex(0);
  293.     if(temperature2 == DEVICE_DISCONNECTED_C) {
  294.         temperature2 = -100; // Indicator for sensor disconnect
  295.     }  
  296. }
  297.  
  298. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement