Advertisement
pleasedontcode

**Sensor Integration** rev_17

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: **Sensor Integration**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-20 15:42:39
  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. char ssid[] = "pleasedontcode.com";        // your network SSID (name)
  42. char pass[] = "prova1234";        // your network password (use for WPA, or use as key for WEP)
  43.  
  44. // Create an instance of the web server
  45. AsyncWebServer server(80);
  46.  
  47. // Create instances for sensors and components
  48. TinyGPSPlus gps;
  49. X9C10X pot(10000);  // 10KΩ digital potentiometer
  50. OneWire oneWire1(19); // OneWire bus for temperature sensor 1
  51. OneWire oneWire2(21); // OneWire bus for temperature sensor 2
  52. DallasTemperature sensor1(&oneWire1);
  53. DallasTemperature sensor2(&oneWire2);
  54. ACS712 ACS(current_pin, 3.3, 4095, 40); // Current sensor setup
  55.  
  56. #define VELOCITA_SOGLIA_HIGH 75.0 //km/h
  57. #define VELOCITA_SOGLIA_LOW 65.0 //km/h
  58. #define VELOCITA_SOGLIA_0A_HIGH  20.0 //km/h
  59. #define VELOCITA_SOGLIA_0A_LOW  10.0 //km/h
  60.  
  61. // PINS
  62. const int water_level_pin = 18;
  63. const int voltage_pin     = 35;
  64. const int current_pin     = 34;
  65. const int tempSensor1_pin = 33;
  66. const int tempSensor2_pin = 32;
  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 to transfer
  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. // Initialize SPIFFS for file storage
  91. void initSPIFFS() {
  92.   if (!SPIFFS.begin(true)) {
  93.     Serial.println("An error occurred while mounting SPIFFS");
  94.     return;
  95.   }
  96.   Serial.println("SPIFFS mounted successfully");
  97. }
  98.  
  99. void setup(void)
  100. {
  101.     // Start Serial for debugging
  102.     Serial.begin(115200);
  103.     delay(1000);
  104.     Serial.println("ciao");
  105.  
  106.     // Initialize digital potentiometer
  107.     pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS);  // pulse, direction, select
  108.  
  109.     // Initialize temperature sensors
  110.     sensor1.begin();
  111.     sensor2.begin();
  112.  
  113.     // Initialize ACS712
  114.     ACS.setMidPoint(ADC_Offset);
  115.  
  116.     Serial.println(F("Access Point Web Server"));
  117.  
  118.     // Initialize SPIFFS
  119.     initSPIFFS();
  120.  
  121.     WiFi.mode(WIFI_AP); // Set the ESP32 to access point mode
  122.     if (!WiFi.softAP(ssid, pass)) {
  123.         Serial.println("Soft AP creation failed.");
  124.         while(1);
  125.     }
  126.  
  127.     // Print the IP address of the access point
  128.     IPAddress IP = WiFi.softAPIP();
  129.     Serial.print("Access Point IP Address: ");
  130.     Serial.println(IP);
  131.  
  132.     // Serve the JPEG image
  133.     server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
  134.         Serial.println("0");
  135.         request->send(SPIFFS, "/background.jpg", "image/jpeg");
  136.     });
  137.  
  138.     // Serve the HTML page
  139.     server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  140.         Serial.println("1");
  141.         request->send(SPIFFS, "/index.html", "text/html");
  142.     });
  143.  
  144.     server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
  145.         // Log post request handling
  146.         //Serial.println("postUpdateData");
  147.  
  148.         // Allocate a temporary JsonDocument
  149.         StaticJsonDocument<512> doc;
  150.  
  151.         // Gather data and populate JSON
  152.         updateData(doc);
  153.  
  154.         // Convert the JSON document to string and send it in response
  155.         String response;
  156.         serializeJsonPretty(doc, response);
  157.  
  158.         request->send(200, "application/json", response);
  159.     });
  160.  
  161.     // Start the server
  162.     server.begin();
  163. }
  164.  
  165. void loop(void)
  166. {
  167.     // No logic in the loop; the server works asynchronously
  168.     readGPSAndCheckSpeed();
  169.     readCurrent();
  170.     readVoltage();
  171.     readWaterTank();
  172.     readTemp1();
  173.     readTemp2();
  174.     updateMinutesCounter();
  175.     checkRegeneration();
  176. }
  177.  
  178. // The updateData function to generate the JSON payload
  179. void updateData(JsonDocument &doc) {
  180.     if (velocita > 0.0) {
  181.         doc["speed"] = String(velocita) + String(F(" km/h"));
  182.     } else {
  183.         doc["speed"] = String(F("ERROR"));
  184.     }
  185.     doc["voltageCommandOut"] = String(F("Tensione uscita comando: ")) + String(voltage_output_value) + String(F("/3.3 [V]"));
  186.     doc["percentageCommandOut"] = String(F("Uscita comando PWM: ")) + String(PWM_output_percentage) + String(F(" [%]"));
  187.     doc["waterTankLevel"] = waterLevelEmpty ? String(F("VUOTO")) : String(F("PIENO"));
  188.     doc["powerOnTime"] = String(powerOnTime) + String(F(" min"));
  189.     doc["current"] = String(current) + String(F(" A"));
  190.     doc["voltage"] = String(voltage) + String(F(" V"));
  191.     doc["temperature1"] = String(temperature1) + String(F(" °C"));
  192.     doc["temperature2"] = String(temperature2) + String(F(" °C"));
  193.     doc["set_timer"] = String(F("Timer : ")) + String(set_timer) + String(F(" min"));
  194. }
  195.  
  196. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement