Advertisement
pleasedontcode

**Sensor Server** rev_43

Oct 22nd, 2024
55
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 Server**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-22 09:37:41
  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 <SoftwareSerial.h> // SoftwareSerial is not compatible with ESP32, use hardware serial instead
  28. #include "ACS712.h"
  29. #include <WiFi.h>
  30. #include <ESPAsyncWebServer.h>
  31. #include <SPIFFS.h>
  32. #include <ArduinoJson.h>
  33. #include <EEPROM.h>
  34. #include <OneWire.h>
  35. #include <DallasTemperature.h>
  36. #include "X9C10X.h"
  37.  
  38. /****** FUNCTION PROTOTYPES *****/
  39. void setup(void);
  40. void loop(void);
  41.  
  42. // Access Point credentials
  43. char ssid[] = "pleasedontcode.com"; // your network SSID (name)
  44. char pass[] = "prova1234"; // your network password (use for WPA, or use as key for WEP)
  45.  
  46. // Create an instance of the web server
  47. AsyncWebServer server(80);
  48.  
  49. TinyGPSPlus gps;
  50.  
  51. // Pin configuration
  52. #define VELOCITA_SOGLIA_HIGH 75.0 // km/h
  53. #define VELOCITA_SOGLIA_LOW 65.0 // km/h
  54. #define VELOCITA_SOGLIA_0A_HIGH 20.0 // km/h
  55. #define VELOCITA_SOGLIA_0A_LOW 10.0 // km/h
  56.  
  57. // PINS
  58. const int water_level_pin = 18;
  59. const int voltage_pin = 35;
  60. const int current_pin = 34;
  61. const int tempSensor1_pin = 33;
  62. const int tempSensor2_pin = 32;
  63. const int ONE_WIRE_BUS_Temp1 = 19; // sonda temp 1
  64. const int ONE_WIRE_BUS_Temp2 = 21; // sonda temp 2
  65. const int DIGPOT_INC = 4; // pin INC - X9C103S
  66. const int DIGPOT_UD = 5; // pin UD - X9C103S
  67. const int DIGPOT_CS = 15; // pin CS - X9C103S
  68.  
  69. const float PWM_output_percentage_0A = 0.0; // 0%
  70. const float PWM_output_percentage_9A = 20.0; // 20%
  71. const float PWM_output_percentage_5A = 13.0; // 13%
  72.  
  73. // Global variables for sensor data
  74. float PWM_output_percentage = 0.0;
  75. float velocita = 0.0;
  76. float voltage_output_value = 0.0;
  77. bool waterLevelEmpty = true;
  78. float voltage = 0.0;
  79. float current = 0.0;
  80. float temperature1 = 0.0;
  81. float temperature2 = 0.0;
  82. unsigned long powerOnTime = 0; // minutes
  83.  
  84. boolean startRegeneration = false;
  85. unsigned int set_current = 0;
  86. unsigned int set_timer = 0;
  87.  
  88. // ACS712 configuration
  89. unsigned int ADC_Offset = 1930;
  90. ACS712 ACS(current_pin, 3.3, 4095, 40); // pin 34 for current sensor acquisition
  91.  
  92. X9C10X pot(10000); // 10KΩ - digital potentiometer X9C103S
  93.  
  94. // Function prototypes for setup logic
  95. void printWiFiStatus();
  96. void printWebPage();
  97. void checkClientRequest(String currentLine);
  98.  
  99. // HTTP server response states
  100. bool ReplyWebPageContent = true;
  101. bool ReplyResetTime = false;
  102. bool postUpdateData = false;
  103.  
  104. static const int GPSBaud = 9600;
  105. #define ss Serial2 // Use Serial2 for GPS communication
  106.  
  107. unsigned long TEMPO_ATTESA_VISUALIZZAZIONE_VELOCITA = 300;
  108. unsigned long SpeedShowTimer = 0;
  109.  
  110. // Timer counters
  111. unsigned long secondsCounter            = 0;
  112. unsigned long previousMillis            = millis(); // Variable to store the previous millis value
  113. unsigned long currentMillis             = millis();
  114. unsigned long minutesCounter            = 0;
  115.  
  116. // Setup OneWire instances for temperature sensors
  117. OneWire oneWire1(ONE_WIRE_BUS_Temp1);
  118. OneWire oneWire2(ONE_WIRE_BUS_Temp2);
  119.  
  120. // Setup Dallas Temperature Library
  121. DallasTemperature sensor1(&oneWire1);
  122. DallasTemperature sensor2(&oneWire2);
  123.  
  124. // Characteristic curves for ADC inputs
  125. const uint8_t SEGMENT_POINTS_voltage_Temperature = 10;
  126. const float voltage_Temperature_lookup[2][SEGMENT_POINTS_voltage_Temperature] =
  127. {
  128.     {0.0, 1.2, 1.9, 5.0, 10.0, 14.5, 17.0, 20.0, 30.0, 35.0}, // voltage [V]
  129.     {0.0, 5.0, 7.0, 13.0, 22.0, 29.0, 33.0, 37.0, 56.0, 65.0} // temperature [°C]
  130. };
  131.  
  132. // Initialize SPIFFS for file storage
  133. void initSPIFFS() {
  134.   if (!SPIFFS.begin(true)) {
  135.     Serial.println("An error occurred while mounting SPIFFS.");
  136.     return;
  137.   }
  138.   Serial.println("SPIFFS mounted successfully.");
  139. }
  140.  
  141. void setup() {
  142.   Serial.begin(115200); // Start Serial for debugging
  143.  
  144.   delay(1000);
  145.   Serial.println("ciao");
  146.  
  147.   pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS); // Initialize digital pot
  148.  
  149.   for (int i=0; i<10; i++)
  150.   {
  151.     setCurrentOutput(0);
  152.     delay(20);
  153.     setCurrentOutput(100);
  154.     delay(20);
  155.   }
  156.  
  157.   setCurrentOutput(PWM_output_percentage);
  158.   readMinutesCounterFromEEPROM();
  159.  
  160.   ss.begin(GPSBaud);
  161.   Serial.println("ciao2");
  162.  
  163.   sensor1.begin();
  164.   sensor2.begin();
  165.  
  166.   smartDelay(1000);
  167.  
  168.   ACS.setMidPoint(ADC_Offset);
  169.  
  170.   Serial.println(F("Access Point Web Server"));
  171.  
  172.   initSPIFFS(); // Initialize SPIFFS
  173.  
  174.   WiFi.mode(WIFI_AP); // Set ESP32 to access point mode
  175.   if (!WiFi.softAP(ssid, pass)) {
  176.     Serial.println("Soft AP creation failed.");
  177.     while(1);
  178.   }
  179.  
  180.   // Print the IP address of the access point
  181.   IPAddress IP = WiFi.softAPIP();
  182.   Serial.print("Access Point IP Address: ");
  183.   Serial.println(IP);
  184.  
  185.   // Web server routes
  186.   server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request) {
  187.     request->send(SPIFFS, "/background.jpg", "image/jpeg");
  188.   });
  189.  
  190.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
  191.     request->send(SPIFFS, "/index.html", "text/html");
  192.   });
  193.  
  194.   server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
  195.     // Create JSON document
  196.     StaticJsonDocument<512> doc;
  197.     updateData(doc); // Fill JSON with updated data
  198.     String response;
  199.     serializeJsonPretty(doc, response); // Convert to string
  200.     request->send(200, "application/json", response);
  201.   });
  202.  
  203.   server.begin(); // Start the server
  204. }
  205.  
  206. void loop() {
  207.   readGPSAndCheckSpeed();
  208.   readCurrent();
  209.   readVoltage();
  210.   readWaterTank();
  211.   readTemp1();
  212.   readTemp2();
  213.   updateMinutesCounter();
  214.   checkRegeneration();
  215. }
  216.  
  217. // Function to send data as JSON response
  218. void updateData(JsonDocument &doc) {
  219.   if (velocita > 0.0) {
  220.     doc["speed"] = String(velocita) + String(F(" km/h"));
  221.   } else {
  222.     doc["speed"] = String(F("ERROR"));
  223.   }
  224.   doc["voltageCommandOut"] = String(F("Tensione uscita comando: ")) + String(voltage_output_value) + String(F("/3.3 [V]"));
  225.   doc["percentageCommandOut"] = String(F("Uscita comando PWM: ")) + String(PWM_output_percentage) + String(F(" [%]"));
  226.  
  227.   doc["waterTankLevel"] = waterLevelEmpty ? String(F("VUOTO")) : String(F("PIENO"));
  228.   doc["powerOnTime"] = String(powerOnTime) + String(F(" min"));
  229.   doc["current"] = String(current) + String(F(" A"));
  230.   doc["voltage"] = String(voltage) + String(F(" V"));
  231.   doc["temperature1"] = String(temperature1) + String(F(" °C"));
  232.   doc["temperature2"] = String(temperature2) + String(F(" °C"));
  233.   doc["set_timer"] = String(F("Timer : ")) + String(set_timer) + String(F(" min"));
  234. }
  235.  
  236. void checkRegeneration() {
  237.   if(set_timer == 0) {
  238.     startRegeneration = false;
  239.   }
  240. }
  241.  
  242. // Reading functions continue..
  243. void readMinutesCounterFromEEPROM() {
  244.   unsigned long max_minutesCounter = 0UL - 1UL;
  245.   EEPROM.begin(sizeof(powerOnTime));
  246.   EEPROM.get(0, powerOnTime);
  247.  
  248.   if (powerOnTime == max_minutesCounter) {
  249.     powerOnTime = 0;
  250.   }
  251.  
  252.   Serial.print("powerOnTime: ");
  253.   Serial.println(powerOnTime);
  254. }
  255.  
  256. void updateMinutesCounter() {
  257.   currentMillis = millis();
  258.   if ((currentMillis - previousMillis) > 1000) {
  259.     previousMillis = currentMillis;
  260.     secondsCounter++;
  261.     if (secondsCounter % 60 == 0) {
  262.       secondsCounter = 0;
  263.       minutesCounter++;
  264.       powerOnTime++;
  265.       if (minutesCounter % 10 == 0) { // Save counter every 10 minutes
  266.         saveMinutesCounterInEEPROM();
  267.       }
  268.       if (set_timer > 0) {
  269.         set_timer--;
  270.       }
  271.     }
  272.   }
  273. }
  274.  
  275. void saveMinutesCounterInEEPROM() {
  276.   EEPROM.put(0, powerOnTime);
  277.   EEPROM.commit();
  278. }
  279.  
  280. void readGPSAndCheckSpeed() {
  281.   while (ss.available() > 0) {
  282.     if (gps.encode(ss.read())) {
  283.       checkSpeed();
  284.     }
  285.   }
  286. }
  287.  
  288. void checkSpeed() {
  289.   // Insert timed sensor reading logic
  290. }
  291.  
  292. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement