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: # Smart Cannabis Monitor
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2026-01-17 17:50:29
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Gunakan ESP32 AsyncWebServer untuk memantau */
- /* pembacaan encoder putar dan mengontrol parameter */
- /* mekanisme THC dari jarak jauh melalui koneksi */
- /* WebSocket, menyediakan visualisasi pengukur secara */
- /* real-time dan eksekusi perintah dengan umpan balik */
- /* status. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- // SYSTEM REQUIREMENTS:
- // Gunakan ESP32 AsyncWebServer untuk memantau pembacaan encoder putar
- // dan mengontrol parameter mekanisme THC dari jarak jauh melalui koneksi
- // WebSocket, menyediakan visualisasi pengukur secara real-time dan
- // eksekusi perintah dengan umpan balik status.
- #include <EEPROM.h>
- #include "_webserver.h"
- // ============ VARIABLE DECLARATIONS ============
- // EEPROM address variables
- int SetVa, DTa, HySa, StVa;
- // THC Parameters
- int SetV = 100; // Set voltage (0-250V)
- int DT = 5; // Delay time (0.5s = value 5)
- int HyS = 80; // Hysteresis (8.0V = value 80)
- int StV = 100; // Start voltage (100V)
- int ArcV = 0; // Arc voltage (read from sensor)
- // Encoder variables
- int encoderVal = 0; // Current encoder position value
- int oldValue = 0; // Previous encoder value for change detection
- // Program selection
- int program = 1; // Currently selected program (1, 2, or 3)
- const int ParamItem = 4; // Number of parameters per program
- int Param[ParamItem]; // Array to store program parameters
- // LCD menu control
- int menu = 0; // Current menu state
- int pos = 0; // Position in menu
- int show = 0; // LCD update counter
- int LCDtime = 0; // LCD timeout counter
- int defaultLCDtime = 30; // LCD timeout in 50ms intervals (1.5s)
- // Timer and control variables
- esp_timer_handle_t timer_handle;
- bool Do = false; // Timer trigger flag
- // Pin definitions
- const int outputUpPin = 27; // GPIO 27 - Torch UP control
- const int outputDnPin = 26; // GPIO 26 - Torch DOWN control
- const int outputOkPin = 25; // GPIO 25 - Torch OK control
- // WebSocket update timing
- unsigned long lastWebSocketUpdate = 0;
- const unsigned long WEBSOCKET_UPDATE_INTERVAL = 100; // 100ms update interval
- // ============ SETUP FUNCTION ============
- void setup() {
- // Initialize Serial communication
- Serial.begin(115200);
- delay(1000);
- Serial.println("\n\n========== ESP32 THC System Starting ==========");
- // Initialize EEPROM
- EEPROM.begin(128);
- delay(100);
- // Read program selection from EEPROM
- ReadProg();
- // Load selected program parameters
- if (program == 1) {
- doProgramSet(1);
- } else if (program == 2) {
- doProgramSet(2);
- } else {
- doProgramSet(3);
- }
- // Initialize LCD display
- Setup_LCD();
- // Initialize THC control outputs
- Setup_THC();
- // Initialize timer for periodic tasks
- Setup_Timer2();
- // Initialize WiFi Access Point
- startWiFiAP();
- // Initialize Web Server and WebSocket
- setupWebServer();
- Serial.println("Setup complete!");
- Serial.println("Connect to WiFi SSID: " WIFI_SSID);
- Serial.println("Access web interface at: http://<ESP32_IP>");
- Serial.println("==========================================\n");
- }
- // ============ MAIN LOOP ============
- void loop() {
- // Process THC control logic
- doTHC();
- // Update LCD display
- doLCD();
- // Send WebSocket updates at regular intervals
- unsigned long currentTime = millis();
- if (currentTime - lastWebSocketUpdate >= WEBSOCKET_UPDATE_INTERVAL) {
- lastWebSocketUpdate = currentTime;
- // Send encoder update via WebSocket
- sendEncoderUpdate();
- // Send gauge update via WebSocket
- sendGaugeUpdate();
- // Send THC status via WebSocket
- sendTHCStatus();
- }
- // Small delay to prevent watchdog timeout
- delay(10);
- }
- // ============ EEPROM FUNCTIONS ============
- // Initialize EEPROM with default values for all 3 programs
- void Default() {
- // Set parameter addresses for Program 1 as default
- SetVa = 0;
- DTa = 1;
- HySa = 2;
- StVa = 3;
- // Set default parameter values
- SetV = 100; // Set voltage
- DT = 5; // Delay time (0.5s)
- HyS = 80; // Hysteresis (8.0V)
- StV = 100; // Start voltage (100V)
- // Store Program 1 default values at addresses 0-3
- EEPROM.write(0, SetV);
- EEPROM.write(1, DT);
- EEPROM.write(2, HyS);
- EEPROM.write(3, StV);
- // Store Program 2 default values at addresses 4-7
- EEPROM.write(4, SetV);
- EEPROM.write(5, DT);
- EEPROM.write(6, HyS);
- EEPROM.write(7, StV);
- // Store Program 3 default values at addresses 8-11
- EEPROM.write(8, SetV);
- EEPROM.write(9, DT);
- EEPROM.write(10, HyS);
- EEPROM.write(11, StV);
- // Store selected program at address 12 (default to Program 1)
- EEPROM.write(12, 1);
- // Commit changes to EEPROM
- EEPROM.commit();
- }
- // Read the last selected program from EEPROM
- void ReadProg() {
- // Initialize EEPROM access (required for ESP32)
- // Size of 14 bytes covers addresses 0-13
- EEPROM.begin(14);
- // Read the last selected program from address 12
- program = EEPROM.read(12);
- // Validate program value
- if (program < 1 || program > 3) {
- program = 1;
- }
- }
- // Read Program 1 parameters from EEPROM addresses 0-3
- void ReadDataProg_1() {
- for (int j = 0; j < ParamItem; j++) {
- Param[j] = EEPROM.read(j);
- }
- }
- // Read Program 2 parameters from EEPROM addresses 4-7
- void ReadDataProg_2() {
- for (int j = 0; j < ParamItem; j++) {
- Param[j] = EEPROM.read(j + 4);
- }
- }
- // Read Program 3 parameters from EEPROM addresses 8-11
- void ReadDataProg_3() {
- for (int j = 0; j < ParamItem; j++) {
- Param[j] = EEPROM.read(j + 8);
- }
- }
- // Save a single parameter value to EEPROM
- void SaveData(int add, int value) {
- EEPROM.write(add, value);
- EEPROM.commit();
- }
- // Helper function to set program and load its parameters
- void doProgramSet(int prg) {
- if (prg == 1) {
- SetVa = 0;
- DTa = 1;
- HySa = 2;
- StVa = 3;
- ReadDataProg_1();
- } else if (prg == 2) {
- SetVa = 4;
- DTa = 5;
- HySa = 6;
- StVa = 7;
- ReadDataProg_2();
- } else {
- SetVa = 8;
- DTa = 9;
- HySa = 10;
- StVa = 11;
- ReadDataProg_3();
- }
- SaveData(100, prg);
- program = prg;
- SetV = Param[0];
- DT = Param[1];
- HyS = Param[2];
- StV = Param[3];
- // Preset value for encoder
- encoderVal = SetV;
- menu = 0;
- }
- // ============ TIMER CALLBACK ============
- // Timer callback function for periodic task triggering
- void onTimerCallback(void* arg) {
- Do = true;
- }
- // Setup ESP32 timer for periodic callbacks
- void Setup_Timer2() {
- // Setting structure for the timer
- esp_timer_create_args_t timer_args = {
- .callback = &onTimerCallback,
- .name = "thc_periodic_timer"
- };
- // Create the timer
- ESP_ERROR_CHECK(esp_timer_create(&timer_args, &timer_handle));
- // Start the timer. It will repeat every 10,000 microseconds (10ms)
- ESP_ERROR_CHECK(esp_timer_start_periodic(timer_handle, 10000));
- }
- // ============ THC CONTROL LOGIC ============
- unsigned int delayTime;
- unsigned int SetVx10;
- // Initialize THC control pins
- void Setup_THC() {
- pinMode(outputUpPin, OUTPUT); // GPIO 27 - Torch UP
- pinMode(outputOkPin, OUTPUT); // GPIO 25 - Torch OK
- pinMode(outputDnPin, OUTPUT); // GPIO 26 - Torch DOWN
- // Ensure all outputs are initially OFF
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputOkPin, LOW);
- digitalWrite(outputDnPin, LOW);
- }
- // Main THC control function - monitors arc voltage and adjusts torch height
- void doTHC() {
- if (Do) {
- Do = false;
- LCDtime++;
- show++;
- // Reset to default menu if timeout occurs
- if (LCDtime > defaultLCDtime) {
- menu = 0;
- pos = 0;
- LCDtime = 0;
- encoderVal = SetV;
- }
- // Check if arc voltage is within valid range (500mV to 2500mV)
- if ((500 < ArcV) && (ArcV < 2500)) {
- // Increment delay counter while waiting for arc stabilization
- if (ArcV > StV * 10) {
- delayTime++;
- }
- // Once delay threshold is reached, enable torch control
- if (delayTime >= DT * 10) {
- SetVx10 = SetV * 10;
- delayTime = DT * 10;
- // Signal that arc is OK
- digitalWrite(outputOkPin, HIGH);
- // Hysteresis control - move torch up if voltage too high
- if (ArcV >= SetVx10 + HyS) {
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputDnPin, HIGH);
- }
- // Hysteresis control - move torch down if voltage too low
- else if (ArcV <= SetVx10 - HyS) {
- digitalWrite(outputDnPin, LOW);
- digitalWrite(outputUpPin, HIGH);
- }
- // Voltage is within acceptable range - hold position
- else {
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputDnPin, LOW);
- }
- }
- }
- // Arc voltage out of range or not in test mode - turn off all outputs
- else if (menu != 12) {
- delayTime = 0;
- digitalWrite(outputUpPin, LOW);
- digitalWrite(outputOkPin, LOW);
- digitalWrite(outputDnPin, LOW);
- }
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment