Advertisement
pleasedontcode

**Sensor Initialization** rev_18

Oct 20th, 2024
103
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 Initialization**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-10-20 15:57:00
  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. // Instantiate library objects
  38. TinyGPSPlus gps; // GPS object
  39. ACS712 ACS(A0, 5.0, 1023, 100); // Current sensor object
  40. AsyncWebServer server(80); // Web server object
  41. OneWire oneWire(2); // OneWire object for temperature sensors
  42. DallasTemperature sensors(&oneWire); // Temperature sensor object
  43.  
  44. // Define constants
  45. const char* ssid = "your-ssid"; // WiFi SSID
  46. const char* pass = "your-password"; // WiFi password
  47. const int GPSBaud = 9600; // GPS baud rate
  48. const int ADC_Offset = 512; // ADC offset for ACS712
  49. const int PWM_output_percentage = 50; // PWM output percentage
  50. const int DIGPOT_INC = 4; // Digital potentiometer INC pin
  51. const int DIGPOT_UD = 5; // Digital potentiometer U/D pin
  52. const int DIGPOT_CS = 15; // Digital potentiometer CS pin
  53.  
  54. /****** FUNCTION PROTOTYPES *****/
  55. void setup(void);
  56. void loop(void);
  57. void updateData(JsonDocument &doc);
  58. void initSPIFFS();
  59. void readMinutesCounterFromEEPROM();
  60. void setCurrentOutput(int output);
  61. void readGPSAndCheckSpeed();
  62. void readCurrent();
  63. void readVoltage();
  64. void readWaterTank();
  65. void readTemp1();
  66. void readTemp2();
  67. void updateMinutesCounter();
  68. void checkRegeneration();
  69.  
  70. void setup(void)
  71. {
  72.     // put your setup code here, to run once:
  73.     // Start Serial for debugging
  74.     Serial.begin(115200);
  75.     delay(1000);
  76.     Serial.println("ciao");
  77.  
  78.     // Initialize digital potentiometer
  79.     pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS);  //  pulse, direction, select
  80.  
  81.     //NON TOGLIERE - SERVE PER NON FAR SBARELLARE LA MACCHINA
  82.     for(int i=0; i<10;i++)
  83.     {
  84.         setCurrentOutput(0);
  85.         delay(20);
  86.         setCurrentOutput(100);
  87.         delay(20);
  88.     }
  89.  
  90.     setCurrentOutput(PWM_output_percentage);
  91.     readMinutesCounterFromEEPROM();
  92.  
  93.     // Initialize GPS
  94.     ss.begin(GPSBaud);
  95.     Serial.println("ciao2");
  96.  
  97.     // Initialize temperature sensors
  98.     sensors.begin();
  99.  
  100.     // Delay for GPS to stabilize
  101.     smartDelay(1000);
  102.  
  103.     // Set ACS712 midpoint
  104.     ACS.setMidPoint(ADC_Offset);
  105.  
  106.     Serial.println(F("Access Point Web Server"));
  107.  
  108.     // Initialize SPIFFS
  109.     initSPIFFS();
  110.  
  111.     // Set up WiFi access point
  112.     WiFi.mode(WIFI_AP); // Set the ESP32 to access point mode
  113.     if (!WiFi.softAP(ssid, pass)) {
  114.         Serial.println("Soft AP creation failed.");
  115.         while(1);
  116.     }
  117.  
  118.     // Print the IP address of the access point
  119.     IPAddress IP = WiFi.softAPIP();
  120.     Serial.print("Access Point IP Address: ");
  121.     Serial.println(IP);
  122.  
  123.     // Serve the JPEG image
  124.     server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
  125.         Serial.println("0");
  126.         request->send(SPIFFS, "/background.jpg", "image/jpeg");
  127.     });
  128.  
  129.     // Serve the HTML page
  130.     server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  131.         Serial.println("1");
  132.         request->send(SPIFFS, "/index.html", "text/html");
  133.     });
  134.  
  135.     server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
  136.         // Log post request handling
  137.  
  138.         // Allocate a temporary JsonDocument
  139.         StaticJsonDocument<512> doc;
  140.  
  141.         // Gather data and populate JSON
  142.         updateData(doc);
  143.  
  144.         // Convert the JSON document to string and send it in response
  145.         String response;
  146.         serializeJsonPretty(doc, response);
  147.  
  148.         request->send(200, "application/json", response);
  149.     });
  150.  
  151.     // Start the server
  152.     server.begin();
  153. }
  154.  
  155. void loop(void)
  156. {
  157.     // No logic in the loop; the server works asynchronously
  158.     readGPSAndCheckSpeed();
  159.     readCurrent();
  160.     readVoltage();
  161.     readWaterTank();
  162.     readTemp1();
  163.     readTemp2();
  164.     updateMinutesCounter();
  165.     checkRegeneration();
  166. }
  167.  
  168. // The updateData function to generate the JSON payload
  169. void updateData(JsonDocument &doc) {
  170.     if (velocita > 0.0) {
  171.         doc["speed"] = String(velocita) + String(F(" km/h"));
  172.     } else {
  173.         doc["speed"] = String(F("ERROR"));
  174.     }
  175.     doc["voltageCommandOut"] = String(F("Tensione uscita comando: ")) + String(voltage_output_value) + String(F("/3.3 [V]"));
  176.     doc["percentageCommandOut"] = String(F("Uscita comando PWM: ")) + String(PWM_output_percentage) + String(F(" [%]"));
  177.  
  178.     doc["waterTankLevel"] = waterLevelEmpty ? String(F("VUOTO")) : String(F("PIENO"));
  179.     doc["powerOnTime"] = String(powerOnTime) + String(F(" min"));
  180.     doc["current"] = String(current) + String(F(" A"));
  181.     doc["voltage"] = String(voltage) + String(F(" V"));
  182.     doc["temperature1"] = String(temperature1) + String(F(" °C"));
  183.     doc["temperature2"] = String(temperature2) + String(F(" °C"));
  184.     doc["set_timer"] = String(F("Timer : ")) + String(set_timer) + String(F(" min"));
  185. }
  186.  
  187. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement