Advertisement
pleasedontcode

**Sensor Integration** rev_22

Oct 20th, 2024
95
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 Integration**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-20 16:57:49
  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";        // at least 8 characters - 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 TinyGPSPlus object
  49. TinyGPSPlus gps;
  50.  
  51. // Instantiate ACS712 current sensor
  52. ACS712 ACS(current_pin, 3.3, 4095, 40); // pin 34 for current sensor acquisition; sensor power is 3.3V
  53.  
  54. // Instantiate X9C10X digital potentiometer
  55. X9C10X pot(10000);  // 10KΩ - digital potentiometer X9C103S
  56.  
  57. // Setup OneWire and DallasTemperature
  58. OneWire oneWire1(ONE_WIRE_BUS_Temp1);
  59. OneWire oneWire2(ONE_WIRE_BUS_Temp2);
  60. DallasTemperature sensor1(&oneWire1);
  61. DallasTemperature sensor2(&oneWire2);
  62.  
  63. /****** FUNCTION DEFINITIONS *****/
  64. void printWiFiStatus();
  65. void printWebPage();
  66. void checkClientRequest(String currentLine);
  67.  
  68. bool ReplyWebPageContent = true;
  69. bool ReplyResetTime = false;
  70. bool postUpdateData = false;
  71.  
  72. static const int GPSBaud = 9600;
  73. #define ss Serial2
  74.  
  75. unsigned long TEMPO_ATTESA_VISUALIZZAZIONE_VELOCITA = 300;
  76. unsigned long SpeedShowTimer = 0;
  77.  
  78. // Counter to track seconds
  79. unsigned long secondsCounter            = 0;
  80. unsigned long previousMillis            = millis(); // Variable to store the previous millis value
  81. unsigned long currentMillis             = millis();
  82. unsigned long minutesCounter            = 0;
  83.  
  84. // PINS
  85. const int water_level_pin = 18;
  86. const int voltage_pin     = 35;
  87. const int current_pin     = 34;
  88. const int tempSensor1_pin = 33;
  89. const int tempSensor2_pin = 32;
  90. const int ONE_WIRE_BUS_Temp1 = 19; // temperature probe 1
  91. const int ONE_WIRE_BUS_Temp2 = 21; // temperature probe 2
  92. const int DIGPOT_INC      = 4;    // pin INC - X9C103S
  93. const int DIGPOT_UD       = 5;    // pin UD - X9C103S
  94. const int DIGPOT_CS       = 15;   // pin CS - X9C103S
  95.  
  96. #define VELOCITA_SOGLIA_HIGH 75.0 // km/h
  97. #define VELOCITA_SOGLIA_LOW 65.0 // km/h
  98. #define VELOCITA_SOGLIA_0A_HIGH  20.0 // km/h - threshold below 15 km/h turns off current supply
  99. #define VELOCITA_SOGLIA_0A_LOW  10.0 // km/h
  100.  
  101. const float PWM_output_percentage_0A = 0.0; // 0%
  102. const float PWM_output_percentage_9A = 20.0; // 20% --> 1V/5V
  103. const float PWM_output_percentage_5A = 13.0; // 13% --> 0.65V/5V
  104.  
  105. // Global data to transfer
  106. float PWM_output_percentage = 0.0;
  107. float velocita = 0.0;
  108. float voltage_output_value = 0.0;
  109. bool waterLevelEmpty = true;
  110. float voltage = 0.0;
  111. float current = 0.0;
  112. float temperature1 = 0.0;
  113. float temperature2 = 0.0;
  114. unsigned long powerOnTime = 0; // minutes
  115.  
  116. boolean startRegeneration = false;
  117. unsigned int set_current  = 0;
  118. unsigned int set_timer    = 0;
  119.  
  120. // Initialize SPIFFS for file storage
  121. void initSPIFFS() {
  122.   if (!SPIFFS.begin(true)) {
  123.     Serial.println("An error occurred while mounting SPIFFS");
  124.     return;
  125.   }
  126.   Serial.println("SPIFFS mounted successfully");
  127. }
  128.  
  129. void setup() {
  130.   // Start Serial for debugging
  131.   Serial.begin(115200);
  132.   delay(1000);
  133.   Serial.println("ciao");
  134.  
  135.   // Initialize digital potentiometer
  136.   pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS);  //  pulse, direction, select // INC = 4, UD = 5, CS = 15
  137.  
  138.   // NON TOGLIERE - SERVE PER NON FAR SBARELLARE LA MACCHINA
  139.   for(int i=0; i<10;i++) {
  140.     setCurrentOutput(0);
  141.     delay(20);
  142.     setCurrentOutput(100);
  143.     delay(20);
  144.   }
  145.  
  146.   setCurrentOutput(PWM_output_percentage);
  147.   readMinutesCounterFromEEPROM();
  148.  
  149.   ss.begin(GPSBaud);
  150.   Serial.println("ciao2");
  151.  
  152.   sensor1.begin();
  153.   sensor2.begin();
  154.  
  155.   smartDelay(1000);
  156.  
  157.   ACS.setMidPoint(ADC_Offset);
  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.   // Serve the JPEG image
  176.   server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
  177.     Serial.println("0");
  178.     request->send(SPIFFS, "/background.jpg", "image/jpeg");
  179.   });
  180.  
  181.   // Serve the HTML page
  182.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  183.     Serial.println("1");
  184.     request->send(SPIFFS, "/index.html", "text/html");
  185.   });
  186.  
  187.   server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
  188.     // Log post request handling
  189.     //Serial.println("postUpdateData");
  190.  
  191.     // Allocate a temporary JsonDocument
  192.     StaticJsonDocument<512> doc;
  193.  
  194.     // Gather data and populate JSON
  195.     updateData(doc);
  196.  
  197.     // Convert the JSON document to string and send it in response
  198.     String response;
  199.     serializeJsonPretty(doc, response);
  200.  
  201.     request->send(200, "application/json", response);
  202.   });
  203.  
  204.   // Start the server
  205.   server.begin();
  206. }
  207.  
  208. void loop() {
  209.   // No logic in the loop; the server works asynchronously
  210.  
  211.   readGPSAndCheckSpeed();
  212.   readCurrent();
  213.   readVoltage();
  214.   readWaterTank();
  215.   readTemp1();
  216.   readTemp2();
  217.   updateMinutesCounter();
  218.   checkRegeneration();
  219. }
  220.  
  221. // The updateData function to generate the JSON payload
  222. void updateData(JsonDocument &doc) {
  223.   if (velocita > 0.0) {
  224.     doc["speed"] = String(velocita) + String(F(" km/h"));
  225.   } else {
  226.     doc["speed"] = String(F("ERROR"));
  227.   }
  228.   doc["voltageCommandOut"] = String(F("Tensione uscita comando: ")) + String(voltage_output_value) + String(F("/3.3 [V]"));
  229.   doc["percentageCommandOut"] = String(F("Uscita comando PWM: ")) + String(PWM_output_percentage) + String(F(" [%]"));
  230.  
  231.   doc["waterTankLevel"] = waterLevelEmpty ? String(F("VUOTO")) : String(F("PIENO"));
  232.   doc["powerOnTime"] = String(powerOnTime) + String(F(" min"));
  233.   doc["current"] = String(current) + String(F(" A"));
  234.   doc["voltage"] = String(voltage) + String(F(" V"));
  235.   doc["temperature1"] = String(temperature1) + String(F(" °C"));
  236.   doc["temperature2"] = String(temperature2) + String(F(" °C"));
  237.   doc["set_timer"] = String(F("Timer : ")) + String(set_timer) + String(F(" min"));
  238. }
  239.  
  240. // Additional functions remain unchanged...
  241.  
  242. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement