Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Sensor Integration**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-10-20 15:42:39
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* merge code. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <TinyGPSPlus.h>
- #include "ACS712.h"
- #include <WiFi.h>
- #include <ESPAsyncWebServer.h>
- #include <SPIFFS.h>
- #include <ArduinoJson.h>
- #include <EEPROM.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include "X9C10X.h"
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- char ssid[] = "pleasedontcode.com"; // your network SSID (name)
- char pass[] = "prova1234"; // your network password (use for WPA, or use as key for WEP)
- // Create an instance of the web server
- AsyncWebServer server(80);
- // Create instances for sensors and components
- TinyGPSPlus gps;
- X9C10X pot(10000); // 10KΩ digital potentiometer
- OneWire oneWire1(19); // OneWire bus for temperature sensor 1
- OneWire oneWire2(21); // OneWire bus for temperature sensor 2
- DallasTemperature sensor1(&oneWire1);
- DallasTemperature sensor2(&oneWire2);
- ACS712 ACS(current_pin, 3.3, 4095, 40); // Current sensor setup
- #define VELOCITA_SOGLIA_HIGH 75.0 //km/h
- #define VELOCITA_SOGLIA_LOW 65.0 //km/h
- #define VELOCITA_SOGLIA_0A_HIGH 20.0 //km/h
- #define VELOCITA_SOGLIA_0A_LOW 10.0 //km/h
- // PINS
- const int water_level_pin = 18;
- const int voltage_pin = 35;
- const int current_pin = 34;
- const int tempSensor1_pin = 33;
- const int tempSensor2_pin = 32;
- const int DIGPOT_INC = 4; // pin INC - X9C103S
- const int DIGPOT_UD = 5; // pin UD - X9C103S
- const int DIGPOT_CS = 15; // pin CS - X9C103S
- const float PWM_output_percentage_0A = 0.0; //0%
- const float PWM_output_percentage_9A = 20.0; //20% --> 1V/5V
- const float PWM_output_percentage_5A = 13.0; //13% --> 0,65V/5V
- // Global data to transfer
- float PWM_output_percentage = 0.0;
- float velocita = 0.0;
- float voltage_output_value = 0.0;
- bool waterLevelEmpty = true;
- float voltage = 0.0;
- float current = 0.0;
- float temperature1 = 0.0;
- float temperature2 = 0.0;
- unsigned long powerOnTime = 0; //minutes
- boolean startRegeneration = false;
- unsigned int set_current = 0;
- unsigned int set_timer = 0;
- // Initialize SPIFFS for file storage
- void initSPIFFS() {
- if (!SPIFFS.begin(true)) {
- Serial.println("An error occurred while mounting SPIFFS");
- return;
- }
- Serial.println("SPIFFS mounted successfully");
- }
- void setup(void)
- {
- // Start Serial for debugging
- Serial.begin(115200);
- delay(1000);
- Serial.println("ciao");
- // Initialize digital potentiometer
- pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS); // pulse, direction, select
- // Initialize temperature sensors
- sensor1.begin();
- sensor2.begin();
- // Initialize ACS712
- ACS.setMidPoint(ADC_Offset);
- Serial.println(F("Access Point Web Server"));
- // Initialize SPIFFS
- initSPIFFS();
- WiFi.mode(WIFI_AP); // Set the ESP32 to access point mode
- if (!WiFi.softAP(ssid, pass)) {
- Serial.println("Soft AP creation failed.");
- while(1);
- }
- // Print the IP address of the access point
- IPAddress IP = WiFi.softAPIP();
- Serial.print("Access Point IP Address: ");
- Serial.println(IP);
- // Serve the JPEG image
- server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
- Serial.println("0");
- request->send(SPIFFS, "/background.jpg", "image/jpeg");
- });
- // Serve the HTML page
- server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
- Serial.println("1");
- request->send(SPIFFS, "/index.html", "text/html");
- });
- server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
- // Log post request handling
- //Serial.println("postUpdateData");
- // Allocate a temporary JsonDocument
- StaticJsonDocument<512> doc;
- // Gather data and populate JSON
- updateData(doc);
- // Convert the JSON document to string and send it in response
- String response;
- serializeJsonPretty(doc, response);
- request->send(200, "application/json", response);
- });
- // Start the server
- server.begin();
- }
- void loop(void)
- {
- // No logic in the loop; the server works asynchronously
- readGPSAndCheckSpeed();
- readCurrent();
- readVoltage();
- readWaterTank();
- readTemp1();
- readTemp2();
- updateMinutesCounter();
- checkRegeneration();
- }
- // The updateData function to generate the JSON payload
- void updateData(JsonDocument &doc) {
- if (velocita > 0.0) {
- doc["speed"] = String(velocita) + String(F(" km/h"));
- } else {
- doc["speed"] = String(F("ERROR"));
- }
- doc["voltageCommandOut"] = String(F("Tensione uscita comando: ")) + String(voltage_output_value) + String(F("/3.3 [V]"));
- doc["percentageCommandOut"] = String(F("Uscita comando PWM: ")) + String(PWM_output_percentage) + String(F(" [%]"));
- doc["waterTankLevel"] = waterLevelEmpty ? String(F("VUOTO")) : String(F("PIENO"));
- doc["powerOnTime"] = String(powerOnTime) + String(F(" min"));
- doc["current"] = String(current) + String(F(" A"));
- doc["voltage"] = String(voltage) + String(F(" V"));
- doc["temperature1"] = String(temperature1) + String(F(" °C"));
- doc["temperature2"] = String(temperature2) + String(F(" °C"));
- doc["set_timer"] = String(F("Timer : ")) + String(set_timer) + String(F(" min"));
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement