Advertisement
pleasedontcode

**Sensor Server** rev_24

Oct 20th, 2024
75
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-20 23:11: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. /****** 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 library objects
  49. TinyGPSPlus gps;
  50. ACS712 ACS(current_pin, 3.3, 4095, 40); // pin 34 for current sensor acquisition; sensor power is 3.3V
  51. X9C10X pot(10000);  // 10KΩ - digital potentiometer X9C103S
  52.  
  53. // Other global variables and constants remain unchanged...
  54.  
  55. void setup() {
  56.   // Start Serial for debugging
  57.   Serial.begin(115200);
  58.  
  59.   delay(1000);
  60.   Serial.println("ciao");
  61.  
  62.   pot.begin(DIGPOT_INC, DIGPOT_UD, DIGPOT_CS);  // pulse, direction, select // INC = 4, UD = 5, CS = 15
  63.  
  64.   // Prevent machine from malfunctioning
  65.   for(int i=0; i<10; i++) {
  66.     setCurrentOutput(0);
  67.     delay(20);
  68.     setCurrentOutput(100);
  69.     delay(20);
  70.   }
  71.  
  72.   setCurrentOutput(PWM_output_percentage);
  73.   readMinutesCounterFromEEPROM();
  74.  
  75.   ss.begin(GPSBaud);
  76.   Serial.println("ciao2");
  77.  
  78.   sensor1.begin();
  79.   sensor2.begin();
  80.  
  81.   smartDelay(1000);
  82.  
  83.   ACS.setMidPoint(ADC_Offset);
  84.  
  85.   Serial.println(F("Access Point Web Server"));
  86.  
  87.   // Initialize SPIFFS
  88.   initSPIFFS();
  89.  
  90.   WiFi.mode(WIFI_AP); // Set the ESP32 to access point mode
  91.   if (!WiFi.softAP(ssid, pass)) {
  92.     Serial.println("Soft AP creation failed.");
  93.     while(1);
  94.   }
  95.  
  96.   // Print the IP address of the access point
  97.   IPAddress IP = WiFi.softAPIP();
  98.   Serial.print("Access Point IP Address: ");
  99.   Serial.println(IP);
  100.  
  101.   // Serve the JPEG image
  102.   server.on("/background.jpg", HTTP_GET, [](AsyncWebServerRequest *request){
  103.     Serial.println("0");
  104.     request->send(SPIFFS, "/background.jpg", "image/jpeg");
  105.   });
  106.  
  107.   // Serve the HTML page
  108.   server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  109.     Serial.println("1");
  110.     request->send(SPIFFS, "/index.html", "text/html");
  111.   });
  112.  
  113.   server.on("/UPDATEDATA", HTTP_POST, [](AsyncWebServerRequest *request) {
  114.     // Log post request handling
  115.  
  116.     // Allocate a temporary JsonDocument
  117.     StaticJsonDocument<512> doc;
  118.  
  119.     // Gather data and populate JSON
  120.     updateData(doc);
  121.  
  122.     // Convert the JSON document to string and send it in response
  123.     String response;
  124.     serializeJsonPretty(doc, response);
  125.  
  126.     request->send(200, "application/json", response);
  127.   });
  128.  
  129.   // Start the server
  130.   server.begin();
  131. }
  132.  
  133. void loop() {
  134.   // No logic in the loop; the server works asynchronously
  135.  
  136.   readGPSAndCheckSpeed();
  137.   readCurrent();
  138.   readVoltage();
  139.   readWaterTank();
  140.   readTemp1();
  141.   readTemp2();
  142.   updateMinutesCounter();
  143.   checkRegeneration();
  144. }
  145.  
  146. // The updateData function to generate the JSON payload
  147. void updateData(JsonDocument &doc) {
  148.   if (velocita > 0.0) {
  149.     doc["speed"] = String(velocita) + String(F(" km/h"));
  150.   } else {
  151.     doc["speed"] = String(F("ERROR"));
  152.   }
  153.   doc["voltageCommandOut"] = String(F("Tensione uscita comando: ")) + String(voltage_output_value) + String(F("/3.3 [V]"));
  154.   doc["percentageCommandOut"] = String(F("Uscita comando PWM: ")) + String(PWM_output_percentage) + String(F(" [%]"));
  155.  
  156.   doc["waterTankLevel"] = waterLevelEmpty ? String(F("VUOTO")) : String(F("PIENO"));
  157.   doc["powerOnTime"] = String(powerOnTime) + String(F(" min"));
  158.   doc["current"] = String(current) + String(F(" A"));
  159.   doc["voltage"] = String(voltage) + String(F(" V"));
  160.   doc["temperature1"] = String(temperature1) + String(F(" °C"));
  161.   doc["temperature2"] = String(temperature2) + String(F(" °C"));
  162.   doc["set_timer"] = String(F("Timer : ")) + String(set_timer) + String(F(" min"));
  163. }
  164.  
  165. // Other functions remain unchanged...
  166.  
  167. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement